Search code examples
pythonsympypolynomialscoefficients

Is there a sympy function to extract coefficients of negative power monomials?


I have a polynomial: eps * x3 - x2 + 2 + 3 * x * eps-2. How can I get a list of all coefficients including negative?

I have tried coeffs() and all_coeffs() methods but they do not work with negative powers of epsilon:

import sympy as sp
x, eps = sp.symbols('x E')
expr = eps * x**3 - x**2 + 2 + 3 * x * eps**(-2)
coeffs_list = sp.Poly(expr, eps).coeffs()

I want to get list of coefficients like [x^3, x^2 + 2, 3*x]


Solution

  • Multiplying with a large power of eps helps to get the coeffients.

    import sympy as sp
    x, eps = sp.symbols('x E')
    expr = eps * x**3 - x**2 + 2 + 3 * x * eps**(-2)
    coeffs_list = sp.Poly(expr*eps**2, eps).coeffs()
    

    gives

    [x**3, 2 - x**2, 3*x]