1
$\begingroup$

Is it possible to Hold/Defer some of outer expressions, having evaluates inner ones?

For example, I want to prepare a table of expression values, like this

Table[Defer[Sin[x]^2 + Cos[x]^2] == Sin[x]^2 + Cos[x]^2, {x, 0, 1, 0.1}] // TableForm

Currently this displays left hand sides as

Sin[x]^2 + Cos[x]^2

but can I have x-s evaluated/substituted?

UPDATE

Or just simpler example.

Suppose I write

x = 12
Defer[Sin[x]^2 + Cos[x]^2] == Sin[x]^2 + Cos[x]^2

which gives

12
Sin[x]^2 + Cos[x]^2 == 1.

but can I have

12
Sin[12]^2 + Cos[12]^2 == 1.
$\endgroup$
  • 1
    $\begingroup$ Are you working on V10? $\endgroup$ – Kuba Nov 25 '14 at 8:06
  • $\begingroup$ No, I have V9. What advantages V10 has? $\endgroup$ – Suzan Cioc Nov 25 '14 at 12:28
  • 1
    $\begingroup$ @SuzanCioc v10 has a set of functions exactly for this. Please check Inactive. $\endgroup$ – Silvia Nov 25 '14 at 12:37
  • $\begingroup$ Incredible. Why did they wait 9 versions? $\endgroup$ – Suzan Cioc Nov 25 '14 at 12:50
1
$\begingroup$

Your can use Replace* inside Hold or Deffer like this:

x = 12;
test = Defer[Sin[x]^2 + Cos[x]^2] == Sin[x]^2 + Cos[x]^2

Sin[x]^2 + Cos[x]^2 == 1.

Block[{x}, test /. x -> y] /. y -> x

Sin[12.]^2 + Cos[12.]^2 == 1.

Just idea, no doubt better approach can be found.

$\endgroup$
  • $\begingroup$ It seems to be even a bit more simple than that: Defer[Sin[x]^2 + Cos[x]^2] == Sin[x]^2 + Cos[x]^2 /.x -> 12// Simplify yields what you need. $\endgroup$ – Alexei Boulbitch Nov 25 '14 at 8:41
  • $\begingroup$ I assume that x has a value, so 12->12. $\endgroup$ – user18792 Nov 25 '14 at 9:18
  • $\begingroup$ In my code x has no value. I do not see your point. $\endgroup$ – Alexei Boulbitch Nov 25 '14 at 9:26
  • $\begingroup$ Not my point. It is put in the formulation of the task: x=12; $\endgroup$ – user18792 Nov 25 '14 at 9:34
  • $\begingroup$ Nice, but Alexei's solution in its present form doesn't work in a table, i.e. Table[Defer[Sin[x]^2 + Cos[x]^2] == Sin[x]^2 + Cos[x]^2 /. x -> 12 // Simplify, {x, 0, 1, 0.1}] // TableForm $\endgroup$ – Chris Degnen Nov 25 '14 at 9:41
0
$\begingroup$

Mapping Hold or HoldForm over all the elements effectively freezes the expression for easier manipulation. (This method was used in R. Maeder's Programming in Mathematica, page 137.)

x = 12.;
Replace[Map[HoldForm, Defer[Sin[x]^2 + Cos[x]^2], Infinity] ,
  HoldForm[x] -> HoldForm[12]] == Sin[x]^2 + Cos[x]^2

Sin[12]^2+Cos[12]^2==1.

And in a table:

Table[
  ReplaceAll[Map[HoldForm, Defer[Sin[x]^2 + Cos[x]^2], Infinity] ,
    HoldForm[x] -> HoldForm[Evaluate[x]]] == Sin[x]^2 + Cos[x]^2,
  {x, 0, 1, 0.1}] // TableForm

Simpler method

Table[With[{y = x}, Defer[Sin[y]^2 + Cos[y]^2] == Sin[x]^2 + Cos[x]^2],
  {x, 0, 1, 0.1}] // TableForm
$\endgroup$
0
$\begingroup$

You could also use Activate/Inactivate:

TableForm @ Activate @ Table[
    Inactivate @ Defer[Sin[x]^2+Cos[x]^2] == Sin[x]^2+Cos[x]^2,
    {x, 0, 1, 0.1}
] //TeXForm

$\begin{array}{c} \sin ^2(0.)+\cos ^2(0.)=1. \\ \sin ^2(0.1)+\cos ^2(0.1)=1. \\ \sin ^2(0.2)+\cos ^2(0.2)=1. \\ \sin ^2(0.3)+\cos ^2(0.3)=1. \\ \sin ^2(0.4)+\cos ^2(0.4)=1. \\ \sin ^2(0.5)+\cos ^2(0.5)=1. \\ \sin ^2(0.6)+\cos ^2(0.6)=1. \\ \sin ^2(0.7)+\cos ^2(0.7)=1. \\ \sin ^2(0.8)+\cos ^2(0.8)=1. \\ \sin ^2(0.9)+\cos ^2(0.9)=1. \\ \sin ^2(1.)+\cos ^2(1.)=1. \\ \end{array}$

$\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.