Python Is it possible to evaluate a sum in SymPy without breaking the expression?

  • Thread starter Thread starter Gaussian97
  • Start date Start date
  • Tags Tags
    Sum
Click For Summary
The discussion focuses on evaluating the infinite sum using Python's SymPy library. A user initially attempts to compute the sum of exp(-n*x) from n=1 to infinity but encounters an issue where SymPy returns an unevaluated sum. They successfully find a workaround by substituting x with exp(-x) after breaking the expression into x^(-n). However, they seek a method to evaluate the sum directly without this substitution. It is noted that while SymPy can recognize certain series, it does not automatically simplify the sum of exp(-n*x) to a known form. Additionally, the importance of checking the range of summation is highlighted, with examples demonstrating how to use Piecewise functions in SymPy to handle different conditions for convergence.
Gaussian97
Homework Helper
Messages
683
Reaction score
412
TL;DR
SymPy doesn't evaluate a sum
I want to evaluate the sum

gif.gif


using Python and SymPy, I'm relatively new using SymPy and the first thing I tried was

Python:
from sympy import exp, oo, summation, symbols

n, x = symbols('n,x')
summation(exp(-n*x), (n, 1, oo))

But it doesn't work, it just returns the unevaluated sum. I can make it work using

Python:
from sympy import exp, oo, summation, symbols

n, x = symbols('n,x')
f = exp(-x)
summation(x**(-n), (n, 1, oo)).subs(x,f)

But I would like to know if it is possible to make it work without need to break the expression into x^n and then substitute x by e^-x.

Thank you
 
Technology news on Phys.org
Probably not.

Sympy knows that \sum_{n=1}^\infty z^n = z/(1 - z) when |z| < 1, but can't work out that \sum_{n=1}^\infty e^{-nx} reduces to that on substituting z = e^{-x}. You have to do that yourself.

You may also want to check the range of your summation:
Python:
>>> from sympy import *
>>> x = Dummy('x', real = True, positive=True)
>>> summation(x**n, (n, 1, oo))
Piecewise((_x/(1 - _x), _x < 1), (Sum(_x**n, (n, 1, oo)), True))
>>> summation(x**n, (n, 0, oo))
Piecewise((1/(1 - _x), _x < 1), (Sum(_x**n, (n, 0, oo)), True))
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 9 ·
Replies
9
Views
9K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K