-1

It is a program that calculates a continuous uniform distribution. So for example I enter x^2, with a = 0, b = 1. The program would evaluate x^2 from 0 to 1. The coefficient 1/(b-a) is part of the rules of a continuous uniform distribution.

formula = input(
    "What is the formula? Use x for variables.")
a = input("What is the value of a?")
b = input("What is the value of b?")

formula = formula.replace("^", "**")

coefficient = 1 / (float(b) - float(a))
coefficient = float(coefficient)

ans,err = quad(formula, a, b)

print(ans * coefficient)

It is telling me that it can't convert the formula from a string to a float. The problem is that the formula string has the variable 'x' in it. So even if I do float(formula) it gives me an error that it can't convert from string to float. Is there any way around this?

hse23
  • 15
  • 5
  • I am not really sure what you are asking, but could it be that you are looking for the 'eval' function? It makes it possible to evaluate strings, e.g. eval('2*3') returns 6. If you need variables you could use format: eval('{a}*{b}'.format(a=2, b=3)) also returns 6 – fetteelke Aug 21 '17 at 14:28
  • You may find https://stackoverflow.com/questions/2371436/evaluating-a-mathematical-expression-in-a-string useful, to fit to your specific use case. This is complicated stuff. I wouldn't recommend using `eval`, as discussed in that post, unless your user is explicitly trustworthy, as `eval` can and will always break your system. The same goes for `exec`. – Izaak van Dongen Aug 21 '17 at 14:29
  • `formula` has to be a function (e.g. `def formula(x):...`) or `lambda` expression. While not impossible, converting a string to a function at runtime is not trivial, and isn't something that a new Python programmer should focus on. It would make more sense to have your user write a simple Python script file with the function. You can ask for the file name, import it, and then evaluate it. Get that approach working first, then revisit the string input idea. – hpaulj Aug 21 '17 at 16:06
  • Convert `a` and `b` to float before giving them `quad` – hpaulj Aug 21 '17 at 16:19
  • You could explore `sympy`, a package that lets you create symbolic expressions. Those can be `lambdified` to make a `numpy` compatible function. But there's a good size learning curve. Another thing to consider is creating a polynomial function. `numpy` has `poly` functions that create such a function from coefficients. – hpaulj Aug 21 '17 at 17:41

1 Answers1

1

If I understand correctly, the user is able to input a formula using the variables a and b. So an example would be: formula = 'a*b', a = 3, b = 5, which would yield 15.

Perhaps you could use the python exec(string) method to execute the code given in formula.

Thijs van Ede
  • 78
  • 1
  • 8
  • Where would you apply `eval`. Suppose the string is `x**2`? `eval('x**2')` should give an Name error, `x` undefined. What `quad` expects is a function created with `def formula(x): return x**2`. – hpaulj Aug 21 '17 at 16:36