Skip to content

Commit 487c973

Browse files
committed
change "a" to "array" in documentation as suggested by jvdp1
1 parent 7970636 commit 487c973

File tree

1 file changed

+40
-37
lines changed

1 file changed

+40
-37
lines changed

doc/specs/stdlib_selection.md

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ title: Selection Procedures
1010

1111
Suppose you wish to find the value of the kth-smallest entry in an array, or
1212
the index of that value. While it could be done by sorting the whole array
13-
using `[[stdlib_sorting(module):sort(interface)]]` or `[[stdlib_sorting(module):sort_index(interface)]]` from `[[stdlib_sorting(module)]]` and then finding the k-th
14-
entry, that would require O(N x LOG(N)) time. However selection of a single
15-
entry can be done in O(N) time, which is much faster for large arrays. This is
16-
useful, for example, to quickly find the median of an array, or some other
17-
percentile.
13+
using `[[stdlib_sorting(module):sort(interface)]]` or
14+
`[[stdlib_sorting(module):sort_index(interface)]]` from
15+
`[[stdlib_sorting(module)]]` and then finding the k-th entry, that would
16+
require O(N x LOG(N)) time. However selection of a single entry can be done in
17+
O(N) time, which is much faster for large arrays. This is useful, for example,
18+
to quickly find the median of an array, or some other percentile.
1819

1920
The Fortran Standard Library therefore provides a module, `stdlib_selection`,
2021
which implements selection algorithms.
@@ -86,49 +87,50 @@ Experimental
8687

8788
##### Description
8889

89-
Returns the k-th smallest value of array `a(:)`, and also partially sorts `a(:)`
90-
such that `all(a(1:k) <= a(k))` and `all(a(k) <= a((k+1):size(a)))`
90+
Returns the k-th smallest value of `array(:)`, and also partially sorts `array(:)`
91+
such that `all(array(1:k) <= array(k))` and `all(array(k) <= array((k+1):size(array)))`
9192

9293
##### Syntax
9394

94-
`call [[stdlib_selection(module):select(interface)]]( a, k, kth_smallest [, left, right ] )`
95+
`call [[stdlib_selection(module):select(interface)]]( array, k, kth_smallest [, left, right ] )`
9596

9697
##### Class
9798

9899
Generic subroutine.
99100

100101
##### Arguments
101102

102-
`a` : shall be a rank one array of any of the types:
103+
`array` : shall be a rank one array of any of the types:
103104
`integer(int8)`, `integer(int16)`, `integer(int32)`, `integer(int64)`,
104105
`real(sp)`, `real(dp)`, `real(qp)`. It is an `intent(inout)` argument. On input it is
105106
the array in which we search for the kth smallest entry. On return its elements
106107
will be partially sorted such that:
107-
`all( a(1:k-1) <= a(k) )` and `all(a(k) <= a(k+1:size(a)))`.
108+
`all( array(1:k-1) <= array(k) )` and `all(array(k) <= array(k+1:size(array)))`.
108109

109110
`k`: shall be a scalar with any of the types:
110111
`integer(int8)`, `integer(int16)`, `integer(int32)`, `integer(int64)`. It
111-
is an `intent(in)` argument. We search for the `k`-th smallest entry of `a(:)`.
112+
is an `intent(in)` argument. We search for the `k`-th smallest entry of `array(:)`.
112113

113-
`kth_smallest`: shall be a scalar with the same type as `a`. On return it contains
114-
the k-th smallest entry of `a(:)`.
114+
`kth_smallest`: shall be a scalar with the same type as `array`. On return it contains
115+
the k-th smallest entry of `array(:)`.
115116

116117
`left` (optional): shall be a scalar with the same type as `k`. It is an `intent(in)`
117118
argument. If specified then we assume the k-th smallest value is definitely contained
118-
in `a(left:size(a))`. If not present it is 1. This is typically useful if multiple calls
119-
to `select` are made, because the partial sorting of `a` implies constraints on where
119+
in `array(left:size(array))`. If not present it is 1. This is typically useful if multiple calls
120+
to `select` are made, because the partial sorting of `array` implies constraints on where
120121
we need to search.
121122

122123
`right` (optional): shall be a scalar with the same type as `k`. It is an `intent(in)`
123124
argument. If specified then we assume the k-th smallest value is definitely contained
124-
in `a(1:right)`. If not present it is `size(a)`. This is typically useful if multiple calls
125-
to `select` are made, because the partial sorting of `a` implies constraints on where
125+
in `array(1:right)`. If not present it is `size(a)`. This is typically useful if multiple calls
126+
to `select` are made, because the partial sorting of `array` implies constraints on where
126127
we need to search.
127128

128129
##### Notes
129130

130-
Selection of a single value should have runtime of O(`size(a)`), so it is asymptotically faster
131-
than sorting `a` entirely.
131+
Selection of a single value should have runtime of O(`size(array)`), so it is
132+
asymptotically faster than sorting `array` entirely. The test program at the the
133+
end of this document shows that is the case.
132134

133135
##### Example
134136

@@ -170,58 +172,59 @@ Experimental
170172

171173
##### Description
172174

