2
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ Define y[x_] := x^2 // HoldForm and then use y[2] to get 2^2 and y[2] // ReleaseHold to get 4. $\endgroup$ – Boson Mar 21 '14 at 17:25
  • $\begingroup$ Take a look at Defer too. $\endgroup$ – Kuba Mar 21 '14 at 17:48
2
$\begingroup$

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

|improve this answer|||||
$\endgroup$
  • 1
    $\begingroup$ I'm happy it seems to work as I've imagined it works :) Can't wait to test it. $\endgroup$ – Kuba Mar 21 '14 at 20:33
2
$\begingroup$

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.

|improve this answer|||||
$\endgroup$

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.