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, 08 Oct 2016 02:02:33 +0200How to extend ring homomorphism to polynomial ring (or its fraction field)https://ask.sagemath.org/question/34947/how-to-extend-ring-homomorphism-to-polynomial-ring-or-its-fraction-field/I have a homomorphism from a number field `nf` to the field of algebraic numbers `QQbar`:
`nf, alpha, hom = QQbar(sqrt(2)).as_number_field_element()`
I now work in the polynomial ring `R` over `nf`:
`R.<x> = nf[]`
`f = x - alpha; f`
How do I get a homomorphism from `R` to the polynomial ring over `QQbar` extending `hom`?
For the moment, I can use
`f.map_coefficients(hom)`
Same question about the fraction field of `R`, e.g.,
`g = f/(x+1)`
Is there a more elegant way than calling
`g.numerator().map_coefficients(hom)/g.denominator().map_coefficients(hom)`
So basically, I'd like to extend my homomorphism `hom` to the polynomial ring and its field of fractions.Mon, 26 Sep 2016 14:15:18 +0200https://ask.sagemath.org/question/34947/how-to-extend-ring-homomorphism-to-polynomial-ring-or-its-fraction-field/Answer by nbruin for <p>I have a homomorphism from a number field <code>nf</code> to the field of algebraic numbers <code>QQbar</code>:</p>
<p><code>nf, alpha, hom = QQbar(sqrt(2)).as_number_field_element()</code></p>
<p>I now work in the polynomial ring <code>R</code> over <code>nf</code>:</p>
<p><code>R.<x> = nf[]</code></p>
<p><code>f = x - alpha; f</code></p>
<p>How do I get a homomorphism from <code>R</code> to the polynomial ring over <code>QQbar</code> extending <code>hom</code>?
For the moment, I can use</p>
<p><code>f.map_coefficients(hom)</code></p>
<p>Same question about the fraction field of <code>R</code>, e.g.,</p>
<p><code>g = f/(x+1)</code></p>
<p>Is there a more elegant way than calling</p>
<p><code>g.numerator().map_coefficients(hom)/g.denominator().map_coefficients(hom)</code></p>
<p>So basically, I'd like to extend my homomorphism <code>hom</code> to the polynomial ring and its field of fractions.</p>
https://ask.sagemath.org/question/34947/how-to-extend-ring-homomorphism-to-polynomial-ring-or-its-fraction-field/?answer=34954#post-id-34954There are some tools to wrap things up a bit. Note that "map_coefficients" might still do funny things with the codomain parent: for instance, in your case I think you end up with polynomials over the Algebraic Real Field rather than QQbar.
sage: from sage.structure.coerce_maps import CallableConvertMap
sage: H=CallableConvertMap(RR,QbarX,lambda self,f:QbarX(f.map_coefficients(hom)))
Making a homomorphism between the fields of fractions could be done similarly. I think the tools for this kind of stuff could be made more convenient and/or be documented in a more discoverable way.
In your particular case there might be a shortcut. It's not as general as good tools to naturally extend ring homomorphisms to modules and algebras over these bases. Given that your number field comes from QQbar, it should perhaps be constructed with an embedding into it as well. We can hack our way around that a little bit by registering the embedding ourselves (since this modifies the coercion graph at a node that might already have been used, it could lead to inconsistencies, so we have to take some extra steps to neutralize safety precautions. Furthermore, it modifies the behaviour of a possibly globally unique parent, so it's really a hack at this point. Embeddings should be supplied upon construction, not added later):
sage: nf, alpha, hom = QQbar(sqrt(2)).as_number_field_element()
sage: nf._unset_coercions_used()
sage: nf.register_embedding(hom)
sage: nfX.<x>=nf[]
sage: QbarX.<x>=QQbar[]
sage: 1/nfX.0 - QbarX.0
(-x^2 + 1)/x
sage: parent(1/nfX.0 - QbarX.0)
Fraction Field of Univariate Polynomial Ring in x over Algebraic Field
sage: QbarX.coerce_map_from(nfX)
Ring morphism:
From: Univariate Polynomial Ring in x over Number Field in a with defining polynomial y^2 - 2
To: Univariate Polynomial Ring in x over Algebraic Field
Defn: Induced from base ring by
Composite map:
From: Number Field in a with defining polynomial y^2 - 2
To: Algebraic Field
Defn: Ring morphism:
From: Number Field in a with defining polynomial y^2 - 2
To: Algebraic Real Field
Defn: a |--> 1.414213562373095?
then
Conversion map:
From: Algebraic Real Field
To: Algebraic Field
Sage does have quite good tools available for discovering/constructing these kinds of induced maps, but unfortunately they are only available for registered coercion maps. It would be great if the same tools would also be usable with an explicitly given, non-standard set of homomorphisms for the construction of new maps.Mon, 26 Sep 2016 18:18:01 +0200https://ask.sagemath.org/question/34947/how-to-extend-ring-homomorphism-to-polynomial-ring-or-its-fraction-field/?answer=34954#post-id-34954Comment by Clemens Heuberger for <p>There are some tools to wrap things up a bit. Note that "map_coefficients" might still do funny things with the codomain parent: for instance, in your case I think you end up with polynomials over the Algebraic Real Field rather than QQbar.</p>
<pre><code> sage: from sage.structure.coerce_maps import CallableConvertMap
sage: H=CallableConvertMap(RR,QbarX,lambda self,f:QbarX(f.map_coefficients(hom)))
</code></pre>
<p>Making a homomorphism between the fields of fractions could be done similarly. I think the tools for this kind of stuff could be made more convenient and/or be documented in a more discoverable way.</p>
<p>In your particular case there might be a shortcut. It's not as general as good tools to naturally extend ring homomorphisms to modules and algebras over these bases. Given that your number field comes from QQbar, it should perhaps be constructed with an embedding into it as well. We can hack our way around that a little bit by registering the embedding ourselves (since this modifies the coercion graph at a node that might already have been used, it could lead to inconsistencies, so we have to take some extra steps to neutralize safety precautions. Furthermore, it modifies the behaviour of a possibly globally unique parent, so it's really a hack at this point. Embeddings should be supplied upon construction, not added later):</p>
<pre><code>sage: nf, alpha, hom = QQbar(sqrt(2)).as_number_field_element()
sage: nf._unset_coercions_used()
sage: nf.register_embedding(hom)
sage: nfX.<x>=nf[]
sage: QbarX.<x>=QQbar[]
sage: 1/nfX.0 - QbarX.0
(-x^2 + 1)/x
sage: parent(1/nfX.0 - QbarX.0)
Fraction Field of Univariate Polynomial Ring in x over Algebraic Field
sage: QbarX.coerce_map_from(nfX)
Ring morphism:
From: Univariate Polynomial Ring in x over Number Field in a with defining polynomial y^2 - 2
To: Univariate Polynomial Ring in x over Algebraic Field
Defn: Induced from base ring by
Composite map:
From: Number Field in a with defining polynomial y^2 - 2
To: Algebraic Field
Defn: Ring morphism:
From: Number Field in a with defining polynomial y^2 - 2
To: Algebraic Real Field
Defn: a |--> 1.414213562373095?
then
Conversion map:
From: Algebraic Real Field
To: Algebraic Field
</code></pre>
<p>Sage does have quite good tools available for discovering/constructing these kinds of induced maps, but unfortunately they are only available for registered coercion maps. It would be great if the same tools would also be usable with an explicitly given, non-standard set of homomorphisms for the construction of new maps.</p>
https://ask.sagemath.org/question/34947/how-to-extend-ring-homomorphism-to-polynomial-ring-or-its-fraction-field/?comment=34965#post-id-34965Thank you, the shortcut was exactly what I need.Tue, 27 Sep 2016 05:28:23 +0200https://ask.sagemath.org/question/34947/how-to-extend-ring-homomorphism-to-polynomial-ring-or-its-fraction-field/?comment=34965#post-id-34965Comment by Clemens Heuberger for <p>There are some tools to wrap things up a bit. Note that "map_coefficients" might still do funny things with the codomain parent: for instance, in your case I think you end up with polynomials over the Algebraic Real Field rather than QQbar.</p>
<pre><code> sage: from sage.structure.coerce_maps import CallableConvertMap
sage: H=CallableConvertMap(RR,QbarX,lambda self,f:QbarX(f.map_coefficients(hom)))
</code></pre>
<p>Making a homomorphism between the fields of fractions could be done similarly. I think the tools for this kind of stuff could be made more convenient and/or be documented in a more discoverable way.</p>
<p>In your particular case there might be a shortcut. It's not as general as good tools to naturally extend ring homomorphisms to modules and algebras over these bases. Given that your number field comes from QQbar, it should perhaps be constructed with an embedding into it as well. We can hack our way around that a little bit by registering the embedding ourselves (since this modifies the coercion graph at a node that might already have been used, it could lead to inconsistencies, so we have to take some extra steps to neutralize safety precautions. Furthermore, it modifies the behaviour of a possibly globally unique parent, so it's really a hack at this point. Embeddings should be supplied upon construction, not added later):</p>
<pre><code>sage: nf, alpha, hom = QQbar(sqrt(2)).as_number_field_element()
sage: nf._unset_coercions_used()
sage: nf.register_embedding(hom)
sage: nfX.<x>=nf[]
sage: QbarX.<x>=QQbar[]
sage: 1/nfX.0 - QbarX.0
(-x^2 + 1)/x
sage: parent(1/nfX.0 - QbarX.0)
Fraction Field of Univariate Polynomial Ring in x over Algebraic Field
sage: QbarX.coerce_map_from(nfX)
Ring morphism:
From: Univariate Polynomial Ring in x over Number Field in a with defining polynomial y^2 - 2
To: Univariate Polynomial Ring in x over Algebraic Field
Defn: Induced from base ring by
Composite map:
From: Number Field in a with defining polynomial y^2 - 2
To: Algebraic Field
Defn: Ring morphism:
From: Number Field in a with defining polynomial y^2 - 2
To: Algebraic Real Field
Defn: a |--> 1.414213562373095?
then
Conversion map:
From: Algebraic Real Field
To: Algebraic Field
</code></pre>
<p>Sage does have quite good tools available for discovering/constructing these kinds of induced maps, but unfortunately they are only available for registered coercion maps. It would be great if the same tools would also be usable with an explicitly given, non-standard set of homomorphisms for the construction of new maps.</p>
https://ask.sagemath.org/question/34947/how-to-extend-ring-homomorphism-to-polynomial-ring-or-its-fraction-field/?comment=34966#post-id-34966It worked once, but then my code came accross another example and UniqueRepresentation gave me problems. So I ended up avoiding `as_number_field_element` alltogether and replacing it by
alpha_qqbar = QQbar(sqrt(2))
nf = NumberField(alpha_qqbar.minpoly(), 'alpha', embedding=alpha_qqbar)
hom = nf.coerce_embedding()
x1 = polygen(nf, 'x')
x2 = polygen(QQbar, 'x')
1/x1 - 1/x2Tue, 27 Sep 2016 08:24:43 +0200https://ask.sagemath.org/question/34947/how-to-extend-ring-homomorphism-to-polynomial-ring-or-its-fraction-field/?comment=34966#post-id-34966Comment by nbruin for <p>There are some tools to wrap things up a bit. Note that "map_coefficients" might still do funny things with the codomain parent: for instance, in your case I think you end up with polynomials over the Algebraic Real Field rather than QQbar.</p>
<pre><code> sage: from sage.structure.coerce_maps import CallableConvertMap
sage: H=CallableConvertMap(RR,QbarX,lambda self,f:QbarX(f.map_coefficients(hom)))
</code></pre>
<p>Making a homomorphism between the fields of fractions could be done similarly. I think the tools for this kind of stuff could be made more convenient and/or be documented in a more discoverable way.</p>
<p>In your particular case there might be a shortcut. It's not as general as good tools to naturally extend ring homomorphisms to modules and algebras over these bases. Given that your number field comes from QQbar, it should perhaps be constructed with an embedding into it as well. We can hack our way around that a little bit by registering the embedding ourselves (since this modifies the coercion graph at a node that might already have been used, it could lead to inconsistencies, so we have to take some extra steps to neutralize safety precautions. Furthermore, it modifies the behaviour of a possibly globally unique parent, so it's really a hack at this point. Embeddings should be supplied upon construction, not added later):</p>
<pre><code>sage: nf, alpha, hom = QQbar(sqrt(2)).as_number_field_element()
sage: nf._unset_coercions_used()
sage: nf.register_embedding(hom)
sage: nfX.<x>=nf[]
sage: QbarX.<x>=QQbar[]
sage: 1/nfX.0 - QbarX.0
(-x^2 + 1)/x
sage: parent(1/nfX.0 - QbarX.0)
Fraction Field of Univariate Polynomial Ring in x over Algebraic Field
sage: QbarX.coerce_map_from(nfX)
Ring morphism:
From: Univariate Polynomial Ring in x over Number Field in a with defining polynomial y^2 - 2
To: Univariate Polynomial Ring in x over Algebraic Field
Defn: Induced from base ring by
Composite map:
From: Number Field in a with defining polynomial y^2 - 2
To: Algebraic Field
Defn: Ring morphism:
From: Number Field in a with defining polynomial y^2 - 2
To: Algebraic Real Field
Defn: a |--> 1.414213562373095?
then
Conversion map:
From: Algebraic Real Field
To: Algebraic Field
</code></pre>
<p>Sage does have quite good tools available for discovering/constructing these kinds of induced maps, but unfortunately they are only available for registered coercion maps. It would be great if the same tools would also be usable with an explicitly given, non-standard set of homomorphisms for the construction of new maps.</p>
https://ask.sagemath.org/question/34947/how-to-extend-ring-homomorphism-to-polynomial-ring-or-its-fraction-field/?comment=34974#post-id-34974Good example for why we need a to be a little careful with the `UniqueRepresentation` thing. In this case, I think `as_number_field_element` can be patched to return the number field as you construct it. Also: the method is called `as_*_element`, so the first returned value should be the element, not the parent!Tue, 27 Sep 2016 16:17:56 +0200https://ask.sagemath.org/question/34947/how-to-extend-ring-homomorphism-to-polynomial-ring-or-its-fraction-field/?comment=34974#post-id-34974Comment by Clemens Heuberger for <p>There are some tools to wrap things up a bit. Note that "map_coefficients" might still do funny things with the codomain parent: for instance, in your case I think you end up with polynomials over the Algebraic Real Field rather than QQbar.</p>
<pre><code> sage: from sage.structure.coerce_maps import CallableConvertMap
sage: H=CallableConvertMap(RR,QbarX,lambda self,f:QbarX(f.map_coefficients(hom)))
</code></pre>
<p>Making a homomorphism between the fields of fractions could be done similarly. I think the tools for this kind of stuff could be made more convenient and/or be documented in a more discoverable way.</p>
<p>In your particular case there might be a shortcut. It's not as general as good tools to naturally extend ring homomorphisms to modules and algebras over these bases. Given that your number field comes from QQbar, it should perhaps be constructed with an embedding into it as well. We can hack our way around that a little bit by registering the embedding ourselves (since this modifies the coercion graph at a node that might already have been used, it could lead to inconsistencies, so we have to take some extra steps to neutralize safety precautions. Furthermore, it modifies the behaviour of a possibly globally unique parent, so it's really a hack at this point. Embeddings should be supplied upon construction, not added later):</p>
<pre><code>sage: nf, alpha, hom = QQbar(sqrt(2)).as_number_field_element()
sage: nf._unset_coercions_used()
sage: nf.register_embedding(hom)
sage: nfX.<x>=nf[]
sage: QbarX.<x>=QQbar[]
sage: 1/nfX.0 - QbarX.0
(-x^2 + 1)/x
sage: parent(1/nfX.0 - QbarX.0)
Fraction Field of Univariate Polynomial Ring in x over Algebraic Field
sage: QbarX.coerce_map_from(nfX)
Ring morphism:
From: Univariate Polynomial Ring in x over Number Field in a with defining polynomial y^2 - 2
To: Univariate Polynomial Ring in x over Algebraic Field
Defn: Induced from base ring by
Composite map:
From: Number Field in a with defining polynomial y^2 - 2
To: Algebraic Field
Defn: Ring morphism:
From: Number Field in a with defining polynomial y^2 - 2
To: Algebraic Real Field
Defn: a |--> 1.414213562373095?
then
Conversion map:
From: Algebraic Real Field
To: Algebraic Field
</code></pre>
<p>Sage does have quite good tools available for discovering/constructing these kinds of induced maps, but unfortunately they are only available for registered coercion maps. It would be great if the same tools would also be usable with an explicitly given, non-standard set of homomorphisms for the construction of new maps.</p>
https://ask.sagemath.org/question/34947/how-to-extend-ring-homomorphism-to-polynomial-ring-or-its-fraction-field/?comment=35041#post-id-35041I now have performance problems with my above solution:
R.<x> = AA[]
v = QQbar.polynomial_root(AA.common_polynomial(x^5 - 2), CIF(RIF(RR(0.35496731310463009), RR(0.35496731310463014)), RIF(RR(1.0924770557774537), RR(1.0924770557774539))))
rho = 1/60*v^4 + 1/30*v^3 + 1/60*v^2 + 1/30*v + 1/15
mp = rho.minpoly()
%time NumberField(mp, 'rho', embedding=rho)
CPU times: user 1min 5s, sys: 64 ms, total: 1min 5s
Wall time: 1min 5s
Number Field in rho with defining polynomial x^5 - 1/3*x^4 + 1/30*x^3 - 1/600*x^2 + 1/24000*x - 1/2400000
Any ideas?Mon, 03 Oct 2016 13:59:24 +0200https://ask.sagemath.org/question/34947/how-to-extend-ring-homomorphism-to-polynomial-ring-or-its-fraction-field/?comment=35041#post-id-35041Comment by nbruin for <p>There are some tools to wrap things up a bit. Note that "map_coefficients" might still do funny things with the codomain parent: for instance, in your case I think you end up with polynomials over the Algebraic Real Field rather than QQbar.</p>
<pre><code> sage: from sage.structure.coerce_maps import CallableConvertMap
sage: H=CallableConvertMap(RR,QbarX,lambda self,f:QbarX(f.map_coefficients(hom)))
</code></pre>
<p>Making a homomorphism between the fields of fractions could be done similarly. I think the tools for this kind of stuff could be made more convenient and/or be documented in a more discoverable way.</p>
<p>In your particular case there might be a shortcut. It's not as general as good tools to naturally extend ring homomorphisms to modules and algebras over these bases. Given that your number field comes from QQbar, it should perhaps be constructed with an embedding into it as well. We can hack our way around that a little bit by registering the embedding ourselves (since this modifies the coercion graph at a node that might already have been used, it could lead to inconsistencies, so we have to take some extra steps to neutralize safety precautions. Furthermore, it modifies the behaviour of a possibly globally unique parent, so it's really a hack at this point. Embeddings should be supplied upon construction, not added later):</p>
<pre><code>sage: nf, alpha, hom = QQbar(sqrt(2)).as_number_field_element()
sage: nf._unset_coercions_used()
sage: nf.register_embedding(hom)
sage: nfX.<x>=nf[]
sage: QbarX.<x>=QQbar[]
sage: 1/nfX.0 - QbarX.0
(-x^2 + 1)/x
sage: parent(1/nfX.0 - QbarX.0)
Fraction Field of Univariate Polynomial Ring in x over Algebraic Field
sage: QbarX.coerce_map_from(nfX)
Ring morphism:
From: Univariate Polynomial Ring in x over Number Field in a with defining polynomial y^2 - 2
To: Univariate Polynomial Ring in x over Algebraic Field
Defn: Induced from base ring by
Composite map:
From: Number Field in a with defining polynomial y^2 - 2
To: Algebraic Field
Defn: Ring morphism:
From: Number Field in a with defining polynomial y^2 - 2
To: Algebraic Real Field
Defn: a |--> 1.414213562373095?
then
Conversion map:
From: Algebraic Real Field
To: Algebraic Field
</code></pre>
<p>Sage does have quite good tools available for discovering/constructing these kinds of induced maps, but unfortunately they are only available for registered coercion maps. It would be great if the same tools would also be usable with an explicitly given, non-standard set of homomorphisms for the construction of new maps.</p>
https://ask.sagemath.org/question/34947/how-to-extend-ring-homomorphism-to-polynomial-ring-or-its-fraction-field/?comment=35088#post-id-35088I think QQbar is designed to have performance problems, so to a large extent this is a feature. Apparently, there are no attempts to simplify the algebraic data on roots, so your description of "rho" is just horrible. You could improve it by hand:
sage: rho0=[v[0] for v in mp.roots(QQbar) if v[0]==rho][0]
sage: %time NumberField(mp, 'rho', embedding=rho0)
CPU times: user 11.5 ms, sys: 1.06 ms, total: 12.6 ms
Wall time: 11.5 ms
Number Field in rho with defining polynomial x^5 - 1/3*x^4 + 1/30*x^3 - 1/600*x^2 + 1/24000*x - 1/2400000Sat, 08 Oct 2016 02:02:33 +0200https://ask.sagemath.org/question/34947/how-to-extend-ring-homomorphism-to-polynomial-ring-or-its-fraction-field/?comment=35088#post-id-35088