mat: add DiagDense.Norm

This commit is contained in:
Vladimir Chalupecky
2021-05-11 23:28:17 +02:00
committed by Vladimír Chalupecký
parent 39cb21da6b
commit 7d9d51f30f
2 changed files with 25 additions and 0 deletions

View File

@@ -5,6 +5,8 @@
package mat package mat
import ( import (
"math"
"gonum.org/v1/gonum/blas" "gonum.org/v1/gonum/blas"
"gonum.org/v1/gonum/blas/blas64" "gonum.org/v1/gonum/blas/blas64"
) )
@@ -324,3 +326,25 @@ func (d *DiagDense) Trace() float64 {
return tr return tr
} }
// Norm returns the specified norm of the receiver. Valid norms are:
// 1 or Inf - The maximum diagonal element magnitude
// 2 - The Frobenius norm, the square root of the sum of the squares of
// the diagonal elements
//
// Norm will panic with ErrNormOrder if an illegal norm is specified and with
// ErrZeroLength if the receiver has zero size.
func (d *DiagDense) Norm(norm float64) float64 {
if d.IsEmpty() {
panic(ErrZeroLength)
}
switch norm {
default:
panic(ErrNormOrder)
case 1, math.Inf(1):
imax := blas64.Iamax(d.mat)
return math.Abs(d.at(imax, imax))
case 2:
return blas64.Nrm2(d.mat)
}
}

View File

@@ -44,6 +44,7 @@ type allMatrix interface {
type denseMatrix interface { type denseMatrix interface {
DiagView() Diagonal DiagView() Diagonal
Tracer Tracer
Normer
} }
var ( var (