Skip to content

Commit 676c853

Browse files
committed
feat: add stats/strided/nanmin
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: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - 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 7005170 commit 676c853

File tree

15 files changed

+1753
-0
lines changed

15 files changed

+1753
-0
lines changed
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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+
# nanmin
22+
23+
> Calculate the minimum value of a strided array, ignoring `NaN` values.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var nanmin = require( '@stdlib/stats/strided/nanmin' );
37+
```
38+
39+
#### nanmin( N, x, strideX )
40+
41+
Computes the minimum value of a strided array, ignoring `NaN` values.
42+
43+
```javascript
44+
var x = [ 1.0, -2.0, NaN, 2.0 ];
45+
46+
var v = nanmin( x.length, x, 1 );
47+
// returns -2.0
48+
```
49+
50+
The function has the following parameters:
51+
52+
- **N**: number of indexed elements.
53+
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
54+
- **strideX**: stride length for `x`.
55+
56+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the minimum value of every other element in `x`,
57+
58+
```javascript
59+
var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, NaN, NaN ];
60+
61+
var v = nanmin( 5, x, 2 );
62+
// returns -2.0
63+
```
64+
65+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
66+
67+
<!-- eslint-disable stdlib/capitalized-comments -->
68+
69+
```javascript
70+
var Float64Array = require( '@stdlib/array/float64' );
71+
72+
var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, NaN, NaN, 4.0 ] );
73+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
74+
75+
var v = nanmin( 4, x1, 2 );
76+
// returns -2.0
77+
```
78+
79+
#### nanmin.ndarray( N, x, strideX, offsetX )
80+
81+
Computes the minimum value of a strided array, ignoring `NaN` values and using alternative indexing semantics.
82+
83+
```javascript
84+
var x = [ 1.0, -2.0, NaN, 2.0 ];
85+
86+
var v = nanmin.ndarray( x.length, x, 1, 0 );
87+
// returns -2.0
88+
```
89+
90+
The function has the following additional parameters:
91+
92+
- **offsetX**: starting index for `x`.
93+
94+
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 minimum value for every other element in `x` starting from the second element
95+
96+
```javascript
97+
var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, NaN, NaN, 2.0, 3.0, 4.0 ];
98+
99+
var v = nanmin.ndarray( 5, x, 2, 1 );
100+
// returns -2.0
101+
```
102+
103+
</section>
104+
105+
<!-- /.usage -->
106+
107+
<section class="notes">
108+
109+
## Notes
110+
111+
- If `N <= 0`, both functions return `NaN`.
112+
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
113+
- Depending on the environment, the typed versions ([`dnanmin`][@stdlib/stats/strided/dnanmin], [`snanmin`][@stdlib/stats/strided/snanmin], etc.) are likely to be significantly more performant.
114+
115+
</section>
116+
117+
<!-- /.notes -->
118+
119+
<section class="examples">
120+
121+
## Examples
122+
123+
<!-- eslint no-undef: "error" -->
124+
125+
```javascript
126+
var uniform = require( '@stdlib/random/base/uniform' );
127+
var filledarrayBy = require( '@stdlib/array/filled-by' );
128+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
129+
var nanmin = require( '@stdlib/stats/strided/nanmin' );
130+
131+
function rand() {
132+
if ( bernoulli( 0.8 ) < 1 ) {
133+
return NaN;
134+
}
135+
return uniform( -50.0, 50.0 );
136+
}
137+
138+
var x = filledarrayBy( 10, 'float64', rand );
139+
console.log( x );
140+
141+
var v = nanmin( x.length, x, 1 );
142+
console.log( v );
143+
```
144+
145+
</section>
146+
147+
<!-- /.examples -->
148+
149+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
150+
151+
<section class="related">
152+
153+
* * *
154+
155+
## See Also
156+
157+
- <span class="package-name">[`@stdlib/stats/strided/dnanmin`][@stdlib/stats/strided/dnanmin]</span><span class="delimiter">: </span><span class="description">calculate the minimum value of a double-precision floating-point strided array, ignoring NaN values.</span>
158+
- <span class="package-name">[`@stdlib/stats/strided/min`][@stdlib/stats/strided/min]</span><span class="delimiter">: </span><span class="description">calculate the minimum value of a strided array.</span>
159+
- <span class="package-name">[`@stdlib/stats/strided/nanmax`][@stdlib/stats/strided/nanmax]</span><span class="delimiter">: </span><span class="description">calculate the maximum value of a strided array, ignoring NaN values.</span>
160+
- <span class="package-name">[`@stdlib/stats/strided/snanmin`][@stdlib/stats/strided/snanmin]</span><span class="delimiter">: </span><span class="description">calculate the minimum value of a single-precision floating-point strided array, ignoring NaN values.</span>
161+
162+
</section>
163+
164+
<!-- /.related -->
165+
166+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
167+
168+
<section class="links">
169+
170+
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
171+
172+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
173+
174+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
175+
176+
<!-- <related-links> -->
177+
178+
[@stdlib/stats/strided/dnanmin]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dnanmin
179+
180+
[@stdlib/stats/strided/min]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/min
181+
182+
[@stdlib/stats/strided/nanmax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/nanmax
183+
184+
[@stdlib/stats/strided/snanmin]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/snanmin
185+
186+
<!-- </related-links> -->
187+
188+
</section>
189+
190+
<!-- /.links -->
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var uniform = require( '@stdlib/random/base/uniform' );
26+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
27+
var filledarrayBy = require( '@stdlib/array/filled-by' );
28+
var pow = require( '@stdlib/math/base/special/pow' );
29+
var pkg = require( './../package.json' ).name;
30+
var nanmin = require( './../lib/main.js' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Returns a random number.
37+
*
38+
* @private
39+
* @returns {number} random number
40+
*/
41+
function rand() {
42+
if ( bernoulli( 0.8 ) < 1 ) {
43+
return NaN;
44+
}
45+
return uniform( -10.0, 10.0 );
46+
}
47+
48+
/**
49+
* Creates a benchmark function.
50+
*
51+
* @private
52+
* @param {PositiveInteger} len - array length
53+
* @returns {Function} benchmark function
54+
*/
55+
function createBenchmark( len ) {
56+
var x = filledarrayBy( len, 'generic', rand );
57+
return benchmark;
58+
59+
function benchmark( b ) {
60+
var v;
61+
var i;
62+
63+
b.tic();
64+
for ( i = 0; i < b.iterations; i++ ) {
65+
v = nanmin( x.length, x, 1 );
66+
if ( isnan( v ) ) {
67+
b.fail( 'should not return NaN' );
68+
}
69+
}
70+
b.toc();
71+
if ( isnan( v ) ) {
72+
b.fail( 'should not return NaN' );
73+
}
74+
b.pass( 'benchmark finished' );
75+
b.end();
76+
}
77+
}
78+
79+
80+
// MAIN //
81+
82+
/**
83+
* Main execution sequence.
84+
*
85+
* @private
86+
*/
87+
function main() {
88+
var len;
89+
var min;
90+
var max;
91+
var f;
92+
var i;
93+
94+
min = 1; // 10^min
95+
max = 6; // 10^max
96+
97+
for ( i = min; i <= max; i++ ) {
98+
len = pow( 10, i );
99+
f = createBenchmark( len );
100+
bench( pkg+':len='+len, f );
101+
}
102+
}
103+
104+
main();

0 commit comments

Comments
 (0)