Skip to content

Commit c8df03c

Browse files
committed
refactor: add support for ancilliary ndarrays which decompose into subarrays
--- 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 9d4553a commit c8df03c

File tree

5 files changed

+50
-17
lines changed

5 files changed

+50
-17
lines changed

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

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

126126
## Notes
127127

128-
- 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.
128+
- The output ndarray is expected to have the same dimensions as the non-reduced dimensions of the input ndarray.
129+
130+
- Any additional ndarray arguments are expected to have the same leading dimensions as the non-reduced dimensions of the input ndarray.
131+
132+
- 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.
129133

130134
- The reduction function is expected to have the following signature:
131135

@@ -135,7 +139,7 @@ Each provided ndarray should be an object with the following properties:
135139
136140
where
137141
138-
- **arrays**: array containing a one-dimensional subarray of the input ndarray and any additional ndarray arguments as zero-dimensional ndarrays.
142+
- **arrays**: array containing a one-dimensional subarray of the input ndarray and any additional ndarray arguments as subarrays.
139143
- **options**: function options (_optional_).
140144
141145
- 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-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 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,8 +45,7 @@
3145
where
3246

3347
- arrays: array containing a one-dimensional subarray of the 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/unary-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 = 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-strided1d/lib/main.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,16 @@ function unaryReduceStrided1d( fcn, arrays, dims, options ) { // eslint-disable-
374374
if ( M > ndims ) {
375375
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, ',' ) ) );
376376
}
377-
// Verify that provided ndarrays have the expected number of dimensions...
377+
// Compute the number of non-reduced dimensions:
378378
K = ndims - M;
379-
for ( i = 1; i < N; i++ ) {
380-
if ( arr[ i ].shape.length !== K ) {
379+
380+
// Verify that the output ndarray has the expected number of dimensions...
381+
if ( arr[ 1 ].shape.length !== K ) {
382+
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 ) );
383+
}
384+
// Verify that any ancillary ndarrays have at least the number of non-reduced dimensions...
385+
for ( i = 2; i < N; i++ ) {
386+
if ( arr[ i ].shape.length < K ) {
381387
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 ) );
382388
}
383389
}
@@ -425,7 +431,7 @@ function unaryReduceStrided1d( fcn, arrays, dims, options ) { // eslint-disable-
425431
'order': x.order
426432
}
427433
];
428-
initializeViews( arr, views );
434+
initializeViews( arr, K, views );
429435

430436
// Determine the strategy for reshaping sub-array views of the input array prior to performing a reduction:
431437
strategy = reshapeStrategy( views[ 0 ] );

lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d/lib/reshape_strategy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ function copy( len, workspace ) {
186186
*
187187
* @private
188188
* @param {ndarrayLike} x - input ndarray
189-
* @param {string} x.dtype - input ndarray data type
189+
* @param {*} x.dtype - input ndarray data type
190190
* @param {Collection} x.data - input ndarray data buffer
191191
* @param {NonNegativeIntegerArray} x.shape - input ndarray shape
192192
* @param {IntegerArray} x.strides - input ndarray strides

0 commit comments

Comments
 (0)