7
$\begingroup$

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

$\endgroup$
0

4 Answers 4

6
$\begingroup$

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)
$\endgroup$
6
  • $\begingroup$ +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. $\endgroup$
    – rcollyer
    Jun 21, 2012 at 1:39
  • $\begingroup$ @ rcollyer, I've updated it. $\endgroup$
    – yulinlinyu
    Jun 21, 2012 at 1:43
  • 2
    $\begingroup$ You could have shortened further to # - 2 & /@ (3 x + 5 == 2). $\endgroup$ Jun 21, 2012 at 2:06
  • $\begingroup$ @J.M., it is a great idea. See for my update. $\endgroup$
    – yulinlinyu
    Jun 21, 2012 at 2:08
  • 3
    $\begingroup$ 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. $\endgroup$
    – rcollyer
    Jun 21, 2012 at 4:03
7
$\begingroup$

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.

$\endgroup$
3
$\begingroup$

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.

$\endgroup$
1
$\begingroup$

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]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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