Skip to content

Commit 9929838

Browse files
committed
refactor: add support for ancillary ndarray arguments having trailing dimensions
--- 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: na - task: lint_repl_help status: skipped - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - 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: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent ed8da6f commit 9929838

4 files changed

Lines changed: 49 additions & 15 deletions

File tree

lib/node_modules/@stdlib/ndarray/base/unary-reduce-subarray-by/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,11 @@ var count = ctx.count;
194194

195195
## Notes
196196

197-
- 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+
- The output ndarray is expected to have the same dimensions as the non-reduced dimensions of the input ndarray.
198+
199+
- Any additional ndarray arguments are expected to have the same leading dimensions as the non-reduced dimensions of the input ndarray.
200+
201+
- When calling the reduction function, any additional ndarray arguments are provided as k-dimensional subarrays, where `k = M - N` with `M` being the number of dimensions in an ndarray argument and `N` being the number of non-reduced dimensions in the input ndarray. For example, if an input ndarray has three dimensions, the number of reduced dimensions is two, and an additional ndarray argument has one dimension, thus matching the number of non-reduced dimensions in the input ndarray, the reduction function is provided a zero-dimensional subarray as an additional ndarray argument. In the same scenario but where an additional ndarray argument has two dimensions, thus exceeding the number of non-reduced dimensions in the input ndarray, the reduction function is provided a one-dimensional subarray as an additional ndarray argument.
198202

199203
- The reduction function is expected to have the following signature:
200204

@@ -204,7 +208,7 @@ var count = ctx.count;
204208
205209
where
206210
207-
- **arrays**: array containing a subarray of the input ndarray and any additional ndarray arguments as zero-dimensional ndarrays.
211+
- **arrays**: array containing a subarray of the input ndarray and any additional ndarray arguments as subarrays.
208212
- **options**: function options (_optional_).
209213
- **wrappedCallback**: callback function. This function is a wrapper around a provided `clbk` argument.
210214

lib/node_modules/@stdlib/ndarray/base/unary-reduce-subarray-by/docs/repl.txt

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,24 @@
1414
- order: specifies whether an ndarray is row-major (C-style) or column-major
1515
(Fortran-style).
1616

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.
17+
The output ndarray is expected to have the same dimensions as the non-
18+
reduced dimensions of the input ndarray.
19+
20+
Any additional ndarray arguments are expected to have the same leading
21+
dimensions as the non-reduced dimensions of the input ndarray.
22+
23+
When calling the reduction function, any additional ndarray arguments are
24+
provided as k-dimensional subarrays, where `k = M - N` with `M` being the
25+
number of dimensions in an ndarray argument and `N` being the number of non-
26+
reduced dimensions in the input ndarray. For example, if an input ndarray
27+
has three dimensions, the number of reduced dimensions is two, and an
28+
additional ndarray argument has one dimension, thus matching the number of
29+
non-reduced dimensions in the input ndarray, the reduction function is
30+
provided a zero-dimensional subarray as an additional ndarray argument. In
31+
the same scenario but where an additional ndarray argument has two
32+
dimensions, thus exceeding the number of non-reduced dimensions in the input
33+
ndarray, the reduction function is provided a one-dimensional subarray as an
34+
additional ndarray argument.
2135

2236
Parameters
2337
----------
@@ -31,7 +45,7 @@
3145
where
3246

3347
- arrays: array containing a subarray of the input ndarray and any
34-
additional ndarray arguments as zero-dimensional ndarrays.
48+
additional ndarray arguments as subarrays.
3549
- options: function options.
3650
- wrappedCallback: callback function. This function is a wrapper around
3751
a provided `clbk` argument.

lib/node_modules/@stdlib/ndarray/base/unary-reduce-subarray-by/lib/initialize_array_views.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@
1818

1919
'use strict';
2020

21+
// MODULES //
22+
23+
var slice = require( '@stdlib/array/base/slice' );
24+
25+
2126
// MAIN //
2227

2328
/**
24-
* Initialize ndarray-like objects for representing zero-dimensional sub-array views of ancillary ndarray arguments.
29+
* Initialize ndarray-like objects for representing sub-array views of ancillary ndarray arguments.
2530
*
2631
* ## Notes
2732
*
@@ -30,20 +35,25 @@
3035
*
3136
* @private
3237
* @param {ArrayLikeObject<Object>} arrays - list of ndarray-like objects
38+
* @param {NonNegativeInteger} k - number of non-reduced dimensions
3339
* @param {Array<Object>} out - output array
3440
* @returns {Array<Object>} output array
3541
*/
36-
function initializeViews( arrays, out ) {
42+
function initializeViews( arrays, k, out ) {
43+
var sh;
44+
var N;
3745
var v;
3846
var i;
3947

4048
for ( i = 2; i < arrays.length; i++ ) {
4149
v = arrays[ i ];
50+
sh = v.shape;
51+
N = sh.length;
4252
out.push({
4353
'dtype': v.dtype,
4454
'data': v.data,
45-
'shape': [],
46-
'strides': [ 0 ],
55+
'shape': slice( sh, k, N ),
56+
'strides': ( N === k ) ? [ 0 ] : slice( v.strides, k, N ),
4757
'offset': v.offset,
4858
'order': v.order
4959
});

lib/node_modules/@stdlib/ndarray/base/unary-reduce-subarray-by/lib/main.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,16 @@ function unaryReduceSubarrayBy( fcn, arrays, dims, options, clbk, thisArg ) { //
392392
if ( M > ndims ) {
393393
throw new RangeError( format( 'invalid argument. Number of specified dimensions cannot exceed the number of dimensions in the input array. Number of dimensions: %d. Value: [%s].', ndims, join( dims, ',' ) ) );
394394
}
395-
// Verify that provided ndarrays have the expected number of dimensions...
395+
// Compute the number of non-reduced dimensions:
396396
K = ndims - M;
397-
for ( i = 1; i < N; i++ ) {
398-
if ( arr[ i ].shape.length !== K ) {
397+
398+
// Verify that the output ndarray has the expected number of dimensions...
399+
if ( arr[ 1 ].shape.length !== K ) {
400+
throw new Error( format( 'invalid argument. Arrays which are not being reduced must have the same number of non-reduced dimensions. Input array shape: [%s]. Number of non-reduced dimensions: %d. Array shape: [%s] (index: %d).', join( shx, ',' ), K, join( arr[ 1 ].shape, ',' ), 1 ) );
401+
}
402+
// Verify that any ancillary ndarrays have at least the number of non-reduced dimensions...
403+
for ( i = 2; i < N; i++ ) {
404+
if ( arr[ i ].shape.length < K ) {
399405
throw new Error( format( 'invalid argument. Arrays which are not being reduced must have the same number of non-reduced dimensions. Input array shape: [%s]. Number of non-reduced dimensions: %d. Array shape: [%s] (index: %d).', join( shx, ',' ), K, join( arr[ i ].shape, ',' ), i ) );
400406
}
401407
}
@@ -447,7 +453,7 @@ function unaryReduceSubarrayBy( fcn, arrays, dims, options, clbk, thisArg ) { //
447453
'order': x.order
448454
}
449455
];
450-
initializeViews( arr, views );
456+
initializeViews( arr, K, views );
451457

452458
// Determine whether we only have one loop dimension and can thus readily perform one-dimensional iteration...
453459
if ( K === 1 ) {

0 commit comments

Comments
 (0)