Unexpanded coefficients of a polynomial

51 views
Skip to first unread message

Paul Royik

unread,
Apr 27, 2023, 1:02:33 PM4/27/23
to sympy
Hello.

How can I get all unexpanded coeffs of a polynomial?

For example, Poly(360*x*y**71*(y**72 - 1)**4 + 360*x**4*(x**5 - 1)**71, x).all_coeffs() should return [360*y**71*(y**72 - 1)**4, 360*x**4*(x**5 - 1)**71], but, instead, it returns expanded long expression.

How to avoid this and return untouched coeffs?

Thank you. 

Aaron Meurer

unread,
Apr 27, 2023, 2:05:09 PM4/27/23
to sy...@googlegroups.com
Poly is designed to expand all polynomials out. If you just want the
terms of an expression as you've written it you can use
Add.make_args(). I wouldn't really call these the "coefficients" of
the expression though.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/eda7343d-f557-48b2-85e0-d52162a12bb5n%40googlegroups.com.

Chris Smith

unread,
Apr 27, 2023, 10:05:54 PM4/27/23
to sympy
The things you call "coefficients" are called "terms" of the sum. If you know you have a sum then `eq.args` will give you the terms. If the equation might have a single term then `Add.make_args(eq)` will give you 1 or more terms.

/c

emanuel.c...@gmail.com

unread,
Apr 28, 2023, 2:31:21 AM4/28/23
to sympy

BTW :

>>> x, y = symbols("x, y") >>> P = 360*x*y**71*(y**72 - 1)**4 + 360*x**4*(x**5 - 1)**7 >>> P.args (360*x**4*(x**5 - 1)**7, 360*x*y**71*(y**72 - 1)**4) >>> P.func.make_args(P) (360*x**4*(x**5 - 1)**7, 360*x*y**71*(y**72 - 1)**4) >>> P.func.make_args(P)==P.args True

HTH,

Paul Royik

unread,
May 23, 2023, 1:49:05 AM5/23/23
to sympy
Thanks to all!
Reply all
Reply to author
Forward
0 new messages