# recognize a rational function is a polynomial

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

Sort by » oldest newest most voted

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

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)


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