Message Boards Message Boards

0
|
5030 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:

function evaluation

In[18]:= y = Integrate[z, z]

Out[18]= z^2/2

In[19]:= fu[u_] := y /. z -> u

In[20]:= fs[3]

Out[20]= 9/2

In[21]:= fz[z_] := y

In[22]:= fz[3]

Out[22]= z^2/2

why doesn't fz[3] evaluate and how can I force it?

POSTED BY: Kay Herbert
3 Replies

When you do

y = z^2/2;
fz[z_] := y;
fz[3]

the delayed evaluation f[z_]:=y means that the right-hand side is memorized verbatim. You can check with ?fz

When you then call f[3], the replacement z->3 is done in the frozen y, resulting in no replacement at all, since the verbatim y does not contain z.

After this useless replacement y/.z->3 is done, the result is unfrozen, and the outside rule y=z^2/2 gets finally applied.

POSTED BY: Gianluca Gorni

Thanks, my beef is that I have y=z^2/2 the equal sign should sybstitute z^2/2 whenever y is encountered right? However when I use y in the function fz definition it seems to substitute for y after evaluating the function. I guess Evaluate forces it:

In[13]:= y = Integrate[z, z]

Out[13]= z^2/2

In[14]:= fz[z_] := Evaluate[y]

In[15]:= fz[3]

Out[15]= 9/2

maybe I'm just foggy about rules here - why do I need evaluate here?

POSTED BY: Kay Herbert
POSTED BY: Marco Thiel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract