Ask Your Question

Extracting coefficients of multivariate polynomials?

asked 2013-06-05 21:46:50 +0200

Alasdair gravatar image

If I enter, say:

P.<x,y,z> = QQ[]
ex = 6*x*y^2-4*x*y*z+2*x^2*z

then Sage returns

[6, -4, 2]

as it should. My problem is that I'm trying to obtain a list of coefficients when the coefficients are themselves expressions:

ex = (1-a^2)*x*y^2+(a-b^2+c)*x*y*z+(b^2-c^2-a)*x^2*z

What I want returned is

[1-a^2, a-b^2+c, b^2-c^2-a]

(the order doesn't matter). If I enter a, b, c as elements of the same polynomial ring as x, y, z then I have the problem of distinguishing the different variables: x, y, z, from a, b, c.

What's the best way to do this?

Thanks, Alasdair

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2016-04-14 08:45:04 +0200

nbruin gravatar image

updated 2016-04-14 08:46:57 +0200

Just define the appropriate polynomial rings so that coefficients does what you want:

sage: A.<a,b,c>=QQ[]
sage: B.<x,y,z>=A[]
sage: ex = (1-a^2)*x*y^2+(a-b^2+c)*x*y*z+(b^2-c^2-a)*x^2*z
sage: ex.coefficients()
[-a^2 + 1, b^2 - c^2 - a, -b^2 + a + c]
sage: ex.monomials()
[x*y^2, x^2*z, x*y*z]
edit flag offensive delete link more

answered 2013-06-05 22:40:40 +0200

calc314 gravatar image

updated 2013-06-05 22:41:22 +0200

Here is a quick hack.

map(lambda w: w.subs(x=1,y=1,z=1),ex.operands())

I don't see an obvious command to extract abstract coefficients, even list doesn't work.

edit flag offensive delete link more


Yep, that certainly works - thanks! I was hoping, though, that there might be a natural, simple command...

Alasdair gravatar imageAlasdair ( 2013-06-05 22:53:11 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2013-06-05 21:46:50 +0200

Seen: 3,539 times

Last updated: Apr 14 '16