For example : NestList(f,x,3) ----> [x, f(x), f(f(x)), f(f(f(x)))]
http://reference.wolfram.com/mathematica/ref/NestList.html
You could write it as a generator:
def nestList(f,x,c):
for i in range(c):
yield x
x = f(x)
yield x
import math
print(list(nestList(math.cos, 1.0, 10)))
Or if you want the results as a list, you can append in a loop:
def nestList(f,x,c):
result = [x]
for i in range(c):
x = f(x)
result.append(x)
return result
import math
print(nestList(math.cos, 1.0, 10))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With