gonum.org/v1/gonum@v0.14.0/lapack/testlapack/dlantb.go (about) 1 // Copyright ©2020 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 "fmt" 9 "math" 10 "testing" 11 12 "golang.org/x/exp/rand" 13 14 "gonum.org/v1/gonum/blas" 15 "gonum.org/v1/gonum/floats" 16 "gonum.org/v1/gonum/lapack" 17 ) 18 19 type Dlantber interface { 20 Dlantb(norm lapack.MatrixNorm, uplo blas.Uplo, diag blas.Diag, n, k int, a []float64, lda int, work []float64) float64 21 } 22 23 func DlantbTest(t *testing.T, impl Dlantber) { 24 rnd := rand.New(rand.NewSource(1)) 25 for _, norm := range []lapack.MatrixNorm{lapack.MaxAbs, lapack.MaxRowSum, lapack.MaxColumnSum, lapack.Frobenius} { 26 for _, uplo := range []blas.Uplo{blas.Lower, blas.Upper} { 27 for _, diag := range []blas.Diag{blas.NonUnit, blas.Unit} { 28 name := normToString(norm) + uploToString(uplo) + diagToString(diag) 29 t.Run(name, func(t *testing.T) { 30 for _, n := range []int{0, 1, 2, 3, 4, 5, 10} { 31 for _, k := range []int{0, 1, 2, 3, n, n + 2} { 32 for _, lda := range []int{k + 1, k + 3} { 33 for iter := 0; iter < 10; iter++ { 34 dlantbTest(t, impl, rnd, norm, uplo, diag, n, k, lda) 35 } 36 } 37 } 38 } 39 }) 40 } 41 } 42 } 43 } 44 45 func dlantbTest(t *testing.T, impl Dlantber, rnd *rand.Rand, norm lapack.MatrixNorm, uplo blas.Uplo, diag blas.Diag, n, k, lda int) { 46 const tol = 1e-14 47 48 name := fmt.Sprintf("n=%v,k=%v,lda=%v", n, k, lda) 49 50 // Deal with zero-sized matrices early. 51 if n == 0 { 52 got := impl.Dlantb(norm, uplo, diag, n, k, nil, lda, nil) 53 if got != 0 { 54 t.Errorf("%v: unexpected result for zero-sized matrix", name) 55 } 56 return 57 } 58 59 a := make([]float64, max(0, (n-1)*lda+k+1)) 60 if rnd.Float64() < 0.5 { 61 // Sometimes fill A with elements between -0.5 and 0.5 so that for 62 // blas.Unit matrices the largest element is the 1 on the main diagonal. 63 for i := range a { 64 // Between -0.5 and 0.5. 65 a[i] = rnd.Float64() - 0.5 66 } 67 } else { 68 for i := range a { 69 // Between -2 and 2. 70 a[i] = 4*rnd.Float64() - 2 71 } 72 } 73 // Sometimes put a NaN into A. 74 if rnd.Float64() < 0.5 { 75 a[rnd.Intn(len(a))] = math.NaN() 76 } 77 // Make a copy of A for later comparison. 78 aCopy := make([]float64, len(a)) 79 copy(aCopy, a) 80 81 var work []float64 82 if norm == lapack.MaxColumnSum { 83 work = make([]float64, n) 84 } 85 // Fill work with random garbage. 86 for i := range work { 87 work[i] = rnd.NormFloat64() 88 } 89 90 got := impl.Dlantb(norm, uplo, diag, n, k, a, lda, work) 91 92 if !floats.Same(a, aCopy) { 93 t.Fatalf("%v: unexpected modification of a", name) 94 } 95 96 // Generate a dense representation of A and compute the wanted result. 97 ldaGen := n 98 aGen := make([]float64, n*ldaGen) 99 if uplo == blas.Upper { 100 for i := 0; i < n; i++ { 101 for j := 0; j < min(n-i, k+1); j++ { 102 aGen[i*ldaGen+i+j] = a[i*lda+j] 103 } 104 } 105 } else { 106 for i := 0; i < n; i++ { 107 for j := max(0, k-i); j < k+1; j++ { 108 aGen[i*ldaGen+i-(k-j)] = a[i*lda+j] 109 } 110 } 111 } 112 if diag == blas.Unit { 113 for i := 0; i < n; i++ { 114 aGen[i*ldaGen+i] = 1 115 } 116 } 117 want := dlange(norm, n, n, aGen, ldaGen) 118 119 if math.IsNaN(want) { 120 if !math.IsNaN(got) { 121 t.Errorf("%v: unexpected result with NaN element; got %v, want %v", name, got, want) 122 } 123 return 124 } 125 126 if norm == lapack.MaxAbs { 127 if got != want { 128 t.Errorf("%v: unexpected result; got %v, want %v", name, got, want) 129 } 130 return 131 } 132 diff := math.Abs(got - want) 133 if diff > tol { 134 t.Errorf("%v: unexpected result; got %v, want %v, diff=%v", name, got, want, diff) 135 } 136 }