ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 24 Apr 2021 22:27:45 +0200Convert element from fraction field of polynomial ring to number fieldhttps://ask.sagemath.org/question/56762/convert-element-from-fraction-field-of-polynomial-ring-to-number-field/Consider the following
F.<u> = NumberField(x^2-3)
R.<y> = PolynomialRing(F)
Q = R.fraction_field()
Then, `F(Q(2*u))` yields an error since
> TypeError: unable to convert 2*u to
> Number Field in u with defining
> polynomial x^2 - 3
Of course one could expect that this conversion should be not a problem. Doing the same over the base field `QQ` instead of a number field works as expected.
This problem can be fixed (in this case) by converting first to the polynomial ring and then to the number field, i.e., `F(R(Q(2*u)))` works fine. However, in practice, if we want to convert some `a` (where we know it "should" be in `F` but it might technically not) to `F`, it is very unpractical to check first whether `a` belongs in some certain ring and then convert it by going via the polynomial ring.
Is there a good built in way to do this? So we are given some `a` (which might be already in `F` or in some construction built upon `F`) and want to have `a` in `F`.Thu, 22 Apr 2021 10:06:15 +0200https://ask.sagemath.org/question/56762/convert-element-from-fraction-field-of-polynomial-ring-to-number-field/Comment by vdelecroix for <p>Consider the following</p>
<pre><code>F.<u> = NumberField(x^2-3)
R.<y> = PolynomialRing(F)
Q = R.fraction_field()
</code></pre>
<p>Then, <code>F(Q(2*u))</code> yields an error since </p>
<blockquote>
<p>TypeError: unable to convert 2*u to
Number Field in u with defining
polynomial x^2 - 3</p>
</blockquote>
<p>Of course one could expect that this conversion should be not a problem. Doing the same over the base field <code>QQ</code> instead of a number field works as expected.</p>
<p>This problem can be fixed (in this case) by converting first to the polynomial ring and then to the number field, i.e., <code>F(R(Q(2*u)))</code> works fine. However, in practice, if we want to convert some <code>a</code> (where we know it "should" be in <code>F</code> but it might technically not) to <code>F</code>, it is very unpractical to check first whether <code>a</code> belongs in some certain ring and then convert it by going via the polynomial ring.</p>
<p>Is there a good built in way to do this? So we are given some <code>a</code> (which might be already in <code>F</code> or in some construction built upon <code>F</code>) and want to have <code>a</code> in <code>F</code>.</p>
https://ask.sagemath.org/question/56762/convert-element-from-fraction-field-of-polynomial-ring-to-number-field/?comment=56767#post-id-56767Instead of going via `_convert_non_number_field_element` I would rather stick in a converter on the fraction field element following https://trac.sagemath.org/ticket/10147. See the fix I propose in https://trac.sagemath.org/ticket/31716.Thu, 22 Apr 2021 15:22:09 +0200https://ask.sagemath.org/question/56762/convert-element-from-fraction-field-of-polynomial-ring-to-number-field/?comment=56767#post-id-56767Comment by rburing for <p>Consider the following</p>
<pre><code>F.<u> = NumberField(x^2-3)
R.<y> = PolynomialRing(F)
Q = R.fraction_field()
</code></pre>
<p>Then, <code>F(Q(2*u))</code> yields an error since </p>
<blockquote>
<p>TypeError: unable to convert 2*u to
Number Field in u with defining
polynomial x^2 - 3</p>
</blockquote>
<p>Of course one could expect that this conversion should be not a problem. Doing the same over the base field <code>QQ</code> instead of a number field works as expected.</p>
<p>This problem can be fixed (in this case) by converting first to the polynomial ring and then to the number field, i.e., <code>F(R(Q(2*u)))</code> works fine. However, in practice, if we want to convert some <code>a</code> (where we know it "should" be in <code>F</code> but it might technically not) to <code>F</code>, it is very unpractical to check first whether <code>a</code> belongs in some certain ring and then convert it by going via the polynomial ring.</p>
<p>Is there a good built in way to do this? So we are given some <code>a</code> (which might be already in <code>F</code> or in some construction built upon <code>F</code>) and want to have <code>a</code> in <code>F</code>.</p>
https://ask.sagemath.org/question/56762/convert-element-from-fraction-field-of-polynomial-ring-to-number-field/?comment=56763#post-id-56763Good question. You may want to open a [trac ticket](https://trac.sagemath.org/) for this. It should be a matter of adding an extra case to the [_convert_non_number_field_element](https://github.com/sagemath/sage/blob/de32db6991a5293f037bb35d12890152a8ab99e6/src/sage/rings/number_field/number_field.py#L1797) method of NumberField.Thu, 22 Apr 2021 11:48:36 +0200https://ask.sagemath.org/question/56762/convert-element-from-fraction-field-of-polynomial-ring-to-number-field/?comment=56763#post-id-56763Answer by vdelecroix for <p>Consider the following</p>
<pre><code>F.<u> = NumberField(x^2-3)
R.<y> = PolynomialRing(F)
Q = R.fraction_field()
</code></pre>
<p>Then, <code>F(Q(2*u))</code> yields an error since </p>
<blockquote>
<p>TypeError: unable to convert 2*u to
Number Field in u with defining
polynomial x^2 - 3</p>
</blockquote>
<p>Of course one could expect that this conversion should be not a problem. Doing the same over the base field <code>QQ</code> instead of a number field works as expected.</p>
<p>This problem can be fixed (in this case) by converting first to the polynomial ring and then to the number field, i.e., <code>F(R(Q(2*u)))</code> works fine. However, in practice, if we want to convert some <code>a</code> (where we know it "should" be in <code>F</code> but it might technically not) to <code>F</code>, it is very unpractical to check first whether <code>a</code> belongs in some certain ring and then convert it by going via the polynomial ring.</p>
<p>Is there a good built in way to do this? So we are given some <code>a</code> (which might be already in <code>F</code> or in some construction built upon <code>F</code>) and want to have <code>a</code> in <code>F</code>.</p>
https://ask.sagemath.org/question/56762/convert-element-from-fraction-field-of-polynomial-ring-to-number-field/?answer=56769#post-id-56769This is fixed in https://trac.sagemath.org/ticket/31716
In the meantime, I would convert `elt` via `F(elt.numerator()) / F(elt.denominator())`. With your example
sage: elt = Q(u)
sage: F(elt.numerator()) / F(elt.denominator())
uThu, 22 Apr 2021 16:20:50 +0200https://ask.sagemath.org/question/56762/convert-element-from-fraction-field-of-polynomial-ring-to-number-field/?answer=56769#post-id-56769Comment by vdelecroix for <p>This is fixed in <a href="https://trac.sagemath.org/ticket/31716">https://trac.sagemath.org/ticket/31716</a></p>
<p>In the meantime, I would convert <code>elt</code> via <code>F(elt.numerator()) / F(elt.denominator())</code>. With your example</p>
<pre><code>sage: elt = Q(u)
sage: F(elt.numerator()) / F(elt.denominator())
u
</code></pre>
https://ask.sagemath.org/question/56762/convert-element-from-fraction-field-of-polynomial-ring-to-number-field/?comment=56799#post-id-56799And also thank you for the report.Sat, 24 Apr 2021 22:27:45 +0200https://ask.sagemath.org/question/56762/convert-element-from-fraction-field-of-polynomial-ring-to-number-field/?comment=56799#post-id-56799Comment by philipp7 for <p>This is fixed in <a href="https://trac.sagemath.org/ticket/31716">https://trac.sagemath.org/ticket/31716</a></p>
<p>In the meantime, I would convert <code>elt</code> via <code>F(elt.numerator()) / F(elt.denominator())</code>. With your example</p>
<pre><code>sage: elt = Q(u)
sage: F(elt.numerator()) / F(elt.denominator())
u
</code></pre>
https://ask.sagemath.org/question/56762/convert-element-from-fraction-field-of-polynomial-ring-to-number-field/?comment=56786#post-id-56786Thank you very much to everyone! I'm glad this was a quick fix in Sage!Fri, 23 Apr 2021 21:02:37 +0200https://ask.sagemath.org/question/56762/convert-element-from-fraction-field-of-polynomial-ring-to-number-field/?comment=56786#post-id-56786Answer by slelievre for <p>Consider the following</p>
<pre><code>F.<u> = NumberField(x^2-3)
R.<y> = PolynomialRing(F)
Q = R.fraction_field()
</code></pre>
<p>Then, <code>F(Q(2*u))</code> yields an error since </p>
<blockquote>
<p>TypeError: unable to convert 2*u to
Number Field in u with defining
polynomial x^2 - 3</p>
</blockquote>
<p>Of course one could expect that this conversion should be not a problem. Doing the same over the base field <code>QQ</code> instead of a number field works as expected.</p>
<p>This problem can be fixed (in this case) by converting first to the polynomial ring and then to the number field, i.e., <code>F(R(Q(2*u)))</code> works fine. However, in practice, if we want to convert some <code>a</code> (where we know it "should" be in <code>F</code> but it might technically not) to <code>F</code>, it is very unpractical to check first whether <code>a</code> belongs in some certain ring and then convert it by going via the polynomial ring.</p>
<p>Is there a good built in way to do this? So we are given some <code>a</code> (which might be already in <code>F</code> or in some construction built upon <code>F</code>) and want to have <code>a</code> in <code>F</code>.</p>
https://ask.sagemath.org/question/56762/convert-element-from-fraction-field-of-polynomial-ring-to-number-field/?answer=56764#post-id-56764As a workaround, evaluate a rational function at zero
to get its constant term as a number field element.
Below some musings around the question, including
the workaround already suggested in the question.
Define the number field, the polynomial ring, and the field of rational functions:
sage: x = polygen(ZZ)
sage: F.<u> = NumberField(x^2 - 3)
sage: R.<y> = PolynomialRing(F)
sage: Q = R.fraction_field()
A constant rational function:
sage: a = Q(2*u)
sage: a
2*u
Membership tests, with one surprise:
sage: a in Q, a in R, a in F
(True, True, False)
Forcing `a` into `F` does not work:
sage: F(a)
Traceback (most recent call last)
...
TypeError: unable to convert 2*u to Number Field in u with defining polynomial x^2 - 3
Going through the polynomial ring:
sage: b = R(a)
sage: b
2*u
sage: b in Q, b in R, b in F
(True, True, True)
Evaluating at zero to get the constant term as a base field element:
sage: c = a(0)
sage: c
2*u
sage: c in Q, c in R, c in F
(True, True, True)
Thu, 22 Apr 2021 12:21:50 +0200https://ask.sagemath.org/question/56762/convert-element-from-fraction-field-of-polynomial-ring-to-number-field/?answer=56764#post-id-56764Comment by vdelecroix for <p>As a workaround, evaluate a rational function at zero
to get its constant term as a number field element.</p>
<p>Below some musings around the question, including
the workaround already suggested in the question.</p>
<p>Define the number field, the polynomial ring, and the field of rational functions:</p>
<pre><code>sage: x = polygen(ZZ)
sage: F.<u> = NumberField(x^2 - 3)
sage: R.<y> = PolynomialRing(F)
sage: Q = R.fraction_field()
</code></pre>
<p>A constant rational function:</p>
<pre><code>sage: a = Q(2*u)
sage: a
2*u
</code></pre>
<p>Membership tests, with one surprise:</p>
<pre><code>sage: a in Q, a in R, a in F
(True, True, False)
</code></pre>
<p>Forcing <code>a</code> into <code>F</code> does not work:</p>
<pre><code>sage: F(a)
Traceback (most recent call last)
...
TypeError: unable to convert 2*u to Number Field in u with defining polynomial x^2 - 3
</code></pre>
<p>Going through the polynomial ring:</p>
<pre><code>sage: b = R(a)
sage: b
2*u
sage: b in Q, b in R, b in F
(True, True, True)
</code></pre>
<p>Evaluating at zero to get the constant term as a base field element:</p>
<pre><code>sage: c = a(0)
sage: c
2*u
sage: c in Q, c in R, c in F
(True, True, True)
</code></pre>
https://ask.sagemath.org/question/56762/convert-element-from-fraction-field-of-polynomial-ring-to-number-field/?comment=56781#post-id-56781https://trac.sagemath.org/ticket/31716 changes the surprise to
sage: a in Q, a in R, a in F
(True, True, True)Fri, 23 Apr 2021 09:33:15 +0200https://ask.sagemath.org/question/56762/convert-element-from-fraction-field-of-polynomial-ring-to-number-field/?comment=56781#post-id-56781