github.com/gopherd/gonum@v0.0.4/blas/testblas/zgbmv.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  	"math/rand"
    12  	"github.com/gopherd/gonum/blas"
    13  )
    14  
    15  type Zgbmver interface {
    16  	Zgbmv(trans blas.Transpose, m, n, kL, kU int, alpha complex128, ab []complex128, ldab int, x []complex128, incX int, beta complex128, y []complex128, incY int)
    17  
    18  	Zgemver
    19  }
    20  
    21  func ZgbmvTest(t *testing.T, impl Zgbmver) {
    22  	rnd := rand.New(rand.NewSource(1))
    23  	for _, trans := range []blas.Transpose{blas.NoTrans, blas.Trans, blas.ConjTrans} {
    24  		// Generate all possible size combinations.
    25  		for _, mn := range allPairs([]int{1, 2, 3, 5}, []int{1, 2, 3, 5}) {
    26  			m := mn[0]
    27  			n := mn[1]
    28  			// Generate all possible numbers of lower and upper
    29  			// diagonals. Use slices to reduce indentation.
    30  			kLs := make([]int, max(1, m))
    31  			for i := range kLs {
    32  				kLs[i] = i
    33  			}
    34  			kUs := make([]int, max(1, n))
    35  			for i := range kUs {
    36  				kUs[i] = i
    37  			}
    38  			for _, ks := range allPairs(kLs, kUs) {
    39  				kL := ks[0]
    40  				kU := ks[1]
    41  				for _, ab := range []struct {
    42  					alpha complex128
    43  					beta  complex128
    44  				}{
    45  					// All potentially relevant values of
    46  					// alpha and beta.
    47  					{0, 0},
    48  					{0, 1},
    49  					{0, complex(rnd.NormFloat64(), rnd.NormFloat64())},
    50  					{complex(rnd.NormFloat64(), rnd.NormFloat64()), 0},
    51  					{complex(rnd.NormFloat64(), rnd.NormFloat64()), 1},
    52  					{complex(rnd.NormFloat64(), rnd.NormFloat64()), complex(rnd.NormFloat64(), rnd.NormFloat64())},
    53  				} {
    54  					for _, ldab := range []int{kL + kU + 1, kL + kU + 20} {
    55  						for _, inc := range allPairs([]int{-3, -2, -1, 1, 2, 3}, []int{-3, -2, -1, 1, 2, 3}) {
    56  							incX := inc[0]
    57  							incY := inc[1]
    58  							testZgbmv(t, impl, rnd, trans, m, n, kL, kU, ab.alpha, ab.beta, ldab, incX, incY)
    59  						}
    60  					}
    61  				}
    62  			}
    63  		}
    64  	}
    65  }
    66  
    67  // testZgbmv tests Zgbmv by comparing its output to that of Zgemv.
    68  func testZgbmv(t *testing.T, impl Zgbmver, rnd *rand.Rand, trans blas.Transpose, m, n, kL, kU int, alpha, beta complex128, ldab, incX, incY int) {
    69  	const tol = 1e-13
    70  
    71  	// Allocate a dense-storage band matrix filled with NaNs that will be
    72  	// used as the reference matrix for Zgemv.
    73  	lda := max(1, n)
    74  	a := makeZGeneral(nil, m, n, lda)
    75  	// Fill the matrix with zeros.
    76  	for i := 0; i < m; i++ {
    77  		for j := 0; j < n; j++ {
    78  			a[i*lda+j] = 0
    79  		}
    80  	}
    81  	// Fill the band with random data.
    82  	for i := 0; i < m; i++ {
    83  		for j := max(0, i-kL); j < min(n, i+kU+1); j++ {
    84  			re := rnd.NormFloat64()
    85  			im := rnd.NormFloat64()
    86  			a[i*lda+j] = complex(re, im)
    87  		}
    88  	}
    89  	// Create the actual band matrix.
    90  	ab := zPackBand(kL, kU, ldab, m, n, a, lda)
    91  	abCopy := make([]complex128, len(ab))
    92  	copy(abCopy, ab)
    93  
    94  	// Compute correct lengths of vectors x and y.
    95  	var lenX, lenY int
    96  	switch trans {
    97  	case blas.NoTrans:
    98  		lenX = n
    99  		lenY = m
   100  	case blas.Trans, blas.ConjTrans:
   101  		lenX = m
   102  		lenY = n
   103  	}
   104  
   105  	// Generate a random complex vector x.
   106  	xtest := make([]complex128, lenX)
   107  	for i := range xtest {
   108  		re := rnd.NormFloat64()
   109  		im := rnd.NormFloat64()
   110  		xtest[i] = complex(re, im)
   111  	}
   112  	x := makeZVector(xtest, incX)
   113  	xCopy := make([]complex128, len(x))
   114  	copy(xCopy, x)
   115  
   116  	// Generate a random complex vector y.
   117  	ytest := make([]complex128, lenY)
   118  	for i := range ytest {
   119  		re := rnd.NormFloat64()
   120  		im := rnd.NormFloat64()
   121  		ytest[i] = complex(re, im)
   122  	}
   123  	y := makeZVector(ytest, incY)
   124  
   125  	want := make([]complex128, len(y))
   126  	copy(want, y)
   127  
   128  	// Compute the reference result of alpha*op(A)*x + beta*y, storing it
   129  	// into want.
   130  	impl.Zgemv(trans, m, n, alpha, a, lda, x, incX, beta, want, incY)
   131  	// Compute alpha*op(A)*x + beta*y, storing the result in-place into y.
   132  	impl.Zgbmv(trans, m, n, kL, kU, alpha, ab, ldab, x, incX, beta, y, incY)
   133  
   134  	name := fmt.Sprintf("trans=%v,m=%v,n=%v,kL=%v,kU=%v,lda=%v,incX=%v,incY=%v", trans, m, n, kL, kU, lda, incX, incY)
   135  	if !zsame(ab, abCopy) {
   136  		t.Errorf("%v: unexpected modification of ab", name)
   137  	}
   138  	if !zsame(x, xCopy) {
   139  		t.Errorf("%v: unexpected modification of x", name)
   140  	}
   141  	if !zSameAtNonstrided(y, want, incY) {
   142  		t.Errorf("%v: unexpected modification of y", name)
   143  	}
   144  	if !zEqualApproxAtStrided(y, want, incY, tol) {
   145  		t.Errorf("%v: unexpected result\ngot  %v\nwant %v\n", name, y, want)
   146  	}
   147  }