173-
Returns the index of the k-th smallest value of array `a(:)`, and also partially sorts
174-
the index-array `indx(:)` such that `all(a(indx(1:k)) <= a(indx(k)))` and
175-
`all(a(indx(k)) <= a(indx((k+1):size(a))))`
175+
Returns the index of the k-th smallest value of `array(:)`, and also partially sorts
176+
the index-array `indx(:)` such that `all(array(indx(1:k)) <= array(indx(k)))` and
177+
`all(array(indx(k)) <= array(indx((k+1):size(array))))`
176178

177179
##### Syntax
178180

179-
`call [[stdlib_selection(module):arg_select(interface)]]( a, indx, k, kth_smallest [, left, right ] )`
181+
`call [[stdlib_selection(module):arg_select(interface)]]( array, indx, k, kth_smallest [, left, right ] )`
180182

181183
##### Class
182184

183185
Generic subroutine.
184186

185187
##### Arguments
186188

187-
`a` : shall be a rank one array of any of the types:
189+
`array` : shall be a rank one array of any of the types:
188190
`integer(int8)`, `integer(int16)`, `integer(int32)`, `integer(int64)`,
189191
`real(sp)`, `real(dp)`, `real(qp)`. It is an `intent(in)` argument. On input it is
190192
the array in which we search for the kth smallest entry.
191193

192-
`indx`: shall be a rank one array with the same size as `a`, containing integers
194+
`indx`: shall be a rank one array with the same size as `array`, containing all integers
193195
from `1:size(a)` in any order. It is of any of the types:
194196
`integer(int8)`, `integer(int16)`, `integer(int32)`, `integer(int64)`. It is an
195-
`intent(inout)` argument. On return its elements will define a partial sorting of `a(:)` such that:
196-
`all( a(indx(1:k-1)) <= a(indx(k)) )` and `all(a(indx(k)) <= a(indx(k+1:size(a))))`.
197+
`intent(inout)` argument. On return its elements will define a partial sorting of `array(:)` such that:
198+
`all( array(indx(1:k-1)) <= array(indx(k)) )` and `all(array(indx(k)) <= array(indx(k+1:size(array))))`.
197199

198200
`k`: shall be a scalar with the same type as `indx`. It is an `intent(in)`
199-
argument. We search for the `k`-th smallest entry of `a(:)`.
201+
argument. We search for the `k`-th smallest entry of `array(:)`.
200202

201203
`kth_smallest`: a scalar with the same type as `indx`. It is an `intent(out)` argument,
202-
and on return it contains the index of the k-th smallest entry of `a(:)`.
204+
and on return it contains the index of the k-th smallest entry of `array(:)`.
203205

204206
`left` (optional): shall be a scalar with the same type as `k`. It is an `intent(in)`
205207
argument. If specified then we assume the k-th smallest value is definitely contained
206-
in `a(indx(left:size(a)))`. If not present it is 1. This is typically useful if multiple calls
208+
in `array(indx(left:size(array)))`. If not present it is 1. This is typically useful if multiple calls
207209
to `arg_select` are made, because the partial sorting of `indx` implies constraints on where
208210
we need to search.
209211

210212
`right` (optional): shall be a scalar with the same type as `k`. It is an `intent(in)`
211213
argument. If specified then we assume the k-th smallest value is definitely contained
212-
in `a(indx(1:right))`. If not present it is `size(a)`. This is typically useful if multiple calls
214+
in `array(indx(1:right))`. If not present it is `size(array)`. This is typically useful if multiple calls
213215
to `arg_select` are made, because the reordering of `indx` implies constraints on
214216
where we need to search.
215217

216218
##### Notes
217219

218-
`arg_select` does not modify `a`, unlike `select`.
220+
`arg_select` does not modify `array`, unlike `select`.
219221

220-
While it is essential that that `indx` contains the integers `1:size(a)` (in any
222+
While it is essential that that `indx` contains the integers `1:size(array)` (in any
221223
order), the code does not check for this.
222224

223-
Selection of a single value should have runtime of O(`size(a)`), so it is asymptotically faster
224-
than sorting `a` entirely.
225+
Selection of a single value should have runtime of O(`size(array)`), so it is
226+
asymptotically faster than sorting `array` entirely. The test program at the end of
227+
these documents confirms that is the case.
225228

226229

227230
##### Example
@@ -264,7 +267,7 @@ than sorting `a` entirely.
264267
The following program compares the timings of `select` and `arg_select` for
265268
computing the median of an array, vs using `sort` from stdlib. In theory we
266269
should see a speed improvement with the selection routines which grows like
267-
LOG(size(array)).
270+
LOG(size(`array`)).
268271

269272
```fortran
270273
program selection_vs_sort
@@ -347,7 +350,7 @@ LOG(size(array)).
347350
end program
348351
```
349352

350-
The results seem consistent with expectations when the array is large; the program prints:
353+
The results seem consistent with expectations when the `array` is large; the program prints:
351354
```
352355
select ; N= 1 ; PASS; Relative-speedup-vs-sort: 2.11456394
353356
arg_select; N= 1 ; PASS; Relative-speedup-vs-sort: 3.48637915

0 commit comments

Comments
 (0)