-
Notifications
You must be signed in to change notification settings - Fork 6
/
mo_linear_algebra.f90
1223 lines (1008 loc) · 39.8 KB
/
mo_linear_algebra.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
!> \file mo_linear_algebra.f90
!> \brief Wrapper for LAPACK's linear algebra routines.
!> \details This modules provides mostly wrappers for LAPACK's F77 linear algebra routines.
!> It adds a few convenience functions such as diag.
!> \authors Matthias Cuntz, Sebastian Mueller
!> \date May 2014
MODULE mo_linear_algebra
! Wrapper for LAPACK's F77 linear algebra routines.
! Written Matthias Cuntz, May 2014
! Modified Matthias Cuntz, May 2016 - calc single precision via double precision
! Modified Sebastian Mueller, Oct 2016 - solver for banded coefficent-matrices and some involved algorithms
! Modified Matthias Cuntz, Mar 2020 - allocate out of sp routines calling dp routines only if not allocated
! - allocate out for Python.
! License
! -------
! This file is part of the JAMS Fortran package, distributed under the MIT License.
!
! Copyright (c) 2014-2020 Matthias Cuntz, Sebastian Mueller - mc (at) macu (dot) de
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in all
! copies or substantial portions of the Software.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
! SOFTWARE.
USE mo_kind, ONLY: i4, i8, sp, dp
IMPLICIT NONE
PUBLIC :: banded ! banded form of a given matrix
PUBLIC :: diag ! diagonal of matrix
PUBLIC :: inverse ! inverse of matrix
PUBLIC :: min_diag ! minor diagonal of matrix
PUBLIC :: solve_linear_equations ! solve linear system of equations with LU decomposition
PUBLIC :: solve_linear_equations_svd ! solve linear system of equations with SVD
PUBLIC :: solve_linear_equations_band ! solve linear system of equations for banded matrix
! ------------------------------------------------------------------
!
! NAME
! banded
!
! PURPOSE
! Banded form of squared matrix for the banded linear equations solver
!
!> \brief Banded form of squared matrix.
!
!> \details Converts a given squared matrix into the banded form needed for the Lapack-solver for banded matrices.
!
! INTENT(IN)
!> \param[in] "real(sp/dp) :: matrix(:,:)" Squared 2D-array
!> \param[in] "integer(i4) :: l " number of lower minor-diagonals
!> \param[in] "integer(i4) :: u " number of upper minor-diagonals
!
! INTENT(INOUT)
! None
!
! INTENT(OUT)
! None
!
! INTENT(IN), OPTIONAL
! None
!
! INTENT(INOUT), OPTIONAL
! None
!
! INTENT(OUT), OPTIONAL
! None
!
! RETURNS
!> \return real(sp/dp) :: banded(:) — Banded form of input matrix
!
! RESTRICTIONS
! None
!
! EXAMPLE
! band = banded(matrix,l,u)
! -> see also example in test directory
! LITERATURE
! None
! HISTORY
!> \author Sebastian Mueller
!> \date October 2016
INTERFACE banded
MODULE PROCEDURE banded_dp, banded_sp
END INTERFACE banded
! ------------------------------------------------------------------
!
! NAME
! diag
!
! PURPOSE
! Returns the diagonal of a square 2D-array.
!
!> \brief Diagonal elements of a squared matrix.
!
!> \details Returns the diagonal of a square 2D-array.
!
! INTENT(IN)
!> \param[in] "real(sp/dp)/integer(i4/i8)/logical :: matrix(:,:)" Squared 2D-array
!
! INTENT(INOUT)
! None
!
! INTENT(OUT)
! None
!
! INTENT(IN), OPTIONAL
! None
!
! INTENT(INOUT), OPTIONAL
! None
!
! INTENT(OUT), OPTIONAL
! None
!
! RETURNS
!> \return real(sp/dp)/integer(i4/i8)/logical :: diag(:) — Diagonal elements of matrix
!
! RESTRICTIONS
! None
!
! EXAMPLE
! m = diag(matrix)
! -> see also example in test directory
!
! LITERATURE
! None
!
! HISTORY
!> \author Matthias Cuntz
!> \date May 2014
INTERFACE diag
MODULE PROCEDURE diag_sp, diag_dp, diag_i4, diag_i8, diag_lgt
END INTERFACE diag
! ------------------------------------------------------------------
!
! NAME
! inverse
!
! PURPOSE
! Inverse of squared matrix
!
!> \brief Inverse of squared matrix.
!
!> \details Inverts squared matrix using LU decomposition.
!>
!> Uses standard Lapack routines using LU decomposition: dgetrf and dgetri.
!> Conditions columns before decomposition.
!
! INTENT(IN)
!> \param[in] "real(sp/dp) :: matrix(:,:)" Squared 2D-array
!
! INTENT(INOUT)
! None
!
! INTENT(OUT)
! None
!
! INTENT(IN), OPTIONAL
!> \param[in] "logical, optional :: condition" If true, condition matrix before inversion (default: true)
!
! INTENT(INOUT), OPTIONAL
! None
!
! INTENT(OUT), OPTIONAL
! None
!
! RETURNS
!> \return real(sp/dp) :: inverse(:) — Inverse of input matrix
!
! RESTRICTIONS
! None
!
! EXAMPLE
! inv = inverse(matrix)
! -> see also example in test directory
! LITERATURE
! None
! HISTORY
!> \author Matthias Cuntz
!> \date May 2014
INTERFACE inverse
MODULE PROCEDURE inverse_dp, inverse_sp
END INTERFACE inverse
! ------------------------------------------------------------------
!
! NAME
! min_diag
!
! PURPOSE
! Returns the n-th minor-diagonal of a square 2D-array, where positive n indicate super-diagonals and negativ n indicate
! sub-diagonals.
!
!> \brief Minor-diagonal elements of a squared matrix.
!
!> \details Returns the n-th minor-diagonal of a square 2D-array.
!
! INTENT(IN)
!> \param[in] "real(sp/dp)/integer(i4/i8)/logical :: matrix(:,:)" Squared 2D-array
!> \param[in] "integer(i4) :: n " number of minor-diagonal
!
! INTENT(INOUT)
! None
!
! INTENT(OUT)
! None
!
! INTENT(IN), OPTIONAL
! None
!
! INTENT(INOUT), OPTIONAL
! None
!
! INTENT(OUT), OPTIONAL
! None
!
! RETURNS
!> \return real(sp/dp)/integer(i4/i8)/logical :: min_diag(:) — n-th minor-diagonal elements of matrix
!
! RESTRICTIONS
! None
!
! EXAMPLE
! m = min_diag(matrix,n)
! -> see also example in test directory
!
! LITERATURE
! None
!
! HISTORY
!> \author Sebastian Mueller
!> \date October 2016
INTERFACE min_diag
MODULE PROCEDURE min_diag_sp, min_diag_dp, min_diag_i4, min_diag_i8, min_diag_lgt
END INTERFACE min_diag
! ------------------------------------------------------------------
!
! NAME
! solve_linear_equations
!
! PURPOSE
! Solve linear system of equations
!
!> \brief Solve linear system of equations.
!
!> \details Solve linear system of equations using LU decomposition
!> \f[ A x = b \f]
!> Returns \f$ x \f$.
!>
!> Uses standard Lapack routine using LU decomposition: dgesv.
!> Conditions rows and columns before decomposition.
!
! INTENT(IN)
!> \param[in] "real(sp/dp) :: lhs(:,:)" Coefficients of left hand side \f$ A \f$
!> \param[in] "real(sp/dp) :: rhs(:)" Right hand side \f$ b \f$
!
! INTENT(INOUT)
! None
!
! INTENT(OUT)
! None
!
! INTENT(IN), OPTIONAL
!> \param[in] "logical, optional :: condition" If true, condition lhs and rhs before decomposition (default: true)
!
! INTENT(INOUT), OPTIONAL
! None
!
! INTENT(OUT), OPTIONAL
! None
!
! RETURNS
!> \return real(sp/dp) :: x(:) — Solution x to \f$ A x = b \f$
!
! RESTRICTIONS
! Only one right-hand side.
!
! EXAMPLE
! sol = solve_linear_equations(lhs, rhs)
! -> see also example in test directory
! LITERATURE
! None
! HISTORY
!> \author Matthias Cuntz
!> \date May 2014
INTERFACE solve_linear_equations
MODULE PROCEDURE solve_linear_equations_1_dp, solve_linear_equations_1_sp
END INTERFACE solve_linear_equations
! ------------------------------------------------------------------
!
! NAME
! solve_linear_equations_svd
!
! PURPOSE
! Solve linear system of equations
!
!> \brief Solve linear system of equations.
!
!> \details Solve linear system of equations using singular value decomposition
!> \f[ A x = b \f]
!> Returns \f$ x \f$.
!>
!> Uses standard Lapack routine to do SVD: dgesvd.
!> then solving routine similar to Numerical Recipes: svdksb.
!> It conditions rows and columns before decomposition.
!
! INTENT(IN)
!> \param[in] "real(sp/dp) :: lhs(:,:)" Coefficients of left hand side \f$ A \f$
!> \param[in] "real(sp/dp) :: rhs(:)" Right hand side \f$ b \f$
!
! INTENT(INOUT)
! None
!
! INTENT(OUT)
! None
!
! INTENT(IN), OPTIONAL
!> \param[in] "logical, optional :: condition" If true, condition lhs and rhs before decomposition (default: true)
!
! INTENT(INOUT), OPTIONAL
! None
!
! INTENT(OUT), OPTIONAL
! None
!
! RETURNS
!> \return real(sp/dp) :: x(:) — Solution x to \f$ A x = b \f$
!
! RESTRICTIONS
! Only one right-hand side.
!
! EXAMPLE
! sol = solve_linear_equations_svd(lhs, rhs)
! -> see also example in test directory
! LITERATURE
! Press WH, Teukolsky SA, Vetterling WT, & Flannery BP - Numerical Recipes in Fortran 90 -
! The Art of Parallel Scientific Computing, 2nd Edition, Volume 2 of Fortran Numerical Recipes,
! Cambridge University Press, UK, 1996
! HISTORY
!> \author Matthias Cuntz
!> \date May 2014
INTERFACE solve_linear_equations_svd
MODULE PROCEDURE solve_linear_equations_svd_1_dp, solve_linear_equations_svd_1_sp
END INTERFACE solve_linear_equations_svd
! ------------------------------------------------------------------
!
! NAME
! solve_linear_equations_band
!
! PURPOSE
! Solve linear system of equations for a banded matrix A=(aij).
! The band storage scheme is illustrated by the following example, when
! N = 6, l = 2, u = 1:
!
! * a12 a23 a34 a45 a56
! a11 a22 a33 a44 a55 a66
! a21 a32 a43 a54 a65 *
! a31 a42 a53 a64 * *
!
! Array elements marked * are not used by the routine. You can set them zero.
!
!> \brief Solve linear system of equations.
!
!> \details Solve linear system of equations for banded coefficent-matrix
!> \f[ A x = b \f]
!> Returns \f$ x \f$.
!>
!> Uses standard Lapack routine for banded matrix: dgbsv.
!> Conditions columns before decomposition.
!
! INTENT(IN)
!> \param[in] "real(sp/dp) :: lhsb(:,:)" Coefficients of left hand side in banded form of \f$ A \f$
!> \param[in] "real(sp/dp) :: rhs(:) " Right hand side \f$ b \f$
!> \param[in] "integer(i4) :: l " number of lower minor-diagonals
!> \param[in] "integer(i4) :: u " number of upper minor-diagonals
!
! INTENT(INOUT)
! None
!
! INTENT(OUT)
! None
!
! INTENT(IN), OPTIONAL
!> \param[in] "logical, optional :: condition" If true, condition lhsb before decomposition (default: true)
!
! INTENT(INOUT), OPTIONAL
! None
!
! INTENT(OUT), OPTIONAL
! None
!
! RETURNS
!> \return real(sp/dp) :: x(:) — Solution x to \f$ A x = b \f$
!
! RESTRICTIONS
! Only one right-hand side.
! Left-hand side needs to be in banded form. You can use the banded-function to convert a given squared matrix.
!
! EXAMPLE
! sol = solve_linear_equations_band(lhsb, rhs, l, u)
! sol = solve_linear_equations_band(banded(lhs), rhs, l, u)
! -> see also example in test directory
! LITERATURE
! None
! HISTORY
!> \author Sebastian Mueller
!> \date October 2016
! Modified, Matthias Cuntz, May 2018 - allocate output for solve_linear_equations_band_1_sp
INTERFACE solve_linear_equations_band
MODULE PROCEDURE solve_linear_equations_band_1_dp, solve_linear_equations_band_1_sp
END INTERFACE solve_linear_equations_band
PRIVATE
INTERFACE svdksb
MODULE PROCEDURE svdksb_dp, svdksb_sp
END INTERFACE svdksb
! ------------------------------------------------------------------
CONTAINS
! ------------------------------------------------------------------
FUNCTION banded_dp(matrix, l, u)
IMPLICIT NONE
REAL(dp), DIMENSION(:,:), INTENT(IN) :: matrix
INTEGER(i4), INTENT(IN) :: l, u
#ifndef __PYTHON__
REAL(dp), DIMENSION(:,:), allocatable :: banded_dp
#else
REAL(dp), DIMENSION(l+u+1,size(matrix,1)) :: banded_dp
#endif
INTEGER(i4) :: i
if (size(matrix,1) /= size(matrix,2)) stop 'banded_dp: array must be squared matrix.'
if (l >= size(matrix,1)) stop 'banded_dp: l is to big. You need to choose minor diagonals within the matrix.'
if (u >= size(matrix,1)) stop 'banded_dp: u is to big. You need to choose minor diagonals within the matrix.'
if (l < 0_i4 ) stop 'banded_dp: l needs to be non-negativ.'
if (u < 0_i4 ) stop 'banded_dp: u needs to be non-negativ.'
#ifndef __PYTHON__
if (.not. allocated(banded_dp)) allocate(banded_dp(l+u+1,size(matrix,1)))
#endif
banded_dp = 0.0_dp
do i=0_i4,u
banded_dp(u+1-i,i+1:size(matrix,1)) = min_diag_dp(matrix, i)
end do
do i=1_i4,l
banded_dp(u+1+i,1:size(matrix,1)-i) = min_diag_dp(matrix,-i)
end do
END FUNCTION banded_dp
FUNCTION banded_sp(matrix, l, u)
IMPLICIT NONE
REAL(sp), DIMENSION(:,:), INTENT(IN) :: matrix
INTEGER(i4), INTENT(IN) :: l, u
#ifndef __PYTHON__
REAL(sp), DIMENSION(:,:), allocatable :: banded_sp
#else
REAL(sp), DIMENSION(l+u+1,size(matrix,1)) :: banded_sp
#endif
INTEGER(i4) :: i
if (size(matrix,1) /= size(matrix,2)) stop 'banded_sp: array must be squared matrix.'
if (l >= size(matrix,1)) stop 'banded_sp: l is to big. You need to choose minor diagonals within the matrix.'
if (u >= size(matrix,1)) stop 'banded_sp: u is to big. You need to choose minor diagonals within the matrix.'
if (l < 0_i4 ) stop 'banded_sp: l needs to be non-negativ.'
if (u < 0_i4 ) stop 'banded_sp: u needs to be non-negativ.'
#ifndef __PYTHON__
if (.not. allocated(banded_sp)) allocate(banded_sp(l+u+1,size(matrix,1)))
#endif
banded_sp = 0.0_sp
do i=0_i4,u
banded_sp(u+1-i,i+1:size(matrix,1)) = min_diag_sp(matrix, i)
end do
do i=1_i4,l
banded_sp(u+1+i,1:size(matrix,1)-i) = min_diag_sp(matrix,-i)
end do
END FUNCTION banded_sp
! ------------------------------------------------------------------
FUNCTION diag_dp(matrix)
IMPLICIT NONE
REAL(dp), DIMENSION(:,:), INTENT(IN) :: matrix
#ifndef __PYTHON__
REAL(dp), DIMENSION(:), allocatable :: diag_dp
#else
REAL(dp), DIMENSION(size(matrix,1)) :: diag_dp
#endif
INTEGER(i4) :: i
if (size(matrix,1) /= size(matrix,2)) stop 'diag_dp: array must be squared matrix.'
#ifndef __PYTHON__
if (.not. allocated(diag_dp)) allocate(diag_dp(size(matrix,1)))
#endif
forall(i=1:size(matrix,1)) diag_dp(i) = matrix(i,i)
END FUNCTION diag_dp
FUNCTION diag_sp(matrix)
IMPLICIT NONE
REAL(sp), DIMENSION(:,:), INTENT(IN) :: matrix
#ifndef __PYTHON__
REAL(sp), DIMENSION(:), allocatable :: diag_sp
#else
REAL(sp), DIMENSION(size(matrix,1)) :: diag_sp
#endif
INTEGER(i4) :: i
if (size(matrix,1) /= size(matrix,2)) stop 'diag_sp: array must be squared matrix.'
#ifndef __PYTHON__
if (.not. allocated(diag_sp)) allocate(diag_sp(size(matrix,1)))
#endif
forall(i=1:size(matrix,1)) diag_sp(i) = matrix(i,i)
END FUNCTION diag_sp
FUNCTION diag_i4(matrix)
IMPLICIT NONE
INTEGER(i4), DIMENSION(:,:), INTENT(IN) :: matrix
#ifndef __PYTHON__
INTEGER(i4), DIMENSION(:), allocatable :: diag_i4
#else
INTEGER(i4), DIMENSION(size(matrix,1)) :: diag_i4
#endif
INTEGER(i4) :: i
if (size(matrix,1) /= size(matrix,2)) stop 'diag_i4: array must be squared matrix.'
#ifndef __PYTHON__
if (.not. allocated(diag_i4)) allocate(diag_i4(size(matrix,1)))
#endif
forall(i=1:size(matrix,1)) diag_i4(i) = matrix(i,i)
END FUNCTION diag_i4
FUNCTION diag_i8(matrix)
IMPLICIT NONE
INTEGER(i8), DIMENSION(:,:), INTENT(IN) :: matrix
#ifndef __PYTHON__
INTEGER(i8), DIMENSION(:), allocatable :: diag_i8
#else
INTEGER(i8), DIMENSION(size(matrix,1)) :: diag_i8
#endif
INTEGER(i8) :: i
if (size(matrix,1) /= size(matrix,2)) stop 'diag_i8: array must be squared matrix.'
#ifndef __PYTHON__
if (.not. allocated(diag_i8)) allocate(diag_i8(size(matrix,1)))
#endif
forall(i=1:size(matrix,1)) diag_i8(i) = matrix(i,i)
END FUNCTION diag_i8
FUNCTION diag_lgt(matrix)
IMPLICIT NONE
LOGICAL, DIMENSION(:,:), INTENT(IN) :: matrix
#ifndef __PYTHON__
LOGICAL, DIMENSION(:), allocatable :: diag_lgt
#else
LOGICAL, DIMENSION(size(matrix,1)) :: diag_lgt
#endif
INTEGER(i4) :: i
if (size(matrix,1) /= size(matrix,2)) stop 'diag_lgt: array must be squared matrix.'
#ifndef __PYTHON__
if (.not. allocated(diag_lgt)) allocate(diag_lgt(size(matrix,1)))
#endif
forall(i=1:size(matrix,1)) diag_lgt(i) = matrix(i,i)
END FUNCTION diag_lgt
! ------------------------------------------------------------------
FUNCTION inverse_dp(matrix, condition)
IMPLICIT NONE
REAL(dp), DIMENSION(:,:), INTENT(IN) :: matrix
LOGICAL, OPTIONAL, INTENT(IN) :: condition
#ifndef __PYTHON__
REAL(dp), DIMENSION(:,:), allocatable :: inverse_dp
#else
REAL(dp), DIMENSION(size(matrix,2),size(matrix,1)) :: inverse_dp
#endif
INTEGER(i4) :: i, nn
real(dp), dimension(:), allocatable :: scale_cols ! scale matrix for better conditioning
integer(i4), dimension(:), allocatable :: ipiv ! needed for dgetrf lapack routine
integer(i4) :: info ! "
real(dp), dimension(:), allocatable :: work ! needed for dgetri lapack routine
integer(i4) :: lwork ! "
logical :: icondition
external :: dgetrf ! Lapack routine to compute LU factorization of a general matrix
external :: dgetri ! Lapack routine to compute inverse of matrix using the LU factorization computed by DGETRF
nn = size(matrix,2)
if (size(matrix,1) /= nn) stop 'inverse_dp: matrix must be square.'
#ifndef __PYTHON__
if (.not. allocated(inverse_dp)) allocate(inverse_dp(nn,nn))
#endif
inverse_dp = matrix
if (present(condition)) then
icondition = condition
else
icondition = .true.
endif
if (icondition) then
! Condition columns
allocate(scale_cols(nn))
scale_cols(:) = maxval(abs(inverse_dp(:,:)),1)
where (scale_cols(:) < tiny(1.0_dp)) scale_cols(:) = 1.0_dp
scale_cols(:) = 1.0_dp / scale_cols(:)
forall(i=1:nn) inverse_dp(:,i) = inverse_dp(:,i) * scale_cols(i)
endif
! LU factorisation of imatrix
allocate(ipiv(nn))
call dgetrf(nn, nn, inverse_dp, nn, ipiv, info)
if (info /= 0) stop 'inverse_dp: LU factorisation did not work.'
! Inverse of LU factorisation of imatrix
allocate(work(1))
call dgetri(nn, inverse_dp, nn, ipiv, work, -1, info)
lwork = int(work(1),i4)
deallocate(work)
allocate(work(lwork))
call dgetri(nn, inverse_dp, nn, ipiv, work, lwork, info)
if (info /= 0) stop 'hdmr_hessian: Inversion did not work.'
if (icondition) then
! rescale result
forall(i=1:nn) inverse_dp(i,:) = inverse_dp(i,:) * scale_cols(i)
deallocate(scale_cols)
endif
deallocate(ipiv)
END FUNCTION inverse_dp
FUNCTION inverse_sp(matrix, condition)
IMPLICIT NONE
REAL(sp), DIMENSION(:,:), INTENT(IN) :: matrix
LOGICAL, OPTIONAL, INTENT(IN) :: condition
#ifndef __PYTHON__
REAL(sp), DIMENSION(:,:), allocatable :: inverse_sp
#else
REAL(sp), DIMENSION(size(matrix,2),size(matrix,1)) :: inverse_sp
#endif
#ifndef __PYTHON__
if (.not. allocated(inverse_sp)) allocate(inverse_sp(size(matrix,2),size(matrix,1)))
#endif
inverse_sp = real(inverse_dp(real(matrix,dp), condition), sp)
END FUNCTION inverse_sp
! ------------------------------------------------------------------
FUNCTION min_diag_dp(matrix, n)
IMPLICIT NONE
REAL(dp), DIMENSION(:,:), INTENT(IN) :: matrix
INTEGER(i4), INTENT(IN) :: n
#ifndef __PYTHON__
REAL(dp), DIMENSION(:), allocatable :: min_diag_dp
#else
REAL(dp), DIMENSION(size(matrix,1)-abs(n)) :: min_diag_dp
#endif
if (size(matrix,1) /= size(matrix,2)) stop 'min_diag_dp: array must be squared matrix.'
if (abs(n) >= size(matrix,1)) stop 'min_diag_dp: n is to big. You need to choose a minor diagonal within the matrix.'
#ifndef __PYTHON__
if (.not. allocated(min_diag_dp)) allocate(min_diag_dp(size(matrix,1) - abs(n)))
#endif
if (n >= 0_i4) then
min_diag_dp = diag_dp(matrix(1_i4:size(matrix,1)-n,1_i4+n:size(matrix,1)))
else
min_diag_dp = diag_dp(matrix(1_i4-n:size(matrix,1),1_i4:size(matrix,1)+n))
end if
END FUNCTION min_diag_dp
FUNCTION min_diag_sp(matrix, n)
IMPLICIT NONE
REAL(sp), DIMENSION(:,:), INTENT(IN) :: matrix
INTEGER(i4), INTENT(IN) :: n
#ifndef __PYTHON__
REAL(sp), DIMENSION(:), allocatable :: min_diag_sp
#else
REAL(sp), DIMENSION(size(matrix,1)-abs(n)) :: min_diag_sp
#endif
if (size(matrix,1) /= size(matrix,2)) stop 'min_diag_sp: array must be squared matrix.'
if (abs(n) >= size(matrix,1)) stop 'min_diag_sp: n is to big. You need to choose a minor diagonal within the matrix.'
#ifndef __PYTHON__
if (.not. allocated(min_diag_sp)) allocate(min_diag_sp(size(matrix,1) - abs(n)))
#endif
if (n >= 0_i4) then
min_diag_sp = diag_sp(matrix(1_i4:size(matrix,1)-n,1_i4+n:size(matrix,1)))
else
min_diag_sp = diag_sp(matrix(1_i4-n:size(matrix,1),1_i4:size(matrix,1)+n))
end if
END FUNCTION min_diag_sp
FUNCTION min_diag_i4(matrix, n)
IMPLICIT NONE
INTEGER(i4), DIMENSION(:,:), INTENT(IN) :: matrix
INTEGER(i4), INTENT(IN) :: n
#ifndef __PYTHON__
INTEGER(i4), DIMENSION(:), allocatable :: min_diag_i4
#else
INTEGER(i4), DIMENSION(size(matrix,1)-abs(n)) :: min_diag_i4
#endif
if (size(matrix,1) /= size(matrix,2)) stop 'min_diag_i4: array must be squared matrix.'
if (abs(n) >= size(matrix,1)) stop 'min_diag_i4: n is to big. You need to choose a minor diagonal within the matrix.'
#ifndef __PYTHON__
if (.not. allocated(min_diag_i4)) allocate(min_diag_i4(size(matrix,1) - abs(n)))
#endif
if (n >= 0_i4) then
min_diag_i4 = diag_i4(matrix(1_i4:size(matrix,1)-n,1_i4+n:size(matrix,1)))
else
min_diag_i4 = diag_i4(matrix(1_i4-n:size(matrix,1),1_i4:size(matrix,1)+n))
end if
END FUNCTION min_diag_i4
FUNCTION min_diag_i8(matrix, n)
IMPLICIT NONE
INTEGER(i8), DIMENSION(:,:), INTENT(IN) :: matrix
INTEGER(i4), INTENT(IN) :: n
#ifndef __PYTHON__
INTEGER(i8), DIMENSION(:), allocatable :: min_diag_i8
#else
INTEGER(i8), DIMENSION(size(matrix,1)-abs(n)) :: min_diag_i8
#endif
if (size(matrix,1) /= size(matrix,2)) stop 'min_diag_i8: array must be squared matrix.'
if (abs(n) >= size(matrix,1)) stop 'min_diag_i8: n is to big. You need to choose a minor diagonal within the matrix.'
#ifndef __PYTHON__
if (.not. allocated(min_diag_i8)) allocate(min_diag_i8(size(matrix,1) - abs(n)))
#endif
if (n >= 0_i4) then
min_diag_i8 = diag_i8(matrix(1_i4:size(matrix,1)-n,1_i4+n:size(matrix,1)))
else
min_diag_i8 = diag_i8(matrix(1_i4-n:size(matrix,1),1_i4:size(matrix,1)+n))
end if
END FUNCTION min_diag_i8
FUNCTION min_diag_lgt(matrix, n)
IMPLICIT NONE
LOGICAL, DIMENSION(:,:), INTENT(IN) :: matrix
INTEGER(i4), INTENT(IN) :: n
#ifndef __PYTHON__
LOGICAL, DIMENSION(:), allocatable :: min_diag_lgt
#else
LOGICAL, DIMENSION(size(matrix,1)-abs(n)) :: min_diag_lgt
#endif
if (size(matrix,1) /= size(matrix,2)) stop 'min_diag_lgt: array must be squared matrix.'
if (abs(n) >= size(matrix,1)) stop 'min_diag_lgt: n is to big. You need to choose a minor diagonal within the matrix.'
#ifndef __PYTHON__
if (.not. allocated(min_diag_lgt)) allocate(min_diag_lgt(size(matrix,1) - abs(n)))
#endif
if (n >= 0_i4) then
min_diag_lgt = diag_lgt(matrix(1_i4:size(matrix,1)-n,1_i4+n:size(matrix,1)))
else
min_diag_lgt = diag_lgt(matrix(1_i4-n:size(matrix,1),1_i4:size(matrix,1)+n))
end if
END FUNCTION min_diag_lgt
! ------------------------------------------------------------------
FUNCTION solve_linear_equations_1_dp(lhs, rhs, condition)
IMPLICIT NONE
REAL(dp), DIMENSION(:,:), INTENT(IN) :: lhs
REAL(dp), DIMENSION(:), INTENT(IN) :: rhs
LOGICAL, OPTIONAL, INTENT(IN) :: condition
#ifndef __PYTHON__
REAL(dp), DIMENSION(:), allocatable :: solve_linear_equations_1_dp
#else
REAL(dp), DIMENSION(size(lhs,1)) :: solve_linear_equations_1_dp
#endif
INTEGER(i4) :: ii, nZeilen
real(dp), dimension(:,:), allocatable :: ilhs ! internal lhs
real(dp), dimension(:), allocatable :: irhs ! internal rhs
real(dp), dimension(:), allocatable :: scale_cols ! scale matrix for better conditioning
real(dp), dimension(:), allocatable :: scale_rows ! "
integer(i4), dimension(:), allocatable :: ipiv ! needed for dgesv lapack routine
integer(i4) :: info ! "
logical :: icondition
external :: dgesv ! Lapack routine to compute solution of real system of linear equations
nZeilen = size(lhs,1)
if (size(lhs,2) /= nZeilen) stop 'solve_linear_equations_1_dp: left hand side must be squared matrix.'
if (size(rhs,1) /= nZeilen) stop 'solve_linear_equations_1_dp: right hand side must have same size as left hand side.'
! internal arrays
allocate(ilhs(nZeilen,nZeilen), irhs(nZeilen))
ilhs = lhs
irhs = rhs
if (present(condition)) then
icondition = condition
else
icondition = .true.
endif
if (icondition) then
! Condition of matrix
allocate(scale_cols(nZeilen), scale_rows(nZeilen))
! Condition columns
scale_cols(:) = maxval(abs(ilhs(:,:)),1)
where (scale_cols(:) < tiny(1.0_dp)) scale_cols(:) = 1.0_dp
scale_cols(:) = 1.0_dp / scale_cols(:)
forall(ii=1:nZeilen) ilhs(:,ii) = ilhs(:,ii) * scale_cols(ii)
! Condition rows
scale_rows(:) = maxval(abs(ilhs(:,:)),2)
where (scale_rows(:) < tiny(1.0_dp)) scale_rows(:) = 1.0_dp
scale_rows(:) = 1.0_dp / scale_rows(:)
forall(ii=1:nZeilen) ilhs(ii,:) = ilhs(ii,:) * scale_rows(ii)
irhs(:) = irhs(:) * scale_rows(:)
endif
! solve linear system of equations
allocate(ipiv(nZeilen))
call dgesv(nZeilen, 1, ilhs, nZeilen, ipiv, irhs, nZeilen, info)
if (info /= 0) stop 'solve_linear_equations_1_dp: Solving of linear system did not work.'
#ifndef __PYTHON__
if (.not. allocated(solve_linear_equations_1_dp)) allocate(solve_linear_equations_1_dp(nZeilen))
#endif
solve_linear_equations_1_dp = irhs
if (icondition) then
! rescale result
solve_linear_equations_1_dp(:) = solve_linear_equations_1_dp(:) * scale_cols(:)
deallocate(scale_cols, scale_rows)
endif
deallocate(ilhs, irhs)
deallocate(ipiv)
END FUNCTION solve_linear_equations_1_dp
FUNCTION solve_linear_equations_1_sp(lhs, rhs, condition)
IMPLICIT NONE
REAL(sp), DIMENSION(:,:), INTENT(IN) :: lhs
REAL(sp), DIMENSION(:), INTENT(IN) :: rhs
LOGICAL, OPTIONAL, INTENT(IN) :: condition
#ifndef __PYTHON__
REAL(sp), DIMENSION(:), allocatable :: solve_linear_equations_1_sp
#else
REAL(sp), DIMENSION(size(rhs,1)) :: solve_linear_equations_1_sp
#endif
#ifndef __PYTHON__
if (.not. allocated(solve_linear_equations_1_sp)) allocate(solve_linear_equations_1_sp(size(rhs,1)))
#endif
solve_linear_equations_1_sp = real(solve_linear_equations_1_dp(real(lhs,dp), real(rhs,dp), condition), sp)
END FUNCTION solve_linear_equations_1_sp
! ------------------------------------------------------------------
FUNCTION solve_linear_equations_svd_1_dp(lhs, rhs, condition)
IMPLICIT NONE
REAL(dp), DIMENSION(:,:), INTENT(IN) :: lhs
REAL(dp), DIMENSION(:), INTENT(IN) :: rhs
LOGICAL, OPTIONAL, INTENT(IN) :: condition
#ifndef __PYTHON__
REAL(dp), DIMENSION(:), allocatable :: solve_linear_equations_svd_1_dp
#else
REAL(dp), DIMENSION(size(lhs,1)) :: solve_linear_equations_svd_1_dp
#endif
INTEGER(i4) :: ii, nZeilen
real(dp), dimension(:,:), allocatable :: ilhs ! internal lhs
real(dp), dimension(:), allocatable :: irhs ! internal rhs
real(dp), dimension(:), allocatable :: scale_cols ! scale matrix for better conditioning
real(dp), dimension(:), allocatable :: scale_rows ! "
real(dp), dimension(:), allocatable :: work ! needed for dgesvd lapack routine
real(dp), dimension(:), allocatable :: svdw ! "
real(dp), dimension(:,:), allocatable :: svdu ! "
real(dp), dimension(:,:), allocatable :: svdv ! "
integer(i4) :: lwork ! "
integer(i4) :: info ! "
real(dp), parameter :: svdtol = 1.0e-5_dp ! if <svdtol*maxval(svdw), then set svdw=0
logical :: icondition
external :: dgesvd ! Lapack routine to compute singular value decomposition of matrix
nZeilen = size(lhs,1)
if (size(lhs,2) /= nZeilen) stop 'solve_linear_equations_svd_1_dp: left hand side must be squared matrix.'
if (size(rhs,1) /= nZeilen) stop 'solve_linear_equations_svd_1_dp: right hand side must have same size as left hand side.'
! internal arrays
allocate(ilhs(nZeilen,nZeilen), irhs(nZeilen))