ParaMonte Fortran 2.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
pm_arraySelect::getSelected Interface Reference

Generate and return the rankth smallest value in the input array by first sorting its elements in ascending order (optionally only between the specified indices [lb, ub]).
More...

Detailed Description

Generate and return the rankth smallest value in the input array by first sorting its elements in ascending order (optionally only between the specified indices [lb, ub]).

By default, the array will be sorted in ascending order, unless the user-specific isSorted() external function is supplied to define a custom sorting criterion.

Parameters
[in]array: The input contiguous array of shape (:) of either
  • type css_type (scalar string container of default kind SK) or
  • type css_pdt (scalar string container of kind any supported by the processor (e.g., SK, SKA, SKD , or SKU)) or
  • type character of kind any supported by the processor (e.g., SK, SKA, SKD , or SKU), or
  • type logical of kind any supported by the processor (e.g., LK), or
  • type integer of kind any supported by the processor (e.g., IK, IK8, IK16, IK32, or IK64), or
  • type complex of kind any supported by the processor (e.g., CK, CK32, CK64, or CK128), or
  • type real of kind any supported by the processor (e.g., RK, RK32, RK64, or RK128),
or,
  • a scalar assumed-length character of kind any supported by the processor (e.g., SK, SKA, SKD , or SKU),
