11
$\begingroup$

Can I define an equation (for example, x+1 == y^2 + 2), and tell Mathematica to square both sides?

If not, what is an equivalent way to achieve this?

$\endgroup$
3
  • $\begingroup$ I changed the = to a == as the latter represents equality while the former is used to set a variable. $\endgroup$
    – rcollyer
    Nov 28, 2012 at 5:15
  • 1
    $\begingroup$ A related question you might find useful: Is it possible to have Mathematica move all terms to one side of an equation? $\endgroup$
    – Jens
    Nov 28, 2012 at 5:17
  • $\begingroup$ @Jens definitely a good one. Although, there is a built-in function that can handle this while leaving the form intact. $\endgroup$
    – rcollyer
    Nov 28, 2012 at 5:19

3 Answers 3

14
$\begingroup$

As this has been answered, already, here is my solution using Distribute:

Distribute[ (x+1 == y^2 + 2)^2, Equal ]
(* (1 + x)^2 == (2 + y^2)^2 *)
$\endgroup$
1
  • 2
    $\begingroup$ You could also add Thread[(x + 1 == y^2 + 2)^2, Equal] which feels more natural if you want to go that route... $\endgroup$
    – Jens
    Nov 28, 2012 at 5:26
14
$\begingroup$

Did you mean this?

Power[#, 2] & /@ (x + 1 == y^2 + 2)

Or

#^2 & /@ (x + 1 == y^2 + 2)

works as well, according to Vitaliy Kaurov's advice.

(1 + x)^2 == (2 + y^2)^2
$\endgroup$
2
  • 3
    $\begingroup$ #^2 & /@ (x + 1 == y^2 + 2) works too $\endgroup$ Nov 28, 2012 at 7:40
  • $\begingroup$ @Vitaliy Kaurov, Thank U for reminding me of it $\endgroup$
    – yulinlinyu
    Dec 2, 2012 at 5:05
13
$\begingroup$

When you have an equation:

eqn = x + 1 == y^2 + 2

What Mathematica actually "sees" is this:

eqn // FullForm
(* Equal[Plus[1,x], Plus[2,Power[y,2]]] *)

In order to square both sides, you somehow have to "reach into" the Equal and square the expressions inside of it.

This can be done with pattern matching, using ReplaceAll

eqn /. Equal[a_, b_] :> Equal[a^2, b^2]
(* (1 + x)^2 == (2 + y^2)^2 *)

You'll notice that this matches expression pattern a_ to Plus[1,x] and b_ to Plus[2, Power[y, 2]]. Then it returns the two, but squared (the a^2, b^2 part).

Another way to do this would be, as @Jens' link points out, using Apply, which passes the sequence of arguments from one function to another (i.e., g @@ f[a, b] becomes g[a, b] - note @@ is shorthand for Apply.

We use this to our advantage with the pure function which squares both sides of the expression.

#1^2 == #2^2 & @@ eqn
(* (1 + x)^2 == (2 + y^2)^2 *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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