gonum.org/v1/gonum@v0.14.0/lapack/testlapack/dlanst.go (about)

     1  // Copyright ©2016 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package testlapack
     6  
     7  import (
     8  	"math"
     9  	"testing"
    10  
    11  	"golang.org/x/exp/rand"
    12  
    13  	"gonum.org/v1/gonum/lapack"
    14  )
    15  
    16  type Dlanster interface {
    17  	Dlanst(norm lapack.MatrixNorm, n int, d, e []float64) float64
    18  	Dlanger
    19  }
    20  
    21  func DlanstTest(t *testing.T, impl Dlanster) {
    22  	rnd := rand.New(rand.NewSource(1))
    23  	for _, norm := range []lapack.MatrixNorm{lapack.MaxAbs, lapack.MaxColumnSum, lapack.MaxRowSum, lapack.Frobenius} {
    24  		for _, n := range []int{1, 3, 10, 100} {
    25  			for cas := 0; cas < 100; cas++ {
    26  				// Generate randomly the main diagonal of the
    27  				// symmetric tridiagonal matrix A.
    28  				d := make([]float64, n)
    29  				for i := range d {
    30  					d[i] = rnd.NormFloat64()
    31  				}
    32  				// Generate randomly the off-diagonal of A.
    33  				e := make([]float64, n-1)
    34  				for i := range e {
    35  					e[i] = rnd.NormFloat64()
    36  				}
    37  
    38  				// Create A in dense representation.
    39  				m := n
    40  				lda := n
    41  				a := make([]float64, m*lda)
    42  				for i := 0; i < n; i++ {
    43  					a[i*lda+i] = d[i]
    44  				}
    45  				for i := 0; i < n-1; i++ {
    46  					a[i*lda+i+1] = e[i]
    47  					a[(i+1)*lda+i] = e[i]
    48  				}
    49  
    50  				work := make([]float64, n)
    51  				// Compute a norm of A using Dlanst.
    52  				syNorm := impl.Dlanst(norm, n, d, e)
    53  				// Compute a reference value for the norm using
    54  				// Dlange and the dense representation of A.
    55  				geNorm := impl.Dlange(norm, m, n, a, lda, work)
    56  				if math.Abs(syNorm-geNorm) > 1e-12 {
    57  					t.Errorf("Norm mismatch: norm = %v, cas = %v, n = %v. Want %v, got %v.", string(norm), cas, n, geNorm, syNorm)
    58  				}
    59  			}
    60  		}
    61  	}
    62  }