Skip to content

Commit cfd1ccb

Browse files
committed
feat: add stats/strided/nanminabs
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 dcc45ac commit cfd1ccb

File tree

15 files changed

+1816
-0
lines changed

15 files changed

+1816
-0
lines changed
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
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+
# nanminabs
22+
23+
> Calculate the minimum absolute 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 nanminabs = require( '@stdlib/stats/strided/nanminabs' );
37+
```
38+
39+
#### nanminabs( N, x, strideX )
40+
41+
Computes the minimum absolute 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 = nanminabs( x.length, x, 1 );
47+
// returns 1.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 absolute 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 = nanminabs( 5, x, 2 );
62+
// returns 1.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 = nanminabs( 4, x1, 2 );
76+
// returns 1.0
77+
```
78+
79+
#### nanminabs.ndarray( N, x, strideX, offsetX )
80+
81+
Computes the minimum absolute 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 = nanminabs.ndarray( x.length, x, 1, 0 );
87+
// returns 1.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 absolute 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 = nanminabs.ndarray( 5, x, 2, 1 );
100+
// returns 1.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 ([`dnanminabs`][@stdlib/stats/strided/dnanminabs], [`snanminabs`][@stdlib/stats/strided/snanminabs], 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 nanminabs = require( '@stdlib/stats/strided/nanminabs' );
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 = nanminabs( 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/dnanminabs`][@stdlib/stats/strided/dnanminabs]</span><span class="delimiter">: </span><span class="description">calculate the minimum absolute value of a double-precision floating-point strided array, ignoring NaN values.</span>
158+
- <span class="package-name">[`@stdlib/stats/strided/minabs`][@stdlib/stats/strided/minabs]</span><span class="delimiter">: </span><span class="description">calculate the minimum absolute value of a strided array.</span>
159+
- <span class="package-name">[`@stdlib/stats/strided/nanmaxabs`][@stdlib/stats/strided/nanmaxabs]</span><span class="delimiter">: </span><span class="description">calculate the maximum absolute value of a strided array, ignoring NaN values.</span>
160+
- <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>
161+
- <span class="package-name">[`@stdlib/stats/strided/snanminabs`][@stdlib/stats/strided/snanminabs]</span><span class="delimiter">: </span><span class="description">calculate the minimum absolute value of a single-precision floating-point strided array, ignoring NaN values.</span>
162+
163+
</section>
164+
165+
<!-- /.related -->
166+
167+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
168+
169+
<section class="links">
170+
171+
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
172+
173+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
174+
175+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
176+
177+
<!-- <related-links> -->
178+
179+
[@stdlib/stats/strided/dnanminabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dnanminabs
180+
181+
[@stdlib/stats/strided/minabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/minabs
182+
183+
[@stdlib/stats/strided/nanmaxabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/nanmaxabs
184+
185+
[@stdlib/stats/base/nanmin]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/nanmin
186+
187+
[@stdlib/stats/strided/snanminabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/snanminabs
188+
189+
<!-- </related-links> -->
190+
191+
</section>
192+
193+
<!-- /.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 uniform = require( '@stdlib/random/base/uniform' );
25+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
26+
var filledarrayBy = require( '@stdlib/array/filled-by' );
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 nanminabs = require( './../lib/main.js' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Returns a random value.
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 = nanminabs( 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)