I'm working through some exercises in my python book and was asked to implement an infinite product to estimate the sine function depending on some amount of iterations k. The code I wrote is the following:
def approx_sin(x,k):
def u(k):
if k==0:
return x
else:
return (1-((x**2)/((k**2)*(pi**2))))
u0=u(0)
yield u0
for n in range(1,k+1):
u0=u(n)*u0
yield u0
Which works as intended. Now I'm asked to sketch this function on some interval for different values of k, meaning we have to extract some values from the generator. To do this, I create the following function:
def sin_sketch(x,k):
return list(itertools.islice(approx_sin(x,k),k,k+1))[0]
And I can now plot sin_sketch(x,n) for some linspace x and a given value for n. My question boils down to, there has to be a better/more time efficient way to extract the value produced by itertools.islice(approx_sin(x,k),k-1,k)
rather then converting it to a list, then taking its only element. Any tips?