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.Thu, 28 Dec 2017 10:41:44 -0600Question abour numerical coercionshttp://ask.sagemath.org/question/40335/question-abour-numerical-coercions/I do not understand this :
sage: t,u=(1.4,0.7)
sage: z=complex(t,u)
This works :
sage: QQ(t)
7/5
But :
sage: AA(t)
---------------------------------------------------------------------------
TypeError
# Redacted...
TypeError: Illegal initializer for algebraic number
Similarly :
sage: QQbar(t)
---------------------------------------------------------------------------
TypeError
## Redacted...
TypeError: Illegal initializer for algebraic number
However, this works :
sage: QQbar(QQ(t))
7/5
Similarly :
sage: QQbar(z)
---------------------------------------------------------------------------
TypeError
# Redacted...
TypeError: Illegal initializer for algebraic number
One has to do :
sage: QQbar(QQ(real_part(z))+I*QQ(imag_part(z)))
7/10*I + 7/5
The same happens when you try to use other numerical types (RDF, CDF, etc...)
There must be (good) reasons to have direct coercions from numerical real types to `QQ` but not to have a direct coercion from numerical complex types to algebraic types (`AA` and `QQbar`), but I do not know them.
Or does it happen to be an oversight ?Thu, 28 Dec 2017 02:19:41 -0600http://ask.sagemath.org/question/40335/question-abour-numerical-coercions/Answer by vdelecroix for <p>I do not understand this :</p>
<pre><code>sage: t,u=(1.4,0.7)
sage: z=complex(t,u)
</code></pre>
<p>This works :</p>
<pre><code>sage: QQ(t)
7/5
</code></pre>
<p>But :</p>
<pre><code>sage: AA(t)
---------------------------------------------------------------------------
TypeError
# Redacted...
TypeError: Illegal initializer for algebraic number
</code></pre>
<p>Similarly :</p>
<pre><code>sage: QQbar(t)
---------------------------------------------------------------------------
TypeError
## Redacted...
TypeError: Illegal initializer for algebraic number
</code></pre>
<p>However, this works :</p>
<pre><code>sage: QQbar(QQ(t))
7/5
</code></pre>
<p>Similarly :</p>
<pre><code>sage: QQbar(z)
---------------------------------------------------------------------------
TypeError
# Redacted...
TypeError: Illegal initializer for algebraic number
</code></pre>
<p>One has to do :</p>
<pre><code>sage: QQbar(QQ(real_part(z))+I*QQ(imag_part(z)))
7/10*I + 7/5
</code></pre>
<p>The same happens when you try to use other numerical types (RDF, CDF, etc...)</p>
<p>There must be (good) reasons to have direct coercions from numerical real types to <code>QQ</code> but not to have a direct coercion from numerical complex types to algebraic types (<code>AA</code> and <code>QQbar</code>), but I do not know them.</p>
<p>Or does it happen to be an oversight ?</p>
http://ask.sagemath.org/question/40335/question-abour-numerical-coercions/?answer=40337#post-id-40337First of all, there is a difference between **conversion** and **coercion**. Conversion is mostly a convenience used to initialize data from other data. The following is a conversion
sage: QQ(1.4)
7/5
Coercion is a much stronger requirement that implies that there is a natural map between the underlying set (such as ZZ -> QQ or ZZ -> Zmod(3))
sage: QQ.coerce(3)
3
sage: Zmod(3).coerce(3)
0
But there is no coercion from floating point to QQ
sage: QQ.coerce(1.4)
Traceback (most recent call last):
....
TypeError: no canonical coercion from Real Field with 53 bits of precision to Rational Field
Now to answer your question, it would be delicate to implement a conversion from floating point complex numbers to QQbar. The main reason is because of input input such as 1.41421356237310? Should it be converted to sqrt(2)? Or to 131836323/93222358 as in
sage: QQ(sqrt(2.))
131836323/93222358Thu, 28 Dec 2017 09:11:51 -0600http://ask.sagemath.org/question/40335/question-abour-numerical-coercions/?answer=40337#post-id-40337Comment by vdelecroix for <p>First of all, there is a difference between <strong>conversion</strong> and <strong>coercion</strong>. Conversion is mostly a convenience used to initialize data from other data. The following is a conversion</p>
<pre><code>sage: QQ(1.4)
7/5
</code></pre>
<p>Coercion is a much stronger requirement that implies that there is a natural map between the underlying set (such as ZZ -> QQ or ZZ -> Zmod(3))</p>
<pre><code>sage: QQ.coerce(3)
3
sage: Zmod(3).coerce(3)
0
</code></pre>
<p>But there is no coercion from floating point to QQ</p>
<pre><code>sage: QQ.coerce(1.4)
Traceback (most recent call last):
....
TypeError: no canonical coercion from Real Field with 53 bits of precision to Rational Field
</code></pre>
<p>Now to answer your question, it would be delicate to implement a conversion from floating point complex numbers to QQbar. The main reason is because of input input such as 1.41421356237310? Should it be converted to sqrt(2)? Or to 131836323/93222358 as in</p>
<pre><code>sage: QQ(sqrt(2.))
</code></pre>
<p>131836323/93222358</p>
http://ask.sagemath.org/question/40335/question-abour-numerical-coercions/?comment=40340#post-id-40340Yes. `sqrt(2.) = 1.41421356237310` is a good example.
And for getting back sqrt(2) from the numerical value there is
sage: a = sqrt(2.)
sage: a
1.41421356237310
sage: poly = algdep(a, 3)
sage: poly
x^2 - 2
sage: b = QQbar.polynomial_root(poly, a)
sage: b
1.4142135623730952?
sage: b*b
2.000000000000001?Thu, 28 Dec 2017 10:41:44 -0600http://ask.sagemath.org/question/40335/question-abour-numerical-coercions/?comment=40340#post-id-40340Comment by Emmanuel Charpentier for <p>First of all, there is a difference between <strong>conversion</strong> and <strong>coercion</strong>. Conversion is mostly a convenience used to initialize data from other data. The following is a conversion</p>
<pre><code>sage: QQ(1.4)
7/5
</code></pre>
<p>Coercion is a much stronger requirement that implies that there is a natural map between the underlying set (such as ZZ -> QQ or ZZ -> Zmod(3))</p>
<pre><code>sage: QQ.coerce(3)
3
sage: Zmod(3).coerce(3)
0
</code></pre>
<p>But there is no coercion from floating point to QQ</p>
<pre><code>sage: QQ.coerce(1.4)
Traceback (most recent call last):
....
TypeError: no canonical coercion from Real Field with 53 bits of precision to Rational Field
</code></pre>
<p>Now to answer your question, it would be delicate to implement a conversion from floating point complex numbers to QQbar. The main reason is because of input input such as 1.41421356237310? Should it be converted to sqrt(2)? Or to 131836323/93222358 as in</p>
<pre><code>sage: QQ(sqrt(2.))
</code></pre>
<p>131836323/93222358</p>
http://ask.sagemath.org/question/40335/question-abour-numerical-coercions/?comment=40339#post-id-40339Thanks. If I follow you, you mean that there is no _canonical_ mapping from numerical types to algebraic, and that Sage authors choose not to impose an artificial one.
I summose that one can always write a convenience function specific to a given problem...Thu, 28 Dec 2017 09:36:45 -0600http://ask.sagemath.org/question/40335/question-abour-numerical-coercions/?comment=40339#post-id-40339