Skip to content

Commit 8d357b1

Browse files
committed
feat: add stats/max
--- 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 4ae5a06 commit 8d357b1

File tree

13 files changed

+2719
-0
lines changed

13 files changed

+2719
-0
lines changed
Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
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+
# max
22+
23+
> Compute the maximum value along one or more [ndarray][@stdlib/ndarray/ctor] dimensions.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var max = require( '@stdlib/stats/max' );
31+
```
32+
33+
#### max( x\[, options] )
34+
35+
Computes the maximum value along one or more [ndarray][@stdlib/ndarray/ctor] dimensions.
36+
37+
```javascript
38+
var ndarray = require( '@stdlib/ndarray/ctor' );
39+
40+
var xbuf = [ -1.0, 2.0, -3.0 ];
41+
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
42+
43+
var y = max( x );
44+
// returns <ndarray>
45+
46+
var v = y.get();
47+
// returns 2.0
48+
```
49+
50+
The function has the following parameters:
51+
52+
- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or generic [data type][@stdlib/ndarray/dtypes].
53+
- **options**: function options (_optional_).
54+
55+
The function accepts the following options:
56+
57+
- **dims**: list of dimensions over which to perform a reduction. If not provided, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor].
58+
- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. Must be a real-valued or generic [data type][@stdlib/ndarray/dtypes].
59+
- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`.
60+
61+
By default, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. To perform a reduction 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 = max( x, {
71+
'dims': [ 0 ]
72+
});
73+
// returns <ndarray>
74+
75+
var v = ndarray2array( y );
76+
// returns [ -1.0, 4.0 ]
77+
78+
y = max( x, {
79+
'dims': [ 1 ]
80+
});
81+
// returns <ndarray>
82+
83+
v = ndarray2array( y );
84+
// returns [ 2.0, 4.0 ]
85+
86+
y = max( x, {
87+
'dims': [ 0, 1 ]
88+
});
89+
// returns <ndarray>
90+
91+
v = y.get();
92+
// returns 4.0
93+
```
94+
95+
By default, the function excludes reduced dimensions from the output [ndarray][@stdlib/ndarray/ctor]. To include the reduced dimensions as singleton dimensions, which can be useful when broadcasting the output [ndarray][@stdlib/ndarray/ctor] against ndarrays having the same shape as the input [ndarray][@stdlib/ndarray/ctor], set the `keepdims` option to `true`.
96+
97+
```javascript
98+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
99+
var ndarray = require( '@stdlib/ndarray/ctor' );
100+
101+
var xbuf = [ -1.0, 2.0, -3.0, 4.0 ];
102+
var x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' );
103+
104+
var y = max( x, {
105+
'dims': [ 0 ],
106+
'keepdims': true
107+
});
108+
// returns <ndarray>
109+
110+
var v = ndarray2array( y );
111+
// returns [ [ -1.0, 4.0 ] ]
112+
113+
y = max( x, {
114+
'dims': [ 1 ],
115+
'keepdims': true
116+
});
117+
// returns <ndarray>
118+
119+
v = ndarray2array( y );
120+
// returns [ [ 2.0 ], [ 4.0 ] ]
121+
122+
y = max( x, {
123+
'dims': [ 0, 1 ],
124+
'keepdims': true
125+
});
126+
// returns <ndarray>
127+
128+
v = ndarray2array( y );
129+
// returns [ [ 4.0 ] ]
130+
```
131+
132+
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], in which the function returns an [ndarray][@stdlib/ndarray/ctor] having the same [data type][@stdlib/ndarray/dtypes] as the input [ndarray][@stdlib/ndarray/ctor]. To override the default behavior, set the `dtype` option.
133+
134+
```javascript
135+
var dtype = require( '@stdlib/ndarray/dtype' );
136+
var ndarray = require( '@stdlib/ndarray/ctor' );
137+
138+
var xbuf = [ -1.0, 2.0, -3.0 ];
139+
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
140+
141+
var y = max( x, {
142+
'dtype': 'float64'
143+
});
144+
// returns <ndarray>
145+
146+
var dt = dtype( y );
147+
// returns 'float64'
148+
```
149+
150+
#### max.assign( x, out\[, options] )
151+
152+
Computes the maximum value along one or more [ndarray][@stdlib/ndarray/ctor] dimensions and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor].
153+
154+
```javascript
155+
var ndarray = require( '@stdlib/ndarray/ctor' );
156+
157+
var xbuf = [ -1.0, 2.0, -3.0 ];
158+
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
159+
160+
var ybuf = [ 0.0 ];
161+
var y = new ndarray( 'generic', ybuf, [], [ 0 ], 0, 'row-major' );
162+
163+
var out = max.assign( x, y );
164+
// returns <ndarray>
165+
166+
var v = out.get();
167+
// returns 2.0
168+
169+
var bool = ( out === y );
170+
// returns true
171+
```
172+
173+
The method has the following parameters:
174+
175+
- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or generic [data type][@stdlib/ndarray/dtypes].
176+
- **out**: output [ndarray][@stdlib/ndarray/ctor].
177+
- **options**: function options (_optional_).
178+
179+
The function accepts the following `options`:
180+
181+
- **dims**: list of dimensions over which to perform a reduction. If not provided, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor].
182+
183+
</section>
184+
185+
<!-- /.usage -->
186+
187+
<section class="notes">
188+
189+
## Notes
190+
191+
- The output data type [policy][@stdlib/ndarray/output-dtype-policies] only applies to the main function. For the `assign` method, the output [ndarray][@stdlib/ndarray/ctor] is allowed to have any [data type][@stdlib/ndarray/dtypes].
192+
193+
</section>
194+
195+
<!-- /.notes -->
196+
197+
<section class="examples">
198+
199+
## Examples
200+
201+
<!-- eslint no-undef: "error" -->
202+
203+
```javascript
204+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
205+
var dtype = require( '@stdlib/ndarray/dtype' );
206+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
207+
var ndarray = require( '@stdlib/ndarray/ctor' );
208+
var max = require( '@stdlib/stats/max' );
209+
210+
// Generate an array of random numbers:
211+
var xbuf = discreteUniform( 25, 0, 20, {
212+
'dtype': 'generic'
213+
});
214+
215+
// Wrap in an ndarray:
216+
var x = new ndarray( 'generic', xbuf, [ 5, 5 ], [ 5, 1 ], 0, 'row-major' );
217+
console.log( ndarray2array( x ) );
218+
219+
// Perform a reduction:
220+
var y = max( x, {
221+
'dims': [ 0 ]
222+
});
223+
224+
// Resolve the output array data type:
225+
var dt = dtype( y );
226+
console.log( dt );
227+
228+
// Print the results:
229+
console.log( ndarray2array( y ) );
230+
```
231+
232+
</section>
233+
234+
<!-- /.examples -->
235+
236+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
237+
238+
<section class="related">
239+
240+
</section>
241+
242+
<!-- /.related -->
243+
244+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
245+
246+
<section class="links">
247+
248+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
249+
250+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
251+
252+
[@stdlib/ndarray/output-dtype-policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/output-dtype-policies
253+
254+
</section>
255+
256+
<!-- /.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 zeros = require( '@stdlib/array/zeros' );
28+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
29+
var pkg = require( './../package.json' ).name;
30+
var max = 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 = new ndarray( options.dtype, zeros( 1, options.dtype ), [], [ 0 ], 0, 'row-major' );
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 = max.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() ) ) {
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)