gonum.org/v1/gonum@v0.14.0/lapack/testlapack/dpbtrs.go (about) 1 // Copyright ©2019 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/blas/blas64" 16 "gonum.org/v1/gonum/floats" 17 ) 18 19 type Dpbtrser interface { 20 Dpbtrs(uplo blas.Uplo, n, kd, nrhs int, ab []float64, ldab int, b []float64, ldb int) 21 22 Dpbtrfer 23 } 24 25 // DpbtrsTest tests Dpbtrs by comparing the computed and known, generated solutions of 26 // a linear system with a random symmetric positive definite band matrix. 27 func DpbtrsTest(t *testing.T, impl Dpbtrser) { 28 rnd := rand.New(rand.NewSource(1)) 29 for _, n := range []int{0, 1, 2, 3, 4, 5, 65, 100, 129} { 30 for _, kd := range []int{0, (n + 1) / 4, (3*n - 1) / 4, (5*n + 1) / 4} { 31 for _, nrhs := range []int{0, 1, 2, 5} { 32 for _, uplo := range []blas.Uplo{blas.Upper, blas.Lower} { 33 for _, ldab := range []int{kd + 1, kd + 1 + 3} { 34 for _, ldb := range []int{max(1, nrhs), nrhs + 4} { 35 dpbtrsTest(t, impl, rnd, uplo, n, kd, nrhs, ldab, ldb) 36 } 37 } 38 } 39 } 40 } 41 } 42 } 43 44 func dpbtrsTest(t *testing.T, impl Dpbtrser, rnd *rand.Rand, uplo blas.Uplo, n, kd, nrhs int, ldab, ldb int) { 45 const tol = 1e-12 46 47 name := fmt.Sprintf("uplo=%v,n=%v,kd=%v,nrhs=%v,ldab=%v,ldb=%v", string(uplo), n, kd, nrhs, ldab, ldb) 48 49 // Generate a random symmetric positive definite band matrix. 50 ab := randSymBand(uplo, n, kd, ldab, rnd) 51 52 // Compute the Cholesky decomposition of A. 53 abFac := make([]float64, len(ab)) 54 copy(abFac, ab) 55 ok := impl.Dpbtrf(uplo, n, kd, abFac, ldab) 56 if !ok { 57 t.Fatalf("%v: bad test matrix, Dpbtrs failed", name) 58 } 59 abFacCopy := make([]float64, len(abFac)) 60 copy(abFacCopy, abFac) 61 62 // Generate a random solution. 63 xWant := make([]float64, n*ldb) 64 for i := range xWant { 65 xWant[i] = rnd.NormFloat64() 66 } 67 68 // Compute the corresponding right-hand side. 69 bi := blas64.Implementation() 70 b := make([]float64, len(xWant)) 71 if n > 0 { 72 for j := 0; j < nrhs; j++ { 73 bi.Dsbmv(uplo, n, kd, 1, ab, ldab, xWant[j:], ldb, 0, b[j:], ldb) 74 } 75 } 76 77 // Solve Uᵀ * U * X = B or L * Lᵀ * X = B. 78 impl.Dpbtrs(uplo, n, kd, nrhs, abFac, ldab, b, ldb) 79 xGot := b 80 81 // Check that the Cholesky factorization matrix has not been modified. 82 if !floats.Equal(abFac, abFacCopy) { 83 t.Errorf("%v: unexpected modification of ab", name) 84 } 85 86 // Compute and check the max-norm difference between the computed and generated solutions. 87 var diff float64 88 for i := 0; i < n; i++ { 89 for j := 0; j < nrhs; j++ { 90 diff = math.Max(diff, math.Abs(xWant[i*ldb+j]-xGot[i*ldb+j])) 91 } 92 } 93 if diff > tol { 94 t.Errorf("%v: unexpected result, diff=%v", name, diff) 95 } 96 }