mat: fix GrowSym behaviour for Reset matrices
Also fix CovarianceMatrix benchmarks in stat that found this.
MODIFY
mat/symmetric.go
mat/symmetric.go
@@ -600,7 +600,7 @@ func (s *SymDense) GrowSym(n int) Symmetric {
}
var v SymDense
n += s.mat.N
- if n > s.cap {
+ if s.IsEmpty() || n > s.cap {
v.mat = blas64.Symmetric{
N: n,
Stride: n,

MODIFY
mat/symmetric_test.go
mat/symmetric_test.go
@@ -685,6 +685,12 @@ func TestViewGrowSquare(t *testing.T) {
}
}
}
+
+ s.Reset()
+ rg := s.GrowSym(n).(*SymDense)
+ if rg.mat.Stride < n {
+ t.Errorf("unexpected stride after GrowSym on empty matrix: got:%d want >= %d", rg.mat.Stride, n)
+ }
}
}

MODIFY
stat/statmat_test.go
stat/statmat_test.go
@@ -88,8 +88,8 @@ func TestCovarianceMatrix(t *testing.T) {
}
}
}
-
}
+
if !panics(func() { CovarianceMatrix(nil, mat.NewDense(5, 2, nil), []float64{}) }) {
t.Errorf("CovarianceMatrix did not panic with weight size mismatch")
}
@@ -99,6 +99,13 @@ func TestCovarianceMatrix(t *testing.T) {
if !panics(func() { CovarianceMatrix(nil, mat.NewDense(2, 2, []float64{1, 2, 3, 4}), []float64{1, -1}) }) {
t.Errorf("CovarianceMatrix did not panic with negative weights")
}
+ if panics(func() {
+ dst := mat.NewSymDense(4, nil)
+ dst.Reset()
+ CovarianceMatrix(dst, mat.NewDense(2, 2, []float64{1, 2, 3, 4}), nil)
+ }) {
+ t.Errorf("CovarianceMatrix panics with reset destination")
+ }
}
func TestCorrelationMatrix(t *testing.T) {
@@ -307,9 +314,11 @@ func randMat(r, c int) mat.Matrix {
}
func benchmarkCovarianceMatrix(b *testing.B, m mat.Matrix) {
+ var res mat.SymDense
b.ResetTimer()
for i := 0; i < b.N; i++ {
- CovarianceMatrix(nil, m, nil)
+ res.Reset()
+ CovarianceMatrix(&res, m, nil)
}
}
func benchmarkCovarianceMatrixWeighted(b *testing.B, m mat.Matrix) {
@@ -318,9 +327,11 @@ func benchmarkCovarianceMatrixWeighted(b *testing.B, m mat.Matrix) {
for i := range wts {
wts[i] = 0.5
}
+ var res mat.SymDense
b.ResetTimer()
for i := 0; i < b.N; i++ {
- CovarianceMatrix(nil, m, wts)
+ res.Reset()
+ CovarianceMatrix(&res, m, wts)
}
}
func benchmarkCovarianceMatrixInPlace(b *testing.B, m mat.Matrix) {

mat: fix GrowSym behaviour for Reset matrices

Also fix CovarianceMatrix benchmarks in stat that found this.