简体   繁体   English

创建一个将表达式作为python输入的函数

[英]Create a function that accepts an expression as an input in python

I am busy just doing some code on the bisection method. 我正忙于对等分方法做一些代码。 It is easy of course to just write the script and run it. 当然,编写脚本并运行它很容易。 However, the trick comes in when I want to define a function that will take an expression such as x**2 as an input. 但是,当我要定义一个将诸如x ** 2之类的表达式作为输入的函数时,就会出现这种技巧。 I've scrapped something together though. 我把东西凑在一起了。 It sort of gets the job done but then at my first if statement it complains about not being able to multiply a function by a function. 它可以完成工作,但是在我的第一个if语句中,它抱怨无法将一个函数与一个函数相乘。 How would I solve this problem as well? 我也将如何解决这个问题? I really tried doing my homework on this problem before asking and unfortunately cannot find the solution. 我真的尝试过在这个问题上做功课,然后才提出要求,很遗憾找不到解决方案。 I would just love to know how to do this though. 我只是想知道如何做到这一点。

from sympy.abc import x

def bisect(f, a, b):
    f = lambda x: f
    f_a, f_b = f(a), f(b)
    tol = 1e-4 
    count = 0
    print '\t'.join( ['Step' , 'a', 'b', 'c', 'f(c) ' , '(b-a)/2)'])
    while (b-a)/float(2) > tol:
        c = (a+b)/2
        f_c = f(c)
        print '\t'.join( [str(count) , str(a) , str(b) , str(c), str((b-a)/float(2) )])
        if f_a*f_c < 0:
            b = c
            f_b = f_c
        else:
            a = c
            f_a = f_c
        count = count + 1

Is this what you need to know? 这是您需要知道的吗?

>>> def demo(func):
...   for i in range(5):
...     print func( float(i))
... 
>>> demo( lambda x: x**2 + 2*x - 5 )
-5.0
-2.0
3.0
10.0
19.0

You can also pass in a function created with def as well as a one-liner defined with lambda . 您还可以传入由def创建的函数以及由lambda定义的单行代码。 the above is the same as 以上与

def foo(x):
    return x**2 + 2*x - 5 

demo( foo) 

The other thing you might want to know is that 您可能想知道的另一件事是

definition = "x**2 + 2*x - 5" # or read it from the user as text input
demo( eval( "lambda x: " + definition))

works ... 作品...

obligatory warning , "eval is evil", accepting Python code from a user and executing it is generally regarded as a security no-no-NO! 强制性警告 :“评估是邪恶的”,接受用户的Python代码并执行它通常被认为是安全性! However, if this code is run by the same user who is experimenting with plotting mathematical forms, no great harm can come of it. 但是,如果此代码是由正在尝试绘制数学形式的同一用户运行的,则不会造成太大危害。 He can't do anything worse than what he could do with python at the command line. 他不能做任何比在命令行上使用python更糟糕的事情。 If you were to do anything like this in a web-server or suchlike, I would fear for your employment and future prospects thereof! 如果您要在网络服务器或类似服务器上进行任何此类操作,我会担心您的就业和未来前景!

Let's assume you have created an expression for function f using sympy : 假设您已经使用sympy为函数f创建了一个表达式:

f = x**2-0.1

Your bisect function will work if you change the lambda definition: 如果更改lambda定义,则bisect函数将起作用:

def bisect(fexpr, a, b):
    f = lambda xval: fexpr.subs(x,xval)
    f_a, f_b = f(a), f(b)
    ...

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

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