Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Re: [mg5355] Problem with If[] and %
Replies: 0

 Allan Hayes Posts: 1,508 Registered: 12/6/04
Re: [mg5355] Problem with If[] and %
Posted: Dec 6, 1996 11:46 PM

hans.steffani@e-technik.tu-chemnitz.de (Hans Steffani)
[mg5355] Problem with If[] and %
writes

> i^2+v^2
> If[ % >15,15,%]
> Plot3D[ %,{i,-5,5},{v,-5,5}]
>

Hans,

In version 2.2 you can get what you want by replacing
If[ % >15,15,%]
with
If[ Evaluate[% >15],15,Evaluate[%]];
In version 3.0 you can use this or If[ % >15,15,Evaluate[%]]

Here is what lies behind this in version 2.2 (version3.0 is
slightly different - I'll deal with this after).

In evaluating an expression
f[x,y z]
the first step (the HE step)is, * subject to any attributes of f *,
to evaluate the head and the entries, f,x,y,z, in turn.
This means that, if f has the attribute HoldAll (as If has in
version 2.2), then the entries x,y,z are held unevaluated, and we
get

f'[x,y,z] (where f' is the value of f). (1)

Then the *rules* for evaluating (1) are used.

In the evaluation of your original code the HE step in the
evaluation of If[ % >15,15,%] simply gives (because If has the
attribute Hold all an the value of If is If)

If[ % >15,15,%] (2)

Then the rules for evaluating (2) are used.
the value of % >15, that is i^2+v^2 > 15, is examined
since this is neither True nor False, (1) is returned.

With the change suggested, the HE step in the evaluation of
If[ Evaluate[% >15],15,Evaluate[%]] gives

If[ i^2+v^2 > 15, 15, i^2+v^2 ] (3)

Then the rules for evaluating (3) are used:
the value of i^2+v^2 > 15, that is i^2+v^2 > 15, is examined
since this is neither True nor False, (3) is returned.

In version 3.0 If has the attribute HoldRest (so in the HE step the
first entry is evaluated). This is the reason that you can use

If[ % >15,15,Evaluate[%]]

Incidentally, you get the same picture with

i^2+v^2
Plot3D[ %,{i,-5,5},{v,-5,5}, PlotRange -> {0,5}]

and you can modify the clipped part with the option ClipFill.

Allan Hayes
hay@haystack.demon.co.uk
http://www.haystack.demon.co.uk