Skip to content

Commit d38aee3

Browse files
authored
feat: add stats/base/ndarray/cumin
PR-URL: #7508 Reviewed-by: Athan Reines <[email protected]>
1 parent 0643fd0 commit d38aee3

File tree

10 files changed

+886
-0
lines changed

10 files changed

+886
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
# cumin
22+
23+
> Compute the cumulative minimum value of a one-dimensional ndarray.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var cumin = require( '@stdlib/stats/base/ndarray/cumin' );
37+
```
38+
39+
#### cumin( arrays )
40+
41+
Computes the cumulative minimum value of a one-dimensional ndarray.
42+
43+
```javascript
44+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
45+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
46+
47+
var xbuf = [ 1.0, 3.0, 4.0, 2.0 ];
48+
var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
49+
50+
var ybuf = [ 0.0, 0.0, 0.0, 0.0 ];
51+
var y = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
52+
53+
var v = cumin( [ x, y ] );
54+
// returns <ndarray>
55+
56+
var bool = ( v === y );
57+
// returns true
58+
59+
var arr = ndarray2array( v );
60+
// returns [ 1.0, 1.0, 1.0, 1.0 ]
61+
```
62+
63+
The function has the following parameters:
64+
65+
- **arrays**: array-like object containing a one-dimensional input ndarray and a one-dimensional output ndarray.
66+
67+
</section>
68+
69+
<!-- /.usage -->
70+
71+
<section class="notes">
72+
73+
## Notes
74+
75+
- If provided an empty one-dimensional input ndarray, the function returns the output ndarray unchanged.
76+
77+
</section>
78+
79+
<!-- /.notes -->
80+
81+
<section class="examples">
82+
83+
## Examples
84+
85+
<!-- eslint no-undef: "error" -->
86+
87+
```javascript
88+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
89+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
90+
var zerosLike = require( '@stdlib/ndarray/zeros-like' );
91+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
92+
var cumin = require( '@stdlib/stats/base/ndarray/cumin' );
93+
94+
var xbuf = discreteUniform( 10, -50, 50, {
95+
'dtype': 'generic'
96+
});
97+
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
98+
console.log( ndarray2array( x ) );
99+
100+
var y = zerosLike( x );
101+
console.log( ndarray2array( y ) );
102+
103+
var v = cumin( [ x, y ] );
104+
console.log( ndarray2array( v ) );
105+
```
106+
107+
</section>
108+
109+
<!-- /.examples -->
110+
111+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
112+
113+
<section class="related">
114+
115+
</section>
116+
117+
<!-- /.related -->
118+
119+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
120+
121+
<section class="links">
122+
123+
</section>
124+
125+
<!-- /.links -->
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
29+
var pkg = require( './../package.json' ).name;
30+
var cumin = 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 xbuf;
51+
var ybuf;
52+
var x;
53+
var y;
54+
55+
xbuf = uniform( len, -10.0, 10.0, options );
56+
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
57+
58+
ybuf = zeros( len, options.dtype );
59+
y = new ndarray( options.dtype, ybuf, [ len ], [ 1 ], 0, 'row-major' );
60+
61+
return benchmark;
62+
63+
function benchmark( b ) {
64+
var v;
65+
var i;
66+
67+
b.tic();
68+
for ( i = 0; i < b.iterations; i++ ) {
69+
v = cumin( [ x, y ] );
70+
if ( typeof v !== 'object' ) {
71+
b.fail( 'should return an ndarray' );
72+
}
73+
}
74+
b.toc();
75+
if ( isnan( v.get( i%len ) ) ) {
76+
b.fail( 'should not return NaN' );
77+
}
78+
b.pass( 'benchmark finished' );
79+
b.end();
80+
}
81+
}
82+
83+
84+
// MAIN //
85+
86+
/**
87+
* Main execution sequence.
88+
*
89+
* @private
90+
*/
91+
function main() {
92+
var len;
93+
var min;
94+
var max;
95+
var f;
96+
var i;
97+
98+
min = 1; // 10^min
99+
max = 6; // 10^max
100+
101+
for ( i = min; i <= max; i++ ) {
102+
len = pow( 10, i );
103+
f = createBenchmark( len );
104+
bench( pkg+':len='+len, f );
105+
}
106+
}
107+
108+
main();
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
{{alias}}( arrays )
3+
Computes the cumulative minimum value of a one-dimensional ndarray.
4+
5+
If provided an empty input ndarray, the function returns the output ndarray
6+
unchanged.
7+
8+
Parameters
9+
----------
10+
arrays: ArrayLikeObject<ndarray>
11+
Array-like object containing a one-dimensional input ndarray and a one-
12+
dimensional output ndarray.
13+
14+
Returns
15+
-------
16+
out: ndarray
17+
Output ndarray.
18+
19+
Examples
20+
--------
21+
> var xbuf = [ 1.0, -2.0, 2.0 ];
22+
> var ybuf = [ 0.0, 0.0, 0.0 ];
23+
> var dt = 'generic';
24+
> var sh = [ xbuf.length ];
25+
> var st = [ 1 ];
26+
> var oo = 0;
27+
> var ord = 'row-major';
28+
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, st, oo, ord );
29+
> var y = new {{alias:@stdlib/ndarray/ctor}}( dt, ybuf, sh, st, oo, ord );
30+
> {{alias}}( [ x, y ] );
31+
> {{alias:@stdlib/ndarray/to-array}}( y )
32+
[ 1.0, -2.0, -2.0 ]
33+
34+
See Also
35+
--------
36+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 { ndarray, typedndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Computes the cumulative minimum value of a one-dimensional ndarray.
27+
*
28+
* @param arrays - array-like object containing an input ndarray and an output ndarray
29+
* @returns output ndarray
30+
*
31+
* @example
32+
* var ndarray2array = require( '@stdlib/ndarray/to-array' );
33+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
34+
*
35+
* var xbuf = [ 1.0, 3.0, 4.0, 2.0 ];
36+
* var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
37+
*
38+
* var ybuf = [ 0.0, 0.0, 0.0, 0.0 ];
39+
* var y = new ndarray( 'generic', ybuf, [ 4 ], [ 1 ], 0, 'row-major' );
40+
*
41+
* var v = cumin( [ x, y ] );
42+
* // returns <ndarray>
43+
*
44+
* var bool = ( v === y );
45+
* // returns true
46+
*
47+
* var arr = ndarray2array( v );
48+
* // returns [ 1.0, 1.0, 1.0, 1.0 ]
49+
*/
50+
declare function cumin<T extends typedndarray<unknown> = typedndarray<unknown>>( arrays: [ ndarray, T ] ): T;
51+
52+
53+
// EXPORTS //
54+
55+
export = cumin;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
/* eslint-disable space-in-parens */
20+
21+
import zeros = require( '@stdlib/ndarray/zeros' );
22+
import cumin = require( './index' );
23+
24+
25+
// TESTS //
26+
27+
// The function returns an ndarray...
28+
{
29+
const x = zeros( [ 10 ], {
30+
'dtype': 'generic'
31+
});
32+
const y = zeros( [ 10 ], {
33+
'dtype': 'generic'
34+
});
35+
36+
cumin( [ x, y ] ); // $ExpectType genericndarray<number>
37+
}
38+
39+
// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays...
40+
{
41+
cumin( '10' ); // $ExpectError
42+
cumin( 10 ); // $ExpectError
43+
cumin( true ); // $ExpectError
44+
cumin( false ); // $ExpectError
45+
cumin( null ); // $ExpectError
46+
cumin( undefined ); // $ExpectError
47+
cumin( [] ); // $ExpectError
48+
cumin( {} ); // $ExpectError
49+
cumin( ( x: number ): number => x ); // $ExpectError
50+
}
51+
52+
// The compiler throws an error if the function is provided an unsupported number of arguments...
53+
{
54+
const x = zeros( [ 10 ], {
55+
'dtype': 'generic'
56+
});
57+
const y = zeros( [ 10 ], {
58+
'dtype': 'generic'
59+
});
60+
61+
cumin(); // $ExpectError
62+
cumin( [ x, y ], {} ); // $ExpectError
63+
}

0 commit comments

Comments
 (0)