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.Wed, 02 Jul 2014 12:00:37 +0200manipulation of multivariate polynomialhttps://ask.sagemath.org/question/23156/manipulation-of-multivariate-polynomial/Hello,
I have an expression that comes from expanding a product of polynomials in one variable each. For demonstration purposes I'm using the Symbolic Ring but the question is not specific to it:
sage: var('a,b')
(a, b)
sage: ex=(1+a+a^2+a^3)*(1+b+b^2+b^3)
sage: ex.expand()
a^3*b^3 + a^3*b^2 + a^2*b^3 + a^3*b + a^2*b^2 + a*b^3 + a^3 + a^2*b + a*b^2 + b^3 + a^2 + a*b + b^2 + a + b + 1
Is there a nice way to get a new expression from that which has only exponents <=3 when a and b are seen as identical? The above product would then yield:
a^3 + a^2*b + a*b^2 + b^3 + a^2 + a*b + b^2 + a + b + 1
that is, for example a*b^3 is removed because it has combined exponent 4.
I'm looking for a way to achieve this without writing a loop that goes through all terms.
Tue, 01 Jul 2014 17:18:06 +0200https://ask.sagemath.org/question/23156/manipulation-of-multivariate-polynomial/Answer by vdelecroix for <p>Hello,
I have an expression that comes from expanding a product of polynomials in one variable each. For demonstration purposes I'm using the Symbolic Ring but the question is not specific to it:</p>
<pre><code>sage: var('a,b')
(a, b)
sage: ex=(1+a+a^2+a^3)*(1+b+b^2+b^3)
sage: ex.expand()
a^3*b^3 + a^3*b^2 + a^2*b^3 + a^3*b + a^2*b^2 + a*b^3 + a^3 + a^2*b + a*b^2 + b^3 + a^2 + a*b + b^2 + a + b + 1
</code></pre>
<p>Is there a nice way to get a new expression from that which has only exponents <=3 when a and b are seen as identical? The above product would then yield:</p>
<pre><code>a^3 + a^2*b + a*b^2 + b^3 + a^2 + a*b + b^2 + a + b + 1
</code></pre>
<p>that is, for example a*b^3 is removed because it has combined exponent 4.</p>
<p>I'm looking for a way to achieve this without writing a loop that goes through all terms.</p>
https://ask.sagemath.org/question/23156/manipulation-of-multivariate-polynomial/?answer=23165#post-id-23165I have a loop but not through the terms
sage: R.<a,b> = ZZ[]
sage: f=(1+a+a^2+a^3)*(1+b+b^2+b^3)
sage: f.mod([a^i*b^(4-i) for i in xrange(5)])
a^3 + a^2*b + a*b^2 + b^3 + a^2 + a*b + b^2 + a + b + 1
VincentWed, 02 Jul 2014 09:46:58 +0200https://ask.sagemath.org/question/23156/manipulation-of-multivariate-polynomial/?answer=23165#post-id-23165Answer by rws for <p>Hello,
I have an expression that comes from expanding a product of polynomials in one variable each. For demonstration purposes I'm using the Symbolic Ring but the question is not specific to it:</p>
<pre><code>sage: var('a,b')
(a, b)
sage: ex=(1+a+a^2+a^3)*(1+b+b^2+b^3)
sage: ex.expand()
a^3*b^3 + a^3*b^2 + a^2*b^3 + a^3*b + a^2*b^2 + a*b^3 + a^3 + a^2*b + a*b^2 + b^3 + a^2 + a*b + b^2 + a + b + 1
</code></pre>
<p>Is there a nice way to get a new expression from that which has only exponents <=3 when a and b are seen as identical? The above product would then yield:</p>
<pre><code>a^3 + a^2*b + a*b^2 + b^3 + a^2 + a*b + b^2 + a + b + 1
</code></pre>
<p>that is, for example a*b^3 is removed because it has combined exponent 4.</p>
<p>I'm looking for a way to achieve this without writing a loop that goes through all terms.</p>
https://ask.sagemath.org/question/23156/manipulation-of-multivariate-polynomial/?answer=23166#post-id-23166I meanwhile found that in the ring of multivariate power series the above product can be truncated using bigoh with two arguments:
sage: R.<a,b> = PowerSeriesRing(QQ); R
Multivariate Power Series Ring in a, b over Rational Field
sage: (1+a+a^2+a^3)*(1+b+b^2+b^3)+O(a,b)^4
1 + a + b + a^2 + a*b + b^2 + a^3 + a^2*b + a*b^2 + b^3 + O(a, b)^4
Wed, 02 Jul 2014 10:44:37 +0200https://ask.sagemath.org/question/23156/manipulation-of-multivariate-polynomial/?answer=23166#post-id-23166Comment by niles for <p>I meanwhile found that in the ring of multivariate power series the above product can be truncated using bigoh with two arguments:</p>
<pre><code>sage: R.<a,b> = PowerSeriesRing(QQ); R
Multivariate Power Series Ring in a, b over Rational Field
sage: (1+a+a^2+a^3)*(1+b+b^2+b^3)+O(a,b)^4
1 + a + b + a^2 + a*b + b^2 + a^3 + a^2*b + a*b^2 + b^3 + O(a, b)^4
</code></pre>
https://ask.sagemath.org/question/23156/manipulation-of-multivariate-polynomial/?comment=23167#post-id-23167I think (but am not sure) that both the big-O and .mod use a loop over terms internally, but it sounds like that's ok with you.Wed, 02 Jul 2014 12:00:37 +0200https://ask.sagemath.org/question/23156/manipulation-of-multivariate-polynomial/?comment=23167#post-id-23167