Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d1987903ea | ||
|
|
ccdfbb303e | ||
|
|
2b925c04b4 | ||
|
|
b9ec8595e0 | ||
|
|
f4e57c5b6a | ||
|
|
5bb4fc088c | ||
|
|
04c69d820c | ||
|
|
bd01e3802f | ||
|
|
cb1504df5f | ||
|
|
fb6cbe8491 | ||
|
|
e50379d959 | ||
|
|
033c3ef8c6 |
+47
-1
@@ -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
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user