1

I am using quad method of Scipy library and I want to evaluate numerical integration of a function that entered by a user. The function is stored as a string. However, quad method does not except string input. So, how can I convert this string expression into a numerical value?

Assume that, the string expression is:

s1 = "5*x**2 - 4*x + 2" and lower_bound = 0 , upper_bound = 5

quad(s1, lower_bound, upper_bound)

I've tried splitting this string into tokens with respect to arithmetic symbols then invoke the quad method but the independent variable(x in this case) still remains as string.

How can I solve this problem?

3 Answers3

2

You can use eval() like so:

>>> eval( "lambda x:x+1" )
<function <lambda> at 0x7f1b80d8ef28>

If you add a "lambda x:" string on the beginning of your expression, then use eval() on the result, eval will return a function which does what you want. You can then pass this function to your Scipy integrating function.

Note: eval will execute any code which is passed to it, therefore do not use it in a context where it would create a security problem. If this is an online application, then eval() is the wrong choice. If this is a desktop application, then the user can already do anything they want with the computer, and in this context eval() is fine.

bobflux
  • 11,123
  • 3
  • 27
  • 27
  • You're welcome ;) Note the difference between this answer and the others is that eval() is used only once (because it's slow) and the function it returns will have the same speed as any other python function you'd have entered in the code... – bobflux Nov 28 '17 at 19:37
1

I'm not sure if there's too much you can do here except for:

  1. Write your own parser that takes string as an input and returns function of that form. Although eval with lambda could do the trick, it's rather not safe and will be slower. If you are expecting only polynomials you might want to take a look at NumPy Polynomial module (link).
  2. Look into symbolic evaluation libraries like SymPy

Also, take a look at what's been already asked: Equation parsing in Python (SO).

Dawid Laszuk
  • 1,773
  • 21
  • 39
  • Do you know any open source, free parser that can solve this problem? –  Nov 26 '17 at 22:04
  • @OnurErtugral Not really. Simple cases are rather fun past time or university project, so I image there should be plenty scattered codes on the web, but none that I know of solid foundation. – Dawid Laszuk Nov 26 '17 at 22:12
0

scipy.integrate.quad() expects a function as its first argument (docs).

Here you can either use function or lambda expression:

lower_bound = 0
upper_bound = 5

def s1(x):
    return 5*x**2 - 4*x + 2

quad(s1, lower_bound, upper_bound)
quad(lambda x: 5*x**2 - 4*x + 2, lower_bound, upper_bound)

If you wanted to parse from the symbolic expression in a string, as a quick hack you can use eval():

s1 = "5*x**2 - 4*x + 2"
quad(lambda x: eval(s1), lower_bound, upper_bound)

If you are doing a lot of symbolic maths, I would suggest you to use SymPy.

Tuwuh S
  • 291
  • 1
  • 7