p3: add lrls/helper.go

This commit is contained in:
leo 2023-03-05 17:48:31 +01:00
parent 0d66b660bb
commit a44f8def21
Signed by: wanderer
SSH Key Fingerprint: SHA256:Dp8+iwKHSlrMEHzE3bJnPng70I7LEsa3IJXRH/U+idQ
2 changed files with 59 additions and 1 deletions

@ -4,4 +4,4 @@ go 1.19
require git.dotya.ml/wanderer/ak9im/p2 v0.0.0-20230227032746-9910d7702660
require gonum.org/v1/gonum v0.12.0 // indirect
require gonum.org/v1/gonum v0.12.0

58
p3/lrls/helper.go Normal file

@ -0,0 +1,58 @@
package lrls
import (
"errors"
"log"
"gonum.org/v1/gonum/mat"
)
// diagMat returns a Diagonal Matrix initialised with *data* across the main
// diagonal.
// nolint: unused
func diagMat(data []float64) *mat.DiagDense {
n := len(data)
dm := mat.NewDiagDense(n, data)
return dm
}
// nolint: unused
func diagVals(n int) []float64 {
d := make([]float64, n)
for i := 0; i < n; i++ {
d[i] = 1000000
}
return d
}
// nolint: unused
func covMatrices(size, params int) ([]*mat.Dense, error) {
switch {
case size < 1:
return nil, errors.New("ErrCovMatricesSize")
case params < 4 || params > 5:
log.Printf("error: params needs to be from the set {4 ,5}, got: %d", params)
return nil, errors.New("ErrBadParams")
}
// slice of covariance matrices.
cms := make([]*mat.Dense, 0, size)
for i := range cms {
cms[i] = mat.NewDense(params, params, nil)
}
// create a matrix of the same shape as the cov matrix, with *diagVals*
// along the main diagonal.
dm := diagMat(diagVals(params))
// replace the second covariance matrix with the diagonal matrix.
cms[1].CloneFrom(dm)
return cms, nil
}