2

I am pretty new to programming but I want to define a function like the following with user input:

function = raw_input('What function shall be used? ') #user puts in "(1+1/x)^x"
def f(x): #using this to define
 y = (1+1/x)**x 
 return y

I hope it is clear what I try to do. Could you use input and define a priori that sin is used as math.sin? But how do I define the function afterwards?

Edited to add specification from the comments

Let's say the valid input is python code such as "(1+1/x)**x" like in the question. Could you now use eval to define a function f(x) which can be later used?

juanpa.arrivillaga
97.7k14 gold badges141 silver badges190 bronze badges
asked Jun 22, 2017 at 17:39
27
  • Give then way the question is worded, it sounds like you want to define your own mini-language, or computer algebra system. Commented Jun 22, 2017 at 17:43
  • You can use eval, but then you have to trust the user input will not blow up the server. Commented Jun 22, 2017 at 17:43
  • 1
    sympy.org may help... Commented Jun 22, 2017 at 17:43
  • @PauloScardine no, you could not use eval because eval evaluates a python expression, but (1+1/x)^x does not mean (1+1/x)**x in python. Commented Jun 22, 2017 at 17:43
  • You can use eval() but don't... It's very unsafe and your users would need to know at least a bit of Python (e.g. something to the power of something else is defined as ** not ^) - and if they do, why not let them define their own modules that your app will load instead of requiring them to manually type math formulas? Commented Jun 22, 2017 at 17:44

1 Answer 1

2

As I commented, this can be done with something like eval, but is unsafe because you should never trust random user input.

>>> def expr_to_fn(expr):
... expr = expr.replace('^', '**')
... symbols = set(re.findall(r'([A-Z,a-z]+)', expr))
... fn = eval("lambda {}: {}".format(",".join(symbols), expr))
... return fn
>>> f = expr_to_fn("(1+1/x)^x")
>>> f(2)
2.25

The safe way is to implenet a DSL and a parser, or use a CAS (computer algebra system) like sympy, as suggested by Hiro.

answered Jun 22, 2017 at 18:09
Sign up to request clarification or add additional context in comments.

4 Comments

I think this is almost there, but it still doesn't deal with the issue of sin, cos and the like. However, this does make me understand what you meant by using a context dictionary, which could probably take this the rest of the way there.
Totally overlooked Hiro's comment but sympy might be what I was looking for.
I will try what you did later tonight and see how far I can get with it, thank you!
@juanpa.arrivillaga I accept your apology but I will have to left that as an exercise for the OP because I lack the time to post a complete work solution right now.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.