mat: make RowView and ColView return Vector type and add RowViewOf and ColViewOf

Also change Dot signature to accept Vector parameters.
This commit is contained in:
James Bowman
2017-09-01 22:25:38 +01:00
committed by Dan Kortschak
parent d7342e68fb
commit e01a71d4d5
12 changed files with 157 additions and 57 deletions

View File

@@ -98,10 +98,10 @@ type Mutable interface {
Matrix
}
// A RowViewer can return a VecDense reflecting a row that is backed by the matrix
// data. The VecDense returned will have length equal to the number of columns.
// A RowViewer can return a Vector reflecting a row that is backed by the matrix
// data. The Vector returned will have length equal to the number of columns.
type RowViewer interface {
RowView(i int) *VecDense
RowView(i int) Vector
}
// A RawRowViewer can return a slice of float64 reflecting a row that is backed by the matrix
@@ -110,10 +110,10 @@ type RawRowViewer interface {
RawRowView(i int) []float64
}
// A ColViewer can return a VecDense reflecting a column that is backed by the matrix
// data. The VecDense returned will have length equal to the number of rows.
// A ColViewer can return a Vector reflecting a column that is backed by the matrix
// data. The Vector returned will have length equal to the number of rows.
type ColViewer interface {
ColView(j int) *VecDense
ColView(j int) Vector
}
// A RawColViewer can return a slice of float64 reflecting a column that is backed by the matrix
@@ -330,13 +330,22 @@ func Det(a Matrix) float64 {
// Dot returns the sum of the element-wise product of a and b.
// Dot panics if the matrix sizes are unequal.
func Dot(a, b *VecDense) float64 {
func Dot(a, b Vector) float64 {
la := a.Len()
lb := b.Len()
if la != lb {
panic(ErrShape)
}
return blas64.Dot(la, a.mat, b.mat)
if arv, ok := a.(RawVectorer); ok {
if brv, ok := b.(RawVectorer); ok {
return blas64.Dot(la, arv.RawVector(), brv.RawVector())
}
}
var sum float64
for i := 0; i < la; i++ {
sum += a.At(i, 0) * b.At(i, 0)
}
return sum
}
// Equal returns whether the matrices a and b have the same size