Skip to content

Commit

Permalink
Merge pull request #153 from skirpichev/fix-omg31
Browse files Browse the repository at this point in the history
Fix wrong cancelation of expr with O terms in Add/Mul.flatten
  • Loading branch information
skirpichev committed Dec 15, 2015
2 parents 0cd9e5f + 7eb7000 commit 2ac6091
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
5 changes: 5 additions & 0 deletions sympy/core/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ def flatten(cls, seq):
sympy.core.mul.Mul.flatten
"""
from sympy.series.order import Order

rv = None
if len(seq) == 2:
a, b = seq
Expand Down Expand Up @@ -151,6 +153,9 @@ def flatten(cls, seq):
seq.extend(o.args) # TODO zerocopy?
continue

elif o.has(Order):
c, s = S.One, o

# Mul([...])
elif o.is_Mul:
c, s = o.as_coeff_Mul()
Expand Down
10 changes: 0 additions & 10 deletions sympy/core/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,16 +429,6 @@ def class_key(cls):

return 4, i, name

@property
def is_commutative(self):
"""
Returns whether the functon is commutative.
"""
if all(getattr(t, 'is_commutative') for t in self.args):
return True
else:
return False

def _eval_evalf(self, prec):
# Lookup mpmath function based on name
fname = self.func.__name__
Expand Down
9 changes: 7 additions & 2 deletions sympy/core/mul.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ def flatten(cls, seq):
you should only consider them when your code is highly performance
sensitive.
"""
from sympy.series.order import Order

rv = None
if len(seq) == 2:
a, b = seq
Expand Down Expand Up @@ -289,9 +291,12 @@ def flatten(cls, seq):
# o = b
b, e = o.as_base_exp()

if o.has(Order):
b, e = o, S.One

# y
# 3
if o.is_Pow:
elif o.is_Pow:
if b.is_Number:

# get all the factors with numeric base so they can be
Expand Down Expand Up @@ -343,7 +348,7 @@ def flatten(cls, seq):
# not an Add. This allow things like a**2*b**3 == a**5
# if a.is_commutative == False, but prohibits
# a**x*a**y and x**a*x**b from combining (x,y commute).
if b1 == b2 and (not new_exp.is_Add):
if b1 == b2 and (not new_exp.is_Add) and not (o.has(Order) or o1.has(Order)):
o12 = b1 ** new_exp

# now o12 could be a commutative object
Expand Down
8 changes: 7 additions & 1 deletion sympy/core/tests/test_arit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from sympy import (Basic, Symbol, sin, cos, exp, sqrt, Rational, Float, re, pi,
sympify, Add, Mul, Pow, Mod, I, log, S, Max, symbols, oo, Integer,
sign, im, nan, Dummy, factorial, comp
sign, im, nan, Dummy, factorial, comp, O
)
from sympy.core.compatibility import long, range
from sympy.utilities.iterables import cartes
Expand Down Expand Up @@ -1704,6 +1704,12 @@ def test_add_flatten():
assert (1/a).simplify() == (1/b).simplify() == 0


def test_omgissue_31():
assert sin(x + O(x**2)) - sin(x + O(x**2)) == \
Add(-sin(x + O(x**2)), sin(x + O(x**2)), evaluate=False)
assert sin(O(x))/sin(O(x)) == Mul(sin(O(x)), 1/sin(O(x)), evaluate=False)


def test_issue_5160_6087_6089_6090():
# issue 6087
assert ((-2*x*y**y)**3.2).n(2) == (2**3.2*(-x*y**y)**3.2).n(2)
Expand Down

0 comments on commit 2ac6091

Please sign in to comment.