简体   繁体   English

Python:我正在尝试使用另一个函数返回的函数

[英]Python: I am trying to use a function returned by another function

I am learning Python for scientific computing, and there is an exercise where I create a polynomial using its roots: 我正在学习用于科学计算的Python,在一个练习中,我使用其根创建多项式:

在此处输入图片说明

Exercise 3.5 练习3.5

from sympy import symbols, expand

def poly(roots):            #Pass real and/or complex roots

    x = symbols('x')
    f = 1            
    for r in roots:
        f *=(x - r)

    return expand(f)

Test: 测试:

from numpy.lib.scimath import sqrt
poly([-1/2, 5,(21/5),(-7/2) + (1/2)*sqrt(73), (-7/2) - (1/2)*sqrt(73)])

Gives: 给出:

x**5 - 1.7*x**4 - 50.5*x**3 + 177.5*x**2 - 24.8999999999999*x - 63.0

I am trying to return the polynomial and use it by passing ax value: 我试图返回多项式并通过传递ax值来使用它:

f = lambda x: poly([-1/2, 5,(21/5),(-7/2) + (1/2)*sqrt(73), (-7/2) - (1/2)*sqrt(73)])
f(-1/2)

Gives: 给出:

x**5 - 1.7*x**4 - 50.5*x**3 + 177.5*x**2 - 24.8999999999999*x - 63.0

The problem is that f(-1/2) is not giving 0 , which it should. 问题是f(-1/2)没有给出0 ,它应该给出。 How can I tell Python to use the expression algebraically? 如何告诉Python以代数方式使用该表达式? Thank you! 谢谢!

You have to substitute for x and evaluate the polynomial to a float: 您必须替换x并将多项式评估为浮点数:

poly(...).subs('x', y).evalf()

Try with it: 试试看:

from sympy import symbols, expand, sqrt


def poly(roots):  # Pass real and/or complex roots
    x = symbols('x')
    f = 1
    for r in roots:
        f *= (x - r)

    return expand(f)


f = lambda y: poly([-1 / 2, 5, (21 / 5), (-7 / 2) + (1 / 2) * sqrt(73), (-7 / 2) - (1 / 2) * sqrt(73)]).subs('x',
                                                                                                             y).evalf()
print(f(-1 / 2))

Output: 输出:

-1.06581410364015e-14

You can also use python's builtin eval() and str() function. 您还可以使用python的内置eval()和str()函数。 eval() takes a string as an input. eval()将字符串作为输入。

from sympy import symbols, expand,sqrt

def poly(roots):            #Pass real and/or complex roots

x = symbols('x')
f = 1            
for r in roots:
    f *=(x - r)

return str(expand(f))

f = lambda x: eval(poly([-1 / 2, 5, (21 / 5), (-7 / 2) + (1 / 2) * sqrt(73),(-7 / 2) - (1 / 2) * sqrt(73)]))
print(f(-1/2))

Output: 0.0 输出: 0.0

暂无
暂无

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

相关问题 如何在python的另一个应用程序中使用函数返回的值? - How can I use a value returned by a function in another application in python? 我正在尝试使用上层功能进行输入 - I am trying to use the upper function for the input Python:使用从一个函数返回的值到另一个函数 - Python: Use returned values from a function to another function 如何在另一个 function 中使用前一个 function 返回的变量? (Python) - How to use a returned variable from a previous function in another function? (python) 在 Python Asyncio 上,我试图将值从一个函数返回到另一个函数 - On Python Asyncio I am trying to return a value from one function to another python 将另一个 function 的返回值设置为在 header 中使用的变量 - python set returned value from another function as a variable to use in header 如何访问在另一个 function (Python) 中返回的 dataframe - How can I access a dataframe that is returned in another function (Python) 我正在尝试从另一个模块导入另一个函数,但我在 M1 Mac 上得到了这个 - I am trying to import another function from another module but I am getting this on an M1 Mac 如何将一个函数作为参数传递给另一个函数,并在python中使用该函数返回的值? - How to pass a function as argument to another function and use the value returned by that function in python? 我正在尝试在python 3.4版本中运行此功能 - I am trying to run this function in python 3.4 version
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM