On 05.01.2014, at 02:36, Nils Bruin <nbr...@sfu.ca> wrote:

> It's an odd confluence of circumstances that makes numpy.float64 behave this 
> way. The code that causes the problem is this:
> 
> sage/structure/coerce.pyx:965 (cpdef canonical_coercion)
>        elif y_numeric:
>            try:
>                sage_parent = py_scalar_parent(type(y))
>                if sage_parent is None or sage_parent.has_coerce_map_from(xp):
>                    return y.__class__(x), y
>                else:
>                    return self.canonical_coercion(x, sage_parent(y))
>            except (TypeError, ValueError):
>                self._record_exception()
> 
> Some of the relevant facts:
> 
> sage: sage.structure.coerce.py_scalar_parent(type(np.float(1)))
> Real Double Field
> sage: sage.structure.coerce.py_scalar_parent(type(np.float64(1)))
> None
> 
> but on the other hand, with  cython("def isnum(a): return PY_IS_NUMERIC(a)")
> 
> sage: isnum(np.float(1))
> True
> sage: isnum(np.float32(1))
> False
> sage: isnum(np.float64(1))
> True
> sage: isnum(np.float128(1))
> False
> 
> So it seems to be the combination of "float64" being "numeric" but having no 
> corresponding "sage scalar parent" defined for it. We have
> 
> #define PY_IS_NUMERIC(zzz_obj) \
>     (PyInt_Check(zzz_obj) ||  PyBool_Check(zzz_obj) || PyLong_Check(zzz_obj) 
> || \
>       PyFloat_Check(zzz_obj) || PyComplex_Check(zzz_obj))
> 
> Indeed:
> 
> sage: isinstance(np.float64(1),float)
> True
> sage: isinstance(np.float32(1),float)
> False
> sage: isinstance(np.float128(1),float)
> False
> sage: isinstance(np.float(1),float)
> True
> sage: type(np.float(1))
> <type 'float'>
> 
> so the problem is that float64 (at least on most machines) inherits from 
> float but isn't exactly a float.
> 
> We can probably solve the problem by changing py_scalar_parent to check types 
> via "isinstance" rather than comparison by "is". In the process, you can also 
> type the argument "py_type" to be a "type". That will cause cython to 
> generate more efficient code for "is instance".

It's a pity that there doesn't seem to be an easy fix for this. This problem 
essentially breaks Sage as an efficient and friendly environment for me (and 
probably most of the engineering tasks that always involve Complex numbers). I 
extremely like the Sage notebook for trying out new ideas before converting the 
Sage notebook into a Python/numpy/scipy script. Unfortunately my experience 
with Sage's internals is too limited to be able to help much here. If there's 
anything where I can help (testing, …) let me know.

Bernhard

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

Reply via email to