/
dgebrd.go
152 lines (138 loc) · 3.12 KB
/
dgebrd.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
// Copyright ©2015 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 (
"math"
"testing"
"golang.org/x/exp/rand"
"gonum.org/v1/gonum/floats"
)
type Dgebrder interface {
Dgebrd(m, n int, a []float64, lda int, d, e, tauQ, tauP, work []float64, lwork int)
Dgebd2er
}
func DgebrdTest(t *testing.T, impl Dgebrder) {
rnd := rand.New(rand.NewSource(1))
for _, test := range []struct {
m, n, lda int
}{
{100, 100, 0},
{100, 150, 0},
{150, 100, 0},
{100, 100, 200},
{100, 150, 200},
{150, 100, 200},
{300, 300, 0},
{300, 400, 0},
{400, 300, 0},
{300, 300, 500},
{300, 400, 500},
{300, 400, 500},
} {
m := test.m
n := test.n
lda := test.lda
if lda == 0 {
lda = n
}
minmn := min(m, n)
a := make([]float64, m*lda)
for i := range a {
a[i] = rnd.NormFloat64()
}
d := make([]float64, minmn)
e := make([]float64, minmn-1)
tauP := make([]float64, minmn)
tauQ := make([]float64, minmn)
work := make([]float64, max(m, n))
for i := range work {
work[i] = math.NaN()
}
// Store a.
aCopy := make([]float64, len(a))
copy(aCopy, a)
// Compute the true answer with the unblocked algorithm.
impl.Dgebd2(m, n, a, lda, d, e, tauQ, tauP, work)
aAns := make([]float64, len(a))
copy(aAns, a)
dAns := make([]float64, len(d))
copy(dAns, d)
eAns := make([]float64, len(e))
copy(eAns, e)
tauQAns := make([]float64, len(tauQ))
copy(tauQAns, tauQ)
tauPAns := make([]float64, len(tauP))
copy(tauPAns, tauP)
// Test with optimal work.
lwork := -1
copy(a, aCopy)
impl.Dgebrd(m, n, a, lda, d, e, tauQ, tauP, work, lwork)
work = make([]float64, int(work[0]))
lwork = len(work)
for i := range work {
work[i] = math.NaN()
}
for i := range d {
d[i] = math.NaN()
}
for i := range e {
e[i] = math.NaN()
}
for i := range tauQ {
tauQ[i] = math.NaN()
}
for i := range tauP {
tauP[i] = math.NaN()
}
impl.Dgebrd(m, n, a, lda, d, e, tauQ, tauP, work, lwork)
// Test answers
if !floats.EqualApprox(a, aAns, 1e-10) {
t.Errorf("a mismatch")
}
if !floats.EqualApprox(d, dAns, 1e-10) {
t.Errorf("d mismatch")
}
if !floats.EqualApprox(e, eAns, 1e-10) {
t.Errorf("e mismatch")
}
if !floats.EqualApprox(tauQ, tauQAns, 1e-10) {
t.Errorf("tauQ mismatch")
}
if !floats.EqualApprox(tauP, tauPAns, 1e-10) {
t.Errorf("tauP mismatch")
}
// Test with shorter than optimal work.
lwork--
copy(a, aCopy)
for i := range d {
d[i] = 0
}
for i := range e {
e[i] = 0
}
for i := range tauP {
tauP[i] = 0
}
for i := range tauQ {
tauQ[i] = 0
}
impl.Dgebrd(m, n, a, lda, d, e, tauQ, tauP, work, lwork)
// Test answers
if !floats.EqualApprox(a, aAns, 1e-10) {
t.Errorf("a mismatch")
}
if !floats.EqualApprox(d, dAns, 1e-10) {
t.Errorf("d mismatch")
}
if !floats.EqualApprox(e, eAns, 1e-10) {
t.Errorf("e mismatch")
}
if !floats.EqualApprox(tauQ, tauQAns, 1e-10) {
t.Errorf("tauQ mismatch")
}
if !floats.EqualApprox(tauP, tauPAns, 1e-10) {
t.Errorf("tauP mismatch")
}
}
}