Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

share|improve this question
add comment

4 Answers

up vote 6 down vote accepted

How do you like this:

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

(* 3 + 3 x == 0 *)

Edit: (according to rcollyer's advice)

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

Edit: (according to J.M's advice)

(#-2)&/@(3 x + 5 == 2)
share|improve this answer
    
+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
2  
You could have shortened further to # - 2 & /@ (3 x + 5 == 2). –  J. M. Jun 21 '12 at 2:06
    
@J.M., it is a great idea. See for my update. –  yulinlinyu Jun 21 '12 at 2:08
2  
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
show 2 more comments

This works nicely:

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

Thread[% - 2, Equal]
3 + 3 x == 0

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

share|improve this answer
add comment

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

enter image description here

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.

share|improve this answer
add comment

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]
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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