Skip to content

Commit ed8da6f

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 94e56f0 commit ed8da6f

4 files changed

Lines changed: 50 additions & 17 deletions

File tree

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,11 @@ Each provided ndarray should be an object with the following properties:
117117

118118
## Notes
119119

120-
- 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.
120+
- The output ndarray is expected to have the same dimensions as the non-reduced dimensions of the input ndarray.
121+
122+
- Any additional ndarray arguments are expected to have the same leading dimensions as the non-reduced dimensions of the input ndarray.
123+
124+
- 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.
121125

122126
- The reduction function is expected to have the following signature:
123127

@@ -127,7 +131,7 @@ Each provided ndarray should be an object with the following properties:
127131
128132
where
129133
130-
- **arrays**: array containing a subarray of the input ndarray and any additional ndarray arguments as zero-dimensional ndarrays.
134+
- **arrays**: array containing a subarray of the input ndarray and any additional ndarray arguments as subarrays.
131135
- **options**: function options (_optional_).
132136
133137
- 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.

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

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

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

2135
Parameters
2236
----------
@@ -30,7 +44,7 @@
3044
where
3145

3246
- arrays: array containing a subarray of the input ndarray and any
33-
additional ndarray arguments as zero-dimensional ndarrays.
47+
additional ndarray arguments as subarrays.
3448
- options: function options.
3549

3650
arrays: ArrayLikeObject<ndarray>

lib/node_modules/@stdlib/ndarray/base/unary-reduce-subarray/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/lib/main.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,16 @@ function unaryReduceSubarray( fcn, arrays, dims, options ) { // eslint-disable-l
348348
if ( M > ndims ) {
349349
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, ',' ) ) );
350350
}
351-
// Verify that provided ndarrays have the expected number of dimensions...
351+
// Compute the number of non-reduced dimensions:
352352
K = ndims - M;
353-
for ( i = 1; i < N; i++ ) {
354-
if ( arr[ i ].shape.length !== K ) {
353+
354+
// Verify that the output ndarray has the expected number of dimensions...
355+
if ( arr[ 1 ].shape.length !== K ) {
356+
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 ) );
357+
}
358+
// Verify that any ancillary ndarrays have at least the number of non-reduced dimensions...
359+
for ( i = 2; i < N; i++ ) {
360+
if ( arr[ i ].shape.length < K ) {
355361
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 ) );
356362
}
357363
}
@@ -406,7 +412,7 @@ function unaryReduceSubarray( fcn, arrays, dims, options ) { // eslint-disable-l
406412
'order': x.order
407413
}
408414
];
409-
initializeViews( arr, views );
415+
initializeViews( arr, K, views );
410416

411417
// Determine whether we only have one loop dimension and can thus readily perform one-dimensional iteration...
412418
if ( K === 1 ) {
@@ -415,8 +421,6 @@ function unaryReduceSubarray( fcn, arrays, dims, options ) { // eslint-disable-l
415421
}
416422
return UNARY[ K ]( fcn, arr, views, sl, opts );
417423
}
418-
sy = y.strides;
419-
420424
// Determine whether the loop dimensions have only **one** non-singleton dimension (e.g., shape=[10,1,1,1]) so that we can treat loop iteration as being equivalent to one-dimensional iteration...
421425
if ( ns === K-1 ) {
422426
// Get the index of the non-singleton dimension...
@@ -435,6 +439,7 @@ function unaryReduceSubarray( fcn, arrays, dims, options ) { // eslint-disable-l
435439
}
436440
return UNARY[ 1 ]( fcn, arr, views, sl, opts );
437441
}
442+
sy = y.strides;
438443
iox = iterationOrder( sl ); // +/-1
439444
ioy = iterationOrder( sy ); // +/-1
440445

0 commit comments

Comments
 (0)