On Thu, Feb 14, 2013 at 10:48 AM, G B <[email protected]> wrote:
> Sorry if this is covered in the documentation somewhere, I tried to check...
>
> Is there a way to subtract one equation from another?  What I'm looking to
> do boils down to:
> a,b,r,s=symbols('a,b,r,s')
> eq1=Eq(a,r)     #a==r
> eq2=Eq(b,s)    #b==s
> eq3=eq1-eq2


You've got to write your own helpers do do this:

>>> def do2(a, b, op):
...     return Eq(op(a.lhs, b.lhs), op(a.rhs, b.rhs))
...
>>> def do(a, op):
...     return Eq(op(a.lhs), op(a.rhs))
...
>>> def neg(a):
...   return do(a, lambda x: -x)
...
>>> def inv(a):
...   return do(a, lambda x: 1/x)
...
>>> a=Eq(x+3,y);b=Eq(x+y,4)
>>> b
x + y == 4
>>> neg(b)
-x - y == -4
>>> do2(a, neg(b), Add)
-y + 3 == y - 4
>>> do2(a, b, lambda x, y: x-y)
-y + 3 == y - 4
>>> do(_, lambda x: x - y)  # subtract y from both sides
-2*y + 3 == -4
>>> inv(_)
1/(-2*y + 3) == -1/4
>>> inv(_)
-2*y + 3 == -4
>>> do(_, lambda x: x - 3)  # subtract 3 from both sides
-2*y == -7
>>> do(_, lambda x: x/-2)  # div by -2
y == 7/2

That's a little awkward using lambda. Let's define the operation by example:

>>> side = Dummy()
>>> def do(a, pat):
...  return Eq(*[pat.subs(pat.free_symbols.pop(),i) for i in a.args])
...
>>> a
x + 3 == y
>>> do(a, side-3)
x == y - 3
>>> do(a, 1/side)
1/(x + 3) == 1/y
>>> do(_, 1/side)
x + 3 == y
>>> do(_, x-3)  # you can use any variable to indicate "side"
x == y - 3

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sympy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to