Skip to content

Commit b83aa6e

Browse files
committed
feat(lib): add stats/incr/nanmmax
--- 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 e924862 commit b83aa6e

10 files changed

Lines changed: 753 additions & 0 deletions

File tree

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
# nanmmax
22+
23+
> Compute a cumulative maximum value incrementally, ignoring NaN values.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var nanmmax = require( '@stdlib/stats/incr/nanmmax' );
31+
```
32+
33+
#### nanmmax()
34+
35+
Returns an accumulator function which incrementally computes a moving maximum value. The window parameter defines the number of values over which to compute the moving maximum.
36+
37+
38+
#### accumulator( \[x] )
39+
40+
If provided an input value x, the accumulator function returns an updated maximum value. If not provided an input value x, the accumulator function returns the current maximum value.
41+
42+
```javascript
43+
var accumulator = nanmmax();
44+
45+
var m = accumulator();
46+
// returns null
47+
48+
m = accumulator( 2.0 );
49+
// returns 2.0
50+
51+
m = accumulator( NaN );
52+
// returns 2.0
53+
54+
m = accumulator( 5.0 );
55+
// returns 5.0
56+
57+
m = accumulator();
58+
// returns 5.0
59+
```
60+
61+
</section>
62+
63+
64+
<!-- /.usage -->
65+
66+
<section class="notes">
67+
68+
## Notes
69+
70+
71+
- Input values are not type checked.
72+
- `Nan` values are ignored and do not affect the accumulated maximum.
73+
- If no non-NaN values have been provided, the accumulator returns null. `null`.
74+
75+
76+
</section>
77+
78+
<!-- /.notes -->
79+
80+
<section class="examples">
81+
82+
## Examples
83+
84+
<!-- eslint no-undef: "error" -->
85+
86+
```javascript
87+
var randu = require( '@stdlib/random/base/randu' );
88+
var nanmmax = require( '@stdlib/stats/incr/nanmmax' );
89+
90+
var accumulator = nanmmax();
91+
var v;
92+
var i;
93+
94+
for ( i = 0; i < 100; i++ ) {
95+
v = randu() * 100.0;
96+
accumulator( v );
97+
}
98+
console.log( accumulator() );
99+
```
100+
101+
</section>
102+
103+
<!-- /.examples -->
104+
105+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
106+
107+
<section class="related">
108+
109+
</section>
110+
111+
<!-- /.related -->
112+
113+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
114+
115+
<section class="links">
116+
117+
<!-- <related-links> -->
118+
119+
<!-- </related-links> -->
120+
121+
</section>
122+
123+
<!-- /.links -->
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 nanmmax = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var f;
33+
var i;
34+
35+
b.tic();
36+
for ( i = 0; i < b.iterations; i++ ) {
37+
f = nanmmax();
38+
if ( typeof f !== 'function' ) {
39+
b.fail( 'should return a function' );
40+
}
41+
}
42+
b.toc();
43+
44+
if ( typeof f !== 'function' ) {
45+
b.fail( 'should return a function' );
46+
}
47+
b.pass( 'benchmark finished' );
48+
b.end();
49+
});
50+
51+
bench( pkg+'::accumulator', function benchmark( b ) {
52+
var acc;
53+
var v;
54+
var i;
55+
56+
acc = nanmmax();
57+
58+
b.tic();
59+
for ( i = 0; i < b.iterations; i++ ) {
60+
v = acc( randu() );
61+
if ( v !== v ) {
62+
b.fail( 'should not return NaN' );
63+
}
64+
}
65+
b.toc();
66+
67+
if ( v !== v ) {
68+
b.fail( 'should not return NaN' );
69+
}
70+
b.pass( 'benchmark finished' );
71+
b.end();
72+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{{alias}}()
2+
Returns an accumulator function which incrementally computes a cumulative
3+
maximum value while **ignoring NaN values**.
4+
5+
If provided a numeric value, the accumulator function updates the cumulative
6+
maximum. If provided `NaN`, the value is ignored. If not provided any value,
7+
the accumulator function returns the current maximum.
8+
9+
Until a non-NaN value is provided, the accumulator returns `null`.
10+
11+
Parameters
12+
----------
13+
(none)
14+
15+
Returns
16+
-------
17+
acc: Function
18+
Accumulator function.
19+
20+
Examples
21+
--------
22+
> var accumulator = {{alias}}();
23+
> var m = accumulator()
24+
null
25+
> m = accumulator( 2.0 )
26+
2.0
27+
> m = accumulator( NaN )
28+
2.0
29+
> m = accumulator( -5.0 )
30+
2.0
31+
> m = accumulator( 3.0 )
32+
3.0
33+
> m = accumulator( 5.0 )
34+
5.0
35+
> m = accumulator()
36+
5.0
37+
38+
See Also
39+
--------
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 maximum value; otherwise, returns the current maximum value.
25+
*
26+
* ## Notes
27+
*
28+
* - `NaN` values are ignored.
29+
* - The accumulator returns `null` until at least one non-NaN value has been provided.
30+
*
31+
* @param x - input value
32+
* @returns maximum value or null
33+
*/
34+
type accumulator = ( x?: number ) => number | null;
35+
36+
/**
37+
* Returns an accumulator function which incrementally computes a maximum value,
38+
* **ignoring `NaN` values**.
39+
*
40+
* @returns accumulator function
41+
*
42+
* @example
43+
* var acc = nanmmax();
44+
*
45+
* var m = acc();
46+
* // returns null
47+
*
48+
* m = acc( 2.0 );
49+
* // returns 2.0
50+
*
51+
* m = acc( NaN );
52+
* // returns 2.0
53+
*
54+
* m = acc( 5.0 );
55+
* // returns 5.0
56+
*
57+
* m = acc();
58+
* // returns 5.0
59+
*/
60+
declare function nanmmax(): accumulator;
61+
62+
63+
// EXPORTS //
64+
65+
export = nanmmax;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 nanmmax = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an accumulator function...
25+
{
26+
nanmmax(); // $ExpectType accumulator
27+
}
28+
29+
// The compiler throws an error if the returned accumulator function is provided invalid arguments...
30+
{
31+
const acc = nanmmax();
32+
33+
acc( '5' ); // $ExpectError
34+
acc( true ); // $ExpectError
35+
acc( false ); // $ExpectError
36+
acc( null ); // $ExpectError
37+
acc( [] ); // $ExpectError
38+
acc( {} ); // $ExpectError
39+
acc( ( x: number ): number => x ); // $ExpectError
40+
}
41+
42+
// The function returns an accumulator function which returns an accumulated result...
43+
{
44+
const acc = nanmmax();
45+
46+
acc(); // $ExpectType number | null
47+
acc( 3.14 ); // $ExpectType number | null
48+
}

0 commit comments

Comments
 (0)