Skip to content

Commit 45b78a6

Browse files
committed
feat: add stats/strided/nanmin-by
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 41e9b04 commit 45b78a6

File tree

15 files changed

+2261
-0
lines changed

15 files changed

+2261
-0
lines changed
Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
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+
# nanminBy
22+
23+
> Calculate the minimum value of a strided array via a callback function, ignoring `NaN` values.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var nanminBy = require( '@stdlib/stats/strided/nanmin-by' );
31+
```
32+
33+
#### nanminBy( N, x, strideX, clbk\[, thisArg] )
34+
35+
Computes the minimum value of a strided array via a callback function, ignoring `NaN` values.
36+
37+
```javascript
38+
function accessor( v ) {
39+
return v * 2.0;
40+
}
41+
42+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, NaN, 0.0, -1.0, -3.0, NaN ];
43+
44+
var v = nanminBy( x.length, x, 1, accessor );
45+
// returns -10.0
46+
```
47+
48+
The function has the following parameters:
49+
50+
- **N**: number of indexed elements.
51+
- **x**: input [`Array`][mdn-array], [`typed array`][mdn-typed-array], or an array-like object (excluding strings and functions).
52+
- **strideX**: stride length.
53+
- **clbk**: callback function.
54+
- **thisArg**: execution context (_optional_).
55+
56+
The invoked callback is provided four arguments:
57+
58+
- **value**: array element.
59+
- **aidx**: array index.
60+
- **sidx**: strided index (`offset + aidx*stride`).
61+
- **array**: input array/collection.
62+
63+
To set the callback execution context, provide a `thisArg`.
64+
65+
```javascript
66+
function accessor( v ) {
67+
this.count += 1;
68+
return v * 2.0;
69+
}
70+
71+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, NaN, 0.0, -1.0, -3.0, NaN ];
72+
73+
var context = {
74+
'count': 0
75+
};
76+
77+
var v = nanminBy( x.length, x, 1, accessor, context );
78+
// returns -10.0
79+
80+
var cnt = context.count;
81+
// returns 10
82+
```
83+
84+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to access every other element
85+
86+
```javascript
87+
function accessor( v ) {
88+
return v * 2.0;
89+
}
90+
91+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0, NaN, NaN ];
92+
93+
var v = nanminBy( 5, x, 2, accessor );
94+
// returns -4.0
95+
```
96+
97+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
98+
99+
```javascript
100+
var Float64Array = require( '@stdlib/array/float64' );
101+
102+
function accessor( v ) {
103+
return v * 2.0;
104+
}
105+
106+
// Initial array...
107+
var x0 = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
108+
109+
// Create an offset view...
110+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
111+
112+
// Access every other element...
113+
var v = nanminBy( 3, x1, 2, accessor );
114+
// returns -12.0
115+
```
116+
117+
#### nanminBy.ndarray( N, x, strideX, offsetX, clbk\[, thisArg] )
118+
119+
Computes the minimum value of a strided array via a callback function, ignoring `NaN` values and using alternative indexing semantics.
120+
121+
```javascript
122+
function accessor( v ) {
123+
return v * 2.0;
124+
}
125+
126+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, NaN, 0.0, -1.0, -3.0, NaN ];
127+
128+
var v = nanminBy.ndarray( x.length, x, 1, 0, accessor );
129+
// returns -10.0
130+
```
131+
132+
The function has the following additional parameters:
133+
134+
- **offsetX**: starting index.
135+
136+
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 access only the last three elements of `x`
137+
138+
```javascript
139+
function accessor( v ) {
140+
return v * 2.0;
141+
}
142+
143+
var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ];
144+
145+
var v = nanminBy.ndarray( 3, x, 1, x.length-3, accessor );
146+
// returns -12.0
147+
```
148+
149+
</section>
150+
151+
<!-- /.usage -->
152+
153+
<section class="notes">
154+
155+
## Notes
156+
157+
- If `N <= 0`, both functions return `NaN`.
158+
- A provided callback function should return a numeric value.
159+
- If a provided callback function returns `NaN`, the value is ignored.
160+
- If a provided callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is ignored.
161+
- 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]).
162+
- When possible, prefer using [`dnanmin`][@stdlib/stats/strided/dnanmin], [`snanmin`][@stdlib/stats/strided/snanmin], and/or [`nanmin`][@stdlib/stats/base/nanmin], as, depending on the environment, these interfaces are likely to be significantly more performant.
163+
164+
</section>
165+
166+
<!-- /.notes -->
167+
168+
<section class="examples">
169+
170+
## Examples
171+
172+
<!-- eslint no-undef: "error" -->
173+
174+
```javascript
175+
var uniform = require( '@stdlib/random/base/uniform' );
176+
var filledarrayBy = require( '@stdlib/array/filled-by' );
177+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
178+
var nanminBy = require( '@stdlib/stats/strided/nanmin-by' );
179+
180+
function rand() {
181+
if ( bernoulli( 0.8 )< 0.2 ) {
182+
return NaN;
183+
}
184+
return uniform( -50, 50 );
185+
}
186+
187+
function accessor( v ) {
188+
return v * 2.0;
189+
}
190+
191+
var x = filledarrayBy( 10, 'float64', rand );
192+
console.log( x );
193+
194+
var v = nanminBy( x.length, x, 1, accessor );
195+
console.log( v );
196+
```
197+
198+
</section>
199+
200+
<!-- /.examples -->
201+
202+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
203+
204+
<section class="related">
205+
206+
* * *
207+
208+
## See Also
209+
210+
- <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>
211+
- <span class="package-name">[`@stdlib/stats/strided/min-by`][@stdlib/stats/strided/min-by]</span><span class="delimiter">: </span><span class="description">calculate the minimum value of a strided array via a callback function.</span>
212+
- <span class="package-name">[`@stdlib/stats/strided/nanmax-by`][@stdlib/stats/strided/nanmax-by]</span><span class="delimiter">: </span><span class="description">calculate the maximum value of a strided array via a callback function, ignoring NaN values.</span>
213+
- <span class="package-name">[`@stdlib/stats/base/nanmin`][@stdlib/stats/base/nanmin]</span><span class="delimiter">: </span><span class="description">calculate the minimum value of a strided array, ignoring NaN values.</span>
214+
- <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>
215+
216+
</section>
217+
218+
<!-- /.related -->
219+
220+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
221+
222+
<section class="links">
223+
224+
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
225+
226+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
227+
228+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
229+
230+
<!-- <related-links> -->
231+
232+
[@stdlib/stats/strided/dnanmin]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dnanmin
233+
234+
[@stdlib/stats/strided/min-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/min-by
235+
236+
[@stdlib/stats/strided/nanmax-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/nanmax-by
237+
238+
[@stdlib/stats/base/nanmin]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/nanmin
239+
240+
[@stdlib/stats/strided/snanmin]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/snanmin
241+
242+
<!-- </related-links> -->
243+
244+
</section>
245+
246+
<!-- /.links -->
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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 uniform = require( '@stdlib/random/base/uniform' );
25+
var filledarrayBy = require( '@stdlib/array/filled-by' );
26+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
27+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
28+
var pow = require( '@stdlib/math/base/special/pow' );
29+
var pkg = require( './../package.json' ).name;
30+
var nanminBy = require( './../lib/main.js' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Accessor function.
37+
*
38+
* @private
39+
* @param {number} value - array element
40+
* @returns {number} accessed value
41+
*/
42+
function accessor( value ) {
43+
return value * 2.0;
44+
}
45+
46+
/**
47+
* Returns a random number.
48+
*
49+
* @private
50+
* @returns {number} random number
51+
*/
52+
function rand() {
53+
if ( bernoulli( 0.8 ) < 1 ) {
54+
return NaN;
55+
}
56+
return uniform( -50.0, 50.0 );
57+
}
58+
59+
/**
60+
* Create a benchmark function.
61+
*
62+
* @private
63+
* @param {PositiveInteger} len - array length
64+
* @returns {Function} benchmark function
65+
*/
66+
function createBenchmark( len ) {
67+
var x = filledarrayBy( len, 'generic', rand );
68+
return benchmark;
69+
70+
function benchmark( b ) {
71+
var y;
72+
var i;
73+
74+
b.tic();
75+
for ( i = 0; i < b.iterations; i++ ) {
76+
y = nanminBy( x.length, x, 1, accessor );
77+
if ( isnan( y ) ) {
78+
b.fail( 'should not return NaN' );
79+
}
80+
}
81+
b.toc();
82+
if ( isnan( y ) ) {
83+
b.fail( 'should not return NaN' );
84+
}
85+
b.pass( 'benchmark finished' );
86+
b.end();
87+
}
88+
}
89+
90+
91+
// MAIN //
92+
93+
function main() {
94+
var len;
95+
var min;
96+
var max;
97+
var f;
98+
var i;
99+
100+
min = 1; // 10^min
101+
max = 6; // 10^max
102+
103+
for ( i = min; i <= max; i++ ) {
104+
len = pow( 10, i );
105+
f = createBenchmark( len );
106+
bench( pkg+':len='+len, f );
107+
}
108+
}
109+
110+
main();

0 commit comments

Comments
 (0)