Skip to content

Commit 7139779

Browse files
committed
feat: add stats/cumax
--- 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 0216d98 commit 7139779

File tree

13 files changed

+2467
-0
lines changed

13 files changed

+2467
-0
lines changed
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
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+
# cumax
22+
23+
> Compute the cumulative maximum value along one or more [ndarray][@stdlib/ndarray/ctor] dimensions.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var cumax = require( '@stdlib/stats/cumax' );
31+
```
32+
33+
#### cumax( x\[, options] )
34+
35+
Computes the cumulative maximum value along one or more [ndarray][@stdlib/ndarray/ctor] dimensions.
36+
37+
```javascript
38+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
39+
var ndarray = require( '@stdlib/ndarray/ctor' );
40+
41+
var xbuf = [ -1.0, 2.0, -3.0 ];
42+
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
43+
44+
var y = cumax( x );
45+
// returns <ndarray>
46+
47+
var arr = ndarray2array( y );
48+
// returns [ -1.0, 2.0, 2.0 ]
49+
```
50+
51+
The function has the following parameters:
52+
53+
- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes].
54+
- **options**: function options (_optional_).
55+
56+
The function accepts the following options:
57+
58+
- **dims**: list of dimensions over which to perform operation. If not provided, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor].
59+
- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. Must be a real-valued or "generic" [data type][@stdlib/ndarray/dtypes].
60+
61+
By default, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. To perform the operation over specific dimensions, provide a `dims` option.
62+
63+
```javascript
64+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
65+
var ndarray = require( '@stdlib/ndarray/ctor' );
66+
67+
var xbuf = [ -1.0, 2.0, -3.0, 4.0 ];
68+
var x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' );
69+
70+
var y = cumax( x, {
71+
'dims': [ 0 ]
72+
});
73+
// returns <ndarray>
74+
75+
var v = ndarray2array( y );
76+
// returns [ [ -1.0, 2.0 ], [ -1.0, 4.0 ] ]
77+
78+
y = cumax( x, {
79+
'dims': [ 1 ]
80+
});
81+
// returns <ndarray>
82+
83+
v = ndarray2array( y );
84+
// returns [ [ -1.0, 2.0 ], [ -3.0, 4.0 ] ]
85+
86+
y = cumax( x, {
87+
'dims': [ 0, 1 ]
88+
});
89+
// returns <ndarray>
90+
91+
v = ndarray2array( y );
92+
// returns [ [ -1.0, 2.0 ], [ 2.0, 4.0 ] ]
93+
```
94+
95+
By default, the function returns an [ndarray][@stdlib/ndarray/ctor] having a [data type][@stdlib/ndarray/dtypes] determined by the function's output data type [policy][@stdlib/ndarray/output-dtype-policies]. To override the default behavior, set the `dtype` option.
96+
97+
```javascript
98+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
99+
var dtype = require( '@stdlib/ndarray/dtype' );
100+
var ndarray = require( '@stdlib/ndarray/ctor' );
101+
102+
var xbuf = [ -1.0, 2.0, -3.0 ];
103+
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
104+
105+
var y = cumax( x, {
106+
'dtype': 'float64'
107+
});
108+
// returns <ndarray>
109+
110+
var dt = dtype( y );
111+
// returns 'float64'
112+
```
113+
114+
#### cumax.assign( x, out\[, options] )
115+
116+
Computes the cumulative maximum value along one or more [ndarray][@stdlib/ndarray/ctor] dimensions and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor].
117+
118+
```javascript
119+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
120+
var ndarray = require( '@stdlib/ndarray/ctor' );
121+
122+
var xbuf = [ -1.0, 2.0, -3.0 ];
123+
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
124+
125+
var ybuf = [ 0.0, 0.0, 0.0 ];
126+
var y = new ndarray( 'generic', ybuf, [ ybuf.length ], [ 1 ], 0, 'row-major' );
127+
128+
var out = cumax.assign( x, y );
129+
// returns <ndarray>
130+
131+
var v = ndarray2array( out );
132+
// returns [ -1.0, 2.0, 2.0 ]
133+
134+
var bool = ( out === y );
135+
// returns true
136+
```
137+
138+
The method has the following parameters:
139+
140+
- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or generic [data type][@stdlib/ndarray/dtypes].
141+
- **out**: output [ndarray][@stdlib/ndarray/ctor].
142+
- **options**: function options (_optional_).
143+
144+
The function accepts the following options:
145+
146+
- **dims**: list of dimensions over which to perform operation. If not provided, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor].
147+
148+
</section>
149+
150+
<!-- /.usage -->
151+
152+
<section class="notes">
153+
154+
## Notes
155+
156+
- The output data type [policy][@stdlib/ndarray/output-dtype-policies] only applies to the main function and specifies that, by default, the function must return an [ndarray][@stdlib/ndarray/ctor] having the same [data type][@stdlib/ndarray/dtypes] as the input [ndarray][@stdlib/ndarray/ctor]. For the `assign` method, the output [ndarray][@stdlib/ndarray/ctor] is allowed to have any [data type][@stdlib/ndarray/dtypes].
157+
158+
</section>
159+
160+
<!-- /.notes -->
161+
162+
<section class="examples">
163+
164+
## Examples
165+
166+
<!-- eslint no-undef: "error" -->
167+
168+
```javascript
169+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
170+
var dtype = require( '@stdlib/ndarray/dtype' );
171+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
172+
var ndarray = require( '@stdlib/ndarray/ctor' );
173+
var cumax = require( '@stdlib/stats/cumax' );
174+
175+
// Generate an array of random numbers:
176+
var xbuf = discreteUniform( 25, 0, 20, {
177+
'dtype': 'generic'
178+
});
179+
180+
// Wrap in an ndarray:
181+
var x = new ndarray( 'generic', xbuf, [ 5, 5 ], [ 5, 1 ], 0, 'row-major' );
182+
console.log( ndarray2array( x ) );
183+
184+
// Perform operation:
185+
var y = cumax( x, {
186+
'dims': [ 0 ]
187+
});
188+
189+
// Resolve the output array data type:
190+
var dt = dtype( y );
191+
console.log( dt );
192+
193+
// Print the results:
194+
console.log( ndarray2array( y ) );
195+
```
196+
197+
</section>
198+
199+
<!-- /.examples -->
200+
201+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
202+
203+
<section class="related">
204+
205+
</section>
206+
207+
<!-- /.related -->
208+
209+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
210+
211+
<section class="links">
212+
213+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
214+
215+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
216+
217+
[@stdlib/ndarray/output-dtype-policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/output-dtype-policies
218+
219+
</section>
220+
221+
<!-- /.links -->
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var uniform = require( '@stdlib/random/array/uniform' );
27+
var zerosLike = require( '@stdlib/ndarray/zeros-like' );
28+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
29+
var pkg = require( './../package.json' ).name;
30+
var cumax = require( './../lib' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'float64'
37+
};
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Creates a benchmark function.
44+
*
45+
* @private
46+
* @param {PositiveInteger} len - array length
47+
* @returns {Function} benchmark function
48+
*/
49+
function createBenchmark( len ) {
50+
var out;
51+
var x;
52+
53+
x = uniform( len, -50.0, 50.0, options );
54+
x = new ndarray( options.dtype, x, [ len ], [ 1 ], 0, 'row-major' );
55+
56+
out = zerosLike( x );
57+
58+
return benchmark;
59+
60+
/**
61+
* Benchmark function.
62+
*
63+
* @private
64+
* @param {Benchmark} b - benchmark instance
65+
*/
66+
function benchmark( b ) {
67+
var o;
68+
var i;
69+
70+
b.tic();
71+
for ( i = 0; i < b.iterations; i++ ) {
72+
o = cumax.assign( x, out );
73+
if ( typeof o !== 'object' ) {
74+
b.fail( 'should return an ndarray' );
75+
}
76+
}
77+
b.toc();
78+
if ( isnan( o.get( i%len ) ) ) {
79+
b.fail( 'should not return NaN' );
80+
}
81+
b.pass( 'benchmark finished' );
82+
b.end();
83+
}
84+
}
85+
86+
87+
// MAIN //
88+
89+
/**
90+
* Main execution sequence.
91+
*
92+
* @private
93+
*/
94+
function main() {
95+
var len;
96+
var min;
97+
var max;
98+
var f;
99+
var i;
100+
101+
min = 1; // 10^min
102+
max = 6; // 10^max
103+
104+
for ( i = min; i <= max; i++ ) {
105+
len = pow( 10, i );
106+
f = createBenchmark( len );
107+
bench( pkg+':assign:dtype='+options.dtype+',len='+len, f );
108+
}
109+
}
110+
111+
main();

0 commit comments

Comments
 (0)