Line | Branch | Exec | Source |
---|---|---|---|
1 | module raffle__misc_maths | ||
2 | !! Module for miscellaneous mathematical functions. | ||
3 | use raffle__io_utils, only: stop_program | ||
4 | use raffle__constants, only: real32 | ||
5 | implicit none | ||
6 | |||
7 | |||
8 | private | ||
9 | |||
10 | public :: lnsum, triangular_number, set_difference | ||
11 | |||
12 | |||
13 | |||
14 | contains | ||
15 | |||
16 | !############################################################################### | ||
17 | 1 | function lnsum(n) | |
18 | !! Return the sum of the logs of the integers from 1 to n. | ||
19 | implicit none | ||
20 | |||
21 | ! Arguments | ||
22 | integer :: n | ||
23 | !! The upper limit of the range. | ||
24 | real(real32) :: lnsum | ||
25 | !! The sum of the logs of the integers from 1 to n. | ||
26 | |||
27 | ! Local variables | ||
28 | integer :: i | ||
29 | !! Loop index. | ||
30 | |||
31 | 1 | lnsum = 0._real32 | |
32 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
|
6 | do i = 1, n |
33 | 6 | lnsum = lnsum + log( real(i, real32) ) | |
34 | end do | ||
35 | |||
36 | 1 | return | |
37 | end function lnsum | ||
38 | !############################################################################### | ||
39 | |||
40 | |||
41 | !############################################################################### | ||
42 | 361 | integer function triangular_number(n) | |
43 | !! Return the nth triangular number. | ||
44 | implicit none | ||
45 | |||
46 | ! Arguments | ||
47 | integer :: n | ||
48 | !! The index of the triangular number to return. | ||
49 | 361 | triangular_number = n * ( n + 1 ) / 2 | |
50 | 361 | end function triangular_number | |
51 | !############################################################################### | ||
52 | |||
53 | |||
54 | !############################################################################### | ||
55 |
3/6✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 78 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 78 times.
✗ Branch 5 not taken.
|
78 | function set_difference(a, b, set_min_zero) |
56 | !! Return the set difference of two arrays. | ||
57 | implicit none | ||
58 | |||
59 | ! Arguments | ||
60 | real(real32), dimension(:), intent(in) :: a | ||
61 | !! The first array. | ||
62 | real(real32), dimension(:), intent(in) :: b | ||
63 | !! The second array. | ||
64 | logical, optional :: set_min_zero | ||
65 | !! Boolean to set the maximum value of the output array to zero. | ||
66 | real(real32), dimension(size(a)) :: set_difference | ||
67 | !! The set difference of the two arrays. | ||
68 | |||
69 | ! Local variables | ||
70 | integer :: i | ||
71 | !! Loop indices. | ||
72 | logical :: set_min_zero_ | ||
73 | !! Boolean to set all values below zero to zero. | ||
74 | |||
75 | |||
76 |
2/2✓ Branch 0 taken 76 times.
✓ Branch 1 taken 2 times.
|
78 | if(present(set_min_zero)) then |
77 | 76 | set_min_zero_ = set_min_zero | |
78 | else | ||
79 | 2 | set_min_zero_ = .false. | |
80 | end if | ||
81 | |||
82 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 77 times.
|
78 | if(size(a,1) .ne. size(b,1)) then |
83 | 1 | call stop_program('Arrays must be the same size.') | |
84 | 1 | return | |
85 | end if | ||
86 | |||
87 |
2/2✓ Branch 0 taken 76 times.
✓ Branch 1 taken 1 times.
|
77 | if(set_min_zero_)then |
88 |
2/2✓ Branch 0 taken 10490 times.
✓ Branch 1 taken 76 times.
|
10566 | do i = 1, size(a,1) |
89 | 10566 | set_difference(i) = max(0.0_real32, a(i) - b(i)) | |
90 | end do | ||
91 | else | ||
92 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
|
4 | set_difference = a - b |
93 | end if | ||
94 | |||
95 | end function set_difference | ||
96 | !############################################################################### | ||
97 | |||
98 | end module raffle__misc_maths | ||
99 |