-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfmath4.f95
More file actions
1434 lines (1141 loc) · 45.3 KB
/
fmath4.f95
File metadata and controls
1434 lines (1141 loc) · 45.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!> =============================================================================
!>
!> fmath4.f95 - Support and general math functions and subroutines.
!>
!> =============================================================================
!>
!> Copyright (C) 2018 - 2020 Pablo Edronkin (pablo.edronkin at yahoo.com)
!>
!> This program is free software: you can redistribute it and/or modify
!> it under the terms of the GNU Lesser General Public License as published
!> by the Free Software Foundation, either version 3 of the License, or
!> (at your option) any later version.
!>
!> This program is distributed in the hope that it will be useful,
!> but WITHOUT ANY WARRANTY; without even the implied warranty of
!> MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
!> GNU Lesser General Public License for more details.
!>
!> You should have received a copy of the GNU Lesser General Public License
!> along with this program. If not, see <https://www.gnu.org/licenses/>.
!>
!> =============================================================================
!>
!> Compilation:
!> - ./cfmath.sh or
!> - mpif90 -std='gnu' -c fmath4.f95 -O3 -march=native -Wall -Wextra -fopenmp
!>
module fmath4
implicit none
! Setting a platform-independent floating point precision.
integer, parameter :: fmath4_p1 = selected_real_kind( 10,300 )
!------------------------------------------------------------------------------
contains
!------------------------------------------------------------------------------
! Misc functions.
!> Draws a simple line in the console.
!>
subroutine RspFLine()
implicit none
write(*,*) "--------------------------------------------------------------"
end subroutine
!> "Not implemented yet" place holder. Puts a standard message on console.
!>
subroutine RspFNiy()
implicit none
call RspFComment( 'Test not implemented yet...' )
read(*,*)
end subroutine
!> Writes a comment on console.
!>
!> Arguments:
!> - p_t: text to be written.
!>
subroutine RspFComment( p_t )
implicit none
character( len = * ) :: p_t
write(*,*) p_t
write(*,*)
end subroutine
!> Places a comment and asks for [enter].
!>
!> Arguments:
!> - p_t: text to be written.
!>
subroutine RspFCommentEn( p_t )
implicit none
character( len = * ) :: p_t
call RspFComment( p_t )
call RspfComment( "Press [enter] to continue..." )
read(*,*)
end subroutine
!------------------------------------------------------------------------------
! Math functions.
!> Calculates the nth root of a number.
!>
!> Arguments:
!> - p_n:
!> - p_r:
!>
!> Output:
!> - The nth root of a number (p_n**(/p_r)).
!>
pure real( kind = fmath4_p1 ) function RspFNthRoot( p_n, p_r )
implicit none
real( kind = fmath4_p1 ), intent(in) :: p_n, p_r
RspFNthRoot = p_n**( 1.0 / p_r )
end function
!> Calculates the radius of the nth circle of a Pappus chain, given that the radius of the
!> first circle r = ac / ab.
!>
!> Arguments:
!> - p_ac: diameter of cicle 2, being a the point on which the inversion is centered.
!> - p_ab: diameter of the first circle.
!> - p_n: nth order.
!>
!> Output:
!> - Radius of nth circle.
!>
!> Sources:
!> - Adapted from rosettacode.org by Pablo Edronkin.
!> - https://en.wikipedia.org/wiki/Pappus_chain
!> - https://en.wikipedia.org/wiki/Arbelos
!>
real( kind = fmath4_p1 ) function RspFPappusChainRadiiGenerator( p_ac, p_ab, p_n )
implicit none
real( kind = fmath4_p1 ) :: p_ac, p_ab, p_n, r
r = p_ac / p_ab
RspFPappusChainRadiiGenerator = ( (1 - r) * r ) / ( 2 * ( p_n**2 * ( (1 - r)**2 ) + r ) )
return
end function
!> Generates a Mersenne number M = 2^p_pow - 1.
!>
!> Arguments:
!> - p_pow: power; should be <= 1023.
!>
!> Output:
!> - Mersenne number.
!>
!> Sources:
!> - https://en.wikipedia.org/wiki/Mersenne_prime
!>
real( kind = fmath4_p1 ) function RspFMersenneNumberGenerator( p_pow )
implicit none
real( kind = fmath4_p1 ) :: p_pow
RspFMersenneNumberGenerator = 2.0**p_pow - 1
return
end function
!> Maps p_x to the interval defined by [p_minr, p_maxr] for data in the range [p_mind, p_maxd].
!>
!> Arguments:
!> - p_x: value to normalize.
!> - p_minr: lower threshold of the range interval.
!> - p_maxr: higher threshold of the range interval.
!> - p_mind: lower threshold of the data range.
!> - p_maxd: higher threshold of the data range.
!>
!> Output:
!> - Normalized value.
!>
!> Sources:
!> - https://www.youtube.com/watch?v=SrjX2cjM3Es&list=PLea0WJq13cnDXK34DuEHacBR3rgAVJZkZ
!>
real( kind = fmath4_p1 ) function RspFNormalize2MinMaxInterval( p_x , p_minr, p_maxr, p_mind, p_maxd )
implicit none
real( kind = fmath4_p1 ) :: p_x , p_minr, p_maxr, p_mind, p_maxd
RspFNormalize2MinMaxInterval = ( ( ( p_x - p_mind ) * ( p_maxr - p_minr ) ) / ( p_maxd- p_mind ) ) + p_minr
return
end function
!> Computes growth according to the Solow - Swan economic growth model at a given time instance.
!>
!> Arguments:
!> - p_l: labour.
!> - p_k: capital
!> - p_a: technology
!> - p_e: elasticity, ( 0 < a < 1 ).
!>
!> Output:
!> - Growth.
!>
real( kind = fmath4_p1 ) function RspFSolowSwanModel( p_l, p_k, p_a, p_e )
implicit none
real( kind = fmath4_p1 ) :: p_l, p_k, p_a, p_e
RspFSolowSwanModel = ( p_k**p_e ) * ( ( p_a * p_l )**( 1 - p_e ) )
return
end function
!> Denormalizes p_y from a mapped interval defined by [p_minr, p_maxr] to a data range
!> defined by range [p_mind, p_maxd].
!>
!> Arguments:
!> - p_x: value to denormalize.
!> - p_minr: lower threshold of the range interval.
!> - p_maxr: higher threshold of the range interval.
!> - p_mind: lower threshold of the data range.
!> - p_maxd: higher threshold of the data range.
!>
!> Output:
!> - Denormalized value.
!>
!> Sources:
!> - Adapted from rosettacode.org by Pablo Edronkin.
!> - See RspFNormalize2MinMaxInterval(...).
!>
!> NEEDS TESTING.
!>
real( kind = fmath4_p1 ) function RspFDenormalizeFromMinMaxInterval( p_y , p_minr, p_maxr, p_mind, p_maxd )
implicit none
real( kind = fmath4_p1 ) :: p_y , p_minr, p_maxr, p_mind, p_maxd
RspFDenormalizeFromMinMaxInterval = ( ( ( p_y - p_minr ) * ( p_maxd- p_mind ) ) / ( p_maxr - p_minr ) ) + p_mind
return
end function
!> Calulates Z according to the Euler-Riemann Zeta function; bare-bones implementation.
!>
!> Arguments:
!> - p_s: Riemann s value.
!> - p_i: iterations
!>
!> Output:
!> - Z value.
!>
!> Sources:
!> - Adapted from rosettacode.org by Pablo Edronkin.
!> - https://en.wikipedia.org/wiki/Riemann_zeta_function
!> - http://mathworld.wolfram.com/RiemannZetaFunction.html
!>
!> NEEDS TESTING AND CONVERSION TO COMPLEX NUMBERS.
!>
real( kind = fmath4_p1 ) function RspFZeta( p_s, p_i )
implicit none
real( kind = fmath4_p1 ) :: p_s, z, x
integer :: p_i, n
x = 0.0
z = 0.0
if ( (p_s - (-1) < epsilon(x) ) .or. (p_s - (-13) < epsilon(x) ) ) then
z = -1/12
else if ( p_s - 0.0 < epsilon(x) ) then
z = -1/2
else if ( p_s - 1/2 < epsilon(x) ) then
z = -1.4603545;
else if ( p_s - 2.0 < epsilon(x) ) then
z = 1.645
else if ( p_s - 3.0 < epsilon(x) ) then
z = 1.202
else if ( p_s - 4.0 < epsilon(x) ) then
z = 1.0823
else
n = 1
do n = 1, p_i
z = z + ( n**(-p_s) );
end do
end if
RspFZeta = z
return
end function
!> If condition p_cond is fulfilled, returns p_num3, otherwise returns p_num4.
!>
!> Arguments:
!> - p_cond: condition that should be fulfilled, wwritten as a string:
!> - "==" equal.
!> - "<" less than.
!> - ">" greater than.
!> - "<=" less or equal than.
!> - ">=" greater or equal than.
!> - "abs" compares the equality of both absolute values.
!> - "opp" oposite numbers.
!> - "recip" reciprocal numbers.
!> - "++" both positive.
!> - "--" both negative.
!> - "00" both zero.
!> - "+-" first positive and second negative.
!> - "-+" first negative and second positive.
!> - "0+" first zero, second positive.
!> - "+0" first positive, second zero.
!> - "0-" first zero, second negative.
!> - "-0" first negative, second zero.
!> - p_num1: number to be compared.
!> - p_num2: number that the function should not return.
!> - p_num3: result returned if p_num1 = p_num2.
!> - p_num4: result if p_num1 != p_num2
!>
!> Output:
!> - Either p_num3 or p_num4.
!>
pure real( kind = fmath4_p1 ) function RspFRoicf( p_cond, p_num1, p_num2, p_num3, p_num4 )
implicit none
real( kind = fmath4_p1 ) :: x
real( kind = fmath4_p1 ), intent(in) :: p_num1, p_num2, p_num3, p_num4
character, intent(in) :: p_cond
logical :: cond_f
cond_f = .false.
x = 0.0
! Equal.
if( p_cond == "==" ) then
if ( p_num1 - p_num2 < epsilon(x) ) then
cond_f = .true.
end if
end if
! Less than.
if( p_cond == "<" ) then
if ( p_num2 - p_num1 > epsilon(x) ) then
cond_f = .true.
end if
end if
! Greater than.
if( p_cond == ">" ) then
if ( p_num1 - p_num2 > epsilon(x) ) then
cond_f = .true.
end if
end if
! Less or equal than.
if( p_cond == "<=" ) then
if ( p_num2 - p_num1 >= epsilon(x) ) then
cond_f = .true.
end if
end if
! Greater or equal than.
if( p_cond == ">=" ) then
if ( p_num1 - p_num2 >= epsilon(x) ) then
cond_f = .true.
end if
end if
! Have the same absolute value.
if( p_cond == "abs" ) then
if ( abs( p_num1 ) - abs( p_num2 ) < epsilon(x) ) then
cond_f = .true.
end if
end if
! Opposite numbers.
if( p_cond == "opp" ) then
if ( p_num1 - ( ( -1.0) * p_num2 ) < epsilon(x) ) then
cond_f = .true.
end if
end if
! Recip.
if( p_cond == "recip" ) then
if ( ( p_num1 * ( 1 / p_num2 ) ) < ( 1 + epsilon(x) ) ) then
cond_f = .true.
end if
end if
! Both positive.
if( p_cond == "++" ) then
if ( ( p_num1 > 0 + epsilon(x) ) .and. ( p_num2 > 0 + epsilon(x) ) ) then
cond_f = .true.
end if
end if
! Both negative.
if( p_cond == "--" ) then
if ( ( p_num1 < 0 - epsilon(x) ) .and. ( p_num2 < 0 - epsilon(x) ) ) then
cond_f = .true.
end if
end if
! Both zero.
if( p_cond == "00" ) then
if ( ( abs(p_num1) < 0 + epsilon(x) ) .and. ( abs(p_num2) < 0 + epsilon(x) ) ) then
cond_f = .true.
end if
end if
! First zero, second positive.
if( p_cond == "0+" ) then
if ( ( abs(p_num1) < 0 + epsilon(x) ) .and. ( abs(p_num2) > 0 + epsilon(x) ) ) then
cond_f = .true.
end if
end if
! First zero, second negative.
if( p_cond == "0-" ) then
if ( ( abs(p_num1) < 0 + epsilon(x) ) .and. ( p_num2 < 0 - epsilon(x) ) ) then
cond_f = .true.
end if
end if
! First positive, second zero.
if( p_cond == "+0" ) then
if ( ( p_num1 > 0 + epsilon(x) ) .and. ( abs(p_num2) < 0 + epsilon(x) ) ) then
cond_f = .true.
end if
end if
! First negative, second zero.
if( p_cond == "-0" ) then
if ( ( p_num1 < 0 - epsilon(x) ) .and. ( abs(p_num2) < 0 + epsilon(x) ) ) then
cond_f = .true.
end if
end if
! Return.
if ( cond_f .eqv. .true. ) then
RspFRoicf = p_num3;
else
RspFRoicf = p_num4;
end if
return
end function
!> Put a value within a range.
!>
!> Arguments:
!> - p_min: lower limit of the range.
!> - p_max: higher limit of the range.
!> - p_val: value to be tested against the range.
!>
!> Output:
!> - Value within the range.
!>
!> Sources:
!> - http://dlib.net/dlib/algs.h.html#put_in_range
!>
pure real( kind = fmath4_p1 ) function RspFPutInRange( p_min, p_max, p_val )
implicit none
real( kind = fmath4_p1 ) :: res
real( kind = fmath4_p1 ), intent(in) :: p_min, p_max, p_val
res = p_val
res = RspFRoicf( "<", res, p_min, p_min, res )
res = RspFRoicf( ">", res, p_max, p_max, res )
RspFPutInRange = res
return
end function
!> Puts a value within a range if p_pir is 1.0.
!>
!> Arguments:
!> - p_pir: 1.0 to put in range, any other number otherwise.
!> - p_min: Lowlr limit of the range.
!> - p_max: higher limit of the range.
!> - p_w: value to be ranged.
!>
!> Output:
!> - Adapted from rosettacode.org by Pablo Edronkin.
!> - Value ranged if p_pir is 1, original value otherwise.
!>
pure real( kind = fmath4_p1 ) function RspFPir( p_pir, p_min, p_max, p_w )
implicit none
real( kind = fmath4_p1 ), intent(in) :: p_pir, p_min, p_max, p_w
if ( abs( p_pir ) - 1.0 < epsilon( abs( p_pir ) ) ) then
RspFPir = RspFPutInRange( p_min, p_max, p_w );
else
RspFPir = p_w;
end if
return
end function
!> Calculates the tolerance value of real rounding and comparisons on a given system.
!>
!> Arguments:
!> - p_x: value.
!>
!> Output:
!> - Epsilon value.
!>
!> Sources:
!> - Adapted from rosettacode.org by Pablo Edronkin.
!> - http://jules-lsm.github.io/coding_standards/guidelines/fp_arithmetic.html
!>
pure real( kind = fmath4_p1 ) function RspFTol( p_x )
implicit none
real( kind = fmath4_p1 ), intent(in) :: p_x
RspFTol = epsilon( p_x )
return
end function
!> Performs operation of p_op to all elements of array p_vec from element p_min to element p_max
!> successively and as a series. For example, in the case of a serial sum, the function will
!> yield p_vec(0) + p_vec(1) + ... p_vec(n).
!>
!> Arguments:
!> - p_cond: operation to be applied, as per the following list:
!> - "++": summation.
!> - "--": substraction.
!> - "**": multiplication.
!> - "//": division.
!> - "am": aritmetic mean.
!> - "gm": geometric mean.
!> - "hm": harmonic mean.
!> - p_vec: array of real numbers.
!> - p_vec_len: number of elements contained in p_vec.
!> - p_min: ordinal of the first element of the sub array to which p_op will be applied.
!> - p_max: ordinal of the last element of the sub array to which p_op will be applied.
!>
!> Output:
!> - The result of p_cond applied to the relevant elements of p_vec succesively; it returns 0.0 if p_cond is not recognized.
!>
!> Sources:
!> - Adapted from rosettacode.org by Pablo Edronkin.
!> - http://www.mathcs.emory.edu/~cheung/Courses/561/Syllabus/6-Fortran/array4.html
!> - https://en.wikipedia.org/wiki/Mean
!>
pure real( kind = fmath4_p1 ) function RspFSerialOp( p_cond, p_vec, p_vec_len, p_min, p_max )
implicit none
integer :: x1, x2
integer, intent(in) :: p_vec_len, p_min, p_max
real( kind = fmath4_p1 ) :: res, x3
real( kind = fmath4_p1 ), dimension(p_vec_len), intent(in) :: p_vec
character(2), intent(in) :: p_cond
x2 = (p_max + 1) - p_min
x3 = 1.00 / x2
if (p_cond == "**") then
res = 1.0
else if (p_cond == "//") then
res = 1.0
else if (p_cond == "gm") then
res = 1.0
else
res = 0.0
end if
do x1 = p_min, p_max
! Sum.
if ( p_cond == "++" ) then
res = res + p_vec(x1)
endif
! Arithmetic mean.
if (p_cond == "am") then
res = res + p_vec(x1)
endif
! Harmonic mean.
if (p_cond == "hm") then
res = res + ( 1 / p_vec(x1) )
endif
! Substraction.
if ( p_cond == "--" ) then
res = res - p_vec(x1)
endif
! Multiplication.
if ( p_cond == "**" ) then
res = res * p_vec(x1)
endif
! Geometric mean.
if ( p_cond == "gm" ) then
res = res * p_vec(x1)
endif
! Division.
if ( p_cond == "//" ) then
res = res / p_vec(x1)
endif
end do
! Mean calculation, aritmetic.
if (p_cond == "am") then
!res = res / ((p_max + 1) - p_min)
res = res / x2
end if
! Mean calculation, geometric.
if (p_cond == "gm") then
res = res ** x3
end if
! Mean calculation, harmonic.
if (p_cond == "hm") then
res = x2 / res
end if
!Result
RspFSerialOp = res
return
end function
!> Calculate the step size for numerical integration using equal step methods.
!>
!> Arguments:
!> p_steps: number of steps required.
!> p_ll: lower limit of the integration.
!> p_ulL upper limit of the integral.
!>
!> Output:
!> - Step size.
!>
!> Sources:
!> - MHJ_Ch7_integration.pdf
!> - http://www.bu.edu/tech/support/research/training-consulting/online-tutorials/mpi/example1-2/
!>
pure real( kind = fmath4_p1 ) function RspFStepSize( p_steps, p_ll, p_ul )
implicit none
real( kind = fmath4_p1 ), intent(in) :: p_steps, p_ll, p_ul
RspFStepSize = ( p_ul - p_ll ) / p_steps
return
end function
!> Create a vector with random numbers; normal distribution.
!>
!> Arguments:
!> - p_rows: number of elements that the vector shall have.
!>
!> Output:
!> - Real numbers between [0:1)
!>
function RspFRndDistNormal( p_rows )
implicit none
integer, intent(in) :: p_rows
real( kind = fmath4_p1 ), dimension(p_rows) :: RspFRndDistNormal
call random_number(RspFRndDistNormal)
return
end function
!> Create a vector with evenly-distributed numbers using quasi random generation.
!>
!> Arguments:
!> - p_rows: number of elements that the vector shall have.
!>
!> Output:
!> - Real numbers between [0:1]
!>
pure function RspFRndDistEven( p_rows )
implicit none
integer :: x1
integer, intent(in) :: p_rows
real( kind = fmath4_p1 ) :: n_sum, step, a
real( kind = fmath4_p1 ), dimension(p_rows) :: RspFRndDistEven
a = 1.0
step = a / p_rows
n_sum = 0
do x1 = 1, p_rows
RspFRndDistEven(x1) = n_sum
n_sum = n_sum + step
if (n_sum > 1.0) then
n_sum = 1.0
endif
end do
return
end function
!> Numeric integration using a variant of the Monte Carlo method. This version uses no pointers
!> and needs only a matrix of function results and a selected number of iterations with which
!> the function will test results for each input value of the results matrix provided. The
!> area of the functions is then calculated based on the number of tests made corresponding to
!> each x value that provide random y results below the y value for the actual function.
!>
!> Arguments:
!> - p_rm: random method used.
!> - "no": normal.
!> - "ev": even.
!> - p_rows: number of steps used to solve the integral (row length of p_xy).
!> - p_xy: 2D matrix of type p_xy(x,y) containing values for x and the corresponding y solved
!> using a user - defined integral f(x).
!> - p_iter: number of iterations.
!> - p_x1: min x value.
!> - p_x2: max x value.
!> - p_y1: min y value.
!> - p_y2: max y value.
!>
!> Output:
!> - Value of the integral defined between p_xy(1, 1) and p_xy(p_rows, 1).
!>
!> Sources:
!> - https://stackoverflow.com/questions/8612466/how-to-alias-a-function-name-in-fortran?noredirect=1&lq=1
!> - http://ocw.uci.edu/upload/files/mae10_w2011_lecture13.pdf
!> - https://en.wikipedia.org/wiki/Monte_Carlo_method
!> - https://en.wikipedia.org/wiki/Monte_Carlo_algorithm
!> - https://gcc.gnu.org/onlinedocs/gfortran/RANDOM_005fSEED.html
!>
!> NEEDS TESTING.
!>
real( kind = fmath4_p1 ) function RspFNumericIntegration1( p_rm, p_rows, p_xy, p_iter, p_x1, p_x2, p_y1, p_y2 )
implicit none
integer :: x1, x2, counted_y, total_y
integer, intent(in) :: p_rows, p_iter
real( kind = fmath4_p1 ) :: y_span, area_fun, xy(p_iter), a
real( kind = fmath4_p1 ), intent(in) :: p_x1, p_x2, p_y1, p_y2, p_xy(p_rows,2)
character(2), intent(in) :: p_rm
!Some initial stuff.
a = 1.0
counted_y = 0
total_y = p_iter * p_rows
y_span = p_y2 - p_y1
!Calculate random values for xy()
if ( p_rm .eq. "ev" ) then
xy = RspFRndDistEven( p_rows )
else
call random_seed()
xy = RspFRndDistNormal( p_rows )
!Put the random numbers within the range [p_y1:p_y2]
!do x1 = 1, p_iter
! xy(x1) = p_y1 + ( y_span * xy(x1) )
!end do
end if
!For each x value in p_xy, perform certain tasks.
do x1 = 1, p_rows
!Generate p_iter random y points.
do x2 = 1, p_iter
!For each random y point if it is greater than the y for current p_xy point, add one to counter.
if ( xy(x2) .le. p_xy(x1,2) ) then
counted_y = counted_y + 1
endif
end do
end do
!For each x in p_xy we just generated p_iter random numbers and compared then to the
!y value of p_xy element corresponding to x. A proportion of all those random y values
!was counted as higher or lowwer than the p_xy y value. Therefore we can calculate how many
!points lie within the area below the function curve and thus, estimate the integral.
!Area used by the function.
area_fun = ( counted_y / total_y ) * a
RspFNumericIntegration1 = area_fun * RspFAreaTot( p_x1, p_x2, p_y1, p_y2 )
return
end function
!> Numeric integration using the Newton-Cotes equal step quadrature method (trapezoidal rule);
!> This version uses no pointers.
!>
!> Arguments:
!> - p_rows: number of steps used to solve the integral (row length of p_xy).
!> - p_xy: 2D matrix of type p_xy(x,y) containing values for x and the corresponding y solved
!> using a user - defined integral f(x).
!> - x: defined from [1:p_rows], contains the x input for which the user-defined function
!> f was solved.
!> - y: contains the result of f(x)
!>
!> Output:
!> - Value of the integral defined between p_xy(1, 1) and p_xy(p_rows, 1).
!>
!> Sources:
!> - http://www.phy.ohiou.edu/~elster/phys5071/extras/MHJ_Ch7_integration.pdf
!> - http://www.bu.edu/tech/support/research/training-consulting/online-tutorials/mpi/example1-2/
!>
pure real( kind = fmath4_p1 ) function RspFNumericIntegration2( p_rows, p_xy )
implicit none
integer :: j
integer, intent(in) :: p_rows
real( kind = fmath4_p1 ), intent(in) :: p_xy( p_rows, 2)
real( kind = fmath4_p1 ) :: trap_sum, step, rows
rows = p_rows
step = RspFStepSize( rows, p_xy( 1, 1 ), p_xy( p_rows, 1 ) )
trap_sum = 0.0
do j = 1, p_rows
trap_sum = trap_sum + p_xy( j, 2 )
end do
RspFNumericIntegration2 = ( trap_sum + ( p_xy( 1, 2 ) / 2 ) + ( p_xy( p_rows, 2 ) / 2 ) ) * step
return
end function
!> Calculates the total area of a 2D rectangle given four coordinates that inscribe it.
!>
!> Arguments:
!> - p_x1: min x value.
!> - p_x2: max x value.
!> - p_y1: min y value.
!> - p_y2: max y value.
!>
!> Output:
!> - Area of the rectangle.
!>
pure real( kind = fmath4_p1 ) function RspFAreaTot( p_x1, p_x2, p_y1, p_y2 )
implicit none
real( kind = fmath4_p1 ), intent(in) :: p_x1, p_x2, p_y1, p_y2
RspFAreaTot = ( p_x2 - p_x1 ) * ( p_y2 - p_y1 )
return
end function
!> Finds if a point in a plane is within the rectangle defined by the coordinates passed
!> as arguments.
!>
!> Arguments:
!> - p_x1: min x value.
!> - p_x2: max x value.
!> - p_y1: min y value.
!> - p_y2: max y value.
!> - p_px: x coordinate of point.
!> - p_py: y coordinate of point.
!>
!> Output:
!> - 1 if point is inside rectangle, 0 otherwise.
!>
pure real( kind = fmath4_p1 ) function RspFIsInsideRectangle( p_x1, p_x2, p_y1, p_y2, p_px, p_py )
implicit none
real( kind = fmath4_p1 ), intent(in) :: p_x1, p_x2, p_y1, p_y2, p_px, p_py
if ( ( ( p_px <= p_x2 ) .and. ( p_px >= p_x1 ) ) .and. ( ( p_py <= p_y2 ) .and. ( p_py >= p_y1 ) ) ) then
RspFIsInsideRectangle = 1.0
else
RspFIsInsideRectangle = 0.0
endif
return
end function
!> Calculates the factorial of the absolute value of input number.
!>
!> Arguments:
!> - p_x1: number.
!>
!> Output:
!> - Area of the rectangle.
!>
pure recursive real( kind = fmath4_p1 ) function RspFFact( p_x1 ) result(res)
implicit none
real( kind = fmath4_p1 ), intent(in) :: p_x1
if ( abs(p_x1) <= 1 ) then
res = 1
else
res = p_x1 * RspFFact(p_x1 - 1)
end if
return
end function
!> Calculates the inverse exponent of a number using 1 / p_x1**p_x2.
!>
!> Arguments:
!> - p_x1: base.
!> - p_x2: exponent.
!>
!> Output:
!> - Area of the rectangle.
!>
pure real( kind = fmath4_p1 ) function RspFInvExp( p_x1, p_x2 )
implicit none
real( kind = fmath4_p1 ), intent(in) :: p_x1, p_x2
RspFInvExp = 1 / ( p_x1**p_x2 )
return
end function
!> Calculates exp(p_x1, p_x2) + p_x3.
!>
!> Arguments:
!> - p_x1: base.
!> - p_x2: exponent.
!> - p_x3: summand.
!>
pure real( kind = fmath4_p1 ) function RspFS1( p_x1, p_x2, p_x3 )
implicit none
real( kind = fmath4_p1 ), intent(in) :: p_x1, p_x2, p_x3
RspFS1 = p_x1**p_x2 + p_x3
return
end function
!> Performs p_x1**p_x2 [p_cond] p_x3**p_x4
!>
!> Arguments:
!> - p_cond: operation to be applied, as per the following list:
!> - "+": summation.
!> - "-": substraction.
!> - "*": multiplication.
!> - "/": division.
!> - p_x1: first operand.
!> - p_x2: second operand.
!> - p_x3: third operand.
!> - p_x4: fourth operand.
!>
!> Output:
!> - Res = p_x1**p_x2 [p_cond] p_x3**p_x4
!>
pure real( kind = fmath4_p1 ) function RspFS2( p_cond, p_x1, p_x2, p_x3, p_x4 )
implicit none