# Square both sides of an equation?

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?

-
What have you tried so far? Are you accessing the equation from a symbol? –  VF1 Nov 28 '12 at 5:14
I changed the = to a == as the latter represents equality while the former is used to set a variable. –  rcollyer Nov 28 '12 at 5:15
A related question you might find useful: Is it possible to have Mathematica move all terms to one side of an equation? –  Jens Nov 28 '12 at 5:17
@Jens definitely a good one. Although, there is a built-in function that can handle this while leaving the form intact. –  rcollyer Nov 28 '12 at 5:19

## 3 Answers

Do you mean this?

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


Or

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

, according to Vitaliy Kaurov's advice.

(1 + x)^2 == (2 + y^2)^2

-
#^2 & /@ (x + 1 == y^2 + 2) works too –  Vitaliy Kaurov Nov 28 '12 at 7:40
@Vitaliy Kaurov, Thank U for reminding me of it –  yulinlinyu Dec 2 '12 at 5:05

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 *)

-
You could also add Thread[(x + 1 == y^2 + 2)^2, Equal] which feels more natural if you want to go that route... –  Jens Nov 28 '12 at 5:26

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 *)

-