-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathHouseholderReflectors.pas
441 lines (362 loc) · 15.6 KB
/
HouseholderReflectors.pas
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// ###################################################################
// #### This file is part of the mathematics library project, and is
// #### offered under the licence agreement described on
// #### http://www.mrsoft.org/
// ####
// #### Copyright:(c) 2017, Michael R. . All rights reserved.
// ####
// #### Unless required by applicable law or agreed to in writing, software
// #### distributed under the License is distributed on an "AS IS" BASIS,
// #### WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// #### See the License for the specific language governing permissions and
// #### limitations under the License.
// ###################################################################
// ###################################################################
// #### a set of routines to create and apply housholder reflections
// -> used in svd and QR decomposition
unit HouseholderReflectors;
interface
uses MatrixConst, MatrixASMStubSwitch;
procedure GenElemHousholderRefl(A : PDouble; LineWidthA : NativeInt; Height : NativeInt; var Alpha : double; Tau : PDouble);
procedure ApplyElemHousholderReflLeft(V : PDouble; LineWidthV : NativeInt; C : PDouble; const LineWidthC : NativeInt; width, height : NativeInt;
Tau : PDouble; Work : PDouble);
// work needs to be at least w4 + h4 where w4 is the next number of width that is divisable by 4 without rest and same for h4
procedure ApplyElemHousholderReflRight(V : PDouble; LineWidthV : NativeInt; C : PDouble; const LineWidthC : NativeInt; width, height : NativeInt;
Tau : PDouble; Work : PDouble);
// ###########################################
// #### Base implementation of Block reflectors for the Lapack decompositions
type
TBlockReflrec = record
// work : PDouble;
// LineWidthWork : NativeInt;
T : PDouble;
LineWidthT : NativeInt;
BlkMultSize : integer;
BlkMultMem : PDouble;
MatrixMultT1 : TMatrixBlockedMultfunc;
MatrixMultT2 : TMatrixBlockedMultfunc;
MatrixMultEx : TMatrixBlockedMultfunc;
end;
// apply block reflector to a matrix
// original DLARFB in Lapack - Left, Transpose, Forward, Columnwise
procedure ApplyBlockReflectorLFC(A : PDouble; LineWidthA : NativeInt; const reflData : TBlockReflrec;
width, height : NativeInt; k : NativeInt; Transposed : boolean);
// block reflector right transposed forward rowwise
procedure ApplyBlockReflectorRFR(A : PDouble; LineWidthA : NativeInt; const reflData : TBlockReflrec; //const qrData : TRecMtxQRDecompData;
width, height : NativeInt; widthT : NativeInt; Transposed : boolean);
// block reflector left, No transpose, backward, columnwise
procedure ApplyBlockReflectorLBC(V : PDouble; LineWidthV : NativeInt;
C : PDouble; LineWidthC : NativeInt; work : PDouble; LineWidthWork : NativeInt;
width, height : NativeInt; k : NativeInt; Transposed : boolean; const reflData : TBlockReflrec);
implementation
uses MathUtilFunc;
// "right" part of dlarf
procedure ApplyElemHousholderReflRight(V : PDouble; LineWidthV : NativeInt; C : PDouble; const LineWidthC : NativeInt; width, height : NativeInt;
Tau : PDouble; Work : PDouble);
var pVWork : PDouble;
h4 : NativeInt;
begin
// work = A(1:m, 2:n)T*A(1:m, 1)
if tau^ <> 0 then
begin
// note: the original routine has here an option C -> but in all cases it's
// A + 1
h4 := height;
if height and $03 <> 0 then
h4 := height + 4 - height and $03;
pVWork := V;
if LineWidthV <> sizeof(double) then
begin
pVWork := GenPtr(Work, h4, 0, sizeof(double));
// make it a vector
MatrixCopy(pVWork, sizeof(double), V, LineWidthV, 1, width);
end;
MatrixMtxVecMult(work, sizeof(double), C, pVWork, LineWidthC, sizeof(double), width, height, 1, 0);
MatrixRank1Update(C, LineWidthC, width, height, -Tau^, work, pVWork, sizeof(double), sizeof(double));
// todo: there is some other scanning going on for non zero columns...
// do the basic operation here...
end;
end;
// apply householder transformation to A (one column)
// original DLARFG in Lapack
procedure GenElemHousholderRefl(A : PDouble; LineWidthA : NativeInt; Height : NativeInt; var Alpha : double; Tau : PDouble);
var beta : double;
xNorm : double;
saveMin : double;
rsafmn : double;
cnt : integer;
begin
dec(height);
if height <= 0 then
begin
Tau^ := 0;
exit;
end;
xNorm := MatrixElementwiseNorm2(A, LineWidthA, 1, height, True);
if xNorm = 0 then
begin
// H = I
Tau^ := 0;
exit;
end;
beta := -sign( pythag(alpha, xnorm), alpha);
cnt := 0;
// todo: apply under/overflow code here as lapack does
// check for possible under/overflow -> rescale
saveMin := cMinDblDivEps;
// // note this is not the original code
if Abs(beta) < saveMin then
begin
rsafmn := 1/saveMin;
repeat
inc(cnt);
MatrixScaleAndAdd(A, LineWidthA, 1, Height, 0, rsafmn);
beta := beta*rsafmn;
alpha := alpha*rsafmn;
until abs(beta) >= saveMin;
xnorm := MatrixElementwiseNorm2(A, LineWidthA, 1, height, True);
beta := -sign( pythag(alpha, xnorm), alpha );
end;
Tau^ := (beta - alpha)/beta;
MatrixScaleAndAdd(A, LineWidthA, 1, Height, 0, 1/(alpha - beta));
while cnt > 0 do
begin
beta := beta*saveMin;
dec(cnt);
end;
alpha := beta;
end;
// original Dlarf in lapack
procedure ApplyElemHousholderReflLeft(V : PDouble; LineWidthV : NativeInt; C : PDouble; const LineWidthC : NativeInt; width, height : NativeInt;
Tau : PDouble; Work : PDouble);
begin
// work = A(1:m, 2:n)T*A(1:m, 1)
if tau^ <> 0 then
begin
// todo: there is some other scanning going on for non zero columns...
// do the basic operation here...
MatrixMtxVecMultT(work, sizeof(double), C, V, LineWidthC, LineWidthV, width, height, 1, 0);
MatrixRank1Update(C, LineWidthC, width, height, -tau^, V, work, LineWidthV, sizeof(double));
end;
end;
// ###########################################
// #### Blocked reflectors
// ###########################################
// apply block reflector to a matrix
// original DLARFB in Lapack - Left, Transpose, Forward, Columnwise
procedure ApplyBlockReflectorLFC(A : PDouble; LineWidthA : NativeInt; const reflData : TBlockReflrec;
width, height : NativeInt; k : NativeInt; Transposed : boolean);
var pC1, pC2 : PDouble;
pV1, pV2 : PDouble;
T : PDouble;
LineWidthT : NativeInt;
mem : PDouble;
LineWidthMem : NativeInt;
//s : string;
//s1, s2 : string;
begin
if (width <= 0) or (height <= 0) then
exit;
mem := reflData.T;
inc(PByte(mem), k*reflData.LineWidthT); // upper part (nb x nb) is dedicated to T, lower part to W in dlarfb
LineWidthMem := reflData.LineWidthT;
T := reflData.T;
//s := WriteMtx(T, reflData.LineWidthT, 3, 3);
lineWidthT := reflData.LineWidthT;
// (I - Y*T*Y')xA_trailing
pV1 := A;
pV2 := GenPtr(A, 0, k, LineWidthA);
pC1 := GenPtr(A, k, 0, LineWidthA);
pC2 := GenPtr(A, k, k, LineWidthA);
// W = C1'*V1
MatrixMultTria2T1(mem, LineWidthMem, pC1, LineWidthA, pV1, LineWidthA, width, k, k, k);
//s := WriteMtx( mem, LineWidthMem, k, 5 );
// W = W + C2'*V2
if height > k then
reflData.MatrixMultT1(mem, LineWidthMem, pC2, pV2, Width, height - k, k, height - k, LineWidthA, LineWidthA, reflData.BlkMultSize, doAdd, reflData.BlkMultMem);
// W = W * T (using dtrm) or W = W*T'
if transposed
then
MtxMultRightUpperTriaNoUnitT2(mem, LineWidthMem, T, LineWidthT, k, width, k, k)
else
MtxMultRightUpperTriaNoUnit(mem, LineWidthMem, T, LineWidthT, k, width, k, k);
if height > k then
begin
// C2 = C2 - V2*W'
reflData.MatrixMultT2(pC2, LineWidthA, pV2, mem, k, height - k, k, width,
LineWidthA, LineWidthMem, reflData.BlkMultSize, doSub, reflData.BlkMultMem);
// W = W*V1' (lower left part of Y1! -> V1)
MtxMultRightLowerTriaUnitT2(mem, LineWidthMem, A, LineWidthA, k, width, k, k);
end;
// C1 = C1 - W'
MatrixSubT(pC1, LineWidthA, mem, LineWidthMem, width, k);
end;
// dlarfb 'Right', 'Transpose', 'Forward', 'Rowwise'
procedure ApplyBlockReflectorRFR(A : PDouble; LineWidthA : NativeInt; const reflData : TBlockReflrec;
width, height : NativeInt; widthT : NativeInt; Transposed : boolean);
var pC1, pC2 : PDouble;
pV1, pV2 : PDouble;
T : PDouble;
LineWidthT : NativeInt;
LineWidthW : NativeInt;
W : PDouble;
heightC1, widthC1 : NativeInt;
widthC2, heightC2 : NativeInt;
widthV1, heightV1 : NativeInt;
widthV2, heightV2 : NativeInt;
widthW, heightW : NativeInt;
begin
// it is assumed that height contains the full height of the input matrix A
// we subsect it in this routine
widthC1 := widthT;
heightC1 := height - widthT;
widthC2 := width - widthT;
heightC2 := height - widthT;
widthV1 := widthT;
heightV1 := widthT;
widthV2 := width - widthT;
heightV2 := widthT;
widthW := widthT;
heightW := height - widthT;
// W := C * V**T = (C1*V1**T + C2*V2**T) (stored in WORK)
// upper part (nb x nb) is dedicated to T, lower part to W in dlarfb
W := GenPtr(reflData.T, 0, widthT, reflData.LineWidthT);
LineWidthW := reflData.LineWidthT;
T := reflData.T;
LineWidthT := reflData.LineWidthT;
pC1 := GenPtr(A, 0, widthT, LineWidthA);
pC2 := pC1;
inc(pC2, widthT);
pV1 := A; // GenPtr(A, 0, 0, LineWidthA);
pV2 := GenPtr(pV1, widthT, 0, LineWidthA);
// W := W * V1**T // dtrm right upper transpose unit combined with copy
MtxMultTria2TUpperUnit(W, LineWidthW, pC1, LineWidthA, pV1, LineWidthA, widthC1, heightC1, widthV1, heightV1);
if width > widthT then
begin
// W := W + C2 * V2**T
reflData.MatrixMultT2(W, LineWidthW, pC2, pV2,
widthC2, heightC2, widthV2, heightV2,
LineWidthA, LineWidthA, reflData.BlkMultSize, doAdd, reflData.BlkMultMem);
end;
// W := W * T or W * T**T
if transposed
then
MtxMultRightUpperTriaNoUnitT2(W, LineWidthW, T, LineWidthT, widthW, heightW, WidthT, WidthT)
else
MtxMultRightUpperTriaNoUnit(W, LineWidthW, T, LineWidthT, widthW, heightW, WidthT, WidthT);
// C2 := C2 - W * V2
if width > widthT then
reflData.MatrixMultEx(pC2, LineWidthA, W, pV2, widthW, heightW, widthV2, heightV2, LineWidthW,
LineWidthA, reflData.BlkMultSize, doSub, reflData.BlkMultMem);
// W := W * V1
MtxMultRightUpperTriaUnit(W, LineWidthW, pV1, LineWidthA, widthW, heightW, widthV1, heightV1);
// C1 := C1 - W
MatrixSub(pC1, LineWidthA, pC1, W, widthC1, heightC1, LineWidthA, LineWidthW);
end;
(*
W := C**T * V = (C1**T * V1 + C2**T * V2) (stored in WORK)
*
* W := C2**T
*
DO 70 j = 1, k
CALL dcopy( n, c( m-k+j, 1 ), ldc, work( 1, j ), 1 )
70 CONTINUE
*
* W := W * V2
*
CALL dtrmm( 'Right', 'Upper', 'No transpose', 'Unit', n,
$ k, one, v( m-k+1, 1 ), ldv, work, ldwork )
IF( m.GT.k ) THEN
*
* W := W + C1**T * V1
*
CALL dgemm( 'Transpose', 'No transpose', n, k, m-k,
$ one, c, ldc, v, ldv, one, work, ldwork )
END IF
*
* W := W * T**T or W * T
*
CALL dtrmm( 'Right', 'Lower', transt, 'Non-unit', n, k,
$ one, t, ldt, work, ldwork )
*
* C := C - V * W**T
*
IF( m.GT.k ) THEN
*
* C1 := C1 - V1 * W**T
*
CALL dgemm( 'No transpose', 'Transpose', m-k, n, k,
$ -one, v, ldv, work, ldwork, one, c, ldc )
END IF
*
* W := W * V2**T
*
CALL dtrmm( 'Right', 'Upper', 'Transpose', 'Unit', n, k,
$ one, v( m-k+1, 1 ), ldv, work, ldwork )
*
* C2 := C2 - W**T
*
DO 90 j = 1, k
DO 80 i = 1, n
c( m-k+j, i ) = c( m-k+j, i ) - work( i, j )
80 CONTINUE
90 CONTINUE
*)
// used in symmetric eigenvalue problem:
// dlarfb 'left', 'No transpose', 'backward', 'columnwise'
procedure ApplyBlockReflectorLBC(V : PDouble; LineWidthV : NativeInt;
C : PDouble; LineWidthC : NativeInt; work : PDouble; LineWidthWork : NativeInt;
width, height : NativeInt; k : NativeInt; Transposed : boolean; const reflData : TBlockReflrec);
var pC1, pC2 : PDouble;
pV1, pV2 : PDouble;
hk : NativeInt;
begin
if (width <= 0) or (height <= 0) then
exit;
// C = wxh = width x height
// V = wxh = k x Height
// T = wxh = k x k
// Work = wxh = k x Width
// V = (V1) C = (C1)
// (V2) last k rows (C2) = last k rows
// V2 is an upper triangular
pV1 := V;
pV2 := GenPtr(V, 0, height - k, LineWidthV);
pC1 := C;
pC2 := GenPtr(C, 0, height - k, LineWidthC);
hk := height - k;
// W = C2**T
MatrixTranspose( work, LineWidthWork, pC2, LineWidthC, width, k);
//CALL dtrmm( 'Right', 'Upper', 'No transpose', 'Unit', n,
// $ k, one, v( m-k+1, 1 ), ldv, work, ldwork )
// W = W*V2
MtxMultRightUpperTriaUnit(work, LineWidthWork, pV2, LineWidthV, k, width, k, k);
// W = W + C1**T*V1
// CALL dgemm( 'Transpose', 'No transpose', n, k, m-k,
// one, c, ldc, v, ldv, one, work, ldwork )
if height > k then
reflData.MatrixMultT1(work, LineWidthWork, pC1, pV1, width, hk, k, hk, LineWidthC, LineWidthV,
reflData.blkMultSize, doAdd, reflData.blkMultMem);
// CALL dtrmm( 'Right', 'Lower', transt, 'Non-unit', n, k,
// $ one, t, ldt, work, ldwork )
// W = W*T**T or W*T
if Transposed
then
MtxMultRightLowerTriaNoUnitT2( work, LineWidthWork, reflData.T, reflData.LineWidthT, k, width, k, k )
else
MtxMultRightLowerTriaNoUnit(work, LineWidthWork, reflData.T, reflData.LineWidthT, k, width, k, k );
// C1 = C1 - V1*W**T
// CALL dgemm( 'No transpose', 'Transpose', m-k, n, k,
// $ -one, v, ldv, work, ldwork, one, c, ldc )
if height > k then
reflData.MatrixMultT2( pC1, LineWidthC, pV1, work, k, hk, k, width,
LineWidthV, LineWidthWork, reflData.blkMultSize,
doSub, reflData.blkMultMem );
// W = W*V2**T
// CALL dtrmm( 'Right', 'Upper', 'Transpose', 'Unit', n, k,
// $ one, v( m-k+1, 1 ), ldv, work, ldwork )
MtxMultRightUpperTriaUnitT2(work, LineWidthWork, pV2, LineWidthV, k, width, k, k);
// C2 := C2 - W**T
MatrixSubT(pC2, LineWidthC, work, LineWidthWork, width, k);
end;
end.