Skip to content

Commit ea5425b

Browse files
committed
added nanmax
1 parent 65d1ef4 commit ea5425b

File tree

10 files changed

+749
-0
lines changed

10 files changed

+749
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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+
# incrnanmax
22+
23+
> Compute a maximum value incrementally, while ignoring `NaN` values.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var incrnanmax = require( '@stdlib/stats/incr/nanmax' );
31+
```
32+
33+
#### incrnanmax()
34+
35+
Returns an accumulator `function` which incrementally computes a maximum value, while ignoring `NaN` values.
36+
37+
```javascript
38+
var accumulator = incrnanmax();
39+
```
40+
41+
#### accumulator( \[x] )
42+
43+
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.
44+
45+
```javascript
46+
var accumulator = incrnanmax();
47+
48+
var max = accumulator( 2.0 );
49+
// returns 2.0
50+
51+
max = accumulator( 1.0 );
52+
// returns 2.0
53+
54+
max = accumulator( NaN );
55+
// returns 2.0
56+
57+
max = accumulator( 3.0 );
58+
// returns 3.0
59+
60+
max = accumulator();
61+
// returns 3.0
62+
```
63+
64+
</section>
65+
66+
<!-- /.usage -->
67+
68+
<section class="notes">
69+
70+
## Notes
71+
72+
- 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.
73+
- For long running accumulations or accumulations of large numbers, care should be taken to prevent overflow.
74+
75+
</section>
76+
77+
<!-- /.notes -->
78+
79+
<section class="examples">
80+
81+
## Examples
82+
83+
<!-- eslint no-undef: "error" -->
84+
85+
```javascript
86+
var randu = require( '@stdlib/random/base/randu' );
87+
var incrnanmax = require( '@stdlib/stats/incr/nanmax' );
88+
89+
var accumulator;
90+
var v;
91+
var i;
92+
93+
// Initialize an accumulator:
94+
accumulator = incrnanmax();
95+
96+
// For each simulated datum, update the max...
97+
for ( i = 0; i < 100; i++ ) {
98+
if ( randu() < 0.2 ) {
99+
v = NaN;
100+
} else {
101+
v = randu() * 100.0;
102+
}
103+
accumulator( v );
104+
}
105+
console.log( accumulator() );
106+
```
107+
108+
</section>
109+
110+
<!-- /.examples -->
111+
112+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
113+
114+
<section class="related">
115+
116+
* * *
117+
118+
## See Also
119+
120+
- <span class="package-name">[`@stdlib/stats/incr/midrange`][@stdlib/stats/incr/midrange]</span><span class="delimiter">: </span><span class="description">compute a mid-range incrementally.</span>
121+
- <span class="package-name">[`@stdlib/stats/incr/min`][@stdlib/stats/incr/min]</span><span class="delimiter">: </span><span class="description">compute a minimum value incrementally.</span>
122+
- <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>
123+
- <span class="package-name">[`@stdlib/stats/incr/range`][@stdlib/stats/incr/range]</span><span class="delimiter">: </span><span class="description">compute a range incrementally.</span>
124+
- <span class="package-name">[`@stdlib/stats/incr/summary`][@stdlib/stats/incr/summary]</span><span class="delimiter">: </span><span class="description">compute a statistical summary incrementally.</span>
125+
126+
</section>
127+
128+
<!-- /.related -->
129+
130+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
131+
132+
<section class="links">
133+
134+
<!-- <related-links> -->
135+
136+
[@stdlib/stats/incr/midrange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/midrange
137+
138+
[@stdlib/stats/incr/min]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/min
139+
140+
[@stdlib/stats/incr/mmax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mmax
141+
142+
[@stdlib/stats/incr/range]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/range
143+
144+
[@stdlib/stats/incr/summary]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/summary
145+
146+
<!-- </related-links> -->
147+
148+
</section>
149+
150+
<!-- /.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 incrnanmax = 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 = incrnanmax();
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 = incrnanmax();
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = acc( randu() );
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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
{{alias}}()
3+
Returns an accumulator function which incrementally computes a maximum
4+
value, ignoring `NaN` values.
5+
6+
If provided a value, the accumulator function returns an updated maximum
7+
value. If not provided a value, the accumulator function returns the current
8+
maximum value.
9+
10+
For long running accumulations or accumulations of large numbers, care
11+
should be taken to prevent overflow.
12+
13+
Returns
14+
-------
15+
acc: Function
16+
Accumulator function.
17+
18+
Examples
19+
--------
20+
> var accumulator = {{alias}}();
21+
> var m = accumulator()
22+
null
23+
> m = accumulator( 3.14 )
24+
3.14
25+
> m = accumulator( NaN )
26+
3.14
27+
> m = accumulator( -5.0 )
28+
3.14
29+
> m = accumulator( 10.1 )
30+
10.1
31+
> m = accumulator()
32+
10.1
33+
34+
See Also
35+
--------
36+
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+
// 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+
* - If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for all future invocations.
29+
*
30+
* @param x - value
31+
* @returns maximum value
32+
*/
33+
type accumulator = ( x?: number ) => number | null;
34+
35+
/**
36+
* Returns an accumulator function which incrementally computes a maximum value, while ignoring `NaN` values.
37+
*
38+
* @returns accumulator function
39+
*
40+
* @example
41+
* var accumulator = incrnanmax();
42+
*
43+
* var v = accumulator();
44+
* // returns null
45+
*
46+
* v = accumulator( 2.0 );
47+
* // returns 2.0
48+
*
49+
* v = accumulator( -4.0 );
50+
* // returns 2.0
51+
*
52+
* * v = accumulator( NaN );
53+
* // returns 2.0
54+
*
55+
* v = accumulator( 3.0 );
56+
* // returns 3.0
57+
*
58+
* v = accumulator();
59+
* // returns 3.0
60+
*/
61+
declare function incrnanmax(): accumulator;
62+
63+
64+
// EXPORTS //
65+
66+
export = incrnanmax;
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) 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 incrnanmax = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an accumulator function...
25+
{
26+
incrnanmax(); // $ExpectType accumulator
27+
}
28+
29+
// The compiler throws an error if the function is provided arguments...
30+
{
31+
incrnanmax( '5' ); // $ExpectError
32+
incrnanmax( 5 ); // $ExpectError
33+
incrnanmax( true ); // $ExpectError
34+
incrnanmax( false ); // $ExpectError
35+
incrnanmax( null ); // $ExpectError
36+
incrnanmax( undefined ); // $ExpectError
37+
incrnanmax( [] ); // $ExpectError
38+
incrnanmax( {} ); // $ExpectError
39+
incrnanmax( ( x: number ): number => x ); // $ExpectError
40+
}
41+
42+
// The function returns an accumulator function which returns an accumulated result...
43+
{
44+
const acc = incrnanmax();
45+
46+
acc(); // $ExpectType number | null
47+
acc( 3.14 ); // $ExpectType number | null
48+
}
49+
50+
// The compiler throws an error if the returned accumulator function is provided invalid arguments...
51+
{
52+
const acc = incrnanmax();
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)