# 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?

edit retag close merge delete

Sort by ยป oldest newest most voted

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.

more

A 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.

more

I 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.

( 2022-11-19 16:58:42 +0200 )edit

Unfortunately, implementation of InfinitePolynomialRingappears 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 0

( 2022-11-19 19:37:59 +0200 )edit

It 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.

( 2022-11-19 22:54:05 +0200 )edit

However, 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])

( 2022-11-19 23:11:17 +0200 )edit

( 2022-11-20 02:22:00 +0200 )edit