Dumping members of a lower order polynomial using Sympy

Say we have this feature,

f = poly (2 * x ** 2 + 3 * x - 1, x)

How could terms of degree n or less be dropped.

For example, if n = 1, the result will be 2 * x ** 2.

+4
source share
1 answer
from sympy import poly
from sympy.abc import x

p = poly(x ** 5 + 2 * x ** 4 - x ** 3 - 2 * x ** 2 + x)
print(p)
n = 2
new_p = poly(sum(c * x ** i[0] for i, c in p.terms() if i[0] > n))
print(new_p)

Conclusion:

Poly(x**5 + 2*x**4 - x**3 - 2*x**2 + x, x, domain='ZZ')
Poly(x**5 + 2*x**4 - x**3, x, domain='ZZ')
+4
source

All Articles