I'd like to figure out how to code the following pseudo-code:
# Base-case
u_0(x) = x^3
for i in [0,5):
u_(i+1)(x) = u_(i)(x)^2
So that in the end I can call u_5(x)
, for example.
The difficulty I'm having with accomplishing the above is finding a way to index Python functions by i
so that I can iteratively define each function.
I tried using recursion with two functions in place of indexing but I get "maximum recursion depth exceeded".
Here is a minimal working example:
import math
import sympy as sym
a,b = sym.symbols('x y')
def f1(x,y):
return sym.sin(x) + sym.cos(y)*sym.tan(x*y)
for i in range(0,5):
def f2(x,y):
return sym.diff(f1(x,y),x) + sym.cos(sym.diff(f1(x,y),y,y))
def f1(x,y):
return f2(x,y)
print(f2(a,b))