Reputation: 305
How can you make Python take a polynomial as an input while maintaining the ability to substitute x for a real value?
Here is what I tried:
fx=input("Enter a Polynomial: ")
x=float(input("At which position should the polynomial be evaluated: "))
while True:
print(eval("fx"))
continue
The problem that arises is that Python will just evaluate fx as x and not as the value that I gave to x via my second input.
Upvotes: 4
Views: 12017
Reputation: 519
This code will be used to take input of the polynomial as y for instance.
y = x**2 + x*4 and it takes input of x as float value like if i give 2 for x then this expression will prints 12. in this code i use eval means evaluate i.e., the built in function of the python 3 in this eval polynomial function is passed as string and then the value of independent variable i.e., x for polynomial y is passed as string inside the eval as the second argument.
y = input("Enter a Polynomial: ") x = float(input("At wich position should the polynomial be evaluated: "))
dict = {}
d['x'] = x
print(eval(y, d))
Upvotes: 0
Reputation: 1448
Python 3
You can write:
fx = input("Enter a Polynomial: ")
x = float(input("At wich position should the polynomial be evaluated: "))
print(eval(fx.replace('x', str(x))))
Note: Above code will only work if you have entered polynomial in the form of *
. For example: x**4 - x + 1
Upvotes: 4
Reputation: 2484
This should help:
def eval_polynomial(poly, val):
xs = [ x.strip().replace('^','**') for x in poly.split('+') ]
return sum( [eval(n.replace('x', str(val))) for n in xs] )
Please keep in mind that you earlier have to make sure val is a number for safety reasons.
EDIT: more self-descriptive version as asked by Dipen Bakraniya
def fix_power_sign(equation):
return equation.replace("^", "**")
def eval_polynomial(equation, x_value):
fixed_equation = fix_power_sign(equation.strip())
parts = fixed_equation.split("+")
x_str_value = str(x_value)
parts_with_values = (part.replace("x", x_str_value) for part in parts )
partial_values = (eval(part) for part in parts_with_values)
return sum(partial_values)
Upvotes: 5
Reputation: 22544
Since the particular variable, such as x
, does not matter, just have the user enter a sequence of numbers, which will be the coefficients of the polynomial. Have the user enter the constant coefficient, then the linear coefficient, then the quadratic coefficient, and so on. If these are done as separate inputs you would need a stop signal, one that is not a number. If you want it all as one input, the user could put spaces or commas or something similar between the coefficients, and no stop signal would be needed.
You then place the coefficients into a list: if the input was one line, just use the split()
method. Any operations on polynomials, such as substitution into the variable, can be done with this list. Substitution, for example, can be done easily and efficiently with Horner's method. There are simple algorithms for addition, subtraction, multiplication, division, differentiation, and so on. Let me know if you need more details on how to implement any of those algorithms in Python.
Python's eval()
function is very dangerous, so I highly recommend that you do not use that, despite its simplicity.
Upvotes: 3