Imagine there is a given function f, defined with SetDelayed, say

f[x_] := Sin[x]^2 + Cos[x]^2.

Is it possible to get rhs of this function before evaluation? For instance of f[5] I want to get expression

Sin[5]^2 + Cos[5]^2.
  • $\begingroup$ Somebody had nice suggestion with DownValues, but it disappeared. DownValues is very cool, but I guess my example is too simple. Because I want to extract previous inputs which obviously have executions. In[2]:=(Print[2]; 2+2), where I would like to get the full form of expression, e.g Print[2]; 2+2 without evaluating it. $\endgroup$ – Vladimir Mar 21 '13 at 8:57
  • $\begingroup$ See my answer - it gives you that. If you are interested in the input, use DownValues[In]. $\endgroup$ – Leonid Shifrin Mar 21 '13 at 8:58
  • $\begingroup$ @LeonidShifrin Yes, off course, I was just playing in Mathematica with your suggestion. Thank you! Sorry for my delay :) $\endgroup$ – Vladimir Mar 21 '13 at 9:15
  • $\begingroup$ Nothing to be sorry about :-). There is no rush with anything here on SE, including accepting an answer. It is actually usually better to wait for some time, to give people an incentive to post alternative solutions. $\endgroup$ – Leonid Shifrin Mar 21 '13 at 9:20
  • 1
    $\begingroup$ You can see at mathematica.stackexchange.com/questions/20236/… $\endgroup$ – minthao_2011 Mar 21 '13 at 12:21

You can do

Hold[f[5]] /. DownValues[f]

which would return to you the r.h.s. wrapped in Hold - which you can then strip or do anything else with it.

Note that while this is a useful technique, in many cases it is not really needed, so I'd first reconsider the design of your functions, and only use the above if it is really necessary.As a light-weight alternative, you can use local rules (RuleDelayed), e.g.

myFormula = f[x_] :> Sin[x]^2 + Cos[x]^2;

Then, you get what you need simply by

f[5] /. myFormula
| improve this answer | |

The general from of this question is what prompted my question and self-answer:
How do I evaluate only one step of an expression?

Using my step function from that answer this is simply:

step @ f[5]
Sin[5]^2 + Cos[5]^2.

The output is wrapped in HoldForm. You could use e.g. Defer @@ if you wish to change the head.

Again, the advantage to this method over working with DownValues is that it is a more general construct, and will work with other kinds of sets that produce UpValues or SubValues etc., as well as internal functions to the limited extent that is possible.

| improve this answer | |
  • $\begingroup$ +1. But you should also mention a disadvantage: generally a single evaluation step will not be what we think it is - there is no easy and general mapping between the chunks of evaluation that we consider a single step, and the steps of Mathematica evaluator. $\endgroup$ – Leonid Shifrin Mar 24 '13 at 16:01
  • $\begingroup$ @Leonid conceptually this is true, but using my definition: the first step that transforms the entire expression, this has been acceptably predictable for me. Can you give a counter-example where it behaves differently? $\endgroup$ – Mr.Wizard Mar 24 '13 at 16:03
  • $\begingroup$ Nothing comes to mind now, but I recall that we discussed some such examples in the context of safe ValueQ function. $\endgroup$ – Leonid Shifrin Mar 24 '13 at 16:10
  • $\begingroup$ B.t.w., I have some time to chat now, if you wish. $\endgroup$ – Leonid Shifrin Mar 24 '13 at 16:11
  • 1
    $\begingroup$ @Mr.Wizard regarding a counter-example, if you do step@f[5+5], you get f[10], whereas with the method of Leonid, you get the right hand side of f applied to 5+5. In order to get the same result with step, f should have a HoldAll attribute. $\endgroup$ – faysou Mar 24 '13 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.