forked from mikerabat/mrmath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Eigensystems.pas
2115 lines (1840 loc) · 79 KB
/
Eigensystems.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ###################################################################
// #### This file is part of the mathematics library project, and is
// #### offered under the licence agreement described on
// #### http://www.mrsoft.org/
// ####
// #### Copyright:(c) 2011, 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.
// ###################################################################
unit Eigensystems;
// ############################################
// #### Functions to extract eigenvalues and eigenvectors
// ############################################
interface
uses MatrixConst;
// ############################################
// #### functions for nonsymmetric matrices:
// executes the functions below in order to get the result.
function MatrixUnsymEigVecInPlace(A : PDouble; const LineWidthA : TASMNativeInt; width : TASMNativeInt; WR : PDouble;
const LineWidthWR : TASMNativeInt; WI : PDouble; const LineWidthWI : TASMNativeInt; Eivec : PDouble; const LineWidthEivec : TASMNativeInt; balance : boolean) : TEigenvalueConvergence;
function MatrixUnsymEigVec(const A : PDouble; const LineWidthA : TASMNativeInt; width : TASMNativeInt; WR : PDouble;
const LineWidthWR : TASMNativeInt; WI : PDouble; const LineWidthWI : TASMNativeInt; Eivec : PDouble; const LineWidthEivec : TASMNativeInt; balance : boolean) : TEigenvalueConvergence;
function MatrixUnsymEigInPlace(A : PDouble; const LineWidthA : TASMNativeInt; width : TASMNativeInt; WR : PDouble;
const LineWidthWR : TASMNativeInt; WI : PDouble; const LineWidthWI : TASMNativeInt; balance : boolean) : TEigenvalueConvergence;
function MatrixUnsymEig(const A : PDouble; const LineWidthA : TASMNativeInt; width : TASMNativeInt; WR : PDouble;
const LineWidthWR : TASMNativeInt; WI : PDouble; const LineWidthWI : TASMNativeInt; balance : boolean) : TEigenvalueConvergence;
// Given a Matrix A[0..width-1][0..Width-1], this routine replaces it by a balanced matrix
// with identical eigenvalues. A symmetric matrix is already balanced and is unaffected by this procedure.
procedure MatrixBalanceInPlace(A : PDouble; const LineWidthA : TASMNativeInt; width : TASMNativeInt; Scale : PDouble; const LineWidthScale : TASMNativeInt);
procedure MatrixBalanceBackInPlace(Eivec : PDouble; const LineWidthEivec : TASMNativeInt; width : TASMNativeInt; Scale : PDouble; const LineWidthScale : TASMNativeInt);
// Reduction of Matrix A to hessenberg form by the elimination method. The real, nonsymmetric matrix
// A is replaced by an upper Hessenberg matrix with identical eigenvalues. Recommended, but not
// required, i sthat this routine be preceded by MatrixBalance. Non Hessenberg elements (which should be zero)
// are filled with random values and not replaced by zero elements.
procedure MatrixHessenbergPermInPlace(A : PDouble; const LineWidthA : TASMNativeInt; width : TASMNativeInt; perm : PInteger; const LineWidthPerm : TASMNativeInt);
procedure MatrixHessenbergPerm(dest : PDouble; const LineWidthDest : TASMNativeInt; A : PDouble; const LineWidthA : TASMNativeInt; width : TASMNativeInt; perm : PInteger; const LineWidthPerm : TASMNativeInt);
procedure MatrixHessenbergInPlace(A : PDouble; const LineWidthA : TASMNativeInt; width : TASMNativeInt);
procedure MatrixHessenberg(dest : PDouble; const LineWidthDest : TASMNativeInt; A : PDouble; const LineWidthA : TASMNativeInt; width : TASMNativeInt);
// copies the hessenberg matrix to dest using the permutation vector from the hessenberg transformation. Initializes the destination matrix for the eigenvector
// finding routine for unsymmetric matrices
procedure MatrixInitEivecHess(hess : PDouble; const LineWidthHess : TASMNativeInt; width : TASMNativeInt; dest : PDouble; const LineWidthDest : TASMNativeInt; perm : PInteger; const LineWidthPerm : TASMNativeInt);
// Finds all eigenvalues of an upper Hessenberg matrix A. On input a can be exactly as output from MatrixHessenberg, on output
// A is destroyed. The real and imaginary parts of the eigenvalues are returned in wr[0..width-1] and wi[0..width-1], respectively
function MatrixEigHessenbergInPlace(A : PDouble; const LineWidthA : TASMNativeInt; width : TASMNativeInt; WR : PDouble;
const LineWidthWR : TASMNativeInt; WI : PDouble; const LineWidthWI : TASMNativeInt) : TEigenvalueConvergence;
// the following function does not destroy A on output.
function MatrixEigHessenberg(A : PDouble; const LineWidthA : TASMNativeInt; width : TASMNativeInt; WR : PDouble;
const LineWidthWR : TASMNativeInt; WI : PDouble; const LineWidthWI : TASMNativeInt) : TEigenvalueConvergence;
// finds all eigenvalues and eigenvectors in an upper hessenberg matrix A. On input A can be exactly as output from MatrixHessenbergPerm, on
// output A is destroyed. For complex eigenvalues (n, n+1) only one eigenvector is stored whereas the real part is stored in vector n and
// the imaginary part is stored in n+1. Note the function does not seem to correctly work with matrices with rank lower than width!
function MatrixEigVecHessenbergInPlace(A : PDouble; const LineWidthA : TASMNativeInt; width : TASMNativeInt; WR : PDouble;
const LineWidthWR : TASMNativeInt; WI : PDouble; const LineWidthWI : TASMNativeInt; Eivec : PDouble; const LineWidthEivec : TASMNativeInt) : TEigenvalueConvergence;
procedure MatrixNormEivecInPlace(Eivec : PDouble; const LineWidthEivec : TASMNativeInt; width : TASMNativeInt; WI : PDouble; const LineWidthWI : TASMNativeInt);
// ############################################
// #### functions for symmetric matrices:
// Householder reduction of a real, symmetric matrix A[0..width-1][0..width-1]. On output
// A is replaced by the orthogonal matrix Q effecting the transformation. D[0..width-1] returns
// the diagonal elements of the tridiagonal matrix, and e[0..width - 1] of the off-diagonal elements with e[0] = 0.
procedure MatrixTridiagonalHouseInPlace(A : PDouble; const LineWidthA : TASMNativeInt; const width : TASMNativeInt;
D : PDouble; const LineWidthD : TASMNativeInt; E : PDouble; const LineWidthE : TASMNativeInt);
procedure MatrixTridiagonalHouse(dest : PDouble; const LineWidthDest : TASMNativeInt; A : PDouble; const LineWidthA : TASMNativeInt; const width : TASMNativeInt;
D : PDouble; const LineWidthD : TASMNativeInt; E : PDouble; const LineWidthE : TASMNativeInt);
function MatrixTridiagonalQLImplicitInPlace(Z : PDouble; const LineWidthZ : TASMNativeInt; width : TASMNativeInt;
D : PDouble; const LineWidthD : TASMNativeInt; E : PDouble; const LineWidthE : TASMNativeInt) : TEigenvalueConvergence;
function MatrixEigTridiagonalMatrixInPlace(A : PDouble; const LineWidthA : TASMNativeInt; width : TASMNativeInt; EigVals : PDouble; const LineWidthEigVals : TASMNativeInt) : TEigenvalueConvergence;
function MatrixEigTridiagonalMatrix(Dest : PDouble; const LineWidthDest : TASMNativeInt; A : PDouble; const LineWidthA : TASMNativeInt; width : TASMNativeInt; EigVals : PDouble; const LineWidthEigVals : TASMNativeInt) : TEigenvalueConvergence;
implementation
uses Math, MathUtilFunc;
function MatrixEigTridiagonalMatrixInPlace(A : PDouble; const LineWidthA : TASMNativeInt; width : TASMNativeInt; EigVals : PDouble; const LineWidthEigVals : TASMNativeInt) : TEigenvalueConvergence;
var E : Array of double;
begin
SetLength(E, width);
MatrixTridiagonalHouseInPlace(A, LineWidthA, width, EigVals, LineWidthEigVals, @E[0], sizeof(double));
Result := MatrixTridiagonalQLImplicitInPlace(A, LineWidthA, width, EigVals, LineWidthEigVals, @E[0], sizeof(double));
end;
function MatrixEigTridiagonalMatrix(Dest : PDouble; const LineWidthDest : TASMNativeInt; A : PDouble; const LineWidthA : TASMNativeInt; width : TASMNativeInt; EigVals : PDouble; const LineWidthEigVals : TASMNativeInt) : TEigenvalueConvergence;
var E : Array of double;
begin
SetLength(E, width);
MatrixTridiagonalHouse(dest, LineWidthDest, A, LineWidthA, width, EigVals, LineWidthEigVals, @E[0], sizeof(double));
Result := MatrixTridiagonalQLImplicitInPlace(dest, LineWidthDest, width, EigVals, LineWidthEigVals, @E[0], sizeof(double));
end;
procedure MatrixTridiagonalHouse(dest : PDouble; const LineWidthDest : TASMNativeInt; A : PDouble; const LineWidthA : TASMNativeInt; const width : TASMNativeInt;
D : PDouble; const LineWidthD : TASMNativeInt; E : PDouble; const LineWidthE : TASMNativeInt);
var pDest : PDouble;
i : TASMNativeInt;
begin
Assert(width > 0, 'Dimension Error');
Assert(LineWidthA >= width*sizeof(double), 'Dimension error');
Assert(LineWidthD >= sizeof(double), 'Dimension error');
Assert(LineWidthE >= sizeof(double), 'Dimension error');
pDest := dest;
// copy data -> now we can perform an inline LU decomposition
for i := 0 to width - 1 do
begin
Move(A^, pDest^, sizeof(double)*width);
inc(PByte(A), LineWidthA);
inc(PByte(pDest), LineWidthDest);
end;
MatrixTridiagonalHouseInPlace(dest, LineWidthDest, width, D, LineWidthD, E, LineWidthE);
end;
procedure MatrixTridiagonalHouseInPlace(A : PDouble; const LineWidthA : TASMNativeInt; const width : TASMNativeInt;
D : PDouble; const LineWidthD : TASMNativeInt; E : PDouble; const LineWidthE : TASMNativeInt);
var i, j : TASMNativeInt;
l, k : TASMNativeInt;
scale, hh, h, g, f : double;
pA, pAi, pAj, pAk : PDouble;
pEi, pEj, pEk : PDouble;
pD : PDouble;
begin
Assert(width > 0, 'Dimension Error');
Assert(LineWidthA >= width*sizeof(double), 'Dimension error');
Assert(LineWidthD >= sizeof(double), 'Dimension error');
Assert(LineWidthE >= sizeof(double), 'Dimension error');
pAi := A;
inc(PByte(pAi), (width-1)*LineWidthA);
pEi := E;
inc(PByte(pEi), (width-1)*LineWidthE);
for i := width - 1 downto 1 do
begin
l := i - 1;
h := 0;
scale := 0;
if l > 0 then
begin
pA := pAi;
for k := 0 to l do
begin
scale := scale + abs(pA^);
inc(pA);
end;
if scale = 0 then
begin
// skip transformation
dec(pA);
pEi^ := pA^;
end
else
begin
pA := pAi;
for k := 0 to l do
begin
pA^ := pA^/scale; // use scaled a's for transformation
h := h + sqr(pA^);
inc(pA);
end;
dec(pA);
f := pA^;
if f >= 0
then
g := -sqrt(h)
else
g := sqrt(h);
pEi^ := scale*g;
h := h - f*g;
pA^ := f - g; // store u in the ith row of a
f := 0;
pAj := A;
pEj := E;
for j := 0 to l do
begin
pA := pAj;
inc(pA, i);
pAk := pAi;
inc(pAk, j);
// next statement can be omitted if eigenvectors not wanted
pA^ := pAk^/h; // store u/H in ith column of a.
g := 0;
// Form an element of A*u in g
pAk := pAj;
pA := pAi;
for k := 0 to j do
begin
g := g + pA^*pAk^;
inc(pAk);
inc(pA);
end;
pAk := pAj;
pA := pAi;
inc(pA, j + 1);
inc(PByte(pAk), LineWidthA);
inc(pAk, j);
for k := j + 1 to l do
begin
g := g + pA^*pAk^;
inc(PByte(pAk), LineWidthA);
inc(pA);
end;
pEj^ := g/h;
pA := pAi;
inc(pA, j);
f := f + pEj^*pA^;
// next line
inc(PByte(pEj), LineWidthE);
inc(PByte(pAj), LineWidthA);
end;
hh := f/(h + h);
pAj := A;
pEj := E;
for j := 0 to l do
begin
pA := pAi;
inc(pA, j);
f := pA^;
pEj^ := pEj^ - hh*f;
g := pEj^;
pAk := pAj;
pEk := E;
pA := pAi;
for k := 0 to j do
begin
pAk^ := pAk^ - (f*pEk^ + g*pA^);
inc(pA);
inc(pAk);
inc(PByte(pEk), LineWidthE);
end;
inc(PByte(pAj), LineWidthA);
inc(PByte(pEj), LineWidthE);
end;
end;
end
else
begin
pA := pAi;
inc(pA, l);
pEi^ := pA^;
end;
pD := D;
inc(PByte(pD), i*LineWidthD);
pD^ := h;
dec(PByte(pAi), LineWidthA);
dec(PByte(pEi), LineWidthE);
end;
// next statement can be omitted if eigenvectors not wanted
D^ := 0;
E^ := 0;
// contents of this loop can be omitted if eigenvectors not wanted except for statement d[i] = a[i][i]
// begin accumulation of transformation matrices.
pAi := A;
pD := D;
for i := 0 to width - 1 do
begin
l := i - 1;
if (pD^ <> 0) then
begin
for j := 0 to l do
begin
g := 0;
pA := pAi;
pAk := A;
inc(pAk, j);
for k := 0 to l do
begin
g := g + pA^*pAk^;
inc(pA);
inc(PByte(pAk), LineWidthA);
end;
pA := A;
inc(pA, j);
pAk := A;
inc(pAk, i);
for k := 0 to l do
begin
pA^ := pA^ - g*pAk^;
inc(PByte(pA), LineWidthA);
inc(PByte(pAk), LineWidthA);
end;
end;
end;
pA := pAi;
inc(pA, i);
pD^ := pA^;
pA^ := 1;
pAj := A;
inc(pAj, i);
pA := pAi;
for j := 0 to l do
begin
pAj^ := 0;
pA^ := 0;
inc(PByte(pAj), LineWidthA);
inc(pA);
end;
inc(PByte(pAi), LineWidthA);
inc(PByte(pD), LineWidthD);
end;
end;
function MatrixTridiagonalQLImplicitInPlace(Z : PDouble; const LineWidthZ : TASMNativeInt; width : TASMNativeInt;
D : PDouble; const LineWidthD : TASMNativeInt; E : PDouble; const LineWidthE : TASMNativeInt) : TEigenvalueConvergence;
var m, l, iter, i, k : TASMNativeInt;
s, r, p, g, f, dd, c, b : double;
pE, pEl, pEi : PDouble;
pD, pDl : PDouble;
pZ, pZi : PDouble;
const cMaxTridiagIter = 30;
begin
pE := E;
pEi := E;
inc(PByte(pEi), LineWidthE);
Result := qlNoConverge;
for i := 1 to width - 1 do
begin
pE^ := pEi^;
inc(PByte(pE), LineWidthE);
inc(PByte(pEi), LineWidthE);
end;
pE := E;
inc(PByte(pE), (width - 1)*LineWidthE);
pE^ := 0;
pDl := D;
pEl := E;
for l := 0 to width - 1 do
begin
iter := 0;
repeat
pD := pDl;
pE := pEl;
m := l;
while m < width - 1 do
begin
dd := abs(pD^);
inc(PByte(pD), LineWidthD);
dd := dd + abs(pD^);
if pE^ + dd = dd then
break;
inc(PByte(pE), LineWidthE);
inc(m);
end;
if m <> l then
begin
inc(iter);
if iter = cMaxTridiagIter then
exit;
g := (PDouble(PAnsiChar(pDl) + LineWidthD)^ - pDl^)/(2*pEl^);
r := pythag(g, 1);
pD := D;
inc(PByte(pD), m*LineWidthD);
g := pD^ - pDl^ + pEl^/(g + sign(r, g));
s := 1;
c := 1;
p := 0;
pE := E;
inc(PByte(pE), (m - 1)*LineWidthE);
for i := m - 1 downto l do
begin
f := s*pE^;
b := c*pE^;
r := pythag(f, g);
inc(PByte(pE), LineWidthE);
pE^ := r;
if r = 0 then
begin
pD^ := pD^ - p;
pE := E;
inc(PByte(pE), m*LineWidthE);
pE^ := 0;
break;
end;
s := f/r;
c := g/r;
g := pD^ - p;
dec(PByte(pD), LineWidthD);
r := (pD^ - g)*s + 2*c*b;
p := s*r;
inc(PByte(pD), LineWidthD);
pD^ := g + p;
g := c*r - b;
// next loop can be omitted if eigenvectors not wanted
pZ := Z;
pZi := Z;
inc(pZ, i);
inc(pZi, i+1);
for k := 0 to width - 1 do
begin
f := pZi^;
pZi^ := s*pZ^ + c*f;
pZ^ := c*pZ^ - s*f;
inc(PByte(pZi), LineWidthZ);
inc(PByte(pZ), LineWidthZ);
end;
dec(PByte(pD), LineWidthD);
dec(PByte(pE), 2*LineWidthE);
end;
if (r = 0) and (i >= l) then
continue;
pDl^ := pDl^ - p;
pEl^ := g;
pE := E;
inc(PByte(pE), m*LineWidthE);
pE^ := 0;
end;
until m = l;
inc(PByte(pDl), LineWidthD);
inc(PByte(pEl), LineWidthE);
end;
Result := qlOk;
end;
procedure MatrixBalanceBackInPlace(Eivec : PDouble; const LineWidthEivec : TASMNativeInt; width : TASMNativeInt; Scale : PDouble; const LineWidthScale : TASMNativeInt);
var i, j : TASMNativeInt;
pEiveci : PDouble;
pEivecj : PDouble;
pScale : PDouble;
begin
pEiveci := Eivec;
pScale := Scale;
for i := 0 to width - 1 do
begin
pEivecj := pEiveci;
for j := 0 to width - 1 do
begin
pEivecj^ := pEivecj^*pScale^;
inc(pEivecj);
end;
inc(PByte(pEiveci), LineWidthEivec);
inc(PByte(pScale), LineWidthScale);
end;
end;
procedure MatrixBalanceInPlace(A : PDouble; const LineWidthA : TASMNativeInt; width : TASMNativeInt; Scale : PDouble; const LineWidthScale : TASMNativeInt);
var j, i : TASMNativeInt;
last : boolean;
s, r, g, f, c, sqrdx : double;
pAi, pAj, pA, pScale : PDouble;
const RADIX = 2.0;
begin
assert(lineWidthA >= width*sizeof(double), 'Dimension error');
assert(LineWidthScale >= sizeof(double), 'Dimension error');
sqrdx := sqr(RADIX);
last := False;
pScale := Scale;
for i := 0 to width - 1 do
begin
pScale^ := 1;
inc(PByte(pScale), LineWidthScale);
end;
while last = False do
begin
last := True;
pAi := A;
pScale := Scale;
for i := 0 to width - 1 do
begin
r := 0;
c := 0;
pA := pAi;
pAj := A;
inc(pAj, i);
for j := 0 to width - 1 do
begin
if i <> j then
begin
c := c + abs(pAj^);
r := r + abs(pA^);
end;
inc(pA);
inc(PByte(pAj), LineWidthA);
end;
g := r/RADIX;
f := 1;
s := c + r;
// find the integer power of the machine radix that comes closest to balancing the matrix.
while c < g do
begin
f := f*RADIX;
c := c*sqrdx;
end;
g := r*RADIX;
while c > g do
begin
f := f/RADIX;
c := c/sqrdx;
end;
if (c + r)/f < 0.95*s then
begin
pScale^ := pScale^*f;
last := False;
g := 1/f;
// Apply similarity transformation
pA := pAi;
for j := 0 to width - 1 do
begin
pA^ := pA^*g;
inc(pA);
end;
pAj := A;
inc(pAj, i);
for j := 0 to width - 1 do
begin
pAj^ := pAj^*f;
inc(PByte(pAj), LineWidthA);
end;
end;
inc(PByte(pAi), LineWidthA);
inc(PByte(pScale), LineWidthScale);
end;
end;
end;
procedure MatrixHessenbergPerm(dest : PDouble; const LineWidthDest : TASMNativeInt; A : PDouble; const LineWidthA : TASMNativeInt; width : TASMNativeInt; perm : PInteger; const LineWidthPerm : TASMNativeInt);
var pDest : PDouble;
i : TASMNativeInt;
begin
assert(LineWidthA >= width*sizeof(double), 'Dimension Error');
assert(LineWidthDest >= width*sizeof(double), 'Dimension Error');
pDest := dest;
// copy data -> now we can perform an inline LU decomposition
for i := 0 to width - 1 do
begin
Move(A^, pDest^, sizeof(double)*width);
inc(PByte(A), LineWidthA);
inc(PByte(pDest), LineWidthDest);
end;
MatrixHessenbergPermInPlace(dest, LineWidthDest, width, perm, LineWidthPerm);
end;
procedure MatrixHessenbergPermInPlace(A : PDouble; const LineWidthA : TASMNativeInt; width : TASMNativeInt; perm : PInteger; const LineWidthPerm : TASMNativeInt);
var m, j, i : TASMNativeInt;
x, y : double;
pAm, pAj, pAi : PDouble;
pA : PDouble;
pPerm : PInteger;
begin
assert(LineWidthA >= width*sizeof(double), 'Dimension Error');
pAm := A;
pPerm := Perm;
if Assigned(pPerm) then
begin
pPerm^ := 0;
inc(PByte(pPerm), LineWidthPerm);
end;
m := 1;
while m < width - 1 do
begin
inc(PByte(pAm), LineWidthA);
x := 0;
i := m;
// find the pivot
pAj := pAm;
inc(pAj, m - 1);
for j := m to width - 1 do
begin
if abs(pAj^) > abs(x) then
begin
x := pAj^;
i := j;
end;
inc(PByte(pAj), LineWidthA);
end;
if Assigned(pPerm) then
begin
pPerm^ := i;
inc(PByte(pPerm), LineWidthPerm);
end;
// interchange rows and columns
if i <> m then
begin
pAi := A;
inc(pAi, m - 1);
inc(PByte(pAi), i*LineWidthA);
pAj := pAm;
inc(pAj, m - 1);
for j := m - 1 to width - 1 do
begin
DoubleSwap(pAi^, pAj^);
inc(pAj);
inc(pAi);
end;
pAi := A;
inc(pAi, i);
pAj := A;
inc(pAj, m);
for j := 0 to width - 1 do
begin
DoubleSwap(pAi^, pAj^);
inc(PByte(pAi), LineWidthA);
inc(PByte(pAj), LineWidthA);
end;
end;
// carry out the elimination
if x <> 0 then
begin
pA := pAm;
inc(PByte(pA), LineWidthA);
inc(pA, m - 1);
i := m + 1;
while i < width do
begin
y := pA^;
if y <> 0 then
begin
y := y/x;
pA^ := y;
pAi := A;
inc(pAi, m);
inc(PByte(pAi), i*LineWidthA);
pAj := pAm;
inc(pAj, m);
for j := m to width - 1 do
begin
pAi^ := pAi^ - y*pAj^;
inc(pAj);
inc(pAi);
end;
pAi := A;
inc(pAi, i);
pAj := A;
inc(pAj, m);
for j := 0 to width - 1 do
begin
pAj^ := pAj^ + y*pAi^;
inc(PByte(pAi), LineWidthA);
inc(PByte(pAj), LineWidthA);
end;
end;
inc(PByte(pA), LineWidthA);
inc(i);
end;
end;
inc(m);
end;
end;
function MatrixEigHessenberg(A : PDouble; const LineWidthA : TASMNativeInt; width : TASMNativeInt; WR : PDouble;
const LineWidthWR : TASMNativeInt; WI : PDouble; const LineWidthWI : TASMNativeInt) : TEigenvalueConvergence;
var pDest : PDouble;
dest : Array of double;
i : TASMNativeInt;
begin
Assert(width > 0, 'Dimension Error');
Assert(LineWidthA >= width*sizeof(double), 'Dimension error');
setLength(dest, width*width);
pDest := @dest[0];
// copy data
for i := 0 to width - 1 do
begin
Move(A^, pDest^, sizeof(double)*width);
inc(PByte(A), LineWidthA);
inc(PByte(pDest), width*sizeof(double));
end;
Result := MatrixEigHessenbergInPlace(@dest[0], width*sizeof(double), width, WR, LineWidthWR, WI, LineWidthWI);
end;
function MatrixEigHessenbergInPlace(A : PDouble; const LineWidthA : TASMNativeInt; width : TASMNativeInt; WR : PDouble;
const LineWidthWR : TASMNativeInt; WI : PDouble; const LineWidthWI : TASMNativeInt) : TEigenvalueConvergence;
var nn, m, l, k : TASMNativeInt;
j, its, i, mmin : TASMNativeInt;
x, y, z : double;
u, v, w : double;
r, s, t : double;
q, p, anorm : double;
pAi, pAj, pAl, pA, pAm, pAk : PDouble;
pWr : PDouble;
pWi : PDouble;
const cMaxEigHessenbergIter = 30;
begin
Assert(width > 0, 'Dimension Error');
Assert(LineWidthA >= width*sizeof(double), 'Dimension error');
Assert(LineWidthWR >= sizeof(double), 'Dimension error');
Assert(LineWidthWI >= sizeof(double), 'Dimension error');
r := 0;
p := 0;
q := 0;
Result := qlNoConverge;
anorm := abs(A^);
pAi := A;
inc(PByte(pAi), LineWidthA);
// compute matrix norm for possible use in locating single small subdiagonal element
for i := 1 to width - 1 do
begin
pAj := pAi;
inc(pAj, i - 1);
for j := i - 1 to width - 1 do
begin
anorm := anorm + abs(pAj^);
inc(pAj);
end;
inc(PByte(pAi), LineWidthA);
end;
nn := width - 1;
t := 0;
// gets changed only by an exceptional shift.
while nn >= 0 do
begin
// begin search of the next eigenvalue
its := 0;
repeat
// begin iteration: look for single small subdiagonal element.
pAl := A;
inc(pAl, nn);
inc(PByte(pAl), nn*LineWidthA);
l := nn;
while l >= 1 do
begin
s := abs(pAl^);
dec(pAl);
dec(PByte(pAl), LineWidthA);
s := s + abs(pAl^);
if s = 0 then
s := anorm;
inc(PByte(pAl), LineWidthA);
if abs(pAl^) + s = s then
break;
dec(PByte(pAl), LineWidthA);
dec(l);
end;
pA := A;
inc(pA, nn);
inc(PByte(pA), nn*LineWidthA);
x := pA^;
// one root found
if l = nn then
begin
pWr := WR;
inc(PByte(pWr), nn*LineWidthWR);
pWr^ := x + t;
pWi := WI;
inc(PByte(pWi), nn*LineWidthWI);
pWi^ := 0;
dec(nn);
end
else
begin
dec(pA);
dec(PByte(pA), LineWidthA);
y := pA^;
inc(pA);
w := pA^;
dec(pA);
inc(PByte(pA), LineWidthA);
w := w*pA^;
// two roots found ...
if l = nn - 1 then
begin
p := 0.5*(y - x);
q := sqr(p) + w;
z := sqrt(abs(q));
x := x + t;
pWr := WR;
inc(PByte(pWr), (nn-1)*LineWidthWR);
pWi := WI;
inc(PByte(pWi), (nn-1)*LineWidthWI);
// ... a real pair
if q >= 0 then
begin
z := p + sign(z, p);
pWr^ := x + z;
inc(PByte(pWr), LineWidthWR);
pWr^ := x + z;
if z <> 0 then
pWr^ := x - w/z;
pWi^ := 0;
inc(PByte(pWi), LineWidthWI);
pWi^ := 0;
end
else
begin
// ...a complex pair
pWr^ := x + p;
inc(PByte(pWr), LineWidthWR);
pWr^ := x + p;
pWi^ := -z;
inc(PByte(pWi), LineWidthWI);
pWi^ := z;
end;
dec(nn, 2);
end
else
begin
// no roots found. Continue iteration
if its = cMaxEigHessenbergIter then
exit;
// form exceptional shift
if (its = 10) or (its = 20) then
begin
t := t + x;
pAi := A;
for i := 0 to width - 1 do
begin
pAi^ := pAi^ - x;
inc(pAi);
inc(PByte(pAi), LineWidthA);
end;
pA := A;
inc(pA, nn - 1);
inc(PByte(pA), nn*LineWidthA);
s := abs(pA^);
dec(pA);
dec(PByte(pA), LineWidthA);
s := s + abs(pA^);
x := 0.75*s;
y := x;
w := -0.4375*sqr(s);
end;
inc(its);
// form shift and the look for 2 consecutive small subdiagonal elements
pAm := A;
inc(pAm, nn - 2);
inc(PByte(pAm), (nn - 2)*LineWidthA);
m := nn - 2;
while m >= l do
begin
z := pAm^;
r := x - z;
s := y - z;
pAi := pAm;
inc(pAi);
pAl := pAm;
inc(PByte(pAl), LineWidthA);
p := (r*s - w)/pAl^ + pAi^;
inc(pAl);
q := pAl^ - z - r - s;
inc(PByte(pAl), LineWidthA);
r := pAl^;
// scale to prevent under- or overflow
s := abs(p) + abs(q) + abs(r);
p := p/s;
q := q/s;
r := r/s;
if m = l then
break;
pAi := pAm;
dec(pAi);
u := abs(pAi^)*(abs(q) + abs(r));
dec(PByte(pAi), LineWidthA);
dec(PByte(pAl), LineWidthA);
v := abs(p)*(abs(pAi^) + abs(z) + abs(pAl^));
if u + v = v then
break;
dec(pAm);
dec(PByte(pAm), LineWidthA);
dec(m);
end;
pAi := A;
inc(pAi, m);
inc(PByte(pAi), (m + 2)*LineWidthA);
for i := m + 2 to nn do
begin
pAi^ := 0;
if i <> m + 2 then
begin
dec(pAi);
pAi^ := 0;
inc(pAi);
end;
inc(pAi);
inc(PByte(pAi), LineWidthA);
end;
// double QR step on rows l to nn and columns m to nn
pAk := A;
inc(pAk, m - 1);
inc(PByte(pAk), m*LineWidthA);
for k := m to nn - 1 do
begin
if k <> m then
begin
p := pAk^;
inc(PByte(pAk), LineWidthA);
q := pAk^;
r := 0;
if k <> nn - 1 then
begin
inc(PByte(pAk), LineWidthA);
r := pAk^;
dec(PByte(pAk), LineWidthA);