1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
// Copyright ©2018 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package testblas
import (
"fmt"
"math"
"testing"
"golang.org/x/exp/rand"
"gonum.org/v1/gonum/blas"
)
type Zhbmver interface {
Zhbmv(uplo blas.Uplo, n, k int, alpha complex128, ab []complex128, ldab int, x []complex128, incX int, beta complex128, y []complex128, incY int)
Zhemver
}
func ZhbmvTest(t *testing.T, impl Zhbmver) {
rnd := rand.New(rand.NewSource(1))
for _, uplo := range []blas.Uplo{blas.Upper, blas.Lower} {
for _, n := range []int{0, 1, 2, 3, 5} {
for k := 0; k < n; k++ {
for _, ldab := range []int{k + 1, k + 1 + 10} {
// Generate all possible combinations of given increments.
// Use slices to reduce indentation.
for _, inc := range allPairs([]int{-11, 1, 7}, []int{-3, 1, 5}) {
incX := inc[0]
incY := inc[1]
for _, ab := range []struct {
alpha complex128
beta complex128
}{
// All potentially relevant values of
// alpha and beta.
{0, 0},
{0, 1},
{0, complex(rnd.NormFloat64(), rnd.NormFloat64())},
{complex(rnd.NormFloat64(), rnd.NormFloat64()), 0},
{complex(rnd.NormFloat64(), rnd.NormFloat64()), 1},
{complex(rnd.NormFloat64(), rnd.NormFloat64()), complex(rnd.NormFloat64(), rnd.NormFloat64())},
} {
testZhbmv(t, impl, rnd, uplo, n, k, ab.alpha, ab.beta, ldab, incX, incY)
}
}
}
}
}
}
}
// testZhbmv tests Zhbmv by comparing its output to that of Zhemv.
func testZhbmv(t *testing.T, impl Zhbmver, rnd *rand.Rand, uplo blas.Uplo, n, k int, alpha, beta complex128, ldab, incX, incY int) {
const tol = 1e-13
// Allocate a dense-storage Hermitian band matrix filled with NaNs that will be
// used as the reference matrix for Zhemv.
lda := max(1, n)
a := makeZGeneral(nil, n, n, lda)
// Fill the matrix with zeros.
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
a[i*lda+j] = 0
}
}
// Fill the triangle band with random data, invalidating the imaginary
// part of diagonal elements because it should not be referenced by
// Zhbmv and Zhemv.
if uplo == blas.Upper {
for i := 0; i < n; i++ {
a[i*lda+i] = complex(rnd.NormFloat64(), math.NaN())
for j := i + 1; j < min(n, i+k+1); j++ {
re := rnd.NormFloat64()
im := rnd.NormFloat64()
a[i*lda+j] = complex(re, im)
}
}
} else {
for i := 0; i < n; i++ {
for j := max(0, i-k); j < i; j++ {
re := rnd.NormFloat64()
im := rnd.NormFloat64()
a[i*lda+j] = complex(re, im)
}
a[i*lda+i] = complex(rnd.NormFloat64(), math.NaN())
}
}
// Create the actual Hermitian band matrix.
ab := zPackTriBand(k, ldab, uplo, n, a, lda)
abCopy := make([]complex128, len(ab))
copy(abCopy, ab)
// Generate a random complex vector x.
xtest := make([]complex128, n)
for i := range xtest {
re := rnd.NormFloat64()
im := rnd.NormFloat64()
xtest[i] = complex(re, im)
}
x := makeZVector(xtest, incX)
xCopy := make([]complex128, len(x))
copy(xCopy, x)
// Generate a random complex vector y.
ytest := make([]complex128, n)
for i := range ytest {
re := rnd.NormFloat64()
im := rnd.NormFloat64()
ytest[i] = complex(re, im)
}
y := makeZVector(ytest, incY)
want := make([]complex128, len(y))
copy(want, y)
// Compute the reference result of alpha*op(A)*x + beta*y, storing it
// into want.
impl.Zhemv(uplo, n, alpha, a, lda, x, incX, beta, want, incY)
// Compute alpha*op(A)*x + beta*y, storing the result in-place into y.
impl.Zhbmv(uplo, n, k, alpha, ab, ldab, x, incX, beta, y, incY)
prefix := fmt.Sprintf("uplo=%v,n=%v,k=%v,incX=%v,incY=%v,ldab=%v", uplo, n, k, incX, incY, ldab)
if !zsame(x, xCopy) {
t.Errorf("%v: unexpected modification of x", prefix)
}
if !zsame(ab, abCopy) {
t.Errorf("%v: unexpected modification of ab", prefix)
}
if !zSameAtNonstrided(y, want, incY) {
t.Errorf("%v: unexpected modification of y", prefix)
}
if !zEqualApproxAtStrided(y, want, incY, tol) {
t.Errorf("%v: unexpected result\nwant %v\ngot %v", prefix, want, y)
}
}
|