# 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 ?

edit retag close merge delete

Sort by ยป oldest newest most voted

First 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/93222358

more

Thanks. 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...

( 2017-12-28 09:36:45 -0600 )edit

Yes. 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?

( 2017-12-28 10:41:44 -0600 )edit