[SciPy-user] scipy.sparse.lil_matrix and fancy indexing

Anne Archibald peridot.faceted at gmail.com
Sun Feb 3 17:29:23 EST 2008


Hi,

It looks to me like there's an inconsistency between how numpy
matrices handle fancy indexing and how scipy.sparse.lil_matrix handles
fancy indexing:

In [15]: A = N.zeros((3,3))

In [16]: A[[0,1,2],[0,1,2]] = 1

In [18]: A
Out[18]:
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])

In [19]: B = scipy.sparse.lil_matrix((3,3))

In [21]: B[[0,1,2],[0,1,2]] = [1,1,1]

In [22]: print B
  (0, 0)        1
  (0, 1)        1
  (0, 2)        1
  (1, 0)        1
  (1, 1)        1
  (1, 2)        1
  (2, 0)        1
  (2, 1)        1
  (2, 2)        1

In [23]: B-A
Out[23]:
matrix([[ 0.,  1.,  1.],
        [ 1.,  0.,  1.],
        [ 1.,  1.,  0.]])


(Fancy indexing also does not accept scalars, but that's presumably
just not been implemented yet.)

In light of the following, this seems like unintended behaviour:

In [24]: B[[0,1,2],[0,1,2]] = [1,2,3]

In [25]: p B
  (0, 0)        1
  (0, 1)        1
  (0, 2)        1
  (1, 0)        2
  (1, 1)        2
  (1, 2)        2
  (2, 0)        3
  (2, 1)        3
  (2, 2)        3


At the least, I can't see why I would have expected this result.

Is this intended behaviour? Failure to raise an exception on bad
input? Just a bug?

Thanks,
Anne



More information about the SciPy-User mailing list