diff --git a/mat64/matrix.go b/mat64/matrix.go index 3f64388..a1f5d7b 100644 --- a/mat64/matrix.go +++ b/mat64/matrix.go @@ -695,6 +695,9 @@ func Norm(a Matrix, norm float64) float64 { } return lapack64.Lansy(n, rm, work) case *Vector: + if rma.isZero() { + return 0 + } rv := rma.RawVector() switch norm { default: diff --git a/mat64/matrix_test.go b/mat64/matrix_test.go index ca340e6..56fef75 100644 --- a/mat64/matrix_test.go +++ b/mat64/matrix_test.go @@ -8,6 +8,9 @@ import ( "fmt" "math" "testing" + + "github.com/gonum/blas" + "github.com/gonum/blas/blas64" ) var inf = math.Inf(1) @@ -253,6 +256,22 @@ func TestNorm(t *testing.T) { testOneInputFunc(t, "Norm_inf", f, denseComparison, sameAnswerFloatApprox, isAnyType, isAnySize) } +func TestNormZero(t *testing.T) { + for _, a := range []Matrix{ + &Dense{}, + &SymDense{mat: blas64.Symmetric{Uplo: blas.Upper}}, + &TriDense{mat: blas64.Triangular{Uplo: blas.Upper, Diag: blas.NonUnit}}, + &Vector{}, + } { + for _, norm := range []float64{1, 2, math.Inf(1)} { + panicked, message := panics(func() { Norm(a, norm) }) + if panicked { + t.Errorf("unexpected panic for Norm(&%T{}, %v): %v", a, norm, message) + } + } + } +} + func TestSum(t *testing.T) { f := func(a Matrix) interface{} { return Sum(a)