containing the potentially unsorted sequence of values that is to be sorted and whose rankth smallest value is to be reported as the output selection.
[in]rank: The input integer of default kind IK, representing the index of the rankth smallest value in the input array or the rankth ordered value according to the user-supplied external logical function isSorted().
isSorted: The external user-specified function that takes two input scalar arguments of the same type and kind as the input array.
It returns a scalar logical of default kind LK that is .true. if the first input scalar argument is sorted with respect to the second input argument according to the user-defined condition within isSorted, otherwise, it is .false..
If array is a Fortran string (i.e., an assumed-length scalar character), then both input arguments to isSorted() are character(1,SK) of default kind SK.
The following illustrates the generic interface of isSorted(),
function isSorted(a,b) result (sorted)
use pm_kind, only: IK, LK
TYPE(KIND) , intent(in) :: a, b
logical(LK) :: sorted
end function
This module defines the relevant Fortran kind type-parameters frequently used in the ParaMonte librar...
Definition: pm_kind.F90:268
integer, parameter LK
The default logical kind in the ParaMonte library: kind(.true.) in Fortran, kind(....
Definition: pm_kind.F90:541
integer, parameter IK
The default integer kind in the ParaMonte library: int32 in Fortran, c_int32_t in C-Fortran Interoper...
Definition: pm_kind.F90:540
where TYPE(KIND) is the same as the type and kind of the input argument array, which can be one of the following.
character(*, SK), intent(in) :: a, b
character(1, SK), intent(in) :: a, b
type(css_type) , intent(in) :: a, b
type(css_pdt) , intent(in) :: a, b
integer(IK) , intent(in) :: a, b
real(RK) , intent(in) :: a, b
This module contains the derived types for generating allocatable containers of scalar,...
This is the css_pdt parameterized type for generating instances of container of scalar of string obje...
This is the css_type type for generating instances of container of scalar of string objects.
where the specified kind type parameters (SK, IK, LK, CK, RK) can refer to any of the supported kinds by the processor.
This user-defined equivalence check is extremely useful where a user-defined sorting criterion other than simple ascending order is needed, for example, when the case-sensitivity of an input string or array of strings is irrelevant or when sorting of the absolute values matters excluding the signs of the numbers, or when descending order is desired.
In such cases, user can define a custom sorting condition within the user-defined external function isSorted to achieve the goal.
(optional, the default sorting condition is ascending order, that is a < b.)
[in]lb: The optional input scalar integer of default kind IK representing the lower bound of the segment of array below which the array elements are assumed to be in ascending order, such that only the elements starting with and beyond lb require sorting.
If the initial segment of the input array is ordered, specifying this lower bound will lead to better performance of the algorithm.
(optional, default = 1_IK)
[in]ub: The optional input scalar integer of default kind IK representing the upper bound of the segment of array above which the array elements are assumed to be in ascending order, such that only the elements ending with and below ub require sorting.
If the final segment of the input array is ordered, specifying this upper bound will lead to better performance of the algorithm.
(optional, default = size(array, kind = IK))
Returns
selection : The output scalar of the same type and kind as array containing the rankth smallest (or ordered) value in the input array.


Possible calling interfaces

selection = getSelected(array, rank, lb = lb, ub = ub)
selection = getSelected(array, rank, isSorted, lb = lb, ub = ub)
Generate and return the rankth smallest value in the input array by first sorting its elements in asc...
This module contains procedures and generic interfaces for selecting the th smallest element in unsor...
Warning
If lb is present as an input argument, its value must be larger than 0 and less than ub or its default value.
Furthermore, the specified rank must be larger than or equal to the specified value for lb.
If ub is present as an input argument, its value must be less than or equal to the length of array and larger than ub or its default value.
Furthermore, the specified rank must be smaller than or equal to the specified value for ub.
These conditions are verified only if the library is built with the preprocessor macro CHECK_ENABLED=1.
The functions under this generic interface are impure when the external input argument isSorted() is present.
The pure procedure(s) documented herein become impure when the ParaMonte library is compiled with preprocessor macro CHECK_ENABLED=1.
By default, these procedures are pure in release build and impure in debug and testing builds.
See also
setSelected
setRankDense
setRankOrdinal
setRankFractional
setRankStandard
setRankModified
setSorted
setSorted
getRemoved
setReversed
getReplaced
setReplaced
setSplit


Example usage

1program example
2
3 use pm_kind, only: SK, IK, LK, RK
4 use pm_distUnif, only: getUnifRand
6 use pm_io, only: display_type
7
8 implicit none
9
10 type(display_type) :: disp
11 disp = display_type(file = "main.out.F90")
12
13 call disp%skip()
14 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
15 call disp%show("!Select the `rank`th smallest element in the input `integer` Array.")
16 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
17 call disp%skip()
18
19 block
20 use pm_kind, only: IKG => IKL
21 integer(IK) :: rank, lenArray
22 integer, parameter :: TKG = merge(IKG, IK, IKG > 0)
23 integer(TKG), allocatable :: array(:)
24 integer(TKG) :: selection
25 call disp%skip()
26 call disp%show("lenArray = getUnifRand(5_IK, 9_IK)")
27 lenArray = getUnifRand(5_IK, 9_IK)
28 call disp%show("rank = getUnifRand(1_IK, lenArray)")
29 rank = getUnifRand(1_IK, lenArray)
30 call disp%show("[lenArray, rank]")
31 call disp%show( [lenArray, rank] )
32 call disp%show("array = getUnifRand(-9, +9, lenArray)")
33 array = getUnifRand(-9, +9, lenArray)
34 call disp%show("array")
35 call disp%show( array )
36 call disp%show("selection = getSelected(array, rank)")
37 selection = getSelected(array, rank)
38 call disp%show("selection")
39 call disp%show( selection )
40 call disp%show("array")
41 call disp%show( array )
42 call disp%skip()
43 end block
44
45 block
46 use pm_kind, only: IKG => IKS
47 integer(IK) :: rank, lenArray
48 integer, parameter :: TKG = merge(IKG, IK, IKG > 0)
49 integer(TKG), allocatable :: array(:)
50 integer(TKG) :: selection
51 call disp%skip()
52 call disp%show("lenArray = getUnifRand(5_IK, 9_IK)")
53 lenArray = getUnifRand(5_IK, 9_IK)
54 call disp%show("rank = getUnifRand(1_IK, lenArray)")
55 rank = getUnifRand(1_IK, lenArray)
56 call disp%show("[lenArray, rank]")
57 call disp%show( [lenArray, rank] )
58 call disp%show("array = getUnifRand(-9, +9, lenArray)")
59 array = getUnifRand(-9, +9, lenArray)
60 call disp%show("array")
61 call disp%show( array )
62 call disp%show("selection = getSelected(array, rank)")
63 selection = getSelected(array, rank)
64 call disp%show("selection")
65 call disp%show( selection )
66 call disp%show("array")
67 call disp%show( array )
68 call disp%skip()
69 end block
70
71 block
72 use pm_kind, only: IKG => IKD
73 integer(IK) :: rank, lenArray
74 integer, parameter :: TKG = merge(IKG, IK, IKG > 0)
75 integer(TKG), allocatable :: array(:)
76 integer(TKG) :: selection
77 call disp%skip()
78 call disp%show("lenArray = getUnifRand(5_IK, 9_IK)")
79 lenArray = getUnifRand(5_IK, 9_IK)
80 call disp%show("rank = getUnifRand(1_IK, lenArray)")
81 rank = getUnifRand(1_IK, lenArray)
82 call disp%show("[lenArray, rank]")
83 call disp%show( [lenArray, rank] )
84 call disp%show("array = getUnifRand(-9, +9, lenArray)")
85 array = getUnifRand(-9, +9, lenArray)
86 call disp%show("array")
87 call disp%show( array )
88 call disp%show("selection = getSelected(array, rank)")
89 selection = getSelected(array, rank)
90 call disp%show("selection")
91 call disp%show( selection )
92 call disp%show("array")
93 call disp%show( array )
94 call disp%skip()
95 end block
96
97 block
98 use pm_kind, only: IKG => IKH
99 integer(IK) :: rank, lenArray
100 integer, parameter :: TKG = merge(IKG, IK, IKG > 0)
101 integer(TKG), allocatable :: array(:)
102 integer(TKG) :: selection
103 call disp%skip()
104 call disp%show("lenArray = getUnifRand(5_IK, 9_IK)")
105 lenArray = getUnifRand(5_IK, 9_IK)
106 call disp%show("rank = getUnifRand(1_IK, lenArray)")
107 rank = getUnifRand(1_IK, lenArray)
108 call disp%show("[lenArray, rank]")
109 call disp%show( [lenArray, rank] )
110 call disp%show("array = getUnifRand(-9, +9, lenArray)")
111 array = getUnifRand(-9, +9, lenArray)
112 call disp%show("array")
113 call disp%show( array )
114 call disp%show("selection = getSelected(array, rank)")
115 selection = getSelected(array, rank)
116 call disp%show("selection")
117 call disp%show( selection )
118 call disp%show("array")
119 call disp%show( array )
120 call disp%skip()
121 end block
122
123 call disp%skip()
124 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
125 call disp%show("!Select the `rank`th smallest element in the input `real` Array.")
126 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
127 call disp%skip()
128
129 block
130 use pm_kind, only: RKG => RKS
131 integer(IK) :: rank, lenArray
132 integer, parameter :: TKG = merge(RKG, RK, RKG > 0)
133 real(TKG), allocatable :: array(:)
134 real(TKG) :: selection
135 call disp%skip()
136 call disp%show("lenArray = getUnifRand(5_IK, 9_IK)")
137 lenArray = getUnifRand(5_IK, 9_IK)
138 call disp%show("rank = getUnifRand(1_IK, lenArray)")
139 rank = getUnifRand(1_IK, lenArray)
140 call disp%show("[lenArray, rank]")
141 call disp%show( [lenArray, rank] )
142 call disp%show("array = getUnifRand(-9, +9, lenArray)")
143 array = getUnifRand(-9, +9, lenArray)
144 call disp%show("array")
145 call disp%show( array )
146 call disp%show("selection = getSelected(array, rank)")
147 selection = getSelected(array, rank)
148 call disp%show("selection")
149 call disp%show( selection )
150 call disp%show("array")
151 call disp%show( array )
152 call disp%skip()
153 end block
154
155 block
156 use pm_kind, only: RKG => RKD
157 integer(IK) :: rank, lenArray
158 integer, parameter :: TKG = merge(RKG, RK, RKG > 0)
159 real(TKG), allocatable :: array(:)
160 real(TKG) :: selection
161 call disp%skip()
162 call disp%show("lenArray = getUnifRand(5_IK, 9_IK)")
163 lenArray = getUnifRand(5_IK, 9_IK)
164 call disp%show("rank = getUnifRand(1_IK, lenArray)")
165 rank = getUnifRand(1_IK, lenArray)
166 call disp%show("[lenArray, rank]")
167 call disp%show( [lenArray, rank] )
168 call disp%show("array = getUnifRand(-9, +9, lenArray)")
169 array = getUnifRand(-9, +9, lenArray)
170 call disp%show("array")
171 call disp%show( array )
172 call disp%show("selection = getSelected(array, rank)")
173 selection = getSelected(array, rank)
174 call disp%show("selection")
175 call disp%show( selection )
176 call disp%show("array")
177 call disp%show( array )
178 call disp%skip()
179 end block
180
181 block
182 use pm_kind, only: RKG => RKH
183 integer(IK) :: rank, lenArray
184 integer, parameter :: TKG = merge(RKG, RK, RKG > 0)
185 real(TKG), allocatable :: array(:)
186 real(TKG) :: selection
187 call disp%skip()
188 call disp%show("lenArray = getUnifRand(5_IK, 9_IK)")
189 lenArray = getUnifRand(5_IK, 9_IK)
190 call disp%show("rank = getUnifRand(1_IK, lenArray)")
191 rank = getUnifRand(1_IK, lenArray)
192 call disp%show("[lenArray, rank]")
193 call disp%show( [lenArray, rank] )
194 call disp%show("array = getUnifRand(-9, +9, lenArray)")
195 array = getUnifRand(-9, +9, lenArray)
196 call disp%show("array")
197 call disp%show( array )
198 call disp%show("selection = getSelected(array, rank)")
199 selection = getSelected(array, rank)
200 call disp%show("selection")
201 call disp%show( selection )
202 call disp%show("array")
203 call disp%show( array )
204 call disp%skip()
205 end block
206
207 call disp%skip()
208 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
209 call disp%show("!Select the `rank`th smallest element in the input Fortran `string` (character) array.")
210 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
211 call disp%skip()
212
213 block
214 use pm_kind, only: TKG => SK
215 character(2, TKG) :: selection
216 character(2, TKG), allocatable :: array(:)
217 integer(IK) :: rank, lenArray
218 call disp%skip()
219 call disp%show("lenArray = getUnifRand(5_IK, 9_IK)")
220 lenArray = getUnifRand(5_IK, 9_IK)
221 call disp%show("rank = getUnifRand(1_IK, lenArray)")
222 rank = getUnifRand(1_IK, lenArray)
223 call disp%show("[lenArray, rank]")
224 call disp%show( [lenArray, rank] )
225 call disp%show("array = getUnifRand('AA', 'ZZ', lenArray)")
226 array = getUnifRand('AA', 'ZZ', lenArray)
227 call disp%show("array")
228 call disp%show( array , deliml = SK_"""" )
229 call disp%show("selection = getSelected(array, rank)")
230 selection = getSelected(array, rank)
231 call disp%show("selection")
232 call disp%show( selection )
233 call disp%show("array")
234 call disp%show( array , deliml = SK_"""" )
235 call disp%skip()
236 end block
237
238 call disp%skip()
239 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
240 call disp%show("!Select the `rank`th smallest element in the input Fortran `string` (character) scalar.")
241 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
242 call disp%skip()
243
244 block
245 use pm_kind, only: TKG => SK
246 character(1, TKG) :: selection
247 character(:, TKG), allocatable :: array
248 integer(IK) :: rank, lenArray
249 call disp%skip()
250 call disp%show("lenArray = getUnifRand(5_IK, 9_IK)")
251 lenArray = getUnifRand(5_IK, 9_IK)
252 call disp%show("rank = getUnifRand(1_IK, lenArray)")
253 rank = getUnifRand(1_IK, lenArray)
254 call disp%show("[lenArray, rank]")
255 call disp%show( [lenArray, rank] )
256 call disp%show("array = getUnifRand(repeat('A', lenArray), repeat('Z', lenArray))")
257 array = getUnifRand(repeat('A', lenArray), repeat('Z', lenArray))
258 call disp%show("array")
259 call disp%show( array , deliml = SK_"""" )
260 call disp%show("selection = getSelected(array, rank)")
261 selection = getSelected(array, rank)
262 call disp%show("selection")
263 call disp%show( selection )
264 call disp%show("array")
265 call disp%show( array , deliml = SK_"""" )
266 call disp%skip()
267 end block
268
269 call disp%skip()
270 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
271 call disp%show("!Select the `rank`th smallest element in the input array of containers of Fortran `string` (character) scalars.")
272 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
273 call disp%skip()
274
275 block
276 use pm_container, only: css => css_type
277 integer(IK) :: rank, lenArray, iell
278 type(css), allocatable :: array(:)
279 type(css) :: selection
280 call disp%skip()
281 call disp%show("lenArray = getUnifRand(5_IK, 9_IK)")
282 lenArray = getUnifRand(5_IK, 9_IK)
283 call disp%show("rank = getUnifRand(1_IK, lenArray)")
284 rank = getUnifRand(1_IK, lenArray)
285 call disp%show("[lenArray, rank]")
286 call disp%show( [lenArray, rank] )
287 call disp%show("array = [(css(getUnifRand('A', 'Z', getUnifRand(1_IK, 9_IK))), iell = 1, lenArray)]")
288 array = [(css(getUnifRand('A', 'Z', getUnifRand(1_IK, 9_IK))), iell = 1, lenArray)]
289 call disp%show("array")
290 call disp%show( array , deliml = SK_"""" )
291 call disp%show("selection = getSelected(array, rank)")
292 selection = getSelected(array, rank)
293 call disp%show("selection")
294 call disp%show( selection )
295 call disp%show("array")
296 call disp%show( array , deliml = SK_"""" )
297 call disp%skip()
298 end block
299
300 call disp%skip()
301 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
302 call disp%show("!Select according to an input user-defined comparison function.")
303 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
304 call disp%skip()
305
306 block
307 use pm_kind, only: TKG => IK
308 integer(IK) :: rank, lenArray, i
309 integer(TKG), allocatable :: array(:)
310 integer(TKG) :: selection
311 call disp%skip()
312 call disp%show("lenArray = getUnifRand(5_IK, 9_IK)")
313 lenArray = getUnifRand(5_IK, 9_IK)
314 call disp%show("rank = getUnifRand(1_IK, lenArray)")
315 rank = getUnifRand(1_IK, lenArray)
316 call disp%show("[lenArray, rank]")
317 call disp%show( [lenArray, rank] )
318 call disp%show("array = int([((-2)**i, i = 1, lenArray)], kind = TKG)")
319 array = int([((-2)**i, i = 1, lenArray)], kind = TKG)
320 call disp%show("array")
321 call disp%show( array )
322 call disp%show("!Select the `rank`th largest element via an input custom-designed `isSorted()` function.")
323 call disp%show("selection = getSelected(array, rank, isSorted_IK)")
324 selection = getSelected(array, rank, isSorted_IK)
325 call disp%show("selection")
326 call disp%show( selection )
327 call disp%show("array")
328 call disp%show( array )
329 call disp%skip()
330 end block
331
332 block
333 use pm_kind, only: TKG => RK
334 integer(IK) :: rank, lenArray, i
335 real(TKG), allocatable :: array(:)
336 real(TKG) :: selection
337 call disp%skip()
338 call disp%show("lenArray = getUnifRand(5_IK, 9_IK)")
339 lenArray = getUnifRand(5_IK, 9_IK)
340 call disp%show("rank = getUnifRand(1_IK, lenArray)")
341 rank = getUnifRand(1_IK, lenArray)
342 call disp%show("[lenArray, rank]")
343 call disp%show( [lenArray, rank] )
344 call disp%show("array = int([((-2)**i, i = 1, lenArray)], kind = TKG)")
345 array = int([((-2)**i, i = 1, lenArray)], kind = TKG)
346 call disp%show("array")
347 call disp%show( array )
348 call disp%show("!Select the `rank`th smallest element solely based on the magnitude of numbers using a custom comparison function.")
349 call disp%show("selection = getSelected(array, rank, isSorted_RK)")
350 selection = getSelected(array, rank, isSorted_RK)
351 call disp%show("selection")
352 call disp%show( selection )
353 call disp%show("array")
354 call disp%show( array )
355 call disp%skip()
356 end block
357
358 block
359 use pm_kind, only: TKG => SK
360 integer(IK) :: rank, lenArray, i
361 character(:,TKG), allocatable :: array
362 character(1,TKG) :: selection
363 call disp%skip()
364 call disp%show("lenArray = getUnifRand(5_IK, 9_IK)")
365 lenArray = getUnifRand(5_IK, 9_IK)
366 call disp%show("rank = getUnifRand(1_IK, lenArray)")
367 rank = getUnifRand(1_IK, lenArray)
368 call disp%show("[lenArray, rank]")
369 call disp%show( [lenArray, rank] )
370 call disp%show("array = 'ParaMonte'")
371 array = 'ParaMonte'
372 call disp%show("array")
373 call disp%show( array , deliml = SK_"""" )
374 call disp%show("!Select the `rank`th smallest element with case-sensitivity (default behavior).")
375 call disp%show("selection = getSelected(array, rank)")
376 selection = getSelected(array, rank)
377 call disp%show("selection")
378 call disp%show( selection , deliml = SK_"""" )
379 call disp%show("array")
380 call disp%show( array , deliml = SK_"""" )
381 call disp%skip()
382 end block
383
384 block
385 use pm_kind, only: TKG => SK
386 integer(IK) :: rank, lenArray, i
387 character(:,TKG), allocatable :: array
388 character(1,TKG) :: selection
389 call disp%skip()
390 call disp%show("lenArray = getUnifRand(5_IK, 9_IK)")
391 lenArray = getUnifRand(5_IK, 9_IK)
392 call disp%show("rank = getUnifRand(1_IK, lenArray)")
393 rank = getUnifRand(1_IK, lenArray)
394 call disp%show("[lenArray, rank]")
395 call disp%show( [lenArray, rank] )
396 call disp%show("array = 'ParaMonte'")
397 array = 'ParaMonte'
398 call disp%show("array")
399 call disp%show( array , deliml = SK_"""" )
400 call disp%show("!Select the `rank`th smallest element WITHOUT case-sensitivity via a custom-designed input comparison function.")
401 call disp%show("selection = getSelected(array, rank, isSorted_SK)")
402 selection = getSelected(array, rank, isSorted_SK)
403 call disp%show("selection")
404 call disp%show( selection , deliml = SK_"""" )
405 call disp%show("array")
406 call disp%show( array , deliml = SK_"""" )
407 call disp%skip()
408 end block
409
410 call disp%skip()
411 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
412 call disp%show("!Expedite the selection process for a partially sorted array via optional arguments `lb` or `ub`.")
413 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
414 call disp%skip()
415
416 block
417 integer(IK), allocatable :: array(:)
418 integer(IK) :: selection
419 integer(IK) :: rank
420 call disp%skip()
421 call disp%show("rank = 3_IK")
422 rank = 3_IK
423 call disp%show("array = [-3, -1, -2, 1, 2, 3] ! all elements after index `3` are sorted (`ub = 3_IK`).")
424 array = [-3, -1, -2, 1, 2, 3] ! all elements after index `3` are sorted (`ub = 3_IK`).
425 call disp%show("selection = getSelected(array, rank, ub = 3_IK) ! all elements after index `3` are sorted (`ub = 3_IK`).")
426 selection = getSelected(array, rank, ub = 3_IK)
427 call disp%show("selection")
428 call disp%show( selection )
429 call disp%show("array")
430 call disp%show( array )
431 call disp%skip()
432 end block
433
434contains
435
436 function isSorted_IK(a,b) result(isSorted)
437 use pm_kind, only: LK, IK8
438 integer(IK), intent(in) :: a, b
439 logical(LK) :: isSorted
440 isSorted = a > b
441 end function
442
443 function isSorted_RK(a,b) result(isSorted)
444 use pm_kind, only: LK, RK
445 integer(RK), intent(in) :: a, b
446 logical(LK) :: isSorted
447 isSorted = abs(a) < abs(b)
448 end function
449
450 function isSorted_SK(a,b) result(isSorted)
451 use pm_kind, only: LK, SK
452 use pm_strASCII, only: getStrLower
453 character(1, SK), intent(in) :: a, b
454 logical(LK) :: isSorted
455 isSorted = getStrLower(a) < getStrLower(b)
456 end function
457
458end program example
Generate and return a scalar or a contiguous array of rank 1 of length s1 of randomly uniformly distr...
This is a generic method of the derived type display_type with pass attribute.
Definition: pm_io.F90:11726
This is a generic method of the derived type display_type with pass attribute.
Definition: pm_io.F90:11508
Generate and return the input string where the uppercase English alphabets are all converted to lower...
This module contains classes and procedures for computing various statistical quantities related to t...
This module contains classes and procedures for input/output (IO) or generic display operations on st...
Definition: pm_io.F90:252
type(display_type) disp
This is a scalar module variable an object of type display_type for general display.
Definition: pm_io.F90:11393
integer, parameter RK
The default real kind in the ParaMonte library: real64 in Fortran, c_double in C-Fortran Interoperati...
Definition: pm_kind.F90:543
integer, parameter IKS
The single-precision integer kind in Fortran mode. On most platforms, this is a 32-bit integer kind.
Definition: pm_kind.F90:563
integer, parameter IKL
The scalar integer constant of intrinsic default kind, representing the lowest range integer kind typ...
Definition: pm_kind.F90:749
integer, parameter IKH
The scalar integer constant of intrinsic default kind, representing the highest range integer kind ty...
Definition: pm_kind.F90:828
integer, parameter RKD
The double precision real kind in Fortran mode. On most platforms, this is an 64-bit real kind.
Definition: pm_kind.F90:568
integer, parameter IK8
The integer kind for an 8-bits container.
Definition: pm_kind.F90:548
integer, parameter IKD
The double precision integer kind in Fortran mode. On most platforms, this is a 64-bit integer kind.
Definition: pm_kind.F90:564
integer, parameter SK
The default character kind in the ParaMonte library: kind("a") in Fortran, c_char in C-Fortran Intero...
Definition: pm_kind.F90:539
integer, parameter RKH
The scalar integer constant of intrinsic default kind, representing the highest-precision real kind t...
Definition: pm_kind.F90:858
integer, parameter RKS
The single-precision real kind in Fortran mode. On most platforms, this is an 32-bit real kind.
Definition: pm_kind.F90:567
This module contains the uncommon and hardly representable ASCII characters as well as procedures for...
Definition: pm_strASCII.F90:61
Generate and return an object of type display_type.
Definition: pm_io.F90:10282

Example Unix compile command via Intel ifort compiler
1#!/usr/bin/env sh
2rm main.exe
3ifort -fpp -standard-semantics -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example Windows Batch compile command via Intel ifort compiler
1del main.exe
2set PATH=..\..\..\lib;%PATH%
3ifort /fpp /standard-semantics /O3 /I:..\..\..\include main.F90 ..\..\..\lib\libparamonte*.lib /exe:main.exe
4main.exe

Example Unix / MinGW compile command via GNU gfortran compiler
1#!/usr/bin/env sh
2rm main.exe
3gfortran -cpp -ffree-line-length-none -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example output
1
2!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3!Select the `rank`th smallest element in the input `integer` Array.
4!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5
6
7lenArray = getUnifRand(5_IK, 9_IK)
8rank = getUnifRand(1_IK, lenArray)
9[lenArray, rank]
10+5, +3
11array = getUnifRand(-9, +9, lenArray)
12array
13+6, +6, -3, +6, -4
14selection = getSelected(array, rank)
15selection
16+6
17array
18+6, +6, -3, +6, -4
19
20
21lenArray = getUnifRand(5_IK, 9_IK)
22rank = getUnifRand(1_IK, lenArray)
23[lenArray, rank]
24+7, +4
25array = getUnifRand(-9, +9, lenArray)
26array
27+8, +2, -6, +0, +6, +6, +9
28selection = getSelected(array, rank)
29selection
30+6
31array
32+8, +2, -6, +0, +6, +6, +9
33
34
35lenArray = getUnifRand(5_IK, 9_IK)
36rank = getUnifRand(1_IK, lenArray)
37[lenArray, rank]
38+8, +4
39array = getUnifRand(-9, +9, lenArray)
40array
41-1, -9, +8, +5, +3, -2, -5, -3
42selection = getSelected(array, rank)
43selection
44-2
45array
46-1, -9, +8, +5, +3, -2, -5, -3
47
48
49lenArray = getUnifRand(5_IK, 9_IK)
50rank = getUnifRand(1_IK, lenArray)
51[lenArray, rank]
52+6, +6
53array = getUnifRand(-9, +9, lenArray)
54array
55+4, +2, +8, +3, -8, +2
56selection = getSelected(array, rank)
57selection
58+8
59array
60+4, +2, +8, +3, -8, +2
61
62
63!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64!Select the `rank`th smallest element in the input `real` Array.
65!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66
67
68lenArray = getUnifRand(5_IK, 9_IK)
69rank = getUnifRand(1_IK, lenArray)
70[lenArray, rank]
71+9, +2
72array = getUnifRand(-9, +9, lenArray)
73array
74-7.00000000, +0.00000000, +3.00000000, +4.00000000, -2.00000000, +2.00000000, -1.00000000, -2.00000000, -1.00000000
75selection = getSelected(array, rank)
76selection
77-2.00000000
78array
79-7.00000000, +0.00000000, +3.00000000, +4.00000000, -2.00000000, +2.00000000, -1.00000000, -2.00000000, -1.00000000
80
81
82lenArray = getUnifRand(5_IK, 9_IK)
83rank = getUnifRand(1_IK, lenArray)
84[lenArray, rank]
85+5, +3
86array = getUnifRand(-9, +9, lenArray)
87array
88+9.0000000000000000, +0.0000000000000000, +8.0000000000000000, +8.0000000000000000, +9.0000000000000000
89selection = getSelected(array, rank)
90selection
91+8.0000000000000000
92array
93+9.0000000000000000, +0.0000000000000000, +8.0000000000000000, +8.0000000000000000, +9.0000000000000000
94
95
96lenArray = getUnifRand(5_IK, 9_IK)
97rank = getUnifRand(1_IK, lenArray)
98[lenArray, rank]
99+6, +4
100array = getUnifRand(-9, +9, lenArray)
101array
102+9.00000000000000000000000000000000000, +6.00000000000000000000000000000000000, +4.00000000000000000000000000000000000, -4.00000000000000000000000000000000000, -1.00000000000000000000000000000000000, +5.00000000000000000000000000000000000
103selection = getSelected(array, rank)
104selection
105+5.00000000000000000000000000000000000
106array
107+9.00000000000000000000000000000000000, +6.00000000000000000000000000000000000, +4.00000000000000000000000000000000000, -4.00000000000000000000000000000000000, -1.00000000000000000000000000000000000, +5.00000000000000000000000000000000000
108
109
110!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111!Select the `rank`th smallest element in the input Fortran `string` (character) array.
112!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113
114
115lenArray = getUnifRand(5_IK, 9_IK)
116rank = getUnifRand(1_IK, lenArray)
117[lenArray, rank]
118+6, +1
119array = getUnifRand('AA', 'ZZ', lenArray)
120array
121"MJ", "BI", "JL", "VG", "NC", "LR"
122selection = getSelected(array, rank)
123selection
124BI
125array
126"MJ", "BI", "JL", "VG", "NC", "LR"
127
128
129!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130!Select the `rank`th smallest element in the input Fortran `string` (character) scalar.
131!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
132
133
134lenArray = getUnifRand(5_IK, 9_IK)
135rank = getUnifRand(1_IK, lenArray)
136[lenArray, rank]
137+5, +3
138array = getUnifRand(repeat('A', lenArray), repeat('Z', lenArray))
139array
140"GWEKK"
141selection = getSelected(array, rank)
142selection
143K
144array
145"GWEKK"
146
147
148!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149!Select the `rank`th smallest element in the input array of containers of Fortran `string` (character) scalars.
150!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151
152
153lenArray = getUnifRand(5_IK, 9_IK)
154rank = getUnifRand(1_IK, lenArray)
155[lenArray, rank]
156+7, +5
157array = [(css(getUnifRand('A', 'Z', getUnifRand(1_IK, 9_IK))), iell = 1, lenArray)]
158array
159"E", "N", "R", "I", "C", "F", "H", "T", "Z", "G", "C", "G", "B", "F", "H", "D", "X", "B", "R", "H", "X", "M", "X", "U", "T", "Z", "I", "W", "X", "V"
160selection = getSelected(array, rank)
161selection
162D
163array
164"E", "N", "R", "I", "C", "F", "H", "T", "Z", "G", "C", "G", "B", "F", "H", "D", "X", "B", "R", "H", "X", "M", "X", "U", "T", "Z", "I", "W", "X", "V"
165
166
167!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
168!Select according to an input user-defined comparison function.
169!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
170
171
172lenArray = getUnifRand(5_IK, 9_IK)
173rank = getUnifRand(1_IK, lenArray)
174[lenArray, rank]
175+9, +7
176array = int([((-2)**i, i = 1, lenArray)], kind = TKG)
177array
178-2, +4, -8, +16, -32, +64, -128, +256, -512
179!Select the `rank`th largest element via an input custom-designed `isSorted()` function.
180selection = getSelected(array, rank, isSorted_IK)
181selection
182-32
183array
184-2, +4, -8, +16, -32, +64, -128, +256, -512
185
186
187lenArray = getUnifRand(5_IK, 9_IK)
188rank = getUnifRand(1_IK, lenArray)
189[lenArray, rank]
190+9, +1
191array = int([((-2)**i, i = 1, lenArray)], kind = TKG)
192array
193-2.0000000000000000, +4.0000000000000000, -8.0000000000000000, +16.000000000000000, -32.000000000000000, +64.000000000000000, -128.00000000000000, +256.00000000000000, -512.00000000000000
194!Select the `rank`th smallest element solely based on the magnitude of numbers using a custom comparison function.
195selection = getSelected(array, rank, isSorted_RK)
196selection
197-512.00000000000000
198array
199-2.0000000000000000, +4.0000000000000000, -8.0000000000000000, +16.000000000000000, -32.000000000000000, +64.000000000000000, -128.00000000000000, +256.00000000000000, -512.00000000000000
200
201
202lenArray = getUnifRand(5_IK, 9_IK)
203rank = getUnifRand(1_IK, lenArray)
204[lenArray, rank]
205+8, +1
206array = 'ParaMonte'
207array
208"ParaMonte"
209!Select the `rank`th smallest element with case-sensitivity (default behavior).
210selection = getSelected(array, rank)
211selection
212"M"
213array
214"ParaMonte"
215
216
217lenArray = getUnifRand(5_IK, 9_IK)
218rank = getUnifRand(1_IK, lenArray)
219[lenArray, rank]
220+5, +5
221array = 'ParaMonte'
222array
223"ParaMonte"
224!Select the `rank`th smallest element WITHOUT case-sensitivity via a custom-designed input comparison function.
225selection = getSelected(array, rank, isSorted_SK)
226selection
227"n"
228array
229"ParaMonte"
230
231
232!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233!Expedite the selection process for a partially sorted array via optional arguments `lb` or `ub`.
234!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
235
236
237rank = 3_IK
238array = [-3, -1, -2, 1, 2, 3] ! all elements after index `3` are sorted (`ub = 3_IK`).
239selection = getSelected(array, rank, ub = 3_IK) ! all elements after index `3` are sorted (`ub = 3_IK`).
240selection
241-1
242array
243-3, -1, -2, +1, +2, +3
244
245
Test:
test_pm_arraySelect
Todo:
Low Priority: This generic interface can be extended to higher-rank input objects.


Final Remarks


If you believe this algorithm or its documentation can be improved, we appreciate your contribution and help to edit this page's documentation and source file on GitHub.
For details on the naming abbreviations, see this page.
For details on the naming conventions, see this page.
This software is distributed under the MIT license with additional terms outlined below.

  1. If you use any parts or concepts from this library to any extent, please acknowledge the usage by citing the relevant publications of the ParaMonte library.
  2. If you regenerate any parts/ideas from this library in a programming environment other than those currently supported by this ParaMonte library (i.e., other than C, C++, Fortran, MATLAB, Python, R), please also ask the end users to cite this original ParaMonte library.

This software is available to the public under a highly permissive license.
Help us justify its continued development and maintenance by acknowledging its benefit to society, distributing it, and contributing to it.

Author:
Amir Shahmoradi, September 1, 2017, 12:00 AM, Institute for Computational Engineering and Sciences (ICES), The University of Texas Austin

Definition at line 546 of file pm_arraySelect.F90.


The documentation for this interface was generated from the following file: