Conversion to float taking forever

44 views
Skip to first unread message

Nico Van Cleemput

unread,
Oct 10, 2014, 4:42:44 AM10/10/14
to sage-s...@googlegroups.com
Today I came across the following problem. I start by computing the Gutman energy for a graph:

sage: c26 = Graph('YsP@H?PC?O`?@@?_?G?@??CC?G??GG?E??@_??K???W???W???H???E_')
sage: ge = sum(lam for lam in c26.adjacency_matrix().eigenvalues() if lam > 0)
sage: ge
19.87092248988078?

So far everything seems to be OK, but then I try

sage: float(ge)

Now you can wait for a very long time. (I actually didn't test to see how long it would exactly take) The type of ge is sage.rings.qqbar.AlgebraicNumber. Looking at the implementation of the float conversion, the problem seems to come from the following:

sage: AA(ge)

This indeed also seems to take forever. At that point I'm lost. I don't really understand much of that part of Sage, so I don't know how to look further for a solution to this problem. Is there another way to get a good float approximation of that value?

Cheers
Nico

Marc Mezzarobba

unread,
Oct 10, 2014, 5:05:35 AM10/10/14
to sage-s...@googlegroups.com
Nico Van Cleemput wrote:
> Looking at the implementation of the
> float conversion, the problem seems to come from the following:
>
> sage: AA(ge)

So apparently Sage is trying to prove that the imaginary part of the
algebraic result is exactly zero.

> Is there another way to get a good float approximation of that value?

Yes:

sage: float(CDF(ge))
19.870922489880776

--
Marc

Volker Braun

unread,
Oct 10, 2014, 6:48:39 AM10/10/14
to sage-s...@googlegroups.com
If you don't need provable correct answers then just change to RDF/CDF for fast computation:

sage: c26 = Graph('YsP@H?PC?O`?@@?_?G?@??CC?G??GG?E??@_??K???W???W???H???E_')
sage: sum(lam for lam in c26.adjacency_matrix().change_ring(RDF).eigenvalues() if lam > 0)
19.870922489880787

John Cremona

unread,
Oct 10, 2014, 12:34:52 PM10/10/14
to SAGE support
What people have not been saying explicitly is this: if you ask for
the eigenvalues of an integer matrix then they will be returned as
elements of QQbar, i.e. as algebraic numbers with a specific embedding
into CC, which display looking like approximate floating points
numbers but are not. These are then very inefficient to do a lot of
comutations with, as you discovered. To avoid this, explicitly change
your matrix's base ring to something approximate like RDF and then
the eigenvalue computation will be done there, with all the usual
warnings from numerical analysis about precision issues.

John
> --
> You received this message because you are subscribed to the Google Groups
> "sage-support" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sage-support...@googlegroups.com.
> To post to this group, send email to sage-s...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sage-support.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages