Skip to content

Commit

Permalink
Merge pull request #1405 from skirpichev/misc
Browse files Browse the repository at this point in the history
Misc fixes
  • Loading branch information
skirpichev committed May 12, 2024
2 parents 6ccc6ae + 4da4924 commit 2def774
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 43 deletions.
6 changes: 4 additions & 2 deletions diofant/calculus/limits.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .gruntz import limitinf


def limit(expr, z, z0, dir=None):
def limit(expr, z, z0, dir=None, **kwargs):
"""
Compute the directional limit of ``expr`` at the point ``z0``.
Expand All @@ -25,7 +25,9 @@ def limit(expr, z, z0, dir=None):
Limit
"""
return Limit(expr, z, z0, dir).doit(deep=False)
heuristics = kwargs.pop('heuristics', True)
return Limit(expr, z, z0, dir).doit(deep=False,
heuristics=heuristics)


def heuristics(e, z, z0, dir):
Expand Down
69 changes: 31 additions & 38 deletions diofant/core/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2418,37 +2418,38 @@ def series(self, x=None, x0=0, n=6, dir=None, logx=None):
return collect(s1.removeO(), x) + target_order
except NotImplementedError: # XXX parse_derivative of radsimp.py
return s1 + target_order
else: # lseries handling
def yield_lseries(s):
"""Return terms of lseries one at a time."""
for si in s:
if not si.is_Add:
yield si

# lseries handling
def yield_lseries(s):
"""Return terms of lseries one at a time."""
for si in s:
if not si.is_Add:
yield si
continue
# yield terms 1 at a time if possible
# by increasing order until all the
# terms have been returned
yielded = 0
o = Order(si, x)*x
if expand_mul(o.expr).is_Add:
raise NotImplementedError
ndid = 0
ndo = len(si.args)
while 1:
do = (si - yielded + o).removeO()
o *= x
if not do or do.is_Order:
continue
# yield terms 1 at a time if possible
# by increasing order until all the
# terms have been returned
yielded = 0
o = Order(si, x)*x
if expand_mul(o.expr).is_Add:
raise NotImplementedError
ndid = 0
ndo = len(si.args)
while 1:
do = (si - yielded + o).removeO()
o *= x
if not do or do.is_Order:
continue
if do.is_Add:
ndid += len(do.args)
else:
ndid += 1
yield do
if ndid == ndo:
break
yielded += do
if do.is_Add:
ndid += len(do.args)
else:
ndid += 1
yield do
if ndid == ndo:
break
yielded += do

return yield_lseries(self.removeO()._eval_lseries(x, logx=logx))
return yield_lseries(self.removeO()._eval_lseries(x, logx=logx))

def taylor_term(self, n, x, *previous_terms):
"""General method for the taylor term.
Expand Down Expand Up @@ -2684,12 +2685,7 @@ def compute_leading_term(self, x, logx=None):
This is a wrapper to compute a series first.
"""
from ..functions import log
from .symbol import Dummy

d = logx if logx else Dummy('logx')

for t in self.series(x, n=None, logx=d):
for t in self.series(x, n=None, logx=logx):
t = t.cancel()

is_zero = t.equals(0)
Expand All @@ -2699,9 +2695,6 @@ def compute_leading_term(self, x, logx=None):
break
raise NotImplementedError(f'Zero-decision problem for {t}')

if logx is None:
t = t.subs({d: log(x)})

return t.as_leading_term(x)

@cacheit
Expand Down
3 changes: 0 additions & 3 deletions diofant/core/power.py
Original file line number Diff line number Diff line change
Expand Up @@ -1123,8 +1123,6 @@ def _eval_nseries(self, x, n, logx):
from ..simplify import powsimp
if self.is_Exp:
e_series = self.exp.nseries(x, n, logx)
if e_series.is_Order:
return 1 + e_series
e0 = limit(e_series.removeO(), x, 0)
if e0 in (-oo, oo):
return self
Expand All @@ -1139,7 +1137,6 @@ def _eval_nseries(self, x, n, logx):
return powsimp(exp_series, deep=True, combine='exp')
if self.exp.has(x):
return exp(self.exp*log(self.base)).nseries(x, n, logx)

b_series = self.base.nseries(x, n, logx)
while b_series.is_Order:
n += 1
Expand Down
2 changes: 2 additions & 0 deletions diofant/tests/calculus/test_limits.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ def test_heuristic():
assert heuristics(sin(1/x) + atan(x), x, 0, -1) == sin(oo)
assert heuristics(log(2 + sqrt(atan(x))*sin(1/x)), x, 0, -1) == log(2)
assert heuristics(tan(tan(1/x)), x, 0, -1) is None
assert isinstance(limit(log(2 + sqrt(atan(x))*sin(1/x)),
x, 0, -1, heuristics=False), Limit)


def test_sympyissue_3871():
Expand Down

0 comments on commit 2def774

Please sign in to comment.