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.Tue, 19 Jan 2021 10:57:55 +0100Multiply polynomials from different ringshttps://ask.sagemath.org/question/55351/multiply-polynomials-from-different-rings/Suppose I take a polynomial from $K[x]$, say $x^2 + 5x$,
and another polynomial from $K[y]$, say $y^3$.
I want to formally multiply them and get $x^2y^3 + 5xy^3$ as the output.
How can I do that?
Note: `K` is the finite field of size 4 in `a`.
My efforts:
sage: K.<a> = FiniteField(4)
sage: R_1 = PolynomialRing(K, ['z%s' % p for p in range(1, 3)])
sage: R_2 = PolynomialRing(K, ['x%s' % p for p in range(1, 3)])
sage: pp = R_1.random_element()
sage: pp
(a + 1)*z1^2 + (a)*z1*z2 + (a + 1)*z2 + (a)
sage: qq = R_2.random_element()
sage: qq
x1*x2 + (a + 1)*x2^2 + x1 + 1
When I do `pp*qq` I get the following output
unsupported operand parent(s) for *: 'Multivariate Polynomial Ring
in z1, z2 over Finite Field in a of size 2^2' and 'Multivariate Polynomial
Ring in x1, x2 over Finite Field in a of size 2^2'Tue, 19 Jan 2021 10:36:55 +0100https://ask.sagemath.org/question/55351/multiply-polynomials-from-different-rings/Answer by tmonteil for <p>Suppose I take a polynomial from $K[x]$, say $x^2 + 5x$,
and another polynomial from $K[y]$, say $y^3$.
I want to formally multiply them and get $x^2y^3 + 5xy^3$ as the output.</p>
<p>How can I do that?</p>
<p>Note: <code>K</code> is the finite field of size 4 in <code>a</code>.</p>
<p>My efforts: </p>
<pre><code>sage: K.<a> = FiniteField(4)
sage: R_1 = PolynomialRing(K, ['z%s' % p for p in range(1, 3)])
sage: R_2 = PolynomialRing(K, ['x%s' % p for p in range(1, 3)])
sage: pp = R_1.random_element()
sage: pp
(a + 1)*z1^2 + (a)*z1*z2 + (a + 1)*z2 + (a)
sage: qq = R_2.random_element()
sage: qq
x1*x2 + (a + 1)*x2^2 + x1 + 1
</code></pre>
<p>When I do <code>pp*qq</code> I get the following output</p>
<pre><code>unsupported operand parent(s) for *: 'Multivariate Polynomial Ring
in z1, z2 over Finite Field in a of size 2^2' and 'Multivariate Polynomial
Ring in x1, x2 over Finite Field in a of size 2^2'
</code></pre>
https://ask.sagemath.org/question/55351/multiply-polynomials-from-different-rings/?answer=55352#post-id-55352Ther is not *coercion* (see the doc) defined between the two polynomial rings, so you have to construct a common *parent* by yourself and *convert* the polynomials. Here is a possible way:
First, you can get the names of the variables of both polynomial rings as follows:
sage: R_1.variable_names() + R_2.variable_names()
('z1', 'z2', 'x1', 'x2')
Then you can construct the polynomial ring with this new list of variable names:
sage: R = PolynomialRing(K, R_1.variable_names() + R_2.variable_names())
sage: R
Multivariate Polynomial Ring in z1, z2, x1, x2 over Finite Field in a of size 2^2
Then, you can convert a polynomial from `R_1` to `R`:
sage: pp
(a)*z1^2 + (a)*z2^2 + z1 + z2
sage: pp.parent()
Multivariate Polynomial Ring in z1, z2 over Finite Field in a of size 2^2
sage: R(pp)
(a)*z1^2 + (a)*z2^2 + z1 + z2
sage: R(pp).parent()
Multivariate Polynomial Ring in z1, z2, x1, x2 over Finite Field in a of size 2^2
Now, you can add the converted polynomials within the new larger parent:
sage: R(pp) + R(qq)
(a)*z1^2 + (a)*z2^2 + (a)*x1^2 + x1*x2 + (a + 1)*x2^2 + z1 + z2 + 1
sage: rr = R(pp) + R(qq)
sage: rr
(a)*z1^2 + (a)*z2^2 + (a)*x1^2 + x1*x2 + (a + 1)*x2^2 + z1 + z2 + 1
sage: rr.parent()
Multivariate Polynomial Ring in z1, z2, x1, x2 over Finite Field in a of size 2^2
Tue, 19 Jan 2021 10:53:05 +0100https://ask.sagemath.org/question/55351/multiply-polynomials-from-different-rings/?answer=55352#post-id-55352Comment by Sri1729 for <p>Ther is not <em>coercion</em> (see the doc) defined between the two polynomial rings, so you have to construct a common <em>parent</em> by yourself and <em>convert</em> the polynomials. Here is a possible way:</p>
<p>First, you can get the names of the variables of both polynomial rings as follows:</p>
<pre><code>sage: R_1.variable_names() + R_2.variable_names()
('z1', 'z2', 'x1', 'x2')
</code></pre>
<p>Then you can construct the polynomial ring with this new list of variable names:</p>
<pre><code>sage: R = PolynomialRing(K, R_1.variable_names() + R_2.variable_names())
sage: R
Multivariate Polynomial Ring in z1, z2, x1, x2 over Finite Field in a of size 2^2
</code></pre>
<p>Then, you can convert a polynomial from <code>R_1</code> to <code>R</code>:</p>
<pre><code>sage: pp
(a)*z1^2 + (a)*z2^2 + z1 + z2
sage: pp.parent()
Multivariate Polynomial Ring in z1, z2 over Finite Field in a of size 2^2
sage: R(pp)
(a)*z1^2 + (a)*z2^2 + z1 + z2
sage: R(pp).parent()
Multivariate Polynomial Ring in z1, z2, x1, x2 over Finite Field in a of size 2^2
</code></pre>
<p>Now, you can add the converted polynomials within the new larger parent:</p>
<pre><code>sage: R(pp) + R(qq)
(a)*z1^2 + (a)*z2^2 + (a)*x1^2 + x1*x2 + (a + 1)*x2^2 + z1 + z2 + 1
sage: rr = R(pp) + R(qq)
sage: rr
(a)*z1^2 + (a)*z2^2 + (a)*x1^2 + x1*x2 + (a + 1)*x2^2 + z1 + z2 + 1
sage: rr.parent()
Multivariate Polynomial Ring in z1, z2, x1, x2 over Finite Field in a of size 2^2
</code></pre>
https://ask.sagemath.org/question/55351/multiply-polynomials-from-different-rings/?comment=55353#post-id-55353Don't have words to thank you. I spent a lot of time on this but still was not able to think about it. Have a good day :)Tue, 19 Jan 2021 10:57:55 +0100https://ask.sagemath.org/question/55351/multiply-polynomials-from-different-rings/?comment=55353#post-id-55353