other/s_common.f90
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | submodule(m_common) s_common | ||
| 2 | contains | ||
| 3 | !> @brief Sort an array of integers using the quicksort algorithm | ||
| 4 | !> | ||
| 5 | !> @param [inout] array The array of integers to be sorted | ||
| 6 | !> @param [in] _(optional) inverse If true, sort in descending order; otherwise, ascending order (default: false) | ||
| 7 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 1006 times.
|
1006 | module pure subroutine sort_int(array, inverse) |
| 8 | implicit none | ||
| 9 | |||
| 10 | integer, intent(inout) :: array(:) | ||
| 11 | logical, intent(in), optional :: inverse | ||
| 12 | |||
| 13 | 1006 | call quicksort_int(array, 1, size(array)) | |
| 14 |
1/2✗ Branch 5 → 6 not taken.
✓ Branch 5 → 16 taken 1006 times.
|
1006 | if (present(inverse)) then |
| 15 | ✗ | if (inverse) then | |
| 16 | ✗ | array = array(size(array):1:-1) | |
| 17 | end if | ||
| 18 | end if | ||
| 19 | |||
| 20 | contains | ||
| 21 | |||
| 22 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 1894 times.
|
1894 | pure recursive subroutine quicksort_int(a, left, right) |
| 23 | integer, intent(inout) :: a(:) | ||
| 24 | integer, intent(in) :: left, right | ||
| 25 | integer :: i, j, pivot_idx, pivot, temp | ||
| 26 | |||
| 27 |
1/2✓ Branch 4 → 5 taken 1894 times.
✗ Branch 4 → 23 not taken.
|
1894 | if (left < right) then |
| 28 | 1894 | pivot_idx = left + (right - left) / 2 | |
| 29 | 1894 | pivot = a(pivot_idx) | |
| 30 | 1894 | i = left | |
| 31 | 1894 | j = right | |
| 32 | do | ||
| 33 |
2/2✓ Branch 7 → 8 taken 1920 times.
✓ Branch 7 → 10 taken 171 times.
|
2091 | do while (a(i) < pivot) |
| 34 | 2091 | i = i + 1 | |
| 35 | end do | ||
| 36 |
2/2✓ Branch 11 → 12 taken 1826 times.
✓ Branch 11 → 13 taken 1920 times.
|
3746 | do while (a(j) > pivot) |
| 37 | 1826 | j = j - 1 | |
| 38 | end do | ||
| 39 |
2/2✓ Branch 14 → 15 taken 1915 times.
✓ Branch 14 → 16 taken 5 times.
|
1920 | if (i <= j) then |
| 40 | temp = a(i) | ||
| 41 | 1915 | a(i) = a(j) | |
| 42 | 1915 | a(j) = temp | |
| 43 | 1915 | i = i + 1 | |
| 44 | 1915 | j = j - 1 | |
| 45 | end if | ||
| 46 |
2/2✓ Branch 16 → 6 taken 26 times.
✓ Branch 16 → 17 taken 1894 times.
|
1920 | if (i > j) exit |
| 47 | end do | ||
| 48 |
2/2✓ Branch 17 → 18 taken 43 times.
✓ Branch 17 → 20 taken 1851 times.
|
1894 | if (left < j) call quicksort_int(a, left, j) |
| 49 |
2/2✓ Branch 20 → 21 taken 845 times.
✓ Branch 20 → 23 taken 1049 times.
|
1894 | if (i < right) call quicksort_int(a, i, right) |
| 50 | end if | ||
| 51 | 1894 | end subroutine quicksort_int | |
| 52 | |||
| 53 | end subroutine sort_int | ||
| 54 | |||
| 55 | !> @brief Sort an array of real numbers using the quicksort algorithm | ||
| 56 | !> | ||
| 57 | !> @param [inout] array The array of real numbers to be sorted | ||
| 58 | !> @param [in] _(optional) inverse If true, sort in descending order; otherwise, ascending order (default: false) | ||
| 59 | !> @param [in] _(optional) sort_fun A function to determine the sorting order | ||
| 60 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 6 times.
|
6 | module subroutine sort_real(array, inverse, sort_fun) |
| 61 | implicit none | ||
| 62 | |||
| 63 | real(wp), intent(inout) :: array(:) | ||
| 64 | logical, intent(in), optional :: inverse | ||
| 65 | procedure(user_function_1d_interface), optional :: sort_fun | ||
| 66 | |||
| 67 | 6 | call quicksort_real(array, 1, size(array)) | |
| 68 |
2/2✓ Branch 5 → 6 taken 1 time.
✓ Branch 5 → 16 taken 5 times.
|
6 | if (present(inverse)) then |
| 69 |
1/2✓ Branch 6 → 7 taken 1 time.
✗ Branch 6 → 16 not taken.
|
1 | if (inverse) then |
| 70 |
5/6✓ Branch 7 → 8 taken 1 time.
✗ Branch 7 → 9 not taken.
✓ Branch 10 → 11 taken 1 time.
✓ Branch 10 → 12 taken 10 times.
✓ Branch 13 → 14 taken 10 times.
✓ Branch 13 → 15 taken 1 time.
|
22 | array = array(size(array):1:-1) |
| 71 | end if | ||
| 72 | end if | ||
| 73 | |||
| 74 | contains | ||
| 75 | |||
| 76 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 1155 times.
|
1155 | recursive subroutine quicksort_real(a, left, right) |
| 77 | real(wp), intent(inout) :: a(:) | ||
| 78 | integer, intent(in) :: left, right | ||
| 79 | integer :: i, j, pivot_idx | ||
| 80 | real(wp) :: pivot, temp | ||
| 81 | real(wp) :: val_i, val_j | ||
| 82 | |||
| 83 |
1/2✓ Branch 4 → 5 taken 1155 times.
✗ Branch 4 → 36 not taken.
|
1155 | if (left < right) then |
| 84 | 1155 | pivot_idx = left + (right - left) / 2 | |
| 85 |
2/2✓ Branch 5 → 6 taken 387 times.
✓ Branch 5 → 7 taken 768 times.
|
1155 | if (present(sort_fun)) then |
| 86 | 387 | pivot = sort_fun(a(pivot_idx)) | |
| 87 | else | ||
| 88 | 768 | pivot = a(pivot_idx) | |
| 89 | end if | ||
| 90 | 1155 | i = left | |
| 91 | 1155 | j = right | |
| 92 | do | ||
| 93 |
1/2✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 7088 times.
|
7088 | do while (i <= right) |
| 94 |
2/2✓ Branch 12 → 13 taken 2403 times.
✓ Branch 12 → 14 taken 4685 times.
|
7088 | if (present(sort_fun)) then |
| 95 | 2403 | val_i = sort_fun(a(i)) | |
| 96 | else | ||
| 97 | 4685 | val_i = a(i) | |
| 98 | end if | ||
| 99 |
2/2✓ Branch 15 → 16 taken 3246 times.
✓ Branch 15 → 18 taken 3842 times.
|
7088 | if (val_i >= pivot) exit |
| 100 | 7088 | i = i + 1 | |
| 101 | end do | ||
| 102 |
1/2✓ Branch 19 → 20 taken 7439 times.
✗ Branch 19 → 26 not taken.
|
7439 | do while (j >= left) |
| 103 |
2/2✓ Branch 20 → 21 taken 2522 times.
✓ Branch 20 → 22 taken 4917 times.
|
7439 | if (present(sort_fun)) then |
| 104 | 2522 | val_j = sort_fun(a(j)) | |
| 105 | else | ||
| 106 | 4917 | val_j = a(j) | |
| 107 | end if | ||
| 108 |
2/2✓ Branch 23 → 24 taken 3246 times.
✓ Branch 23 → 25 taken 4193 times.
|
7439 | if (val_j <= pivot) exit |
| 109 | 7439 | j = j - 1 | |
| 110 | end do | ||
| 111 |
2/2✓ Branch 27 → 28 taken 2980 times.
✓ Branch 27 → 29 taken 266 times.
|
3246 | if (i <= j) then |
| 112 | 2980 | temp = a(i) | |
| 113 | 2980 | a(i) = a(j) | |
| 114 | 2980 | a(j) = temp | |
| 115 | 2980 | i = i + 1 | |
| 116 | 2980 | j = j - 1 | |
| 117 | end if | ||
| 118 |
2/2✓ Branch 29 → 9 taken 2091 times.
✓ Branch 29 → 30 taken 1155 times.
|
3246 | if (i > j) exit |
| 119 | end do | ||
| 120 |
2/2✓ Branch 30 → 31 taken 576 times.
✓ Branch 30 → 33 taken 579 times.
|
1155 | if (left < j) call quicksort_real(a, left, j) |
| 121 |
2/2✓ Branch 33 → 34 taken 573 times.
✓ Branch 33 → 36 taken 582 times.
|
1155 | if (i < right) call quicksort_real(a, i, right) |
| 122 | end if | ||
| 123 | 1155 | end subroutine quicksort_real | |
| 124 | |||
| 125 | end subroutine sort_real | ||
| 126 | |||
| 127 | !> @brief Compute the union of two sets of integers | ||
| 128 | !> | ||
| 129 | !> @param [out] union The resulting union of the two sets | ||
| 130 | !> @param [in] set1 The first set of integers | ||
| 131 | !> @param [in] set2 The second set of integers | ||
| 132 |
2/4✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 1 time.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 1 time.
|
1 | module subroutine set_union(union, set1, set2) |
| 133 | implicit none | ||
| 134 | |||
| 135 | integer, allocatable, intent(out) :: union(:) | ||
| 136 | integer, intent(in) :: set1(0:), set2(0:) | ||
| 137 | |||
| 138 | 1 | integer, allocatable :: temp(:) | |
| 139 | integer :: nr_duplicates, i | ||
| 140 | |||
| 141 | ! 1: append, 2: sort, 3: remove duplicates | ||
| 142 | |||
| 143 |
1/4✗ Branch 6 → 7 not taken.
✓ Branch 6 → 13 taken 1 time.
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 13 not taken.
|
1 | if (size(set1) == 0 .and. size(set2) == 0) then |
| 144 | ✗ | allocate (union(0:-1)) | |
| 145 | return | ||
| 146 | end if | ||
| 147 | |||
| 148 |
6/12✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 1 time.
✓ Branch 15 → 14 taken 1 time.
✗ Branch 15 → 16 not taken.
✓ Branch 16 → 17 taken 1 time.
✗ Branch 16 → 18 not taken.
✓ Branch 18 → 19 taken 1 time.
✗ Branch 18 → 20 not taken.
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 1 time.
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 24 taken 1 time.
|
3 | allocate (temp(0:size(set1) + size(set2) - 1)) |
| 149 |
2/2✓ Branch 25 → 26 taken 6 times.
✓ Branch 25 → 27 taken 1 time.
|
7 | temp(0:size(set1) - 1) = set1 |
| 150 |
2/2✓ Branch 28 → 29 taken 6 times.
✓ Branch 28 → 30 taken 1 time.
|
7 | temp(size(set1):size(set1) + size(set2) - 1) = set2 |
| 151 | |||
| 152 | 1 | call sort(temp) | |
| 153 | |||
| 154 | nr_duplicates = 0 | ||
| 155 |
2/2✓ Branch 33 → 34 taken 11 times.
✓ Branch 33 → 36 taken 1 time.
|
12 | do i = 0, size(temp) - 2 |
| 156 |
2/2✓ Branch 34 → 32 taken 8 times.
✓ Branch 34 → 35 taken 3 times.
|
12 | if (temp(i) == temp(i + 1)) then |
| 157 | 3 | nr_duplicates = nr_duplicates + 1 | |
| 158 | end if | ||
| 159 | end do | ||
| 160 | |||
| 161 |
7/14✗ Branch 37 → 38 not taken.
✓ Branch 37 → 39 taken 1 time.
✓ Branch 39 → 38 taken 1 time.
✗ Branch 39 → 40 not taken.
✓ Branch 40 → 41 taken 1 time.
✗ Branch 40 → 42 not taken.
✓ Branch 42 → 43 taken 1 time.
✗ Branch 42 → 44 not taken.
✗ Branch 44 → 45 not taken.
✓ Branch 44 → 46 taken 1 time.
✗ Branch 46 → 47 not taken.
✓ Branch 46 → 48 taken 1 time.
✗ Branch 48 → 49 not taken.
✓ Branch 48 → 50 taken 1 time.
|
3 | allocate (union(0:size(temp) - nr_duplicates - 1)) |
| 162 | |||
| 163 | nr_duplicates = 0 | ||
| 164 | 1 | union(0) = temp(0) | |
| 165 |
2/2✓ Branch 51 → 52 taken 11 times.
✓ Branch 51 → 56 taken 1 time.
|
12 | do i = 1, size(temp) - 1 |
| 166 |
2/2✓ Branch 52 → 53 taken 3 times.
✓ Branch 52 → 54 taken 8 times.
|
12 | if (temp(i - 1) == temp(i)) then |
| 167 | 3 | nr_duplicates = nr_duplicates + 1 | |
| 168 | else | ||
| 169 | 8 | union(i - nr_duplicates) = temp(i) | |
| 170 | end if | ||
| 171 | end do | ||
| 172 | |||
| 173 | 1 | deallocate (temp) | |
| 174 | end subroutine set_union | ||
| 175 | |||
| 176 | 11 | module pure subroutine prime_factors(n, factors) | |
| 177 | implicit none | ||
| 178 | |||
| 179 | integer, intent(in) :: n | ||
| 180 | integer, allocatable, intent(out) :: factors(:) | ||
| 181 | |||
| 182 | integer :: num, divisor, count, i | ||
| 183 | |||
| 184 | ! Handle edge cases | ||
| 185 |
2/2✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 8 taken 9 times.
|
11 | if (n <= 1) then |
| 186 |
2/4✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 2 times.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 2 times.
|
2 | allocate (factors(0)) |
| 187 | 2 | return | |
| 188 | end if | ||
| 189 | |||
| 190 | ! Count the number of prime factors | ||
| 191 | num = n | ||
| 192 | count = 0 | ||
| 193 | |||
| 194 | ! Count factors of 2 | ||
| 195 |
2/2✓ Branch 8 → 9 taken 16 times.
✓ Branch 8 → 10 taken 9 times.
|
25 | do while (mod(num, 2) == 0) |
| 196 | 16 | count = count + 1 | |
| 197 | 16 | num = num / 2 | |
| 198 | end do | ||
| 199 | |||
| 200 | ! Count odd factors | ||
| 201 | divisor = 3 | ||
| 202 |
2/2✓ Branch 11 → 12 taken 8 times.
✓ Branch 11 → 16 taken 9 times.
|
17 | do while (divisor * divisor <= num) |
| 203 |
2/2✓ Branch 12 → 13 taken 5 times.
✓ Branch 12 → 14 taken 8 times.
|
13 | do while (mod(num, divisor) == 0) |
| 204 | 5 | count = count + 1 | |
| 205 | 5 | num = num / divisor | |
| 206 | end do | ||
| 207 | 8 | divisor = divisor + 2 | |
| 208 | end do | ||
| 209 | |||
| 210 | ! If num > 1, then it's a prime factor | ||
| 211 |
2/2✓ Branch 17 → 18 taken 5 times.
✓ Branch 17 → 19 taken 4 times.
|
9 | if (num > 1) then |
| 212 | 5 | count = count + 1 | |
| 213 | end if | ||
| 214 | |||
| 215 | ! Allocate the array | ||
| 216 |
7/14✗ Branch 19 → 20 not taken.
✓ Branch 19 → 21 taken 9 times.
✓ Branch 21 → 20 taken 9 times.
✗ Branch 21 → 22 not taken.
✓ Branch 22 → 23 taken 9 times.
✗ Branch 22 → 24 not taken.
✓ Branch 24 → 25 taken 9 times.
✗ Branch 24 → 26 not taken.
✗ Branch 26 → 27 not taken.
✓ Branch 26 → 28 taken 9 times.
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 9 times.
✗ Branch 30 → 31 not taken.
✓ Branch 30 → 32 taken 9 times.
|
27 | allocate (factors(count)) |
| 217 | |||
| 218 | ! Fill the array with prime factors | ||
| 219 | num = n | ||
| 220 | i = 1 | ||
| 221 | |||
| 222 | ! Extract factors of 2 | ||
| 223 |
2/2✓ Branch 33 → 34 taken 16 times.
✓ Branch 33 → 35 taken 9 times.
|
25 | do while (mod(num, 2) == 0) |
| 224 | 16 | factors(i) = 2 | |
| 225 | 16 | i = i + 1 | |
| 226 | 16 | num = num / 2 | |
| 227 | end do | ||
| 228 | |||
| 229 | ! Extract odd factors | ||
| 230 | divisor = 3 | ||
| 231 |
2/2✓ Branch 36 → 37 taken 8 times.
✓ Branch 36 → 41 taken 9 times.
|
17 | do while (divisor * divisor <= num) |
| 232 |
2/2✓ Branch 37 → 38 taken 5 times.
✓ Branch 37 → 39 taken 8 times.
|
13 | do while (mod(num, divisor) == 0) |
| 233 | 5 | factors(i) = divisor | |
| 234 | 5 | i = i + 1 | |
| 235 | 5 | num = num / divisor | |
| 236 | end do | ||
| 237 | 8 | divisor = divisor + 2 | |
| 238 | end do | ||
| 239 | |||
| 240 | ! If num > 1, then it's a prime factor | ||
| 241 |
2/2✓ Branch 42 → 43 taken 5 times.
✓ Branch 42 → 44 taken 4 times.
|
9 | if (num > 1) then |
| 242 | 5 | factors(i) = num | |
| 243 | end if | ||
| 244 | |||
| 245 | end subroutine prime_factors | ||
| 246 | |||
| 247 | !> @brief Split an integer into two factors that are as close as possible | ||
| 248 | !> | ||
| 249 | !> Given an integer N, find n1 and n2 such that N = n1 * n2 and |n1 - n2| is minimized. | ||
| 250 | !> The result satisfies n1 <= n2. | ||
| 251 | !> | ||
| 252 | !> @param[in] n The integer to split | ||
| 253 | !> @param[out] n1 The smaller factor | ||
| 254 | !> @param[out] n2 The larger factor | ||
| 255 | 3835 | module pure subroutine balanced_split_2(n, n1, n2) | |
| 256 | implicit none | ||
| 257 | |||
| 258 | integer, intent(in) :: n | ||
| 259 | integer, intent(out) :: n1, n2 | ||
| 260 | |||
| 261 | integer :: i, sqrt_n | ||
| 262 | |||
| 263 | ! Handle edge cases | ||
| 264 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 3835 times.
|
3835 | if (n <= 0) then |
| 265 | ✗ | n1 = 1 | |
| 266 | ✗ | n2 = n | |
| 267 | ✗ | return | |
| 268 | end if | ||
| 269 | |||
| 270 |
2/2✓ Branch 4 → 5 taken 3 times.
✓ Branch 4 → 6 taken 3832 times.
|
3835 | if (n == 1) then |
| 271 | 3 | n1 = 1 | |
| 272 | 3 | n2 = 1 | |
| 273 | 3 | return | |
| 274 | end if | ||
| 275 | |||
| 276 | ! Start from sqrt(n) and work downward to find the largest divisor <= sqrt(n) | ||
| 277 | 3832 | sqrt_n = int(sqrt(real(n))) | |
| 278 | |||
| 279 |
1/2✓ Branch 7 → 8 taken 35439 times.
✗ Branch 7 → 11 not taken.
|
35439 | do i = sqrt_n, 1, -1 |
| 280 |
2/2✓ Branch 8 → 9 taken 3832 times.
✓ Branch 8 → 10 taken 31607 times.
|
35439 | if (mod(n, i) == 0) then |
| 281 | 3832 | n1 = i | |
| 282 | 3832 | n2 = n / i | |
| 283 | 3832 | return | |
| 284 | end if | ||
| 285 | end do | ||
| 286 | |||
| 287 | ! Should never reach here, but as a fallback | ||
| 288 | ✗ | n1 = 1 | |
| 289 | ✗ | n2 = n | |
| 290 | |||
| 291 | end subroutine balanced_split_2 | ||
| 292 | |||
| 293 | !> @brief Split an integer into three factors that are as close as possible | ||
| 294 | !> | ||
| 295 | !> Given an integer N, find n1, n2, and n3 such that N = n1 * n2 * n3 and the factors are as balanced as possible. | ||
| 296 | !> The result satisfies n1 <= n2 <= n3. | ||
| 297 | !> | ||
| 298 | !> @param[in] n The integer to split | ||
| 299 | !> @param[out] n1 The smallest factor | ||
| 300 | !> @param[out] n2 The middle factor | ||
| 301 | !> @param[out] n3 The largest factor | ||
| 302 | 1006 | module pure subroutine balanced_split_3(n, n1, n2, n3) | |
| 303 | implicit none | ||
| 304 | |||
| 305 | integer, intent(in) :: n | ||
| 306 | integer, intent(out) :: n1, n2, n3 | ||
| 307 | |||
| 308 | integer :: i, cbrt_n, temp_n2, temp_n3 | ||
| 309 | integer :: best_n1, best_n2, best_n3 | ||
| 310 | integer :: max_diff, current_max_diff | ||
| 311 | |||
| 312 | integer :: nlist(3) | ||
| 313 | |||
| 314 | ! Handle edge cases | ||
| 315 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 1006 times.
|
1006 | if (n <= 0) then |
| 316 | ✗ | n1 = 1 | |
| 317 | ✗ | n2 = 1 | |
| 318 | ✗ | n3 = n | |
| 319 | 2 | return | |
| 320 | end if | ||
| 321 | |||
| 322 |
2/2✓ Branch 4 → 5 taken 2 times.
✓ Branch 4 → 6 taken 1004 times.
|
1006 | if (n == 1) then |
| 323 | 2 | n1 = 1 | |
| 324 | 2 | n2 = 1 | |
| 325 | 2 | n3 = 1 | |
| 326 | 2 | return | |
| 327 | end if | ||
| 328 | |||
| 329 | ! Start from cbrt(n) and search for the most balanced factorization | ||
| 330 | 1004 | cbrt_n = int(real(n)**(1.0 / 3.0)) + 1 | |
| 331 | best_n1 = 1 | ||
| 332 | best_n2 = 1 | ||
| 333 | best_n3 = n | ||
| 334 | 1004 | max_diff = n - 1 | |
| 335 | |||
| 336 |
2/2✓ Branch 7 → 8 taken 8000 times.
✓ Branch 7 → 13 taken 1004 times.
|
9004 | do i = cbrt_n, 1, -1 |
| 337 |
2/2✓ Branch 8 → 9 taken 5299 times.
✓ Branch 8 → 10 taken 2701 times.
|
9004 | if (mod(n, i) == 0) then |
| 338 | ! i is a divisor, now split n/i into two balanced factors | ||
| 339 | 2701 | call balanced_split(n / i, temp_n2, temp_n3) | |
| 340 | |||
| 341 | ! Check if this is more balanced | ||
| 342 | 2701 | current_max_diff = max(temp_n3 - i, temp_n3 - temp_n2, temp_n2 - i) | |
| 343 | |||
| 344 |
2/2✓ Branch 11 → 9 taken 1693 times.
✓ Branch 11 → 12 taken 1008 times.
|
2701 | if (current_max_diff < max_diff) then |
| 345 | best_n1 = i | ||
| 346 | best_n2 = temp_n2 | ||
| 347 | best_n3 = temp_n3 | ||
| 348 | max_diff = current_max_diff | ||
| 349 | end if | ||
| 350 | end if | ||
| 351 | end do | ||
| 352 | |||
| 353 | ! Sort the results to ensure n1 <= n2 <= n3 | ||
| 354 |
2/2✓ Branch 15 → 16 taken 3012 times.
✓ Branch 15 → 17 taken 1004 times.
|
4016 | nlist = [best_n1, best_n2, best_n3] |
| 355 | 1004 | call sort_int(nlist) | |
| 356 | 1004 | n1 = nlist(1) | |
| 357 | 1004 | n2 = nlist(2) | |
| 358 | 1004 | n3 = nlist(3) | |
| 359 | |||
| 360 | end subroutine balanced_split_3 | ||
| 361 | |||
| 362 | 285 | module real(wp) function brent(fun, xa_in, xb_in, x_tol, max_iter, fa_in, fb_in, success) result(xsol) | |
| 363 | implicit none | ||
| 364 | |||
| 365 | procedure(user_function_1d_interface) :: fun | ||
| 366 | real(wp), intent(in) :: xa_in, xb_in, x_tol | ||
| 367 | integer, intent(in) :: max_iter | ||
| 368 | real(wp), intent(in), optional :: fa_in, fb_in | ||
| 369 | logical, intent(out), optional :: success | ||
| 370 | |||
| 371 | ! Local variables | ||
| 372 | real(wp) :: xa, xb, fa, fb, fc, xc, xd, xe, xm, p, q, r, s | ||
| 373 | real(wp) :: toler | ||
| 374 | integer :: iter, flag | ||
| 375 | |||
| 376 | |||
| 377 | |||
| 378 | iter = 0 | ||
| 379 | |||
| 380 | 285 | xa = xa_in | |
| 381 | 285 | xb = xb_in | |
| 382 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 285 times.
|
285 | if (present(fa_in)) then |
| 383 | ✗ | fa = fa_in | |
| 384 | else | ||
| 385 | 285 | fa = fun(xa) | |
| 386 | iter = iter + 1 | ||
| 387 | endif | ||
| 388 |
1/2✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 285 times.
|
285 | if (present(fb_in)) then |
| 389 | ✗ | fb = fb_in | |
| 390 | else | ||
| 391 | 285 | fb = fun(xb) | |
| 392 | 285 | iter = iter + 1 | |
| 393 | endif | ||
| 394 | |||
| 395 | flag = 1 ! 1: continue, 0: converged, -1: some problem | ||
| 396 |
1/2✓ Branch 9 → 10 taken 285 times.
✗ Branch 9 → 13 not taken.
|
285 | if (fa == 0) then |
| 397 | xsol = xa | ||
| 398 | flag = 0 | ||
| 399 |
1/2✓ Branch 10 → 11 taken 285 times.
✗ Branch 10 → 13 not taken.
|
285 | elseif (fb == 0) then |
| 400 | xsol = xb | ||
| 401 | flag = 0 | ||
| 402 |
2/2✓ Branch 11 → 12 taken 45 times.
✓ Branch 11 → 13 taken 240 times.
|
285 | elseif (fa * fb > 0) then |
| 403 | xsol = 0 | ||
| 404 | flag = -1 | ||
| 405 | endif | ||
| 406 | |||
| 407 | xc = xa; fc = fa | ||
| 408 | 285 | xd = xb - xa; xe = xd | |
| 409 | |||
| 410 |
3/4✓ Branch 14 → 15 taken 1967 times.
✓ Branch 14 → 42 taken 45 times.
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 1967 times.
|
2012 | do while (flag == 1 .and. fb /= 0 .and. xa /= xb) |
| 411 | |||
| 412 | ! Ensure that b is the best result so far, a is the previous | ||
| 413 | ! value of b, and c is on the opposite side of the zero from b. | ||
| 414 |
2/2✓ Branch 17 → 18 taken 1015 times.
✓ Branch 17 → 19 taken 952 times.
|
1967 | if (fb * fc > 0) then |
| 415 | xc = xa; fc = fa | ||
| 416 | 1015 | xd = xb - xa; xe = xd | |
| 417 | endif | ||
| 418 |
2/2✓ Branch 19 → 20 taken 545 times.
✓ Branch 19 → 21 taken 1422 times.
|
1967 | if (abs(fc) < abs(fb)) then |
| 419 | 545 | xa = xb; xb = xc; xc = xa | |
| 420 | fa = fb; fb = fc; fc = fa | ||
| 421 | endif | ||
| 422 | |||
| 423 | ! Convergence test and possible exit | ||
| 424 | 1967 | xm = 0.5*(xc - xb) | |
| 425 | |||
| 426 | 1967 | toler = x_tol | |
| 427 |
3/4✓ Branch 21 → 22 taken 1727 times.
✓ Branch 21 → 43 taken 240 times.
✓ Branch 22 → 23 taken 1727 times.
✗ Branch 22 → 43 not taken.
|
1967 | if ((abs(xm) <= toler) .or. (fb == 0.0)) then |
| 428 | flag = 0 | ||
| 429 | exit | ||
| 430 |
1/2✓ Branch 23 → 24 taken 1727 times.
✗ Branch 23 → 43 not taken.
|
1727 | elseif (iter == max_iter) then |
| 431 | flag = -1 | ||
| 432 | exit | ||
| 433 | endif | ||
| 434 | |||
| 435 | ! Choose bisection or interpolation | ||
| 436 |
2/4✓ Branch 24 → 25 taken 1727 times.
✗ Branch 24 → 35 not taken.
✓ Branch 25 → 26 taken 1727 times.
✗ Branch 25 → 35 not taken.
|
1727 | if ((abs(xe) < toler) .or. (abs(fa) <= abs(fb))) then |
| 437 | ! Bisection | ||
| 438 | xd = xm; xe = xm | ||
| 439 | else | ||
| 440 | ! Interpolation | ||
| 441 | 1727 | s = fb/fa; | |
| 442 |
2/2✓ Branch 26 → 27 taken 1015 times.
✓ Branch 26 → 28 taken 712 times.
|
1727 | if (xa == xc) then |
| 443 | ! Linear interpolation | ||
| 444 | 1015 | p = 2*xm*s | |
| 445 | 1015 | q = 1 - s | |
| 446 | else | ||
| 447 | ! Inverse quadratic interpolation | ||
| 448 | 712 | q = fa/fc | |
| 449 | 712 | r = fb/fc | |
| 450 | 712 | p = s*(2*xm*q*(q - r) - (xb - xa)*(r - 1)) | |
| 451 | 712 | q = (q - 1)*(r - 1)*(s - 1) | |
| 452 | endif | ||
| 453 |
2/2✓ Branch 29 → 30 taken 1104 times.
✓ Branch 29 → 31 taken 623 times.
|
1727 | if (p > 0) then |
| 454 | 1104 | q = -q | |
| 455 | else | ||
| 456 | 623 | p = -p | |
| 457 | endif | ||
| 458 | ! Is interpolated point acceptable | ||
| 459 |
3/4✓ Branch 32 → 33 taken 1451 times.
✓ Branch 32 → 35 taken 276 times.
✓ Branch 33 → 34 taken 1451 times.
✗ Branch 33 → 35 not taken.
|
1727 | if ((2*p < 3*xm*q - abs(toler*q)) .and. (p < abs(0.5*xe*q))) then |
| 460 | 1451 | xe = xd; xd = p/q | |
| 461 | else | ||
| 462 | xd = xm; xe = xm | ||
| 463 | endif | ||
| 464 | endif ! Interpolation | ||
| 465 | |||
| 466 | ! Next point | ||
| 467 | 1727 | xa = xb | |
| 468 | fa = fb | ||
| 469 |
2/2✓ Branch 35 → 36 taken 1511 times.
✓ Branch 35 → 37 taken 216 times.
|
1727 | if (abs(xd) > toler) then |
| 470 | 1511 | xb = xb + xd | |
| 471 |
2/2✓ Branch 37 → 38 taken 107 times.
✓ Branch 37 → 39 taken 109 times.
|
216 | elseif (xb > xc) then |
| 472 | 107 | xb = xb - toler | |
| 473 | else | ||
| 474 | 109 | xb = xb + toler | |
| 475 | endif | ||
| 476 | 1727 | fb = fun(xb) | |
| 477 | 1727 | iter = iter + 1 | |
| 478 | |||
| 479 | end do | ||
| 480 | |||
| 481 | 285 | xsol = xb | |
| 482 | |||
| 483 |
1/6✗ Branch 43 → 44 not taken.
✓ Branch 43 → 47 taken 285 times.
✗ Branch 44 → 45 not taken.
✗ Branch 44 → 46 not taken.
✗ Branch 45 → 46 not taken.
✗ Branch 45 → 47 not taken.
|
285 | if (flag == 1 .and. (fb == 0 .or. xa == xb)) then |
| 484 | flag = 0 | ||
| 485 | end if | ||
| 486 | |||
| 487 |
1/2✓ Branch 47 → 48 taken 285 times.
✗ Branch 47 → 49 not taken.
|
285 | if (present(success)) success = flag == 0 |
| 488 | |||
| 489 | 285 | end function | |
| 490 | |||
| 491 | end submodule | ||
| 492 |