[Numpy-discussion] Adapting algorithm to accept Scipy sparse matrix

David Koch ogdude@googlemail....
Fri Mar 23 09:28:20 CDT 2007


Ok, I got it to work now but - damn, it's ugly. I thought I'd have to watch
the differences between ndarray and matrix type but it turns out
sparseMatrix is yet again different from matrix in several respects when it
comes to certain operations. Is this intended or something that will be
mended as sparse stuff becomes more mature?

Concrete examples for matrix/vector multiplication:

testMat = asmatrix(around(10 * random.randn(3,2)))
testSp = sparse.lil_matrix(testMat)

testMat * array([1, 2])
# matrix([[-24., -26., -26.]])

testMat * array([[1, 2]])
# ValueError: matrices are not aligned

testMat * array([[1, 2]]).transpose()
# matrix([[-24.],
#        [-26.],
#         [-26.]])

... and "dot" has the same effect as "*". Basically mat * rank1 array = row
matrix and mat * rank2 array = column matrix

For sparse matrices on the other hand the same operations:

testSp * array([1, 2])
# array([-24., -26., -26.])

testSp * array([[1, 2]])
# array([-24., -26., -26.])

testSp * array([[1, 2]]).transpose()
# array([-24., -26., -26.])

... and "dot" != "*" (don't know what the former does exactly). So unlike
with regular matrices, "*" operations return a (dense) rank1 array
regardless of the shape/rank of the vector.

I find this confusing and it makes for very ugly code, since a lot of ndim,
shape checking is involved if a routine is supposed to accept array AND
matrix as input.

Any comments or hints?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20070323/0faf6053/attachment.html 

More information about the Numpy-discussion mailing list