Compare commits

...
12 Commits
Author SHA1 Message Date
Christopher Smith d1987903ea Update add.py 2019-07-21 14:26:01 -05:00
Christopher Smith ccdfbb303e Update add.py 2019-07-21 12:51:40 -05:00
Christopher Smith 2b925c04b4 Update add.py 2019-07-21 11:50:32 -05:00
Christopher Smith b9ec8595e0 Update add.py 2019-07-21 11:41:04 -05:00
Christopher Smith f4e57c5b6a Update add.py 2019-07-21 11:36:25 -05:00
Christopher Smith 5bb4fc088c Update add.py 2019-07-21 10:47:15 -05:00
Christopher Smith 04c69d820c Update add.py 2019-07-21 10:12:03 -05:00
Christopher Smith bd01e3802f Update add.py 2019-07-21 09:28:45 -05:00
Christopher Smith cb1504df5f Update add.py 2019-07-21 09:18:54 -05:00
Christopher Smith fb6cbe8491 Update add.py 2019-07-21 07:25:25 -05:00
Christopher Smith e50379d959 Update add.py 2019-07-21 07:17:01 -05:00
Christopher Smith 033c3ef8c6 Update mul.py 2019-07-21 07:00:51 -05:00
2 changed files with 50 additions and 17 deletions
+47 -1
View File
@@ -87,6 +87,21 @@ class Add(Expr, AssocOp):
NB: the removal of 0 is already handled by AssocOp.__new__
Examples
========
>>> from sympy.abc import x, y
>>> x + y - (x + y)
0
>>> x + y - 2*(x + y)
-(x + y)
In order for this to go to zero, negated terms would have to be
collected and added at the end
>>> -(x + y) + x + y
x + y - (x + y)
See also
========
@@ -201,8 +216,39 @@ class Add(Expr, AssocOp):
if terms[s] is S.NaN and not extra:
# we know for sure the result will be nan
return [S.NaN], [], None
continue
if s.is_Add:
others = []
for si in s.args:
if si in terms:
terms[si] += c
if terms[si] is S.NaN and not extra:
# we know for sure the result will be nan
return [S.NaN], [], None
else:
others.append(si)
if len(others) != len(s.args):
s = Add(*others, evaluate=False)
elif not others:
continue # all combined with other terms
else:
terms[s] = c
from sympy.core.compatibility import ordered
for t in ordered(terms):
if not t.is_Add or s not in t.args:
continue
# s will be a new term and will be removed from t's args
ct = terms.pop(t)
c += ct # will be registered to terms[s] below
ix = t.args.index(s)
a = t.func(*(t.args[:ix] + t.args[ix+1:]), evaluate=False)
if a not in terms:
terms[a] = 0
terms[a] += ct
break
terms[s] = c
# now let's construct new args:
# [2*x**2, x**3, 7*x**4, pi, ...]
+3 -16
View File
@@ -185,18 +185,11 @@ class Mul(Expr, AssocOp):
if not a.is_zero and a.is_Rational:
r, b = b.as_coeff_Mul()
if b.is_Add:
if r is not S.One: # 2-arg hack
if r is not S.One and a*r is S.One:
rv = [b], [], None
else:
# leave the Mul as a Mul
rv = [cls(a*r, b, evaluate=False)], [], None
elif global_distribute[0] and b.is_commutative:
r, b = b.as_coeff_Add()
bargs = [_keep_coeff(a, bi) for bi in Add.make_args(b)]
_addsort(bargs)
ar = a*r
if ar:
bargs.insert(0, ar)
bargs = [Add._from_args(bargs)]
rv = bargs, [], None
if rv:
return rv
@@ -612,12 +605,6 @@ class Mul(Expr, AssocOp):
c_part.insert(0, coeff)
# we are done
if (global_distribute[0] and not nc_part and len(c_part) == 2 and
c_part[0].is_Number and c_part[0].is_finite and c_part[1].is_Add):
# 2*(1+a) -> 2 + 2 * a
coeff = c_part[0]
c_part = [Add(*[coeff*f for f in c_part[1].args])]
return c_part, nc_part, order_symbols
def _eval_power(b, e):