Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong result in the convergence of infinite sums with factorials #16699

Closed
maurogaravello opened this issue Apr 20, 2019 · 8 comments
Closed
Labels
concrete Could Close Wrong Result The output produced by SymPy is mathematically incorrect.

Comments

@maurogaravello
Copy link
Contributor

>>> n = Symbol('n', integer = True)
>>> expr = factorial(n) / factorial(n+2)
>>> s = Sum(expr, (n, 1, oo))
>>> print(s.is_convergent())
False

The correct answer is True

@asmeurer
Copy link
Member

Sum(simplify(expr), (n, 1, oo)).is_convergent() gives the right answer (for some reason simplify(s) doesn't work).

@asmeurer asmeurer added concrete Wrong Result The output produced by SymPy is mathematically incorrect. labels Apr 20, 2019
@maurogaravello
Copy link
Contributor Author

The different behaviour originates in the code line

order = O(sequence_term, (sym, S.Infinity))

If you use simplify, then order is n**(-2). In the other case is 1.
Both results are correct, even if the latter is not sharp.
I think that the code lines

            if p1_series_test[p] >= -1:
                return S.false

are not logically correct, since, in case order does not return a sharp result, these lines can produce a wrong result.

@AnimeshSinha1309
Copy link
Contributor

Can I take up this issue and try to fix it?

@maurogaravello
Copy link
Contributor Author

@AnimeshSinha1309 Sure.

@AnimeshSinha1309
Copy link
Contributor

Is it not just possible to add the line

sequence_term = simplify(self.function)

Simplifying the terms first should always make it easier to test the convergence of any series summation.

AnimeshSinha1309 added a commit to AnimeshSinha1309/sympy-repo that referenced this issue Apr 27, 2019
Added simplification of function, before testing for convergence and added tests. Fixed sympy#16699.
@czgdp1807
Copy link
Member

Current status,

>>> n = Symbol('n', integer=True)
>>> expr = factorial(n)/factorial(n+2)
>>> s = Sum(expr, (n, 1, oo))
>>> print(s.is_convergent())
Traceback (most recent call last):
  File "/usr/lib/python3.6/code.py", line 91, in runcode
    exec(code, self.locals)
  File "<console>", line 1, in <module>
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/concrete/summations.py", line 616, in is_convergent
    "is not yet implemented" % (sequence_term))
NotImplementedError: The algorithm to find the Sum convergence of factorial(n)/factorial(n + 2) is not yet implemented

It looks like the issue is outdated. May be we can close this.

@czgdp1807
Copy link
Member

>>> n = Symbol('n', integer=True)
>>> expr = factorial(n)/factorial(n+2)
>>> s = Sum(expr, (n, 1, oo))
>>> print(s.is_convergent())
^CTraceback (most recent call last):
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/assumptions.py", line 454, in getit
    return self._assumptions[fact]
KeyError: 'zero'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/assumptions.py", line 454, in getit
    return self._assumptions[fact]
