Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
468 views
in Technique[技术] by (71.8m points)

python - Best way to isolate one coefficient of a multivariate polynomial in sympy

I have a multivariate polynomial (which in the general case many many variables) whose coefficients list some data that I need to read off, but it doesn't seem like sympy gives a good way to do this.

The collect function seemed like the right idea, but when you use it with several variables, it doesn't actually give you the individual monomials, but rather strange groupings of monomials that depend on the order you listed the variables.

Does anyone know of a way to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The documentation of polynomial module lists plenty of ways to handle coefficients. For example:

>>> import sympy
>>> x,y,z = sympy.symbols('x,y,z')
>>> p = sympy.poly((x+2*y-z)**3)
>>> p.coeffs()
[1, 6, -3, 12, -12, 3, 8, -12, 6, -1]

These are nonzero coefficients in lexicographic order. To see the monomials in matching order, use

>>> p.monoms()
[(3, 0, 0), (2, 1, 0), (2, 0, 1), (1, 2, 0), (1, 1, 1), (1, 0, 2), (0, 3, 0), (0, 2, 1), (0, 1, 2), (0, 0, 3)]

To get the coefficient of a particular monomial, use

>>> p.coeff_monomial(x**2*y)
6

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...