11

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?

rm -rf
  • 88,781
  • 21
  • 293
  • 472
Fawkes5
  • 367
  • 1
  • 11

3 Answers3

14

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
yulinlinyu
  • 4,815
  • 2
  • 29
  • 36
14

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 *)
rcollyer
  • 33,976
  • 7
  • 92
  • 191
  • 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 05:26
13

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 *)
VF1
  • 4,702
  • 23
  • 31