-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcf_matrices.f90
1440 lines (1201 loc) · 32.1 KB
/
mcf_matrices.f90
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
module m_slineales_sp
use mcf_tipos
private
!
! Routines to solve systems of linear equations
!
public :: tridag, ctridag, lu_descomposicion, lu_resolucion, gaussj
public :: ludcmp, lubksb ! Aliases
public :: print_matrix
interface tridag
module procedure tridag_sp
end interface
private :: tridag_sp
interface gaussj
module procedure gaussj_sp
end interface
private :: gaussj_sp
interface ludcmp
module procedure lu_descomposicion_sp
end interface
interface lubksb
module procedure lu_resolucion_sp
end interface
!
interface lu_descomposicion
module procedure lu_descomposicion_sp
end interface
private :: lu_descomposicion_sp
interface lu_resolucion
module procedure lu_resolucion_sp
end interface
private :: lu_resolucion_sp
interface print_matrix
module procedure print_matrix_sp
end interface
private :: print_matrix_sp
CONTAINS !==================================================
SUBROUTINE TRIDAG_sp(A,D,C,B,X,n)
!
! Resuelve un sistema tridiagonal de ecuaciones lineales.
! D(1:n) contiene los elementos diagonales
! a(1:n) contiene los elementos infradiagonales (a(1) no se usa)
! c(1:n) contiene los elementos supradiagonales (c(n) no se usa)
! B(1:n) contiene los terminos independientes del sistema
!
! A la salida, x(1:n) contiene la solucion del sistema
!
!
integer, intent(in) :: n
real(kind=sp), intent(in), dimension(:) :: a, d, c, b
real(kind=sp), intent(out), dimension(:) :: x
real(kind=sp) :: BET
INTEGER :: J
real(kind=sp), allocatable, dimension(:) :: GAM ! Array de trabajo
IF (D(1) == 0.0_sp) THEN
STOP "D(1)=0 in tridag"
END IF
allocate(gam(n))
BET = D(1)
X(1) = B(1)/BET
DO J = 2,N
GAM(J) = C(J-1)/BET
BET = D(J) - A(J)*GAM(J)
IF (BET == 0.0_sp) THEN
deallocate(gam)
STOP "BET=0.0, Tridag failed."
END IF
X(J) = (B(J)-A(J)*X(J-1))/BET
ENDDO
DO J = N - 1,1,-1
X(J) = X(J) - GAM(J+1)*X(J+1)
ENDDO
deallocate(gam)
END subroutine tridag_sp
SUBROUTINE CTRIDAG(A,D,C,B,X,n)
!
! Resuelve un sistema tridiagonal de ecuaciones lineales.
! D(1:n) contiene los elementos diagonales
! a(1:n) contiene los elementos infradiagonales (a(1) no se usa)
! c(1:n) contiene los elementos supradiagonales (c(n) no se usa)
! B(1:n) contiene los terminos independientes del sistema
!
! A la salida, x(1:n) contiene la solucion del sistema
!
!
integer, intent(in) :: n
complex, intent(in), dimension(:) :: a, d, c, b
complex, intent(out), dimension(:) :: x
complex :: BET
INTEGER :: J
complex, allocatable, dimension(:) :: GAM ! Array de trabajo
IF (D(1) == 0.0_sp) THEN
STOP "D(1)=0 in tridag"
END IF
allocate(gam(n))
BET = D(1)
X(1) = B(1)/BET
DO J = 2,N
GAM(J) = C(J-1)/BET
BET = D(J) - A(J)*GAM(J)
IF (BET == 0.0_sp) THEN
deallocate(gam)
STOP "BET=0.0, Tridag failed."
END IF
X(J) = (B(J)-A(J)*X(J-1))/BET
ENDDO
DO J = N - 1,1,-1
X(J) = X(J) - GAM(J+1)*X(J+1)
ENDDO
deallocate(gam)
END subroutine ctridag
!===================================================================
SUBROUTINE LU_DESCOMPOSICION_sp(A,INDX,D)
!
! Performs the LU decomposition of a square matrix A
! indx is an array holding permutation indexes. It must
! be kept unchanged before calling lu_resolucion.
! d is the signature of the permutation.
! On exit, A contains the L and U matrices.
!
real(kind=sp), dimension(:,:) , intent(inout) :: A
INTEGER, dimension(:), intent(out) :: INDX
real(kind=sp), intent(out) :: D
real(kind=sp) :: AAMAX,DUM,SUM
INTEGER :: n,I,IMAX,J,K
real(kind=sp), PARAMETER :: TINY=1.0e-20_sp
! To avoid division by zero
real(kind=sp), dimension(:), allocatable :: VV
n = size(a,dim=1)
if (size(a,dim=2) /= n) then
STOP "Matrix is not square in lu_descomposicion"
end if
if (size(indx) < n) then
STOP "Size of Indx is too small in lu_descomposicion"
end if
allocate(vv(n))
D = 1.0_sp
DO I = 1,N
!! aamax = maxval(abs(a(i,1:n)))
AAMAX = 0.0_sp
DO J = 1,N
IF (ABS(A(I,J)) > AAMAX) then
AAMAX = ABS(A(I,J))
end if
ENDDO
IF (AAMAX == 0.0_sp) THEN
deallocate(vv)
STOP "Singular matrix in lu_descomposicion"
END IF
VV(I) = 1.0_sp/AAMAX
ENDDO
DO J = 1,N
IF (J > 1) THEN
DO I = 1,J - 1
SUM = A(I,J)
IF (I > 1) THEN
DO K = 1,I - 1
SUM = SUM - A(I,K)*A(K,J)
ENDDO
A(I,J) = SUM
END IF
ENDDO
END IF
AAMAX = 0.0_sp
DO I = J,N
SUM = A(I,J)
IF (J > 1) THEN
DO K = 1,J - 1
SUM = SUM - A(I,K)*A(K,J)
ENDDO
A(I,J) = SUM
END IF
DUM = VV(I)*ABS(SUM)
IF (DUM >= AAMAX) THEN
IMAX = I
AAMAX = DUM
END IF
ENDDO
IF (J /= IMAX) THEN
DO K = 1,N
DUM = A(IMAX,K)
A(IMAX,K) = A(J,K)
A(J,K) = DUM
ENDDO
D = -D
VV(IMAX) = VV(J)
END IF
INDX(J) = IMAX
IF (J /= N) THEN
IF (A(J,J) == 0.0_sp) then
A(J,J) = TINY
end if
DUM = 1.0_sp/A(J,J)
DO I = J + 1,N
A(I,J) = A(I,J)*DUM
ENDDO
END IF
ENDDO
IF (A(N,N) == 0.0_sp) then
A(N,N) = TINY
end if
deallocate(vv)
END subroutine lu_descomposicion_sp
SUBROUTINE LU_RESOLUCION_sp(A,INDX,B)
!
! Performs backsubstitution to solve a linear system.
! A is a matrix holding the LU decomposition in the standard manner.
! Indx is an array holding permutation indexes
! B is the right-hand-side vector, which is replaced by the solution
!
real(kind=sp), dimension(:,:) , intent(inout) :: A
INTEGER, dimension(:), intent(in) :: INDX
real(kind=sp), dimension(:), intent(inout) :: B
real(kind=sp) :: SUM
INTEGER :: n, I,II,J,LL
n = size(a,dim=1)
if (size(a,dim=2) /= n) then
STOP "Matrix is not square in lu_resolucion"
end if
if (size(indx) < n) then
STOP "Size of Indx is too small in lu_resolucion"
end if
if (size(b) < n) then
STOP "Size of B is too small in lu_resolucion"
end if
II = 0
DO I = 1,N
LL = INDX(I)
SUM = B(LL)
B(LL) = B(I)
IF (II /= 0) THEN
DO J = II,I - 1
SUM = SUM - A(I,J)*B(J)
ENDDO
ELSE IF (SUM /= 0.0_sp) THEN
II = I
END IF
B(I) = SUM
ENDDO
DO I = N,1,-1
SUM = B(I)
IF (I < N) THEN
DO J = I + 1,N
SUM = SUM - A(I,J)*B(J)
ENDDO
END IF
B(I) = SUM/A(I,I)
ENDDO
END subroutine lu_resolucion_sp
SUBROUTINE GAUSSJ_sp(A,B)
!
! Performs Gaussian elimination in matrix A to solve the
! multiple linear system Ax=B
!
! A is replaced by its inverse, and B by the solution vectors
!
real(kind=sp), intent(inout), dimension(:,:) :: A
real(kind=sp), intent(inout), dimension(:,:) :: B
real(kind=sp) :: BIG,DUM,PIVINV
INTEGER :: I,ICOL,IROW,J,K,L,LL
integer :: n, m
INTEGER, dimension(:), allocatable :: INDXC,INDXR,IPIV
n = size(a,dim=2)
if (size(a,dim=1) /= n) then
STOP "A is not square in gaussj"
end if
if (size(b,dim=1) /= n) then
STOP "Array mismatch in gaussj"
end if
m = size(b,dim=2)
allocate(ipiv(n),indxc(n),indxr(n))
IPIV(1:N) = 0
DO I = 1,N
BIG = 0.0_sp
DO J = 1,N
IF (IPIV(J) /= 1) THEN
DO K = 1,N
IF (IPIV(K) == 0) THEN
IF (ABS(A(J,K)) >= BIG) THEN
BIG = ABS(A(J,K))
IROW = J
ICOL = K
END IF
ELSE IF (IPIV(K) > 1) THEN
deallocate(ipiv,indxc,indxr)
STOP "Singular matrix in gaussj"
END IF
ENDDO
END IF
ENDDO
IPIV(ICOL) = IPIV(ICOL) + 1
IF (IROW /= ICOL) THEN
DO L = 1,N
DUM = A(IROW,L)
A(IROW,L) = A(ICOL,L)
A(ICOL,L) = DUM
ENDDO
DO L = 1,M
DUM = B(IROW,L)
B(IROW,L) = B(ICOL,L)
B(ICOL,L) = DUM
ENDDO
END IF
INDXR(I) = IROW
INDXC(I) = ICOL
IF (A(ICOL,ICOL) == 0.0_sp) THEN
deallocate(ipiv,indxc,indxr)
STOP "Singular matrix in gaussj"
END IF
PIVINV = 1.0_sp/A(ICOL,ICOL)
A(ICOL,ICOL) = 1.0_sp
A(ICOL,1:N) = A(ICOL,1:N) * PIVINV
B(ICOL,1:M) = B(ICOL,1:M) * PIVINV
DO LL = 1,N
IF (LL /= ICOL) THEN
DUM = A(LL,ICOL)
A(LL,ICOL) = 0.0_sp
A(LL,1:N) = A(LL,1:N) - A(ICOL,1:N)*DUM
B(LL,1:M) = B(LL,1:M) - B(ICOL,1:M)*DUM
END IF
ENDDO
ENDDO
DO L = N,1,-1
IF (INDXR(L) /= INDXC(L)) THEN
DO K = 1,N
DUM = A(K,INDXR(L))
A(K,INDXR(L)) = A(K,INDXC(L))
A(K,INDXC(L)) = DUM
ENDDO
END IF
ENDDO
deallocate(ipiv,indxc,indxr)
END SUBROUTINE GAUSSJ_sp
subroutine print_matrix_sp(a,unit)
real(kind=sp), intent(in), dimension(:,:) :: a
integer, intent(in) :: unit
integer :: n, m, i, j
n = size(a,dim=1)
m = size(a,dim=2)
do i = 1, n
do j = 1, m-1
write(unit=unit,fmt="(f10.4)", advance="no") a(i,j)
enddo
write(unit=unit,fmt="(f10.4)") a(i,m)
enddo
write(unit=unit,fmt=*) "--------"
end subroutine print_matrix_sp
end module m_slineales_sp
module m_slineales_dp
use mcf_tipos
private
!
! Routines to solve systems of linear equations
!
public :: tridag, lu_descomposicion, lu_resolucion, gaussj
public :: ludcmp, lubksb ! Aliases
public :: print_matrix
interface tridag
module procedure tridag_dp
end interface
private :: tridag_dp
interface gaussj
module procedure gaussj_dp
end interface
private :: gaussj_dp
interface ludcmp
module procedure lu_descomposicion_dp
end interface
interface lubksb
module procedure lu_resolucion_dp
end interface
!
interface lu_descomposicion
module procedure lu_descomposicion_dp
end interface
private :: lu_descomposicion_dp
interface lu_resolucion
module procedure lu_resolucion_dp
end interface
private :: lu_resolucion_dp
interface print_matrix
module procedure print_matrix_dp
end interface
private :: print_matrix_dp
CONTAINS !==================================================
SUBROUTINE TRIDAG_dp(A,D,C,B,X,n)
!
! Resuelve un sistema tridiagonal de ecuaciones lineales.
! D(1:n) contiene los elementos diagonales
! a(1:n) contiene los elementos infradiagonales (a(1) no se usa)
! c(1:n) contiene los elementos supradiagonales (c(n) no se usa)
! B(1:n) contiene los terminos independientes del sistema
!
! A la salida, x(1:n) contiene la solucion del sistema
!
!
integer, intent(in) :: n
real(kind=dp), intent(in), dimension(:) :: a, d, c, b
real(kind=dp), intent(out), dimension(:) :: x
real(kind=dp) :: BET
INTEGER :: J
real(kind=dp), allocatable, dimension(:) :: GAM ! Array de trabajo
IF (D(1) == 0.0_dp) THEN
STOP "D(1)=0 in tridag"
END IF
allocate(gam(n))
BET = D(1)
X(1) = B(1)/BET
DO J = 2,N
GAM(J) = C(J-1)/BET
BET = D(J) - A(J)*GAM(J)
IF (BET == 0.0_dp) THEN
deallocate(gam)
STOP "BET=0.0, Tridag failed."
END IF
X(J) = (B(J)-A(J)*X(J-1))/BET
ENDDO
DO J = N - 1,1,-1
X(J) = X(J) - GAM(J+1)*X(J+1)
ENDDO
deallocate(gam)
END subroutine tridag_dp
!===================================================================
SUBROUTINE LU_DESCOMPOSICION_dp(A,INDX,D)
!
! Performs the LU decomposition of a square matrix A
! indx is an array holding permutation indexes. It must
! be kept unchanged before calling lu_resolucion.
! d is the signature of the permutation.
! On exit, A contains the L and U matrices.
!
real(kind=dp), dimension(:,:) , intent(inout) :: A
INTEGER, dimension(:), intent(out) :: INDX
real(kind=dp), intent(out) :: D
real(kind=dp) :: AAMAX,DUM,SUM
INTEGER :: n,I,IMAX,J,K
real(kind=dp), PARAMETER :: TINY=1.0e-20_dp
! To avoid division by zero
real(kind=dp), dimension(:), allocatable :: VV
n = size(a,dim=1)
if (size(a,dim=2) /= n) then
STOP "Matrix is not square in lu_descomposicion"
end if
if (size(indx) < n) then
STOP "Size of Indx is too small in lu_descomposicion"
end if
allocate(vv(n))
D = 1.0_dp
DO I = 1,N
!! aamax = maxval(abs(a(i,1:n)))
AAMAX = 0.0_dp
DO J = 1,N
IF (ABS(A(I,J)) > AAMAX) then
AAMAX = ABS(A(I,J))
end if
ENDDO
IF (AAMAX == 0.0_dp) THEN
deallocate(vv)
STOP "Singular matrix in lu_descomposicion"
END IF
VV(I) = 1.0_dp/AAMAX
ENDDO
DO J = 1,N
IF (J > 1) THEN
DO I = 1,J - 1
SUM = A(I,J)
IF (I > 1) THEN
DO K = 1,I - 1
SUM = SUM - A(I,K)*A(K,J)
ENDDO
A(I,J) = SUM
END IF
ENDDO
END IF
AAMAX = 0.0_dp
DO I = J,N
SUM = A(I,J)
IF (J > 1) THEN
DO K = 1,J - 1
SUM = SUM - A(I,K)*A(K,J)
ENDDO
A(I,J) = SUM
END IF
DUM = VV(I)*ABS(SUM)
IF (DUM >= AAMAX) THEN
IMAX = I
AAMAX = DUM
END IF
ENDDO
IF (J /= IMAX) THEN
DO K = 1,N
DUM = A(IMAX,K)
A(IMAX,K) = A(J,K)
A(J,K) = DUM
ENDDO
D = -D
VV(IMAX) = VV(J)
END IF
INDX(J) = IMAX
IF (J /= N) THEN
IF (A(J,J) == 0.0_dp) then
A(J,J) = TINY
end if
DUM = 1.0_dp/A(J,J)
DO I = J + 1,N
A(I,J) = A(I,J)*DUM
ENDDO
END IF
ENDDO
IF (A(N,N) == 0.0_dp) then
A(N,N) = TINY
end if
deallocate(vv)
END subroutine lu_descomposicion_dp
SUBROUTINE LU_RESOLUCION_dp(A,INDX,B)
!
! Performs backsubstitution to solve a linear system.
! A is a matrix holding the LU decomposition in the standard manner.
! Indx is an array holding permutation indexes
! B is the right-hand-side vector, which is replaced by the solution
!
real(kind=dp), dimension(:,:) , intent(inout) :: A
INTEGER, dimension(:), intent(in) :: INDX
real(kind=dp), dimension(:), intent(inout) :: B
real(kind=dp) :: SUM
INTEGER :: n, I,II,J,LL
n = size(a,dim=1)
if (size(a,dim=2) /= n) then
STOP "Matrix is not square in lu_resolucion"
end if
if (size(indx) < n) then
STOP "Size of Indx is too small in lu_resolucion"
end if
if (size(b) < n) then
STOP "Size of B is too small in lu_resolucion"
end if
II = 0
DO I = 1,N
LL = INDX(I)
SUM = B(LL)
B(LL) = B(I)
IF (II /= 0) THEN
DO J = II,I - 1
SUM = SUM - A(I,J)*B(J)
ENDDO
ELSE IF (SUM /= 0.0_dp) THEN
II = I
END IF
B(I) = SUM
ENDDO
DO I = N,1,-1
SUM = B(I)
IF (I < N) THEN
DO J = I + 1,N
SUM = SUM - A(I,J)*B(J)
ENDDO
END IF
B(I) = SUM/A(I,I)
ENDDO
END subroutine lu_resolucion_dp
SUBROUTINE GAUSSJ_dp(A,B)
!
! Performs Gaussian elimination in matrix A to solve the
! multiple linear system Ax=B
!
! A is replaced by its inverse, and B by the solution vectors
!
real(kind=dp), intent(inout), dimension(:,:) :: A
real(kind=dp), intent(inout), dimension(:,:) :: B
real(kind=dp) :: BIG,DUM,PIVINV
INTEGER :: I,ICOL,IROW,J,K,L,LL
integer :: n, m
INTEGER, dimension(:), allocatable :: INDXC,INDXR,IPIV
n = size(a,dim=2)
if (size(a,dim=1) /= n) then
STOP "A is not square in gaussj"
end if
if (size(b,dim=1) /= n) then
STOP "Array mismatch in gaussj"
end if
m = size(b,dim=2)
allocate(ipiv(n),indxc(n),indxr(n))
IPIV(1:N) = 0
DO I = 1,N
BIG = 0.0_dp
DO J = 1,N
IF (IPIV(J) /= 1) THEN
DO K = 1,N
IF (IPIV(K) == 0) THEN
IF (ABS(A(J,K)) >= BIG) THEN
BIG = ABS(A(J,K))
IROW = J
ICOL = K
END IF
ELSE IF (IPIV(K) > 1) THEN
deallocate(ipiv,indxc,indxr)
STOP "Singular matrix in gaussj"
END IF
ENDDO
END IF
ENDDO
IPIV(ICOL) = IPIV(ICOL) + 1
IF (IROW /= ICOL) THEN
DO L = 1,N
DUM = A(IROW,L)
A(IROW,L) = A(ICOL,L)
A(ICOL,L) = DUM
ENDDO
DO L = 1,M
DUM = B(IROW,L)
B(IROW,L) = B(ICOL,L)
B(ICOL,L) = DUM
ENDDO
END IF
INDXR(I) = IROW
INDXC(I) = ICOL
IF (A(ICOL,ICOL) == 0.0_dp) THEN
deallocate(ipiv,indxc,indxr)
STOP "Singular matrix in gaussj"
END IF
PIVINV = 1.0_dp/A(ICOL,ICOL)
A(ICOL,ICOL) = 1.0_dp
A(ICOL,1:N) = A(ICOL,1:N) * PIVINV
B(ICOL,1:M) = B(ICOL,1:M) * PIVINV
DO LL = 1,N
IF (LL /= ICOL) THEN
DUM = A(LL,ICOL)
A(LL,ICOL) = 0.0_dp
A(LL,1:N) = A(LL,1:N) - A(ICOL,1:N)*DUM
B(LL,1:M) = B(LL,1:M) - B(ICOL,1:M)*DUM
END IF
ENDDO
ENDDO
DO L = N,1,-1
IF (INDXR(L) /= INDXC(L)) THEN
DO K = 1,N
DUM = A(K,INDXR(L))
A(K,INDXR(L)) = A(K,INDXC(L))
A(K,INDXC(L)) = DUM
ENDDO
END IF
ENDDO
deallocate(ipiv,indxc,indxr)
END SUBROUTINE GAUSSJ_dp
subroutine print_matrix_dp(a,unit)
real(kind=dp), intent(in), dimension(:,:) :: a
integer, intent(in) :: unit
integer :: n, m, i, j
n = size(a,dim=1)
m = size(a,dim=2)
do i = 1, n
do j = 1, m-1
write(unit=unit,fmt="(f10.4)", advance="no") a(i,j)
enddo
write(unit=unit,fmt="(f10.4)") a(i,m)
enddo
write(unit=unit,fmt=*) "--------"
end subroutine print_matrix_dp
end module m_slineales_dp
module m_diagonalizacion_sp
!
! Este modulo contiene rutinas para diagonalizar matrices simetricas
! (En versiones 'single' y 'double', pero con el mismo nombre abreviado)
!
use mcf_tipos
private
public :: tred2 ! Convierte una matriz simetrica a tridiagonal
interface tred2
module procedure tred2_sp
end interface
public :: tqli ! Diagonaliza una matriz tridiagonal
interface tqli
module procedure tqli_sp
end interface
public :: eigsrt ! Ordena autovalores
interface eigsrt
module procedure eigsrt_sp
end interface
!--------------------------------------------------------------------
!--------------------------------------------------------------------
private :: pythag
interface pythag
module procedure pythag_sp
end interface
private :: tred2_sp
private :: tqli_sp
private :: pythag_sp
private :: eigsrt_sp
CONTAINS !===========================
SUBROUTINE TQLI_sp(D,E,Z)
! Calcula los autovalores de una matriz simetrica tridiagonal
! D(1:n) es la diagonal
! E(1:n) (e(1) no se usa) son los valores infra- y supradiagonales
! Si Z aparece como argumento, se calculan tambien los autovectores.
! (En este caso Z ha de ser la matriz unidad a la entrada)
!
real(kind=sp), dimension(:), intent(inout) :: D,E
real(kind=sp), dimension(:,:), intent(inout), OPTIONAL :: Z
real(kind=sp) :: B,C,DD,F,G,P,R,S
INTEGER :: I,ITER,K,L,M
integer :: n
logical :: premature_exit
integer, parameter :: maxit = 30
n = size(d)
if (size(e) < n) then
STOP "E too short in tqli"
end if
if (present(z)) then
if (size(z,dim=1) < n) then
STOP "1st dim of z too short in tqli"
end if
if (size(z,dim=2) < n) then
STOP "2nd dim of z too short in tqli"
end if
endif
IF (N <= 1) then
RETURN
end if
DO I = 2,N
E(I-1) = E(I)
ENDDO
E(N) = 0.0_sp
DO L = 1,N
ITER = 0
loop: DO ! iterations
premature_exit = .false. ! Kludge to work around horrible goto
DO M = L,N - 1
DD = ABS(D(M)) + ABS(D(M+1))
IF (ABS(E(M))+DD == DD) then
premature_exit = .true.
exit ! of "do m" loop
endif
ENDDO
if (.not. premature_exit) then
M = N
end if
IF (M == L) then
EXIT
end if
IF (ITER == MAXIT) then
STOP "too many iterations in TQLI"
end if
ITER = ITER + 1
G = (D(L+1)-D(L))/ (2.0_sp*E(L))
R = pythag(G,1.0_sp)
G = D(M) - D(L) + E(L)/ (G+SIGN(R,G))
S = 1.0_sp
C = 1.0_sp
P = 0.0_sp
DO I = M - 1,L,-1
F = S*E(I)
B = C*E(I)
r = pythag(f,g)
e(i+1) = r
! Recover from underflow
if (r == 0.0_sp) then
d(i+1) = d(i+1) - p
e(m) = 0.0_sp
cycle loop
endif
s = f/r
c = g/r
G = D(I+1) - P
R = (D(I)-G)*S + 2.0*C*B
P = S*R
D(I+1) = G + P
G = C*R - B
if (present(z)) then !------------ Autovectores
DO K = 1,N
F = Z(K,I+1)
Z(K,I+1) = S*Z(K,I) + C*F
Z(K,I) = C*Z(K,I) - S*F
ENDDO
endif !------------
ENDDO
D(L) = D(L) - P
E(L) = G
E(M) = 0.0_sp
ENDDO loop !------------ outer loop
ENDDO ! loop over L
END subroutine tqli_sp
!
function pythag_sp(a,b) result(res)
real(kind=sp), intent(in) :: a, b
real(kind=sp) :: res
!
! Computes sqrt(a*a+b*b) without overflow or underflow
!
real(kind=sp) :: absa, absb
absa=abs(a)
absb=abs(b)
if (absa > absb) then
res = absa*sqrt(1.0_sp + (absb/absa)**2)
else
if (absb == 0.0_sp) then
res = 0.0_sp
else
res = absb*sqrt(1.0_sp + (absa/absb)**2)
endif
endif
end function pythag_sp
!SUBROUTINE EIGSRT_sp(D,V,N,NP)
SUBROUTINE EIGSRT_sp(D,V)
!INTEGER, intent(in) :: N,NP
real(kind=sp), intent(inout), dimension(:) :: D
real(kind=sp), intent(inout), dimension(:,:), optional :: V
real(kind=sp) :: P
INTEGER :: I,J,K,n
n = size(d)
DO I = 1,N - 1
K = I
P = D(I)
DO J = I + 1,N
IF (D(J) <= P) THEN
K = J
P = D(J)
END IF
enddo
IF (K /= I) THEN
D(K) = D(I)
D(I) = P
if (present(v)) then
DO J = 1,N
P = V(J,I)
V(J,I) = V(J,K)
V(J,K) = P
enddo
end if
END IF
ENDDO
END subroutine eigsrt_sp
SUBROUTINE TRED2_sp(A,D,E)
! Reduce una matriz simetrica a tridiagonal
real(kind=sp), intent(inout), dimension(:,:) :: a
real(kind=sp), intent(out), dimension(:) :: d, e
real(kind=sp) :: F,G,H,HH,SCALE
INTEGER :: I,J,K,L, N
n = size(a,dim=2)
if (size(a,dim=1) /= n) then
stop "A is not square"
end if
if (size(d) < n) then
stop "d is not big enough in tred2"
end if
if (size(e) < n) then
stop "e is not big enough in tred2"