Sage+numpy -> complex numbers drop image part in calculations with numpy functions

78 views
Skip to first unread message

bspi...@gmail.com

unread,
Jan 2, 2014, 3:22:14 PM1/2/14
to sage-s...@googlegroups.com
After importing numpy, Sage for some reason loses the ability to deal with complex numbers correctly, e.g.

sage: (1+1j)/sqrt(2)
(0.500000000000000 + 0.500000000000000*I)*sqrt(2)
sage: from numpy import *
sage: (1+1j)/sqrt(2)
/Applications/MyApps/Math/Sage-6.0.app/Contents/Resources/sage/src/bin/sage-ipython:1: ComplexWarning: Casting complex values to real discards the imaginary part
#!/usr/bin/env python
0.70710678118654746

The last result is obviously wrong. This happens in 'Sage Version 6.0, Release Date: 2013-12-17' as well as in Sage 5.11 on Mac OS X 10.8. The same problem occurs with other numpy functions like log10 etc and with other operators like '*', '+', ...

When I use the system supplied python all is fine:

Python 2.7.2 (default, Oct 11 2012, 20:14:37)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from numpy import *
>>> (1+1j)/sqrt(2)
(0.7071067811865475+0.7071067811865475j)

What's going on here?

Best regards,
Bernhard

Harald Schilly

unread,
Jan 2, 2014, 10:13:02 PM1/2/14
to sage-s...@googlegroups.com


On Thursday, January 2, 2014 9:22:14 PM UTC+1, bspi...@gmail.com wrote:

What's going on here?


These are two different "sqrt": the first one is a sage specific function, the second one is from numpy. it's just that numpy's sqrt(2) produces something (a float64 type) which makes troubles with the part from sage (which is not of type "complex", but "sage.rings.complex_number.ComplexNumber")

here is a more isolated example:

import numpy as np
1j / np.float64(2)

which gives 0.

note:

type(1j)
<type 'sage.rings.complex_number.ComplexNumber'>

Harald

bspi...@gmail.com

unread,
Jan 3, 2014, 5:53:50 PM1/3/14
to sage-s...@googlegroups.com
I fully agree with your analysis. But still the result is wrong. Should I file a bug? Is it a bug in Sage or in Numpy? I have no idea how the two work together. I'd appreciate any hint on how to proceed.

Thanks,
    Bernhard

Harald Schilly

unread,
Jan 3, 2014, 6:57:58 PM1/3/14
to sage-s...@googlegroups.com
On Fri, Jan 3, 2014 at 11:53 PM, <bspi...@gmail.com> wrote:
> I fully agree with your analysis. But still the result is wrong. Should I
> file a bug? Is it a bug in Sage or in Numpy? I have no idea how the two work
> together. I'd appreciate any hint on how to proceed.

It's definitely something which is Sage related, but I'm not sure
if/how this can be fixed. I'll post this on sage-devel and hopefully
somebody can make a comment ;-)

Harald
Reply all
Reply to author
Forward
0 new messages