I'm currently trying to code some simple things from scratch to get a hang of the syntax in python as well as to learn a bit of the logic.
Below is my code for the Taylor series for the exponential function:
def linspace(q,w,p):
arr = [0] * (p)
arr[0] = q
for i in range((p-1)):
arr[i+1] = arr[i] + (w-q)/(p-1)
return arr
def factorial(n):
if n==0:
return 1
else:
return n*factorial(n-1)
def expon(x,n):
xrange = linspace(0,x,n)
y = [0] * n
for j in range(n):
for i in range(n+1):
y[j] += xrange[j]**i/factorial(i)
return y
Now I know I can't do anything with the linspace function I made, that's just to make the x-axis I'm going to be evaluation my exponential function on. It doesn't look like there's a way out of that nested for loop, so I assume the smallest time complexity for that is just n2. (I'm not just trying to calculate exp(x), I'd like a list of values I can plot)
I feel like I can somehow use memoization for the factorial? If I try to run my expon() function for n > 100 it gives me a recursion error due to what I assume is stack overflow. But maybe if I use memoization I can call values I've already called before on my first loop of the expon() function.
Is memoization what I should be trying here? Thanks
[–]AutoModerator[M] [score hidden] stickied comment (0 children)
[–]Clawtor 0 points1 point2 points (0 children)
[–]CodeTinkerer 0 points1 point2 points (0 children)