> To unsubscribe from this group and stop receiving emails from it, send an email to sy...@googlegroups.com.
>>> def add(s,o):
... assert s.func == o.func
... return s.func(s.lhs+o.lhs,o.rhs+s.rhs)
...
>>> def mul(s,o):
... return s.func(o*s.lhs,o*s.rhs)
...
>>> def rmul(s,o):
... return s.func(o*s.lhs,o*s.rhs)
...
>>> Eq.__add__ = add
>>> Eq.__mul__ = mul
>>> Eq.__rmul__ = rmul
>>>
>>> from sympy import symbols, Eq, expand, simplify, latex, oo, Sum
>>> x, y, p, q, a, j, Aj, Bj = symbols('x y p q a j A_j B_j')
>>> eq1 = Eq(x, y)
>>> eq2 = Eq(p, q)
>>> eq1 + eq2
Eq(p + x, q + y)
>>> a*eq1
Eq(a*x, a*y)
>>> Aj = IndexedBase("A")[j]>>> Bj = IndexedBase("B")[j]
>>> ex1 = Sum(Aj + Bj, (j, -oo, oo))
>>> ex2 = Sum(Aj - Bj, (j, -oo, oo))
>>> ex1 + ex2
Sum(A[j] - B[j], (j, -oo, oo)) + Sum(A[j] + B[j], (j, -oo, oo))
>>> expand(_)
2*Sum(A[j], (j, -oo, oo)) + Sum(-B[j], (j, -oo, oo)) + Sum(B[j], (j, -oo, oo))
>>> factor_terms(_)
2*Sum(A[j], (j, -oo, oo))
In 1.5 it just gives a nonsense object: In [1]: Eq(x, y) + Eq(z, t) Out[1]: (x = y) + (z = t)
However, it should surely be valid to add an equation to an expression:
>>> Eq(a-3,b-3)+3
3 + Eq(a-3,b-3)
>>> simplify(3 + Eq(a - 3, b - 3))
3 + Eq(a - 3, b - 3)
Obviously I thought that would produce Eq(a,b)
At 1.5 the addition happens but the result doesn't simplify in
the obvious way. Is there a rational to that? From what you say, I
suspect this construct will also be banned at 1.6, but it seems
perfectly meaningful.
David
Hi David, I agree entirely. Neither gives a sensible result in 1.5 and both will give errors in 1.6. The Equation class I showed handles all of these though:
So I guess since you say you haven't been able to put it in SymPy yet, the Equation class won't be going in 1.6, but maybe 1.7?
David
Hi Aaron, Yes, if we had Equation we should disallow its use in an expression. I don't see any situation where it would make sense. We would need a clear distinction between Equation and Equality as well as methods to convert between them. 1. Equality is a Boolean 2. Equation is Basic 3. Neither is Expr 4. You can use `.as_Boolean/.as_Equation` or something to convert between 5. Equation should be used instead of Equality for e.g. passing equations to solve/dsolve and for returning solutions. 6. Equation should be invalid in any Boolean context 7. Equality should not support any of the arithmetic manipulations that Equation does 8. Equation should be usable in subs or subseq or something (but Equality should not) 9. Equation should have a solve method for making something the lhs 10. Documentation should find a way of making this understandable...
Hang on, doesn't that exclude the very operations we were just
discussing, such as
3+Equation(a-3,b-3)
David
Yes but isn't that inconsistent with your statement "Yes, if we
had Equation we should disallow its use in an expression"? Surely
the example we are discussing is an Equation embedded in
an expression!
Sorry, I am probably missing something!
David
There is a difference between having an object as part of an expression and having an object that supports arithmetic operations. Objects of type Expr support arithmetic operations and by default the result creates an expression involving the original object so e.g. a+b becomes Add(a, b). That's what happens with Eq in sympy 1.5 because in 1.5 Eq is a subclass of Expr so Eq(a, b)+1 becomes Add(Eq(a, b), 1) which is nonsensical. In 1.6 Eq does not subclass Expr and also Expr is fussier about what types it can interact with through arithmetic operations so this gives an error. The point of Equation is that it can support some arithmetic operations and the result will always be an Equation and never an expression. It should never be a part of an expression though so e.g. Add(Equation(x, y), z) is an error because Add is of type Expr and its arguments need to be of type Expr but Equation is not of type Expr. That does not preclude Equation(x,y)+1 giving an Equation though.
Doesn't that feel a bit arbitrary, I mean you seem to be saying saying that
Equation(a+k,b+k)-k
where a,b, and k are SymPy symbols
would fail?
If that is the case, the new ability to add/subtract things from Equations doesn't seem that powerful.
David
Thanks for that great explanation. I think my confusion was that I assumed that if you typed in the expression
Equation(a+k,b+k)+k
Then the first thing that would happen would be to transform the expression to Add(Equation(add(a,k),k) and then try to evaluate the result. If that had been correct than the outer Add() expression could not be created and would generate an error.
David