2

I would like to use the mathematical_operation as entry argument for function, so the user can enter any mathematical_operation to receive result. Was thinking that something like that might work with default arguments :

mathematical_operation = input()
def fun(a=1, b=1, c=1, mathematical_operation=a*(x**2)+b*x+c):
        x = np.linspace(-1,1,10)
        y = (lambda a,b,c: mathematical_operation)(a,b,c)
        print(x)
        print(y)
    return y
    
    fun(mathematical_operation = a*(x**2)+b*x+c)

However it gives error:

Traceback (most recent call last):
  File "C:\Users\Documents\Python\test.py", line 38, in <module>
    def fun(a = 1, b = 1, c = 1, equation = a*(x**2)+b*x+c):
NameError: name 'a' is not defined
Gerard
  • 117
  • 7
  • You could pass the equation as a function of (a,b,c) directly. – kubatucka Oct 19 '21 at 15:28
  • *what do you mean by "equation"*. There is not data type called "equation" in Python. Are you using a third-party library? – juanpa.arrivillaga Oct 19 '21 at 15:28
  • The default value of a parameter is fixed at the time the function is defined; you can't reference call-time argument values. – chepner Oct 19 '21 at 15:29
  • It seems like you want the equivalent of *call by name* as an evaluation strategy, which python doesn't support. I *suspect* you just want to pass a *function object* as the value of the `equation` paramter – juanpa.arrivillaga Oct 19 '21 at 15:30
  • @Gerard I'm sorry, but that doesn't make any sense. An equation is a mathematical term, that asserts two mathematical expressions are equal. It is very important to understand, although we use the words like *function* and *expression* in programming, they aren't the same thing as the mathematical terms. – juanpa.arrivillaga Oct 19 '21 at 15:32
  • In any case, why not `equation=lambda a,b,c: a*(x**2)+b*x+c`? – juanpa.arrivillaga Oct 19 '21 at 15:33
  • Sorry my bad, by equation I meant mathematical operation inputted by user as argument for calculation parameter y. I would like the user could input mathematical operation for x and receive y – Gerard Oct 19 '21 at 15:36
  • You should take the user input and parse it out to evaluate that expression. You're walking down a dangerous path and you're blindly accepting it because you don't know any better. – Jeff Mercado Oct 19 '21 at 20:52

2 Answers2

1

If you need more complicate evaluation of mathematical expressions/problems you should give a look at sympy, a 3-d party library for symbolic manipulations and more! It is more clean and safe than a string evaluation.

from sympy import symbols, lambdify
import numpy as np

def fun(a=1, b=1, c=1):
    a0, b0, c0 = symbols('a0 b0 c0')  # constant parameters
    x = symbols('x')  # variable
    expr = a0 * (x**2) + b0*x + c0  # expression
    expr = expr.subs(dict(a0=a, b0=b, c0=c))    # evaluation of the parameters
    func = lambdify(x, expr, 'numpy')   # function of x-only

    domain = np.linspace(-1,1, 10)
    y = func(domain)
    print(domain)
    print(y)
    return y

fun()

Output

[-1.         -0.77777778 -0.55555556 -0.33333333 -0.11111111  0.11111111
  0.33333333  0.55555556  0.77777778  1.        ]
[1.         0.82716049 0.75308642 0.77777778 0.90123457 1.12345679
 1.44444444 1.86419753 2.38271605 3.        ]

Remark: the code seems a bit longer (and uglier!) just because I tried to respect the structure of the original code.

cards
  • 3,936
  • 1
  • 7
  • 25
0

Python thinks you are trying to pass a defined variable into the function. You could try eval:

import numpy as np


def fun(a=1, b=1, c=1, equation=None):
    x = np.linspace(-1,1,10)
    y = (lambda a,b,c,x: eval(equation))(a,b,c,x)
    print(x)
    print(y)
    return y

fun(equation = "a*(x**2)+b*x+c")
Tom
  • 440
  • 3
  • 10
  • What would happen if they ran the equation: `import os;os.system('[some actually dangerous command]')`? `eval()` should _never_ be the answer to anything that accepts user input, **ever**. – Jeff Mercado Oct 19 '21 at 20:50
  • 1
    Use `ast.literal_eval()` or manually parse it ([why](https://stackoverflow.com/questions/15197673/using-pythons-eval-vs-ast-literal-eval)). – Jeff Mercado Oct 19 '21 at 20:54