1 // Copyright ©2016 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.
12 "golang.org/x/exp/rand"
14 "gonum.org/v1/gonum/blas"
15 "gonum.org/v1/gonum/blas/blas64"
18 type Dlahqrer interface {
19 Dlahqr(wantt, wantz bool, n, ilo, ihi int, h []float64, ldh int, wr, wi []float64, iloz, ihiz int, z []float64, ldz int) int
22 type dlahqrTest struct {
28 evWant []complex128 // Optional slice holding known eigenvalues.
31 func DlahqrTest(t *testing.T, impl Dlahqrer) {
32 rnd := rand.New(rand.NewSource(1))
34 // Tests that choose the [ilo:ihi+1,ilo:ihi+1] and
35 // [iloz:ihiz+1,ilo:ihi+1] blocks randomly.
36 for _, wantt := range []bool{true, false} {
37 for _, wantz := range []bool{true, false} {
38 for _, n := range []int{1, 2, 3, 4, 5, 6, 10, 18, 31, 53} {
39 for _, extra := range []int{0, 1, 11} {
40 for cas := 0; cas < 100; cas++ {
46 iloz := rnd.Intn(ilo + 1)
47 ihiz := ihi + rnd.Intn(n-ihi)
48 h := randomHessenberg(n, n+extra, rnd)
50 h.Data[ilo*h.Stride+ilo-1] = 0
53 h.Data[(ihi+1)*h.Stride+ihi] = 0
64 testDlahqr(t, impl, test)
70 // Tests that make sure that some potentially problematic corner cases,
71 // like zero-sized matrix, are covered.
72 for _, wantt := range []bool{true, false} {
73 for _, wantz := range []bool{true, false} {
74 for _, extra := range []int{0, 1, 11} {
75 for _, test := range []dlahqrTest{
77 h: randomHessenberg(0, extra, rnd),
84 h: randomHessenberg(1, 1+extra, rnd),
91 h: randomHessenberg(2, 2+extra, rnd),
98 h: randomHessenberg(2, 2+extra, rnd),
105 h: randomHessenberg(10, 10+extra, rnd),
112 h: randomHessenberg(10, 10+extra, rnd),
119 h: randomHessenberg(10, 10+extra, rnd),
126 h: randomHessenberg(10, 10+extra, rnd),
133 h: randomHessenberg(10, 10+extra, rnd),
141 test.h.Data[test.ilo*test.h.Stride+test.ilo-1] = 0
143 if test.ihi+1 < test.h.Rows {
144 test.h.Data[(test.ihi+1)*test.h.Stride+test.ihi] = 0
148 testDlahqr(t, impl, test)
154 // Tests with explicit eigenvalues computed by Octave.
155 for _, test := range []dlahqrTest{
161 Data: []float64{7.09965484086874e-1},
167 evWant: []complex128{7.09965484086874e-1},
183 evWant: []complex128{1i, -1i},
191 6.25219991450918e-1, 8.17510791994361e-1,
192 3.31218891622294e-1, 1.24103744878131e-1,
199 evWant: []complex128{9.52203547663447e-1, -2.02879811334398e-1},
208 0, 6.25219991450918e-1, 8.17510791994361e-1, 0,
209 0, 3.31218891622294e-1, 1.24103744878131e-1, 0,
217 evWant: []complex128{9.52203547663447e-1, -2.02879811334398e-1},
225 -1.1219562276608, 6.85473513349362e-1,
226 -8.19951061145131e-1, 1.93728523178888e-1,
233 evWant: []complex128{
234 -4.64113852240958e-1 + 3.59580510817350e-1i,
235 -4.64113852240958e-1 - 3.59580510817350e-1i,
244 9.57590178533658e-1, -5.10651295522708e-1, 9.24974510015869e-1, -1.30016306879522e-1, 2.92601986926954e-2,
245 -1.08084756637964, 1.77529701001213, -1.36480197632509, 2.23196371219601e-1, 1.12912853063308e-1,
246 0, -8.44075612174676e-1, 1.067867614486, -2.55782915176399e-1, -2.00598563137468e-1,
247 0, 0, -5.67097237165410e-1, 2.07205057427341e-1, 6.54998340743380e-1,
248 0, 0, 0, -1.89441413886041e-1, -4.18125416021786e-1,
255 evWant: []complex128{
257 4.97029793606701e-1 + 3.63041654992384e-1i,
258 4.97029793606701e-1 - 3.63041654992384e-1i,
259 -1.74079119166145e-1 + 2.01570009462092e-1i,
260 -1.74079119166145e-1 - 2.01570009462092e-1i,
266 testDlahqr(t, impl, test)
270 func testDlahqr(t *testing.T, impl Dlahqrer, test dlahqrTest) {
273 h := cloneGeneral(test.h)
275 extra := h.Stride - h.Cols
283 var z, zCopy blas64.General
286 zCopy = cloneGeneral(z)
289 wr := nanSlice(ihi + 1)
290 wi := nanSlice(ihi + 1)
292 unconverged := impl.Dlahqr(wantt, wantz, n, ilo, ihi, h.Data, h.Stride, wr, wi, iloz, ihiz, z.Data, z.Stride)
294 prefix := fmt.Sprintf("Case wantt=%v, wantz=%v, n=%v, ilo=%v, ihi=%v, iloz=%v, ihiz=%v, extra=%v",
295 wantt, wantz, n, ilo, ihi, iloz, ihiz, extra)
297 if !generalOutsideAllNaN(h) {
298 t.Errorf("%v: out-of-range write to H\n%v", prefix, h.Data)
300 if !generalOutsideAllNaN(z) {
301 t.Errorf("%v: out-of-range write to Z\n%v", prefix, z.Data)
304 if !isUpperHessenberg(h) {
305 t.Logf("%v: H is not Hessenberg", prefix)
308 start := ilo // Index of the first computed eigenvalue.
309 if unconverged != 0 {
312 t.Logf("%v: no eigenvalue has converged", prefix)
316 // Check that wr and wi have not been modified in [:start].
317 if !isAllNaN(wr[:start]) {
318 t.Errorf("%v: unexpected modification of wr", prefix)
320 if !isAllNaN(wi[:start]) {
321 t.Errorf("%v: unexpected modification of wi", prefix)
325 for i := start; i <= ihi; {
326 if wi[i] == 0 { // Real eigenvalue.
328 // Check that the eigenvalue corresponds to a 1×1 block
329 // on the diagonal of H.
331 if wr[i] != h.Data[i*h.Stride+i] {
332 t.Errorf("%v: wr[%v] != H[%v,%v]", prefix, i, i, i)
334 for _, index := range []struct{ r, c int }{
336 {i + 1, i - 1}, // 0 wr[i] h
339 if index.r >= n || index.c < 0 {
342 if h.Data[index.r*h.Stride+index.c] != 0 {
343 t.Errorf("%v: H[%v,%v] != 0", prefix, index.r, index.c)
351 // Complex eigenvalue.
353 // In the conjugate pair the real parts must be equal.
354 if wr[i] != wr[i+1] {
355 t.Errorf("%v: real part of conjugate pair not equal, i=%v", prefix, i)
357 // The first imaginary part must be positive.
359 t.Errorf("%v: wi[%v] not positive", prefix, i)
361 // The second imaginary part must be negative with the same
363 if wi[i] != -wi[i+1] {
364 t.Errorf("%v: wi[%v] != -wi[%v]", prefix, i, i+1)
367 // Check that wi[i] has the correct value.
368 if wr[i] != h.Data[i*h.Stride+i] {
369 t.Errorf("%v: wr[%v] != H[%v,%v]", prefix, i, i, i)
371 if wr[i] != h.Data[(i+1)*h.Stride+i+1] {
372 t.Errorf("%v: wr[%v] != H[%v,%v]", prefix, i, i+1, i+1)
374 prod := math.Abs(h.Data[(i+1)*h.Stride+i] * h.Data[i*h.Stride+i+1])
375 if math.Abs(math.Sqrt(prod)-wi[i]) > tol {
376 t.Errorf("%v: unexpected value of wi[%v]: want %v, got %v", prefix, i, math.Sqrt(prod), wi[i])
379 // Check that the corresponding diagonal block is 2×2.
380 for _, index := range []struct{ r, c int }{
382 {i + 1, i - 1}, // h h h h
383 {i + 2, i - 1}, // 0 wr[i] b h i
384 {i + 2, i}, // 0 c wr[i+1] h
385 {i + 2, i + 1}, // 0 0 0 h
387 if index.r >= n || index.c < 0 {
390 if h.Data[index.r*h.Stride+index.c] != 0 {
391 t.Errorf("%v: H[%v,%v] != 0", prefix, index.r, index.c)
397 // If the number of found eigenvalues is odd, at least one must be real.
398 if (ihi+1-start)%2 != 0 && !hasReal {
399 t.Errorf("%v: expected at least one real eigenvalue", prefix)
402 // Compare found eigenvalues to the reference, if known.
403 if test.evWant != nil {
404 for i := start; i <= ihi; i++ {
405 ev := complex(wr[i], wi[i])
406 found, _ := containsComplex(test.evWant, ev, tol)
408 t.Errorf("%v: unexpected eigenvalue %v", prefix, ev)
417 // Z should contain the orthogonal matrix U.
418 if !isOrthonormal(z) {
419 t.Errorf("%v: Z is not orthogonal", prefix)
421 // Z should have been modified only in the
422 // [iloz:ihiz+1,ilo:ihi+1] block.
423 for i := 0; i < n; i++ {
424 for j := 0; j < n; j++ {
425 if iloz <= i && i <= ihiz && ilo <= j && j <= ihi {
428 if z.Data[i*z.Stride+j] != zCopy.Data[i*zCopy.Stride+j] {
429 t.Errorf("%v: Z modified outside of [iloz:ihiz+1,ilo:ihi+1] block", prefix)
435 blas64.Gemm(blas.NoTrans, blas.NoTrans, 1, test.h, z, 0, hu)
437 blas64.Gemm(blas.Trans, blas.NoTrans, 1, z, hu, 0, uhu)
438 if !equalApproxGeneral(uhu, h, 10*tol) {
439 t.Errorf("%v: Z^T*(initial H)*Z and (final H) are not equal", prefix)