gonum.org/v1/gonum@v0.14.0/blas/testblas/ztbmv.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 Ztbmver interface {
    16  	Ztbmv(uplo blas.Uplo, trans blas.Transpose, diag blas.Diag, n, k int, ab []complex128, ldab int, x []complex128, incX int)
    17  
    18  	Ztrmver
    19  }
    20  
    21  func ZtbmvTest(t *testing.T, impl Ztbmver) {
    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, 5} {
    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  								testZtbmv(t, impl, rnd, uplo, trans, diag, n, k, ldab, incX)
    31  							}
    32  						}
    33  					}
    34  				}
    35  			}
    36  		}
    37  	}
    38  }
    39  
    40  // testZtbmv tests Ztbmv by comparing its output to that of Ztrmv.
    41  func testZtbmv(t *testing.T, impl Ztbmver, rnd *rand.Rand, uplo blas.Uplo, trans blas.Transpose, diag blas.Diag, n, k, ldab, incX int) {
    42  	const tol = 1e-13
    43  
    44  	// Allocate a dense-storage triangular band matrix filled with NaNs that
    45  	// will be used as the reference matrix for Ztrmv.
    46  	lda := max(1, n)
    47  	a := makeZGeneral(nil, n, n, lda)
    48  	// Fill the referenced triangle with random data within the band and
    49  	// with zeros outside.
    50  	if uplo == blas.Upper {
    51  		for i := 0; i < n; i++ {
    52  			for j := i; j < min(n, i+k+1); j++ {
    53  				re := rnd.NormFloat64()
    54  				im := rnd.NormFloat64()
    55  				a[i*lda+j] = complex(re, im)
    56  			}
    57  			for j := i + k + 1; j < n; j++ {
    58  				a[i*lda+j] = 0
    59  			}
    60  		}
    61  	} else {
    62  		for i := 0; i < n; i++ {
    63  			for j := 0; j < i-k; j++ {
    64  				a[i*lda+j] = 0
    65  			}
    66  			for j := max(0, i-k); j <= i; j++ {
    67  				re := rnd.NormFloat64()
    68  				im := rnd.NormFloat64()
    69  				a[i*lda+j] = complex(re, im)
    70  			}
    71  		}
    72  	}
    73  	if diag == blas.Unit {
    74  		// The diagonal should not be referenced by Ztbmv and Ztrmv, so
    75  		// invalidate it with NaNs.
    76  		for i := 0; i < n; i++ {
    77  			a[i*lda+i] = znan
    78  		}
    79  	}
    80  	// Create the triangular band matrix.
    81  	ab := zPackTriBand(k, ldab, uplo, n, a, lda)
    82  	abCopy := make([]complex128, len(ab))
    83  	copy(abCopy, ab)
    84  
    85  	// Generate a random complex vector x.
    86  	xtest := make([]complex128, n)
    87  	for i := range xtest {
    88  		re := rnd.NormFloat64()
    89  		im := rnd.NormFloat64()
    90  		xtest[i] = complex(re, im)
    91  	}
    92  	x := makeZVector(xtest, incX)
    93  	xCopy := make([]complex128, len(x))
    94  	copy(xCopy, x)
    95  
    96  	want := make([]complex128, len(x))
    97  	copy(want, x)
    98  
    99  	// Compute the reference result of op(A)*x, storing it into want.
   100  	impl.Ztrmv(uplo, trans, diag, n, a, lda, want, incX)
   101  	// Compute op(A)*x, storing the result in-place into x.
   102  	impl.Ztbmv(uplo, trans, diag, n, k, ab, ldab, x, incX)
   103  
   104  	name := fmt.Sprintf("uplo=%v,trans=%v,diag=%v,n=%v,k=%v,ldab=%v,incX=%v", uplo, trans, diag, n, k, ldab, incX)
   105  	if !zsame(ab, abCopy) {
   106  		t.Errorf("%v: unexpected modification of ab", name)
   107  	}
   108  	if !zSameAtNonstrided(x, want, incX) {
   109  		t.Errorf("%v: unexpected modification of x", name)
   110  	}
   111  	if !zEqualApproxAtStrided(x, want, incX, tol) {
   112  		t.Errorf("%v: unexpected result\nwant %v\ngot  %v", name, want, x)
   113  	}
   114  }