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

  • Python
  • Thread starter Gaussian97
  • Start date
  • Tags
    Sum
In summary, the conversation discusses the use of Python and SymPy to evaluate a sum. The speaker is new to SymPy and has encountered issues with getting the desired output. They mention using the exp() function and the summation() function, but note that it only returns an unevaluated sum. The speaker then shares a workaround using the subs() function, but expresses a desire to find a solution without having to break the expression into x^n and substituting x by e^-x. However, the expert summarizer notes that it is not possible for SymPy to automatically reduce the expression in this way and suggests checking the range of the summation.
  • #1
Gaussian97
Homework Helper
683
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
 
  • Like
Likes pbuk
Technology news on Phys.org
  • #2
Probably not.

Sympy knows that [itex]\sum_{n=1}^\infty z^n = z/(1 - z)[/itex] when [itex]|z| < 1[/itex], but can't work out that [itex]\sum_{n=1}^\infty e^{-nx}[/itex] reduces to that on substituting [itex]z = e^{-x}[/itex]. 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))
 
  • Like
Likes WWGD

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

How do I evaluate a sum in SymPy?

To evaluate a sum in SymPy, you can use the summation() function. This function takes in three arguments: the summation variable, the expression to be summed, and a tuple specifying the range of the summation variable. For example, to evaluate the sum of i from 1 to 5, you would use summation(i, (i, 1, 5)).

Can SymPy evaluate infinite sums?

Yes, SymPy can evaluate infinite sums using the Sum() function. This function takes in the summation variable, the expression to be summed, and the range of the summation variable as arguments. To specify an infinite range, you can use S.Infinity. For example, to evaluate the infinite sum of 1/n from n=1 to infinity, you would use Sum(1/n, (n, 1, S.Infinity)).

How do I simplify the result of a sum in SymPy?

To simplify the result of a sum in SymPy, you can use the simplify() function. This function takes in the expression to be simplified as an argument. If the result of your sum is a fraction, you can also use the cancel() function to simplify the fraction. Additionally, you can use the simplify() function with the rational=True argument to force the result to be a rational number.

Can SymPy evaluate multiple sums?

Yes, SymPy can evaluate multiple sums using the summation() function with multiple summation variables and ranges specified as tuples. For example, to evaluate the sum of i from 1 to 5 and the sum of j from 1 to 3, you would use summation(i, (i, 1, 5), j, (j, 1, 3)).

Is it possible to evaluate sums with more complex expressions in SymPy?

Yes, SymPy can evaluate sums with more complex expressions using the summation() function. The expression to be summed can include any valid SymPy expression, such as variables, functions, and mathematical operations. Additionally, you can use summation() with Symbol() and Function() to create more complex expressions to be summed.

Similar threads

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