I guess not, because on the one hand, you want Eq(x, y) + z to give Eq(x + z, y + z), but on the other hand, you want Eq(x, y) + Eq(z, t) to give Eq(x + z, y + t). So you really need to define them, because they depend on the type of the other argument (you can factor out the common logic to a helper function). There are only a handful of common operations that you probably care about, __add__, __sub__, __mul__, __div__, __neg__, and "r" versions of the first four (like __radd__).
Aaron Meurer On Wed, Feb 13, 2013 at 10:32 PM, Aaron Meurer <[email protected]> wrote: > I think making a subclass is easier: > > class VectorizedEq(Equality): > def __getattr__(self, attr): > return VectorizedEq(self.lhs.attr, self.rhs.attr) > > Unfortunately, things like __add__ will not work, because they are > already defined in the base class. I'm not sure if there's a slick > way to "forget" all those in a small amount of code. > > Aaron Meurer > > On Wed, Feb 13, 2013 at 10:25 PM, Chris Smith <[email protected]> wrote: >> 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. >> >> -- 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.
