#10928: numpy matrices indexed by sage integers are wrong
-------------------------+--------------------------------------------------
Reporter: flawrence | Owner: tbd
Type: defect | Status: new
Priority: critical | Milestone: sage-4.7
Component: packages | Keywords:
Author: | Upstream: N/A
Reviewer: | Merged:
Work_issues: |
-------------------------+--------------------------------------------------
Comment(by dsm):
It's not a Sage-specific issue. E.g. using gmpy's mpz has the same issue:
{{{
Python 2.7 (r27:82500, Aug 18 2010, 23:33:57)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import numpy, gmpy
>>>
>>> m = numpy.matrix(numpy.arange(6)).reshape(3,2)
>>> m
matrix([[0, 1],
[2, 3],
[4, 5]])
>>> m[:,0]
matrix([[0],
[2],
[4]])
>>> m[:,gmpy.mpz(0)]
matrix([[0, 2, 4]])
}}}
The underlying issue is that numpy doesn't recognize Sage integers as
scalars:
{{{
sage: numpy.isscalar(int(0))
True
sage: numpy.isscalar((0))
False
sage: numpy.ScalarType
(<type 'int'>, <type 'float'>, <type 'complex'>, <type 'long'>, <type
'bool'>, <type 'str'>, <type 'unicode'>, <type 'buffer'>, <type
'numpy.int64'>, <type 'numpy.int16'>, <type 'numpy.complex128'>, <type
'numpy.void'>, <type 'numpy.int32'>, <type 'numpy.uint32'>, <type
'numpy.float128'>, <type 'numpy.string_'>, <type 'numpy.int8'>, <type
'numpy.uint8'>, <type 'numpy.float32'>, <type 'numpy.bool_'>, <type
'numpy.uint64'>, <type 'numpy.int64'>, <type 'numpy.uint64'>, <type
'numpy.complex64'>, <type 'numpy.unicode_'>, <type 'numpy.uint16'>, <type
'numpy.float64'>, <type 'numpy.object_'>, <type 'numpy.complex256'>)
}}}
because it doesn't recognize it as a generic and it's not listed in the
acceptable scalar types. This means the wrong branch is taken here:
{{{
# numpy/matrixlib/defmatrix.py
if out.ndim == 1:
sh = out.shape[0]
# Determine when we should have a column array
try:
n = len(index)
except:
n = 0
if n > 1 and isscalar(index[1]):
out.shape = (sh,1)
else:
out.shape = (1,sh)
}}}
n=2, but isscalar(Integer(0)) is False.
--
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/10928#comment:1>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica,
and MATLAB
--
You received this message because you are subscribed to the Google Groups
"sage-trac" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sage-trac?hl=en.