Skip to content

Commit 36f95f3

Browse files
committed
feat: add nanmmaxabs package
1 parent a77fb9e commit 36f95f3

File tree

10 files changed

+859
-0
lines changed

10 files changed

+859
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
# incrnanmmaxabs
22+
23+
> Compute a moving maximum absolute value incrementally, ignoring `NaN` values.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var incrnanmmaxabs = require( '@stdlib/stats/incr/nanmmaxabs' );
31+
```
32+
33+
#### incrnanmmaxabs( window )
34+
35+
Returns an accumulator function which incrementally computes a moving maximum absolute value. The `window` parameter defines the number of values over which to compute the moving maximum absolute value.
36+
37+
```javascript
38+
var accumulator = incrnanmmaxabs( 3 );
39+
```
40+
41+
#### accumulator( \[x] )
42+
43+
If provided an input value `x`, the accumulator function returns an updated maximum absolute value. If not provided an input value `x`, the accumulator function returns the current maximum absolute value.
44+
45+
```javascript
46+
var accumulator = incrnanmmaxabs( 3 );
47+
48+
var m = accumulator();
49+
// returns null
50+
51+
// Fill the window...
52+
m = accumulator( 2.0 ); // [2.0]
53+
// returns 2.0
54+
55+
m = accumulator( NaN ); // [2.0, NaN]
56+
// returns 2.0
57+
58+
m = accumulator( 1.0 ); // [2.0, 1.0]
59+
// returns 2.0
60+
61+
m = accumulator( 3.0 ); // [2.0, 1.0, 3.0]
62+
// returns 3.0
63+
64+
// Window begins sliding...
65+
m = accumulator( -7.0 ); // [1.0, 3.0, -7.0]
66+
// returns 7.0
67+
68+
m = accumulator( -5.0 ); // [3.0, -7.0, -5.0]
69+
// returns 7.0
70+
71+
m = accumulator();
72+
// returns 7.0
73+
```
74+
75+
</section>
76+
77+
<!-- /.usage -->
78+
79+
<section class="notes">
80+
81+
## Notes
82+
83+
- 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.
84+
- As `W` values are needed to fill the window buffer, the first `W-1` returned values are calculated from smaller sample sizes. Until the window is full, each returned value is calculated from all provided values.
85+
86+
</section>
87+
88+
<!-- /.notes -->
89+
90+
<section class="examples">
91+
92+
## Examples
93+
94+
<!-- eslint no-undef: "error" -->
95+
96+
```javascript
97+
var uniform = require( '@stdlib/random/base/uniform' );
98+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
99+
var incrnanmmaxabs = require( '@stdlib/stats/incr/nanmmaxabs' );
100+
101+
102+
// Initialize an accumulator:
103+
var accumulator = incrnanmmaxabs( 5 );
104+
105+
// For each simulated datum, update the moving maximum absolute value...
106+
var i;
107+
for ( i = 0; i < 100; i++ ) {
108+
accumulator( ( bernoulli( 0.8 ) < 1 ) ? NaN : uniform( -100.0, 100.0 ) );
109+
}
110+
console.log( accumulator() );
111+
```
112+
113+
</section>
114+
115+
<!-- /.examples -->
116+
117+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
118+
119+
<section class="related">
120+
121+
</section>
122+
123+
<!-- /.related -->
124+
125+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
126+
127+
<!-- /.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( './../package.json' ).name;
26+
var incrnanmmaxabs = require( './../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 = incrnanmmaxabs( (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 = incrnanmmaxabs( 5 );
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = acc( randu()-0.5 );
59+
if ( v !== v ) {
60+
b.fail( 'should not return NaN' );
61+
}
62+
}
63+
b.toc();
64+
if ( v !== v ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
});
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
{{alias}}( W )
3+
Returns an accumulator function which incrementally computes a moving
4+
maximum absolute value, ignoring `NaN` values.
5+
6+
The `W` parameter defines the number of values over which to compute the
7+
moving maximum absolute value.
8+
9+
If provided a value, the accumulator function returns an updated moving
10+
maximum absolute value. If not provided a value, the accumulator function
11+
returns the current moving maximum absolute value.
12+
13+
As `W` values are needed to fill the window buffer, the first `W-1` returned
14+
values are calculated from smaller sample sizes. Until the window is full,
15+
each returned value is calculated from all provided values.
16+
17+
Parameters
18+
----------
19+
W: integer
20+
Window size.
21+
22+
Returns
23+
-------
24+
acc: Function
25+
Accumulator function.
26+
27+
Examples
28+
--------
29+
> var accumulator = {{alias}}( 3 );
30+
> var m = accumulator()
31+
null
32+
> m = accumulator( 2.0 )
33+
2.0
34+
> m = accumulator( NaN )
35+
2.0
36+
> m = accumulator( -5.0 )
37+
5.0
38+
> m = accumulator( 3.0 )
39+
5.0
40+
> m = accumulator( 5.0 )
41+
5.0
42+
> m = accumulator()
43+
5.0
44+
45+
See Also
46+
--------
47+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
// TypeScript Version: 4.1
20+
21+
/**
22+
* If provided a value, returns an updated maximum absolute value; otherwise, returns the current maximum absolute value.
23+
*
24+
* @param x - value
25+
* @returns maximum absolute value
26+
*/
27+
type accumulator = ( x?: number ) => number | null;
28+
29+
/**
30+
* Returns an accumulator function which incrementally computes a moving maximum absolute value, ignoring `NaN` values.
31+
*
32+
* ## Notes
33+
*
34+
* - The `W` parameter defines the number of values over which to compute the moving maximum absolute value.
35+
* - As `W` values are needed to fill the window buffer, the first `W-1` returned values are calculated from smaller sample sizes. Until the window is full, each returned value is calculated from all provided values.
36+
*
37+
* @param W - window size
38+
* @throws must provide a positive integer
39+
* @returns accumulator function
40+
*
41+
* @example
42+
* var accumulator = incrnanmmaxabs( 3 );
43+
*
44+
* var m = accumulator();
45+
* // returns null
46+
*
47+
* m = accumulator( 2.0 );
48+
* // returns 2.0
49+
*
50+
* m = accumulator( NaN );
51+
* // returns 2.0
52+
*
53+
* m = accumulator( -5.0 );
54+
* // returns 5.0
55+
*
56+
* m = accumulator( 3.0 );
57+
* // returns 5.0
58+
*
59+
* m = accumulator( 5.0 );
60+
* // returns 5.0
61+
*
62+
* m = accumulator();
63+
* // returns 5.0
64+
*/
65+
declare function incrnanmmaxabs( W: number ): accumulator;
66+
67+
68+
// EXPORTS //
69+
70+
export = incrnanmmaxabs;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
import incrnanmmaxabs = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an accumulator function...
25+
{
26+
incrnanmmaxabs( 3 ); // $ExpectType accumulator
27+
}
28+
29+
// The compiler throws an error if the function is provided an argument that is not a number...
30+
{
31+
incrnanmmaxabs( '5' ); // $ExpectError
32+
incrnanmmaxabs( true ); // $ExpectError
33+
incrnanmmaxabs( false ); // $ExpectError
34+
incrnanmmaxabs( null ); // $ExpectError
35+
incrnanmmaxabs( undefined ); // $ExpectError
36+
incrnanmmaxabs( [] ); // $ExpectError
37+
incrnanmmaxabs( {} ); // $ExpectError
38+
incrnanmmaxabs( ( x: number ): number => x ); // $ExpectError
39+
}
40+
41+
// The compiler throws an error if the function is provided an invalid number of arguments...
42+
{
43+
incrnanmmaxabs(); // $ExpectError
44+
incrnanmmaxabs( 2, 5 ); // $ExpectError
45+
}
46+
47+
// The function returns an accumulator function which returns an accumulated result...
48+
{
49+
const acc = incrnanmmaxabs( 3 );
50+
51+
acc(); // $ExpectType number | null
52+
acc( 3.14 ); // $ExpectType number | null
53+
}
54+
55+
// The compiler throws an error if the returned accumulator function is provided invalid arguments...
56+
{
57+
const acc = incrnanmmaxabs( 3 );
58+
59+
acc( '5' ); // $ExpectError
60+
acc( true ); // $ExpectError
61+
acc( false ); // $ExpectError
62+
acc( null ); // $ExpectError
63+
acc( [] ); // $ExpectError
64+
acc( {} ); // $ExpectError
65+
acc( ( x: number ): number => x ); // $ExpectError
66+
}

0 commit comments

Comments
 (0)