mat: return ErrZeroLength when size is zero and document it
MODIFY
mat/band.go
mat/band.go
@@ -292,7 +292,8 @@ func (b *BandDense) Zero() {
// 2 - The Frobenius norm, the square root of the sum of the squares of the elements
// Inf - The maximum absolute row sum
//
-// Norm will panic with ErrNormOrder if an illegal norm is specified.
+// Norm will panic with ErrNormOrder if an illegal norm is specified and with
+// ErrZeroLength if the matrix has zero size.
func (b *BandDense) Norm(norm float64) float64 {
if b.IsEmpty() {
panic(ErrZeroLength)
@@ -304,11 +305,17 @@ func (b *BandDense) Norm(norm float64) float64 {
return lapack64.Langb(lnorm, b.mat)
}
-// Trace computes the trace of the matrix.
+// Trace returns the trace of the matrix.
+//
+// Trace will panic with ErrSquare if the matrix is not square and with
+// ErrZeroLength if the matrix has zero size.
func (b *BandDense) Trace() float64 {
r, c := b.Dims()
if r != c {
- panic(ErrShape)
+ panic(ErrSquare)
+ }
+ if b.IsEmpty() {
+ panic(ErrZeroLength)
}
rb := b.RawBand()
var tr float64

MODIFY
mat/dense.go
mat/dense.go
@@ -573,12 +573,18 @@ func (m *Dense) Augment(a, b Matrix) {
w.Copy(b)
}
-// Trace returns the trace of the matrix. The matrix must be square or Trace
-// will panic.
+// Trace returns the trace of the matrix.
+//
+// Trace will panic with ErrSquare if the matrix is not square and with
+// ErrZeroLength if the matrix has zero size.
func (m *Dense) Trace() float64 {
- if m.mat.Rows != m.mat.Cols {
+ r, c := m.Dims()
+ if r != c {
panic(ErrSquare)
}
+ if m.IsEmpty() {
+ panic(ErrZeroLength)
+ }
// TODO(btracey): could use internal asm sum routine.
var v float64
for i := 0; i < m.mat.Rows; i++ {
@@ -592,7 +598,8 @@ func (m *Dense) Trace() float64 {
// 2 - The Frobenius norm, the square root of the sum of the squares of the elements
// Inf - The maximum absolute row sum
//
-// Norm will panic with ErrNormOrder if an illegal norm is specified.
+// Norm will panic with ErrNormOrder if an illegal norm is specified and with
+// ErrShape if the matrix has zero size.
func (m *Dense) Norm(norm float64) float64 {
if m.IsEmpty() {
panic(ErrZeroLength)

MODIFY
mat/diagonal.go
mat/diagonal.go
@@ -316,15 +316,19 @@ func (d *DiagDense) IsEmpty() bool {
return d.mat.Inc == 0
}
-// Trace returns the trace.
+// Trace returns the trace of the matrix.
+//
+// Trace will panic with with ErrZeroLength if the matrix has zero size.
func (d *DiagDense) Trace() float64 {
+ if d.IsEmpty() {
+ panic(ErrZeroLength)
+ }
rb := d.RawBand()
var tr float64
for i := 0; i < rb.Rows; i++ {
tr += rb.Data[rb.KL+i*rb.Stride]
}
return tr
-
}
// Norm returns the specified norm of the receiver. Valid norms are:

MODIFY
mat/symband.go
mat/symband.go
@@ -249,7 +249,8 @@ func (s *SymBandDense) DoColNonZero(j int, fn func(i, j int, v float64)) {
// 2 - The Frobenius norm, the square root of the sum of the squares of the elements
// Inf - The maximum absolute row sum
//
-// Norm will panic with ErrNormOrder if an illegal norm is specified.
+// Norm will panic with ErrNormOrder if an illegal norm is specified and with
+// ErrZeroLength if the matrix has zero size.
func (s *SymBandDense) Norm(norm float64) float64 {
if s.IsEmpty() {
panic(ErrZeroLength)
@@ -263,8 +264,13 @@ func (s *SymBandDense) Norm(norm float64) float64 {
return lapack64.Lansb(lnorm, s.mat, nil)
}
-// Trace returns the trace.
+// Trace returns the trace of the matrix.
+//
+// Trace will panic with ErrZeroLength if the matrix has zero size.
func (s *SymBandDense) Trace() float64 {
+ if s.IsEmpty() {
+ panic(ErrZeroLength)
+ }
rb := s.RawSymBand()
var tr float64
for i := 0; i < rb.N; i++ {

MODIFY
mat/symmetric.go
mat/symmetric.go
@@ -584,7 +584,8 @@ func (s *SymDense) sliceSym(i, k int) *SymDense {
// 2 - The Frobenius norm, the square root of the sum of the squares of the elements
// Inf - The maximum absolute row sum
//
-// Norm will panic with ErrNormOrder if an illegal norm is specified.
+// Norm will panic with ErrNormOrder if an illegal norm is specified and with
+// ErrZeroLength if the matrix has zero size.
func (s *SymDense) Norm(norm float64) float64 {
if s.IsEmpty() {
panic(ErrZeroLength)
@@ -599,7 +600,12 @@ func (s *SymDense) Norm(norm float64) float64 {
}
// Trace returns the trace of the matrix.
+//
+// Trace will panic with ErrZeroLength if the matrix has zero size.
func (s *SymDense) Trace() float64 {
+ if s.IsEmpty() {
+ panic(ErrZeroLength)
+ }
// TODO(btracey): could use internal asm sum routine.
var v float64
for i := 0; i < s.mat.N; i++ {

MODIFY
mat/triangular.go
mat/triangular.go
@@ -626,7 +626,8 @@ func (t *TriDense) sliceTri(i, k int) *TriDense {
// 2 - The Frobenius norm, the square root of the sum of the squares of the elements
// Inf - The maximum absolute row sum
//
-// Norm will panic with ErrNormOrder if an illegal norm is specified.
+// Norm will panic with ErrNormOrder if an illegal norm is specified and with
+// ErrZeroLength if the matrix has zero size.
func (t *TriDense) Norm(norm float64) float64 {
if t.IsEmpty() {
panic(ErrZeroLength)
@@ -641,7 +642,12 @@ func (t *TriDense) Norm(norm float64) float64 {
}
// Trace returns the trace of the matrix.
+//
+// Trace will panic with ErrZeroLength if the matrix has zero size.
func (t *TriDense) Trace() float64 {
+ if t.IsEmpty() {
+ panic(ErrZeroLength)
+ }
// TODO(btracey): could use internal asm sum routine.
var v float64
for i := 0; i < t.mat.N; i++ {

MODIFY
mat/triband.go
mat/triband.go
@@ -453,7 +453,8 @@ func (t *TriBandDense) DiagView() Diagonal {
// 2 - The Frobenius norm, the square root of the sum of the squares of the elements
// Inf - The maximum absolute row sum
//
-// Norm will panic with ErrNormOrder if an illegal norm is specified.
+// Norm will panic with ErrNormOrder if an illegal norm is specified and with
+// ErrZeroLength if the matrix has zero size.
func (t *TriBandDense) Norm(norm float64) float64 {
if t.IsEmpty() {
panic(ErrZeroLength)
@@ -467,8 +468,13 @@ func (t *TriBandDense) Norm(norm float64) float64 {
return lapack64.Lantb(lnorm, t.mat, nil)
}
-// Trace returns the trace.
+// Trace returns the trace of the matrix.
+//
+// Trace will panic with ErrZeroLength if the matrix has zero size.
func (t *TriBandDense) Trace() float64 {
+ if t.IsEmpty() {
+ panic(ErrZeroLength)
+ }
rb := t.RawTriBand()
var tr float64
var offsetIndex int

MODIFY
mat/tridiag.go
mat/tridiag.go
@@ -177,7 +177,12 @@ func (a *Tridiag) Zero() {
}
// Trace returns the trace of the matrix.
+//
+// Trace will panic with ErrZeroLength if the matrix has zero size.
func (a *Tridiag) Trace() float64 {
+ if a.IsEmpty() {
+ panic(ErrZeroLength)
+ }
return f64.Sum(a.mat.D)
}
@@ -186,7 +191,8 @@ func (a *Tridiag) Trace() float64 {
// 2 - The Frobenius norm, the square root of the sum of the squares of the elements
// Inf - The maximum absolute row sum
//
-// Norm will panic with ErrNormOrder if an illegal norm is specified.
+// Norm will panic with ErrNormOrder if an illegal norm is specified and with
+// ErrZeroLength if the matrix has zero size.
func (a *Tridiag) Norm(norm float64) float64 {
if a.IsEmpty() {
panic(ErrZeroLength)

MODIFY
mat/vector.go
mat/vector.go
@@ -269,7 +269,8 @@ func (v *VecDense) CopyVec(a Vector) int {
// 2 - The Euclidean norm, the square root of the sum of the squares of the elements
// Inf - The maximum element magnitude
//
-// Norm will panic with ErrNormOrder if an illegal norm is specified.
+// Norm will panic with ErrNormOrder if an illegal norm is specified and with
+// ErrZeroLength if the vector has zero size.
func (v *VecDense) Norm(norm float64) float64 {
if v.IsEmpty() {
panic(ErrZeroLength)

mat: return ErrZeroLength when size is zero and document it