Python polynomial with deegre and coefficients from user
I write a program in Python and I need to create a polynomial with deegre(n) and coefficients (a,b,c) from user. I create it but I don't know how use it like function with argument for example polynomial(x)=some value. How i can solve this?
You can specify a polynomial using the numpy package: https://docs.scipy.org/doc/numpy/reference/routines.polynomials.html.
As an alternative you can use sympys poly function: http://docs.sympy.org/latest/modules/polys/reference.html to get a polynomial in symbolic form. To evaluate for a given x see http://docs.sympy.org/latest/modules/evalf.html
Numpy roots will find all the roots of a polynomial, given the coefficients: https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.roots.html
(It might be useful to know (if you don't happen to have them from input) that you can obtain the coefficients from the sympy poly using all_coeffs function: http://docs.sympy.org/0.7.1/modules/polys/reference.html#sympy.polys.polytools.Poly.all_coeffs)
If you want to implement from first principals then I suggest looking at the reference in https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.roots.html.
NB: A degree zero polynomial (although I'm not sure if you meant that) is a constant and has no root unless equal to zero.
Some hints when writing your program:
- Prompt and obtain user input for poly order and then for each coefficient. Store the coefficients in a list. Also prompt and obtain user input for the x value to evaluate the polynomial for.
- If using sympy construct your polynomial object using the list.
- If using sympy evaluate your polynomial for the x value using evalf. If numpy then call a function that takes the list and the x value and evaluates the polynomial using the numpy library.
- Then call numpy.roots with your list of coefficients.