Skip to content

Commit f679f3a

Browse files
committed
feat: add unaryReduceStrided1dBy
--- 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: na - 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 1bbed39 commit f679f3a

File tree

24 files changed

+3761
-0
lines changed

24 files changed

+3761
-0
lines changed
Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
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+
# unaryReduceStrided1dBy
22+
23+
> Perform a reduction over a list of specified dimensions in an input ndarray via a one-dimensional strided array reduction function, according to a callback function and assign results to a provided output 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 unaryReduceStrided1dBy = require( '@stdlib/ndarray/base/unary-reduce-strided1d-by' );
37+
```
38+
39+
#### unaryReduceStrided1dBy( fcn, arrays, dims\[, options], clbk\[, thisArg] )
40+
41+
Performs a reduction over a list of specified dimensions in an input ndarray via a one-dimensional strided array reduction function, according to a callback function and assigns results to a provided output ndarray.
42+
43+
<!-- eslint-disable max-len -->
44+
45+
```javascript
46+
var Float64Array = require( '@stdlib/array/float64' );
47+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
48+
var maxBy = require( '@stdlib/stats/base/ndarray/max-by' );
49+
50+
// Define a callback function:
51+
function clbk( value ) {
52+
return value * 2.0;
53+
}
54+
55+
// Create data buffers:
56+
var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
57+
var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] );
58+
59+
// Define the array shapes:
60+
var xsh = [ 1, 3, 2, 2 ];
61+
var ysh = [ 1, 3 ];
62+
63+
// Define the array strides:
64+
var sx = [ 12, 4, 2, 1 ];
65+
var sy = [ 3, 1 ];
66+
67+
// Define the index offsets:
68+
var ox = 0;
69+
var oy = 0;
70+
71+
// Create an input ndarray-like object:
72+
var x = {
73+
'dtype': 'float64',
74+
'data': xbuf,
75+
'shape': xsh,
76+
'strides': sx,
77+
'offset': ox,
78+
'order': 'row-major'
79+
};
80+
81+
// Create an output ndarray-like object:
82+
var y = {
83+
'dtype': 'float64',
84+
'data': ybuf,
85+
'shape': ysh,
86+
'strides': sy,
87+
'offset': oy,
88+
'order': 'row-major'
89+
};
90+
91+
// Perform a reduction:
92+
unaryReduceStrided1dBy( maxBy, [ x, y ], [ 2, 3 ], clbk );
93+
94+
var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order );
95+
// returns [ [ 8.0, 16.0, 24.0 ] ]
96+
```
97+
98+
The function accepts the following arguments:
99+
100+
- **fcn**: function which will be applied to a one-dimensional subarray and should reduce the subarray to a single scalar value.
101+
- **arrays**: array-like object containing one input ndarray and one output ndarray, followed by any additional ndarray arguments.
102+
- **dims**: list of dimensions over which to perform a reduction.
103+
- **options**: function options which are passed through to `fcn` (_optional_).
104+
- **clbk**: callback function.
105+
- **thisArg**: callback execution context (_optional_).
106+
107+
Each provided ndarray should be an object with the following properties:
108+
109+
- **dtype**: data type.
110+
- **data**: data buffer.
111+
- **shape**: dimensions.
112+
- **strides**: stride lengths.
113+
- **offset**: index offset.
114+
- **order**: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style).
115+
116+
The invoked callback function is provided the following arguments:
117+
118+
- **value**: input ndarray element.
119+
- **indices**: current ndarray element indices.
120+
- **arr**: the input ndarray.
121+
122+
<!-- eslint-disable max-len -->
123+
124+
```javascript
125+
var Float64Array = require( '@stdlib/array/float64' );
126+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
127+
var maxBy = require( '@stdlib/stats/base/ndarray/max-by' );
128+
129+
// Define a callback function:
130+
function clbk( value ) {
131+
this.count += 1;
132+
return value * 2.0;
133+
}
134+
135+
// Create data buffers:
136+
var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
137+
var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] );
138+
139+
// Define the array shapes:
140+
var xsh = [ 1, 3, 2, 2 ];
141+
var ysh = [ 1, 3 ];
142+
143+
// Define the array strides:
144+
var sx = [ 12, 4, 2, 1 ];
145+
var sy = [ 3, 1 ];
146+
147+
// Define the index offsets:
148+
var ox = 0;
149+
var oy = 0;
150+
151+
// Create an input ndarray-like object:
152+
var x = {
153+
'dtype': 'float64',
154+
'data': xbuf,
155+
'shape': xsh,
156+
'strides': sx,
157+
'offset': ox,
158+
'order': 'row-major'
159+
};
160+
161+
// Create an output ndarray-like object:
162+
var y = {
163+
'dtype': 'float64',
164+
'data': ybuf,
165+
'shape': ysh,
166+
'strides': sy,
167+
'offset': oy,
168+
'order': 'row-major'
169+
};
170+
171+
// Define callback execution context:
172+
var ctx = {
173+
'count': 0
174+
};
175+
176+
// Perform a reduction:
177+
unaryReduceStrided1dBy( maxBy, [ x, y ], [ 2, 3 ], clbk, ctx );
178+
179+
var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order );
180+
// returns [ [ 8.0, 16.0, 24.0 ] ]
181+
182+
var count = ctx.count;
183+
// returns 12
184+
```
185+
186+
#### TODO: document factory method
187+
188+
</section>
189+
190+
<!-- /.usage -->
191+
192+
<section class="notes">
193+
194+
## Notes
195+
196+
- The output ndarray and any additional ndarray arguments are expected to have the same dimensions as the non-reduced dimensions of the input ndarray. When calling the reduction function, any additional ndarray arguments are provided as zero-dimensional ndarray-like objects.
197+
198+
- The reduction function is expected to have the following signature:
199+
200+
```text
201+
fcn( arrays[, options], clbk[, thisArg ] )
202+
```
203+
204+
where
205+
206+
- **arrays**: array containing a one-dimensional subarray of the input ndarray and any additional ndarray arguments as zero-dimensional ndarrays.
207+
- **options**: function options (_optional_).
208+
- **clbk**: callback function.
209+
- **thisArg**: callback execution context (_optional_).s
210+
211+
- For very high-dimensional ndarrays which are non-contiguous, one should consider copying the underlying data to contiguous memory before performing a reduction in order to achieve better performance.
212+
213+
</section>
214+
215+
<!-- /.notes -->
216+
217+
<section class="examples">
218+
219+
## Examples
220+
221+
<!-- eslint no-undef: "error" -->
222+
223+
```javascript
224+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
225+
var zeros = require( '@stdlib/array/base/zeros' );
226+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
227+
var maxBy = require( '@stdlib/stats/base/ndarray/max-by' );
228+
var unaryReduceStrided1dBy = require( '@stdlib/ndarray/base/unary-reduce-strided1d-by' );
229+
230+
function clbk( value ) {
231+
return value * 2;
232+
}
233+
234+
var N = 10;
235+
var x = {
236+
'dtype': 'generic',
237+
'data': discreteUniform( N, -5, 5, {
238+
'dtype': 'generic'
239+
}),
240+
'shape': [ 1, 5, 2 ],
241+
'strides': [ 10, 2, 1 ],
242+
'offset': 0,
243+
'order': 'row-major'
244+
};
245+
var y = {
246+
'dtype': 'generic',
247+
'data': zeros( 2 ),
248+
'shape': [ 1, 5 ],
249+
'strides': [ 5, 1 ],
250+
'offset': 0,
251+
'order': 'row-major'
252+
};
253+
254+
unaryReduceStrided1dBy( maxBy, [ x, y ], [ 2 ], clbk );
255+
256+
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
257+
console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) );
258+
```
259+
260+
</section>
261+
262+
<!-- /.examples -->
263+
264+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
265+
266+
<section class="related">
267+
268+
</section>
269+
270+
<!-- /.related -->
271+
272+
<section class="links">
273+
274+
</section>
275+
276+
<!-- /.links -->
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
2+
{{alias}}( fcn, arrays, dims[, options], clbk[, thisArg] )
3+
Performs a reduction over a list of specified dimensions in an input ndarray
4+
via a one-dimensional strided array reduction function, according to a
5+
callback function and assigns results to a provided output ndarray.
6+
7+
Each provided "ndarray" should be an object with the following properties:
8+
9+
- dtype: data type.
10+
- data: data buffer.
11+
- shape: dimensions.
12+
- strides: stride lengths.
13+
- offset: index offset.
14+
- order: specifies whether an ndarray is row-major (C-style) or column-major
15+
(Fortran-style).
16+
17+
The output ndarray and any additional ndarray arguments are expected to have
18+
the same dimensions as the non-reduced dimensions of the input ndarray. When
19+
calling the reduction function, any additional ndarray arguments are
20+
provided as zero-dimensional ndarray-like objects.
21+
22+
Parameters
23+
----------
24+
fcn: Function
25+
Function which will be applied to a one-dimensional subarray and should
26+
reduce the subarray to a single scalar value. The function should have
27+
the following signature:
28+
29+
fcn( arrays[, options], clbk[, thisArg] )
30+
31+
where
32+
33+
- arrays: array containing a one-dimensional subarray of the input
34+
ndarray and any additional ndarray arguments as zero-dimensional
35+
ndarrays.
36+
- options: function options.
37+
- clbk: callback function.
38+
- thisArg: callback execution context.
39+
40+
arrays: ArrayLikeObject<ndarray>
41+
Array-like object containing one input ndarray and one output ndarray,
42+
followed by any additional ndarray arguments.
43+
44+
dims: Array<integer>
45+
List of dimensions over which to perform a reduction.
46+
47+
options: Object (optional)
48+
Function options.
49+
50+
clbk: Function
51+
Callback function.
52+
53+
thisArg: any (optional)
54+
Callback execution context.
55+
56+
Examples
57+
--------
58+
// Define ndarray data and meta data...
59+
> var xbuf = new {{alias:@stdlib/array/float64}}( [ 1.0, 2.0, 3.0, 4.0 ] );
60+
> var ybuf = new {{alias:@stdlib/array/float64}}( [ 0.0 ] );
61+
> var dtype = 'float64';
62+
> var shx = [ 2, 2 ];
63+
> var shy = [];
64+
> var sx = [ 2, 1 ];
65+
> var sy = [ 0 ];
66+
> var ox = 0;
67+
> var oy = 0;
68+
> var order = 'row-major';
69+
70+
// Define a callback function...
71+
> function clbk( value ) { return value * 2.0; };
72+
73+
// Define a wrapper for a statistical function...
74+
> function fcn( arrays, clbk, thisArg ) {
75+
... var x = arrays[ 0 ];
76+
... var N = x.shape[ 0 ];
77+
... var d = x.data;
78+
... var s = x.strides[ 0 ];
79+
... var o = x.offset;
80+
... return {{alias:@stdlib/stats/base/max-by}}.ndarray( N, d, s, o, clbk, thisArg );
81+
... };
82+
83+
// Using minimal ndarray-like objects...
84+
> x = {
85+
... 'dtype': dtype,
86+
... 'data': xbuf,
87+
... 'shape': shx,
88+
... 'strides': sx,
89+
... 'offset': ox,
90+
... 'order': order
91+
... };
92+
> y = {
93+
... 'dtype': dtype,
94+
... 'data': ybuf,
95+
... 'shape': shy,
96+
... 'strides': sy,
97+
... 'offset': oy,
98+
... 'order': order
99+
... };
100+
> {{alias}}( fcn, [ x, y ], [ 0, 1 ], clbk );
101+
> y.data
102+
<Float64Array>[ 8.0 ]
103+
104+
See Also
105+
--------
106+

0 commit comments

Comments
 (0)