Skip to content

Commit b26bdd4

Browse files
committed
feat: add stats/strided/nancumax
1 parent 74f9832 commit b26bdd4

File tree

15 files changed

+1770
-0
lines changed

15 files changed

+1770
-0
lines changed
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# nancumax
22+
23+
> Calculate the cumulative maximum of a strided array, ignoring `NaN` values.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var nancumax = require( '@stdlib/stats/strided/nancumax' );
37+
```
38+
39+
#### nancumax( N, x, strideX, y, strideY )
40+
41+
Computes the cumulative maximum of a strided array `x`, ignoring `NaN` values.
42+
43+
```javascript
44+
var x = [ 1.0, -2.0, NaN, 2.0 ];
45+
var y = [ 0.0, 0.0, 0.0, 0.0 ];
46+
47+
var v = nancumax( x.length, x, 1, y, 1 );
48+
// returns [ 1.0, 1.0, 1.0, 2.0 ]
49+
```
50+
51+
The function has the following parameters:
52+
53+
- **N**: number of indexed elements.
54+
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
55+
- **strideX**: stride length for `x`.
56+
- **y**: output [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
57+
- **strideY**: stride length for `y`.
58+
59+
The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to compute the cumulative maximum of every other element in `x`,
60+
61+
```javascript
62+
var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, NaN, NaN ];
63+
var y = [ 0.0, 0.0, 0.0, 0.0, 0.0 ];
64+
65+
var v = nancumax( 5, x, 2, y, 1 );
66+
// returns [ 1.0, 2.0, 2.0, 4.0, 4.0 ]
67+
```
68+
69+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
70+
71+
<!-- eslint-disable stdlib/capitalized-comments -->
72+
73+
```javascript
74+
var Float64Array = require( '@stdlib/array/float64' );
75+
76+
var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, NaN, NaN, 4.0 ] );
77+
var y0 = new Float64Array( 4 );
78+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
79+
var y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*0 );
80+
81+
var v = nancumax( 4, x1, 2, y1, 1 );
82+
// returns [ 1.0, 1.0, 1.0, 4.0 ]
83+
```
84+
85+
#### nancumax.ndarray( N, x, strideX, offsetX, y, strideY, offsetY )
86+
87+
Computes the cumulative maximum of a strided array, ignoring `NaN` values and using alternative indexing semantics.
88+
89+
```javascript
90+
var x = [ 1.0, -2.0, NaN, 2.0 ];
91+
var y = [ 0.0, 0.0, 0.0, 0.0 ];
92+
93+
var v = nancumax.ndarray( x.length, x, 1, 0, y, 1, 0 );
94+
// returns [ 1.0, 1.0, 1.0, 2.0 ]
95+
```
96+
97+
The function has the following additional parameters:
98+
99+
- **offsetX**: starting index for `x`.
100+
- **offsetY**: starting index for `y`.
101+
102+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on a starting index. For example, to calculate the cumulative maximum for every other element in `x` starting from the second element
103+
104+
```javascript
105+
var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, NaN, NaN, 2.0, 3.0, 4.0 ];
106+
var y = [ 0.0, 0.0, 0.0, 0.0, 0.0 ];
107+
108+
var v = nancumax.ndarray( 5, x, 2, 1, y, 1, 0 );
109+
// returns [ 1.0, 1.0, 2.0, 2.0, 4.0 ]
110+
```
111+
112+
</section>
113+
114+
<!-- /.usage -->
115+
116+
<section class="notes">
117+
118+
## Notes
119+
120+
- If `N <= 0`, both functions return the output array unchanged.
121+
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
122+
123+
</section>
124+
125+
<!-- /.notes -->
126+
127+
<section class="examples">
128+
129+
## Examples
130+
131+
<!-- eslint no-undef: "error" -->
132+
133+
```javascript
134+
var uniform = require( '@stdlib/random/base/uniform' );
135+
var filledarrayBy = require( '@stdlib/array/filled-by' );
136+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
137+
var zeros = require( '@stdlib/array/zeros' );
138+
var nancumax = require( '@stdlib/stats/strided/nancumax' );
139+
140+
function rand() {
141+
if ( bernoulli( 0.8 ) < 1 ) {
142+
return NaN;
143+
}
144+
return uniform( -50.0, 50.0 );
145+
}
146+
147+
var x = filledarrayBy( 10, 'float64', rand );
148+
console.log( x );
149+
150+
var y = zeros( 10, 'float64' );
151+
console.log( y );
152+
153+
var v = nancumax( x.length, x, 1, y, 1 );
154+
console.log( v );
155+
```
156+
157+
</section>
158+
159+
<!-- /.examples -->
160+
161+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
162+
163+
<section class="related">
164+
165+
</section>
166+
167+
<!-- /.related -->
168+
169+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
170+
171+
<section class="links">
172+
173+
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
174+
175+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
176+
177+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
178+
179+
<!-- <related-links> -->
180+
181+
<!-- </related-links> -->
182+
183+
</section>
184+
185+
<!-- /.links -->
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var uniform = require( '@stdlib/random/base/uniform' );
26+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
27+
var filledarrayBy = require( '@stdlib/array/filled-by' );
28+
var zeros = require( '@stdlib/array/zeros' );
29+
var pow = require( '@stdlib/math/base/special/pow' );
30+
var pkg = require( './../package.json' ).name;
31+
var nancumax = require( './../lib/main.js' );
32+
33+
34+
// FUNCTIONS //
35+
36+
/**
37+
* Returns a random number.
38+
*
39+
* @private
40+
* @returns {number} random number
41+
*/
42+
function rand() {
43+
if ( bernoulli( 0.8 ) < 1 ) {
44+
return NaN;
45+
}
46+
return uniform( -10.0, 10.0 );
47+
}
48+
49+
/**
50+
* Creates a benchmark function.
51+
*
52+
* @private
53+
* @param {PositiveInteger} len - array length
54+
* @returns {Function} benchmark function
55+
*/
56+
function createBenchmark( len ) {
57+
var x = filledarrayBy( len, 'generic', rand );
58+
var y = zeros( len, 'generic' );
59+
return benchmark;
60+
61+
/**
62+
* Benchmark function.
63+
*
64+
* @private
65+
* @param {Benchmark} b - benchmark instance
66+
*/
67+
function benchmark( b ) {
68+
var v;
69+
var i;
70+
71+
b.tic();
72+
for ( i = 0; i < b.iterations; i++ ) {
73+
v = nancumax( x.length, x, 1, y, 1 );
74+
if ( isnan( v[ 0 ] ) ) {
75+
b.fail( 'should not return NaN' );
76+
}
77+
}
78+
b.toc();
79+
if ( isnan( v[ 0 ] ) ) {
80+
b.fail( 'should not return NaN' );
81+
}
82+
b.pass( 'benchmark finished' );
83+
b.end();
84+
}
85+
}
86+
87+
88+
// MAIN //
89+
90+
/**
91+
* Main execution sequence.
92+
*
93+
* @private
94+
*/
95+
function main() {
96+
var len;
97+
var min;
98+
var max;
99+
var f;
100+
var i;
101+
102+
min = 1; // 10^min
103+
max = 6; // 10^max
104+
105+
for ( i = min; i <= max; i++ ) {
106+
len = pow( 10, i );
107+
f = createBenchmark( len );
108+
bench( pkg+':len='+len, f );
109+
}
110+
}
111+
112+
main();

0 commit comments

Comments
 (0)