mat: add striding to DiagDense and capacity to extract diags from other matrices
Also add IsZero and Reset needed for this.
MODIFY
mat/index_bound_checks.go
mat/index_bound_checks.go
@@ -238,16 +238,16 @@ func (d *DiagDense) At(i, j int) float64 {
}
func (d *DiagDense) at(i, j int) float64 {
- if uint(i) >= uint(len(d.data)) {
+ if uint(i) >= uint(d.n) {
panic(ErrRowAccess)
}
- if uint(j) >= uint(len(d.data)) {
+ if uint(j) >= uint(d.n) {
panic(ErrColAccess)
}
if i != j {
return 0
}
- return d.data[i]
+ return d.mat.Data[i*d.mat.Inc]
}
// SetDiag sets the element at row i, column i to the value v.
@@ -257,8 +257,8 @@ func (d *DiagDense) SetDiag(i int, v float64) {
}
func (d *DiagDense) setDiag(i int, v float64) {
- if uint(i) >= uint(len(d.data)) {
+ if uint(i) >= uint(d.n) {
panic(ErrRowAccess)
}
- d.data[i] = v
+ d.mat.Data[i*d.mat.Inc] = v
}

MODIFY
mat/index_no_bound_checks.go
mat/index_no_bound_checks.go
@@ -238,10 +238,10 @@ func (s *SymBandDense) set(i, j int, v float64) {
// At returns the element at row i, column j.
func (d *DiagDense) At(i, j int) float64 {
- if uint(i) >= uint(len(d.data)) {
+ if uint(i) >= uint(d.n) {
panic(ErrRowAccess)
}
- if uint(j) >= uint(len(d.data)) {
+ if uint(j) >= uint(d.n) {
panic(ErrColAccess)
}
return d.at(i, j)
@@ -251,18 +251,18 @@ func (d *DiagDense) at(i, j int) float64 {
if i != j {
return 0
}
- return d.data[i]
+ return d.mat.Data[i*d.mat.Inc]
}
// SetDiag sets the element at row i, column i to the value v.
// It panics if the location is outside the appropriate region of the matrix.
func (d *DiagDense) SetDiag(i int, v float64) {
- if uint(i) >= uint(len(d.data)) {
+ if uint(i) >= uint(d.n) {
panic(ErrRowAccess)
}
d.setDiag(i, v)
}
func (d *DiagDense) setDiag(i int, v float64) {
- d.data[i] = v
+ d.mat.Data[i*d.mat.Inc] = v
}

MODIFY
mat/list_test.go
mat/list_test.go
@@ -482,9 +482,10 @@ func makeCopyOf(a Matrix) Matrix {
return m
case *DiagDense:
d := &DiagDense{
- data: make([]float64, len(t.data)),
+ mat: blas64.Vector{Inc: 1, Data: make([]float64, len(t.mat.Data))},
+ n: t.n,
}
- copy(d.data, t.data)
+ copy(d.mat.Data, t.mat.Data)
return d
}
}
@@ -618,10 +619,12 @@ var testMatrices = []Matrix{
&SymDense{},
NewTriDense(3, true, nil),
NewTriDense(3, false, nil),
- &VecDense{mat: blas64.Vector{Inc: 1}},
- &DiagDense{},
&basicVector{},
+ &VecDense{mat: blas64.Vector{Inc: 1}},
&VecDense{mat: blas64.Vector{Inc: 10}},
+ &DiagDense{},
+ &DiagDense{mat: blas64.Vector{Inc: 1}},
+ &DiagDense{mat: blas64.Vector{Inc: 10}},
&basicMatrix{},
&basicSymmetric{},
&basicTriangular{cap: 3, mat: blas64.Triangular{N: 3, Stride: 3, Uplo: blas.Upper}},

mat: add striding to DiagDense and capacity to extract diags from other matrices

Also add IsZero and Reset needed for this.