MathGroup Archive 1995

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

Search the Archive

Re: Equating a variable and its value

  • To: mathgroup at
  • Subject: [mg1231] Re: Equating a variable and its value
  • From: villegas (Robert Villegas)
  • Date: Fri, 26 May 1995 03:59:59 -0400
  • Organization: Wolfram Research, Inc.

> Hello folks.
> Study this codelet:
> m=3; mult=Times[#,m]&; Clear[m]; mult[10]
> To my dismay, I get 10 m, not 30.
> How can i convince Mathematica that only pedants make the distinction
> between a variable and its value?

Hello Jan,

You might be surprised, but the distinction between a variable and
its value truly is an important, practical one, not a pedantic one.

First, though, a way to get what you want:

In[1]:= m = 3

Out[1]= 3

In[2]:= mult = With[{m = m}, Times[#, m]& ]

Out[2]= #1 3 & 

In[3]:= Clear[m]

In[4]:= mult[10]

Out[4]= 30

The useful construct here is With:  a way to replace symbols with values
throughout an expression.  The first argument of With is a list of
constants to be enforced on the body of the With (the second argument).
The m = m probably looks strange at first, but it makes sense.  Any of
the local constants you declare in With has the form

symbol = value

The left side is the symbol to find and replace in the body of the
With.  The right side is the value to substitute whereever the
symbol is found, and it can be any expression, not just a raw
constant like a number or string.  If the right side is an expression,
it is evaluated before the With begins any work, and if it
contains variables with global values, those are used.

In your example, you want to substitute the global value of m for
the symbol m.  The assignment m = m makes the local constant m take
on the value of the global m.

    local name       value
    ==========      =======
         m       =     m

See section 2.6.2 of the Mathematica book (2nd edition) or section 2.6.2
of _Programming in Mathematica_ by Roman Maeder, for a discussion of With.

Another way to get what you want is with /. (ReplaceAll), which is more

In[21]:= Clear[m]

In[22]:= m = 3

Out[22]= 3

In[23]:= mult = Times[#, m]& /. Literal[m] -> m

Out[23]= #1 3 & 

In[24]:= Clear[m]

In[25]:= mult[10]

Out[25]= 30

   So why does Mathematica distinguish between a variable and its

It's not rare that at the time a function is formulated, some of
the parameters have values that are (or can be) different from what
they will be when the function is used later on.  A function could
depend on a global variable that the user can reset if needed.  For
instance, a data plotter might contain a reference to $InstrumentError
or some such parameter that is fixed for any set of computations with
one instrument, but can be reset if computations are redone with data
from a different instrument.

Here's a shorter example:  a quadratic equation where the coefficients
a, b, and c are variables a student could reset:

In[26]:= quadratic = (a #^2 + b # + c)&

Out[26]= a #1  + b #1 + c & 

In[27]:= {a, b, c} = {5, 3, 1};

In[28]:= quadratic[x]

Out[28]= 1 + 3 x + 5 x

In[29]:= {a, b, c} = {-2, 7, -4};

In[30]:= quadratic[x]            

Out[30]= -4 + 7 x - 2 x

The same thing applies to the more typical type of function assignment:

In[31]:= q[x_] := a x^2 + b x + c

In[32]:= q[y]

Out[32]= -4 + 7 y - 2 y

Or the global variable could be one of Mathematica's environment
variables.  For instance, $DisplayFunction is a function that contains
another environment variable $Display which gives the output channels
for PostScript code.

In[131]:= ?? $DisplayFunction
$DisplayFunction gives the default setting for the option DisplayFunction in
   graphics functions.

$DisplayFunction = Display[$Display, #1] & 

Since it contains the variable name $Display, not the value it had when
$DisplayFunction was first defined, the user can reset $Display
during the session and the new value will be used.  The function isn't
stuck with the original value of $Display forever.

If you have a function that prints some information or sends it to a
file, it might politely use $PrePrint to format the output as
set by that user (e.g. the recent blind user might set it to InputForm).
These variables shouldn't be evaluated until the function is called
(if they were, they would be your current values even for future users).

Another situation where a variable's value is not desirable in the
function is when the function acts on the variable itself.  The
function might increment a counter (++c) or accumulate values in a
list (AppendTo[results, newelement]).

Here we stash an instruction in the function to increment c, so we
can find out how many iterations of the function were needed to reach
a fixed point:

In[33]:= c = 0

Out[33]= 0

In[34]:= cosine = (++c ; Cos[#])&

Out[34]= (++c; Cos[#1]) & 

In[35]:= FixedPoint[cosine, 1.]

Out[35]= 0.739085

In[36]:= c

Out[36]= 91

Robby Villegas

  • Prev by Date: Re: Equating a variable and its value
  • Next by Date: Re: Equating a variable and its value
  • Previous by thread: Re: Equating a variable and its value
  • Next by thread: Re: Equating a variable and its value