-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathestnorm.pas
More file actions
394 lines (365 loc) · 10.9 KB
/
estnorm.pas
File metadata and controls
394 lines (365 loc) · 10.9 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
382
383
384
385
386
387
388
389
390
391
392
393
394
{$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 estnorm;
interface
uses Math, Sysutils, Ap;
procedure IterativeEstimate1Norm(N : AlglibInteger;
var V : TReal1DArray;
var X : TReal1DArray;
var ISGN : TInteger1DArray;
var EST : Double;
var KASE : AlglibInteger);
function DemoIterativeEstimate1Norm(const A : TReal2DArray;
N : AlglibInteger):Double;
implementation
(*************************************************************************
Matrix norm estimation
The algorithm estimates the 1-norm of square matrix A on the assumption
that the multiplication of matrix A by the vector is available (the
iterative method is used). It is recommended to use this algorithm if it
is hard to calculate matrix elements explicitly (for example, when
estimating the inverse matrix norm).
The algorithm uses back communication for multiplying the vector by the
matrix. If KASE=0 after returning from a subroutine, its execution was
completed successfully, otherwise it is required to multiply the returned
vector by matrix A and call the subroutine again.
The DemoIterativeEstimateNorm subroutine shows a simple example.
Parameters:
N - size of matrix A.
V - vector. It is initialized by the subroutine on the first
call. It is then passed into it on repeated calls.
X - if KASE<>0, it contains the vector to be replaced by:
A * X, if KASE=1
A^T * X, if KASE=2
Array whose index ranges within [1..N].
ISGN - vector. It is initialized by the subroutine on the first
call. It is then passed into it on repeated calls.
EST - if KASE=0, it contains the lower boundary of the matrix
norm estimate.
KASE - on the first call, it should be equal to 0. After the last
return, it is equal to 0 (EST contains the matrix norm),
on intermediate returns it can be equal to 1 or 2 depending
on the operation to be performed on vector X.
-- LAPACK auxiliary routine (version 3.0) --
Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
Courant Institute, Argonne National Lab, and Rice University
February 29, 1992
*************************************************************************)
procedure IterativeEstimate1Norm(N : AlglibInteger;
var V : TReal1DArray;
var X : TReal1DArray;
var ISGN : TInteger1DArray;
var EST : Double;
var KASE : AlglibInteger);
var
ITMAX : AlglibInteger;
I : AlglibInteger;
T : Double;
Flg : Boolean;
PosITER : AlglibInteger;
PosJ : AlglibInteger;
PosJLAST : AlglibInteger;
PosJUMP : AlglibInteger;
PosALTSGN : AlglibInteger;
PosESTOLD : AlglibInteger;
PosTEMP : AlglibInteger;
begin
ITMAX := 5;
PosALTSGN := N+1;
PosESTOLD := N+2;
PosTEMP := N+3;
PosITER := N+1;
PosJ := N+2;
PosJLAST := N+3;
PosJUMP := N+4;
if KASE=0 then
begin
SetLength(V, N+3+1);
SetLength(X, N+1);
SetLength(ISGN, N+4+1);
T := AP_Double(1)/N;
I:=1;
while I<=N do
begin
X[I] := T;
Inc(I);
end;
KASE := 1;
ISGN[PosJUMP] := 1;
Exit;
end;
//
// ................ ENTRY (JUMP = 1)
// FIRST ITERATION. X HAS BEEN OVERWRITTEN BY A*X.
//
if ISGN[PosJUMP]=1 then
begin
if N=1 then
begin
V[1] := X[1];
EST := ABSReal(V[1]);
KASE := 0;
Exit;
end;
EST := 0;
I:=1;
while I<=N do
begin
EST := EST+AbsReal(X[I]);
Inc(I);
end;
I:=1;
while I<=N do
begin
if AP_FP_Greater_Eq(X[I],0) then
begin
X[I] := 1;
end
else
begin
X[I] := -1;
end;
ISGN[I] := Sign(X[I]);
Inc(I);
end;
KASE := 2;
ISGN[PosJUMP] := 2;
Exit;
end;
//
// ................ ENTRY (JUMP = 2)
// FIRST ITERATION. X HAS BEEN OVERWRITTEN BY TRANDPOSE(A)*X.
//
if ISGN[PosJUMP]=2 then
begin
ISGN[PosJ] := 1;
I:=2;
while I<=N do
begin
if AP_FP_Greater(AbsReal(X[I]),AbsReal(X[ISGN[PosJ]])) then
begin
ISGN[PosJ] := I;
end;
Inc(I);
end;
ISGN[PosITER] := 2;
//
// MAIN LOOP - ITERATIONS 2,3,...,ITMAX.
//
I:=1;
while I<=N do
begin
X[I] := 0;
Inc(I);
end;
X[ISGN[PosJ]] := 1;
KASE := 1;
ISGN[PosJUMP] := 3;
Exit;
end;
//
// ................ ENTRY (JUMP = 3)
// X HAS BEEN OVERWRITTEN BY A*X.
//
if ISGN[PosJUMP]=3 then
begin
APVMove(@V[0], 1, N, @X[0], 1, N);
V[PosESTOLD] := EST;
EST := 0;
I:=1;
while I<=N do
begin
EST := EST+AbsReal(V[I]);
Inc(I);
end;
Flg := False;
I:=1;
while I<=N do
begin
if AP_FP_Greater_Eq(X[I],0) and (ISGN[I]<0) or AP_FP_Less(X[I],0) and (ISGN[I]>=0) then
begin
Flg := True;
end;
Inc(I);
end;
//
// REPEATED SIGN VECTOR DETECTED, HENCE ALGORITHM HAS CONVERGED.
// OR MAY BE CYCLING.
//
if not Flg or AP_FP_Less_Eq(EST,V[PosESTOLD]) then
begin
V[PosALTSGN] := 1;
I:=1;
while I<=N do
begin
X[I] := V[PosALTSGN]*(1+AP_Double((I-1))/(N-1));
V[PosALTSGN] := -V[PosALTSGN];
Inc(I);
end;
KASE := 1;
ISGN[PosJUMP] := 5;
Exit;
end;
I:=1;
while I<=N do
begin
if AP_FP_Greater_Eq(X[I],0) then
begin
X[I] := 1;
ISGN[I] := 1;
end
else
begin
X[I] := -1;
ISGN[I] := -1;
end;
Inc(I);
end;
KASE := 2;
ISGN[PosJUMP] := 4;
Exit;
end;
//
// ................ ENTRY (JUMP = 4)
// X HAS BEEN OVERWRITTEN BY TRANDPOSE(A)*X.
//
if ISGN[PosJUMP]=4 then
begin
ISGN[PosJLAST] := ISGN[PosJ];
ISGN[PosJ] := 1;
I:=2;
while I<=N do
begin
if AP_FP_Greater(AbsReal(X[I]),AbsReal(X[ISGN[PosJ]])) then
begin
ISGN[PosJ] := I;
end;
Inc(I);
end;
if AP_FP_Neq(X[ISGN[PosJLAST]],ABSReal(X[ISGN[PosJ]])) and (ISGN[PosITER]<ITMAX) then
begin
ISGN[PosITER] := ISGN[PosITER]+1;
I:=1;
while I<=N do
begin
X[I] := 0;
Inc(I);
end;
X[ISGN[PosJ]] := 1;
KASE := 1;
ISGN[PosJUMP] := 3;
Exit;
end;
//
// ITERATION COMPLETE. FINAL STAGE.
//
V[PosALTSGN] := 1;
I:=1;
while I<=N do
begin
X[I] := V[PosALTSGN]*(1+AP_Double((I-1))/(N-1));
V[PosALTSGN] := -V[PosALTSGN];
Inc(I);
end;
KASE := 1;
ISGN[PosJUMP] := 5;
Exit;
end;
//
// ................ ENTRY (JUMP = 5)
// X HAS BEEN OVERWRITTEN BY A*X.
//
if ISGN[PosJUMP]=5 then
begin
V[PosTEMP] := 0;
I:=1;
while I<=N do
begin
V[PosTEMP] := V[PosTEMP]+AbsReal(X[I]);
Inc(I);
end;
V[PosTEMP] := 2*V[PosTEMP]/(3*N);
if AP_FP_Greater(V[PosTEMP],EST) then
begin
APVMove(@V[0], 1, N, @X[0], 1, N);
EST := V[PosTEMP];
end;
KASE := 0;
Exit;
end;
end;
(*************************************************************************
Example of usage of an IterativeEstimateNorm subroutine
Input parameters:
A - matrix.
Array whose indexes range within [1..N, 1..N].
Return:
Matrix norm estimated by the subroutine.
-- ALGLIB --
Copyright 2005 by Bochkanov Sergey
*************************************************************************)
function DemoIterativeEstimate1Norm(const A : TReal2DArray;
N : AlglibInteger):Double;
var
I : AlglibInteger;
S : Double;
X : TReal1DArray;
T : TReal1DArray;
V : TReal1DArray;
IV : TInteger1DArray;
KASE : AlglibInteger;
i_ : AlglibInteger;
begin
KASE := 0;
SetLength(T, N+1);
IterativeEstimate1Norm(N, V, X, IV, Result, KASE);
while KASE<>0 do
begin
if KASE=1 then
begin
I:=1;
while I<=N do
begin
S := APVDotProduct(@A[I][0], 1, N, @X[0], 1, N);
T[I] := S;
Inc(I);
end;
end
else
begin
I:=1;
while I<=N do
begin
S := 0.0;
for i_ := 1 to N do
begin
S := S + A[i_,I]*X[i_];
end;
T[I] := S;
Inc(I);
end;
end;
APVMove(@X[0], 1, N, @T[0], 1, N);
IterativeEstimate1Norm(N, V, X, IV, Result, KASE);
end;
end;
end.