python写泰勒展开式
import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl import math #e^x = 1 + x + x^2/2!+... def calc_e_small(x): n = 10 #累乘 cumsum是求和 #1! 2! 3! 4! 5!...10! f = np.arange(1,n+1).cumprod() #x x^2 ... x^10 b = np.array([x]*n).cumprod() return 1+np.sum(b / f) ''' e^x = ln2 + (e^ln2)/1!*(x-ln2) + (e^ln2)/2!*(x-ln2)^2+... x = a*ln2 + b k<= z |b| <= 1/2ln2 a = ln( int( x/ln2 + 0.5 ) ) b = x-a*ln2 e^x = 2^a + e^b ''' def calc_e(x): reverse = False if x < 0:#处理负数 exp(-x) = 1/exp(x) x = -x reverse = True ln2 = 0.69314718055994530941723212145818 c = x/ln2 a = int(c+0.5) b = x-a*ln2