Skip to content

Commit 6736d9f

Browse files
committed
feat: add stats/incr/nanmmaxabs
--- 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 e80fff8 commit 6736d9f

File tree

10 files changed

+854
-0
lines changed

10 files changed

+854
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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]
56+
// returns 2.0
57+
58+
m = accumulator( -5.0 ); // [2.0, -5.0]
59+
// returns 5.0
60+
61+
m = accumulator( -7.0 ); // [1.0, -5.0, -7.0]
62+
// returns 7.0
63+
64+
m = accumulator( NaN ); // [1.0, -5.0, -7.0]
65+
// returns 7.0
66+
67+
m = accumulator();
68+
// returns 7.0
69+
```
70+
71+
</section>
72+
73+
<!-- /.usage -->
74+
75+
<section class="notes">
76+
77+
## Notes
78+
79+
- 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.
80+
81+
</section>
82+
83+
<!-- /.notes -->
84+
85+
<section class="examples">
86+
87+
88+
89+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
90+
91+
<section class="related">
92+
93+
* * *
94+
95+
## See Also
96+
97+
- <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>
98+
- <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>
99+
- <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>
100+
101+
</section>
102+
103+
<!-- /.related -->
104+
105+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
106+
107+
<section class="links">
108+
109+
<!-- <related-links> -->
110+
111+
[@stdlib/stats/incr/maxabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/maxabs
112+
113+
[@stdlib/stats/incr/mmax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmax
114+
115+
[@stdlib/stats/incr/mminabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mminabs
116+
117+
<!-- </related-links> -->
118+
119+
</section>
120+
121+
<!-- /.links -->
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 x;
53+
var i;
54+
55+
acc = incrnanmmaxabs( 5 );
56+
57+
b.tic();
58+
for ( i = 0; i < b.iterations; i++ ) {
59+
if ( (i&7) === 0 ) {
60+
x = NaN;
61+
} else {
62+
x = randu()-0.5;
63+
}
64+
v = acc( x );
65+
if ( v !== v ) {
66+
b.fail( 'should not return NaN' );
67+
}
68+
}
69+
b.toc();
70+
if ( v !== v ) {
71+
b.fail( 'should not return NaN' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{{alias}}( W )
2+
Returns an accumulator function which incrementally computes a moving
3+
maximum absolute value while ignoring `NaN` values.
4+
5+
The `W` parameter defines the number of values over which to compute the
6+
moving maximum absolute value.
7+
8+
If provided a value, the accumulator function returns an updated moving
9+
maximum absolute value. If not provided a value, the accumulator function
10+
returns the current moving maximum absolute value.
11+
12+
Any `NaN` values provided to the accumulator are ignored, and do not
13+
affect the accumulated result.
14+
15+
As `W` values are needed to fill the window buffer, the first `W-1` returned
16+
values are calculated from smaller sample sizes. Until the window is full,
17+
each returned value is calculated from all provided non-NaN values.
18+
19+
Parameters
20+
----------
21+
W: integer
22+
Window size.
23+
24+
Returns
25+
-------
26+
acc: Function
27+
Accumulator function.
28+
29+
Examples
30+
--------
31+
> var accumulator = {{alias}}( 3 );
32+
> var m = accumulator()
33+
null
34+
> m = accumulator( 2.0 )
35+
2.0
36+
> m = accumulator( NaN )
37+
2.0
38+
> m = accumulator( -5.0 )
39+
5.0
40+
> m = accumulator( 3.0 )
41+
5.0
42+
> m = accumulator()
43+
5.0
44+
45+
See Also
46+
--------
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
/// <reference types="@stdlib/types"/>
22+
23+
/**
24+
* If provided a value, returns an updated moving maximum absolute value; otherwise, returns the current maximum absolute value.
25+
*
26+
*
27+
* @param x - value
28+
* @returns moving maximum absolute value
29+
*/
30+
type accumulator = ( x?: number ) => number | null;
31+
32+
/**
33+
* Returns an accumulator function which incrementally computes a moving maximum absolute value while ignoring `NaN` values.
34+
*
35+
* ## Notes
36+
*
37+
* - The `W` parameter defines the number of values over which to compute the moving maximum absolute value.
38+
* - Until the window is full, each returned value is calculated from all non-NaN values received so far. `NaN` values are skipped and do not enter the moving window.
39+
*
40+
* @param W - window size
41+
* @throws must provide a positive integer
42+
* @returns accumulator function
43+
*
44+
* @example
45+
* var accumulator = incrnanmmaxabs( 3 );
46+
*
47+
* var m = accumulator();
48+
* // returns null
49+
*
50+
* m = accumulator( 2.0 );
51+
* // returns 2.0
52+
*
53+
* m = accumulator( NaN );
54+
* // returns 2.0
55+
*
56+
* m = accumulator( -5.0 );
57+
* // returns 5.0
58+
*
59+
* m = accumulator( NaN );
60+
* // returns 5.0
61+
*/
62+
declare function incrnanmmaxabs( W: number ): accumulator;
63+
64+
65+
// EXPORTS //
66+
67+
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)