多项式函数列表 [英] list of polynomial functions

查看:87
本文介绍了多项式函数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的程序中我试图学习如何使用python的功能性编程方面,但下面的程序会崩溃,

声称递归深度是太棒了我试图制作

a多项式函数列表,使得poly [0](3)= 1,poly [1](3)=

3,poly [2 ](3)= 9等。有人能指出我正确的方向吗?

谢谢。


def make_polys(n):

"""列出多项式函数列表n。

""

p = lambda x:1

polys = [p]


for i in range(n):

polys.append(lambda x:polys [i] (x)* x)


返回多边形


#构造多项式向量

polys = make_polys( 5)


#print

for p in polys:

print p(3)

In the following program I am trying to learn how to use functional
programming aspects of python, but the following program will crash,
claiming that the recursion depth is too great. I am attempting to make
a list of polynomial functions such that poly[0](3) = 1, poly[1](3) =
3, poly[2](3) = 9, etc. Could someone point me in the right direction?
Thanks.

def make_polys(n):
"""Make a list of polynomial functions up to order n.
"""
p = lambda x: 1
polys = [p]

for i in range(n):
polys.append(lambda x: polys[i](x)*x)

return polys

# construct a vector of polynomials
polys = make_polys(5)

# print
for p in polys:
print p(3)

推荐答案

在< 11 ********************** @ i40g2000cwc.googlegroups .com> ,Josiah Manson

写道:
In <11**********************@i40g2000cwc.googlegroups .com>, Josiah Manson
wrote:
def make_polys(n):
"""列出多项式函数列表n。
""&
p = lambda x:1
多边形= [p]

我在范围内(n):
polys.append(lambda x:polys [ i](x)* x)
def make_polys(n):
"""Make a list of polynomial functions up to order n.
"""
p = lambda x: 1
polys = [p]

for i in range(n):
polys.append(lambda x: polys[i](x)*x)




问题是`i`。在执行lambda *定义*

但是调用lambda函数时,不会对其进行求值。然后`我`是

总是==`n`。您必须在

lambda定义中将其显式绑定为默认值:


polys.append(lambda x,i = i:polys [i]( x)* x)


然后就行了。


Ciao,

Marc''BlackJack''Rintsch



The `i` is the problem. It''s not evaluated when the lambda *definition*
is executed but when the lambda function is called. And then `i` is
always == `n`. You have to explicitly bind it as default value in the
lambda definition:

polys.append(lambda x, i=i: polys[i](x)*x)

Then it works.

Ciao,
Marc ''BlackJack'' Rintsch


> `i`是问题所在。当lambda
> The `i` is the problem. It''s not evaluated when the lambda
* definition *被执行但是当lambda函数被调用时,它不会被评估。然后`i`总是==`n`。你必须在lambda定义中将它显式绑定为默认值:

polys.append(lambda x,i = i:polys [i](x)* x)

然后就可以了。
*definition* is executed but when the lambda function is
called. And then `i` is always == `n`. You have to
explicitly bind it as default value in the lambda definition:

polys.append(lambda x, i=i: polys[i](x)*x)

Then it works.




为了满足我的好奇心,为什么lambda会找到多边形,但是

找不到我?如果您正在描述的是这种情况,那么在我看来,以下代码也应该起作用:


def make_polys(n):

p = lambda x:1

polys = [p]

for i in range(n):

p = polys [i]

polys.append(lambda x:(p(x)* x))

返回多边形


但它遇到了与原版相同的问题。在make_polys的

范围之外,poly []和i都不存在。


这里有一些微妙的行为,我不知道。


-tkc



Just to sate my curiosity, why can the lambda find "polys", but
not find "i"? If what you''re describing is the case, then it
seems to me that the following code should work too:

def make_polys(n):
p = lambda x: 1
polys = [p]
for i in range(n):
p = polys[i]
polys.append(lambda x: (p(x) * x))
return polys

yet it suffers the same problem as the original. Outside the
scope of make_polys, neither polys[] nor i exists.

There''s some subtle behavior here that I''m missing.

-tkc



Tim Chase写道:
Tim Chase wrote:
`i`是问题所在。在执行lambda
* definition *但是调用lambda函数时,不会对它进行求值。然后`i`总是==`n`。你必须在lambda定义中将它显式绑定为默认值:

polys.append(lambda x,i = i:polys [i](x)* x)

然后就可以了。
为了满足我的好奇心,为什么lambda会找到多边形,但是
找不到我?如果您正在描述的是这种情况,那么在我看来,以下代码也应该起作用:
The `i` is the problem. It''s not evaluated when the lambda
*definition* is executed but when the lambda function is
called. And then `i` is always == `n`. You have to
explicitly bind it as default value in the lambda definition:

polys.append(lambda x, i=i: polys[i](x)*x)

Then it works.
Just to sate my curiosity, why can the lambda find "polys", but
not find "i"? If what you''re describing is the case, then it
seems to me that the following code should work too:




它不是那样的它找不到它,如果你使用一个闭包,我将

对所有lambda都有相同的值。

这里有一些微妙的行为我错过了。



it''s not that it cannot find it, it''s that if you use a closure, i will
have the same value for all lambdas.
There''s some subtle behavior here that I''m missing.




lexical closures绑定到名称,默认参数绑定到值。


< / F>



lexical closures bind to names, default arguments bind to values.

</F>


这篇关于多项式函数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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