mat: implement helper routines for type extraction and update Trace to use an interface (#932)

* Implement helper routines for type extraction and update Trace to use an interface.

Updates #929.
This commit is contained in:
Brendan Tracey
2019-03-31 09:26:36 +01:00
committed by GitHub
parent a4ad4d254f
commit 9a0642d3dd
7 changed files with 164 additions and 45 deletions

View File

@@ -217,6 +217,18 @@ func (t *TriDense) RawTriangular() blas64.Triangular {
return t.mat
}
// SetRawTriangular sets the underlying blas64.Triangular used by the receiver.
// Changes to elements in the receiver following the call will be reflected
// in the input.
//
// The supplied Triangular must not use blas.Unit storage format.
func (t *TriDense) SetRawTriangular(mat blas64.Triangular) {
if mat.Diag == blas.Unit {
panic("mat: cannot set TriDense with Unit storage format")
}
t.mat = mat
}
// Reset zeros the dimensions of the matrix so that it can be reused as the
// receiver of a dimensionally restricted operation.
//
@@ -513,6 +525,16 @@ func (t *TriDense) ScaleTri(f float64, a Triangular) {
}
}
// Trace returns the trace of the matrix.
func (t *TriDense) Trace() float64 {
// TODO(btracey): could use internal asm sum routine.
var v float64
for i := 0; i < t.mat.N; i++ {
v += t.mat.Data[i*t.mat.Stride+i]
}
return v
}
// copySymIntoTriangle copies a symmetric matrix into a TriDense
func copySymIntoTriangle(t *TriDense, s Symmetric) {
n, upper := t.Triangle()