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.Thu, 24 Nov 2022 18:16:51 +0100Return degree of polynomial in InfinitePolynomialRinghttps://ask.sagemath.org/question/64903/return-degree-of-polynomial-in-infinitepolynomialring/Consider the following:
sage: R.<a> = InfinitePolynomialRing(QQ)
sage: p = a[1]*a[2]^2
Then we have
sage: p.degree(a[1].variable())
1
sage: p.degree(a[2].variable())
2
but
sage: p.degree(a[3].variable())
...
TypeError: argument is not coercible to the parent
instead of returning `0`. Moreover, if I execute one of the commands that worked before again, for example `p.degree(a[1].variable())`, it yields the same error. How can I solve this?Sat, 19 Nov 2022 01:16:33 +0100https://ask.sagemath.org/question/64903/return-degree-of-polynomial-in-infinitepolynomialring/Answer by Thrash for <p>Consider the following:</p>
<pre><code>sage: R.<a> = InfinitePolynomialRing(QQ)
sage: p = a[1]*a[2]^2
</code></pre>
<p>Then we have</p>
<pre><code>sage: p.degree(a[1].variable())
1
sage: p.degree(a[2].variable())
2
</code></pre>
<p>but </p>
<pre><code>sage: p.degree(a[3].variable())
...
TypeError: argument is not coercible to the parent
</code></pre>
<p>instead of returning <code>0</code>. Moreover, if I execute one of the commands that worked before again, for example <code>p.degree(a[1].variable())</code>, it yields the same error. How can I solve this?</p>
https://ask.sagemath.org/question/64903/return-degree-of-polynomial-in-infinitepolynomialring/?answer=65043#post-id-65043`p.degrees()[-d-1]` yields the exponent that belongs to `a[d]`, even after we introduce a higher `a[d]`:
sage: R.<a> = InfinitePolynomialRing(QQ)
....: p = a[1]*a[2]^2
....: p.degrees()[-2-1]
2
sage: p.degrees()[-3-1]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In [2], line 1
----> 1 p.degrees()[-Integer(3)-Integer(1)]
IndexError: tuple index out of range
sage: a[3]
a_3
sage: p.degrees()[-2-1]
2
It would be nice to have `0` as the output instead of the error.Thu, 24 Nov 2022 18:16:51 +0100https://ask.sagemath.org/question/64903/return-degree-of-polynomial-in-infinitepolynomialring/?answer=65043#post-id-65043Answer by Max Alekseyev for <p>Consider the following:</p>
<pre><code>sage: R.<a> = InfinitePolynomialRing(QQ)
sage: p = a[1]*a[2]^2
</code></pre>
<p>Then we have</p>
<pre><code>sage: p.degree(a[1].variable())
1
sage: p.degree(a[2].variable())
2
</code></pre>
<p>but </p>
<pre><code>sage: p.degree(a[3].variable())
...
TypeError: argument is not coercible to the parent
</code></pre>
<p>instead of returning <code>0</code>. Moreover, if I execute one of the commands that worked before again, for example <code>p.degree(a[1].variable())</code>, it yields the same error. How can I solve this?</p>
https://ask.sagemath.org/question/64903/return-degree-of-polynomial-in-infinitepolynomialring/?answer=64904#post-id-64904A variable in `InfinitePolynomialRing` object does not exist until you explicitly introduce it. E.g., defining
p = a[1]*a[2]^2 + 0*a[3]
makes the error go away.
See also https://ask.sagemath.org/question/53319/Sat, 19 Nov 2022 02:57:05 +0100https://ask.sagemath.org/question/64903/return-degree-of-polynomial-in-infinitepolynomialring/?answer=64904#post-id-64904Comment by Max Alekseyev for <p>A variable in <code>InfinitePolynomialRing</code> object does not exist until you explicitly introduce it. E.g., defining</p>
<pre><code>p = a[1]*a[2]^2 + 0*a[3]
</code></pre>
<p>makes the error go away.</p>
<p>See also <a href="https://ask.sagemath.org/question/53319/">https://ask.sagemath.org/question/53319/</a></p>
https://ask.sagemath.org/question/64903/return-degree-of-polynomial-in-infinitepolynomialring/?comment=64939#post-id-64939Feel free to add an answer to your own question for benefits of other users.Sun, 20 Nov 2022 02:22:00 +0100https://ask.sagemath.org/question/64903/return-degree-of-polynomial-in-infinitepolynomialring/?comment=64939#post-id-64939Comment by Thrash for <p>A variable in <code>InfinitePolynomialRing</code> object does not exist until you explicitly introduce it. E.g., defining</p>
<pre><code>p = a[1]*a[2]^2 + 0*a[3]
</code></pre>
<p>makes the error go away.</p>
<p>See also <a href="https://ask.sagemath.org/question/53319/">https://ask.sagemath.org/question/53319/</a></p>
https://ask.sagemath.org/question/64903/return-degree-of-polynomial-in-infinitepolynomialring/?comment=64933#post-id-64933However, `p.degrees()[-d-1]` does the job and at least doesn't mess up anything. Try the following:
R.<a> = InfinitePolynomialRing(QQ)
p = a[1]*a[2]^2
p.degrees()[-3-1] # yields an error, but doesn't change the internal configuration
p.degrees()[-2-1] # still works (because we didn't introduce a[3])Sat, 19 Nov 2022 23:11:17 +0100https://ask.sagemath.org/question/64903/return-degree-of-polynomial-in-infinitepolynomialring/?comment=64933#post-id-64933Comment by Thrash for <p>A variable in <code>InfinitePolynomialRing</code> object does not exist until you explicitly introduce it. E.g., defining</p>
<pre><code>p = a[1]*a[2]^2 + 0*a[3]
</code></pre>
<p>makes the error go away.</p>
<p>See also <a href="https://ask.sagemath.org/question/53319/">https://ask.sagemath.org/question/53319/</a></p>
https://ask.sagemath.org/question/64903/return-degree-of-polynomial-in-infinitepolynomialring/?comment=64932#post-id-64932It still doesn't work when you start with `a[3]` (in this example). Try the following
def mydegree(p,v):
return p.degree(v.variable()) if v in p.variables() else 0
R.<a> = InfinitePolynomialRing(QQ)
p = a[1]*a[2]^2
mydegree(p,a[3]) # yields an error and changes the internal configuration
mydegree(p,a[2]) # now yields an error too
Whenever I type `a[3]`, it messes up the originally good behaviour and I have to create `p` again in order to make it work.Sat, 19 Nov 2022 22:54:05 +0100https://ask.sagemath.org/question/64903/return-degree-of-polynomial-in-infinitepolynomialring/?comment=64932#post-id-64932Comment by Max Alekseyev for <p>A variable in <code>InfinitePolynomialRing</code> object does not exist until you explicitly introduce it. E.g., defining</p>
<pre><code>p = a[1]*a[2]^2 + 0*a[3]
</code></pre>
<p>makes the error go away.</p>
<p>See also <a href="https://ask.sagemath.org/question/53319/">https://ask.sagemath.org/question/53319/</a></p>
https://ask.sagemath.org/question/64903/return-degree-of-polynomial-in-infinitepolynomialring/?comment=64926#post-id-64926Unfortunately, implementation of `InfinitePolynomialRing` appears to be very shallow. You are welcome to submit a bugreport requesting it to be fixed/enhanced in a particular way - e.g. see mine at https://trac.sagemath.org/ticket/34758
Meanwhile, as a workaround you can use a custom degree function like:
def mydegree(p,v):
return p.degree(v.variable()) if v in p.variables() else 0Sat, 19 Nov 2022 19:37:59 +0100https://ask.sagemath.org/question/64903/return-degree-of-polynomial-in-infinitepolynomialring/?comment=64926#post-id-64926Comment by Thrash for <p>A variable in <code>InfinitePolynomialRing</code> object does not exist until you explicitly introduce it. E.g., defining</p>
<pre><code>p = a[1]*a[2]^2 + 0*a[3]
</code></pre>
<p>makes the error go away.</p>
<p>See also <a href="https://ask.sagemath.org/question/53319/">https://ask.sagemath.org/question/53319/</a></p>
https://ask.sagemath.org/question/64903/return-degree-of-polynomial-in-infinitepolynomialring/?comment=64919#post-id-64919I find it cumbersome, also because in my case, I generate polynomials in arbitrarily many variables that I pass to other (self-defined) functions where I have to extract the degree relative to a specific variable `a[d]`, where `d` is automatically calculated and can be arbitrarily large, so in general I don't know until where I would have to add zeros. I wonder if there is an elegant solution.Sat, 19 Nov 2022 16:58:42 +0100https://ask.sagemath.org/question/64903/return-degree-of-polynomial-in-infinitepolynomialring/?comment=64919#post-id-64919