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.Tue, 19 Jun 2018 10:47:23 -0500Possible bug with identity morphismhttp://ask.sagemath.org/question/42625/possible-bug-with-identity-morphism/I have a number field `U` for which I consider its automorphisms through `Hom(U,U)`.
The identity `one=Hom(U,U).identity()` behaves weirdly under right multiplication:
U = CyclotomicField(3)
f = Hom(U,U)[1]
print f
print "--------------------"
one = Hom(U,U).identity()
print f*one
print "--------------------"
print one*f
print "--------------------"
print f*f
When I run this code, I expect `f` to be printed thrice, followed by the identity morphism.
However, while the first and third output do in fact both print `f`, the second prints
Composite map:
From: Cyclotomic Field of order 3 and degree 2
To: Cyclotomic Field of order 3 and degree 2
Defn: Identity endomorphism of Cyclotomic Field of order 3 and degree 2
then
Ring endomorphism of Cyclotomic Field of order 3 and degree 2
Defn: zeta3 |--> -zeta3 - 1
Is this a bug or is this this behaviour explained somewhere in the documentation?
Sun, 17 Jun 2018 05:47:03 -0500http://ask.sagemath.org/question/42625/possible-bug-with-identity-morphism/Answer by nbruin for <p>I have a number field <code>U</code> for which I consider its automorphisms through <code>Hom(U,U)</code>.
The identity <code>one=Hom(U,U).identity()</code> behaves weirdly under right multiplication:</p>
<pre><code>U = CyclotomicField(3)
f = Hom(U,U)[1]
print f
print "--------------------"
one = Hom(U,U).identity()
print f*one
print "--------------------"
print one*f
print "--------------------"
print f*f
</code></pre>
<p>When I run this code, I expect <code>f</code> to be printed thrice, followed by the identity morphism.
However, while the first and third output do in fact both print <code>f</code>, the second prints </p>
<pre><code>Composite map:
From: Cyclotomic Field of order 3 and degree 2
To: Cyclotomic Field of order 3 and degree 2
Defn: Identity endomorphism of Cyclotomic Field of order 3 and degree 2
then
Ring endomorphism of Cyclotomic Field of order 3 and degree 2
Defn: zeta3 |--> -zeta3 - 1
</code></pre>
<p>Is this a bug or is this this behaviour explained somewhere in the documentation?</p>
http://ask.sagemath.org/question/42625/possible-bug-with-identity-morphism/?answer=42671#post-id-42671In general it is entirely unclear if "writing out" a composite map is easier/more efficient than keeping the composite form. It depends on the implementations of the maps and the way the maps get used subsequently. Sage is taking a cautious approach here and leaves the map in the form closest to how it's given. It has one optimization: If the identity map finds it gets precomposed with another map than it knows to just return the other map (multiplication happens left-to-right in python). Apparently the other maps don't check if the map they get precomposed with is the identity map. They could, but the test might be more expensive than the gains made.
You can always work out the composed map by using it on the generators:
sage: g=f*one
sage: U.Hom(U)([g(a) for a in U.gens()])
Ring endomorphism of Cyclotomic Field of order 3 and degree 2
Defn: zeta3 |--> -zeta3 - 1
Tue, 19 Jun 2018 10:47:23 -0500http://ask.sagemath.org/question/42625/possible-bug-with-identity-morphism/?answer=42671#post-id-42671Answer by tmonteil for <p>I have a number field <code>U</code> for which I consider its automorphisms through <code>Hom(U,U)</code>.
The identity <code>one=Hom(U,U).identity()</code> behaves weirdly under right multiplication:</p>
<pre><code>U = CyclotomicField(3)
f = Hom(U,U)[1]
print f
print "--------------------"
one = Hom(U,U).identity()
print f*one
print "--------------------"
print one*f
print "--------------------"
print f*f
</code></pre>
<p>When I run this code, I expect <code>f</code> to be printed thrice, followed by the identity morphism.
However, while the first and third output do in fact both print <code>f</code>, the second prints </p>
<pre><code>Composite map:
From: Cyclotomic Field of order 3 and degree 2
To: Cyclotomic Field of order 3 and degree 2
Defn: Identity endomorphism of Cyclotomic Field of order 3 and degree 2
then
Ring endomorphism of Cyclotomic Field of order 3 and degree 2
Defn: zeta3 |--> -zeta3 - 1
</code></pre>
<p>Is this a bug or is this this behaviour explained somewhere in the documentation?</p>
http://ask.sagemath.org/question/42625/possible-bug-with-identity-morphism/?answer=42629#post-id-42629``f`` and ``one*f`` are the same mathematical object, but are not represented in the same way by Sage:
sage: g = f*one
sage: type(f)
<class 'sage.rings.number_field.morphism.CyclotomicFieldHomomorphism_im_gens'>
sage: type(g)
<type 'sage.categories.map.FormalCompositeMap'>
You should not worry about that, since:
sage: g in Hom(U,U)
True
sage: f == g
True
sage: g.parent()
Automorphism group of Cyclotomic Field of order 3 and degree 2
sage: f.parent()
Automorphism group of Cyclotomic Field of order 3 and degree 2
**EDIT** regarding the comment, a possible workaround would be to define the ``one`` not as the identity of ``Hom(U,U).identity()`` but as ``Hom(U,U)[0]`` since it will be of the same type as ``f`` and everything will work well:
sage: Hom(U,U)[0]
Ring endomorphism of Cyclotomic Field of order 3 and degree 2
Defn: zeta3 |--> zeta3
sage: Hom(U,U).identity()
Identity endomorphism of Cyclotomic Field of order 3 and degree 2
sage: Hom(U,U)[0] == Hom(U,U).identity()
True
sage: one = Hom(U,U)[0]
sage: type(one)
<class 'sage.rings.number_field.morphism.CyclotomicFieldHomomorphism_im_gens'>
sage: one*f
Ring endomorphism of Cyclotomic Field of order 3 and degree 2
Defn: zeta3 |--> -zeta3 - 1
sage: f*one
Ring endomorphism of Cyclotomic Field of order 3 and degree 2
Defn: zeta3 |--> -zeta3 - 1
sage: one
Ring endomorphism of Cyclotomic Field of order 3 and degree 2
Defn: zeta3 |--> zeta3
sage: f
Ring endomorphism of Cyclotomic Field of order 3 and degree 2
Defn: zeta3 |--> -zeta3 - 1Sun, 17 Jun 2018 08:40:28 -0500http://ask.sagemath.org/question/42625/possible-bug-with-identity-morphism/?answer=42629#post-id-42629Comment by MadPidgeon for <p><code>f</code> and <code>one*f</code> are the same mathematical object, but are not represented in the same way by Sage:</p>
<pre><code>sage: g = f*one
sage: type(f)
<class 'sage.rings.number_field.morphism.CyclotomicFieldHomomorphism_im_gens'>
sage: type(g)
<type 'sage.categories.map.FormalCompositeMap'>
</code></pre>
<p>You should not worry about that, since:</p>
<pre><code>sage: g in Hom(U,U)
True
sage: f == g
True
sage: g.parent()
Automorphism group of Cyclotomic Field of order 3 and degree 2
sage: f.parent()
Automorphism group of Cyclotomic Field of order 3 and degree 2
</code></pre>
<p><strong>EDIT</strong> regarding the comment, a possible workaround would be to define the <code>one</code> not as the identity of <code>Hom(U,U).identity()</code> but as <code>Hom(U,U)[0]</code> since it will be of the same type as <code>f</code> and everything will work well:</p>
<pre><code>sage: Hom(U,U)[0]
Ring endomorphism of Cyclotomic Field of order 3 and degree 2
Defn: zeta3 |--> zeta3
sage: Hom(U,U).identity()
Identity endomorphism of Cyclotomic Field of order 3 and degree 2
sage: Hom(U,U)[0] == Hom(U,U).identity()
True
sage: one = Hom(U,U)[0]
sage: type(one)
<class 'sage.rings.number_field.morphism.CyclotomicFieldHomomorphism_im_gens'>
sage: one*f
Ring endomorphism of Cyclotomic Field of order 3 and degree 2
Defn: zeta3 |--> -zeta3 - 1
sage: f*one
Ring endomorphism of Cyclotomic Field of order 3 and degree 2
Defn: zeta3 |--> -zeta3 - 1
sage: one
Ring endomorphism of Cyclotomic Field of order 3 and degree 2
Defn: zeta3 |--> zeta3
sage: f
Ring endomorphism of Cyclotomic Field of order 3 and degree 2
Defn: zeta3 |--> -zeta3 - 1
</code></pre>
http://ask.sagemath.org/question/42625/possible-bug-with-identity-morphism/?comment=42630#post-id-42630Is there any way to coerce the formal composite map to one of the former type?
I compute the product of `O(n)` morphisms as `f = one` and `for phi in l: f = phi*f`, and the latter becomes an unreadable block of composite maps when I print the result, and it doesn't help the algorithmic complexity either when trying to evaluate the function I presume.
I can work around this by reverse iterating the list and doing right multiplication with `phi` instead, but this feels like a hack.
Furthermore, my question was whether any piece of documentation justifies this counter intuitive result.Sun, 17 Jun 2018 09:10:17 -0500http://ask.sagemath.org/question/42625/possible-bug-with-identity-morphism/?comment=42630#post-id-42630