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 Dgehrder interface {
19 Dgehrd(n, ilo, ihi int, a []float64, lda int, tau, work []float64, lwork int)
21 Dorgqr(m, n, k int, a []float64, lda int, tau, work []float64, lwork int)
24 func DgehrdTest(t *testing.T, impl Dgehrder) {
25 rnd := rand.New(rand.NewSource(1))
27 // Randomized tests for small matrix sizes that will most likely
28 // use the unblocked algorithm.
29 for _, n := range []int{1, 2, 3, 4, 5, 10, 34} {
30 for _, extra := range []int{0, 13} {
31 for _, optwork := range []bool{true, false} {
32 for cas := 0; cas < 10; cas++ {
38 testDgehrd(t, impl, n, ilo, ihi, extra, optwork, rnd)
44 // These are selected tests for larger matrix sizes to test the blocked
45 // algorithm. Use sizes around several powers of two because that is
46 // where the blocked path will most likely start to be taken. For
47 // example, at present the blocked algorithm is used for sizes larger
49 for _, test := range []struct {
61 {132, 1, 129}, // Size = 129, unblocked.
62 {132, 0, 129}, // Size = 130, blocked.
74 for _, extra := range []int{0, 13} {
75 for _, optwork := range []bool{true, false} {
76 testDgehrd(t, impl, test.n, test.ilo, test.ihi, extra, optwork, rnd)
82 func testDgehrd(t *testing.T, impl Dgehrder, n, ilo, ihi, extra int, optwork bool, rnd *rand.Rand) {
83 a := randomGeneral(n, n, n+extra, rnd)
85 aCopy.Data = make([]float64, len(a.Data))
86 copy(aCopy.Data, a.Data)
96 impl.Dgehrd(n, ilo, ihi, nil, a.Stride, nil, work, -1)
97 work = nanSlice(int(work[0]))
99 work = nanSlice(max(1, n))
102 impl.Dgehrd(n, ilo, ihi, a.Data, a.Stride, tau, work, len(work))
105 // Just make sure there is no panic.
109 prefix := fmt.Sprintf("Case n=%v, ilo=%v, ihi=%v, extra=%v", n, ilo, ihi, extra)
111 // Check any invalid modifications of a.
112 if !generalOutsideAllNaN(a) {
113 t.Errorf("%v: out-of-range write to A\n%v", prefix, a.Data)
115 for i := ilo; i <= ihi; i++ {
116 for j := 0; j < min(ilo, i); j++ {
117 if a.Data[i*a.Stride+j] != aCopy.Data[i*aCopy.Stride+j] {
118 t.Errorf("%v: unexpected modification of A[%v,%v]", prefix, i, j)
122 for i := ihi + 1; i < n; i++ {
123 for j := 0; j < i; j++ {
124 if a.Data[i*a.Stride+j] != aCopy.Data[i*aCopy.Stride+j] {
125 t.Errorf("%v: unexpected modification of A[%v,%v]", prefix, i, j)
129 for i := 0; i <= ilo; i++ {
130 for j := i; j < ilo+1; j++ {
131 if a.Data[i*a.Stride+j] != aCopy.Data[i*aCopy.Stride+j] {
132 t.Errorf("%v: unexpected modification at A[%v,%v]", prefix, i, j)
135 for j := ihi + 1; j < n; j++ {
136 if a.Data[i*a.Stride+j] != aCopy.Data[i*aCopy.Stride+j] {
137 t.Errorf("%v: unexpected modification at A[%v,%v]", prefix, i, j)
141 for i := ihi + 1; i < n; i++ {
142 for j := i; j < n; j++ {
143 if a.Data[i*a.Stride+j] != aCopy.Data[i*aCopy.Stride+j] {
144 t.Errorf("%v: unexpected modification at A[%v,%v]", prefix, i, j)
149 // Check that tau has been assigned properly.
150 for i, v := range tau {
152 t.Errorf("%v: unexpected NaN at tau[%v]", prefix, i)
156 // Extract Q and check that it is orthogonal.
159 for i := ilo + 2; i <= ihi; i++ {
160 for j := ilo + 1; j < ihi; j++ {
161 q.Data[i*q.Stride+j] = a.Data[i*a.Stride+j-1]
165 impl.Dorgqr(nh, nh, nh, q.Data[(ilo+1)*q.Stride+ilo+1:], q.Stride, tau[ilo:ihi], work, len(work))
167 if !isOrthonormal(q) {
168 t.Errorf("%v: Q is not orthogonal\nQ=%v", prefix, q)
171 // Construct Q^T * AOrig * Q and check that it is upper Hessenberg.
172 aq := blas64.General{
176 Data: make([]float64, n*n),
178 blas64.Gemm(blas.NoTrans, blas.NoTrans, 1, aCopy, q, 0, aq)
179 qaq := blas64.General{
183 Data: make([]float64, n*n),
185 blas64.Gemm(blas.Trans, blas.NoTrans, 1, q, aq, 0, qaq)
186 for i := 0; i <= ilo; i++ {
187 for j := ilo + 1; j <= ihi; j++ {
188 qaqij := qaq.Data[i*qaq.Stride+j]
189 diff := qaqij - a.Data[i*a.Stride+j]
190 if math.Abs(diff) > 1e-13 {
191 t.Errorf("%v: Q^T*AOrig*Q and A are not equal, diff at [%v,%v]=%v", prefix, i, j, diff)
195 for i := ilo + 1; i <= ihi; i++ {
196 for j := ilo; j < n; j++ {
197 qaqij := qaq.Data[i*qaq.Stride+j]
199 if math.Abs(qaqij) > 1e-13 {
200 t.Errorf("%v: Q^T*AOrig*Q is not upper Hessenberg, [%v,%v]=%v", prefix, i, j, qaqij)
204 diff := qaqij - a.Data[i*a.Stride+j]
205 if math.Abs(diff) > 1e-13 {
206 t.Errorf("%v: Q^T*AOrig*Q and A are not equal, diff at [%v,%v]=%v", prefix, i, j, diff)