forked from danielfppps/hydrobuddy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevd.pas
More file actions
6632 lines (6256 loc) · 202 KB
/
evd.pas
File metadata and controls
6632 lines (6256 loc) · 202 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
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
{$MODESWITCH RESULT+}
{$GOTO ON}
(*************************************************************************
Copyright (c) 2005-2007, Sergey Bochkanov (ALGLIB project).
>>> 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 evd;
interface
uses Math, Sysutils, Ap, hblas, reflections, creflections, sblas, ablasf, ablas, ortfac, blas, rotations, hsschur;
function SMatrixEVD(A : TReal2DArray;
N : AlglibInteger;
ZNeeded : AlglibInteger;
IsUpper : Boolean;
var D : TReal1DArray;
var Z : TReal2DArray):Boolean;
function SMatrixEVDR(A : TReal2DArray;
N : AlglibInteger;
ZNeeded : AlglibInteger;
IsUpper : Boolean;
B1 : Double;
B2 : Double;
var M : AlglibInteger;
var W : TReal1DArray;
var Z : TReal2DArray):Boolean;
function SMatrixEVDI(A : TReal2DArray;
N : AlglibInteger;
ZNeeded : AlglibInteger;
IsUpper : Boolean;
I1 : AlglibInteger;
I2 : AlglibInteger;
var W : TReal1DArray;
var Z : TReal2DArray):Boolean;
function HMatrixEVD(A : TComplex2DArray;
N : AlglibInteger;
ZNeeded : AlglibInteger;
IsUpper : Boolean;
var D : TReal1DArray;
var Z : TComplex2DArray):Boolean;
function HMatrixEVDR(A : TComplex2DArray;
N : AlglibInteger;
ZNeeded : AlglibInteger;
IsUpper : Boolean;
B1 : Double;
B2 : Double;
var M : AlglibInteger;
var W : TReal1DArray;
var Z : TComplex2DArray):Boolean;
function HMatrixEVDI(A : TComplex2DArray;
N : AlglibInteger;
ZNeeded : AlglibInteger;
IsUpper : Boolean;
I1 : AlglibInteger;
I2 : AlglibInteger;
var W : TReal1DArray;
var Z : TComplex2DArray):Boolean;
function SMatrixTDEVD(var D : TReal1DArray;
E : TReal1DArray;
N : AlglibInteger;
ZNeeded : AlglibInteger;
var Z : TReal2DArray):Boolean;
function SMatrixTDEVDR(var D : TReal1DArray;
const E : TReal1DArray;
N : AlglibInteger;
ZNeeded : AlglibInteger;
A : Double;
B : Double;
var M : AlglibInteger;
var Z : TReal2DArray):Boolean;
function SMatrixTDEVDI(var D : TReal1DArray;
const E : TReal1DArray;
N : AlglibInteger;
ZNeeded : AlglibInteger;
I1 : AlglibInteger;
I2 : AlglibInteger;
var Z : TReal2DArray):Boolean;
function RMatrixEVD(A : TReal2DArray;
N : AlglibInteger;
VNeeded : AlglibInteger;
var WR : TReal1DArray;
var WI : TReal1DArray;
var VL : TReal2DArray;
var VR : TReal2DArray):Boolean;
function InternalBisectionEigenValues(D : TReal1DArray;
E : TReal1DArray;
N : AlglibInteger;
IRANGE : AlglibInteger;
IORDER : AlglibInteger;
VL : Double;
VU : Double;
IL : AlglibInteger;
IU : AlglibInteger;
ABSTOL : Double;
var W : TReal1DArray;
var M : AlglibInteger;
var NSPLIT : AlglibInteger;
var IBLOCK : TInteger1DArray;
var ISPLIT : TInteger1DArray;
var ErrorCode : AlglibInteger):Boolean;
procedure InternalDSTEIN(const N : AlglibInteger;
const D : TReal1DArray;
E : TReal1DArray;
const M : AlglibInteger;
W : TReal1DArray;
const IBLOCK : TInteger1DArray;
const ISPLIT : TInteger1DArray;
var Z : TReal2DArray;
var IFAIL : TInteger1DArray;
var INFO : AlglibInteger);
implementation
function TridiagonalEVD(var D : TReal1DArray;
E : TReal1DArray;
N : AlglibInteger;
ZNeeded : AlglibInteger;
var Z : TReal2DArray):Boolean;forward;
procedure TdEVDE2(const A : Double;
const B : Double;
const C : Double;
var RT1 : Double;
var RT2 : Double);forward;
procedure TdEVDEV2(const A : Double;
const B : Double;
const C : Double;
var RT1 : Double;
var RT2 : Double;
var CS1 : Double;
var SN1 : Double);forward;
function TdEVDPythag(A : Double; B : Double):Double;forward;
function TdEVDExtSign(a : Double; b : Double):Double;forward;
procedure TDINInternalDLAGTF(const N : AlglibInteger;
var A : TReal1DArray;
const LAMBDA : Double;
var B : TReal1DArray;
var C : TReal1DArray;
const TOL : Double;
var D : TReal1DArray;
var IIN : TInteger1DArray;
var INFO : AlglibInteger);forward;
procedure TDINInternalDLAGTS(const N : AlglibInteger;
const A : TReal1DArray;
const B : TReal1DArray;
const C : TReal1DArray;
const D : TReal1DArray;
const IIN : TInteger1DArray;
var Y : TReal1DArray;
var TOL : Double;
var INFO : AlglibInteger);forward;
procedure InternalDLAEBZ(const IJOB : AlglibInteger;
const NITMAX : AlglibInteger;
const N : AlglibInteger;
const MMAX : AlglibInteger;
const MINP : AlglibInteger;
const ABSTOL : Double;
const RELTOL : Double;
const PIVMIN : Double;
const D : TReal1DArray;
const E : TReal1DArray;
const E2 : TReal1DArray;
var NVAL : TInteger1DArray;
var AB : TReal2DArray;
var C : TReal1DArray;
var MOUT : AlglibInteger;
var NAB : TInteger2DArray;
var WORK : TReal1DArray;
var IWORK : TInteger1DArray;
var INFO : AlglibInteger);forward;
procedure InternalTREVC(const T : TReal2DArray;
N : AlglibInteger;
SIDE : AlglibInteger;
HOWMNY : AlglibInteger;
VSELECT : TBoolean1DArray;
var VL : TReal2DArray;
var VR : TReal2DArray;
var M : AlglibInteger;
var INFO : AlglibInteger);forward;
procedure InternalHSEVDLALN2(const LTRANS : Boolean;
const NA : AlglibInteger;
const NW : AlglibInteger;
const SMIN : Double;
const CA : Double;
const A : TReal2DArray;
const D1 : Double;
const D2 : Double;
const B : TReal2DArray;
const WR : Double;
const WI : Double;
var RSWAP4 : TBoolean1DArray;
var ZSWAP4 : TBoolean1DArray;
var IPIVOT44 : TInteger2DArray;
var CIV4 : TReal1DArray;
var CRV4 : TReal1DArray;
var X : TReal2DArray;
var SCL : Double;
var XNORM : Double;
var INFO : AlglibInteger);forward;
procedure InternalHSEVDLADIV(const A : Double;
const B : Double;
const C : Double;
const D : Double;
var P : Double;
var Q : Double);forward;
function NonSymmetricEVD(A : TReal2DArray;
N : AlglibInteger;
VNeeded : AlglibInteger;
var WR : TReal1DArray;
var WI : TReal1DArray;
var VL : TReal2DArray;
var VR : TReal2DArray):Boolean;forward;
procedure ToUpperHessenberg(var A : TReal2DArray;
N : AlglibInteger;
var Tau : TReal1DArray);forward;
procedure UnpackQFromUpperHessenberg(const A : TReal2DArray;
N : AlglibInteger;
const Tau : TReal1DArray;
var Q : TReal2DArray);forward;
procedure UnpackHFromUpperHessenberg(const A : TReal2DArray;
N : AlglibInteger;
const Tau : TReal1DArray;
var H : TReal2DArray);forward;
(*************************************************************************
Finding the eigenvalues and eigenvectors of a symmetric matrix
The algorithm finds eigen pairs of a symmetric matrix by reducing it to
tridiagonal form and using the QL/QR algorithm.
Input parameters:
A - symmetric matrix which is given by its upper or lower
triangular part.
Array whose indexes range within [0..N-1, 0..N-1].
N - size of matrix A.
IsUpper - storage format.
ZNeeded - flag controlling whether the eigenvectors are needed or not.
If ZNeeded is equal to:
* 0, the eigenvectors are not returned;
* 1, the eigenvectors are returned.
Output parameters:
D - eigenvalues in ascending order.
Array whose index ranges within [0..N-1].
Z - if ZNeeded is equal to:
* 0, Z hasnt changed;
* 1, Z contains the eigenvectors.
Array whose indexes range within [0..N-1, 0..N-1].
The eigenvectors are stored in the matrix columns.
Result:
True, if the algorithm has converged.
False, if the algorithm hasn't converged (rare case).
-- ALGLIB --
Copyright 2005-2008 by Bochkanov Sergey
*************************************************************************)
function SMatrixEVD(A : TReal2DArray;
N : AlglibInteger;
ZNeeded : AlglibInteger;
IsUpper : Boolean;
var D : TReal1DArray;
var Z : TReal2DArray):Boolean;
var
Tau : TReal1DArray;
E : TReal1DArray;
begin
A := DynamicArrayCopy(A);
Assert((ZNeeded=0) or (ZNeeded=1), 'SMatrixEVD: incorrect ZNeeded');
SMatrixTD(A, N, IsUpper, Tau, D, E);
if ZNeeded=1 then
begin
SMatrixTDUnpackQ(A, N, IsUpper, Tau, Z);
end;
Result := SMatrixTDEVD(D, E, N, ZNeeded, Z);
end;
(*************************************************************************
Subroutine for finding the eigenvalues (and eigenvectors) of a symmetric
matrix in a given half open interval (A, B] by using a bisection and
inverse iteration
Input parameters:
A - symmetric matrix which is given by its upper or lower
triangular part. Array [0..N-1, 0..N-1].
N - size of matrix A.
ZNeeded - flag controlling whether the eigenvectors are needed or not.
If ZNeeded is equal to:
* 0, the eigenvectors are not returned;
* 1, the eigenvectors are returned.
IsUpperA - storage format of matrix A.
B1, B2 - half open interval (B1, B2] to search eigenvalues in.
Output parameters:
M - number of eigenvalues found in a given half-interval (M>=0).
W - array of the eigenvalues found.
Array whose index ranges within [0..M-1].
Z - if ZNeeded is equal to:
* 0, Z hasnt changed;
* 1, Z contains eigenvectors.
Array whose indexes range within [0..N-1, 0..M-1].
The eigenvectors are stored in the matrix columns.
Result:
True, if successful. M contains the number of eigenvalues in the given
half-interval (could be equal to 0), W contains the eigenvalues,
Z contains the eigenvectors (if needed).
False, if the bisection method subroutine wasn't able to find the
eigenvalues in the given interval or if the inverse iteration subroutine
wasn't able to find all the corresponding eigenvectors.
In that case, the eigenvalues and eigenvectors are not returned,
M is equal to 0.
-- ALGLIB --
Copyright 07.01.2006 by Bochkanov Sergey
*************************************************************************)
function SMatrixEVDR(A : TReal2DArray;
N : AlglibInteger;
ZNeeded : AlglibInteger;
IsUpper : Boolean;
B1 : Double;
B2 : Double;
var M : AlglibInteger;
var W : TReal1DArray;
var Z : TReal2DArray):Boolean;
var
Tau : TReal1DArray;
E : TReal1DArray;
begin
A := DynamicArrayCopy(A);
Assert((ZNeeded=0) or (ZNeeded=1), 'SMatrixTDEVDR: incorrect ZNeeded');
SMatrixTD(A, N, IsUpper, Tau, W, E);
if ZNeeded=1 then
begin
SMatrixTDUnpackQ(A, N, IsUpper, Tau, Z);
end;
Result := SMatrixTDEVDR(W, E, N, ZNeeded, B1, B2, M, Z);
end;
(*************************************************************************
Subroutine for finding the eigenvalues and eigenvectors of a symmetric
matrix with given indexes by using bisection and inverse iteration methods.
Input parameters:
A - symmetric matrix which is given by its upper or lower
triangular part. Array whose indexes range within [0..N-1, 0..N-1].
N - size of matrix A.
ZNeeded - flag controlling whether the eigenvectors are needed or not.
If ZNeeded is equal to:
* 0, the eigenvectors are not returned;
* 1, the eigenvectors are returned.
IsUpperA - storage format of matrix A.
I1, I2 - index interval for searching (from I1 to I2).
0 <= I1 <= I2 <= N-1.
Output parameters:
W - array of the eigenvalues found.
Array whose index ranges within [0..I2-I1].
Z - if ZNeeded is equal to:
* 0, Z hasnt changed;
* 1, Z contains eigenvectors.
Array whose indexes range within [0..N-1, 0..I2-I1].
In that case, the eigenvectors are stored in the matrix columns.
Result:
True, if successful. W contains the eigenvalues, Z contains the
eigenvectors (if needed).
False, if the bisection method subroutine wasn't able to find the
eigenvalues in the given interval or if the inverse iteration subroutine
wasn't able to find all the corresponding eigenvectors.
In that case, the eigenvalues and eigenvectors are not returned.
-- ALGLIB --
Copyright 07.01.2006 by Bochkanov Sergey
*************************************************************************)
function SMatrixEVDI(A : TReal2DArray;
N : AlglibInteger;
ZNeeded : AlglibInteger;
IsUpper : Boolean;
I1 : AlglibInteger;
I2 : AlglibInteger;
var W : TReal1DArray;
var Z : TReal2DArray):Boolean;
var
Tau : TReal1DArray;
E : TReal1DArray;
begin
A := DynamicArrayCopy(A);
Assert((ZNeeded=0) or (ZNeeded=1), 'SMatrixEVDI: incorrect ZNeeded');
SMatrixTD(A, N, IsUpper, Tau, W, E);
if ZNeeded=1 then
begin
SMatrixTDUnpackQ(A, N, IsUpper, Tau, Z);
end;
Result := SMatrixTDEVDI(W, E, N, ZNeeded, I1, I2, Z);
end;
(*************************************************************************
Finding the eigenvalues and eigenvectors of a Hermitian matrix
The algorithm finds eigen pairs of a Hermitian matrix by reducing it to
real tridiagonal form and using the QL/QR algorithm.
Input parameters:
A - Hermitian matrix which is given by its upper or lower
triangular part.
Array whose indexes range within [0..N-1, 0..N-1].
N - size of matrix A.
IsUpper - storage format.
ZNeeded - flag controlling whether the eigenvectors are needed or
not. If ZNeeded is equal to:
* 0, the eigenvectors are not returned;
* 1, the eigenvectors are returned.
Output parameters:
D - eigenvalues in ascending order.
Array whose index ranges within [0..N-1].
Z - if ZNeeded is equal to:
* 0, Z hasnt changed;
* 1, Z contains the eigenvectors.
Array whose indexes range within [0..N-1, 0..N-1].
The eigenvectors are stored in the matrix columns.
Result:
True, if the algorithm has converged.
False, if the algorithm hasn't converged (rare case).
Note:
eigenvectors of Hermitian matrix are defined up to multiplication by
a complex number L, such that |L|=1.
-- ALGLIB --
Copyright 2005, 23 March 2007 by Bochkanov Sergey
*************************************************************************)
function HMatrixEVD(A : TComplex2DArray;
N : AlglibInteger;
ZNeeded : AlglibInteger;
IsUpper : Boolean;
var D : TReal1DArray;
var Z : TComplex2DArray):Boolean;
var
Tau : TComplex1DArray;
E : TReal1DArray;
WORK : TReal1DArray;
T : TReal2DArray;
Q : TComplex2DArray;
I : AlglibInteger;
K : AlglibInteger;
V : Double;
begin
A := DynamicArrayCopy(A);
Assert((ZNeeded=0) or (ZNeeded=1), 'HermitianEVD: incorrect ZNeeded');
//
// Reduce to tridiagonal form
//
HMatrixTD(A, N, IsUpper, Tau, D, E);
if ZNeeded=1 then
begin
HMatrixTDUnpackQ(A, N, IsUpper, Tau, Q);
ZNeeded := 2;
end;
//
// TDEVD
//
Result := SMatrixTDEVD(D, E, N, ZNeeded, T);
//
// Eigenvectors are needed
// Calculate Z = Q*T = Re(Q)*T + i*Im(Q)*T
//
if Result and (ZNeeded<>0) then
begin
SetLength(WORK, N-1+1);
SetLength(Z, N-1+1, N-1+1);
I:=0;
while I<=N-1 do
begin
//
// Calculate real part
//
K:=0;
while K<=N-1 do
begin
WORK[K] := 0;
Inc(K);
end;
K:=0;
while K<=N-1 do
begin
V := Q[I,K].X;
APVAdd(@WORK[0], 0, N-1, @T[K][0], 0, N-1, V);
Inc(K);
end;
K:=0;
while K<=N-1 do
begin
Z[I,K].X := WORK[K];
Inc(K);
end;
//
// Calculate imaginary part
//
K:=0;
while K<=N-1 do
begin
WORK[K] := 0;
Inc(K);
end;
K:=0;
while K<=N-1 do
begin
V := Q[I,K].Y;
APVAdd(@WORK[0], 0, N-1, @T[K][0], 0, N-1, V);
Inc(K);
end;
K:=0;
while K<=N-1 do
begin
Z[I,K].Y := WORK[K];
Inc(K);
end;
Inc(I);
end;
end;
end;
(*************************************************************************
Subroutine for finding the eigenvalues (and eigenvectors) of a Hermitian
matrix in a given half-interval (A, B] by using a bisection and inverse
iteration
Input parameters:
A - Hermitian matrix which is given by its upper or lower
triangular part. Array whose indexes range within
[0..N-1, 0..N-1].
N - size of matrix A.
ZNeeded - flag controlling whether the eigenvectors are needed or
not. If ZNeeded is equal to:
* 0, the eigenvectors are not returned;
* 1, the eigenvectors are returned.
IsUpperA - storage format of matrix A.
B1, B2 - half-interval (B1, B2] to search eigenvalues in.
Output parameters:
M - number of eigenvalues found in a given half-interval, M>=0
W - array of the eigenvalues found.
Array whose index ranges within [0..M-1].
Z - if ZNeeded is equal to:
* 0, Z hasnt changed;
* 1, Z contains eigenvectors.
Array whose indexes range within [0..N-1, 0..M-1].
The eigenvectors are stored in the matrix columns.
Result:
True, if successful. M contains the number of eigenvalues in the given
half-interval (could be equal to 0), W contains the eigenvalues,
Z contains the eigenvectors (if needed).
False, if the bisection method subroutine wasn't able to find the
eigenvalues in the given interval or if the inverse iteration
subroutine wasn't able to find all the corresponding eigenvectors.
In that case, the eigenvalues and eigenvectors are not returned, M is
equal to 0.
Note:
eigen vectors of Hermitian matrix are defined up to multiplication by
a complex number L, such as |L|=1.
-- ALGLIB --
Copyright 07.01.2006, 24.03.2007 by Bochkanov Sergey.
*************************************************************************)
function HMatrixEVDR(A : TComplex2DArray;
N : AlglibInteger;
ZNeeded : AlglibInteger;
IsUpper : Boolean;
B1 : Double;
B2 : Double;
var M : AlglibInteger;
var W : TReal1DArray;
var Z : TComplex2DArray):Boolean;
var
Q : TComplex2DArray;
T : TReal2DArray;
Tau : TComplex1DArray;
E : TReal1DArray;
WORK : TReal1DArray;
I : AlglibInteger;
K : AlglibInteger;
V : Double;
begin
A := DynamicArrayCopy(A);
Assert((ZNeeded=0) or (ZNeeded=1), 'HermitianEigenValuesAndVectorsInInterval: incorrect ZNeeded');
//
// Reduce to tridiagonal form
//
HMatrixTD(A, N, IsUpper, Tau, W, E);
if ZNeeded=1 then
begin
HMatrixTDUnpackQ(A, N, IsUpper, Tau, Q);
ZNeeded := 2;
end;
//
// Bisection and inverse iteration
//
Result := SMatrixTDEVDR(W, E, N, ZNeeded, B1, B2, M, T);
//
// Eigenvectors are needed
// Calculate Z = Q*T = Re(Q)*T + i*Im(Q)*T
//
if Result and (ZNeeded<>0) and (M<>0) then
begin
SetLength(WORK, M-1+1);
SetLength(Z, N-1+1, M-1+1);
I:=0;
while I<=N-1 do
begin
//
// Calculate real part
//
K:=0;
while K<=M-1 do
begin
WORK[K] := 0;
Inc(K);
end;
K:=0;
while K<=N-1 do
begin
V := Q[I,K].X;
APVAdd(@WORK[0], 0, M-1, @T[K][0], 0, M-1, V);
Inc(K);
end;
K:=0;
while K<=M-1 do
begin
Z[I,K].X := WORK[K];
Inc(K);
end;
//
// Calculate imaginary part
//
K:=0;
while K<=M-1 do
begin
WORK[K] := 0;
Inc(K);
end;
K:=0;
while K<=N-1 do
begin
V := Q[I,K].Y;
APVAdd(@WORK[0], 0, M-1, @T[K][0], 0, M-1, V);
Inc(K);
end;
K:=0;
while K<=M-1 do
begin
Z[I,K].Y := WORK[K];
Inc(K);
end;
Inc(I);
end;
end;
end;
(*************************************************************************
Subroutine for finding the eigenvalues and eigenvectors of a Hermitian
matrix with given indexes by using bisection and inverse iteration methods
Input parameters:
A - Hermitian matrix which is given by its upper or lower
triangular part.
Array whose indexes range within [0..N-1, 0..N-1].
N - size of matrix A.
ZNeeded - flag controlling whether the eigenvectors are needed or
not. If ZNeeded is equal to:
* 0, the eigenvectors are not returned;
* 1, the eigenvectors are returned.
IsUpperA - storage format of matrix A.
I1, I2 - index interval for searching (from I1 to I2).
0 <= I1 <= I2 <= N-1.
Output parameters:
W - array of the eigenvalues found.
Array whose index ranges within [0..I2-I1].
Z - if ZNeeded is equal to:
* 0, Z hasnt changed;
* 1, Z contains eigenvectors.
Array whose indexes range within [0..N-1, 0..I2-I1].
In that case, the eigenvectors are stored in the matrix
columns.
Result:
True, if successful. W contains the eigenvalues, Z contains the
eigenvectors (if needed).
False, if the bisection method subroutine wasn't able to find the
eigenvalues in the given interval or if the inverse iteration
subroutine wasn't able to find all the corresponding eigenvectors.
In that case, the eigenvalues and eigenvectors are not returned.
Note:
eigen vectors of Hermitian matrix are defined up to multiplication by
a complex number L, such as |L|=1.
-- ALGLIB --
Copyright 07.01.2006, 24.03.2007 by Bochkanov Sergey.
*************************************************************************)
function HMatrixEVDI(A : TComplex2DArray;
N : AlglibInteger;
ZNeeded : AlglibInteger;
IsUpper : Boolean;
I1 : AlglibInteger;
I2 : AlglibInteger;
var W : TReal1DArray;
var Z : TComplex2DArray):Boolean;
var
Q : TComplex2DArray;
T : TReal2DArray;
Tau : TComplex1DArray;
E : TReal1DArray;
WORK : TReal1DArray;
I : AlglibInteger;
K : AlglibInteger;
V : Double;
M : AlglibInteger;
begin
A := DynamicArrayCopy(A);
Assert((ZNeeded=0) or (ZNeeded=1), 'HermitianEigenValuesAndVectorsByIndexes: incorrect ZNeeded');
//
// Reduce to tridiagonal form
//
HMatrixTD(A, N, IsUpper, Tau, W, E);
if ZNeeded=1 then
begin
HMatrixTDUnpackQ(A, N, IsUpper, Tau, Q);
ZNeeded := 2;
end;
//
// Bisection and inverse iteration
//
Result := SMatrixTDEVDI(W, E, N, ZNeeded, I1, I2, T);
//
// Eigenvectors are needed
// Calculate Z = Q*T = Re(Q)*T + i*Im(Q)*T
//
M := I2-I1+1;
if Result and (ZNeeded<>0) then
begin
SetLength(WORK, M-1+1);
SetLength(Z, N-1+1, M-1+1);
I:=0;
while I<=N-1 do
begin
//
// Calculate real part
//
K:=0;
while K<=M-1 do
begin
WORK[K] := 0;
Inc(K);
end;
K:=0;
while K<=N-1 do
begin
V := Q[I,K].X;
APVAdd(@WORK[0], 0, M-1, @T[K][0], 0, M-1, V);
Inc(K);
end;
K:=0;
while K<=M-1 do
begin
Z[I,K].X := WORK[K];
Inc(K);
end;
//
// Calculate imaginary part
//
K:=0;
while K<=M-1 do
begin
WORK[K] := 0;
Inc(K);
end;
K:=0;
while K<=N-1 do
begin
V := Q[I,K].Y;
APVAdd(@WORK[0], 0, M-1, @T[K][0], 0, M-1, V);
Inc(K);
end;
K:=0;
while K<=M-1 do
begin
Z[I,K].Y := WORK[K];
Inc(K);
end;
Inc(I);
end;
end;
end;
(*************************************************************************
Finding the eigenvalues and eigenvectors of a tridiagonal symmetric matrix
The algorithm finds the eigen pairs of a tridiagonal symmetric matrix by
using an QL/QR algorithm with implicit shifts.
Input parameters:
D - the main diagonal of a tridiagonal matrix.
Array whose index ranges within [0..N-1].
E - the secondary diagonal of a tridiagonal matrix.
Array whose index ranges within [0..N-2].
N - size of matrix A.
ZNeeded - flag controlling whether the eigenvectors are needed or not.
If ZNeeded is equal to:
* 0, the eigenvectors are not needed;
* 1, the eigenvectors of a tridiagonal matrix
are multiplied by the square matrix Z. It is used if the
tridiagonal matrix is obtained by the similarity
transformation of a symmetric matrix;
* 2, the eigenvectors of a tridiagonal matrix replace the
square matrix Z;
* 3, matrix Z contains the first row of the eigenvectors
matrix.
Z - if ZNeeded=1, Z contains the square matrix by which the
eigenvectors are multiplied.
Array whose indexes range within [0..N-1, 0..N-1].
Output parameters:
D - eigenvalues in ascending order.
Array whose index ranges within [0..N-1].
Z - if ZNeeded is equal to:
* 0, Z hasnt changed;
* 1, Z contains the product of a given matrix (from the left)
and the eigenvectors matrix (from the right);
* 2, Z contains the eigenvectors.
* 3, Z contains the first row of the eigenvectors matrix.
If ZNeeded<3, Z is the array whose indexes range within [0..N-1, 0..N-1].
In that case, the eigenvectors are stored in the matrix columns.
If ZNeeded=3, Z is the array whose indexes range within [0..0, 0..N-1].
Result:
True, if the algorithm has converged.
False, if the algorithm hasn't converged.
-- LAPACK routine (version 3.0) --
Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
Courant Institute, Argonne National Lab, and Rice University
September 30, 1994
*************************************************************************)
function SMatrixTDEVD(var D : TReal1DArray;
E : TReal1DArray;
N : AlglibInteger;
ZNeeded : AlglibInteger;
var Z : TReal2DArray):Boolean;
var
D1 : TReal1DArray;
E1 : TReal1DArray;
Z1 : TReal2DArray;
I : AlglibInteger;
begin
E := DynamicArrayCopy(E);
//
// Prepare 1-based task
//
SetLength(D1, N+1);
SetLength(E1, N+1);
APVMove(@D1[0], 1, N, @D[0], 0, N-1);
if N>1 then
begin
APVMove(@E1[0], 1, N-1, @E[0], 0, N-2);
end;
if ZNeeded=1 then
begin
SetLength(Z1, N+1, N+1);
I:=1;
while I<=N do
begin
APVMove(@Z1[I][0], 1, N, @Z[I-1][0], 0, N-1);
Inc(I);
end;
end;
//
// Solve 1-based task
//
Result := TridiagonalEVD(D1, E1, N, ZNeeded, Z1);
if not Result then
begin
Exit;
end;
//
// Convert back to 0-based result
//
APVMove(@D[0], 0, N-1, @D1[0], 1, N);
if ZNeeded<>0 then
begin
if ZNeeded=1 then
begin
I:=1;
while I<=N do
begin
APVMove(@Z[I-1][0], 0, N-1, @Z1[I][0], 1, N);
Inc(I);
end;
Exit;
end;
if ZNeeded=2 then
begin
SetLength(Z, N-1+1, N-1+1);
I:=1;
while I<=N do
begin
APVMove(@Z[I-1][0], 0, N-1, @Z1[I][0], 1, N);
Inc(I);
end;
Exit;
end;
if ZNeeded=3 then
begin
SetLength(Z, 0+1, N-1+1);
APVMove(@Z[0][0], 0, N-1, @Z1[1][0], 1, N);
Exit;
end;
Assert(False, 'SMatrixTDEVD: Incorrect ZNeeded!');
end;
end;
(*************************************************************************
Subroutine for finding the tridiagonal matrix eigenvalues/vectors in a
given half-interval (A, B] by using bisection and inverse iteration.
Input parameters:
D - the main diagonal of a tridiagonal matrix.
Array whose index ranges within [0..N-1].
E - the secondary diagonal of a tridiagonal matrix.
Array whose index ranges within [0..N-2].
N - size of matrix, N>=0.
ZNeeded - flag controlling whether the eigenvectors are needed or not.
If ZNeeded is equal to:
* 0, the eigenvectors are not needed;
* 1, the eigenvectors of a tridiagonal matrix are multiplied
by the square matrix Z. It is used if the tridiagonal
matrix is obtained by the similarity transformation
of a symmetric matrix.
* 2, the eigenvectors of a tridiagonal matrix replace matrix Z.
A, B - half-interval (A, B] to search eigenvalues in.
Z - if ZNeeded is equal to:
* 0, Z isn't used and remains unchanged;
* 1, Z contains the square matrix (array whose indexes range
within [0..N-1, 0..N-1]) which reduces the given symmetric
matrix to tridiagonal form;
* 2, Z isn't used (but changed on the exit).
Output parameters:
D - array of the eigenvalues found.
Array whose index ranges within [0..M-1].
M - number of eigenvalues found in the given half-interval (M>=0).
Z - if ZNeeded is equal to: