Add Reset method

Reset allows reuse of used matrices, a use that would otherwise be
blocked by dim checking for many operations.
This commit is contained in:
kortschak
2014-01-22 09:34:49 +10:30
parent 1a85feca5c
commit 85652f6bd8
2 changed files with 12 additions and 0 deletions

View File

@@ -187,6 +187,11 @@ func (m *Dense) Submatrix(a Matrix, i, j, r, c int) {
m.Clone(m) m.Clone(m)
} }
func (m *Dense) Reset() {
m.mat.Rows, m.mat.Cols = 0, 0
m.mat.Data = m.mat.Data[:0]
}
func (m *Dense) Clone(a Matrix) { func (m *Dense) Clone(a Matrix) {
r, c := a.Dims() r, c := a.Dims()
m.mat = RawMatrix{ m.mat = RawMatrix{

View File

@@ -81,6 +81,13 @@ type Cloner interface {
Clone(a Matrix) Clone(a Matrix)
} }
// A Reset can zero the dimensions of the matrix so that it can be reused as
// the receiver of a dimensionally restricted operation. This is commonly used
// when the matrix is being used a a workspace or temporary matrix.
type Reseter interface {
Reset()
}
// A Copier can make a copy of elements of a into the receiver. The copy operation fills the // A Copier can make a copy of elements of a into the receiver. The copy operation fills the
// submatrix in m with the values from the submatrix of a with the dimensions equal to the // submatrix in m with the values from the submatrix of a with the dimensions equal to the
// minumum of two matrices. The number of row and columns copied it returned. // minumum of two matrices. The number of row and columns copied it returned.