# Manipulating Equations

I would like to be able to manipulate algebraic equations. For example, let's say I enter

3x+5==2

I would next like to be able to manually subtract $2$ from both sides. I tried the obvious thing %-2, but this just returns

-2+(3x+5==2)

Is there a way to

-

How do you like this:

a = 3 x + 5 == 2;
Subtract[#, 2] & /@ a


(* 3 + 3 x == 0 *)

Subtract[#, 2] & /@(3 x + 5 == 2)


(#-2)&/@(3 x + 5 == 2)

-
+1, clever. It can be made a one-liner, though, by replacing a in the second line with (3 x + 5 == 2) where the parentheses are vital. –  rcollyer Jun 21 '12 at 1:39
@ rcollyer, I've updated it. –  yulinlinyu Jun 21 '12 at 1:43
You could have shortened further to # - 2 & /@ (3 x + 5 == 2). –  Ｊ. Ｍ. Jun 21 '12 at 2:06
@J.M., it is a great idea. See for my update. –  yulinlinyu Jun 21 '12 at 2:08
Last 2 cents. Some may be tempted to try and use the first level Apply (@@@) instead of Map here, but it won't work because 2 on the RHS is atomic, i.e. it has no Head to replace. –  rcollyer Jun 21 '12 at 4:03

This works nicely:

3 x + 5 == 2
5 + 3 x == 2

3 + 3 x == 0


Roman Maeder's old EqualThread package may be of interest, but I haven't tested for compatibility with new versions.

-

Let me note in addition that one may want to look at the TreeForm of the equation:

> eq = 3*x + 5 == 2

> TreeForm[eq, ImageSize -> 150]


and get the following

Now one may address any element of equation separately, exactly as it is done on the paper. Say,

> eq[[1, 2]]


3 x

This, in fact, stays behind the approaches proposed above. In the simple case discussed in this post it is, of course, too much, but will fast become necessary, as soon as one goes to a bit more complex equations.

-

Here's a simple attempt at a function which will do the manipulation for any function with the NumericFunction attribute.

threq[func_[x___,HoldPattern@Equal[y__]]/;MemberQ[Attributes@func,NumericFunction]]:=
Equal@@(func[x,#]&/@{y});
threq[x___] := x


Examples:

In[3]:= threq[(3x+5==2)-2]
Out[3]= 3+3 x==0

In[4]:= threq[(3x+5==2)a]
Out[4]= a (5+3 x)==2 a

In[5]:= threq[Sin[3x+5==2]]
Out[5]= Sin[5+3 x]==Sin[2]

In[6]:= threq[f[3x+5==2]]
Out[6]= f[5+3 x==2]

-