2
$\begingroup$

Given that $x = a^2 + b^2$ and $y = a^2 - b^2$, how can I compute $x + y$ in Mathematica such that the result is $a^2 + b^2 + a^2 - b^2$ without simplifying? Essentially, I want to calculate $x + y$ while keeping $x$ and $y$ in their original forms without performing any further calculations.

x = a^2 + b^2;y=a^2 - b^2
Hold[Evaluate[x + y]]

The above code has no effect. How can this be achieved in Mathematica?

$\endgroup$
2
  • $\begingroup$ Try Mr.Wizard's step[] function in their answer to the linked Q&A on x = a^2 + b^2; y = a^2 - b^2; step[x + y]. $\endgroup$
    – Michael E2
    Commented Sep 29 at 2:45
  • $\begingroup$ HoldForm[Evaluate[x]] + HoldForm[Evaluate[y]] $\endgroup$
    – Bill Watts
    Commented Sep 30 at 0:47

2 Answers 2

0
$\begingroup$
Clear["Global`*"];
Hold[x + y] /. {x -> a^2 + b^2, y -> a^2 - b^2}
ReleaseHold[%]
Hold[(a^2 + b^2) + (a^2 - b^2)]
2 a^2
$\endgroup$
0
$\begingroup$

Another way, if you insist on x and y being defined with ownvalues:

x = a^2 + b^2;
y = a^2 - b^2;
HoldForm[x + y] /. OwnValues[x]~Join~OwnValues[y]
(* (a^2 + b^2) + (a^2 - b^2)] *)
$\endgroup$

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