-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
49 lines (33 loc) · 899 Bytes
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import sympy as sp
import math
def main() -> None:
#setting up conditions
x = sp.symbols("x")
e = math.e
f = e**x
n = 5
constant = compute_constants(f, x, n)
t = maclaurin(constant, x, n)
final = sum(t)
print(final)
def take_derivative(f, x, n):
return sp.diff(f, x, n)
def take_factorial(number):
return sp.factorial(number)
def compute_constants(f, x, n):
constants = []
for i in range(n+1):
derivative = take_derivative(f, x, i)
at_zero = derivative.subs(x, 0)
factorial = take_factorial(i)
constants.append(at_zero/factorial)
return constants
def construct_term(constant, x, n):
return constant * (x**n)
def maclaurin(constants, x, n):
terms = []
for i in range(n+1):
terms.append(construct_term(constants[i], x, i))
return terms
if __name__ == '__main__':
main()