-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqrwyk.go
298 lines (256 loc) · 9.06 KB
/
qrwyk.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright (c) Harri Rautila, 2013
// This file is part of github.com/hrautila/matops package. It is free software,
// distributed under the terms of GNU Lesser General Public License Version 3, or
// any later version. See the COPYING tile included in this archive.
package matops
import (
"github.com/hrautila/matrix"
"errors"
//"fmt"
)
// Build Q in place by applying elementary reflectors in reverse order to
// an implied identity matrix. This forms Q = H(1)H(2) ... H(k)
//
// this is compatibe with lapack.DORG2R
func unblockedBuildQ(A, tau, w *matrix.FloatMatrix, kb int) error {
var err error = nil
var ATL, ATR, ABL, ABR matrix.FloatMatrix
var A00, a01, A02, a10t, a11, a12t, A20, a21, A22 matrix.FloatMatrix
var tT, tB matrix.FloatMatrix
var t0, tau1, t2, w1 matrix.FloatMatrix
var mb int
var rowvec bool
mb = A.Rows() - A.Cols()
rowvec = tau.Rows() == 1
partition2x2(
&ATL, &ATR,
&ABL, &ABR, A, mb, 0, pBOTTOMRIGHT)
if rowvec {
partition1x2(
&tT, &tB, tau, 0, pRIGHT)
} else {
partition2x1(
&tT,
&tB, tau, 0, pBOTTOM)
}
// clearing of the columns of the right and setting ABR to unit diagonal
// (only if not applying all reflectors, kb > 0)
for ATL.Rows() > 0 && ATL.Cols() > 0 {
repartition2x2to3x3(&ATL,
&A00, &a01, &A02,
&a10t, &a11, &a12t,
&A20, &a21, &A22, A, 1, pTOPLEFT)
if rowvec {
repartition1x2to1x3(&tT,
&t0, &tau1, &t2, tau, 1, pLEFT)
} else {
repartition2x1to3x1(&tT,
&t0,
&tau1,
&t2, tau, 1, pTOP)
}
// --------------------------------------------------------
// adjust workspace to correct size
w.SubMatrix(&w1, 0, 0, 1, a12t.Cols())
// apply Householder reflection from left
applyHHTo2x1(&tau1, &a21, &a12t, &A22, &w1, LEFT)
// apply (in-place) current elementary reflector to unit vector
a21.Scale(-tau1.Float())
a11.SetAt(0, 0, 1.0 - tau1.Float())
// zero the upper part
a01.SetIndexes(0.0)
// --------------------------------------------------------
continue3x3to2x2(
&ATL, &ATR,
&ABL, &ABR, &A00, &a11, &A22, A, pTOPLEFT)
if rowvec {
continue1x3to1x2(
&tT, &tB, &t0, &tau1, tau, pLEFT)
} else {
continue3x1to2x1(
&tT,
&tB, &t0, &tau1, tau, pTOP)
}
}
return err
}
func blockedBuildQ(A, tau, W *matrix.FloatMatrix, nb int) error {
var err error = nil
var ATL, ATR, ABL, ABR, AL matrix.FloatMatrix
var A00, A01, A02, A10, A11, A12, A20, A21, A22 matrix.FloatMatrix
var tT, tB matrix.FloatMatrix
var t0, tau1, t2, Tw, Wrk matrix.FloatMatrix
var mb int
mb = A.Rows() - A.Cols()
Twork := matrix.FloatZeros(nb, nb)
partition2x2(
&ATL, &ATR,
&ABL, &ABR, A, mb, 0, pBOTTOMRIGHT)
partition2x1(
&tT,
&tB, tau, 0, pBOTTOM)
// clearing of the columns of the right and setting ABR to unit diagonal
// (only if not applying all reflectors, kb > 0)
for ATL.Rows() > 0 && ATL.Cols() > 0 {
repartition2x2to3x3(&ATL,
&A00, &A01, &A02,
&A10, &A11, &A12,
&A20, &A21, &A22, A, nb, pTOPLEFT)
repartition2x1to3x1(&tT,
&t0,
&tau1,
&t2, tau, nb, pTOP)
// --------------------------------------------------------
// build block reflector from current block
merge2x1(&AL, &A11, &A21)
Twork.SubMatrix(&Tw, 0, 0, A11.Cols(), A11.Cols())
unblkQRBlockReflector(&Tw, &AL, &tau1)
// update with current block reflector (I - Y*T*Y.T)*Atrailing
W.SubMatrix(&Wrk, 0, 0, A12.Cols(), A11.Cols())
updateWithQT(&A12, &A22, &A11, &A21, &Tw, &Wrk, nb, false)
// use unblocked version to compute current block
W.SubMatrix(&Wrk, 0, 0, 1, A11.Cols())
unblockedBuildQ(&AL, &tau1, &Wrk, 0)
// zero upper part
A01.SetIndexes(0.0)
// --------------------------------------------------------
continue3x3to2x2(
&ATL, &ATR,
&ABL, &ABR, &A00, &A11, &A22, A, pTOPLEFT)
continue3x1to2x1(
&tT,
&tB, &t0, &tau1, tau, pTOP)
}
return err
}
func blockedBuildQT(A, T, W *matrix.FloatMatrix, nb int) error {
var err error = nil
var ATL, ATR, ABL, ABR, AL matrix.FloatMatrix
var A00, A01, A11, A12, A21, A22 matrix.FloatMatrix
var TTL, TTR, TBL, TBR matrix.FloatMatrix
var T00, T01, T02, T11, T12, T22 matrix.FloatMatrix
var tau1, Wrk matrix.FloatMatrix
var mb int
mb = A.Rows() - A.Cols()
partition2x2(
&ATL, &ATR,
&ABL, &ABR, A, mb, 0, pBOTTOMRIGHT)
partition2x2(
&TTL, &TTR,
&TBL, &TBR, T, 0, 0, pBOTTOMRIGHT)
// clearing of the columns of the right and setting ABR to unit diagonal
// (only if not applying all reflectors, kb > 0)
for ATL.Rows() > 0 && ATL.Cols() > 0 {
repartition2x2to3x3(&ATL,
&A00, &A01, nil,
nil, &A11, &A12,
nil, &A21, &A22, A, nb, pTOPLEFT)
repartition2x2to3x3(&TTL,
&T00, &T01, &T02,
nil, &T11, &T12,
nil, nil, &T22, T, nb, pTOPLEFT)
// --------------------------------------------------------
// update with current block reflector (I - Y*T*Y.T)*Atrailing
W.SubMatrix(&Wrk, 0, 0, A12.Cols(), A11.Cols())
updateWithQT(&A12, &A22, &A11, &A21, &T11, &Wrk, nb, false)
// use unblocked version to compute current block
W.SubMatrix(&Wrk, 0, 0, 1, A11.Cols())
// elementary scalar coefficients on the diagonal, column vector
T11.Diag(&tau1)
merge2x1(&AL, &A11, &A21)
// do an unblocked update to current block
unblockedBuildQ(&AL, &tau1, &Wrk, 0)
// zero upper part
A01.SetIndexes(0.0)
// --------------------------------------------------------
continue3x3to2x2(
&ATL, &ATR,
&ABL, &ABR, &A00, &A11, &A22, A, pTOPLEFT)
continue3x3to2x2(
&TTL, &TTR,
&TBL, &TBR, &T00, &T11, &T22, T, pTOPLEFT)
}
return err
}
/*
* Generate an M-by-N real matrix Q with ortonormal columns, which is
* defined as the product of k elementary reflectors and block reflector T
*
* Q = H(1) H(2) . . . H(k)
*
* as returned by DecomposeQRT().
*
* Arguments:
* A On entry, QR factorization as returned by DecomposeQRT() where the lower
* trapezoidal part holds the elementary reflectors. On exit, the M-by-N
* matrix Q.
*
* tau The scalar factors of elementary reflectors as returned by DecomposeQR()
*
* W Workspace, size A.Cols()-by-nb.
*
* nb Blocksize for blocked invocations. If nb == 0 unblocked algorith is used
*
* Compatible with lapack.DORGQR
*/
func BuildQ(A, tau, W *matrix.FloatMatrix, nb int) (*matrix.FloatMatrix, error) {
var err error = nil
if nb != 0 && W == nil {
return nil, errors.New("workspace not defined")
}
// default is from LEFT
if nb != 0 && (W.Cols() < nb || W.Rows() < A.Cols()) {
return nil, errors.New("workspace too small")
}
if nb == 0 {
w := matrix.FloatZeros(1, A.Cols())
err = unblockedBuildQ(A, tau, w, 0)
} else {
var Wrk matrix.FloatMatrix
Wrk.SubMatrixOf(W, 0, 0, A.Cols(), nb)
err = blockedBuildQ(A, tau, &Wrk, nb)
}
return A, err
}
/*
* Generate an M-by-N real matrix Q with ortonormal columns, which is
* defined as the product of k elementary reflectors and block reflector T
*
* Q = H(1) H(2) . . . H(k)
*
* generated using the compact WY representaion as returned by DecomposeQRT().
*
* Arguments:
* A On entry, QR factorization as returned by DecomposeQRT() where the lower
* trapezoidal part holds the elementary reflectors. On exit, the M-by-N
* matrix Q.
*
* T The block reflector computed from elementary reflectors as returned by
* DecomposeQRT() or computed from elementary reflectors and scalar coefficients
* by BuildT()
*
* W Workspace, size A.Cols()-by-nb.
*
* nb Blocksize for blocked invocations. If nb == 0 default value T.Cols()
* is used.
*
* Compatible with lapack.DORGQR
*/
func BuildQT(A, T, W *matrix.FloatMatrix, nb int) (*matrix.FloatMatrix, error) {
var err error = nil
if nb == 0 {
nb = A.Cols()
}
// default is from LEFT
if nb != 0 && (W.Cols() < nb || W.Rows() < A.Cols()) {
return nil, errors.New("workspace too small")
}
var Wrk matrix.FloatMatrix
Wrk.SubMatrixOf(W, 0, 0, A.Cols(), nb)
err = blockedBuildQT(A, T, &Wrk, nb)
return A, err
}
// Local Variables:
// tab-width: 4
// indent-tabs-mode: nil
// End: