Message Boards Message Boards

0
|
5081 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

Hi,

sorry, I do not get the question. Why do you say that fz[3] does not evaluate? Doesn't Mathematica do exactly what you would expect it to do?

By definition fz does not depend on $z$. So I would expect it to evaluate to $y$. But input 18 defines $y$ as $\frac{z^2}{2}$. So after fz evaluates to $y$ this is substituted by $\frac{z^2}{2}$. So Mathematica's result is as expected. I understand that this is not the result you expect. Would you have expected something like this?

ClearAll["Global`*"]
y[z_] := Evaluate[Integrate[z, z]]
fz[z_] := y[z]
fz[3]

This evaluates to $\frac{9}{2}$. By using the delayed evaluations I have changed to order of evaluations.

Cheers,

Marco

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