gonum.org/v1/gonum@v0.14.0/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  	"golang.org/x/exp/rand"
    12  
    13  	"gonum.org/v1/gonum/blas"
    14  	"gonum.org/v1/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  //
    69  //	A = Uᵀ*U  if uplo == blas.Upper,
    70  //	A = L*Lᵀ  if uplo == blas.Lower,
    71  //
    72  // where U and L is an upper, respectively lower, triangular band matrix
    73  // stored on entry in ab. The result is stored in-place into ab.
    74  func dsbmm(uplo blas.Uplo, n, kd int, ab []float64, ldab int) {
    75  	bi := blas64.Implementation()
    76  	switch uplo {
    77  	case blas.Upper:
    78  		// Compute the product Uᵀ * U.
    79  		for k := n - 1; k >= 0; k-- {
    80  			klen := min(kd, n-k-1) // Number of stored off-diagonal elements in the row
    81  			// Add a multiple of row k of the factor U to each of rows k+1 through n.
    82  			if klen > 0 {
    83  				bi.Dsyr(blas.Upper, klen, 1, ab[k*ldab+1:], 1, ab[(k+1)*ldab:], ldab-1)
    84  			}
    85  			// Scale row k by the diagonal element.
    86  			bi.Dscal(klen+1, ab[k*ldab], ab[k*ldab:], 1)
    87  		}
    88  	case blas.Lower:
    89  		// Compute the product L * Lᵀ.
    90  		for k := n - 1; k >= 0; k-- {
    91  			kc := max(0, kd-k) // Index of the first valid element in the row
    92  			klen := kd - kc    // Number of stored off-diagonal elements in the row
    93  			// Compute the diagonal [k,k] element.
    94  			ab[k*ldab+kd] = bi.Ddot(klen+1, ab[k*ldab+kc:], 1, ab[k*ldab+kc:], 1)
    95  			// Compute the rest of column k.
    96  			if klen > 0 {
    97  				bi.Dtrmv(blas.Lower, blas.NoTrans, blas.NonUnit, klen,
    98  					ab[(k-klen)*ldab+kd:], ldab-1, ab[k*ldab+kc:], 1)
    99  			}
   100  		}
   101  	}
   102  }