Maurizio wrote: > I'd like to jump in the conversation, to describe my own experience. > > I don't know what about real numbers, but I found out that in that > same situation you were talking about (symbolic expression where all > the symbols are substituted by numbers eventually), and the expected > result is a complex number, using the ComplexField() makes the > substitution many orders of magnitude faster than doing .n() > > Example: > > CF = ComplexField() > evaluated_number = CF(numerical_expression) > > In this way, I get the result in the fastest way (I'm not that much > experienced, though). > > Any suggestion is appreciated >
As a shortcut, you could just use CC, which is the same as ComplexField() (i.e., CC is ComplexField with the default 53 bits of precision) This would be analogous to the RR example below (except using complex numbers instead of real numbers. sage: CC is ComplexField() True sage: CC(sqrt(-1)) 1.00000000000000*I I'm surprised that this is orders of magnitude faster than using n(), since I thought they were essentially doing the same thing. My timing tests seem to indicate that the opposite is true: sage: a=sqrt(2) sage: timeit('CC(a)') 625 loops, best of 3: 33 µs per loop sage: timeit('a.n()') 625 loops, best of 3: 19.2 µs per loop sage: timeit('RR(a)') 625 loops, best of 3: 20.7 µs per loop ah, but if the number is complex: sage: a=sqrt(2)*I sage: timeit('CC(a)') 625 loops, best of 3: 83.5 µs per loop sage: timeit('a.n()') 625 loops, best of 3: 213 µs per loop The code for n() first tries to convert to a real number, and if an exception is raised, then it tries to convert to a complex number. Maybe that try/except block is way, way too slow? Comments? Thanks, Jason --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---