What version of sympy are you using? I get an exception from eq3 = eq1 + eq2.
We need a new Equation class that works properly with arithmetic
operations etc. I have made written one but haven't added it to sympy
yet. I got sidetracked at the time because deeper structural changes
were needed like ensuring that Relational does not subclass Expr.
Here's my new Equation class which supports arithmetic operations:
from sympy.core.basic import Basic
from sympy.core.sympify import _sympify
from sympy.simplify import simplify
from sympy.core.relational import Equality
class Equation(Basic):
def __new__(cls, lhs, rhs):
lhs = _sympify(lhs)
rhs = _sympify(rhs)
return Basic.__new__(cls, lhs, rhs)
@property
def lhs(self):
return self.args[0]
@property
def rhs(self):
return self.args[1]
def as_Boolean(self):
return Equality(self.lhs, self.rhs)
@property
def reversed(self):
return Equation(self.rhs, self.lhs)
def applyfunc(self, func):
return Equation(func(self.lhs), func(self.rhs))
def applylhs(self, func):
return Equation(func(self.lhs), self.rhs)
def applyrhs(self, func):
return Equation(self.lhs, func(self.rhs))
@classmethod
def _binary_op(cls, a, b, opfunc_ab):
if isinstance(a, Equation) and not isinstance(b, Equation):
return Equation(opfunc_ab(a.lhs, b), opfunc_ab(a.rhs, b))
elif isinstance(b, Equation) and not isinstance(a, Equation):
return Equation(opfunc_ab(a, b.lhs), opfunc_ab(a, b.rhs))
elif isinstance(a, Equation) and isinstance(b, Equation):
return Equation(opfunc_ab(a.lhs, b.lhs), opfunc_ab(a.rhs, b.rhs))
else:
raise TypeError('One of a or b should be an equation')
def __add__(self, other):
return self._binary_op(self, other, lambda a, b: a + b)
def __radd__(self, other):
return self._binary_op(other, self, lambda a, b: a + b)
def __mul__(self, other):
return self._binary_op(self, other, lambda a, b: a * b)
def __rmul__(self, other):
return self._binary_op(other, self, lambda a, b: a * b)
def __sub__(self, other):
return self._binary_op(self, other, lambda a, b: a - b)
def __rsub__(self, other):
return self._binary_op(other, self, lambda a, b: a - b)
def __truediv__(self, other):
return self._binary_op(self, other, lambda a, b: a / b)
def __rtruediv__(self, other):
return self._binary_op(other, self, lambda a, b: a / b)
def __pow__(self, other):
return self._binary_op(self, other, lambda a, b: a ** b)
def __rpow__(self, other):
return self._binary_op(other, self, lambda a, b: a ** b)
def __str__(self):
return '%r = %r' % self.args
def expand(self, *args, **kwargs):
return self.func(*(arg.expand(*args, **kwargs) for arg in self.args))
def simplify(self, *args, **kwargs):
return simplify(self, *args, **kwargs)
def _eval_simplify(self, *args, **kwargs):
return self.func(*(arg.simplify(*args, **kwargs) for arg in self.args))
def factor(self, *args, **kwargs):
return factor(self, *args, **kwargs)
def evalf(self, *args, **kwargs):
return self.func(*(arg.evalf(*args, **kwargs) for arg in self.args))
def n(self, *args, **kwargs):
return self.func(*(arg.n(*args, **kwargs) for arg in self.args))
from sympy import symbols, cos, sin
x, y = symbols('x, y')
eq = Equation(x, y)
print(eq)
eq = Equation(1, 2)
print(eq)
eq = Equation(1, 2) + 3
print(eq)
eq = Equation(1, 2) + Equation(x, y)
print(eq)
eq = Equation(1, 2) - 3
print(eq)
eq = Equation(1, 2) - Equation(x, y)
print(eq)
eq = 1 - Equation(1, 2)
print(eq)
eq = 1 + Equation(x, y)
print(eq)
eq = Equation(cos(x)**2 + sin(x)**2 - 1, 2)
Oscar
On Thu, 30 Apr 2020 at 16:18, Thomas Ligon <[email protected]> wrote:
>
> I have a few small questions where I can solve the issues manually and have
> the feeling that in my attempts to solve them with SymPy I am overlooking
> something. The examples below are trivial and easy to solve manually, but the
> project I am working on involves much more complex expressions where it would
> be great to have a better solution in SymPy.
> Basically, adding two equations does not produce the desired solution, and
> sums do not expand or simplify as expected.
> PS. The print('end') statement is just there as a convenient place for a
> breakpoint in my debugger.
>
> 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)
> print(latex(eq1)) # OK
> eq2 = Eq(p, q)
> print(latex(eq2)) # OK
> eq3 = eq1 + eq2
> print(latex(eq3)) # not what I want
> eq4 = Eq(eq1.lhs + eq2.lhs, eq1.rhs + eq2.rhs)
> print(latex(eq4)) # yes, what I want, but tedious
> eq5 = a*eq1
> print(latex(eq5)) # not exactly what I want
> eq6 = Eq(a*eq1.lhs, a*eq1.rhs)
> print(latex(eq6)) # yes, what I want, but tedious
> ex1 = Sum(Aj + Bj, (j, -oo, oo))
> print(latex(ex1)) # OK
> ex2 = Sum(Aj - Bj, (j, -oo, oo))
> print(latex(ex2)) # OK
> ex3 = ex1 + ex2
> print(latex(ex3)) # correct, but needs consolidation
> ex4 = expand(ex3)
> print(latex(ex4)) # latex looks bad
> ex5 = simplify(ex3)
> print(latex(ex5)) # latex completely wrong
> print('end')
>
> --
> 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 view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/71f9f37a-1b10-4fe5-b7ef-65f92fe3bc86%40googlegroups.com.
--
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 view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/CAHVvXxS3Bq2pg4K1S%2Bp_mY815gLSbaHgaQpaL2OzZ%3D0J-Q8xHg%40mail.gmail.com.