ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 22 May 2011 18:17:00 -0500coerce pari type or string to rational function workaround?http://ask.sagemath.org/question/8114/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?Sun, 22 May 2011 09:11:39 -0500http://ask.sagemath.org/question/8114/coerce-pari-type-or-string-to-rational-function-workaround/Comment by Kate Stange for <p>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.</p>
<p>Here's what I've tried:</p>
<p>Make a pari type rational function:</p>
<pre><code>sage: test = gp.simplify((x+y)/y)
</code></pre>
<p>This is the field I want to put it in:</p>
<pre><code>sage: R.<x,y> = PolynomialRing(QQ)
sage: S = R.fraction_field()
</code></pre>
<p>coercing directly doesn't work:</p>
<pre><code>sage: S(test)
Traceback (most recent call last):
...
TypeError: unable to convert 1/y*x + 1 to a rational
</code></pre>
<p>So I tried making a string out of it:</p>
<pre><code>sage: str(test)
'1/y*x + 1'
</code></pre>
<p>Of course, sage would accept that string if I typed it in directly:</p>
<pre><code>sage: S(1/y*x + 1)
(x + y)/y
</code></pre>
<p>But it won't accept it as a string:</p>
<pre><code>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
</code></pre>
<p>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?</p>
http://ask.sagemath.org/question/8114/coerce-pari-type-or-string-to-rational-function-workaround/?comment=21686#post-id-21686I've put in a trac ticket at http://trac.sagemath.org/sage_trac/ticket/11368Sun, 22 May 2011 18:17:00 -0500http://ask.sagemath.org/question/8114/coerce-pari-type-or-string-to-rational-function-workaround/?comment=21686#post-id-21686Comment by Kate Stange for <p>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.</p>
<p>Here's what I've tried:</p>
<p>Make a pari type rational function:</p>
<pre><code>sage: test = gp.simplify((x+y)/y)
</code></pre>
<p>This is the field I want to put it in:</p>
<pre><code>sage: R.<x,y> = PolynomialRing(QQ)
sage: S = R.fraction_field()
</code></pre>
<p>coercing directly doesn't work:</p>
<pre><code>sage: S(test)
Traceback (most recent call last):
...
TypeError: unable to convert 1/y*x + 1 to a rational
</code></pre>
<p>So I tried making a string out of it:</p>
<pre><code>sage: str(test)
'1/y*x + 1'
</code></pre>
<p>Of course, sage would accept that string if I typed it in directly:</p>
<pre><code>sage: S(1/y*x + 1)
(x + y)/y
</code></pre>
<p>But it won't accept it as a string:</p>
<pre><code>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
</code></pre>
<p>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?</p>
http://ask.sagemath.org/question/8114/coerce-pari-type-or-string-to-rational-function-workaround/?comment=21687#post-id-21687Thanks for the answers below. I've got my computation working (yay!), but I'm also going to find out if the bug(s) are known and report them if needed. I suspect this must be a known issue, so I'll add a comment when I've looked into it on the tracking system. Any comments or advice on that is welcome.Sun, 22 May 2011 18:04:29 -0500http://ask.sagemath.org/question/8114/coerce-pari-type-or-string-to-rational-function-workaround/?comment=21687#post-id-21687Answer by DSM for <p>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.</p>
<p>Here's what I've tried:</p>
<p>Make a pari type rational function:</p>
<pre><code>sage: test = gp.simplify((x+y)/y)
</code></pre>
<p>This is the field I want to put it in:</p>
<pre><code>sage: R.<x,y> = PolynomialRing(QQ)
sage: S = R.fraction_field()
</code></pre>
<p>coercing directly doesn't work:</p>
<pre><code>sage: S(test)
Traceback (most recent call last):
...
TypeError: unable to convert 1/y*x + 1 to a rational
</code></pre>
<p>So I tried making a string out of it:</p>
<pre><code>sage: str(test)
'1/y*x + 1'
</code></pre>
<p>Of course, sage would accept that string if I typed it in directly:</p>
<pre><code>sage: S(1/y*x + 1)
(x + y)/y
</code></pre>
<p>But it won't accept it as a string:</p>
<pre><code>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
</code></pre>
<p>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?</p>
http://ask.sagemath.org/question/8114/coerce-pari-type-or-string-to-rational-function-workaround/?answer=12373#post-id-12373Here's how I'd get around the problem if I needed it to work Right Now(tm):
sage: var("x y")
(x, y)
sage: t = gp.simplify((x+y)/y)
sage:
sage: R.<x,y> = PolynomialRing(QQ)
sage: S = R.fraction_field()
sage:
sage: t2 = S(sage_eval(repr(t),locals=locals()))
sage: t2
(x + y)/y
sage: parent(t2)
Fraction Field of Multivariate Polynomial Ring in x, y over Rational Field
sage: parent(t2) is S
True
Sun, 22 May 2011 14:21:44 -0500http://ask.sagemath.org/question/8114/coerce-pari-type-or-string-to-rational-function-workaround/?answer=12373#post-id-12373Comment by Kate Stange for <p>Here's how I'd get around the problem if I needed it to work Right Now(tm):</p>
<pre><code>sage: var("x y")
(x, y)
sage: t = gp.simplify((x+y)/y)
sage:
sage: R.<x,y> = PolynomialRing(QQ)
sage: S = R.fraction_field()
sage:
sage: t2 = S(sage_eval(repr(t),locals=locals()))
sage: t2
(x + y)/y
sage: parent(t2)
Fraction Field of Multivariate Polynomial Ring in x, y over Rational Field
sage: parent(t2) is S
True
</code></pre>
http://ask.sagemath.org/question/8114/coerce-pari-type-or-string-to-rational-function-workaround/?comment=21688#post-id-21688Thanks! Right Now(tm) is exactly what I needed, and this did the trick.Sun, 22 May 2011 18:01:15 -0500http://ask.sagemath.org/question/8114/coerce-pari-type-or-string-to-rational-function-workaround/?comment=21688#post-id-21688Answer by niles for <p>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.</p>
<p>Here's what I've tried:</p>
<p>Make a pari type rational function:</p>
<pre><code>sage: test = gp.simplify((x+y)/y)
</code></pre>
<p>This is the field I want to put it in:</p>
<pre><code>sage: R.<x,y> = PolynomialRing(QQ)
sage: S = R.fraction_field()
</code></pre>
<p>coercing directly doesn't work:</p>
<pre><code>sage: S(test)
Traceback (most recent call last):
...
TypeError: unable to convert 1/y*x + 1 to a rational
</code></pre>
<p>So I tried making a string out of it:</p>
<pre><code>sage: str(test)
'1/y*x + 1'
</code></pre>
<p>Of course, sage would accept that string if I typed it in directly:</p>
<pre><code>sage: S(1/y*x + 1)
(x + y)/y
</code></pre>
<p>But it won't accept it as a string:</p>
<pre><code>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
</code></pre>
<p>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?</p>
http://ask.sagemath.org/question/8114/coerce-pari-type-or-string-to-rational-function-workaround/?answer=12372#post-id-12372I think you're right that there are bugs here, but I don't quite know what they are either.
In any case, I think the Symbolic Ring can serve as a useful intermediary here. With your example, I did
sage: test_sr = SR(test)
You can see that it's still the right thing, but as a symbolic Sage object:
sage: test_sr
1/y*x + 1
sage: test_sr.parent()
Symbolic Ring
Unfortunately, converting straight to `S` still doesn't work:
sage: S(test_sr)
...
TypeError:
However, the method `coeffs` returns coefficients for powers of the given variable, and individual coefficients can be coerced to `S`:
sage: test_sr.coeffs(x)
[[1, 0], [1/y, 1]]
sage: test_sr.coeffs(y)
[[x, -1], [1, 0]]
sage: sum(S(c[0])*x^c[1] for c in test_sr.coeffs(x))
(x + y)/y
sage: sum(S(c[0])*y^c[1] for c in test_sr.coeffs(y))
(x + y)/y
Of course it's probably not helpful if `S` unsimplifies something that you specifically wanted simplified, but bear in mind that the rational function field will always try to give you elements which are ratios of polynomials. Even if that's not what you want, coercing the individual coefficients will of course work.
Sun, 22 May 2011 09:42:13 -0500http://ask.sagemath.org/question/8114/coerce-pari-type-or-string-to-rational-function-workaround/?answer=12372#post-id-12372Comment by Kate Stange for <p>I think you're right that there are bugs here, but I don't quite know what they are either.</p>
<p>In any case, I think the Symbolic Ring can serve as a useful intermediary here. With your example, I did</p>
<pre><code>sage: test_sr = SR(test)
</code></pre>
<p>You can see that it's still the right thing, but as a symbolic Sage object:</p>
<pre><code>sage: test_sr
1/y*x + 1
sage: test_sr.parent()
Symbolic Ring
</code></pre>
<p>Unfortunately, converting straight to <code>S</code> still doesn't work:</p>
<pre><code>sage: S(test_sr)
...
TypeError:
</code></pre>
<p>However, the method <code>coeffs</code> returns coefficients for powers of the given variable, and individual coefficients can be coerced to <code>S</code>:</p>
<pre><code>sage: test_sr.coeffs(x)
[[1, 0], [1/y, 1]]
sage: test_sr.coeffs(y)
[[x, -1], [1, 0]]
sage: sum(S(c[0])*x^c[1] for c in test_sr.coeffs(x))
(x + y)/y
sage: sum(S(c[0])*y^c[1] for c in test_sr.coeffs(y))
(x + y)/y
</code></pre>
<p>Of course it's probably not helpful if <code>S</code> unsimplifies something that you specifically wanted simplified, but bear in mind that the rational function field will always try to give you elements which are ratios of polynomials. Even if that's not what you want, coercing the individual coefficients will of course work.</p>
http://ask.sagemath.org/question/8114/coerce-pari-type-or-string-to-rational-function-workaround/?comment=21689#post-id-21689Thanks! This works, but for some reason it is extremely slow, at least on my machine.Sun, 22 May 2011 18:00:25 -0500http://ask.sagemath.org/question/8114/coerce-pari-type-or-string-to-rational-function-workaround/?comment=21689#post-id-21689