Search code examples
pythonlistsympypolynomials

List of coefficients to polynomial


How do I create a polynomial out of a list of coefficients in SymPy?

For example, given a list [1, -2, 1] I would like to get Poly(x**2 - 2*x + 1). I tried looking at the docs but could not find anything close to it.


Solution

  • You could use Poly.from_list to construct the polynomial:

    >>> x = sympy.Symbol('x')
    >>> sympy.Poly.from_list([1, -2, 1], gens=x)
    Poly(x**2 - 2*x + 1, x, domain='ZZ')