KeyError: 'extended_positive'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.6/code.py", line 91, in runcode
    exec(code, self.locals)
  File "<console>", line 1, in <module>
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/concrete/summations.py", line 458, in is_convergent
    lim_val = limit_seq(sequence_term, sym)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/series/limitseq.py", line 232, in limit_seq
    L1 = _limit_seq(expr.xreplace({n: n_}), n_, trials)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/series/limitseq.py", line 119, in _limit_seq
    result = _limit_inf(expr, n)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/series/limitseq.py", line 109, in _limit_inf
    return Limit(expr, n, S.Infinity).doit(deep=False)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/series/limits.py", line 260, in doit
    r = gruntz(e, z, z0, dir)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/series/gruntz.py", line 669, in gruntz
    r = limitinf(e0, z)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/cache.py", line 94, in wrapper
    retval = cfunc(*args, **kwargs)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/series/gruntz.py", line 433, in limitinf
    c0, e0 = mrv_leadterm(e, x)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/cache.py", line 94, in wrapper
    retval = cfunc(*args, **kwargs)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/series/gruntz.py", line 522, in mrv_leadterm
    series = calculate_series(f, w, logx=logw)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/series/gruntz.py", line 475, in calculate_series
    for t in e.lseries(x, logx=logx):
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/expr.py", line 2990, in yield_lseries
    for si in s:
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/expr.py", line 3212, in _eval_lseries
    series = self._eval_nseries(x, n=n, logx=logx)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/mul.py", line 1761, in _eval_nseries
    terms = [t.nseries(x, n=n, logx=logx) for t in self.args]
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/mul.py", line 1761, in <listcomp>
    terms = [t.nseries(x, n=n, logx=logx) for t in self.args]
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/expr.py", line 3289, in nseries
    return self._eval_nseries(x, n=n, logx=logx)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/functions/elementary/exponential.py", line 437, in _eval_nseries
    arg_series = arg._eval_nseries(x, n=n, logx=logx)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/add.py", line 405, in _eval_nseries
    terms = [t.nseries(x, n=n, logx=logx) for t in self.args]
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/add.py", line 405, in <listcomp>
    terms = [t.nseries(x, n=n, logx=logx) for t in self.args]
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/expr.py", line 3289, in nseries
    return self._eval_nseries(x, n=n, logx=logx)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/functions/special/gamma_functions.py", line 995, in _eval_nseries
    return super()._eval_nseries(x, n, logx)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/function.py", line 685, in _eval_nseries
    return self._eval_aseries(n, args0, x, logx)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/functions/special/gamma_functions.py", line 1011, in _eval_aseries
    return (r + Add(*l))._eval_nseries(x, n, logx) + o
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/add.py", line 405, in _eval_nseries
    terms = [t.nseries(x, n=n, logx=logx) for t in self.args]
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/add.py", line 405, in <listcomp>
    terms = [t.nseries(x, n=n, logx=logx) for t in self.args]
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/expr.py", line 3289, in nseries
    return self._eval_nseries(x, n=n, logx=logx)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/mul.py", line 1761, in _eval_nseries
    terms = [t.nseries(x, n=n, logx=logx) for t in self.args]
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/mul.py", line 1761, in <listcomp>
    terms = [t.nseries(x, n=n, logx=logx) for t in self.args]
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/expr.py", line 3289, in nseries
    return self._eval_nseries(x, n=n, logx=logx)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/power.py", line 1567, in _eval_nseries
    return (1/denominator)._eval_nseries(x, n=n, logx=logx)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/power.py", line 1509, in _eval_nseries
    rest = expand_mul((b - prefactor)/prefactor)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/decorators.py", line 251, in _func
    return func(self, other)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/decorators.py", line 127, in binary_op_wrapper
    return func(self, other)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/expr.py", line 235, in __div__
    return Mul(self, Pow(other, S.NegativeOne))
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/cache.py", line 94, in wrapper
    retval = cfunc(*args, **kwargs)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/operations.py", line 52, in __new__
    c_part, nc_part, order_symbols = cls.flatten(args)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/mul.py", line 197, in flatten
    if not a.is_zero and a.is_Rational:
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/assumptions.py", line 458, in getit
    return _ask(fact, self)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/assumptions.py", line 513, in _ask
    _ask(pk, obj)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/assumptions.py", line 513, in _ask
    _ask(pk, obj)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/assumptions.py", line 501, in _ask
    a = evaluate(obj)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/expr.py", line 861, in _eval_is_positive
    extended_positive = self.is_extended_positive
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/assumptions.py", line 458, in getit
    return _ask(fact, self)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/assumptions.py", line 501, in _ask
    a = evaluate(obj)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/add.py", line 644, in _eval_is_extended_positive
    v = _monotonic_sign(a)
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/exprtools.py", line 150, in _monotonic_sign
    n, d = self.as_numer_denom()
  File "/home/czgdp1807ssd/sympy_project/sympy/sympy/core/add.py", line 505, in as_numer_denom
    denoms, numers = [list(i) for i in zip(*iter(nd.items()))]
KeyboardInterrupt

@czgdp1807
Copy link
Member

Closing as the issue is outdated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
concrete Could Close Wrong Result The output produced by SymPy is mathematically incorrect.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants