Skip to content

Commit 0cd788e

Browse files
Release Managervbraun
Release Manager
authored andcommitted
Trac #22004: Allow algorithm='sympy' in symbolic_sum function
I want this to work: {{{ sage: n = var('n') sage: sum(1/((2*n+1)^2-4)^2, n, 0, Infinity, algorithm='sympy') }}} See [https://ask.sagemath.org/question/35839/sage-incorrectly-evaluates- series/ this question on ask.sagemath.org] URL: https://trac.sagemath.org/22004 Reported by: slabbe Ticket author(s): Sébastien Labbé Reviewer(s): Ralf Stephan
2 parents c24e811 + 16acdcf commit 0cd788e

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/sage/calculus/calculus.py

+28
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,8 @@ def symbolic_sum(expression, v, a, b, algorithm='maxima', hold=False):
451451
452452
- ``'giac'`` - (optional) use Giac
453453
454+
- ``'sympy'`` - use SymPy
455+
454456
- ``hold`` - (default: ``False``) if ``True`` don't evaluate
455457
456458
EXAMPLES::
@@ -554,6 +556,22 @@ def symbolic_sum(expression, v, a, b, algorithm='maxima', hold=False):
554556
sage: symbolic_sum(1/(1+k^2), k, -oo, oo, algorithm = 'giac') # optional - giac
555557
(pi*e^(2*pi) - pi*e^(-2*pi))/(e^(2*pi) + e^(-2*pi) - 2)
556558
559+
SymPy can't solve that summation::
560+
561+
sage: symbolic_sum(1/(1+k^2), k, -oo, oo, algorithm = 'sympy')
562+
Traceback (most recent call last):
563+
...
564+
AttributeError: Unable to convert SymPy result (=Sum(1/(k**2 + 1),
565+
(k, -oo, oo))) into Sage
566+
567+
But SymPy can do this one for which Maxima is broken (see
568+
:trac:`22005`)::
569+
570+
sage: sum(1/((2*n+1)^2-4)^2, n, 0, Infinity, algorithm='sympy')
571+
1/64*pi^2
572+
sage: sum(1/((2*n+1)^2-4)^2, n, 0, Infinity)
573+
1/64*pi^2 - 1/12
574+
557575
Use Maple as a backend for summation::
558576
559577
sage: symbolic_sum(binomial(n,k)*x^k, k, 0, n, algorithm = 'maple') # optional - maple
@@ -632,6 +650,16 @@ def symbolic_sum(expression, v, a, b, algorithm='maxima', hold=False):
632650
raise ValueError("Giac cannot make sense of: %s" % sum)
633651
return result.sage()
634652

653+
elif algorithm == 'sympy':
654+
expression,v,a,b = [expr._sympy_() for expr in (expression, v, a, b)]
655+
from sympy import summation
656+
result = summation(expression, (v, a, b))
657+
try:
658+
return result._sage_()
659+
except AttributeError:
660+
raise AttributeError("Unable to convert SymPy result (={}) into"
661+
" Sage".format(result))
662+
635663
else:
636664
raise ValueError("unknown algorithm: %s" % algorithm)
637665

src/sage/misc/functional.py

+2
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,8 @@ def symbolic_sum(expression, *args, **kwds):
420420
421421
- ``'giac'`` - (optional) use Giac
422422
423+
- ``'sympy'`` - use SymPy
424+
423425
EXAMPLES::
424426
425427
sage: k, n = var('k,n')

src/sage/symbolic/expression.pyx

+1
Original file line numberDiff line numberDiff line change
@@ -11452,6 +11452,7 @@ cdef class Expression(CommutativeRingElement):
1145211452
1145311453
- ``'giac'`` - (optional) use Giac
1145411454
11455+
- ``'sympy'`` - use SymPy
1145511456
1145611457
EXAMPLES::
1145711458

0 commit comments

Comments
 (0)