github.com/gopherd/gonum@v0.0.4/lapack/testlapack/dpbtrf.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 "testing" 10 11 "math/rand" 12 13 "github.com/gopherd/gonum/blas" 14 "github.com/gopherd/gonum/blas/blas64" 15 ) 16 17 type Dpbtrfer interface { 18 Dpbtrf(uplo blas.Uplo, n, kd int, ab []float64, ldab int) (ok bool) 19 } 20 21 // DpbtrfTest tests a band Cholesky factorization on random symmetric positive definite 22 // band matrices by checking that the Cholesky factors multiply back to the original matrix. 23 func DpbtrfTest(t *testing.T, impl Dpbtrfer) { 24 // TODO(vladimir-ch): include expected-failure test case. 25 26 // With the current implementation of Ilaenv the blocked code path is taken if kd > 64. 27 // Unfortunately, with the block size nb=32 this also means that in Dpbtrf 28 // it never happens that i2 <= 0 and the state coverage (unlike code coverage) is not complete. 29 rnd := rand.New(rand.NewSource(1)) 30 for _, n := range []int{0, 1, 2, 3, 4, 5, 64, 65, 66, 91, 96, 97, 101, 128, 130} { 31 for _, kd := range []int{0, (n + 1) / 4, (3*n - 1) / 4, (5*n + 1) / 4} { 32 for _, uplo := range []blas.Uplo{blas.Upper, blas.Lower} { 33 for _, ldab := range []int{kd + 1, kd + 1 + 7} { 34 dpbtrfTest(t, impl, uplo, n, kd, ldab, rnd) 35 } 36 } 37 } 38 } 39 } 40 41 func dpbtrfTest(t *testing.T, impl Dpbtrfer, uplo blas.Uplo, n, kd int, ldab int, rnd *rand.Rand) { 42 const tol = 1e-12 43 44 name := fmt.Sprintf("uplo=%v,n=%v,kd=%v,ldab=%v", string(uplo), n, kd, ldab) 45 46 // Generate a random symmetric positive definite band matrix. 47 ab := randSymBand(uplo, n, kd, ldab, rnd) 48 49 // Compute the Cholesky decomposition of A. 50 abFac := make([]float64, len(ab)) 51 copy(abFac, ab) 52 ok := impl.Dpbtrf(uplo, n, kd, abFac, ldab) 53 if !ok { 54 t.Fatalf("%v: bad test matrix, Dpbtrf failed", name) 55 } 56 57 // Reconstruct an symmetric band matrix from the Uᵀ*U or L*Lᵀ factorization, overwriting abFac. 58 dsbmm(uplo, n, kd, abFac, ldab) 59 60 // Compute and check the max-norm distance between the reconstructed and original matrix A. 61 dist := distSymBand(uplo, n, kd, abFac, ldab, ab, ldab) 62 if dist > tol*float64(n) { 63 t.Errorf("%v: unexpected result, diff=%v", name, dist) 64 } 65 } 66 67 // dsbmm computes a symmetric band matrix A 68 // A = Uᵀ*U if uplo == blas.Upper, 69 // A = L*Lᵀ if uplo == blas.Lower, 70 // where U and L is an upper, respectively lower, triangular band matrix 71 // stored on entry in ab. The result is stored in-place into ab. 72 func dsbmm(uplo blas.Uplo, n, kd int, ab []float64, ldab int) { 73 bi := blas64.Implementation() 74 switch uplo { 75 case blas.Upper: 76 // Compute the product Uᵀ * U. 77 for k := n - 1; k >= 0; k-- { 78 klen := min(kd, n-k-1) // Number of stored off-diagonal elements in the row 79 // Add a multiple of row k of the factor U to each of rows k+1 through n. 80 if klen > 0 { 81 bi.Dsyr(blas.Upper, klen, 1, ab[k*ldab+1:], 1, ab[(k+1)*ldab:], ldab-1) 82 } 83 // Scale row k by the diagonal element. 84 bi.Dscal(klen+1, ab[k*ldab], ab[k*ldab:], 1) 85 } 86 case blas.Lower: 87 // Compute the product L * Lᵀ. 88 for k := n - 1; k >= 0; k-- { 89 kc := max(0, kd-k) // Index of the first valid element in the row 90 klen := kd - kc // Number of stored off-diagonal elements in the row 91 // Compute the diagonal [k,k] element. 92 ab[k*ldab+kd] = bi.Ddot(klen+1, ab[k*ldab+kc:], 1, ab[k*ldab+kc:], 1) 93 // Compute the rest of column k. 94 if klen > 0 { 95 bi.Dtrmv(blas.Lower, blas.NoTrans, blas.NonUnit, klen, 96 ab[(k-klen)*ldab+kd:], ldab-1, ab[k*ldab+kc:], 1) 97 } 98 } 99 } 100 }