Polynomials with multiple variables and abstract coefficients

Hey there,

I need to create a polynomials (say P1, P2) that will have a number of variables (ca. 5) and abstract coefficients (ca. 9) (but I can assume that coefficients are variables carrying integer values). Then I would like to multiply P1, P2 and collect variables and extract coefficients standing next to them, e.g

from XA * XB + XA^2 + 2 * A * XA + 4*XB / XC + 5 I should be able to get 2*A*XA + XA^2 + XA*XB + 4*XB/XC + 5 and from this a list of coefficients like [2A, 1, 1, 4, 5] (with zeros in proper places eg. coefficient for XAXBXC)

Browsing the net I have found sth like: B.<x,y,z> = QQ[]; A.<x,y,z>=B[]; ex = (1-a^2)*x*y^2+(a-b^2+c)*x*y*z+(b^2-c^2-a)*x^2*z; ex.coefficients(); ex.monomials()

And my questions are: (1.) how can I pass a list of variables to define a ring, I am interested in sth like B.list = QQ[]; A.list2 = B[] (2.) As you run the code above you can easily note that the output is: [-x*y^2, x^2*z - x*y*z, -x^2*z, -x^2*z + x*y*z, x*y*z, x*y^2]; [a^2, b^2, c^2, a, c, 1] As you can see the list from ex.coefficients() contains multiply entries for xy^2 and -xy^2 separately. And this is no go. Any ideas how can I fix it?

Thanks for any suggestions! Bests Michal

edit retag close merge delete

Sort by » oldest newest most voted

Regarding your question (1), you can not pass a list of "variables" since they are not defined yet (or there is a chicken-and-egg stuff there). However, you can pass the names of the indeterminates as a list of strings as follows:

sage: B = PolynomialRing(QQ,['a','b','c'])
sage: B
Multivariate Polynomial Ring in a, b, c over Rational Field


Now, if you want the Python name a point to the polynomial indeterminate a, and so on, you have to do:

sage: B.inject_variables()
Defining a, b, c


Regarding your question (2), if i understand your question, it seems you are doing things in the reverse order. What you want are polynomial whose indeterminates are x,y,z, so you will define them over the ring which is made of the polynomials over QQ with variables a,b,c:

sage: B.<a,b,c> = QQ[]; B
Multivariate Polynomial Ring in a, b, c over Rational Field
sage: A.<x,y,z>=B[] ; A
Multivariate Polynomial Ring in x, y, z over Multivariate Polynomial Ring in a, b, c over Rational Field
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
(-a^2 + 1)*x*y^2 + (b^2 - c^2 - a)*x^2*z + (-b^2 + a + c)*x*y*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]


But also:

sage: list(ex)
[(-a^2 + 1, x*y^2), (b^2 - c^2 - a, x^2*z), (-b^2 + a + c, x*y*z)]
sage: dict(ex)
{-b^2 + a + c: x*y*z, b^2 - c^2 - a: x^2*z, -a^2 + 1: x*y^2}
sage: ex.dict()
{(1, 1, 1): -b^2 + a + c, (1, 2, 0): -a^2 + 1, (2, 0, 1): b^2 - c^2 - a}

more