get the coefficients from the polynomial of several variables

I have a polynomial $p(x)=[(x_1-x_2)(x_1-x_3)(x_1-x_4)(x_2-x_3)(x_2-x_4)(x_3-x_4)](x_1+x_2+x_3+x_4)^{18}(x_1^2+x_2^2+x_3^2+x_4^2)^2.$

I want to know the coefficient of $x_1^{11}x^8_2x_3^5x^2_4$ by sagemath.

I am not familar with sagemath. What should i do for this question ?
Thanks!

edit retag close merge delete

Sort by » oldest newest most voted

Like this:

K.<x_1,x_2,x_3,x_4> = QQ[]
p = (x_1-x_2)*(x_1-x_3)*(x_1-x_4)*(x_2-x_3)*(x_2-x_4)*(x_3-x_4)^18*(x_1^2+x_2^2+x_3^2+x_4^2)^2
print( p.monomial_coefficient(x_1^11*x_2^8*x_3^5*x_4^2) )

more

Thank you!

( 2024-07-01 13:40:39 +0200 )edit

To complement @max-alekseyev answer, in case one would like the number of indeterminates to change (since they play a symmetric role), you can make the construction depend on the number of indeterminates, as a parameter.

For this, we can first define the polynomial ring, by providing the base ring, the structure of the names of the indeterminates, and the number of indeterminates:

sage: n = 4
sage: K = PolynomialRing(QQ, 'x_', n)


We have:

sage: K
Multivariate Polynomial Ring in x_0, x_1, x_2, x_3 over Rational Field


But, we did not let the Python names x_i point to the polynomial indeterminates x_i:

sage: x_2
NameError: name 'x_2' is not defined


For this, we can do:

sage: K.inject_variables()
Defining x_0, x_1, x_2, x_3


We now have:

sage: x_2
x_2


To easily make loops, instead of manipulating the strings x_i by hand, we can use the tuple of all the indeterminates:

sage: x = K.gens()
sage: x
(x_0, x_1, x_2, x_3)

sage: x[2]
x_2


Now, the first part of your polynomial is a product of (x_i-x_j) for j<i which can be written as a double loop :

sage: prod(x[i]-x[j] for i in range(n) for j in range(i))


Similarly, we can define the whole polynomial using loops:

sage: p = prod(x[i]-x[j] for i in range(n) for j in range(i)) * sum(x[i] for i in range(n))^18 * sum(x[i]^2 for i in range(n))^2


Then, we can extract coefficients you want:

sage: p.monomial_coefficient(x_0^11*x_1^8*x_2^5*x_3^2)
0


Note that it is easier to start with index zero here, since everything in Python start at zero, so that loops are easier to write. We could of course play with i+1 and i-1 everywhere, but learning to work with indices starting at 0 might be a better long-term investment (we will need it in manipulating matrices for example).

more

One can avoid specifying n at all by using InfinitePolynomialRing.

( 2024-07-02 19:05:24 +0200 )edit