github.com/gonum/lapack@v0.0.0-20181123203213-e4cdc5a0bff9/testlapack/dlantr.go (about)

     1  // Copyright ©2015 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  	"math/rand"
    10  	"testing"
    11  
    12  	"github.com/gonum/blas"
    13  	"github.com/gonum/lapack"
    14  )
    15  
    16  type Dlantrer interface {
    17  	Dlanger
    18  	Dlantr(norm lapack.MatrixNorm, uplo blas.Uplo, diag blas.Diag, m, n int, a []float64, lda int, work []float64) float64
    19  }
    20  
    21  func DlantrTest(t *testing.T, impl Dlantrer) {
    22  	rnd := rand.New(rand.NewSource(1))
    23  	for _, norm := range []lapack.MatrixNorm{lapack.MaxAbs, lapack.MaxColumnSum, lapack.MaxRowSum, lapack.NormFrob} {
    24  		for _, diag := range []blas.Diag{blas.NonUnit, blas.Unit} {
    25  			for _, uplo := range []blas.Uplo{blas.Lower, blas.Upper} {
    26  				for _, test := range []struct {
    27  					m, n, lda int
    28  				}{
    29  					{3, 3, 0},
    30  					{3, 5, 0},
    31  					{10, 5, 0},
    32  
    33  					{5, 5, 11},
    34  					{5, 10, 11},
    35  					{10, 5, 11},
    36  				} {
    37  					// Do a couple of random trials since the values change.
    38  					for trial := 0; trial < 100; trial++ {
    39  						m := test.m
    40  						n := test.n
    41  						lda := test.lda
    42  						if lda == 0 {
    43  							lda = n
    44  						}
    45  						a := make([]float64, m*lda)
    46  						if trial == 0 {
    47  							for i := range a {
    48  								a[i] = float64(i)
    49  							}
    50  						} else {
    51  							for i := range a {
    52  								a[i] = rnd.NormFloat64()
    53  							}
    54  						}
    55  						aDense := make([]float64, len(a))
    56  						if uplo == blas.Lower {
    57  							for i := 0; i < m; i++ {
    58  								for j := 0; j <= min(i, n-1); j++ {
    59  									aDense[i*lda+j] = a[i*lda+j]
    60  								}
    61  							}
    62  						} else {
    63  							for i := 0; i < m; i++ {
    64  								for j := i; j < n; j++ {
    65  									aDense[i*lda+j] = a[i*lda+j]
    66  								}
    67  							}
    68  						}
    69  						if diag == blas.Unit {
    70  							for i := 0; i < min(m, n); i++ {
    71  								aDense[i*lda+i] = 1
    72  							}
    73  						}
    74  						work := make([]float64, n+6)
    75  						for i := range work {
    76  							work[i] = rnd.Float64()
    77  						}
    78  						got := impl.Dlantr(norm, uplo, diag, m, n, a, lda, work)
    79  						want := impl.Dlange(norm, m, n, aDense, lda, work)
    80  						if math.Abs(got-want) > 1e-13 {
    81  							t.Errorf("Norm mismatch. norm = %c, unitdiag = %v, upper = %v, m = %v, n = %v, lda = %v, Want %v, got %v.",
    82  								norm, diag == blas.Unit, uplo == blas.Upper, m, n, lda, got, want)
    83  						}
    84  					}
    85  				}
    86  			}
    87  		}
    88  	}
    89  }