Skip to content

Commit 62d5d2e

Browse files
committed
Implemented nanmminmaxabs
1 parent ddc2e98 commit 62d5d2e

File tree

10 files changed

+1290
-0
lines changed

10 files changed

+1290
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# incrmminmaxabs
22+
23+
> Compute moving minimum and maximum absolute values incrementally.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var incrnanmminmaxabs = require( '@stdlib/stats/incr/mminmaxabs' );
31+
```
32+
33+
#### incrnanmminmaxabs( \[out,] window )
34+
35+
Returns an accumulator `function` which incrementally computes moving minimum and maximum absolute values, ignoring `NaN` values. The `window` parameter defines the number of values over which to compute moving minimum and maximum absolute values.
36+
37+
```javascript
38+
var accumulator = incrnanmminmaxabs( 3 );
39+
```
40+
41+
By default, the returned accumulator `function` returns the minimum and maximum 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 = incrnanmminmaxabs( new Float64Array( 2 ), 3 );
47+
```
48+
49+
#### accumulator( \[x] )
50+
51+
If provided an input value `x`, the accumulator function returns updated minimum and maximum absolute values. If not provided an input value `x`, the accumulator function returns the current minimum and maximum absolute values.
52+
53+
```javascript
54+
var accumulator = incrnanmminmaxabs( 3 );
55+
56+
var mm = accumulator();
57+
// returns null
58+
59+
// Fill the window...
60+
mm = accumulator( 2.0 ); // [2.0]
61+
// returns [ 2.0, 2.0 ]
62+
63+
mm = accumulator( 1.0 ); // [2.0, 1.0]
64+
// returns [ 1.0, 2.0 ]
65+
66+
mm = accumulator( 3.0 ); // [2.0, 1.0, 3.0]
67+
// returns [ 1.0, 3.0 ]
68+
69+
// Window begins sliding...
70+
mm = accumulator( -7.0 ); // [1.0, 3.0, -7.0]
71+
// returns [ 1.0, 7.0 ]
72+
73+
mm = accumulator( -5.0 ); // [3.0, -7.0, -5.0]
74+
// returns [ 3.0, 7.0 ]
75+
76+
mm = accumulator();
77+
// returns [ 3.0, 7.0 ]
78+
```
79+
80+
</section>
81+
82+
<!-- /.usage -->
83+
84+
<section class="notes">
85+
86+
## Notes
87+
88+
- Input values are **not** type checked. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function.
89+
- As `W` values are needed to fill the window buffer, the first `W-1` returned minimum and maximum values are calculated from smaller sample sizes. Until the window is full, each returned minimum and maximum is calculated from all provided values.
90+
91+
</section>
92+
93+
<!-- /.notes -->
94+
95+
<section class="examples">
96+
97+
## Examples
98+
99+
<!-- eslint no-undef: "error" -->
100+
101+
```javascript
102+
var randu = require( '@stdlib/random/base/randu' );
103+
var incrnanmminmaxabs = require( '@stdlib/stats/incr/mminmaxabs' );
104+
105+
var accumulator;
106+
var v;
107+
var i;
108+
109+
// Initialize an accumulator:
110+
accumulator = incrnanmminmaxabs( 5 );
111+
112+
// For each simulated datum, update the moving minimum and maximum absolute values...
113+
for ( i = 0; i < 100; i++ ) {
114+
v = ( randu()*100.0 ) - 50.0;
115+
accumulator( v );
116+
}
117+
console.log( accumulator() );
118+
```
119+
120+
</section>
121+
122+
<!-- /.examples -->
123+
124+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
125+
126+
<section class="related">
127+
128+
* * *
129+
130+
## See Also
131+
132+
- <span class="package-name">[`@stdlib/stats/incr/mminmaxabs][@stdlib/stats/incr/mminmaxabs]
133+
</span><span class="delimiter">: </span><span class="description">compute moving minimum and maximum absolute values incrementally.</span>
134+
- <span class="package-name">[`@stdlib/stats/incr/minmaxabs`][@stdlib/stats/incr/minmaxabs]</span><span class="delimiter">: </span><span class="description">compute minimum and maximum absolute values incrementally.</span>
135+
- <span class="package-name">[`@stdlib/stats/incr/mmax`][@stdlib/stats/incr/mmax]</span><span class="delimiter">: </span><span class="description">compute a moving maximum incrementally.</span>
136+
- <span class="package-name">[`@stdlib/stats/incr/mmaxabs`][@stdlib/stats/incr/mmaxabs]</span><span class="delimiter">: </span><span class="description">compute a moving maximum absolute value incrementally.</span>
137+
- <span class="package-name">[`@stdlib/stats/incr/mmin`][@stdlib/stats/incr/mmin]</span><span class="delimiter">: </span><span class="description">compute a moving minimum incrementally.</span>
138+
- <span class="package-name">[`@stdlib/stats/incr/mminabs`][@stdlib/stats/incr/mminabs]</span><span class="delimiter">: </span><span class="description">compute a moving minimum absolute value incrementally.</span>
139+
- <span class="package-name">[`@stdlib/stats/incr/mminmax`][@stdlib/stats/incr/mminmax]</span><span class="delimiter">: </span><span class="description">compute a moving minimum and maximum incrementally.</span>
140+
141+
</section>
142+
143+
<!-- /.related -->
144+
145+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
146+
147+
<section class="links">
148+
149+
<!-- <related-links> -->
150+
151+
[@stdlib/stats/incr/mminmaxabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mminmaxabs
152+
153+
[@stdlib/stats/incr/minmaxabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/minmaxabs
154+
155+
[@stdlib/stats/incr/mmax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmax
156+
157+
[@stdlib/stats/incr/mmaxabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmaxabs
158+
159+
[@stdlib/stats/incr/mmin]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmin
160+
161+
[@stdlib/stats/incr/mminabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mminabs
162+
163+
[@stdlib/stats/incr/mminmax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mminmax
164+
165+
<!-- </related-links> -->
166+
167+
</section>
168+
169+
<!-- /.links -->
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) 2025 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 randu = require( '@stdlib/random/base/randu' );
25+
var pkg = require( '@stdlib/stats/incr/nanmminmaxabs/package.json' ).name;
26+
var incrnanmminmaxabs = require( '@stdlib/stats/incr/nanmminmaxabs/lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var f;
33+
var i;
34+
b.tic();
35+
for ( i = 0; i < b.iterations; i++ ) {
36+
f = incrnanmminmaxabs( (i%5)+1 );
37+
if ( typeof f !== 'function' ) {
38+
b.fail( 'should return a function' );
39+
}
40+
}
41+
b.toc();
42+
if ( typeof f !== 'function' ) {
43+
b.fail( 'should return a function' );
44+
}
45+
b.pass( 'benchmark finished' );
46+
b.end();
47+
});
48+
49+
bench( pkg+'::accumulator', function benchmark( b ) {
50+
var acc;
51+
var v;
52+
var i;
53+
54+
acc = incrnanmminmaxabs( 5 );
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = acc( randu() );
59+
if ( v.length !== 2 ) {
60+
b.fail( 'should contain two elements' );
61+
}
62+
}
63+
b.toc();
64+
if ( v[ 0 ] !== v[ 0 ] || v[ 1 ] !== v[ 1 ] ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
});
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
{{alias}}( [out,] W )
3+
Returns an accumulator function which incrementally computes moving minimum
4+
and maximum absolute values, ignoring `NaN` values.
5+
6+
The `W` parameter defines the number of values over which to compute moving
7+
minimum and maximum absolute values.
8+
9+
If provided a value, the accumulator function returns an updated moving
10+
minimum and maximum. If not provided a value, the accumulator function
11+
returns the current moving minimum and maximum.
12+
13+
As `W` values are needed to fill the window buffer, the first `W-1` returned
14+
minimum and maximum values are calculated from smaller sample sizes. Until
15+
the window is full, each returned minimum and maximum is calculated from all
16+
provided values.
17+
18+
Parameters
19+
----------
20+
out: Array|TypedArray (optional)
21+
Output array.
22+
23+
W: integer
24+
Window size.
25+
26+
Returns
27+
-------
28+
acc: Function
29+
Accumulator function.
30+
31+
Examples
32+
--------
33+
> var accumulator = {{alias}}( 3 );
34+
> var mm = accumulator()
35+
null
36+
> mm = accumulator( 2.0 )
37+
[ 2.0, 2.0 ]
38+
> mm = accumulator( -5.0 )
39+
[ 2.0, 5.0 ]
40+
> mm = accumulator( NaN )
41+
[ 2.0, 5.0 ]
42+
> mm = accumulator( 3.0 )
43+
[ 2.0, 5.0 ]
44+
> mm = accumulator( 5.0 )
45+
[ 3.0, 5.0 ]
46+
> mm = accumulator()
47+
[ 3.0, 5.0 ]
48+
49+
See Also
50+
--------
51+

0 commit comments

Comments
 (0)