Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

coerce pari type or string to rational function workaround?

I am getting some rational functions from pari/gp and want to work with them in Sage. I can't figure out how to do the coercion. I tried coercing them directly, and even via a string.

Here's what I've tried:

Make a pari type rational function:

sage: test = gp.simplify('(x+y)/y')

This is the field I want to put it in:

sage: R.<x,y> = PolynomialRing(QQ)
sage: S = R.fraction_field()

coercing directly doesn't work:

sage: S(test)
Traceback (most recent call last):
...
TypeError: unable to convert 1/y*x + 1 to a rational

So I tried making a string out of it:

sage: str(test)
'1/y*x + 1'

Of course, sage would accept that string if I typed it in directly:

sage: S(1/y*x + 1)
(x + y)/y

But it won't accept it as a string:

sage: S(str(test))
Traceback (most recent call last):
...
TypeError: no canonical coercion from Fraction Field of Multivariate Polynomial Ring in x, y over Rational Field to Rational Field
sage: S('1/y*x + 1')
Traceback (most recent call last):
...
TypeError: no canonical coercion from Fraction Field of Multivariate Polynomial Ring in x, y over Rational Field to Rational Field

All of this works fine for polynomials (just remove the denominator of y from the example above). I think these are bugs and I will report them as such, if you agree. But in the meantime, does anyone have a workaround I can use?

coerce pari type or string to rational function workaround?

I am getting some rational functions from pari/gp and want to work with them in Sage. I can't figure out how to do the coercion. I tried coercing them directly, and even via a string.

Here's what I've tried:

Make a pari type rational function:

sage: test = gp.simplify('(x+y)/y')
gp.simplify((x+y)/y)

This is the field I want to put it in:

sage: R.<x,y> = PolynomialRing(QQ)
sage: S = R.fraction_field()

coercing directly doesn't work:

sage: S(test)
Traceback (most recent call last):
...
TypeError: unable to convert 1/y*x + 1 to a rational

So I tried making a string out of it:

sage: str(test)
'1/y*x + 1'

Of course, sage would accept that string if I typed it in directly:

sage: S(1/y*x + 1)
(x + y)/y

But it won't accept it as a string:

sage: S(str(test))
Traceback (most recent call last):
...
TypeError: no canonical coercion from Fraction Field of Multivariate Polynomial Ring in x, y over Rational Field to Rational Field
sage: S('1/y*x + 1')
Traceback (most recent call last):
...
TypeError: no canonical coercion from Fraction Field of Multivariate Polynomial Ring in x, y over Rational Field to Rational Field

All of this works fine for polynomials (just remove the denominator of y from the example above). I think these are bugs and I will report them as such, if you agree. But in the meantime, does anyone have a workaround I can use?