Change Viewer interface to receiver assignment

This commit is contained in:
kortschak
2013-10-15 14:14:11 +10:30
parent 34da1fe0fb
commit 9184de1c41
2 changed files with 12 additions and 15 deletions

View File

@@ -201,27 +201,24 @@ func (m *Dense) SetRow(r int, v []float64) int {
} }
// View returns a view on the receiver. // View returns a view on the receiver.
func (m *Dense) View(i, j, r, c int) Blasser { func (m *Dense) View(i, j, r, c int) {
v := Dense{BlasMatrix{
Order: m.mat.Order,
Rows: r - i,
Cols: c - j,
Stride: m.mat.Stride,
}}
switch m.mat.Order { switch m.mat.Order {
case blas.RowMajor: case blas.RowMajor:
v.mat.Data = m.mat.Data[i*m.mat.Stride+j : (i+r-1)*m.mat.Stride+(j+c)] m.mat.Data = m.mat.Data[i*m.mat.Stride+j : (i+r-1)*m.mat.Stride+(j+c)]
case blas.ColMajor: case blas.ColMajor:
v.mat.Data = m.mat.Data[i+j*m.mat.Stride : (i+r)+(j+c-1)*m.mat.Stride] m.mat.Data = m.mat.Data[i+j*m.mat.Stride : (i+r)+(j+c-1)*m.mat.Stride]
default: default:
panic(ErrIllegalOrder) panic(ErrIllegalOrder)
} }
return &v m.mat.Rows = r - i
m.mat.Cols = c - j
} }
func (m *Dense) Submatrix(a Matrix, i, j, r, c int) { func (m *Dense) Submatrix(a Matrix, i, j, r, c int) {
// This is probably a bad idea, but for the moment, we do it. // This is probably a bad idea, but for the moment, we do it.
m.Clone(&Dense{m.View(i, j, r, c).BlasMatrix()}) v := *m
v.View(i, j, r, c)
m.Clone(&Dense{v.BlasMatrix()})
} }
func (m *Dense) Clone(a Matrix) { func (m *Dense) Clone(a Matrix) {

View File

@@ -78,11 +78,11 @@ type Copier interface {
// A Viewer can extract a submatrix view of of the receiver, starting at row i, column j // A Viewer can extract a submatrix view of of the receiver, starting at row i, column j
// and extending r rows and c columns. If i or j are out of range, or r or c extend beyond // and extending r rows and c columns. If i or j are out of range, or r or c extend beyond
// the bounds of the matrix View will panic with ErrIndexOutOfRange. Viewer must not return // the bounds of the matrix View will panic with ErrIndexOutOfRange. View must retain the
// a copy of the submatrix; changes in the elements of the submatrix must be reflected in the // receiver's reference to the original matrix such that changes in the elements of the
// original and vice versa. // submatrix are reflected in the original and vice versa.
type Viewer interface { type Viewer interface {
View(i, j, r, c int) Blasser View(i, j, r, c int)
} }
// A Submatrixer can extract a copy of submatrix from a into the receiver, starting at row i, // A Submatrixer can extract a copy of submatrix from a into the receiver, starting at row i,