[Numpy-discussion] sage: isscalar can't hear quacks

D. S. McNeil dsm054 at gmail.com
Mon Mar 14 20:17:06 EDT 2011


(Sending upstream from http://trac.sagemath.org/sage_trac/ticket/10928)

sage: import numpy
sage: m = numpy.matrix(numpy.arange(6)).reshape(3,2)
sage: m[:,int(0)]
matrix([[0],
        [2],
        [4]])
sage: m[:,0]
matrix([[0, 2, 4]])

That is, Python ints produce a column but Sage Integers produce a row!
 This happens because

sage: numpy.isscalar(int(0))
True
sage: numpy.isscalar(Integer(0))
False

where isscalar simply tries

   if isinstance(num, generic):
        return True
    else:
        return type(num) in ScalarType

Since Sage Integers (or Python Fractions, or any other unknown scalar)
aren't instances of generic or listed in ScalarType, isscalar returns
False, and there's an unintended fallthrough in matrix.__getitem__.
ISTM either __getitem__ needs to change or isscalar needs to become
more accepting, or both.. but the former might leave some other
unexpected isscalar-related issues elsewhere and I don't know enough
about numpy internals to know if broadening isscalar would cause
problems of its own.

Recommendations?


Doug

--
Department of Earth Sciences
University of Hong Kong



More information about the NumPy-Discussion mailing list