forked from gonum/gonum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsterf.go
240 lines (219 loc) · 5.7 KB
/
dsterf.go
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright ©2016 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"
"sort"
"testing"
"golang.org/x/exp/rand"
"gonum.org/v1/gonum/blas"
"gonum.org/v1/gonum/blas/blas64"
"gonum.org/v1/gonum/floats"
"gonum.org/v1/gonum/lapack"
)
type Dsterfer interface {
Dsteqrer
Dlansyer
Dsterf(n int, d, e []float64) (ok bool)
}
func DsterfTest(t *testing.T, impl Dsterfer) {
const tol = 1e-14
// Tests with precomputed eigenvalues.
for cas, test := range []struct {
d []float64
e []float64
n int
want []float64
}{
{
d: []float64{1, 3, 4, 6},
e: []float64{2, 4, 5},
n: 4,
// Computed from original Fortran code.
want: []float64{11.046227528488854, 4.795922173417400, -2.546379458290125, 0.704229756383872},
},
} {
n := test.n
got := make([]float64, len(test.d))
copy(got, test.d)
e := make([]float64, len(test.e))
copy(e, test.e)
ok := impl.Dsterf(n, got, e)
if !ok {
t.Errorf("Case %d, n=%v: Dsterf failed", cas, n)
continue
}
want := make([]float64, len(test.want))
copy(want, test.want)
sort.Float64s(want)
diff := floats.Distance(got, want, math.Inf(1))
if diff > tol {
t.Errorf("Case %d, n=%v: unexpected result, |dGot-dWant|=%v", cas, n, diff)
}
}
rnd := rand.New(rand.NewSource(1))
// Probabilistic tests.
for _, n := range []int{0, 1, 2, 3, 4, 5, 6, 10, 50} {
for typ := 0; typ <= 8; typ++ {
d := make([]float64, n)
var e []float64
if n > 1 {
e = make([]float64, n-1)
}
// Generate a tridiagonal matrix A.
switch typ {
case 0:
// The zero matrix.
case 1:
// The identity matrix.
for i := range d {
d[i] = 1
}
case 2:
// A diagonal matrix with evenly spaced entries
// 1, ..., eps and random signs.
for i := 0; i < n; i++ {
if i == 0 {
d[i] = 1
} else {
d[i] = 1 - (1-dlamchE)*float64(i)/float64(n-1)
}
if rnd.Float64() < 0.5 {
d[i] *= -1
}
}
case 3, 4, 5:
// A diagonal matrix with geometrically spaced entries
// 1, ..., eps and random signs.
for i := 0; i < n; i++ {
if i == 0 {
d[i] = 1
} else {
d[i] = math.Pow(dlamchE, float64(i)/float64(n-1))
}
if rnd.Float64() < 0.5 {
d[i] *= -1
}
}
switch typ {
case 4:
// Multiply by SQRT(overflow threshold).
floats.Scale(math.Sqrt(1/dlamchS), d)
case 5:
// Multiply by SQRT(underflow threshold).
floats.Scale(math.Sqrt(dlamchS), d)
}
case 6:
// A diagonal matrix with "clustered" entries 1, eps, ..., eps
// and random signs.
for i := range d {
if i == 0 {
d[i] = 1
} else {
d[i] = dlamchE
}
}
for i := range d {
if rnd.Float64() < 0.5 {
d[i] *= -1
}
}
case 7:
// Diagonal matrix with random entries.
for i := range d {
d[i] = rnd.NormFloat64()
}
case 8:
// Random symmetric tridiagonal matrix.
for i := range d {
d[i] = rnd.NormFloat64()
}
for i := range e {
e[i] = rnd.NormFloat64()
}
}
eCopy := make([]float64, len(e))
copy(eCopy, e)
name := fmt.Sprintf("n=%d,type=%d", n, typ)
// Compute the eigenvalues of A using Dsterf.
dGot := make([]float64, len(d))
copy(dGot, d)
ok := impl.Dsterf(n, dGot, e)
if !ok {
t.Errorf("%v: Dsterf failed", name)
continue
}
if n == 0 {
continue
}
// Test that the eigenvalues are sorted.
if !sort.Float64sAreSorted(dGot) {
t.Errorf("%v: eigenvalues are not sorted", name)
continue
}
// Compute the expected eigenvalues of A using Dsteqr.
dWant := make([]float64, len(d))
copy(dWant, d)
copy(e, eCopy)
z := nanGeneral(n, n, n)
ok = impl.Dsteqr(lapack.EVTridiag, n, dWant, e, z.Data, z.Stride, make([]float64, 2*n))
if !ok {
t.Errorf("%v: computing reference solution using Dsteqr failed", name)
continue
}
if resid := residualOrthogonal(z, false); resid > tol*float64(n) {
t.Errorf("%v: Z is not orthogonal; resid=%v, want<=%v", name, resid, tol*float64(n))
}
// Check whether eigenvalues from Dsteqr and Dsterf (which use
// different algorithms) are equal.
var diff, dMax float64
for i, di := range dGot {
diffAbs := math.Abs(di - dWant[i])
diff = math.Max(diff, diffAbs)
dAbs := math.Max(math.Abs(di), math.Abs(dWant[i]))
dMax = math.Max(dMax, dAbs)
}
dMax = math.Max(dlamchS, dMax)
if diff > tol*dMax {
t.Errorf("%v: unexpected result; |dGot-dWant|=%v", name, diff)
}
// Construct A as a symmetric dense matrix and compute its 1-norm.
copy(e, eCopy)
lda := n
a := make([]float64, n*lda)
var anorm, tmp float64
for i := 0; i < n-1; i++ {
a[i*lda+i] = d[i]
a[i*lda+i+1] = e[i]
tmp2 := math.Abs(e[i])
anorm = math.Max(anorm, math.Abs(d[i])+tmp+tmp2)
tmp = tmp2
}
a[(n-1)*lda+n-1] = d[n-1]
anorm = math.Max(anorm, math.Abs(d[n-1])+tmp)
// Compute A - Z D Zᵀ. The result should be the zero matrix.
bi := blas64.Implementation()
for i := 0; i < n; i++ {
bi.Dsyr(blas.Upper, n, -dGot[i], z.Data[i:], z.Stride, a, lda)
}
// Compute |A - Z D Zᵀ|.
wnorm := impl.Dlansy(lapack.MaxColumnSum, blas.Upper, n, a, lda, make([]float64, n))
// Compute diff := |A - Z D Zᵀ| / (|A| N).
if anorm > wnorm {
diff = wnorm / anorm / float64(n)
} else {
if anorm < 1 {
diff = math.Min(wnorm, float64(n)*anorm) / anorm / float64(n)
} else {
diff = math.Min(wnorm/anorm, float64(n)) / float64(n)
}
}
// Check whether diff is small.
if diff > tol {
t.Errorf("%v: unexpected result; |A - Z D Zᵀ|/(|A| n)=%v", name, diff)
}
}
}
}