See #13386 for a related problem. I do not see how we can make the coercion numpy int/Sage Integer works if numpy is not loaded at startup time... and it seems that we do not want it because it takes too much time. There might be a hack using the hash of the numpy int type. That way we can check efficiently if an object is a numpy int. I would be happy to work on it but the best strategy is not yet completely clear to me.
Best, Vincent 2014/1/4, Thierry <sage-googlesu...@lma.metelu.net>: > Hi, > > On Sat, Jan 04, 2014 at 01:03:21AM +0100, Harald Schilly wrote: >> Here is a problem maybe somebody can help with (reported on sage-support) >> >> A complex number in an arithmetic operation with a numpy float gives a >> wrong answer ... i.e. the complex part is dropped. >> >> import numpy as np >> 1j / np.float64(2) >> >> gives 0.0 > > On my Sage 6.0, i first get a warning (according to > http://sourceforge.net/projects/numpy/files/NumPy/1.5.0/NOTES.txt/view > the message comes from numpy): > > sage: 1j / np.float64(2) > /opt/sagemath/sage-6.0-i686-Linux/src/bin/sage-ipython:1: > ComplexWarning: Casting complex values to real discards the imaginary > part > #!/usr/bin/env python > 0.0 > > So, this is not really a bug ;) > > How i understand it, the problem comes from a lack of good coercion > between ComplexNumbers and np.float64, which only raises a warning and > not an error: > > sage: import sage.structure.element as e > sage: cm = e.get_coercion_model() > sage: cm.common_parent(1j, np.float64(2)) > <type 'numpy.float64'> > sage: np.float64(1j) > 0.0 > > There is an old ticket asking to clean the relation between Sage and > numpy: http://trac.sagemath.org/ticket/8824 > > That said, the following symbolic works (because np.float64 are coreced > to SR): > > sage: I / np.float64(2) > 0.5*I > > Ciao, > Thierry > > >> 1j + np.float64(2) >> >> gives 2.0 >> >> It works with float32/float though: >> >> 1j + np.float32(2) >> >> (2+1j) >> >> Harald >> >> -- >> You received this message because you are subscribed to the Google Groups >> "sage-devel" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to sage-devel+unsubscr...@googlegroups.com. >> To post to this group, send email to sage-devel@googlegroups.com. >> Visit this group at http://groups.google.com/group/sage-devel. >> For more options, visit https://groups.google.com/groups/opt_out. > > -- > You received this message because you are subscribed to the Google Groups > "sage-devel" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sage-devel+unsubscr...@googlegroups.com. > To post to this group, send email to sage-devel@googlegroups.com. > Visit this group at http://groups.google.com/group/sage-devel. > For more options, visit https://groups.google.com/groups/opt_out. > -- You received this message because you are subscribed to the Google Groups "sage-devel" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+unsubscr...@googlegroups.com. To post to this group, send email to sage-devel@googlegroups.com. Visit this group at http://groups.google.com/group/sage-devel. For more options, visit https://groups.google.com/groups/opt_out.