简体   繁体   English

处理lambda函数时python中的递归深度错误

[英]recursion depth error in python when dealing with lambda functions

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.对于给定的自然数 n,令 theta 和 eta 是长度为 n 的两个正向量,而 epsilon 也是长度为 n 的 -1 和 1s 的向量。

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_n=0 的实函数 g=(g_1,...g_n) 的有限序列,该算法验证以下递归关系:

g_(i-1)(x)=f_i(x) if x*epsilon_i > x_star * epsilon_i and 0 otherwise, g_(i-1)(x)=f_i(x) 如果 x*epsilon_i > x_star * epsilon_i 否则为 0,

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). f_i(x)=2eta_i*(x-theta_i)+g_i(x) 和 x_star 是 f_i 的零(我说的是“零”,因为 f_i 应该是一个递增的连续分段仿射函数)。

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.在下面的代码中,computing_zero 是一个辅助函数,它允许我计算 x_star,即 f 的零,假设我知道它的断点。

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.我想某处有某种无限循环,但我无法确定在哪里。

I took a stab at writing this with closures, but I can't verify if the results of the math are what you expected, as you didn't provide code for the function calculating zero, so I just made something up.我尝试用闭包写这个,但我无法验证数学结果是否符合您的预期,因为您没有为计算零的函数提供代码,所以我只是编造了一些东西。 I'm pretty sure that this should avoid the recursion issue you're seeing.我很确定这应该避免您看到的递归问题。 Another change I made, is instead of using [n-1-i] for all of the indexes in the loop, I changed the loop to start at 2, and then every index check is just [-i] .我所做的另一个更改是,我没有对循环中的所有索引使用[n-1-i] ,而是将循环更改为从 2 开始,然后每个索引检查都只是[-i] The exception is when looking up the function in list g to use in calculating generating function f.例外是在列表 g 中查找用于计算生成函数 f 的函数时。 There the index is now [i-1] instead of [i] .那里的索引现在是[i-1]而不是[i]

def get_func_f(eta, theta, i, g):
    """Generate function f(x)"""
    eta_i = eta[-i]
    theta_i = theta[-i]
    g_i = g[i-1]  # This is the one index working from left to right, and i will always be len(g)+1
    def f(x):
        return 2 * eta_i * (x - theta_i) + g_i(x)
    return f


def get_func_g(f, epsilon_i, x_star):
    """generate function g(x)"""
    def g(x):
        if epsilon_i * x > epsilon_i * x_star:
            return f(x)
        else:
            return 0
    return g


def computing_g(theta,epsilon,eta):
    n=len(theta)
    g=[lambda x:0,lambda x:2*eta[-1]*max(0,x-theta[-1])] # initialization of g : g=[g_n,g_(n-1)]
    breakpoints_of_f=[theta[-1]]
    for i in range(2,n):   # Start at 2 and just use [-i] instead of [n-1-i] everywhere.
        f = get_func_f(eta, theta, i, g)
        x_star=computing_zero(breakpoints_of_f,f)
        breakpoints_of_f.append(x_star)
        g.append(get_func_g(f, epsilon[-i], x_star))
        #print(f"{breakpoints_of_f=}\n{g}")
    return(breakpoints_of_f,g)


def computing_zero(a, b):
    """Completely made up as example code wasn't provided."""
    return -(a[-1]+b(a[-1]))

answer = computing_g(theta=[0.5,0.3,0.2,0.1],epsilon=[1,-1,1,-1],eta=[1,3,2,4])
print(f"breakpoints: {answer[0]}\ng={answer[1]}")

Output:输出:

breakpoints: [0.1, 0.30000000000000004, -0.3000000000000004]
g=[<function computing_g.<locals>.<lambda> at 0x0000023F329CB670>, <function computing_g.<locals>.<lambda> at 0x0000023F329CB700>, <function get_func_g.<locals>.g at 0x0000023F329CB820>, <function get_func_g.<locals>.g at 0x0000023F329CB940>]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM