[SciPy-Dev] Indexing sparse matrices

Nathaniel Smith njs@pobox....
Thu Jun 24 10:20:53 CDT 2010

On Thu, Jun 24, 2010 at 7:39 AM, Bruce Southey <bsouthey@gmail.com> wrote:
> On 06/23/2010 11:43 PM, Jason Grout wrote:
>> On 6/23/10 12:31 PM, Bruce Southey wrote:
>>> Please post this discussion on the numpy list from the scipy-dev list
>>> because this is not really scipy issue (yet).
> This is not the place for this discussion!
>> The thing is that the index object may not be a scalar, but it may have
>> an __index__ method that is supposed to be used when the object is used
>> as an index.  So the call to isscalar may rightfully return False, but
>> the __index__ method should still be called to see if there is a value
>> to act as an index.  For example, according to Python>=2.5, this should
>> work:
> Remember (as given below) that numpy officially supports Python 2.4 so
> any Python 2.5+ features are not allowed (at least until some one
> notices them).
> If you just look at the numpy's isscalar function you can see why it
> fails - so file a bug report on it.

How is this not a scipy bug? From what you say, np.isscalar is working
correctly, i.e., it determines whether an object is of a type that can
be stored directly in an array of primitive type. Sage integers do not
have an associated dtype. (Or maybe isscalar is supposed to be doing
something else -- the docs are not at all clear -- but this is a
plausible interpretation. Maybe it's supposed to be checking for
whether an object is iterable?)

The bug is that scipy.sparse is using np.isscalar to determine whether
something is a valid index, and that's not what it does.

So we need an isindex() or toindex() function. Maybe this should be
promoted to numpy so libraries besides scipy.sparse can use it, but
that's a different question.

Even though built-in types (i.e. lists and tuples) don't start
supporting __index__ until 2.5, there's no issue with numpy or scipy
types supporting it on 2.4 -- it's just a method, if it exists then
you can call it. The only thing to be careful of is that in 2.4, you
need a special case to detect that int's are valid indices, whereas in
2.5+ int gains an __index__ method so it can be handled by generic
__index__ handling code.

-- Nathaniel

More information about the SciPy-Dev mailing list