Skip to content

Commit a11e2c5

Browse files
committed
feat: add stats/strided/smidrange
Ref: #4797 --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent ebe023b commit a11e2c5

33 files changed

+3472
-0
lines changed
+311
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,311 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2020 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# smidrange
22+
23+
> Calculate the [mid-range][mid-range] of a single-precision floating-point strided array.
24+
25+
<section class="intro">
26+
27+
The [**mid-range**][mid-range], or **mid-extreme**, is the arithmetic mean of the maximum and minimum values in a data set. The measure is the midpoint of the range and a measure of central tendency.
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<section class="usage">
34+
35+
## Usage
36+
37+
```javascript
38+
var smidrange = require( '@stdlib/stats/strided/smidrange' );
39+
```
40+
41+
#### smidrange( N, x, strideX )
42+
43+
Computes the [mid-range][mid-range] of a single-precision floating-point strided array `x`.
44+
45+
```javascript
46+
var Float32Array = require( '@stdlib/array/float32' );
47+
48+
var x = new Float32Array( [ 1.0, -2.0, 2.0 ] );
49+
50+
var v = smidrange( x.length, x, 1 );
51+
// returns 0.0
52+
```
53+
54+
The function has the following parameters:
55+
56+
- **N**: number of indexed elements.
57+
- **x**: input [`Float32Array`][@stdlib/array/float32].
58+
- **strideX**: stride length for `x`.
59+
60+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the [mid-range][mid-range] of every other element in `x`,
61+
62+
```javascript
63+
var Float32Array = require( '@stdlib/array/float32' );
64+
65+
var x = new Float32Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] );
66+
67+
var v = smidrange( 4, x, 2 );
68+
// returns 1.0
69+
```
70+
71+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
72+
73+
<!-- eslint-disable stdlib/capitalized-comments -->
74+
75+
```javascript
76+
var Float32Array = require( '@stdlib/array/float32' );
77+
78+
var x0 = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
79+
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
80+
81+
var v = smidrange( 4, x1, 2 );
82+
// returns 1.0
83+
```
84+
85+
#### smidrange.ndarray( N, x, strideX, offsetX )
86+
87+
Computes the [mid-range][mid-range] of a single-precision floating-point strided array using alternative indexing semantics.
88+
89+
```javascript
90+
var Float32Array = require( '@stdlib/array/float32' );
91+
92+
var x = new Float32Array( [ 1.0, -2.0, 2.0 ] );
93+
94+
var v = smidrange.ndarray( x.length, x, 1, 0 );
95+
// returns 0.0
96+
```
97+
98+
The function has the following additional parameters:
99+
100+
- **offsetX**: starting index for `x`.
101+
102+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to calculate the [mid-range][mid-range] for every other element in `x` starting from the second element
103+
104+
```javascript
105+
var Float32Array = require( '@stdlib/array/float32' );
106+
107+
var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
108+
109+
var v = smidrange.ndarray( 4, x, 2, 1 );
110+
// returns 1.0
111+
```
112+
113+
</section>
114+
115+
<!-- /.usage -->
116+
117+
<section class="notes">
118+
119+
## Notes
120+
121+
- If `N <= 0`, both functions return `NaN`.
122+
123+
</section>
124+
125+
<!-- /.notes -->
126+
127+
<section class="examples">
128+
129+
## Examples
130+
131+
<!-- eslint no-undef: "error" -->
132+
133+
```javascript
134+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
135+
var smidrange = require( '@stdlib/stats/strided/smidrange' );
136+
137+
var x = discreteUniform( 10, -50, 50, {
138+
'dtype': 'float32'
139+
});
140+
console.log( x );
141+
142+
var v = smidrange( x.length, x, 1 );
143+
console.log( v );
144+
```
145+
146+
</section>
147+
148+
<!-- /.examples -->
149+
150+
<!-- C interface documentation. -->
151+
152+
* * *
153+
154+
<section class="c">
155+
156+
## C APIs
157+
158+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
159+
160+
<section class="intro">
161+
162+
</section>
163+
164+
<!-- /.intro -->
165+
166+
<!-- C usage documentation. -->
167+
168+
<section class="usage">
169+
170+
### Usage
171+
172+
```c
173+
#include "stdlib/stats/strided/smidrange.h"
174+
```
175+
176+
#### stdlib_strided_smidrange( N, \*X, strideX )
177+
178+
Computes the [mid-range][mid-range] of a single-precision floating-point strided array `x`.
179+
180+
```c
181+
const float x[] = { 1.0f, -2.0f, 3.0f, 4.0f };
182+
183+
float v = stdlib_strided_smidrange( 4, x, 1 );
184+
// returns 2.5f
185+
```
186+
187+
The function accepts the following arguments:
188+
189+
- **N**: `[in] CBLAS_INT` number of indexed elements.
190+
- **X**: `[in] float*` input array.
191+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
192+
193+
```c
194+
float stdlib_strided_smidrange( const CBLAS_INT N, const float *X, const CBLAS_INT strideX );
195+
```
196+
197+
#### stdlib_strided_smidrange_ndarray( N, \*X, strideX, offsetX )
198+
199+
Computes the [mid-range][mid-range] of a single-precision floating-point strided array using alternative indexing semantics.
200+
201+
```c
202+
const float x[] = { 1.0f, -2.0f, 3.0f, 4.0f };
203+
204+
float v = stdlib_strided_smidrange_ndarray( 4, x, 1, 0 );
205+
// returns 2.5f
206+
```
207+
208+
The function accepts the following arguments:
209+
210+
- **N**: `[in] CBLAS_INT` number of indexed elements.
211+
- **X**: `[in] float*` input array.
212+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
213+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
214+
215+
```c
216+
float stdlib_strided_smidrange_ndarray( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
217+
```
218+
219+
</section>
220+
221+
<!-- /.usage -->
222+
223+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
224+
225+
<section class="notes">
226+
227+
</section>
228+
229+
<!-- /.notes -->
230+
231+
<!-- C API usage examples. -->
232+
233+
<section class="examples">
234+
235+
### Examples
236+
237+
```c
238+
#include "stdlib/stats/strided/smidrange.h"
239+
#include <stdio.h>
240+
241+
int main( void ) {
242+
// Create a strided array:
243+
const float x[] = { 1.0f, -2.0f, -3.0f, 4.0f, -5.0f, -6.0f, 7.0f, 8.0f };
244+
245+
// Specify the number of elements:
246+
const int N = 4;
247+
248+
// Specify the stride length:
249+
const int strideX = 2;
250+
251+
// Compute the mid-range:
252+
float v = stdlib_strided_smidrange( N, x, strideX );
253+
254+
// Print the result:
255+
printf( "mid-range: %f\n", v );
256+
}
257+
```
258+
259+
</section>
260+
261+
<!-- /.examples -->
262+
263+
</section>
264+
265+
<!-- /.c -->
266+
267+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
268+
269+
<section class="related">
270+
271+
* * *
272+
273+
## See Also
274+
275+
- <span class="package-name">[`@stdlib/stats/strided/dmidrange`][@stdlib/stats/strided/dmidrange]</span><span class="delimiter">: </span><span class="description">calculate the mid-range of a double-precision floating-point strided array.</span>
276+
- <span class="package-name">[`@stdlib/stats/strided/smax`][@stdlib/stats/strided/smax]</span><span class="delimiter">: </span><span class="description">calculate the maximum value of a single-precision floating-point strided array.</span>
277+
- <span class="package-name">[`@stdlib/stats/base/smean`][@stdlib/stats/base/smean]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a single-precision floating-point strided array.</span>
278+
- <span class="package-name">[`@stdlib/stats/strided/smin`][@stdlib/stats/strided/smin]</span><span class="delimiter">: </span><span class="description">calculate the minimum value of a single-precision floating-point strided array.</span>
279+
- <span class="package-name">[`@stdlib/stats/strided/srange`][@stdlib/stats/strided/srange]</span><span class="delimiter">: </span><span class="description">calculate the range of a single-precision floating-point strided array.</span>
280+
281+
</section>
282+
283+
<!-- /.related -->
284+
285+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
286+
287+
<section class="links">
288+
289+
[mid-range]: https://en.wikipedia.org/wiki/Mid-range
290+
291+
[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32
292+
293+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
294+
295+
<!-- <related-links> -->
296+
297+
[@stdlib/stats/strided/dmidrange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dmidrange
298+
299+
[@stdlib/stats/strided/smax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/smax
300+
301+
[@stdlib/stats/base/smean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/smean
302+
303+
[@stdlib/stats/strided/smin]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/smin
304+
305+
[@stdlib/stats/strided/srange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/srange
306+
307+
<!-- </related-links> -->
308+
309+
</section>
310+
311+
<!-- /.links -->

0 commit comments

Comments
 (0)