Ask Your Question

recognize a rational function is a polynomial

asked 2022-02-25 17:28:49 +0200

rue82 gravatar image

updated 2022-02-25 18:42:08 +0200

slelievre gravatar image

Is it possible to make sage recognize that a certain rational function is a Laurent polynomial, and treat it as such?

A simple example:

sage: R = LaurentPolynomialRing(ZZ, ['q1'])
sage: R.inject_variables(verbose=False)
sage: f = (1/(1-q1) + 1/(1-q1^-1))
sage: parent(f)
Fraction Field of Univariate Polynomial Ring in q1 over Integer Ring

Here, f lives correctly in the fraction field. However, it is in particular a polynomial, to which I'd like to apply the polynomial methods, e.g. get its monomial list.

Is this possible to achieve?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2022-02-25 18:44:52 +0200

slelievre gravatar image

updated 2022-02-27 14:52:37 +0200

Good thing you gave the Laurent polynomial ring a name.

That makes it easy to call it on an element of the fraction field.

Here is an example using the element f in the question.

sage: ff = R(f)
sage: parent(f)
Univariate Laurent Polynomial Ring in q1 over Integer Ring

In more complicated cases, it helps to use the numerator and denominator.

sage: g = (1-q1) * (1-q2) * (q2/(1-q1)+q2^-1/(1-q1^-1))
sage: g
(q2^3 - q1*q2 - q2^2 + q1)/(-q2)

sage: gg = R(g)
Traceback (most recent call last)
TypeError: fraction must have unit denominator

sage: gg = R(g.numerator()) / R(g.denominator())
sage: gg
-q2^2 + q1 + q2 - q1*q2^-1
sage: parent(gg)
Multivariate Laurent Polynomial Ring in q1, q2 over Integer Ring
edit flag offensive delete link more


Thanks, that's also what I thought, but why is it not working e.g. here?

R.<q1,q2> = LaurentPolynomialRing(ZZ,2)
def test():
    x = (1-q1) * (1-q2) * (q2/(1-q1)+q2^-1/(1-q1^-1))
    return R(x)
rue82 gravatar imagerue82 ( 2022-02-26 15:51:53 +0200 )edit

the error it gives is: fraction must have unit denominator.

rue82 gravatar imagerue82 ( 2022-02-26 15:53:12 +0200 )edit

Thank you!

rue82 gravatar imagerue82 ( 2022-02-26 22:14:14 +0200 )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


Asked: 2022-02-25 17:28:49 +0200

Seen: 34 times

Last updated: Feb 27