Skip to content

Commit 2771b88

Browse files
Add incrnanminmaxabs: incremental min/max absolute values ignoring NaNs
1 parent 736e165 commit 2771b88

File tree

10 files changed

+890
-0
lines changed

10 files changed

+890
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2018 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+
# incrnanminmaxabs
22+
23+
> Compute minimum and maximum absolute values incrementally, ignoring `NaN` values.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var incrnanminmaxabs = require( '@stdlib/stats/incr/nanminmaxabs' );
31+
```
32+
33+
#### incrnanminmaxabs( \[out] )
34+
35+
Returns an accumulator `function` which incrementally computes minimum and maximum absolute values while ignoring `NaN` values.
36+
37+
```javascript
38+
var accumulator = incrnanminmaxabs();
39+
```
40+
41+
By default, the returned accumulator `function` returns the minimum and maximum absolute values as a two-element `array`. To avoid unnecessary memory allocation, the function supports providing an output (destination) object.
42+
43+
```javascript
44+
var Float64Array = require( '@stdlib/array/float64' );
45+
46+
var accumulator = incrnanminmaxabs( new Float64Array( 2 ) );
47+
```
48+
49+
#### accumulator( \[x] )
50+
51+
If provided an input value `x`, the accumulator function returns updated minimum and maximum absolute values, ignoring `NaN`. If not provided an input value `x`, the accumulator function returns the current minimum and maximum absolute values.
52+
53+
```javascript
54+
var accumulator = incrnanminmaxabs();
55+
56+
var mm = accumulator();
57+
// returns null
58+
59+
mm = accumulator( 2.0 );
60+
// returns [ 2.0, 2.0 ]
61+
62+
mm = accumulator( NaN );
63+
// returns [ 2.0, 2.0 ] (ignores NaN)
64+
65+
mm = accumulator( 1.0 );
66+
// returns [ 1.0, 2.0 ]
67+
68+
mm = accumulator( 3.0 );
69+
// returns [ 1.0, 3.0 ]
70+
71+
mm = accumulator( -7.0 );
72+
// returns [ 1.0, 7.0 ]
73+
74+
mm = accumulator();
75+
// returns [ 1.0, 7.0 ]
76+
```
77+
78+
</section>
79+
80+
<!-- /.usage -->
81+
82+
<section class="notes">
83+
84+
## Notes
85+
86+
- `NaN` values are ignored in calculations. This ensures `NaN` does not contaminate future results.
87+
- If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function.
88+
89+
</section>
90+
91+
<!-- /.notes -->
92+
93+
<section class="examples">
94+
95+
## Examples
96+
97+
<!-- eslint no-undef: "error" -->
98+
99+
```javascript
100+
var randu = require( '@stdlib/random/base/randu' );
101+
var incrnanminmaxabs = require( '@stdlib/stats/incr/nanminmaxabs' );
102+
103+
var accumulator;
104+
var v;
105+
var i;
106+
107+
// Initialize an accumulator:
108+
accumulator = incrnanminmaxabs();
109+
110+
// For each simulated datum, update the minimum and maximum absolute values...
111+
for ( i = 0; i < 100; i++ ) {
112+
v = ( randu()*100.0 ) - 50.0;
113+
if ( randu() < 0.1 ) {
114+
v = NaN;
115+
}
116+
accumulator( v );
117+
}
118+
console.log( accumulator() );
119+
```
120+
121+
</section>
122+
123+
<!-- /.examples -->
124+
125+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
126+
127+
<section class="related">
128+
129+
* * *
130+
131+
## See Also
132+
133+
- <span class="package-name">[`@stdlib/stats/incr/maxabs`][@stdlib/stats/incr/maxabs]</span><span class="delimiter">: </span><span class="description">compute a maximum absolute value incrementally.</span>
134+
- <span class="package-name">[`@stdlib/stats/incr/minabs`][@stdlib/stats/incr/minabs]</span><span class="delimiter">: </span><span class="description">compute a minimum absolute value incrementally.</span>
135+
- <span class="package-name">[`@stdlib/stats/incr/minmax`][@stdlib/stats/incr/minmax]</span><span class="delimiter">: </span><span class="description">compute a minimum and maximum incrementally.</span>
136+
- <span class="package-name">[`@stdlib/stats/incr/mminmaxabs`][@stdlib/stats/incr/mminmaxabs]</span><span class="delimiter">: </span><span class="description">compute moving minimum and maximum absolute values incrementally.</span>
137+
138+
</section>
139+
140+
<!-- /.related -->
141+
142+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
143+
144+
<section class="links">
145+
146+
<!-- <related-links> -->
147+
148+
[@stdlib/stats/incr/maxabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/maxabs
149+
150+
[@stdlib/stats/incr/minabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/minabs
151+
152+
[@stdlib/stats/incr/minmax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/minmax
153+
154+
[@stdlib/stats/incr/mminmaxabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mminmaxabs
155+
156+
<!-- </related-links> -->
157+
158+
</section>
159+
160+
<!-- /.links -->
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"use strict";
2+
3+
// MODULES //
4+
5+
var bench = require("@stdlib/bench");
6+
var randu = require("@stdlib/random/base/randu");
7+
var isnan = require("@stdlib/math/base/assert/is-nan");
8+
var pkg = require("./../package.json").name;
9+
var incrnanminmaxabs = require("./../lib");
10+
11+
// MAIN //
12+
13+
bench(pkg, function benchmark(b) {
14+
var f;
15+
var i;
16+
b.tic();
17+
for (i = 0; i < b.iterations; i++) {
18+
f = incrnanminmaxabs();
19+
if (typeof f !== "function") {
20+
b.fail("should return a function");
21+
}
22+
}
23+
b.toc();
24+
if (typeof f !== "function") {
25+
b.fail("should return a function");
26+
}
27+
b.pass("benchmark finished");
28+
b.end();
29+
});
30+
31+
bench(pkg + "::accumulator", function benchmark(b) {
32+
var acc;
33+
var v;
34+
var i;
35+
36+
acc = incrnanminmaxabs();
37+
38+
b.tic();
39+
for (i = 0; i < b.iterations; i++) {
40+
v = randu() > 0.1 ? randu() - 0.5 : NaN; // Introduce some NaNs
41+
acc(v);
42+
}
43+
v = acc();
44+
b.toc();
45+
46+
// Ensure NaN values are ignored unless all inputs were NaN
47+
if (isnan(v[0]) && isnan(v[1]) && i > 0) {
48+
b.fail("should not return NaN unless all inputs were NaN");
49+
}
50+
51+
b.pass("benchmark finished");
52+
b.end();
53+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
{{alias}}( [out] )
3+
Returns an accumulator function which incrementally computes a minimum and
4+
maximum absolute value.
5+
6+
If provided a value, the accumulator function returns an updated minimum and
7+
maximum. If not provided a value, the accumulator function returns the
8+
current minimum and maximum.
9+
10+
If provided `NaN`, the accumulator ignores it and does not update the minimum or maximum values.
11+
12+
13+
Parameters
14+
----------
15+
out: Array|TypedArray (optional)
16+
Output array.
17+
18+
Returns
19+
-------
20+
acc: Function
21+
Accumulator function.
22+
23+
Examples
24+
--------
25+
> var accumulator = {{alias}}();
26+
> var mm = accumulator()
27+
null
28+
> mm = accumulator( 2.0 )
29+
[ 2.0, 2.0 ]
30+
> mm = accumulator( -5.0 )
31+
[ 2.0, 5.0 ]
32+
> mm = accumulator( 3.0 )
33+
[ 2.0, 5.0 ]
34+
> mm = accumulator( 5.0 )
35+
[ 2.0, 5.0 ]
36+
> mm = accumulator()
37+
[ 2.0, 5.0 ]
38+
39+
See Also
40+
--------
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { ArrayLike } from '@stdlib/types/array';
24+
25+
/**
26+
* If provided a value, the accumulator function returns updated absolute minimum and maximum values. If not provided a value, the accumulator function returns the current minimum and maximum values.
27+
*
28+
* ## Notes
29+
*
30+
* - If provided `NaN`, the minimum and maximum values are equal to `NaN` for all future invocations.
31+
*
32+
* @param x - input value
33+
* @returns output array or null
34+
*/
35+
type accumulator = ( x?: number ) => ArrayLike<number> | null;
36+
37+
/**
38+
* Returns an accumulator function which incrementally computes minimum and maximum absolute values.
39+
*
40+
* @param out - output array
41+
* @returns accumulator function
42+
*
43+
* @example
44+
* var accumulator = incrnanminmaxabs();
45+
*
46+
* var mm = accumulator();
47+
* // returns null
48+
*
49+
* mm = accumulator( 2.0 );
50+
* // returns [ 2.0, 2.0 ]
51+
*
52+
* mm = accumulator( -5.0 );
53+
* // returns [ 2.0, 5.0 ]
54+
*
55+
* mm = accumulator( 3.0 );
56+
* // returns [ 2.0, 5.0 ]
57+
*
58+
* mm = accumulator( 5.0 );
59+
* // returns [ 2.0, 5.0 ]
60+
*
61+
* mm = accumulator();
62+
* // returns [ 2.0, 5.0 ]
63+
*/
64+
declare function incrnanminmaxabs( out?: ArrayLike<number> ): accumulator;
65+
66+
67+
// EXPORTS //
68+
69+
export = incrnanminmaxabs;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 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+
import incrnanminmaxabs from './index';
20+
21+
22+
// TESTS //
23+
24+
// The function returns an accumulator function...
25+
{
26+
incrnanminmaxabs(); // $ExpectType accumulator
27+
const out = [ 0.0, 0.0 ];
28+
incrnanminmaxabs( out ); // $ExpectType accumulator
29+
}
30+
31+
// The compiler throws an error if the function is provided an argument that is not an array-like object of numbers...
32+
{
33+
incrnanminmaxabs( '5' ); // $ExpectError
34+
incrnanminmaxabs( 5 ); // $ExpectError
35+
incrnanminmaxabs( true ); // $ExpectError
36+
incrnanminmaxabs( false ); // $ExpectError
37+
incrnanminmaxabs( null ); // $ExpectError
38+
incrnanminmaxabs( {} ); // $ExpectError
39+
incrnanminmaxabs( ( x: number ): number => x ); // $ExpectError
40+
}
41+
42+
// The function returns an accumulator function which returns an accumulated result...
43+
{
44+
const acc = incrnanminmaxabs();
45+
46+
acc(); // $ExpectType ArrayLike<number> | null
47+
acc( 3.14 ); // $ExpectType ArrayLike<number> | null
48+
}
49+
50+
// The compiler throws an error if the returned accumulator function is provided invalid arguments...
51+
{
52+
const acc = incrnanminmaxabs();
53+
54+
acc( '5' ); // $ExpectError
55+
acc( true ); // $ExpectError
56+
acc( false ); // $ExpectError
57+
acc( null ); // $ExpectError
58+
acc( [] ); // $ExpectError
59+
acc( {} ); // $ExpectError
60+
acc( ( x: number ): number => x ); // $ExpectError
61+
}

0 commit comments

Comments
 (0)