ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 02 May 2020 02:03:02 -0500Polynomials with multiple variables and abstract coefficientshttps://ask.sagemath.org/question/34824/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 XA*XB*XC)
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 x*y^2 and -x*y^2 separately. And this is no go. Any ideas how can I fix it?
Thanks for any suggestions!
Bests
MichalThu, 15 Sep 2016 06:22:11 -0500https://ask.sagemath.org/question/34824/polynomials-with-multiple-variables-and-abstract-coefficients/Answer by tmonteil for <p>Hey there,</p>
<p>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</p>
<p>from
<code>XA * XB + XA^2 + 2 * A * XA + 4*XB / XC + 5</code>
I should be able to get
<code>2*A*XA + XA^2 + XA*XB + 4*XB/XC + 5</code>
and from this a list of coefficients like
<code>[2A, 1, 1, 4, 5]</code> (with zeros in proper places eg. coefficient for XA<em>XB</em>XC)</p>
<p>Browsing the net I have found sth like:
<code>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()</code></p>
<p>And my questions are:
(1.) how can I pass a list of variables to define a ring, I am interested in sth like
<code>B.list = QQ[];
A.list2 = B[]</code>
(2.) As you run the code above you can easily note that the output is:
<code>[-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]</code>
As you can see the list from ex.coefficients() contains multiply entries for x<em>y^2 and -x</em>y^2 separately. And this is no go. Any ideas how can I fix it?</p>
<p>Thanks for any suggestions!
Bests
Michal</p>
https://ask.sagemath.org/question/34824/polynomials-with-multiple-variables-and-abstract-coefficients/?answer=34829#post-id-34829Regarding 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}Thu, 15 Sep 2016 15:51:33 -0500https://ask.sagemath.org/question/34824/polynomials-with-multiple-variables-and-abstract-coefficients/?answer=34829#post-id-34829Comment by mpzajac for <p>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:</p>
<pre><code>sage: B = PolynomialRing(QQ,['a','b','c'])
sage: B
Multivariate Polynomial Ring in a, b, c over Rational Field
</code></pre>
<p>Now, if you want the Python name <code>a</code> point to the polynomial indeterminate <code>a</code>, and so on, you have to do:</p>
<pre><code>sage: B.inject_variables()
Defining a, b, c
</code></pre>
<p>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 <code>x,y,z</code>, so you will define them over the ring which is made of the polynomials over <code>QQ</code> with variables <code>a,b,c</code>:</p>
<pre><code>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]
</code></pre>
<p>But also:</p>
<pre><code>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}
</code></pre>
https://ask.sagemath.org/question/34824/polynomials-with-multiple-variables-and-abstract-coefficients/?comment=51198#post-id-51198Thanks, that helped, sorry for a delayed reply!Sat, 02 May 2020 02:03:02 -0500https://ask.sagemath.org/question/34824/polynomials-with-multiple-variables-and-abstract-coefficients/?comment=51198#post-id-51198