4

Is it possible to enter equations in to python program. As an example if I have set equations as below. Is it possible to enter those equations as a user input using input command.

  ex:  1/x - 5/a + 2x^2           (a is a constant value)
       1/x + 10x/a^2 - x^2/a^3 + 3x^2
     (all equations have one variable 'x')

I now how to define equation using following command. But I want to input that equation as user input.

def my_function(x):
        return(1/x + r^2/a -5x^2)

my_function(value for x)

Because I want to solve each equation for longer range of x value. I can write separate program for each equation. But I prefer to give the equation as user input.

Can any one give me a direction to go?

user3817989
  • 715
  • 1
  • 8
  • 11
  • 1
    if you specify that the equation is to be inputted in proper python syntax, and the equations are only algebraic, something like this perhaps: `def my_fun(x, fun): \ return eval(fun)`. Usage: `my_fun(2.0, '1/x - 5*x**2')`. But you should also note the dangers of eval... – Ben Jul 08 '14 at 20:43
  • you can use ast.literal_eval to evaluate the input. – Padraic Cunningham Jul 08 '14 at 20:53

3 Answers3

3

You can do the following:

  1. grab the user input equation as a string
  2. replace the variable a with its value using string.replace
  3. parse the string as a mathematical expression. see Evaluating a mathematical expression in a string for different ways of doing this
Community
  • 1
  • 1
Jeff Tsui
  • 1,266
  • 12
  • 20
1

You can use the built-in eval() function.

But;

  • The equations have to be in Python syntax.
  • The constants and variables have to de defined before evaluating.

Note that using eval() exposes a lot of the power of Python. But there are ways to restrict it.

An example in iPython:

In [1]: x = 2.4

In [2]: a = 5.27

In [3]: eval('1/x - 5/a + 2x^2')
  File "<string>", line 1
    1/x - 5/a + 2x**2
                 ^
SyntaxError: invalid syntax

This is because python requires explicit multiplication, and the power operator is **, not ^;

In [4]: eval('1/x - 5/a + 2*x**2')
Out[4]: 10.987900063251107

In [5]: eval('1/x + 10*x/a**2 - x**2/a**3 + 3*x**2')
Out[5]: 18.521464252545897

In [6]: eval('1/b')
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-6-555680c1bd65> in <module>()
----> 1 eval('1/b')

<string> in <module>()

NameError: name 'b' is not defined

You cannot evaluate an expression with a name that hasn't been defined yet.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94
  • Eval can be a dangerous function to use, especially since the equations are input by users. The link I posted in my answer below has several alternatives to eval() that can be directly applied to mathematical expressions: http://stackoverflow.com/questions/2371436/evaluating-a-mathematical-expression-in-a-string – Jeff Tsui Jul 08 '14 at 22:03
  • @JeffTsui If you use a proper `globals` and `locals` argument to `eval()`, you can defang it pretty well. See the "restrict" link in my answer. Having said that, the solution using `ast` is pretty elegant. – Roland Smith Jul 08 '14 at 22:10
  • @RolandSmith The "Use eval safely" article you linked to does not protect you: [Eval really is dangerous](http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html) You cannot easily "defang" eval. – Ned Batchelder Jul 12 '14 at 14:49
0

Take a look at this Equation parsing in Python it might be what you are looking for IF you can write(convert) the equation into python arithmetic.

Sergey
  • 978
  • 2
  • 11
  • 33