-
Notifications
You must be signed in to change notification settings - Fork 3
/
forlab.f90
9427 lines (8398 loc) · 286 KB
/
forlab.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
!=======================================================================
! Forlab
!-----------------------------------------------------------------------
! Forlab aims to provide a package of functions for scientific
! computing in Fortran.
!
! Created by
! Keurfon Luu <keurfon.luu@mines-paristech.fr>
! MINES ParisTech - Centre de Géosciences
! PSL - Research University
!
! Notes
!-----------------------------------------------------------------------
! When changing precision (IPRE and/or RPRE), the whole program needs to
! be recompiled.
!=======================================================================
module forlab
#ifdef do_mpi
use mpi
#endif
implicit none
!=======================================================================
! Parameters
!=======================================================================
integer, public, parameter :: IPRE = 4
integer, public, parameter :: RPRE = 4
integer, public, parameter :: CLEN = 512
real(kind = 8), public, parameter :: pi = 3.141592653589793238460d0
real(kind = 8), public, save :: tic_time
!=======================================================================
! Functions
!=======================================================================
private
public :: File, acosd, asind, atand, argmax, argmin, argsort, arange, &
angle, bsplrep1, bsplrep2, bspline1, bspline2, chol, cosd, countlines, &
cov, cumsum, chi2cdf, chi2pdf, chi2inv, chi2rand, check_directory, &
det, diag, disp, deg2utm, datenum, datevec, datestr, deboor, diff, &
eye, eig, find, flip, fliplr, flipud, fminbnd, gammainc, horzcat, &
hann, interp1, interp2, interp3, inv, ismember, isoutlier, issquare, &
isleap, issymmetric, kurtosis, k2test, kde, loadtxt, loadbin, linspace, &
mean, median, mad, meshgrid, nextpow2, norm, normpdf, num2str, ones, &
outer, pascal, prctile, progress_bar, progress_perc, rng, randu, randn, &
randi, randperm, repmat, rms, savetxt, savebin, sind, sort, solve, &
svd, svdsolve, std, spline1, spline2, skewness, signum, sinc, &
split_argument, tand, tic, toc, trace, tril, triu, utm2deg, vertcat, &
var, zeros, dbindex, gmm, kmeans, mbkmeans, silhouette
#ifdef do_mpi
public :: mpi_rpre
#endif
!=======================================================================
! Object File
!=======================================================================
type File
integer(kind = IPRE) :: unit
character(len = CLEN) :: filename
contains
procedure, private :: open1, open2, countlines1, file_exist
procedure, public :: close
generic, public :: open => open1, open2
generic, public :: countlines => countlines1
generic, public :: exist => file_exist
end type File
!=======================================================================
! Abstract function
!=======================================================================
abstract interface
real(kind = RPRE) function func1d(x)
import :: RPRE
real(kind = RPRE), intent(in) :: x
end function func1d
end interface
!=======================================================================
! Polymorphic functions and subroutines
!=======================================================================
!---------------------------------------------------------------------
! Function acosd
!---------------------------------------------------------------------
interface acosd
module procedure acosd0, acosd1, acosd2, acosd3
end interface acosd
!---------------------------------------------------------------------
! Function angle
!---------------------------------------------------------------------
interface angle
module procedure angle0, angle1
end interface angle
!---------------------------------------------------------------------
! Function argmax
!---------------------------------------------------------------------
interface argmax
module procedure argmax1, argmax2, argmax3
end interface argmax
!---------------------------------------------------------------------
! Function argmin
!---------------------------------------------------------------------
interface argmin
module procedure argmin1, argmin2, argmin3
end interface argmin
!---------------------------------------------------------------------
! Function asind
!---------------------------------------------------------------------
interface asind
module procedure asind0, asind1, asind2, asind3
end interface asind
!---------------------------------------------------------------------
! Function atand
!---------------------------------------------------------------------
interface atand
module procedure atand0, atand1, atand2, atand3
end interface atand
!---------------------------------------------------------------------
! Function bspline1
!---------------------------------------------------------------------
interface bspline1
module procedure bspline1_1
end interface bspline1
!---------------------------------------------------------------------
! Function bspline2
!---------------------------------------------------------------------
interface bspline2
module procedure bspline2_2
end interface bspline2
!---------------------------------------------------------------------
! Function chi2cdf
!---------------------------------------------------------------------
interface chi2cdf
module procedure chi2cdf0, chi2cdf1_0, chi2cdf1_1
end interface chi2cdf
!---------------------------------------------------------------------
! Function chi2inv
!---------------------------------------------------------------------
interface chi2inv
module procedure chi2inv0, chi2inv1_0, chi2inv1_1
end interface chi2inv
!---------------------------------------------------------------------
! Function chi2pdf
!---------------------------------------------------------------------
interface chi2pdf
module procedure chi2pdf0, chi2pdf1_0, chi2pdf1_1
end interface chi2pdf
!---------------------------------------------------------------------
! Function chi2rand
!---------------------------------------------------------------------
interface chi2rand
module procedure chi2rand0, chi2rand1
end interface chi2rand
!---------------------------------------------------------------------
! Function cosd
!---------------------------------------------------------------------
interface cosd
module procedure cosd0, cosd1, cosd2, cosd3
end interface cosd
!---------------------------------------------------------------------
! Function countlines
!---------------------------------------------------------------------
interface countlines
module procedure countlines2
end interface countlines
!---------------------------------------------------------------------
! Function cov
!---------------------------------------------------------------------
interface cov
module procedure cov1_1, cov1_2, cov2_1, cov2_2
end interface cov
!---------------------------------------------------------------------
! Function cumsum
!---------------------------------------------------------------------
interface cumsum
module procedure cumsum1, cumsum2
end interface cumsum
!---------------------------------------------------------------------
! Function datenum
!---------------------------------------------------------------------
interface datenum
module procedure datenum0
end interface datenum
!---------------------------------------------------------------------
! Function datestr
!---------------------------------------------------------------------
interface datestr
module procedure datestr0_0
end interface datestr
!---------------------------------------------------------------------
! Function datevec
!---------------------------------------------------------------------
interface datevec
module procedure datevec0
end interface datevec
!---------------------------------------------------------------------
! Function dbindex
!---------------------------------------------------------------------
interface dbindex
module procedure dbindex1, dbindex2
end interface dbindex
!---------------------------------------------------------------------
! Function deg2utm
!---------------------------------------------------------------------
interface deg2utm
module procedure deg2utm0, deg2utm1
end interface deg2utm
!---------------------------------------------------------------------
! Function diag
!---------------------------------------------------------------------
interface diag
module procedure diag1, diag2
end interface diag
!---------------------------------------------------------------------
! Function diff
!---------------------------------------------------------------------
interface diff
module procedure diff1, diff2
end interface diff
!---------------------------------------------------------------------
! Subroutine disp
!---------------------------------------------------------------------
interface disp
module procedure disp_i0, disp_r0, disp_c0, disp_i1, disp_r1, &
disp_c1, disp_i2, disp_r2, disp_i3, disp_r3
end interface disp
!---------------------------------------------------------------------
! Function File
!---------------------------------------------------------------------
interface File
module procedure init_File
end interface File
!---------------------------------------------------------------------
! Function find
!---------------------------------------------------------------------
interface find
module procedure find1, find2, find3
end interface find
!---------------------------------------------------------------------
! Function flip
!---------------------------------------------------------------------
interface flip
module procedure flip_i1, flip_r1, flip_i2, flip_r2, flip_i3, &
flip_r3
end interface flip
!---------------------------------------------------------------------
! Function flipud
!---------------------------------------------------------------------
interface flipud
module procedure flipud_i1, flipud_r1, flipud_i2, flipud_r2
end interface flipud
!---------------------------------------------------------------------
! Function fliplr
!---------------------------------------------------------------------
interface fliplr
module procedure fliplr_i1, fliplr_r1, fliplr_i2, fliplr_r2
end interface fliplr
!---------------------------------------------------------------------
! Function gammainc
!---------------------------------------------------------------------
interface gammainc
module procedure gammainc0, gammainc1_0
end interface gammainc
!---------------------------------------------------------------------
! Function gmm
!---------------------------------------------------------------------
interface gmm
module procedure gmm1, gmm2
end interface gmm
!---------------------------------------------------------------------
! Function horzcat
!---------------------------------------------------------------------
interface horzcat
module procedure horzcat_i1, horzcat_r1, horzcat_i2, horzcat_r2, horzcat_i12, &
horzcat_r12, horzcat_i21, horzcat_r21
end interface horzcat
!---------------------------------------------------------------------
! Function interp1
!---------------------------------------------------------------------
interface interp1
module procedure interp1_0, interp1_1
end interface interp1
!---------------------------------------------------------------------
! Function interp2
!---------------------------------------------------------------------
interface interp2
module procedure interp2_0, interp2_1, interp2_2
end interface interp2
!---------------------------------------------------------------------
! Function interp3
!---------------------------------------------------------------------
interface interp3
module procedure interp3_0, interp3_1
end interface interp3
!---------------------------------------------------------------------
! Function ismember
!---------------------------------------------------------------------
interface ismember
module procedure ismember_i0i1, ismember_i0r1, ismember_i0i2, &
ismember_i0r2, ismember_i0i3, ismember_i0r3, ismember_r0i1, &
ismember_r0r1, ismember_r0i2, ismember_r0r2, ismember_r0i3, &
ismember_r0r3
end interface ismember
!---------------------------------------------------------------------
! Function kde
!---------------------------------------------------------------------
interface kde
module procedure kde1, kde2
end interface kde
!---------------------------------------------------------------------
! Function kmeans
!---------------------------------------------------------------------
interface kmeans
module procedure kmeans1, kmeans2
end interface kmeans
!---------------------------------------------------------------------
! Function kurtosis
!---------------------------------------------------------------------
interface kurtosis
module procedure kurtosis1, kurtosis2
end interface kurtosis
!---------------------------------------------------------------------
! Function linspace
!---------------------------------------------------------------------
interface linspace
module procedure linspace_r8r8, linspace_r4r4, linspace_i4i4, &
linspace_r8i4, linspace_r4i4, linspace_i4r8, linspace_i4r4
end interface linspace
!---------------------------------------------------------------------
! Function loadbin
!---------------------------------------------------------------------
interface loadbin
module procedure loadbin0, loadbin1, loadbin2, loadbin3
end interface loadbin
!---------------------------------------------------------------------
! Function loadtxt
!---------------------------------------------------------------------
interface loadtxt
module procedure loadtxt1, loadtxt2
end interface loadtxt
!---------------------------------------------------------------------
! Function log2
!---------------------------------------------------------------------
interface log2
module procedure log2_i0, log2_r0, log2_i1, log2_r1
end interface log2
!---------------------------------------------------------------------
! Function mad
!---------------------------------------------------------------------
interface mad
module procedure mad1, mad2
end interface mad
!---------------------------------------------------------------------
! Function mbkmeans
!---------------------------------------------------------------------
interface mbkmeans
module procedure mbkmeans1, mbkmeans2
end interface mbkmeans
!---------------------------------------------------------------------
! Function median
!---------------------------------------------------------------------
interface median
module procedure median1, median2
end interface median
!---------------------------------------------------------------------
! Function mean
!---------------------------------------------------------------------
interface mean
module procedure mean1, mean2
end interface mean
!---------------------------------------------------------------------
! Subroutine meshgrid
!---------------------------------------------------------------------
interface meshgrid
module procedure meshgrid2
end interface meshgrid
!---------------------------------------------------------------------
! Function nextpow2
!---------------------------------------------------------------------
interface nextpow2
module procedure nextpow2_0, nextpow2_1
end interface nextpow2
!---------------------------------------------------------------------
! Function norm
!---------------------------------------------------------------------
interface norm
module procedure norm1, norm2
end interface norm
!---------------------------------------------------------------------
! Function normpdf
!---------------------------------------------------------------------
interface normpdf
module procedure normpdf0, normpdf1, normpdf2
end interface normpdf
!---------------------------------------------------------------------
! Function num2str
!---------------------------------------------------------------------
interface num2str
module procedure num2str_i4, num2str_i8, num2str_r4, num2str_r8
end interface num2str
!---------------------------------------------------------------------
! Function ones
!---------------------------------------------------------------------
interface ones
module procedure ones1, ones2, ones3
end interface ones
!---------------------------------------------------------------------
! Function prctile
!---------------------------------------------------------------------
interface prctile
module procedure prctile0, prctile1
end interface prctile
!---------------------------------------------------------------------
! Function randi
!---------------------------------------------------------------------
interface randi
module procedure randi0_0, randi0_1, randi1_0, randi1_1, randi2_0, &
randi2_1, randi3_0, randi3_1
end interface randi
!---------------------------------------------------------------------
! Function randu
!---------------------------------------------------------------------
interface randu
module procedure randu0, randu1, randu2, randu3
end interface randu
!---------------------------------------------------------------------
! Function randn
!---------------------------------------------------------------------
interface randn
module procedure randn0, randn1, randn2, randn3
end interface randn
!---------------------------------------------------------------------
! Function repmat
!---------------------------------------------------------------------
interface repmat
module procedure repmat1, repmat2
end interface repmat
!---------------------------------------------------------------------
! Function rms
!---------------------------------------------------------------------
interface rms
module procedure rms1, rms2
end interface rms
!---------------------------------------------------------------------
! Subroutine savebin
!---------------------------------------------------------------------
interface savebin
module procedure savebin1_r4, savebin1_r8, savebin2_r4, savebin2_r8, &
savebin3_r4, savebin3_r8
end interface savebin
!---------------------------------------------------------------------
! Subroutine savetxt
!---------------------------------------------------------------------
interface savetxt
module procedure savetxt1_i4, savetxt1_r4, savetxt1_i8, savetxt1_r8, &
savetxt2_i4, savetxt2_r4, savetxt2_i8, savetxt2_r8
end interface savetxt
!---------------------------------------------------------------------
! Function signum
!---------------------------------------------------------------------
interface signum
module procedure signum0, signum1, signum2
end interface signum
!---------------------------------------------------------------------
! Function sinc
!---------------------------------------------------------------------
interface sinc
module procedure sinc0, sinc1
end interface sinc
!---------------------------------------------------------------------
! Function silhouette
!---------------------------------------------------------------------
interface silhouette
module procedure silhouette1, silhouette2
end interface silhouette
!---------------------------------------------------------------------
! Function sind
!---------------------------------------------------------------------
interface sind
module procedure sind0, sind1, sind2, sind3
end interface sind
!---------------------------------------------------------------------
! Function skewness
!---------------------------------------------------------------------
interface skewness
module procedure skewness1, skewness2
end interface skewness
!---------------------------------------------------------------------
! Function spline1
!---------------------------------------------------------------------
interface spline1
module procedure spline1_0, spline1_1
end interface spline1
!---------------------------------------------------------------------
! Function spline2
!---------------------------------------------------------------------
interface spline2
module procedure spline2_1, spline2_2
end interface spline2
!---------------------------------------------------------------------
! Function std
!---------------------------------------------------------------------
interface std
module procedure std1, std2
end interface std
!---------------------------------------------------------------------
! Function tand
!---------------------------------------------------------------------
interface tand
module procedure tand0, tand1, tand2, tand3
end interface tand
!---------------------------------------------------------------------
! Function tril
!---------------------------------------------------------------------
interface tril
module procedure tril_i, tril_r, tril_c
end interface tril
!---------------------------------------------------------------------
! Function triu
!---------------------------------------------------------------------
interface triu
module procedure triu_i, triu_r, triu_c
end interface triu
!---------------------------------------------------------------------
! Function utm2deg
!---------------------------------------------------------------------
interface utm2deg
module procedure utm2deg0, utm2deg1
end interface utm2deg
!---------------------------------------------------------------------
! Function var
!---------------------------------------------------------------------
interface var
module procedure var1, var2
end interface var
!---------------------------------------------------------------------
! Function vertcat
!---------------------------------------------------------------------
interface vertcat
module procedure vertcat_r1, vertcat_r2, vertcat_c2, vertcat_r12, &
vertcat_r21
end interface vertcat
!---------------------------------------------------------------------
! Function zeros
!---------------------------------------------------------------------
interface zeros
module procedure zeros1, zeros2, zeros3
end interface zeros
!=======================================================================
! End of declaration of interfaces
!=======================================================================
contains
!=======================================================================
! acosd
!-----------------------------------------------------------------------
! acosd computes the inverse cosine in degrees.
!
! Syntax
!-----------------------------------------------------------------------
! y = acosd(x)
!
! Description
!-----------------------------------------------------------------------
! y = acosd(x) returns the inverse cosine of the elements in x in
! degrees. For real elements of x in the domain [-1,1], acosd returns
! values in the range [0,180]. For values of x outside this range,
! acosd returns NaN (Not a Number).
!
! Examples
!-----------------------------------------------------------------------
! y = acosd(1.)
! 1.
!
! y = acosd(2.)
! NaN
!
! x = [ -1., 0., 1. ]
! y = acosd(x)
! 180. 90. 0.
!=======================================================================
real(kind = RPRE) function acosd0(x)
real(kind = RPRE), intent(in) :: x
acosd0 = acos(x)*180.0d0/pi
return
end function acosd0
function acosd1(x)
real(kind = RPRE), dimension(:), allocatable :: acosd1
real(kind = RPRE), dimension(:), intent(in) :: x
acosd1 = acos(x)*180.0d0/pi
return
end function acosd1
function acosd2(A)
real(kind = RPRE), dimension(:,:), allocatable :: acosd2
real(kind = RPRE), dimension(:,:), intent(in) :: A
acosd2 = acos(A)*180.0d0/pi
return
end function acosd2
function acosd3(X)
real(kind = RPRE), dimension(:,:,:), allocatable :: acosd3
real(kind = RPRE), dimension(:,:,:), intent(in) :: X
acosd3 = acos(X)*180.0d0/pi
return
end function acosd3
!=======================================================================
! angle
!-----------------------------------------------------------------------
! angle compute the phase angle.
!
! Syntax
!-----------------------------------------------------------------------
! p = angle(z)
! P = angle(Z)
!
! Description
!-----------------------------------------------------------------------
! p = angle(z) returns the phase angle in radians of the complex
! number z.
!
! P = angle(Z) returns the phase angles in radians of each complex
! numbers in vector Z.
!=======================================================================
real(kind = RPRE) function angle0(z)
complex(kind = RPRE), intent(in) :: z
angle0 = imag(log(z))
return
end function angle0
function angle1(Z)
real(kind = RPRE), dimension(:), allocatable :: angle1
complex(kind = RPRE), dimension(:), intent(in) :: Z
integer(kind = IPRE) :: i, n
n = size(Z)
angle1 = zeros(n)
do i = 1, n
angle1(i) = angle0(Z(i))
end do
return
end function angle1
!=======================================================================
! arange
!-----------------------------------------------------------------------
! arange returns evenly spaced vector.
!
! Syntax
!-----------------------------------------------------------------------
! x = arange(first, last)
!
! Description
!-----------------------------------------------------------------------
! x = arange(first, last) returns an evenly spaced integer vector
! starting from first and ending at last.
!
! Examples
!-----------------------------------------------------------------------
! x = arange(1, 9)
! 1 2 3 4 5 6 7 8 9
!=======================================================================
function arange(first, last)
integer(kind = IPRE), dimension(:), allocatable :: arange
integer(kind = IPRE), intent(in) :: first, last
integer(kind = IPRE) :: i
arange = [ ( i, i = first, last ) ]
return
end function arange
!=======================================================================
! argmax
!-----------------------------------------------------------------------
! argmax computes the indices of the maximum value of an array.
!
! Syntax
!-----------------------------------------------------------------------
! y = argmax(x)
! y = argmax(A)
! y = argmax(X)
!
! Description
!-----------------------------------------------------------------------
! y = argmax(x) returns the index of the maximum value of the vector x.
!
! y = argmax(A) returns a 2-elements vector with the indices of the
! maximum value of the matrix A.
!
! y = argmax(X) returns a 3-elements vector with the indices of the
! maximum value of the 3-dimensional matrix X.
!
! Notes
!-----------------------------------------------------------------------
! argmax ignores NaN values.
!=======================================================================
integer(kind = IPRE) function argmax1(x)
real(kind = RPRE), dimension(:), intent(in) :: x
argmax1 = maxloc(x, 1, .not. isnan(x))
return
end function argmax1
function argmax2(A)
integer(kind = IPRE) :: argmax2(2)
real(kind = IPRE), dimension(:,:), intent(in) :: A
argmax2 = maxloc(A, .not. isnan(A))
return
end function argmax2
function argmax3(X)
integer(kind = IPRE) :: argmax3(3)
real(kind = IPRE), dimension(:,:,:), intent(in) :: X
argmax3 = maxloc(X, .not. isnan(X))
return
end function argmax3
!=======================================================================
! argmin
!-----------------------------------------------------------------------
! argmin computes the indices of the minimum value of an array.
!
! Syntax
!-----------------------------------------------------------------------
! y = argmin(x)
! y = argmin(A)
! y = argmin(X)
!
! Description
!-----------------------------------------------------------------------
! y = argmin(x) returns the index of the minimum value of the vector x.
!
! y = argmin(A) returns a 2-elements vector with the indices of the
! minimum value of the matrix A.
!
! y = argmin(X) returns a 3-elements vector with the indices of the
! minimum value of the 3-dimensional matrix X.
!
! Notes
!-----------------------------------------------------------------------
! argmin ignores NaN values.
!=======================================================================
integer(kind = IPRE) function argmin1(x)
real(kind = RPRE), dimension(:), intent(in) :: x
argmin1 = minloc(x, 1, .not. isnan(x))
return
end function argmin1
function argmin2(A)
integer(kind = IPRE) :: argmin2(2)
real(kind = IPRE), dimension(:,:), intent(in) :: A
argmin2 = minloc(A, .not. isnan(A))
return
end function argmin2
function argmin3(X)
integer(kind = IPRE) :: argmin3(3)
real(kind = IPRE), dimension(:,:,:), intent(in) :: X
argmin3 = minloc(X, .not. isnan(X))
return
end function argmin3
!=======================================================================
! argsort
!-----------------------------------------------------------------------
! argsort generates the indices that would sort an array.
!
! Syntax
!-----------------------------------------------------------------------
! y = argsort(x)
! y = argsort(x, 1)
! y = argsort(x, 2)
!
! Description
!-----------------------------------------------------------------------
! y = argsort(x) returns the indices that would sort an array in
! ascending order.
!
! y = argsort(x, 1) (see y = argsort(x)).
!
! y = argsort(x, 2) returns the indices that would sort an array in
! descending order.
!
! Notes
!-----------------------------------------------------------------------
! x(argsort(x), order) returns the same result as sort(x, order).
!=======================================================================
function argsort(x, order)
integer(kind = IPRE), dimension(:), allocatable :: argsort
real(kind = RPRE), dimension(:), intent(in) :: x
integer(kind = IPRE), intent(in), optional :: order
integer(kind = IPRE) :: i, n
real(kind = RPRE), dimension(:), allocatable :: xsort
n = size(x)
xsort = x
argsort = [ ( i, i = 1, n ) ]
if ((.not. present(order)) .or. (order .eq. 1)) then
call quickargsort(xsort, argsort, n, 1)
elseif (order .eq. 2) then
call quickargsort(xsort, argsort, n, 2)
end if
return
contains
!-------------------------------------------------------------------
! quickargsort
!-------------------------------------------------------------------
recursive subroutine quickargsort(x, idx, n, order)
real(kind = RPRE), dimension(n), intent(inout) :: x
integer(kind = IPRE), dimension(n), intent(inout) :: idx
integer(kind = IPRE), intent(in) :: n, order
integer(kind = IPRE) :: left, right, marker
real(kind = RPRE) :: pivot, tmp
if (n .gt. 1) then
left = 0
right = n + 1
pivot = x(randi(n))
select case(order)
case(1)
do while ( left .lt. right )
left = left + 1
right = right - 1
do while ( x(left) .lt. pivot )
left = left + 1
end do
do while ( x(right) .gt. pivot )
right = right - 1
end do
if ( left .lt. right ) then
tmp = x(left)
x(left) = x(right)
x(right) = tmp
tmp = idx(left)
idx(left) = idx(right)
idx(right) = tmp
end if
end do
case(2)
do while ( left .lt. right )
left = left + 1
right = right - 1
do while ( x(left) .gt. pivot )
left = left + 1
end do
do while ( x(right) .lt. pivot )
right = right - 1
end do
if ( left .lt. right ) then
tmp = x(left)
x(left) = x(right)
x(right) = tmp
tmp = idx(left)
idx(left) = idx(right)
idx(right) = tmp
end if
end do
end select
if ( left .eq. right ) then
marker = left + 1
else
marker = left
end if
call quickargsort(x(:marker-1), idx(:marker-1), marker-1, order)
call quickargsort(x(marker:), idx(marker:), n-marker+1, order)
end if
return
end subroutine quickargsort
end function argsort
!=======================================================================
! asind
!-----------------------------------------------------------------------
! asind computes the inverse sine in degrees.
!
! Syntax
!-----------------------------------------------------------------------
! y = asind(x)
!
! Description
!-----------------------------------------------------------------------
! y = asind(x) returns the inverse sine of the elements in x in degrees.
! For real elements of x in the domain [-1,1], asind returns values in
! the range [-90,90]. For values of x outside this range, asind returns
! NaN (Not a Number).
!
! Examples
!-----------------------------------------------------------------------
! y = asind(1.)
! 90.
!
! y = asind(2.)
! NaN