-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathcreflections.pas
More file actions
381 lines (341 loc) · 10.7 KB
/
creflections.pas
File metadata and controls
381 lines (341 loc) · 10.7 KB
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
{$MODESWITCH RESULT+}
{$GOTO ON}
(*************************************************************************
Copyright (c) 1992-2007 The University of Tennessee. All rights reserved.
Contributors:
* Sergey Bochkanov (ALGLIB project). Translation from FORTRAN to
pseudocode.
See subroutines comments for additional copyrights.
>>> SOURCE LICENSE >>>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation (www.fsf.org); either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
A copy of the GNU General Public License is available at
http://www.fsf.org/licensing/licenses
>>> END OF LICENSE >>>
*************************************************************************)
unit creflections;
interface
uses Math, Sysutils, Ap;
procedure ComplexGenerateReflection(var X : TComplex1DArray;
N : AlglibInteger;
var Tau : Complex);
procedure ComplexApplyReflectionFromTheLeft(var C : TComplex2DArray;
Tau : Complex;
const V : TComplex1DArray;
M1 : AlglibInteger;
M2 : AlglibInteger;
N1 : AlglibInteger;
N2 : AlglibInteger;
var WORK : TComplex1DArray);
procedure ComplexApplyReflectionFromTheRight(var C : TComplex2DArray;
Tau : Complex;
var V : TComplex1DArray;
M1 : AlglibInteger;
M2 : AlglibInteger;
N1 : AlglibInteger;
N2 : AlglibInteger;
var WORK : TComplex1DArray);
implementation
(*************************************************************************
Generation of an elementary complex reflection transformation
The subroutine generates elementary complex reflection H of order N, so
that, for a given X, the following equality holds true:
( X(1) ) ( Beta )
H' * ( .. ) = ( 0 ), H'*H = I, Beta is a real number
( X(n) ) ( 0 )
where
( V(1) )
H = 1 - Tau * ( .. ) * ( conj(V(1)), ..., conj(V(n)) )
( V(n) )
where the first component of vector V equals 1.
Input parameters:
X - vector. Array with elements [1..N].
N - reflection order.
Output parameters:
X - components from 2 to N are replaced by vector V.
The first component is replaced with parameter Beta.
Tau - scalar value Tau.
This subroutine is the modification of CLARFG subroutines from the LAPACK
library. It has similar functionality except for the fact that it doesn’t
handle errors when intermediate results cause an overflow.
-- LAPACK auxiliary routine (version 3.0) --
Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
Courant Institute, Argonne National Lab, and Rice University
September 30, 1994
*************************************************************************)
procedure ComplexGenerateReflection(var X : TComplex1DArray;
N : AlglibInteger;
var Tau : Complex);
var
J : AlglibInteger;
ALPHA : Complex;
ALPHI : Double;
ALPHR : Double;
BETA : Double;
XNORM : Double;
MX : Double;
T : Complex;
S : Double;
V : Complex;
i_ : AlglibInteger;
begin
if N<=0 then
begin
TAU := C_Complex(0);
Exit;
end;
//
// Scale if needed (to avoid overflow/underflow during intermediate
// calculations).
//
MX := 0;
J:=1;
while J<=N do
begin
MX := Max(AbsComplex(X[J]), MX);
Inc(J);
end;
S := 1;
if AP_FP_Neq(MX,0) then
begin
if AP_FP_Less(MX,1) then
begin
S := Sqrt(MinRealNumber);
V := C_Complex(1/S);
for i_ := 1 to N do
begin
X[i_] := C_Mul(V, X[i_]);
end;
end
else
begin
S := Sqrt(MaxRealNumber);
V := C_Complex(1/S);
for i_ := 1 to N do
begin
X[i_] := C_Mul(V, X[i_]);
end;
end;
end;
//
// calculate
//
ALPHA := X[1];
MX := 0;
J:=2;
while J<=N do
begin
MX := Max(AbsComplex(X[J]), MX);
Inc(J);
end;
XNORM := 0;
if AP_FP_Neq(MX,0) then
begin
J:=2;
while J<=N do
begin
T := C_DivR(X[J],MX);
XNORM := XNORM+C_Mul(T,Conj(T)).X;
Inc(J);
end;
XNORM := Sqrt(XNORM)*MX;
end;
ALPHR := ALPHA.X;
ALPHI := ALPHA.Y;
if AP_FP_Eq(XNORM,0) and AP_FP_Eq(ALPHI,0) then
begin
TAU := C_Complex(0);
X[1] := C_MulR(X[1],S);
Exit;
end;
MX := Max(AbsReal(ALPHR), AbsReal(ALPHI));
MX := Max(MX, AbsReal(XNORM));
BETA := -MX*Sqrt(AP_Sqr(ALPHR/MX)+AP_Sqr(ALPHI/MX)+AP_Sqr(XNORM/MX));
if AP_FP_Less(ALPHR,0) then
begin
BETA := -BETA;
end;
TAU.X := (BETA-ALPHR)/BETA;
TAU.Y := -ALPHI/BETA;
ALPHA := C_RDiv(1,C_SubR(ALPHA,BETA));
if N>1 then
begin
for i_ := 2 to N do
begin
X[i_] := C_Mul(ALPHA, X[i_]);
end;
end;
ALPHA := C_Complex(BETA);
X[1] := ALPHA;
//
// Scale back
//
X[1] := C_MulR(X[1],S);
end;
(*************************************************************************
Application of an elementary reflection to a rectangular matrix of size MxN
The algorithm pre-multiplies the matrix by an elementary reflection
transformation which is given by column V and scalar Tau (see the
description of the GenerateReflection). Not the whole matrix but only a
part of it is transformed (rows from M1 to M2, columns from N1 to N2). Only
the elements of this submatrix are changed.
Note: the matrix is multiplied by H, not by H'. If it is required to
multiply the matrix by H', it is necessary to pass Conj(Tau) instead of Tau.
Input parameters:
C - matrix to be transformed.
Tau - scalar defining transformation.
V - column defining transformation.
Array whose index ranges within [1..M2-M1+1]
M1, M2 - range of rows to be transformed.
N1, N2 - range of columns to be transformed.
WORK - working array whose index goes from N1 to N2.
Output parameters:
C - the result of multiplying the input matrix C by the
transformation matrix which is given by Tau and V.
If N1>N2 or M1>M2, C is not modified.
-- LAPACK auxiliary routine (version 3.0) --
Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
Courant Institute, Argonne National Lab, and Rice University
September 30, 1994
*************************************************************************)
procedure ComplexApplyReflectionFromTheLeft(var C : TComplex2DArray;
Tau : Complex;
const V : TComplex1DArray;
M1 : AlglibInteger;
M2 : AlglibInteger;
N1 : AlglibInteger;
N2 : AlglibInteger;
var WORK : TComplex1DArray);
var
T : Complex;
I : AlglibInteger;
VM : AlglibInteger;
i_ : AlglibInteger;
begin
if C_EqualR(Tau,0) or (N1>N2) or (M1>M2) then
begin
Exit;
end;
//
// w := C^T * conj(v)
//
VM := M2-M1+1;
I:=N1;
while I<=N2 do
begin
WORK[I] := C_Complex(0);
Inc(I);
end;
I:=M1;
while I<=M2 do
begin
T := Conj(V[I+1-M1]);
for i_ := N1 to N2 do
begin
WORK[i_] := C_Add(WORK[i_], C_Mul(T, C[I,i_]));
end;
Inc(I);
end;
//
// C := C - tau * v * w^T
//
I:=M1;
while I<=M2 do
begin
T := C_Mul(V[I-M1+1],TAU);
for i_ := N1 to N2 do
begin
C[I,i_] := C_Sub(C[I,i_], C_Mul(T, WORK[i_]));
end;
Inc(I);
end;
end;
(*************************************************************************
Application of an elementary reflection to a rectangular matrix of size MxN
The algorithm post-multiplies the matrix by an elementary reflection
transformation which is given by column V and scalar Tau (see the
description of the GenerateReflection). Not the whole matrix but only a
part of it is transformed (rows from M1 to M2, columns from N1 to N2).
Only the elements of this submatrix are changed.
Input parameters:
C - matrix to be transformed.
Tau - scalar defining transformation.
V - column defining transformation.
Array whose index ranges within [1..N2-N1+1]
M1, M2 - range of rows to be transformed.
N1, N2 - range of columns to be transformed.
WORK - working array whose index goes from M1 to M2.
Output parameters:
C - the result of multiplying the input matrix C by the
transformation matrix which is given by Tau and V.
If N1>N2 or M1>M2, C is not modified.
-- LAPACK auxiliary routine (version 3.0) --
Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
Courant Institute, Argonne National Lab, and Rice University
September 30, 1994
*************************************************************************)
procedure ComplexApplyReflectionFromTheRight(var C : TComplex2DArray;
Tau : Complex;
var V : TComplex1DArray;
M1 : AlglibInteger;
M2 : AlglibInteger;
N1 : AlglibInteger;
N2 : AlglibInteger;
var WORK : TComplex1DArray);
var
T : Complex;
I : AlglibInteger;
VM : AlglibInteger;
i_ : AlglibInteger;
i1_ : AlglibInteger;
begin
if C_EqualR(Tau,0) or (N1>N2) or (M1>M2) then
begin
Exit;
end;
//
// w := C * v
//
VM := N2-N1+1;
I:=M1;
while I<=M2 do
begin
i1_ := (1)-(N1);
T := C_Complex(0.0);
for i_ := N1 to N2 do
begin
T := C_Add(T,C_Mul(C[I,i_],V[i_+i1_]));
end;
WORK[I] := T;
Inc(I);
end;
//
// C := C - w * conj(v^T)
//
for i_ := 1 to VM do
begin
V[i_] := Conj(V[i_]);
end;
I:=M1;
while I<=M2 do
begin
T := C_Mul(WORK[I],TAU);
i1_ := (1) - (N1);
for i_ := N1 to N2 do
begin
C[I,i_] := C_Sub(C[I,i_], C_Mul(T, V[i_+i1_]));
end;
Inc(I);
end;
for i_ := 1 to VM do
begin
V[i_] := Conj(V[i_]);
end;
end;
end.