gonum / matrix

Matrix packages for the Go language [DEPRECATED]
446 stars 53 forks source link

mat64.Dot function #391

Closed DanLieghio closed 7 years ago

DanLieghio commented 7 years ago

Why does the mat64.Dot function require the 2 matrices to be equal? If I want to get the dot product of e.g. a [2 x 2] matrix and a [2 x 1] matrix, the function panics out.

Thanks!

btracey commented 7 years ago
Dot returns the sum of the element-wise products of the elements of a and b.

In order to perform an element-wise product, the sizes have to be equal. Are you looking for Mul?

DanLieghio commented 7 years ago

Thanks - yes - I was looking for Mul. That's really helpful Brendan, it's working now.

FYI what Mul does, I would normally call that the Dot Product, that's what was confusing me.

If others feel the same way, you could consider modifying Dot to try running a Mul (instead of just panicking) in the event that the matrices are different sizes.

kortschak commented 7 years ago

@DanLieghio there is probably some confusion about the meaning of dot because of its sloppy de facto definition in numpy where it means both the vector dot product and the matrix multiply operator, depending on the shape of the factors.

We don't use that definitions; dot product is normally a vector operation (here we are a little sloppy in that we allow it on matrices at all) otherwise known as the scalar product. Our interpretation of the dot product in Dot is that it is the scalar product of the matrices as if the matrices were unrolled into a pair of vectors with identity between elements (thus requiring identical shape). We could call Dot, Inner instead to avoid the confusion resulting from python and other people's incorrect usage, but Inner is already used for vectors.

@btracey Do we actually need Dot? From a quick survey, the only uses I can see is on vector factors where Inner could have been used.

btracey commented 7 years ago

I've never needed to use it

kortschak commented 7 years ago

It has uses in optimize, but they could be replaced with calls to Inner AFAICS

kortschak commented 7 years ago

The other user is @sjwhitworth in golearn. He would see an efficiency gain in changing to Inner after grabbing the vectors.

kortschak commented 7 years ago

Ooops. Inner is more than I remembered, x^TAy. We could however make Dot explicitly only handle *Vector. This would avoid the confusion.