In the spirit of a previous question, this is a self-assigned exercise in order to understand how we can control expression evaluation with Mathematica.
So, given the following function definition:
f[x_] := Sin[Pi x] + Cos[Pi x]
By default, Mathematica will evaluate expressions as far as possible. So:
f[3]
-1
However, and without having tho change the definition of f
, I would like to obtain only a partial evalutation of the expression where the x
argument is substituted, but where the Cos
and Sin
function aren't evaluated. In clear, I want that:
Cos[3 π] + Sin[3 π]
After some trials and errors, I came to a Block
-based solution given as an answer below. But do you have other suggestions to achieve that result?
This question is really to learn how to control evaluation with Mathematica. I would prefer solutions that demonstrate how to apply the functions given in the docs on that particular case, rather than a more powerful and generic solution like Mr. Wizard's step
function.
step[f[3]]
returnsSin[π 3]+Cos[π 3]
(usingstep
from @MrWizard's answer) $\endgroup$TraceScan
solution is that it will work for all functions, without you having to list everything that shouldn't be evaluated. But i guess it depends on what exactly you want to achieve (also, I wouldn't necessarily call it a "complex" function - it's essentially only two lines) $\endgroup$step
function isn't that long. But for a newcomer, it is not obvious to understand. I tried to applyTraceScan
in my particular case. I tried variations along the lines ofTraceScan[HoldForm, f[3]]
. But it returns the fully evaluated function eaach time. WhereasTraceScan[Print, f[3]]
display the individual evaluation steps. Obviously, I missed something important here :/ $\endgroup$