MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Subscripted variables and function definitions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg65265] Re: Subscripted variables and function definitions
  • From: Peter Pein <petsie at dordos.net>
  • Date: Thu, 23 Mar 2006 06:58:23 -0500 (EST)
  • References: <dvdida$a26$1@smc.vnet.net> <dve4na$ffg$1@smc.vnet.net> <dvgskl$9rc$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Geico Caveman schrieb:
...
> Actually, that was a typo in my post. I am using X[[1]]/(X[[3]]+X[[4]]) and
> I get what I described.
> 
> Any ideas ?
> 

Hi Geico,

you propably want to Evaluate[] the definition:

In[1]:= X = {x1, x2, x3, x4};
In[2]:= f[x1_, x2_, x3_, x4_] := X[[1]]/(X[[3]] + X[[4]])
In[3]:= f[1, 1, 1, 1]
Out[3]= x1/(x3 + x4)

In[4]:= g[x1_, x2_, x3_, x4_] := Evaluate[X[[1]]/(X[[3]] + X[[4]])]
In[5]:= g[1, 1, 1, 1]
Out[5]= 1/2

In[6]:= h[x1_, x2_, x3_, x4_] = X[[1]]/(X[[3]] + X[[4]])
Out[6]= x1/(x3 + x4)
In[7]:= h[1, 1, 1, 1]
Out[7]= 1/2

If you call f[1,1,1,1], Mathematica looks for occurances of x1..x4 in your definition of f and doesn't find any, because f is defined with SetDelayed (":=").
_After_ this it takes X[[1]]/(X[[3]] + X[[4]]) and evaluates this expression.

g is defined using SetDelayed too, but now Mathematica finds the patterns x1..x4 in the expression, replaces them by actual values and evaluates the result.

h has been defined using Set. Set Evaluates its second argument _before_ assigning the result to a name.

To see how Mathematica stores these three functions, enter ?f or ?g or ?h.

A look at "The Mathematica Book" 2.5.8 might help too.


Hope this helps,
   Peter


  • Prev by Date: Re: A question concerning Show and PlotLegend
  • Next by Date: Re: How to sample a 2-dim. r.v. with known density function?
  • Previous by thread: Re: Subscripted variables and function definitions
  • Next by thread: Is this a BUG of D[f, var1, ..., NonConstants -> {u1, ...}] ?