[SciPy-Dev] Indexing sparse matrices

Bruce Southey bsouthey at gmail.com
Wed Jun 23 15:31:32 EDT 2010


On 06/23/2010 09:22 AM, Jason Grout wrote:
> Hi everyone,
>
> This bug in scipy sparse matrices was recently reported in Sage:
>
> ----------------------------------------------------------------------
> | Sage Version 4.4.2, Release Date: 2010-05-19                       |
> | Type notebook() for the GUI, and license() for information.        |
> ----------------------------------------------------------------------
> sage: from scipy import sparse
> sage: a = sparse.lil_matrix((10,10))
> sage: a[1,2] = 1
> ---------------------------------------------------------------------------
> ValueError                                Traceback (most recent call last)
>
> /Users/grout/sage-4.4.2-test3/local/lib/python2.6/site-packages/numpy/core/<ipython
> console>  in<module>()
>
> /Users/grout/sage/local/lib/python2.6/site-packages/scipy/sparse/lil.pyc
> in __setitem__(self, index, x)
>       320                     self._insertat3(row, data, j, xx)
>       321         else:
> -->  322             raise ValueError('invalid index value: %s' % str((i,
> j)))
>       323
>       324     def _mul_scalar(self, other):
>
> ValueError: invalid index value: (1, 2)
>
>
> Since the preparser in Sage makes integers instances of the Sage Integer
> class, and the Sage Integer class is not in the specific list of types
> checked by numpy.isscalar, the case falls through the checks in the
> sparse matrix setitem method to the last error-raising case. The problem
> seems to be that scipy checks for a specific list of types (via
> numpy.isscalar), instead of just using the __index__ magic method which
> was designed for this purpose (by Travis Oliphant for numpy! see
> http://docs.python.org/whatsnew/2.5.html#pep-357-the-index-method).
>
> Note that things work fine in numpy:
>
> sage: import numpy
> sage: a=numpy.array([[1,2],[3,4]],dtype=int); a
> array([[1, 2],
>          [3, 4]])
> sage: a[1,1]=5
> sage: a
> array([[1, 2],
>          [3, 5]])
>
>
> Thanks,
>
> Jason
>
>
> _______________________________________________
> SciPy-Dev mailing list
> SciPy-Dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-dev
>    

Please post this discussion on the numpy list from the scipy-dev list 
because this is not really scipy issue (yet).

I agree that this appears to be an issue between Sage and numpy's 
isscalar function. There are many uses of isscalar() in numpy (by 
grepping the source) and scipy that should be failing with Sage. For 
example  you should be getting errors using numpy's polynomial functions 
(polynomial/polynomial.py).  I would have thought that you would be 
seeing lots of errors when running numpy and scipy tests within Sage. 
(Perhaps those tests don't occur with Sage integers because Sage does 
not create them.)

Also, note that numpy still supports Python 2.4.

Bruce



More information about the SciPy-Dev mailing list