mat: add TriDense.SliceTri

This commit is contained in:
Vladimir Chalupecky
2020-03-07 21:23:43 +01:00
committed by Vladimír Chalupecký
parent 5f268d9394
commit 4363550baf
2 changed files with 99 additions and 0 deletions

View File

@@ -600,6 +600,26 @@ func (t *TriDense) ScaleTri(f float64, a Triangular) {
}
}
// SliceTri returns a new Triangular that shares backing data with the receiver.
// The returned matrix starts at {i,i} of the receiver and extends k-i rows and
// columns. The final row and column in the resulting matrix is k-1.
// SliceTri panics with ErrIndexOutOfRange if the slice is outside the capacity
// of the receiver.
func (t *TriDense) SliceTri(i, k int) Triangular {
return t.sliceTri(i, k)
}
func (t *TriDense) sliceTri(i, k int) *TriDense {
if i < 0 || t.cap < i || k < i || t.cap < k {
panic(ErrIndexOutOfRange)
}
v := *t
v.mat.Data = t.mat.Data[i*t.mat.Stride+i : (k-1)*t.mat.Stride+k]
v.mat.N = k - i
v.cap = t.cap - i
return &v
}
// Trace returns the trace of the matrix.
func (t *TriDense) Trace() float64 {
// TODO(btracey): could use internal asm sum routine.