-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_maths.F90
1015 lines (770 loc) · 26.3 KB
/
mod_maths.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 maths
use precision
implicit none
real(kind=real2), parameter :: pi = 4d0*atan(1d0)
real(kind=real2), parameter :: ex = exp(1d0)
private
public :: choose, determinant, enorm, ex, eigen_std, eigen_stdd
public :: frobeniusNorm, factorial, factorial2
public :: facBig, factorialArrayReal, factorialReal, genHypGeo, heapsort
public :: integrate, invert, inverse, invert_diag, isnan, isinf, linspace
public :: Lpnorm_vector, new_random_seed, pi, pochhammerR, pseudo_inverse
public :: pseudo_inverse_R, random_int, random_int_range, sortrows
public :: sortrows_real
interface heapsort
module procedure heapsort_1array
module procedure heapsort_2array
end interface heapsort
contains
!**********************************************************************
function choose(n,k) result(c)
use precision
implicit none
integer(kind=int1), intent(in) :: n,k
integer(kind=int1) :: c
c = nint(gamma(n+1d0)/gamma(k+1d0)/gamma(n-k+1d0))
return
end function choose
!**********************************************************************
!> @brief calculates the determinant or perminant of a matrix
!> @par to calculate the determinant p=-1, for the perminant p=1, this is
!! a very expensive method with complexity O(n!) so only use for small
!! matrices. A better method would use LU decompostion.
!> @param[in] a matrix
!> @param[in] n size
!> @param[in] p det/per selector
!> @param[out] sum the cumulating sum giving the det/per
!**********************************************************************
recursive function determinant(a,n,p) result(sum)
! p = 1 => permanent
! p = -1 => determinant
use precision
implicit none
integer(kind=int1), intent(in) :: n,p
real(kind=real2), intent(in) :: a(n,n)
real(kind=real2) :: sum
integer(kind=int1) :: i, sgn
real(kind=real2) :: b(n-1,n-1)
if(n .eq. 1) then
sum = a(1,1)
else
sum = 0
sgn = 1
do i=1,n
b(:,:(i-1)) = a(2:,:i-1)
b(:,i:) = a(2:,i+1:)
sum = sum + sgn*a(1, i)*determinant(b,n-1,p)
sgn = sgn*p
enddo
endif
return
end function determinant
!**********************************************************************
subroutine eigen_std(n,a,b,e,v)
use precision
implicit none
integer(kind=int1), intent(in) :: n
real(kind=real2), intent(in) :: a(n),b(n)
real(kind=real2), intent(out) :: e(n),v(n,n)
integer(kind=int1) :: info
real(kind=real2) :: s(n-1)
real(kind=real2) :: work(2*n-2)
e = a
s(1:n-1) = b(1:n-1)
call dstev('V',n,e,s,v,n,work,info)
return
end subroutine eigen_std
!**********************************************************************
subroutine eigen_stdd(n,lwork,liwork,a,b,e,v)
use precision
implicit none
integer(kind=int1), intent(in) :: n,lwork,liwork
real(kind=real2), intent(in) :: a(n),b(n)
real(kind=real2), intent(out) :: e(n),v(n,n)
integer(kind=int1) :: info,iwork(liwork)
real(kind=real2) :: s(n-1)
real(kind=real2) :: work(lwork)
e = a
s(1:n-1) = b(1:n-1)
call dstevd('V',n,e,s,v,n,work,lwork,iwork,liwork,info)
return
end subroutine eigen_stdd
!**********************************************************************
!> @brief calculates the euclidian or l_2 norm
!> @par as this norm is more commonly used, added a more efficient implementation
!> @param[in] x vector
!> @param[out] n l_2 norm
!**********************************************************************
function enorm(x) result(n)
use precision
implicit none
real(kind=real2), intent(in) :: x(:)
real(kind=real2) :: n
n = sum(x(:)*x(:))
n = sqrt(n)
return
end function enorm
!**********************************************************************
function facBig(n) result(fac)
use precision
implicit none
integer(kind=int1), intent(in) :: n
integer*8 :: fac,i
fac = 1
if(n .gt. 0)then
do i=1,n
fac = fac*i
enddo
endif
return
end function facBig
!**********************************************************************
pure recursive double precision function factorial(n) result(fac)
use precision
implicit none
integer(kind=int1), intent(in) :: n
if(n .eq. 0) then
fac = 1d0
else
fac = dble(n)*factorial(n-1)
endif
return
end function factorial
!**********************************************************************
pure recursive double precision function factorial2(n) result(fac)
use precision
implicit none
integer(kind=int1), intent(in) :: n
if(n .ge. 2)then
fac = dble(n)*factorial2(n-2)
else
fac = 1d0
endif
return
end function factorial2
!**********************************************************************
function factorialArrayReal(n) result(facA)
use precision
implicit none
integer(kind=int1) :: n
real(kind=real2) :: facA(n+1)
integer(kind=int1) :: i
facA(1) = 1
do i=1,n
facA(i+1) = facA(i)*dble(i)
enddo
return
end function factorialArrayReal
!**********************************************************************
pure function factorialReal(n) result(fac)
use precision
implicit none
integer(kind=int1), intent(in) :: n
real(kind=real2) :: fac
integer(kind=int1) :: in
fac = 1d0
if(n .eq. 0) then
continue
else
do in=1,n
fac = fac*dble(in)
enddo
endif
return
end function factorialReal
!**********************************************************************
!> @breif calculates the frobenius norm of a matrix
!> @param[in] a matrix
!> @param[out] norm the norm
!**********************************************************************
function frobeniusNorm(a) result(norm)
use precision
implicit none
real(kind=real2), intent(in) :: a(:,:)
real(kind=real2) :: norm
integer(kind=int1) :: n,m,in,im
n = size(a,1); m = size(a,2)
norm = 0d0
do im=1,m
do in=1,n
norm = norm + abs(a(in,im))**2
enddo
enddo
norm = sqrt(norm)
return
end function frobeniusNorm
!**********************************************************************
function genHypGeo(p,q,a,b,z) result(f)
use precision
implicit none
integer(kind=int1), intent(in) :: p,q
real(kind=real2), intent(in) :: a(p),b(q),z
real(kind=real2) :: f
integer(kind=int1), parameter :: nmax = 50
integer(kind=int1) :: ip,iq,n
real(kind=real2), parameter :: tol = 1d-8
real(kind=real2) :: delf,an,bn,fac(nmax+1)
logical :: conv
conv = .false.
fac = factorialArrayReal(nmax)
f = 0d0
do n=0,nmax
an = 1d0
do ip=1,p
an = an*pochhammerR(a(ip),n)
enddo
bn = 1d0
do iq=1,q
bn = bn*pochhammerR(b(iq),n)
enddo
delf = (an/bn)*z**n/fac(n+1)
f = f + delf
if(abs(delf) .lt. abs(f)*tol) exit
enddo
return
end function genHypGeo
!**********************************************************************
subroutine heapsort_1array(a)
use precision
implicit none
real(kind=real2), intent(inout) :: a(0:)
integer(kind=int1) :: start,n,bottom
n = size(a)
do start=(n - 2)/2,0,-1
call siftdown(start,n,a)
enddo
do bottom=n-1,1,-1
call swap_entries(a(0),a(bottom))
call siftdown(0,bottom,a)
enddo
return
end subroutine heapsort_1array
!**********************************************************************
subroutine heapsort_2array(x,a)
use precision
implicit none
real(kind=real2), intent(inout) :: x(0:)
real(kind=real2), intent(inout) :: a(0:)
integer(kind=int1) :: start,n,bottom
n = size(a)
do start=(n - 2)/2,0,-1
call siftdown_array(start,n,x,a)
enddo
do bottom=n-1,1,-1
call swap_entries(a(0),a(bottom))
call swap_entries(x(0),x(bottom))
call siftdown_array(0,bottom,x,a)
enddo
return
end subroutine heapsort_2array
!**********************************************************************
function integrate(f1,g1,w) result(v)
use precision
implicit none
real(kind=real2), intent(in) :: f1(:,:), g1(:,:), w(:)
real(kind=real2) :: v(size(f1,1),size(g1,1))
integer(kind=int1) :: in, jn, kn
! Loop over the points in v
do in=1,size(f1,1)
do jn=1,size(g1,1)
! Accumulate the gaussian integrator
v(in,jn) = 0d0
do kn=1,size(w,1)
v(in,jn) = v(in,jn) + f1(in,kn)*g1(jn,kn)*w(kn)
enddo
enddo
enddo
return
end function integrate
!**********************************************************************
!> @brief matrix inversion
!> @param[in] n matrix size
!> @param[in] a matrix to invert
!> @param[out] b inversion
!> @param[out] safe optional error check
!**********************************************************************
subroutine invert(n,a,b)
use precision
implicit none
integer(kind=int1), intent(in) :: n
real(kind=real2), intent(inout) :: a(n,n),b(n,n)
integer(kind=int1) :: ipiv(n),info
real(kind=real2) :: c(n,n),work(n)
b = a
call dgetrf(n,n,a,n,ipiv,info)
call dgetri(n,a,n,ipiv,work,n,info)
c = b
b = a
a = c
return
end subroutine invert
!*******************************************************************
function invert_diag(n,a) result(ai)
use precision
implicit none
integer(kind=int1), intent(in) :: n
real(kind=real2), intent(in) :: a(n,n)
real(kind=real2) :: ai(n,n)
integer(kind=real2) :: i
ai = 0d0
do i=1,n
ai(i,i) = 1d0/a(i,i)
enddo
return
end function invert_diag
!*******************************************************************
!> @breif functional implementation of matrix inversion
!> @param[in] a matrix to invert
!> @param[out] ai inversion
!*******************************************************************
function inverse(a) result(ai)
use precision
implicit none
real(kind=real2), intent(in) :: a(:,:)
real(kind=real2) :: ai(size(a,1),size(a,2))
integer(kind=int1) :: ipiv(size(a,1)), info, n
real(kind=real2) :: c(size(a,1), size(a,2)),work(size(a,1))
n = size(a,1)
c = a
call dgetrf(n,n,c,n,ipiv,info)
call dgetri(n,c,n,ipiv,work,n,info)
ai = c
return
end function inverse
!**********************************************************************
!> @breif calculates normed errro of inversion
!> @param[in] a matrix
!> @param[in] ai inversion
!> @param[out] error inversion error
!**********************************************************************
function inversionError(a,ai) result(error)
use precision
implicit none
real(kind=real2), intent(in) :: a(:,:),ai(:,:)
real(kind=real2) :: error
integer(kind=int1) :: n,in
real(kind=real2), allocatable :: eye(:,:)
n = size(a,1)
allocate(eye(n,n))
eye = 0
do in=1,n
eye(in,in) = 1d0
enddo
eye = eye - matmul(a,ai)
error = frobeniusNorm(eye)
return
end function inversionError
!**********************************************************************
logical function isinf(y)
use, intrinsic :: iso_fortran_env
use precision
implicit none
class(*), intent(in) :: y
real(kind=4) :: fpinf,fminf
real(kind=8) :: dpinf,dminf
fpinf = b'01111111100000000000000000000000' ! +infinity
fminf = b'11111111100000000000000000000000' ! -infinity
dpinf = b'0111111111110000000000000000000000000000000000000000000000000000' ! +infinity
dminf = b'1111111111110000000000000000000000000000000000000000000000000000' ! -infinity
isinf = .false.
select type(y)
type is (real(real32))
if(y .eq. fpinf) isinf = .true.
if(y .eq. fminf) isinf = .true.
type is (real(real64))
if(y .eq. dpinf) isinf = .true.
if(y .eq. dminf) isinf = .true.
end select
return
end function isinf
!**********************************************************************
logical function isnan(y)
use, intrinsic :: iso_fortran_env
use precision
implicit none
class(*), intent(in) :: y
isnan = .false.
select type(y)
type is (real(real32))
if((y .gt. 0e0) .eqv. (y .le. 0e0)) isnan = .true.
if(y .ne. y) isnan = .true.
type is (real(real64))
if((y .gt. 0d0) .eqv. (y .le. 0d0)) isnan = .true.
if(y .ne. y) isnan = .true.
end select
return
end function isnan
!**********************************************************************
function linspace(n,x0,x1) result(x)
implicit none
integer*4, intent(in) :: n
real*8, intent(in) :: x0,x1
integer*4 :: i
real*8 :: x(n),nd,dx
nd = dble(n) - 1d0
dx = (x1-x0)/nd
do i=1,n
x(i) = x0 + dx*(i-1d0)
enddo
return
end function linspace
!**********************************************************************
!> @brief calculates the L_p norm of a vector
!> @par calculates (x(1)^p + x(2)^p + ...)^(1/p)
!> @param[in] x vector
!> @param[in] p norm type
!> @param[out] lp norm value
!**********************************************************************
function Lpnorm_vector(x,p) result(lp)
use precision
implicit none
integer(kind=int1), intent(in) :: p
real(kind=real2), intent(in) :: x(:)
real(kind=real2) :: lp
real(kind=real2), allocatable :: xp(:)
allocate(xp,mold=x)
xp(:) = x(:)**p
lp = sum(xp)
lp = lp**(1d0/dble(p))
return
end function Lpnorm_vector
!**********************************************************************
function monteCarloIntegration(npnts,stride,v,u) result(int)
use precision
implicit none
integer(kind=int1), intent(in) :: npnts,stride
real(kind=real2), intent(in) :: v,u(npnts)
real(kind=real2) :: int
integer(kind=int1) :: ic,in
real(kind=real2) :: temp
int = 0d0
do ic=1,npnts,stride
temp = 0d0
do in=1,stride
temp = temp + u(ic-1+in)
enddo
int = int + temp
enddo
int = v*int/dble(npnts)
return
end function monteCarloIntegration
!**********************************************************************
recursive function multiindex(ns,ni,sum) result(m)
! ns - number of state, ni - number of indexes, sum = sum of multiindexs
use precision
implicit none
integer(kind=int1), intent(in) :: ns,ni,sum
integer(kind=int1) :: m(ni,ns)
integer(kind=int1) :: ma,s,is
integer(kind=int1), allocatable :: temp(:,:)
if(ni .eq. 1)then
m = sum
else
ma = 0
do is=0,sum
s = simplicy_number(sum+1-is,ni-2)
allocate(temp(ni-1,s))
temp = multiindex(s,ni-1,sum-is)
m(1,ma+1:ma+s) = is
m(1+1:ni,ma+1:ma+s) = temp(1:ni-1,1:s)
ma = ma + s
deallocate(temp)
enddo
endif
return
end function multiindex
!**********************************************************************
subroutine new_random_seed
use precision
implicit none
integer(kind=int1) :: tvalues(8),n_seed
integer(kind=int1), allocatable :: seed(:)
! Set new random number seed
call date_and_time(values=tvalues)
call random_seed(size=n_seed)
allocate(seed(n_seed))
seed(1) = tvalues(8)*tvalues(7)*tvalues(6)
if(n_seed .gt. 2)then
seed(2) = (tvalues(1)+tvalues(2))*tvalues(8)*tvalues(3)
seed(n_seed) = (tvalues(3)+tvalues(4))*tvalues(7)*tvalues(5)
endif
call random_seed(put=seed)
deallocate(seed)
return
end subroutine new_random_seed
!**********************************************************************
pure function pochhammerR(x,n) result(p)
use precision
implicit none
integer(kind=int1), intent(in) :: n
real(kind=real2), intent(in) :: x
real(kind=real2) :: p
integer(kind=int1) :: i
p = 1d0
if(n .eq. 0) return
do i=0,n-1
p = p*(x+dble(i))
enddo
return
end function pochhammerR
!**********************************************************************
function pseudo_inverse_R(b) result(ai)
use precision
implicit none
real(kind=real2), intent(in) :: b(:,:)
real(kind=real2) :: ai(size(b,2),size(b,1))
integer(kind=int1) :: m,n,i,j,lwork,lwmax
real(kind=real2) :: a(size(b,1),size(b,2)),s(size(b,2))
real(kind=real2) :: sigma(size(b,1),size(b,2)),work(10000),sigmi(size(b,2),size(b,1))
real(kind=real2) :: u(size(b,1),size(b,1)),ut(size(b,1),size(b,1))
real(kind=real2) :: v(size(b,2),size(b,2)),vt(size(b,2),size(b,2))
real(kind=real2), parameter :: eps = 1d2*epsilon(b(1,1))
m = size(b,1)
n = size(b,2)
a = b
lwork = -1
lwmax = 10000
call dgesvd('A','A',m,n,a,m,s,u,m,vt,n,work,lwork,i)
lwork = min(lwmax,int(work(1)))
call dgesvd('A','A',m,n,a,m,s,u,m,vt,n,work,lwork,i)
sigma = 0d0
do i=1,n
sigma(i,i) = s(i)
enddo
! find pseudo inverse of sigma
sigmi = transpose(sigma)
do i=1,n
if(sigmi(i,i) .lt. eps) then
sigmi(i,i) = 0d0
else
sigmi(i,i) = 1d0/sigmi(i,i)
endif
enddo
v = transpose(vt)
ut = transpose(u)
ai = matmul(v,matmul(sigmi,ut))
return
end function pseudo_inverse_R
!**********************************************************************
function pseudo_inverse(b) result(ai)
use precision
implicit none
real(kind=real2), intent(in) :: b(:,:)
real(kind=real2) :: ai(size(b,2),size(b,1))
integer(kind=int1) :: m,n,i,j,lwork,lwmax
real(kind=real2) :: a(size(b,1),size(b,2)),s(size(b,2))
real(kind=real2) :: sigma(size(b,1),size(b,2)),work(10000),sigmi(size(b,2),size(b,1))
real(kind=real2) :: u(size(b,1),size(b,1)),ut(size(b,1),size(b,1))
real(kind=real2) :: v(size(b,2),size(b,2)),vt(size(b,2),size(b,2))
real(kind=real2), parameter :: eps = 1d2*epsilon(b(1,1))
m = size(b,1)
n = size(b,2)
a = b
lwork = -1
lwmax = 10000
call dgesvd('A','A',m,n,a,m,s,u,m,vt,n,work,lwork,i)
lwork = min(lwmax,int(work(1)))
call dgesvd('A','A',m,n,a,m,s,u,m,vt,n,work,lwork,i)
sigma = 0d0
do i=1,n
sigma(i,i) = s(i)
enddo
! find pseudo inverse of sigma
sigmi = transpose(sigma)
do i=1,n
if(sigmi(i,i) .lt. eps) then
sigmi(i,i) = 0d0
else
sigmi(i,i) = 1d0/sigmi(i,i)
endif
enddo
v = transpose(vt)
ut = transpose(u)
ai = matmul(v,matmul(sigmi,ut))
return
end function pseudo_inverse
!**********************************************************************
subroutine random_int(harvest)
! Use random_number to creat a random positive integer
use precision
implicit none
integer(kind=int1), intent(out) :: harvest
real(kind=real2) :: a
real(kind=real2) :: b,c
b = 2d0**32d0; c = b/2d0
call random_number(a)
a = a*b
a = a - c
harvest = nint(a)
return
end subroutine random_int
!**********************************************************************
subroutine random_int_range(i1,i2,harvest)
! Use random_number to creat a random positive integer
use precision
implicit none
integer(kind=int1), intent(in) :: i1,i2
integer(kind=int1), intent(out) :: harvest
real(kind=real2) :: a
real(kind=real2) :: di
di = dble(i2-i1)
call random_number(a)
a = a*di
a = a + dble(i1)
harvest = nint(a)
return
end subroutine random_int_range
!**********************************************************************
subroutine siftdown(start,bottom,a)
use precision
implicit none
integer(kind=int1), intent(in) :: start,bottom
real(kind=real2), intent(inout) :: a(0:)
integer(kind=int1) :: child,root
root = start
do while(2*root + 1 .lt. bottom)
child = 2*root + 1
if(child + 1 .lt. bottom) then
if (a(child) .lt. a(child+1)) child = child + 1
endif
if(a(root) .lt. a(child)) then
call swap_entries(a(child),a(root))
root = child
else
return
endif
enddo
return
end subroutine siftdown
!**********************************************************************
subroutine siftdown_array(start,bottom,x,a)
use precision
implicit none
integer(kind=int1), intent(in) :: start,bottom
real(kind=real2), intent(inout) :: x(0:)
real(kind=real2), intent(inout) :: a(0:)
integer(kind=int1) :: child,root
root = start
do while(2*root + 1 .lt. bottom)
child = 2*root + 1
if(child + 1 .lt. bottom) then
if (a(child) .lt. a(child+1)) child = child + 1
endif
if(a(root) .lt. a(child)) then
call swap_entries(a(child),a(root))
call swap_entries(x(child),x(root))
root = child
else
return
endif
enddo
return
end subroutine siftdown_array
!**********************************************************************
pure function simplicy_number(n,d) result(s)
use precision
implicit none
integer(kind=int1), intent(in) :: n,d
integer(kind=int1) :: s
s = nint(pochhammerR(1d0*n,d))/factorial(d)
return
end function simplicy_number
!**********************************************************************
recursive subroutine sortrows(n,m,a) ! with bubble based sort
use precision
implicit none
integer(kind=int1), intent(in) :: n,m
integer(kind=int1), intent(inout) :: a(n,m)
integer(kind=int1) :: temp(m)
integer(kind=int1) :: i,j,k
logical :: swapped
do j=n-1,1,-1
swapped = .false.
do i=1,j
if(a(i,1) .gt. a(i+1,1))then
temp(:) = a(i,:)
a(i,:) = a(i+1,:)
a(i+1,:) = temp(:)
swapped = .true.
endif
if((a(i,1) .eq. a(i+1,1)) .and. (m .gt. 1))then
do k=0,i-1
if((a(i-k,1) .ne. a(i-k+1,1))) exit
enddo
k = k - 1
call sortrows(2+k,m-1,a(i-k:i+1,2:))
endif
enddo
if(.not. swapped) exit
enddo
return
end subroutine sortrows
!**********************************************************************
subroutine sortrows_flip(n,m,a) ! with bubble based sort
use precision
implicit none
integer(kind=int1), intent(in) :: n,m
integer(kind=int1), intent(inout) :: a(n,m)
integer(kind=int1) :: b(n,m)
b = a(:,m:1:-1)
call sortrows(n,m,b)
a = b(:,m:1:-1)
return
end subroutine sortrows_flip
!**********************************************************************
recursive subroutine sortrows_real(n,m,a) ! with bubble based sort
use precision
implicit none
integer(kind=int1), intent(in) :: n,m
real(kind=real2), intent(inout) :: a(n,m)
integer(kind=int1) :: i,j,k
real(kind=real2) :: temp(m)
real(kind=real2), parameter :: tol = 100d0*epsilon(a(1,1))
logical :: swapped
do j=n-1,1,-1
swapped = .false.
do i=1,j
if((a(i,1) .gt. a(i+1,1)) .and. (abs(a(i,1)-a(i+1,1)) .gt. tol))then
temp(:) = a(i,:)
a(i,:) = a(i+1,:)
a(i+1,:) = temp(:)
swapped = .true.
endif
if((abs(a(i,1)-a(i+1,1)) .lt. tol) .and. (m .gt. 1))then
do k=0,i-1
if((a(i-k,1) .ne. a(i-k+1,1))) exit
enddo
k = k - 1
call sortrows_real(2+k,m-1,a(i-k:i+1,2:))
endif
enddo
if(.not. swapped) exit
enddo
return
end subroutine sortrows_real
!**********************************************************************
subroutine sortrows_real_flip(n,m,a) ! with bubble based sort
use precision
implicit none
integer(kind=int1), intent(in) :: n,m
real(kind=real2), intent(inout) :: a(n,m)
real(kind=real2) :: b(n,m)
b = a(:,m:1:-1)
call sortrows_real(n,m,b)
a = b(:,m:1:-1)
return
end subroutine sortrows_real_flip
!**********************************************************************
pure subroutine swap_entries(a,b)
use precision