Ask Your Question
1

get the coefficients from the polynomial of several variables

asked 2024-07-01 10:23:25 +0100

fusheng gravatar image

updated 2024-07-01 12:14:58 +0100

Max Alekseyev gravatar image

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 flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
2

answered 2024-07-01 10:40:43 +0100

Max Alekseyev gravatar image

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) )
edit flag offensive delete link more

Comments

Thank you!

fusheng gravatar imagefusheng ( 2024-07-01 13:40:39 +0100 )edit
1

answered 2024-07-02 12:59:40 +0100

tmonteil gravatar image

updated 2024-07-02 13:41:24 +0100

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).

edit flag offensive delete link more

Comments

One can avoid specifying n at all by using InfinitePolynomialRing.

Max Alekseyev gravatar imageMax Alekseyev ( 2024-07-02 19:05:24 +0100 )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

Stats

Asked: 2024-07-01 10:22:31 +0100

Seen: 347 times

Last updated: Jul 02