| 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 | 579 | pure function triangular_number(n) result(output) | |
| 43 | !! Return the nth triangular number. | ||
| 44 | implicit none | ||
| 45 | |||
| 46 | ! Arguments | ||
| 47 | integer, intent(in) :: n | ||
| 48 | !! The index of the triangular number to return. | ||
| 49 | |||
| 50 | integer :: output | ||
| 51 | !! The nth triangular number. | ||
| 52 | |||
| 53 | 579 | output = n * ( n + 1 ) / 2 | |
| 54 | 579 | end function triangular_number | |
| 55 | !############################################################################### | ||
| 56 | |||
| 57 | |||
| 58 | !############################################################################### | ||
| 59 |
3/6✓ Branch 0 taken 125 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 125 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 125 times.
✗ Branch 5 not taken.
|
125 | function set_difference(a, b, set_min_zero) |
| 60 | !! Return the set difference of two arrays. | ||
| 61 | implicit none | ||
| 62 | |||
| 63 | ! Arguments | ||
| 64 | real(real32), dimension(:), intent(in) :: a | ||
| 65 | !! The first array. | ||
| 66 | real(real32), dimension(:), intent(in) :: b | ||
| 67 | !! The second array. | ||
| 68 | logical, optional :: set_min_zero | ||
| 69 | !! Boolean to set the maximum value of the output array to zero. | ||
| 70 | real(real32), dimension(size(a)) :: set_difference | ||
| 71 | !! The set difference of the two arrays. | ||
| 72 | |||
| 73 | ! Local variables | ||
| 74 | integer :: i | ||
| 75 | !! Loop indices. | ||
| 76 | logical :: set_min_zero_ | ||
| 77 | !! Boolean to set all values below zero to zero. | ||
| 78 | |||
| 79 | |||
| 80 |
2/2✓ Branch 0 taken 123 times.
✓ Branch 1 taken 2 times.
|
125 | if(present(set_min_zero)) then |
| 81 | 123 | set_min_zero_ = set_min_zero | |
| 82 | else | ||
| 83 | 2 | set_min_zero_ = .false. | |
| 84 | end if | ||
| 85 | |||
| 86 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 124 times.
|
125 | if(size(a,1) .ne. size(b,1)) then |
| 87 | 1 | call stop_program('Arrays must be the same size.') | |
| 88 | 1 | return | |
| 89 | end if | ||
| 90 | |||
| 91 |
2/2✓ Branch 0 taken 123 times.
✓ Branch 1 taken 1 times.
|
124 | if(set_min_zero_)then |
| 92 |
2/2✓ Branch 0 taken 14877 times.
✓ Branch 1 taken 123 times.
|
15000 | do i = 1, size(a,1) |
| 93 | 15000 | set_difference(i) = max(0.0_real32, a(i) - b(i)) | |
| 94 | end do | ||
| 95 | else | ||
| 96 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
|
4 | set_difference = a - b |
| 97 | end if | ||
| 98 | |||
| 99 | end function set_difference | ||
| 100 | !############################################################################### | ||
| 101 | |||
| 102 | end module raffle__misc_maths | ||
| 103 |