ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 30 May 2020 13:39:07 +0200Equating coefficients of multivariate polynomialshttps://ask.sagemath.org/question/51639/equating-coefficients-of-multivariate-polynomials/Here is a simple question, and despite looking at answers to other questions I cannot find how to proceed. I would be extremely grateful for any help.
We have a polynomial Pol in several variables (with rational coefficients, if it helps). Its coefficients involve some parameters. And I would like to find values of these parameter so that Pol is the zero polynomial. Here's some code that does not work but capture what I've tried to do.
var('a0,a1') ; R.<x,y> = QQ[] ; Pol = (a1*x+a0)*(1-x*y)-5*y*x ;
ad then I would like to solve (in the variables a0, a1) the system of coefficients equated to 0:
WA = [SR(Pol).coefficient({x:0,y:0})==0,
SR(Pol).coefficient({x:2,y:1})==0,
SR(Pol).coefficient({x:1,y:1})==0,
SR(Pol).coefficient({x:1,y:0})==0
].solve(SR(a0),SR(a1)); WASat, 30 May 2020 10:14:30 +0200https://ask.sagemath.org/question/51639/equating-coefficients-of-multivariate-polynomials/Answer by tmonteil for <p>Here is a simple question, and despite looking at answers to other questions I cannot find how to proceed. I would be extremely grateful for any help.</p>
<p>We have a polynomial Pol in several variables (with rational coefficients, if it helps). Its coefficients involve some parameters. And I would like to find values of these parameter so that Pol is the zero polynomial. Here's some code that does not work but capture what I've tried to do.</p>
<pre><code>var('a0,a1') ; R.<x,y> = QQ[] ; Pol = (a1*x+a0)*(1-x*y)-5*y*x ;
</code></pre>
<p>ad then I would like to solve (in the variables a0, a1) the system of coefficients equated to 0:</p>
<pre><code>WA = [SR(Pol).coefficient({x:0,y:0})==0,
SR(Pol).coefficient({x:2,y:1})==0,
SR(Pol).coefficient({x:1,y:1})==0,
SR(Pol).coefficient({x:1,y:0})==0
].solve(SR(a0),SR(a1)); WA
</code></pre>
https://ask.sagemath.org/question/51639/equating-coefficients-of-multivariate-polynomials/?answer=51641#post-id-51641Whe you write :
sage: var('a0,a1') ; R.<x,y> = QQ[] ;
sage: Pol = (a1*x+a0)*(1-x*y)-5*y*x ;
The product `a1*x` (and the other elementary operations) is done using *coercion*, that is Sage first searches the common parent between the symbolic ring and the bivariate rational polynomials in `x,y`. It turns out that it is the symbolic ring itself. Hence, what you get is and element of the symbolic ring:
sage: Pol.parent()
Symbolic Ring
In particular, you lose the polynomial structure in `x,y`.
The first approach is to let Sage consider `a0` and `a1` a possible coefficients for polynomials in `x,y`, by declaring the polynomial ring `R` to be defined over the symbolic ring `SR`:
sage: var('a0,a1') ; R.<x,y> = SR[]
(a0, a1)
sage: Pol = (a1*x+a0)*(1-x*y)-5*y*x
sage: Pol
(-a1)*x^2*y + (-a0 - 5)*x*y + a1*x + a0
sage: Pol.parent()
Multivariate Polynomial Ring in x, y over Symbolic Ring
Then you can extract the nonzero coefficients:
sage: Pol.coefficients()
[-a1, -a0 - 5, a1, a0]
sage: solve(Pol.coefficients(),[a0,a1])
[]
There is of course no solution since `a0` should be both equal to `0` and `-5`.
Another approach is to let the coefficients be elements of another polynomial ring `S`, that will be the ring over which `R` will be defined:
sage: S.<a0,a1> = QQ[] ; S
Multivariate Polynomial Ring in a0, a1 over Rational Field
sage: R.<x,y> = S[] ; R
Multivariate Polynomial Ring in x, y over Multivariate Polynomial Ring in a0, a1 over Rational Field
sage: Pol = (a1*x+a0)*(1-x*y)-5*y*x
sage: Pol
(-a1)*x^2*y + (-a0 - 5)*x*y + a1*x + a0
sage: Pol.parent()
Multivariate Polynomial Ring in x, y over Multivariate Polynomial Ring in a0, a1 over Rational Field
Now, the common zeroes of the coeficients of `Pol` (which are polynomials, elements of `S`), is nothing but the variety of an ideal:
sage: Pol.coefficients()
[-a1, -a0 - 5, a1, a0]
sage: S.ideal(Pol.coefficients())
Ideal (-a1, -a0 - 5, a1, a0) of Multivariate Polynomial Ring in a0, a1 over Rational Field
sage: I = S.ideal(Pol.coefficients())
sage: I.variety()
[]
Which is empty as well.Sat, 30 May 2020 13:01:19 +0200https://ask.sagemath.org/question/51639/equating-coefficients-of-multivariate-polynomials/?answer=51641#post-id-51641Comment by Tom11 for <p>Whe you write : </p>
<pre><code>sage: var('a0,a1') ; R.<x,y> = QQ[] ;
sage: Pol = (a1*x+a0)*(1-x*y)-5*y*x ;
</code></pre>
<p>The product <code>a1*x</code> (and the other elementary operations) is done using <em>coercion</em>, that is Sage first searches the common parent between the symbolic ring and the bivariate rational polynomials in <code>x,y</code>. It turns out that it is the symbolic ring itself. Hence, what you get is and element of the symbolic ring:</p>
<pre><code>sage: Pol.parent()
Symbolic Ring
</code></pre>
<p>In particular, you lose the polynomial structure in <code>x,y</code>.</p>
<p>The first approach is to let Sage consider <code>a0</code> and <code>a1</code> a possible coefficients for polynomials in <code>x,y</code>, by declaring the polynomial ring <code>R</code> to be defined over the symbolic ring <code>SR</code>:</p>
<pre><code>sage: var('a0,a1') ; R.<x,y> = SR[]
(a0, a1)
sage: Pol = (a1*x+a0)*(1-x*y)-5*y*x
sage: Pol
(-a1)*x^2*y + (-a0 - 5)*x*y + a1*x + a0
sage: Pol.parent()
Multivariate Polynomial Ring in x, y over Symbolic Ring
</code></pre>
<p>Then you can extract the nonzero coefficients:</p>
<pre><code>sage: Pol.coefficients()
[-a1, -a0 - 5, a1, a0]
sage: solve(Pol.coefficients(),[a0,a1])
[]
</code></pre>
<p>There is of course no solution since <code>a0</code> should be both equal to <code>0</code> and <code>-5</code>.</p>
<p>Another approach is to let the coefficients be elements of another polynomial ring <code>S</code>, that will be the ring over which <code>R</code> will be defined:</p>
<pre><code>sage: S.<a0,a1> = QQ[] ; S
Multivariate Polynomial Ring in a0, a1 over Rational Field
sage: R.<x,y> = S[] ; R
Multivariate Polynomial Ring in x, y over Multivariate Polynomial Ring in a0, a1 over Rational Field
sage: Pol = (a1*x+a0)*(1-x*y)-5*y*x
sage: Pol
(-a1)*x^2*y + (-a0 - 5)*x*y + a1*x + a0
sage: Pol.parent()
Multivariate Polynomial Ring in x, y over Multivariate Polynomial Ring in a0, a1 over Rational Field
</code></pre>
<p>Now, the common zeroes of the coeficients of <code>Pol</code> (which are polynomials, elements of <code>S</code>), is nothing but the variety of an ideal:</p>
<pre><code>sage: Pol.coefficients()
[-a1, -a0 - 5, a1, a0]
sage: S.ideal(Pol.coefficients())
Ideal (-a1, -a0 - 5, a1, a0) of Multivariate Polynomial Ring in a0, a1 over Rational Field
sage: I = S.ideal(Pol.coefficients())
sage: I.variety()
[]
</code></pre>
<p>Which is empty as well.</p>
https://ask.sagemath.org/question/51639/equating-coefficients-of-multivariate-polynomials/?comment=51643#post-id-51643Thank you so much for this detailed answer, this is perfect! (Yes in this simple example there is no solution, my actual application has lots of variables and a higher degree so it would have been clumsy to show here).Sat, 30 May 2020 13:39:07 +0200https://ask.sagemath.org/question/51639/equating-coefficients-of-multivariate-polynomials/?comment=51643#post-id-51643