From 35fb0d04134928a92a3b12682312a5933a6d08a1 Mon Sep 17 00:00:00 2001 From: Jonathan J Lawlor Date: Sat, 15 Nov 2014 15:14:24 -0500 Subject: [PATCH] improve comments --- covariancematrix.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/covariancematrix.go b/covariancematrix.go index 45a797b5..d32f0e0b 100644 --- a/covariancematrix.go +++ b/covariancematrix.go @@ -16,26 +16,28 @@ func CovarianceMatrix(x mat64.Matrix) *mat64.Dense { // the correction found in the Covariance and Variance functions. r, _ := x.Dims() + + // determine the mean of each of the columns b := ones(1, r) b.Mul(b, x) b.Scale(1/float64(r), b) - - // todo: avoid unneeded memory expansion here. mu := b.RowView(0) - - // this could also be done with a clone & row viewer + + // subtract the mean from the data xc := mat64.DenseCopyOf(x) for i := 0; i < r; i++ { rv := xc.RowView(i) - for j, mean := range(mu) { + for j, mean := range mu { rv[j] -= mean - } + } } - // todo: avoid matrix copy + // todo: avoid matrix copy? xt := new(mat64.Dense) xt.TCopy(xc) + // It would be nice if we could indicate that this was a symmetric + // matrix. ss := new(mat64.Dense) ss.Mul(xt, xc) ss.Scale(1/float64(r-1), ss)