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
AI Thread 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 Summary
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))
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
1
Views
3K
Replies
1
Views
2K
Replies
2
Views
3K
Replies
4
Views
4K
Replies
15
Views
3K
Replies
9
Views
3K
Replies
15
Views
2K
Back
Top