0
|
4359 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
GROUPS:

# function evaluation

Posted 8 years ago
 In:= y = Integrate[z, z] Out= z^2/2 In:= fu[u_] := y /. z -> u In:= fs Out= 9/2 In:= fz[z_] := y In:= fz Out= z^2/2  why doesn't fz evaluate and how can I force it?
3 Replies
Sort By:
Posted 8 years ago
 When you do y = z^2/2; fz[z_] := y; fz the delayed evaluation f[z_]:=y means that the right-hand side is memorized verbatim. You can check with ?fzWhen you then call f, 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 8 years ago
 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:= y = Integrate[z, z] Out= z^2/2 In:= fz[z_] := Evaluate[y] In:= fz Out= 9/2 maybe I'm just foggy about rules here - why do I need evaluate here?
Posted 8 years ago
 Hi,sorry, I do not get the question. Why do you say that fz 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 `This evaluates to $\frac{9}{2}$. By using the delayed evaluations I have changed to order of evaluations. Cheers,Marco