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
|
// Copyright ©2020 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 testlapack
import (
"fmt"
"math"
"testing"
"golang.org/x/exp/rand"
"gonum.org/v1/gonum/floats"
"gonum.org/v1/gonum/lapack"
)
type Dlangter interface {
Dlangt(norm lapack.MatrixNorm, n int, dl, d, du []float64) float64
}
func DlangtTest(t *testing.T, impl Dlangter) {
rnd := rand.New(rand.NewSource(1))
for _, norm := range []lapack.MatrixNorm{lapack.MaxAbs, lapack.MaxRowSum, lapack.MaxColumnSum, lapack.Frobenius} {
t.Run(normToString(norm), func(t *testing.T) {
for _, n := range []int{0, 1, 2, 3, 4, 5, 10} {
for iter := 0; iter < 10; iter++ {
dlangtTest(t, impl, rnd, norm, n)
}
}
})
}
}
func dlangtTest(t *testing.T, impl Dlangter, rnd *rand.Rand, norm lapack.MatrixNorm, n int) {
const (
tol = 1e-14
extra = 10
)
name := fmt.Sprintf("n=%v", n)
// Generate three random diagonals.
dl := randomSlice(n+extra, rnd)
dlCopy := make([]float64, len(dl))
copy(dlCopy, dl)
d := randomSlice(n+1+extra, rnd)
// Sometimes put a NaN into the matrix.
if n > 0 && rnd.Float64() < 0.5 {
d[rnd.Intn(n)] = math.NaN()
}
dCopy := make([]float64, len(d))
copy(dCopy, d)
du := randomSlice(n+extra, rnd)
duCopy := make([]float64, len(du))
copy(duCopy, du)
// Deal with zero-sized matrices early.
if n == 0 {
got := impl.Dlangt(norm, n, nil, nil, nil)
if got != 0 {
t.Errorf("%v: unexpected result for zero-sized matrix with nil input", name)
}
got = impl.Dlangt(norm, n, dl, d, du)
if !floats.Same(dl, dlCopy) {
t.Errorf("%v: unexpected modification in dl", name)
}
if !floats.Same(d, dCopy) {
t.Errorf("%v: unexpected modification in d", name)
}
if !floats.Same(du, duCopy) {
t.Errorf("%v: unexpected modification in du", name)
}
if got != 0 {
t.Errorf("%v: unexpected result for zero-sized matrix with non-nil input", name)
}
return
}
// Generate a dense representation of the matrix and compute the wanted result.
a := zeros(n, n, n)
for i := 0; i < n-1; i++ {
a.Data[i*a.Stride+i] = d[i]
a.Data[i*a.Stride+i+1] = du[i]
a.Data[(i+1)*a.Stride+i] = dl[i]
}
a.Data[(n-1)*a.Stride+n-1] = d[n-1]
got := impl.Dlangt(norm, n, dl, d, du)
if !floats.Same(dl, dlCopy) {
t.Errorf("%v: unexpected modification in dl", name)
}
if !floats.Same(d, dCopy) {
t.Errorf("%v: unexpected modification in d", name)
}
if !floats.Same(du, duCopy) {
t.Errorf("%v: unexpected modification in du", name)
}
want := dlange(norm, n, n, a.Data, a.Stride)
if math.IsNaN(want) {
if !math.IsNaN(got) {
t.Errorf("%v: unexpected result with NaN element; got %v, want %v", name, got, want)
}
return
}
if norm == lapack.MaxAbs {
if got != want {
t.Errorf("%v: unexpected result; got %v, want %v", name, got, want)
}
return
}
diff := math.Abs(got - want)
if diff > tol {
t.Errorf("%v: unexpected result; got %v, want %v, diff=%v", name, got, want, diff)
}
}
|