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?
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?
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
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 *)
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
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 *)
=to a==as the latter represents equality while the former is used to set a variable. – rcollyer Nov 28 '12 at 05:15