Python 在函数中使用 Lambda [英] Python Using Lambda Within a Function

查看:59
本文介绍了Python 在函数中使用 Lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个名为 calc(f,a,b) 的函数,其中 x 是一个带有变量 f 的方程,我想将此代码放在函数中.

 def calc(f, a, b):限制 = [a, b]积分 = odeint(lambda y, x : f, 0, 限制)返回积分[1]

此函数使用内置的 odeint 函数获取积分.这就是我想要做的

print calc(x**2, 0, 1)

其中 x^2 是要集成的函数.我的问题是这个函数 (x**2) 需要在 y, x: f 之后立即传递给 odeint 函数,其中分号后面的 f 是 calc(f,a,b)

中的 f

我无法弄清楚的是如何将 f 从 calc 函数输入传递到内部的 odeint .它说 f 没有声明,如果我把它放在字符串中......它不起作用

当我运行这个函数时..它不起作用我得到这个错误

NameError: name 'f' 未定义

我不知道如何将我的方程传递到 odeint 中

谢谢

解决方案

如果重写函数 calc 如下:

def calc(f, a, b):限制 = [a, b]积分 = odeint(lambda y, x: f(x), 0, limits)返回积分[1][0]

那么可以这样使用这个函数:

<预><代码>>>>calc(lambda x: x ** 2, 0, 1) # 在区间 [0, 1] 上对 x ** 2 进行积分(预期答案:0.333...)0.33333335809177234>>>calc(lambda x: x, 0, 1) # 在区间 [0, 1] 上对 x 进行积分(预期答案:0.5)0.50000001490120016>>>calc(lambda x: 1, 0, 1) # 在区间 [0, 1] 上积分 1(预期答案:1.0)1.0

scipy.integrate 模块中的 odeint 函数具有签名:

odeint(func, y0, t, ...)

其中:func 是一个可调用的,它接受参数 y, t0, ... 并在给定点返回 dy/dt;y0 是表示 y 初始条件的序列;t 是一个序列,表示要求解 y 的区间(t0 是序列中的第一项).

您似乎正在求解区间 [a, b] 上 dy/dx = f(x) 形式的一阶微分方程,其中 y0 = 0.在这种情况下,当您通过 f(其中接受一个参数)到函数 odeint,您必须将它包装在一个 lambda 中,以便传入的函数接受两个参数(y 和 x——y 参数基本上被忽略,因为您不需要将它用于一阶微分等式).

I am trying to create a function called calc(f,a,b) where x is an equation with the variable f and I want to put this code within the function.

   def calc(f, a, b):
       limits = [a, b]
       integral = odeint(lambda y, x : f, 0, limits)

       return integral[1]  

This function gets the integral using the built in odeint function. This is what I am trying to do

print calc(x**2, 0, 1)

where x^2 is the function to be integrated. My problem is that this function (x**2)needs to be passed on to the odeint function right after y, x: f where f after the semicolon is the f from the calc(f,a,b)

what I cant figure out is that how can I pass f from the calc function input to the odeint inside. It says that f isnt declared and if I put it within strings.. it doest work

When I run this function.. it doesnt work I get this error

NameError: name 'f' is not defined

I am not sure how to pass my equation to be integrated inside odeint

Thanks

解决方案

If one were to rewrite the function calc as follows:

def calc(f, a, b):
    limits = [a, b]
    integral = odeint(lambda y, x: f(x), 0, limits)

    return integral[1][0]

Then one may use this function thus:

>>> calc(lambda x: x ** 2, 0, 1)    # Integrate x ** 2 over the interval [0, 1] (expected answer: 0.333...)
0.33333335809177234
>>> calc(lambda x: x, 0, 1)         # Integrate x over the interval [0, 1] (expected answer: 0.5)
0.50000001490120016
>>> calc(lambda x: 1, 0, 1)         # Integrate 1 over the interval [0, 1] (expected answer: 1.0)
1.0

The odeint function from the scipy.integrate module has the signature:

odeint(func, y0, t, ...)

where: func is a callable that accepts parameters y, t0, ... and returns dy/dt at the given point; y0 is a sequence representing initial condition of y; t is a sequence that represents intervals to solve for y (t0 is the first item in the sequence).

It appears that you are solving a first-order differential equation of the form dy/dx = f(x) over the interval [a, b] where y0 = 0. In such a case, when you pass f (which accepts one argument) to the function odeint, you must wrap it in a lambda so that the passed-in function accepts two arguments (y and x--the y parameter is essentially ignored since you need not use it for a first-order differential equation).

这篇关于Python 在函数中使用 Lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