Search code examples
pythonnumpysympysymbolic-mathpolynomial-math

Transforming polynomial variable in Numpy


I am trying to learn how to use Numpy. Consider I have the roots of a polynomial. I use coeff = np.polynomial.polynomial.polyfromroots(roots) to get the coefficients of the polynomial as an array. Then I use print np.poly1d(coeff) To print out the polynomial. Let that polynomial be

x^2 +3x + 2

Now how do I transform the variable such that

x is now 2/x

That is the equation becomes

(2/x)^2 + 6/x + 2

In scilab I can do this using the horner function. Is it possible in numpy?


Solution

  • In SymPy this would be simply:

    from sympy.abc import x
    
    f = x**2 + 3*x + 2
    g = f.subs({x:2/x})
    

    Resulting in:

    print(g)
    #2 + 6/x + 4/x**2