For context, let me first define some stuff. For a given natural number n, let theta and eta be two positive vectors of length n and epsilon be a vector of -1 and 1s of length n as well.
I am trying to implement an algorithm that computes the finite sequence of real functions g=(g_1,...g_n) with g_n=0 which verifies the following recurrence relation :
g_(i-1)(x)=f_i(x) if x*epsilon_i > x_star * epsilon_i and 0 otherwise,
with f_i(x)=2eta_i*(x-theta_i)+g_i(x) and x_star the zero of f_i (I am saying "the zero" because f_i should be an increasing continuous piecewise affine function).
Below is my attempt. In the following code, computing_zero is an auxiliary function that allows me to compute x_star, the zero of f, assuming I know its breakpoints.
def computing_g(theta,epsilon,eta):
n=len(theta)
g=[lambda x:0,lambda x:2*eta[n-1]*max(0,x-theta[n-1])] # initialization of g : g=[g_n,g_(n-1)]
breakpoints_of_f=[theta[n-1]]
for i in range(1,n):
f= lambda x:2*eta[n-1-i]*(x-theta[n-1-i])+g[i](x)
x_star=computing_zero(breakpoints_of_f,f)
breakpoints_of_f.append(x_star)
g.append(lambda x: f(x) if epsilon[n-1-i]*x > epsilon[n-1-i]*x_star else 0)
return(breakpoints_of_f,g)
Whenever i run the algorithm, i get the following error :
line 6, in <lambda>
f= lambda x:2*eta[n-1-i]*(x-theta[n-1-i])+g[i](x)
line 9, in <lambda>
g.append(lambda x: f(x) if epsilon[n-1-i]*x > epsilon[n-1-i]*x_star else 0)
RecursionError: maximum recursion depth exceeded in comparison
I suppose there is some sort of infinite loop somewhere, but I am unable to identify where.
g = [],for i in range(5):,f = lambda: i,g.append(lambda: f()),for pos in range(len(g)):,print(g[pos]()), and the output was "4" * 5 If I modifyibefore the second loop, then that's the value that will be displayed.fusing a Closure instead of a lambda function, so that the value ofican be set at the time the function is created instead of being a free symbol. The lookups from theta and eta would be handled at that time as well.