Ask Your Question
0

Return degree of polynomial in InfinitePolynomialRing

asked 2022-11-19 01:16:33 +0100

Thrash gravatar image

updated 2022-11-19 01:36:38 +0100

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 flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2022-11-24 18:16:51 +0100

Thrash gravatar image

updated 2022-11-24 18:18:24 +0100

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.

edit flag offensive delete link more
0

answered 2022-11-19 02:57:05 +0100

Max Alekseyev gravatar image

updated 2022-11-19 03:06:27 +0100

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.

See also https://ask.sagemath.org/question/53319/

edit flag offensive delete link more

Comments

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.

Thrash gravatar imageThrash ( 2022-11-19 16:58:42 +0100 )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
Max Alekseyev gravatar imageMax Alekseyev ( 2022-11-19 19:37:59 +0100 )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.

Thrash gravatar imageThrash ( 2022-11-19 22:54:05 +0100 )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])
Thrash gravatar imageThrash ( 2022-11-19 23:11:17 +0100 )edit

Feel free to add an answer to your own question for benefits of other users.

Max Alekseyev gravatar imageMax Alekseyev ( 2022-11-20 02:22:00 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2022-11-19 01:16:33 +0100

Seen: 177 times

Last updated: Nov 24 '22