Ask Your Question

Question abour numerical coercions

asked 2017-12-28 02:19:41 -0500

Emmanuel Charpentier gravatar image

updated 2017-12-28 02:20:47 -0500

I do not understand this :

sage: t,u=(1.4,0.7)
sage: z=complex(t,u)

This works :

sage: QQ(t)

But :

sage: AA(t)
# Redacted...    
TypeError: Illegal initializer for algebraic number

Similarly :

sage: QQbar(t)
## Redacted...
TypeError: Illegal initializer for algebraic number

However, this works :

sage: QQbar(QQ(t))

Similarly :

sage: QQbar(z)
# 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 flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-12-28 09:11:51 -0500

vdelecroix gravatar image

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)

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)
sage: Zmod(3).coerce(3)

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


edit flag offensive delete link 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...

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2017-12-28 09:36:45 -0500 )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
sage: poly = algdep(a, 3)
sage: poly
x^2 - 2
sage: b = QQbar.polynomial_root(poly, a)
sage: b
sage: b*b
vdelecroix gravatar imagevdelecroix ( 2017-12-28 10:41:44 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2017-12-28 02:19:41 -0500

Seen: 20 times

Last updated: Dec 28 '17