Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

If I have

x = 2

and I want to evaluate

y = x^2

I would like to the output to show

y = (2)^2

before evaluating to 4.

share|improve this question
    
Define y[x_] := x^2 // HoldForm and then use y[2] to get 2^2 and y[2] // ReleaseHold to get 4. –  Boson Mar 21 at 17:25
    
Take a look at Defer too. –  Kuba Mar 21 at 17:48
add comment

2 Answers 2

I would recommend to look into new functions that are already available on Raspberry Pi and you have something to look forward to in a general future release: Activate, Inactive and Inactivate.

They are a very powerful way to transform the code and expressions in the way you need. For example.

f[x_] := Inactive[Power][x, 2]

y == f[2]

y == 2^2

y == Activate@f[2]

y == 4

You can make a lot of things including identity tables:

Table[f[k] == Activate[f[k]], {k, 0, 3}] // Column // TraditionalForm

enter image description here

Table[Inactive[Sin][k \[Pi]/6] == Sin[k \[Pi]/6], {k, 0, 3}] // Column // TraditionalForm

enter image description here

But in your specific case you of course could simply do something like:

Table[Superscript[k, 2] == k^2, {k, 0, 3}] // Column // TraditionalForm

enter image description here

share|improve this answer
1  
I'm happy it seems to work as I've imagined it works :) Can't wait to test it. –  Kuba Mar 21 at 20:33
add comment

As Boson suggested, HoldForm and ReleaseHold can be pressed into service to accomplish what you ask. Here is how you can use them.

ySq[x_] := Module[{y}, Row[{"y = ", y = HoldForm[x^2], " = ", ReleaseHold[y]}]]
Column[ySq /@ Range[5]]
y = 1^2 = 1
y = 2^2 = 4
y = 3^2 = 9
y = 4^2 = 16
y = 5^2 = 25

ySq can serve as a model for defining other functions that can produce similar expository output.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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