Skip to content

Commit 6c001a2

Browse files
committed
feat: add stats/array/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 c705a94 commit 6c001a2

File tree

10 files changed

+955
-0
lines changed

10 files changed

+955
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
> Calculate the cumulative maximum value of an array.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var cumax = require( '@stdlib/stats/array/cumax' );
37+
```
38+
39+
#### cumax( x, y )
40+
41+
Computes the cumulative maximum value of an array.
42+
43+
```javascript
44+
var x = [ 1.0, -2.0, 2.0 ];
45+
var y = [ 0.0, 0.0, 0.0 ];
46+
47+
cumax( x, y );
48+
// y => [ 1.0, 1.0, 2.0 ]
49+
```
50+
51+
The function has the following parameters:
52+
53+
- **x**: input array.
54+
- **y**: output array.
55+
56+
</section>
57+
58+
<!-- /.usage -->
59+
60+
<section class="notes">
61+
62+
## Notes
63+
64+
- The function supports array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
65+
66+
</section>
67+
68+
<!-- /.notes -->
69+
70+
<section class="examples">
71+
72+
## Examples
73+
74+
<!-- eslint no-undef: "error" -->
75+
76+
```javascript
77+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
78+
var Float64Array = require( '@stdlib/array/float64' );
79+
var cumax = require( '@stdlib/stats/array/cumax' );
80+
81+
var x = discreteUniform( 10, -50, 50, {
82+
'dtype': 'float64'
83+
});
84+
var y = new Float64Array( x.length );
85+
console.log( x );
86+
console.log( y );
87+
88+
cumax( x, y );
89+
console.log( y );
90+
```
91+
92+
</section>
93+
94+
<!-- /.examples -->
95+
96+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
97+
98+
<section class="related">
99+
100+
</section>
101+
102+
<!-- /.related -->
103+
104+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
105+
106+
<section class="links">
107+
108+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
109+
110+
</section>
111+
112+
<!-- /.links -->
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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 uniform = require( '@stdlib/random/array/uniform' );
25+
var zeros = require( '@stdlib/array/zeros' );
26+
var gfill = require( '@stdlib/blas/ext/base/gfill' );
27+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
28+
var pow = require( '@stdlib/math/base/special/pow' );
29+
var pkg = require( './../package.json' ).name;
30+
var cumax = require( './../lib' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'generic'
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 x = uniform( len, -10, 10, options );
51+
var y = zeros( len, options.dtype );
52+
return benchmark;
53+
54+
function benchmark( b ) {
55+
var v;
56+
var i;
57+
58+
gfill( len, 0.0, y, 1 );
59+
60+
b.tic();
61+
for ( i = 0; i < b.iterations; i++ ) {
62+
x[ 0 ] += 1.0;
63+
v = cumax( x, y );
64+
if ( isnan( v ) ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
}
68+
b.toc();
69+
if ( isnan( v ) ) {
70+
b.fail( 'should not return NaN' );
71+
}
72+
b.pass( 'benchmark finished' );
73+
b.end();
74+
}
75+
}
76+
77+
78+
// MAIN //
79+
80+
/**
81+
* Main execution sequence.
82+
*
83+
* @private
84+
*/
85+
function main() {
86+
var len;
87+
var min;
88+
var max;
89+
var f;
90+
var i;
91+
92+
min = 1; // 10^min
93+
max = 6; // 10^max
94+
95+
for ( i = min; i <= max; i++ ) {
96+
len = pow( 10, i );
97+
f = createBenchmark( len );
98+
bench( pkg+':len='+len, f );
99+
}
100+
}
101+
102+
main();
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
{{alias}}( x, y )
3+
Computes the cumulative maximum value of an array.
4+
5+
Parameters
6+
----------
7+
x: Array<number>|TypedArray
8+
Input array.
9+
10+
y: Array<number>|TypedArray
11+
Output array.
12+
13+
Returns
14+
-------
15+
out: Array<number>|TypedArray
16+
Output array.
17+
18+
Examples
19+
--------
20+
> var x = [ 1.0, -2.0, 2.0 ];
21+
> var y = [ 0.0, 0.0, 0.0 ];
22+
> {{alias}}( x, y )
23+
[ 1.0, 1.0, 2.0 ]
24+
25+
See Also
26+
--------
27+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array';
24+
25+
/**
26+
* Input array.
27+
*/
28+
type InputArray = NumericArray | Collection<number> | AccessorArrayLike<number>;
29+
30+
/**
31+
* Output array.
32+
*/
33+
type OutputArray = NumericArray | Collection<number> | AccessorArrayLike<number>;
34+
35+
/**
36+
* Computes the cumulative maximum value of an array.
37+
*
38+
* @param x - input array
39+
* @param y - output array
40+
* @returns output array
41+
*
42+
* @example
43+
* var x = [ 1.0, -2.0, 2.0 ];
44+
* var y = [ 0.0, 0.0, 0.0 ];
45+
*
46+
* cumax( x, y );
47+
* // y => [ 1.0, 1.0, 2.0 ]
48+
*/
49+
declare function cumax<T extends OutputArray>( x: InputArray, y: T ): T;
50+
51+
52+
// EXPORTS //
53+
54+
export = cumax;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 AccessorArray = require( '@stdlib/array/base/accessor' );
20+
import cumax = require( './index' );
21+
22+
23+
// TESTS //
24+
25+
// The function returns a numeric array...
26+
{
27+
const x = new Float64Array( 10 );
28+
const y = new Float64Array( 10 );
29+
30+
cumax( x, y ); // $ExpectType Float64Array
31+
cumax( new AccessorArray( x ), new AccessorArray( x ) ); // $ExpectType AccessorArray<number>
32+
}
33+
34+
// The compiler throws an error if the function is provided a first argument which is not a numeric array...
35+
{
36+
const y = new Float64Array( 10 );
37+
38+
cumax( 10, y ); // $ExpectError
39+
cumax( '10', y ); // $ExpectError
40+
cumax( true, y ); // $ExpectError
41+
cumax( false, y ); // $ExpectError
42+
cumax( null, y ); // $ExpectError
43+
cumax( undefined, y ); // $ExpectError
44+
cumax( {}, y ); // $ExpectError
45+
cumax( ( x: number ): number => x, y ); // $ExpectError
46+
}
47+
48+
// The compiler throws an error if the function is provided a second argument which is not a numeric array...
49+
{
50+
const x = new Float64Array( 10 );
51+
52+
cumax( x, 10 ); // $ExpectError
53+
cumax( x, '10' ); // $ExpectError
54+
cumax( x, true ); // $ExpectError
55+
cumax( x, false ); // $ExpectError
56+
cumax( x, null ); // $ExpectError
57+
cumax( x, undefined ); // $ExpectError
58+
cumax( x, {} ); // $ExpectError
59+
cumax( x, ( x: number ): number => x ); // $ExpectError
60+
}
61+
62+
// The compiler throws an error if the function is provided an unsupported number of arguments...
63+
{
64+
const x = new Float64Array( 10 );
65+
const y = new Float64Array( 10 );
66+
67+
cumax(); // $ExpectError
68+
cumax( x ); // $ExpectError
69+
cumax( x, y, 10 ); // $ExpectError
70+
}

0 commit comments

Comments
 (0)