Skip to content

Commit 9457f9f

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: passed - 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 1a45821 commit 9457f9f

4 files changed

Lines changed: 51 additions & 19 deletions

File tree

lib/node_modules/@stdlib/ndarray/base/binary-reduce-strided1d/README.md

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

129129
## Notes
130130

131-
- The output ndarray and any additional ndarray arguments are expected to have the same dimensions as the non-reduced dimensions of the input ndarrays. When calling the reduction function, any additional ndarray arguments are provided as zero-dimensional ndarray-like objects.
131+
- The output ndarray is expected to have the same dimensions as the non-reduced dimensions of the input ndarrays.
132+
133+
- Any additional ndarray arguments are expected to have the same leading dimensions as the non-reduced dimensions of the input ndarrays.
134+
135+
- 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 ndarrays. For example, if an input ndarrays have 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 ndarrays, 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 ndarrays, the reduction function is provided a one-dimensional subarray as an additional ndarray argument.
132136

133137
- The reduction function is expected to have the following signature:
134138

@@ -138,7 +142,7 @@ Each provided ndarray should be an object with the following properties:
138142
139143
where
140144
141-
- **arrays**: array containing a one-dimensional subarray for each input ndarray and any additional ndarray arguments as zero-dimensional ndarrays.
145+
- **arrays**: array containing a one-dimensional subarray for each input ndarray and any additional ndarray arguments as subarrays.
142146
- **options**: function options (_optional_).
143147
144148
- 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/binary-reduce-strided1d/docs/repl.txt

Lines changed: 19 additions & 6 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 ndarrays.
19+
20+
Any additional ndarray arguments are expected to have the same leading
21+
dimensions as the non-reduced dimensions of the input ndarrays.
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 ndarrays. For example, if an input ndarrays
27+
have 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 ndarrays, 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+
ndarrays, the reduction function is provided a one-dimensional subarray as
34+
an additional ndarray argument.
2135

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

3347
- arrays: array containing a one-dimensional subarrays for each input
34-
ndarray and any additional ndarray arguments as zero-dimensional
35-
ndarrays.
48+
ndarray and any additional ndarray arguments as subarrays.
3649
- options: function options.
3750

3851
arrays: ArrayLikeObject<ndarray>

lib/node_modules/@stdlib/ndarray/base/binary-reduce-strided1d/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 = 3; 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/binary-reduce-strided1d/lib/main.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,6 @@ function binaryReduceStrided1d( fcn, arrays, dims, options ) { // eslint-disable
418418
throw new Error( format( 'invalid argument. Input arrays must have the same shape. First array shape: [%s]. Second array shape: [%s].', join( shx, ',' ), join( shy, ',' ) ) );
419419
}
420420
}
421-
422421
// Verify that we've been provided a list of unique dimension indices...
423422
M = dims.length;
424423
d = normalizeIndices( dims, ndims-1 );
@@ -433,12 +432,19 @@ function binaryReduceStrided1d( fcn, arrays, dims, options ) { // eslint-disable
433432
if ( M > ndims ) {
434433
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, ',' ) ) );
435434
}
436-
// Verify that provided ndarrays have the expected number of dimensions...
435+
// Compute the number of non-reduced dimensions:
437436
K = ndims - M;
437+
438+
// Verify that the output ndarray has the expected number of dimensions...
438439
if ( z.shape.length !== K ) {
439-
throw new Error( format( 'invalid argument. Output array must have the same number of non-reduced dimensions as input arrays. Input array shape: [%s]. Number of non-reduced dimensions: %d. Output array shape: [%s].', join( shx, ',' ), K, join( z.shape, ',' ) ) );
440+
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( z.shape, ',' ), 2 ) );
441+
}
442+
// Verify that any ancillary ndarrays have at least the number of non-reduced dimensions...
443+
for ( i = 3; i < N; i++ ) {
444+
if ( arr[ i ].shape.length < K ) {
445+
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 ) );
446+
}
440447
}
441-
442448
// Resolve the non-reduced ("loop") dimensions and associated strides:
443449
ldims = indicesComplement( shx.length, d );
444450
tmpx = takeIndexed2( shx, x.strides, ldims );
@@ -495,7 +501,7 @@ function binaryReduceStrided1d( fcn, arrays, dims, options ) { // eslint-disable
495501
'order': y.order
496502
}
497503
];
498-
initializeViews( arr, views );
504+
initializeViews( arr, K, views );
499505

500506
// Determine the strategy for reshaping sub-array views of the input arrays prior to performing a reduction:
501507
strategyX = reshapeStrategy( views[ 0 ] );
@@ -515,8 +521,6 @@ function binaryReduceStrided1d( fcn, arrays, dims, options ) { // eslint-disable
515521
}
516522
return BINARY[ K ]( fcn, arr, views, slx, sly, strategyX, strategyY, opts ); // eslint-disable-line max-len
517523
}
518-
sz = z.strides;
519-
520524
// 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...
521525
if ( ns === K-1 ) {
522526
// Get the index of the non-singleton dimension...
@@ -536,6 +540,7 @@ function binaryReduceStrided1d( fcn, arrays, dims, options ) { // eslint-disable
536540
}
537541
return BINARY[ 1 ]( fcn, arr, views, slx, sly, strategyX, strategyY, opts ); // eslint-disable-line max-len
538542
}
543+
sz = z.strides;
539544
iox = iterationOrder( slx ); // +/-1
540545
ioy = iterationOrder( sly ); // +/-1
541546
ioz = iterationOrder( sz ); // +/-1

0 commit comments

Comments
 (0)