mat: relax restrictions on vector orientation
MODIFY
mat/dense_arithmetic.go
mat/dense_arithmetic.go
@@ -760,17 +760,16 @@ func (m *Dense) Apply(fn func(i, j int, v float64) float64, a Matrix) {
}
}
-// RankOne performs a rank-one update to the matrix a and stores the result
-// in the receiver. If a is zero, see Outer.
-// m = a + alpha * x * y'
+// RankOne performs a rank-one update to the matrix a with the vectors x and
+// y, where x and y are treated as column vectors. The result is stored in the
+// receiver. If a is zero, see Outer.
+// m = a + alpha * x * y^T
func (m *Dense) RankOne(a Matrix, alpha float64, x, y Vector) {
ar, ac := a.Dims()
- xr, xc := x.Dims()
- if xr != ar || xc != 1 {
+ if x.Len() != ar {
panic(ErrShape)
}
- yr, yc := y.Dims()
- if yr != ac || yc != 1 {
+ if y.Len() != ac {
panic(ErrShape)
}
@@ -785,15 +784,17 @@ func (m *Dense) RankOne(a Matrix, alpha float64, x, y Vector) {
fast := true
xU, _ := untranspose(x)
if rv, ok := xU.(RawVectorer); ok {
+ r, c := xU.Dims()
xmat = rv.RawVector()
- m.checkOverlap((&VecDense{mat: xmat}).asGeneral())
+ m.checkOverlap(generalFromVector(xmat, r, c))
} else {
fast = false
}
yU, _ := untranspose(y)
if rv, ok := yU.(RawVectorer); ok {
+ r, c := yU.Dims()
ymat = rv.RawVector()
- m.checkOverlap((&VecDense{mat: ymat}).asGeneral())
+ m.checkOverlap(generalFromVector(ymat, r, c))
} else {
fast = false
}
@@ -815,22 +816,12 @@ func (m *Dense) RankOne(a Matrix, alpha float64, x, y Vector) {
}
}
-// Outer calculates the outer product of the column vectors x and y,
-// and stores the result in the receiver.
-// m = alpha * x * y'
+// Outer calculates the outer product of the vectors x and y, where x and y
+// are treated as column vectors, and stores the result in the receiver.
+// m = alpha * x * y^T
// In order to update an existing matrix, see RankOne.
func (m *Dense) Outer(alpha float64, x, y Vector) {
- xr, xc := x.Dims()
- if xc != 1 {
- panic(ErrShape)
- }
- yr, yc := y.Dims()
- if yc != 1 {
- panic(ErrShape)
- }
-
- r := xr
- c := yr
+ r, c := x.Len(), y.Len()
// Copied from reuseAs with use replaced by useZeroed
// and a final zero of the matrix elements if we pass
@@ -858,16 +849,17 @@ func (m *Dense) Outer(alpha float64, x, y Vector) {
fast := true
xU, _ := untranspose(x)
if rv, ok := xU.(RawVectorer); ok {
+ r, c := xU.Dims()
xmat = rv.RawVector()
- m.checkOverlap((&VecDense{mat: xmat}).asGeneral())
-
+ m.checkOverlap(generalFromVector(xmat, r, c))
} else {
fast = false
}
yU, _ := untranspose(y)
if rv, ok := yU.(RawVectorer); ok {
+ r, c := yU.Dims()
ymat = rv.RawVector()
- m.checkOverlap((&VecDense{mat: ymat}).asGeneral())
+ m.checkOverlap(generalFromVector(ymat, r, c))
} else {
fast = false
}

MODIFY
mat/inner.go
mat/inner.go
@@ -12,8 +12,11 @@ import (
// Inner computes the generalized inner product
// x^T A y
-// between column vectors x and y with matrix A. This is only a true inner product if
-// A is symmetric positive definite, though the operation works for any matrix A.
+// between the vectors x and y with matrix A, where x and y are treated as
+// column vectors.
+//
+// This is only a true inner product if A is symmetric positive definite, though
+// the operation works for any matrix A.
//
// Inner panics if x.Len != m or y.Len != n when A is an m x n matrix.
func Inner(x Vector, a Matrix, y Vector) float64 {

MODIFY
mat/symmetric.go
mat/symmetric.go
@@ -262,12 +262,12 @@ func (s *SymDense) CopySym(a Symmetric) int {
return n
}
-// SymRankOne performs a symetric rank-one update to the matrix a and stores
-// the result in the receiver
-// s = a + alpha * x * x'
+// SymRankOne performs a symmetric rank-one update to the matrix a with x,
+// which is treated as a column vector, and stores the result in the receiver
+// s = a + alpha * x * x^T
func (s *SymDense) SymRankOne(a Symmetric, alpha float64, x Vector) {
- n, c := x.Dims()
- if a.Symmetric() != n || c != 1 {
+ n := x.Len()
+ if a.Symmetric() != n {
panic(ErrShape)
}
s.reuseAs(n)
@@ -281,8 +281,9 @@ func (s *SymDense) SymRankOne(a Symmetric, alpha float64, x Vector) {
xU, _ := untranspose(x)
if rv, ok := xU.(RawVectorer); ok {
+ r, c := xU.Dims()
xmat := rv.RawVector()
- s.checkOverlap((&VecDense{mat: xmat}).asGeneral())
+ s.checkOverlap(generalFromVector(xmat, r, c))
blas64.Syr(alpha, xmat, s.mat)
return
}
@@ -371,17 +372,16 @@ func (s *SymDense) SymOuterK(alpha float64, x Matrix) {
}
}
-// RankTwo performs a symmmetric rank-two update to the matrix a and stores
-// the result in the receiver
-// m = a + alpha * (x * y' + y * x')
+// RankTwo performs a symmetric rank-two update to the matrix a with the
+// vectors x and y, which are treated as column vectors, and stores the
+// result in the receiver
+// m = a + alpha * (x * y^T + y * x^T)
func (s *SymDense) RankTwo(a Symmetric, alpha float64, x, y Vector) {
n := s.mat.N
- xr, xc := x.Dims()
- if xr != n || xc != 1 {
+ if x.Len() != n {
panic(ErrShape)
}
- yr, yc := y.Dims()
- if yr != n || yc != 1 {
+ if y.Len() != n {
panic(ErrShape)
}
@@ -395,15 +395,17 @@ func (s *SymDense) RankTwo(a Symmetric, alpha float64, x, y Vector) {
fast := true
xU, _ := untranspose(x)
if rv, ok := xU.(RawVectorer); ok {
+ r, c := xU.Dims()
xmat = rv.RawVector()
- s.checkOverlap((&VecDense{mat: xmat}).asGeneral())
+ s.checkOverlap(generalFromVector(xmat, r, c))
} else {
fast = false
}
yU, _ := untranspose(y)
if rv, ok := yU.(RawVectorer); ok {
+ r, c := yU.Dims()
ymat = rv.RawVector()
- s.checkOverlap((&VecDense{mat: ymat}).asGeneral())
+ s.checkOverlap(generalFromVector(ymat, r, c))
} else {
fast = false
}

mat: relax restrictions on vector orientation