In [50]:
# Generate e using infinite series
import sympy
from sympy.mpmath import mp
mp.dps = 50
mp.pretty = True
n = sympy.symbols('n', integer=True)
result = sympy.summation(1/sympy.factorial(n), (n, 0, sympy.oo))
print("The sum is: {}".format(result))
str(mp.e)
Out[50]:
In [59]:
import sympy as sy
# Custom (mathematic) function:
# Compute the reciprocal of the factorial of an integer
class factorial_reciprocal(sy.Function):
nargs = 1
@classmethod
def eval(cls, arg):
#if arg < 0:
# raise ValueError('The factorial function takes only arguments greater than or equal to zero (0).')
return 1 / sy.factorial(arg)
x = sy.var('x')
print("Sum: {}".format( sy.summation(factorial_reciprocal(x), (x, 0, sy.oo)) ))
print( factorial_reciprocal(x).series(x, x0=0, n=3) )
In [ ]: