0

I was using mpmath's nsum() function for summation operation from 1 to inf. like this but lambda is one line function and my equations are so long like ∑_(n=1)^∞▒e^(〖-n〗^2 )/(n^2+ 4〖(a-b)〗

for simple one line summation equations it works but for long summations how to use it? Is there any way by which we can give long summation equations to the nsum() function?

import mpmath
mpmath.mp.dps = 50
nsum(lambda x: exp(-x**2), [-inf, inf])
Georgy
  • 12,464
  • 7
  • 65
  • 73

2 Answers2

3

Everything that implements the __call__ method could be used:

Either a lambda:

nsum(lambda x: exp(-x**2), [-inf, inf])

Or a def function:

def exp_sum(x):
    return exp(-x**2)
nsum(exp_sum, [-inf, inf])

Or a class

class exp_sum:
    def __init__(self):
        pass
    def __call__(self,x):
        return exp(-x**2)

nsum(exp_sum(), [-inf, inf])
Uri Goren
  • 13,386
  • 6
  • 58
  • 110
1

You can substitute a normal function for the lambda:

def func(x):
    return exp(-x ** 2)

nsum(func, [-inf, inf])
mackorone
  • 1,056
  • 6
  • 15