Skip to content

Commit

Permalink
core: avoid deep expanding in Mul._eval_nseries()
Browse files Browse the repository at this point in the history
This finally fix diofant#1139
Closes sympy/sympy#21176
  • Loading branch information
skirpichev committed Aug 15, 2021
1 parent 5efe644 commit 3c45e95
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 12 deletions.
4 changes: 3 additions & 1 deletion diofant/core/mul.py
Expand Up @@ -1382,7 +1382,9 @@ def ndiv(a, b):
def _eval_nseries(self, x, n, logx):
from ..simplify import powsimp
terms = [t.nseries(x, n=n, logx=logx) for t in self.args]
return powsimp(self.func(*terms).expand(), combine='exp', deep=True)
return powsimp(self.func(*terms).expand(power_base=False, power_exp=False,
mul=False).expand(deep=False),
combine='exp', deep=True)

def _eval_as_leading_term(self, x):
return self.func(*[t.as_leading_term(x) for t in self.args])
Expand Down
2 changes: 1 addition & 1 deletion diofant/tests/concrete/test_gosper.py
Expand Up @@ -50,7 +50,7 @@ def test_gosper_sum():
# issue sympy/sympy#6033:
assert gosper_sum(
n*(n + a + b)*a**n*b**n/(factorial(n + a)*factorial(n + b)),
(n, 0, m)).rewrite(factorial) == \
(n, 0, m)).rewrite(factorial).powsimp() == \
-a*b*(a**m*b**m*factorial(a) *
factorial(b) - factorial(a + m)*factorial(b + m))/(factorial(a) *
factorial(b)*factorial(a + m)*factorial(b + m))
Expand Down
2 changes: 1 addition & 1 deletion diofant/tests/series/test_limits.py
Expand Up @@ -509,7 +509,7 @@ def test_sympyissue_11526():
df = diff(1/(a*log((x - b)/(x - c))), x)
res = -1/(-a*c + a*b)
assert limit(df, x, oo) == res
assert limit(simplify(df), x, oo) == res
assert (limit(simplify(df), x, oo) - res).simplify() == 0

e = log((1/x - b)/(1/x - c))
assert e.as_leading_term(x) == x*(c - b)
Expand Down
12 changes: 10 additions & 2 deletions diofant/tests/series/test_residues.py
@@ -1,7 +1,7 @@
import pytest

from diofant import (Function, I, Rational, Symbol, cot, exp, factorial, log,
pi, residue, root, sin, sqrt, tanh)
from diofant import (Function, I, Mul, Rational, Symbol, cot, exp, factorial,
log, pi, residue, root, sin, sqrt, tan, tanh)
from diofant.abc import a, s, x, z


Expand Down Expand Up @@ -85,3 +85,11 @@ def test_sympyissue_21177():

assert residue(e1, x, pt) == ans
assert residue(e2, x, pt) == ans


def test_sympyissue_21176():
e = x**2*cot(pi*x)/(x**4 + 1)
pt = -sqrt(2)/2 - sqrt(2)*I/2
assert residue(e, x, pt) == sqrt(2)*I/Mul(2, -2 + 2*I, tan(sqrt(2)*pi/2 +
sqrt(2)*I*pi/2),
evaluate=False)
12 changes: 6 additions & 6 deletions diofant/tests/series/test_series.py
Expand Up @@ -261,12 +261,12 @@ def test_sympyissue_21245():
e = 1/(1 - x - x**2)
assert (e.series(x, 1/fi, 2) ==
-sqrt(5)/(Mul(5, x - 1/(Rational(1, 2) + sqrt(5)/2),
evaluate=False)) + sqrt(5)/(5 + 5*sqrt(5)) +
1/(5 + 5*sqrt(5)) +
(x - 1/(Rational(1, 2) + sqrt(5)/2))*(-6*sqrt(5)/(50*sqrt(5) + 150) -
10/(50*sqrt(5) + 150)) +
O((x - sqrt(5)/2 + Rational(1, 2))**2,
(x, -Rational(1, 2) + sqrt(5)/2)))
evaluate=False)) + 1/(sqrt(5) + 5) +
sqrt(5)/(Mul(5, sqrt(5) + 5, evaluate=False)) +
(x - 1/(Rational(1, 2) + sqrt(5)/2)) *
(-6*sqrt(5)/(Mul(5, 10*sqrt(5) + 30, evaluate=False)) -
2/(10*sqrt(5) + 30)) + O((x - sqrt(5)/2 + Rational(1, 2))**2,
(x, -Rational(1, 2) + sqrt(5)/2)))


def test_diofantissue_1139():
Expand Down
3 changes: 2 additions & 1 deletion diofant/tests/test_wester.py
Expand Up @@ -2017,7 +2017,8 @@ def test_X6():
# Taylor series of nonscalar objects (noncommutative multiplication)
# expected result => (B A - A B) t^2/2 + O(t^3) [Stanly Steinberg]
a, b = symbols('a b', commutative=False, scalar=False)
assert (series(exp((a + b)*x) - exp(a*x) * exp(b*x), x, x0=0, n=3) ==
assert (series(exp((a + b)*x) - exp(a*x) * exp(b*x), x,
x0=0, n=3).expand().collect(x) ==
x**2*(-a*b/2 + b*a/2) + O(x**3))


Expand Down
1 change: 1 addition & 0 deletions docs/release/notes-0.13.rst
Expand Up @@ -116,3 +116,4 @@ These Sympy issues also were addressed:
* :sympyissue:`21785`: Limit gives TypeError from as_leading_term
* :sympyissue:`21812`: LambertW displaying in jupyter lab
* :sympyissue:`21814`: Printing of unevaluated Mul needs brackets
* :sympyissue:`21176`: Incorrect residue of x**2*cot(pi*x)/(x**4 + 1)

0 comments on commit 3c45e95

Please sign in to comment.