gonum.org/v1/gonum@v0.14.0/blas/testblas/ztbsv.go (about)

     1  // Copyright ©2018 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 testblas
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  
    11  	"golang.org/x/exp/rand"
    12  	"gonum.org/v1/gonum/blas"
    13  )
    14  
    15  type Ztbsver interface {
    16  	Ztbsv(uplo blas.Uplo, trans blas.Transpose, diag blas.Diag, n, k int, ab []complex128, ldab int, x []complex128, incX int)
    17  
    18  	Ztbmver
    19  }
    20  
    21  func ZtbsvTest(t *testing.T, impl Ztbsver) {
    22  	rnd := rand.New(rand.NewSource(1))
    23  	for _, uplo := range []blas.Uplo{blas.Upper, blas.Lower} {
    24  		for _, trans := range []blas.Transpose{blas.NoTrans, blas.Trans, blas.ConjTrans} {
    25  			for _, diag := range []blas.Diag{blas.NonUnit, blas.Unit} {
    26  				for _, n := range []int{1, 2, 3, 4, 10} {
    27  					for k := 0; k < n; k++ {
    28  						for _, ldab := range []int{k + 1, k + 1 + 10} {
    29  							for _, incX := range []int{-4, 1, 5} {
    30  								ztbsvTest(t, impl, rnd, uplo, trans, diag, n, k, ldab, incX)
    31  							}
    32  						}
    33  					}
    34  				}
    35  			}
    36  		}
    37  	}
    38  }
    39  
    40  // ztbsvTest tests Ztbsv by checking whether Ztbmv followed by Ztbsv
    41  // round-trip.
    42  func ztbsvTest(t *testing.T, impl Ztbsver, rnd *rand.Rand, uplo blas.Uplo, trans blas.Transpose, diag blas.Diag, n, k, ldab, incX int) {
    43  	const tol = 1e-10
    44  
    45  	// Allocate a dense-storage triangular band matrix filled with NaNs that
    46  	// will be used as a for creating the actual triangular band matrix.
    47  	lda := max(1, n)
    48  	a := makeZGeneral(nil, n, n, lda)
    49  	// Fill the referenced triangle of A with random data within the band
    50  	// and with zeros outside.
    51  	if uplo == blas.Upper {
    52  		for i := 0; i < n; i++ {
    53  			for j := i; j < min(n, i+k+1); j++ {
    54  				re := rnd.NormFloat64()
    55  				im := rnd.NormFloat64()
    56  				a[i*lda+j] = complex(re, im)
    57  			}
    58  			for j := i + k + 1; j < n; j++ {
    59  				a[i*lda+j] = 0
    60  			}
    61  		}
    62  	} else {
    63  		for i := 0; i < n; i++ {
    64  			for j := 0; j < i-k; j++ {
    65  				a[i*lda+j] = 0
    66  			}
    67  			for j := max(0, i-k); j <= i; j++ {
    68  				re := rnd.NormFloat64()
    69  				im := rnd.NormFloat64()
    70  				a[i*lda+j] = complex(re, im)
    71  			}
    72  		}
    73  	}
    74  	if diag == blas.Unit {
    75  		// The diagonal should not be referenced by Ztbmv and Ztbsv, so
    76  		// invalidate it with NaNs.
    77  		for i := 0; i < n; i++ {
    78  			a[i*lda+i] = znan
    79  		}
    80  	}
    81  	// Create the triangular band matrix.
    82  	ab := zPackTriBand(k, ldab, uplo, n, a, lda)
    83  	abCopy := make([]complex128, len(ab))
    84  	copy(abCopy, ab)
    85  
    86  	// Generate a random complex vector x.
    87  	xtest := make([]complex128, n)
    88  	for i := range xtest {
    89  		re := rnd.NormFloat64()
    90  		im := rnd.NormFloat64()
    91  		xtest[i] = complex(re, im)
    92  	}
    93  	x := makeZVector(xtest, incX)
    94  
    95  	// Store a copy of x as the correct result that we want.
    96  	want := make([]complex128, len(x))
    97  	copy(want, x)
    98  
    99  	// Compute A*x, denoting the result by b and storing it in x.
   100  	impl.Ztbmv(uplo, trans, diag, n, k, ab, ldab, x, incX)
   101  	// Solve A*x = b, that is, x = A^{-1}*b = A^{-1}*A*x.
   102  	impl.Ztbsv(uplo, trans, diag, n, k, ab, ldab, x, incX)
   103  	// If Ztbsv is correct, A^{-1}*A = I and x contains again its original value.
   104  
   105  	name := fmt.Sprintf("uplo=%v,trans=%v,diag=%v,n=%v,k=%v,ldab=%v,incX=%v", uplo, trans, diag, n, k, ldab, incX)
   106  	if !zsame(ab, abCopy) {
   107  		t.Errorf("%v: unexpected modification of A", name)
   108  	}
   109  	if !zSameAtNonstrided(x, want, incX) {
   110  		t.Errorf("%v: unexpected modification of x\nwant %v\ngot  %v", name, want, x)
   111  	}
   112  	if !zEqualApproxAtStrided(x, want, incX, tol) {
   113  		t.Errorf("%v: unexpected result\nwant %v\ngot  %v", name, want, x)
   114  	}
   115  }