[SciPy-User] Sparse matrices and dot product

Pauli Virtanen pav@iki...
Sun Nov 28 19:29:04 CST 2010

On Sun, 28 Nov 2010 20:47:35 +0100, Sebastian Walter wrote:
> However, I noticed that when I first joined this mailing list there
> seemed to be a strong agreement among the subscribers that `A.dot(B)` is
> "evil". IIRC the argument was something similar to "there should be only
> one obvious way".

Can you point to a previous discussion on that?

I don't recall strong opposition on adding the dot product as a method at 
any point. The only objection I have seen is that dot(A,B) implementation 
is ambigous if A and B are not of the same type. (However, the current 
ndarray implementation just dispatches to np.dot.)

There have been discussions on adding median() etc. as new methods, but 
in the case of .dot() one has an important syntatical advantage that is 
not relevant in the other cases.

> This is a rather generic code that can operate on `array_like` objects.
> Since `list` does not have a dot method, the use of A.dot(B) would be
> less generic.

I don't believe this is an important concern in practice.

> Wouldn't it be much more convenient if `numpy.dot` were designed such
> that  `A` could also be a sparse matrix instead of adding a `dot`
> method?

This would be nice, but it is a separate concern from what the dot() 
method tries to achieve. The above genericity requirement could apply 
also to other linear algebra operations -- which expands the scope to a 
more general abstract linear algebra framework.

numpy.dot cannot know about sparse matrices, so the protocol for these 
operations would need to be extensible. One way could be try to follow 
python and add a __dot__() or dot() methods, in analog to __mul__.

Pauli Virtanen

More information about the SciPy-User mailing list