diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/README.md b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/README.md new file mode 100644 index 000000000000..c5310d4c7ea2 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/README.md @@ -0,0 +1,278 @@ + + +# unaryReduceStrided1dBy + +> Perform a reduction over a list of specified dimensions in an input ndarray via a one-dimensional strided array reduction function accepting a callback and assign results to a provided output ndarray. + +
+ +
+ + + +
+ +## Usage + +```javascript +var unaryReduceStrided1dBy = require( '@stdlib/ndarray/base/unary-reduce-strided1d-by' ); +``` + +#### unaryReduceStrided1dBy( fcn, arrays, dims\[, options], clbk\[, thisArg] ) + +Performs a reduction over a list of specified dimensions in an input ndarray via a one-dimensional strided array reduction function accepting a callback and assigns results to a provided output ndarray. + + + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); + +// Define a callback function: +function clbk( value ) { + return value * 2.0; +} + +// Create data buffers: +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 ] ); +var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); + +// Define the array shapes: +var xsh = [ 1, 3, 2, 2 ]; +var ysh = [ 1, 3 ]; + +// Define the array strides: +var sx = [ 12, 4, 2, 1 ]; +var sy = [ 3, 1 ]; + +// Define the index offsets: +var ox = 0; +var oy = 0; + +// Create an input ndarray-like object: +var x = { + 'dtype': 'float64', + 'data': xbuf, + 'shape': xsh, + 'strides': sx, + 'offset': ox, + 'order': 'row-major' +}; + +// Create an output ndarray-like object: +var y = { + 'dtype': 'float64', + 'data': ybuf, + 'shape': ysh, + 'strides': sy, + 'offset': oy, + 'order': 'row-major' +}; + +// Perform a reduction: +unaryReduceStrided1dBy( maxBy, [ x, y ], [ 2, 3 ], clbk ); + +var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +// returns [ [ 8.0, 16.0, 24.0 ] ] +``` + +The function accepts the following arguments: + +- **fcn**: function which will be applied to a one-dimensional subarray and should reduce the subarray to a single scalar value. +- **arrays**: array-like object containing one input ndarray and one output ndarray, followed by any additional ndarray arguments. +- **dims**: list of dimensions over which to perform a reduction. +- **options**: function options which are passed through to `fcn` (_optional_). +- **clbk**: callback function. +- **thisArg**: callback execution context (_optional_). + +Each provided ndarray should be an object with the following properties: + +- **dtype**: data type. +- **data**: data buffer. +- **shape**: dimensions. +- **strides**: stride lengths. +- **offset**: index offset. +- **order**: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style). + +The invoked callback function is provided the following arguments: + +- **value**: current array element. +- **indices**: current array element indices. +- **arr**: the input ndarray. + +To set the callback execution context, provide a `thisArg`. + + + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); + +// Define a callback function: +function clbk( value ) { + this.count += 1; + return value * 2.0; +} + +// Create data buffers: +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 ] ); +var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); + +// Define the array shapes: +var xsh = [ 1, 3, 2, 2 ]; +var ysh = [ 1, 3 ]; + +// Define the array strides: +var sx = [ 12, 4, 2, 1 ]; +var sy = [ 3, 1 ]; + +// Define the index offsets: +var ox = 0; +var oy = 0; + +// Create an input ndarray-like object: +var x = { + 'dtype': 'float64', + 'data': xbuf, + 'shape': xsh, + 'strides': sx, + 'offset': ox, + 'order': 'row-major' +}; + +// Create an output ndarray-like object: +var y = { + 'dtype': 'float64', + 'data': ybuf, + 'shape': ysh, + 'strides': sy, + 'offset': oy, + 'order': 'row-major' +}; + +// Define callback execution context: +var ctx = { + 'count': 0 +}; + +// Perform a reduction: +unaryReduceStrided1dBy( maxBy, [ x, y ], [ 2, 3 ], clbk, ctx ); + +var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +// returns [ [ 8.0, 16.0, 24.0 ] ] + +var count = ctx.count; +// returns 12 +``` + +#### TODO: document factory method + +
+ + + +
+ +## Notes + +- 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. + +- The reduction function is expected to have the following signature: + + ```text + fcn( arrays[, options], clbk[, thisArg ] ) + ``` + + where + + - **arrays**: array containing a one-dimensional subarray of the input ndarray and any additional ndarray arguments as zero-dimensional ndarrays. + - **options**: function options (_optional_). + - **clbk**: callback function. + - **thisArg**: callback execution context (_optional_). + +- 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. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +var unaryReduceStrided1dBy = require( '@stdlib/ndarray/base/unary-reduce-strided1d-by' ); + +function clbk( value ) { + return value * 2.0; +} + +var N = 10; +var x = { + 'dtype': 'generic', + 'data': discreteUniform( N, -5, 5, { + 'dtype': 'generic' + }), + 'shape': [ 1, 5, 2 ], + 'strides': [ 10, 2, 1 ], + 'offset': 0, + 'order': 'row-major' +}; +var y = { + 'dtype': 'generic', + 'data': zeros( 2 ), + 'shape': [ 1, 5 ], + 'strides': [ 5, 1 ], + 'offset': 0, + 'order': 'row-major' +}; + +unaryReduceStrided1dBy( maxBy, [ x, y ], [ 2 ], clbk ); + +console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) ); +console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) ); +``` + +
+ + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/docs/repl.txt new file mode 100644 index 000000000000..0e1952fdeeae --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/docs/repl.txt @@ -0,0 +1,106 @@ + +{{alias}}( fcn, arrays, dims[, options], clbk[, thisArg] ) + Performs a reduction over a list of specified dimensions in an input ndarray + via a one-dimensional strided array reduction function accepting a callback + and assigns results to a provided output ndarray. + + Each provided "ndarray" should be an object with the following properties: + + - dtype: data type. + - data: data buffer. + - shape: dimensions. + - strides: stride lengths. + - offset: index offset. + - order: specifies whether an ndarray is row-major (C-style) or column-major + (Fortran-style). + + 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. + + Parameters + ---------- + fcn: Function + Function which will be applied to a one-dimensional subarray and should + reduce the subarray to a single scalar value. The function should have + the following signature: + + fcn( arrays[, options], clbk[, thisArg] ) + + where + + - arrays: array containing a one-dimensional subarray of the input + ndarray and any additional ndarray arguments as zero-dimensional + ndarrays. + - options: function options. + - clbk: callback function. + - thisArg: callback execution context. + + arrays: ArrayLikeObject + Array-like object containing one input ndarray and one output ndarray, + followed by any additional ndarray arguments. + + dims: Array + List of dimensions over which to perform a reduction. + + options: Object (optional) + Function options. + + clbk: Function + Callback function. + + thisArg: any (optional) + Callback execution context. + + Examples + -------- + // Define ndarray data and meta data... + > var xbuf = new {{alias:@stdlib/array/float64}}( [ 1.0, 2.0, 3.0, 4.0 ] ); + > var ybuf = new {{alias:@stdlib/array/float64}}( [ 0.0 ] ); + > var dtype = 'float64'; + > var shx = [ 2, 2 ]; + > var shy = []; + > var sx = [ 2, 1 ]; + > var sy = [ 0 ]; + > var ox = 0; + > var oy = 0; + > var order = 'row-major'; + + // Define a callback function... + > function clbk( value ) { return value * 2.0; }; + + // Define a wrapper for a statistical function... + > function fcn( arrays, cb, ctx ) { + ... var x = arrays[ 0 ]; + ... var N = x.shape[ 0 ]; + ... var d = x.data; + ... var s = x.strides[ 0 ]; + ... var o = x.offset; + ... return {{alias:@stdlib/stats/base/max-by}}.ndarray( N, d, s, o, cb, ctx ); + ... }; + + // Using minimal ndarray-like objects... + > x = { + ... 'dtype': dtype, + ... 'data': xbuf, + ... 'shape': shx, + ... 'strides': sx, + ... 'offset': ox, + ... 'order': order + ... }; + > y = { + ... 'dtype': dtype, + ... 'data': ybuf, + ... 'shape': shy, + ... 'strides': sy, + ... 'offset': oy, + ... 'order': order + ... }; + > {{alias}}( fcn, [ x, y ], [ 0, 1 ], clbk ); + > y.data + [ 8.0 ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/examples/index.js new file mode 100644 index 000000000000..52babe071b17 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/examples/index.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +var unaryReduceStrided1dBy = require( './../lib' ); + +function clbk( value ) { + return value * 2.0; +} + +var N = 10; +var x = { + 'dtype': 'generic', + 'data': discreteUniform( N, -5, 5, { + 'dtype': 'generic' + }), + 'shape': [ 1, 5, 2 ], + 'strides': [ 10, 2, 1 ], + 'offset': 0, + 'order': 'row-major' +}; +var y = { + 'dtype': 'generic', + 'data': zeros( 5 ), + 'shape': [ 1, 5 ], + 'strides': [ 5, 1 ], + 'offset': 0, + 'order': 'row-major' +}; + +unaryReduceStrided1dBy( maxBy, [ x, y ], [ 2 ], clbk ); + +console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) ); +console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) ); diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/0d.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/0d.js new file mode 100644 index 000000000000..499964535ea7 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/0d.js @@ -0,0 +1,138 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len */ + +'use strict'; + +// MODULES // + +var without = require( '@stdlib/array/base/without' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over a list of specified dimensions in an input ndarray according to a callback function and assigns results to a provided output ndarray. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {thisArg} thisArg - callback execution context +* @returns {void} +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); +* var ybuf = new Float64Array( [ 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 2, 2 ]; +* var ysh = []; +* +* // Define the array strides: +* var sx = [ 2, 1 ]; +* var sy = [ 0 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major' +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major' +* }; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = []; +* var cdims = [ 0, 1 ]; +* +* // Perform a reduction: +* unary0d( maxBy, [ x, y ], strategy, ibuf, ldims, cdims, {}, false, clbk, {} ); +* +* var v = y.data; +* // returns [ 8.0 ] +*/ +function unary0d( fcn, arrays, strategy, ibuf, ldims, cdims, opts, hasOpts, clbk, thisArg ) { + var arr; + var x; + var y; + var f; + + x = arrays[ 0 ]; + y = arrays[ 1 ]; + + f = wrap( x.ref, x, ibuf, ldims, [], cdims, clbk, thisArg ); + + arr = without( arrays, 1 ); + arr[ 0 ] = strategy( x ); + + y.data[ y.offset ] = ( hasOpts ) ? fcn( arr, opts, f ) : fcn( arr, f ); +} + + +// EXPORTS // + +module.exports = unary0d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/0d_accessors.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/0d_accessors.js new file mode 100644 index 000000000000..5fc701ad63cc --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/0d_accessors.js @@ -0,0 +1,145 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len */ + +'use strict'; + +// MODULES // + +var without = require( '@stdlib/array/base/without' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over a list of specified dimensions in an input ndarray according to a callback function and assigns results to a provided output ndarray. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {thisArg} thisArg - callback execution context +* @returns {void} +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var accessors = require( '@stdlib/array/base/accessors' ); +* var Float64Array = require( '@stdlib/array/float64' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* var xbuf = toAccessorArray( new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ) ); +* var ybuf = toAccessorArray( new Float64Array( [ 0.0 ] ) ); +* +* // Define the array shapes: +* var xsh = [ 2, 2 ]; +* var ysh = []; +* +* // Define the array strides: +* var sx = [ 2, 1 ]; +* var sy = [ 0 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major', +* 'accessors': accessors( xbuf ).accessors +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major', +* 'accessors': accessors( ybuf ).accessors +* }; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = []; +* var cdims = [ 0, 1 ]; +* +* // Perform a reduction: +* unary0d( maxBy, [ x, y ], strategy, ibuf, ldims, cdims, {}, false, clbk, {} ); +* +* var v = y.data.get( 0 ); +* // returns 8.0 +*/ +function unary0d( fcn, arrays, strategy, ibuf, ldims, cdims, opts, hasOpts, clbk, thisArg ) { + var arr; + var x; + var y; + var f; + + x = arrays[ 0 ]; + y = arrays[ 1 ]; + + f = wrap( x.ref, x, ibuf, ldims, [], cdims, clbk, thisArg ); + + arr = without( arrays, 1 ); + arr[ 0 ] = strategy( x ); + + if ( hasOpts ) { + y.accessors[ 1 ]( y.data, y.offset, fcn( arr, opts, f ) ); + } else { + y.accessors[ 1 ]( y.data, y.offset, fcn( arr, f ) ); + } +} + + +// EXPORTS // + +module.exports = unary0d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/10d.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/10d.js new file mode 100644 index 000000000000..a94245633a65 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/10d.js @@ -0,0 +1,310 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params, max-depth, max-statements */ + +'use strict'; + +// MODULES // + +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to callback function and assigns results to a provided output ndarray. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {boolean} isRowMajor - boolean indicating whether the input ndarray is row-major +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 12, 12, 12, 12, 12, 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 3, 3, 3, 3, 3, 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major' +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major' +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]; +* var cdims = [ 10, 11 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 12, 12, 12, 12, 12, 12, 12, 4 ]; +* +* // Perform a reduction: +* unary10d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, true, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ [ [ [ [ [ [ [ 8.0, 16.0, 24.0 ] ] ] ] ] ] ] ] ] ] +*/ +function unary10d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, isRowMajor, opts, hasOpts, clbk, thisArg ) { + var ybuf; + var dv0; + var dv1; + var dv2; + var dv3; + var dv4; + var dv5; + var dv6; + var dv7; + var dv8; + var dv9; + var sh; + var S0; + var S1; + var S2; + var S3; + var S4; + var S5; + var S6; + var S7; + var S8; + var S9; + var sv; + var iv; + var i0; + var i1; + var i2; + var i3; + var i4; + var i5; + var i6; + var i7; + var i8; + var i9; + var x; + var y; + var v; + var i; + var f; + + // Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop... + + // Cache a reference to the input ndarray: + x = arrays[ 0 ]; + + // Resolve the output ndarray and associated shape: + y = arrays[ 1 ]; + sh = y.shape; + + // Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments... + if ( isRowMajor ) { + // For row-major ndarrays, the last dimensions have the fastest changing indices... + S0 = sh[ 9 ]; + S1 = sh[ 8 ]; + S2 = sh[ 7 ]; + S3 = sh[ 6 ]; + S4 = sh[ 5 ]; + S5 = sh[ 4 ]; + S6 = sh[ 3 ]; + S7 = sh[ 2 ]; + S8 = sh[ 1 ]; + S9 = sh[ 0 ]; + dv0 = [ strides[9] ]; // offset increment for innermost loop + dv1 = [ strides[8] - ( S0*strides[9] ) ]; + dv2 = [ strides[7] - ( S1*strides[8] ) ]; + dv3 = [ strides[6] - ( S2*strides[7] ) ]; + dv4 = [ strides[5] - ( S3*strides[6] ) ]; + dv5 = [ strides[4] - ( S4*strides[5] ) ]; + dv6 = [ strides[3] - ( S5*strides[4] ) ]; + dv7 = [ strides[2] - ( S6*strides[3] ) ]; + dv8 = [ strides[1] - ( S7*strides[2] ) ]; + dv9 = [ strides[0] - ( S8*strides[1] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[9] ); + dv1.push( sv[8] - ( S0*sv[9] ) ); + dv2.push( sv[7] - ( S1*sv[8] ) ); + dv3.push( sv[6] - ( S2*sv[7] ) ); + dv4.push( sv[5] - ( S3*sv[6] ) ); + dv5.push( sv[4] - ( S4*sv[5] ) ); + dv6.push( sv[3] - ( S5*sv[4] ) ); + dv7.push( sv[2] - ( S6*sv[3] ) ); + dv8.push( sv[1] - ( S7*sv[2] ) ); + dv9.push( sv[0] - ( S8*sv[1] ) ); + } + } else { // order === 'column-major' + // For column-major ndarrays, the first dimensions have the fastest changing indices... + S0 = sh[ 0 ]; + S1 = sh[ 1 ]; + S2 = sh[ 2 ]; + S3 = sh[ 3 ]; + S4 = sh[ 4 ]; + S5 = sh[ 5 ]; + S6 = sh[ 6 ]; + S7 = sh[ 7 ]; + S8 = sh[ 8 ]; + S9 = sh[ 9 ]; + dv0 = [ strides[0] ]; // offset increment for innermost loop + dv1 = [ strides[1] - ( S0*strides[0] ) ]; + dv2 = [ strides[2] - ( S1*strides[1] ) ]; + dv3 = [ strides[3] - ( S2*strides[2] ) ]; + dv4 = [ strides[4] - ( S3*strides[3] ) ]; + dv5 = [ strides[5] - ( S4*strides[4] ) ]; + dv6 = [ strides[6] - ( S5*strides[5] ) ]; + dv7 = [ strides[7] - ( S6*strides[6] ) ]; + dv8 = [ strides[8] - ( S7*strides[7] ) ]; + dv9 = [ strides[9] - ( S8*strides[8] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[0] ); + dv1.push( sv[1] - ( S0*sv[0] ) ); + dv2.push( sv[2] - ( S1*sv[1] ) ); + dv3.push( sv[3] - ( S2*sv[2] ) ); + dv4.push( sv[4] - ( S3*sv[3] ) ); + dv5.push( sv[5] - ( S4*sv[4] ) ); + dv6.push( sv[6] - ( S5*sv[5] ) ); + dv7.push( sv[7] - ( S6*sv[6] ) ); + dv8.push( sv[8] - ( S7*sv[7] ) ); + dv9.push( sv[9] - ( S8*sv[8] ) ); + } + } + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + iv = offsets( arrays ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Iterate over the non-reduced ndarray dimensions... + for ( i9 = 0; i9 < S9; i9++ ) { + for ( i8 = 0; i8 < S8; i8++ ) { + for ( i7 = 0; i7 < S7; i7++ ) { + for ( i6 = 0; i6 < S6; i6++ ) { + for ( i5 = 0; i5 < S5; i5++ ) { + for ( i4 = 0; i4 < S4; i4++ ) { + for ( i3 = 0; i3 < S3; i3++ ) { + for ( i2 = 0; i2 < S2; i2++ ) { + for ( i1 = 0; i1 < S1; i1++ ) { + for ( i0 = 0; i0 < S0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ i9, i8, i7, i6, i5, i4, i3, i2, i1, i0 ], cdims, clbk, thisArg ); + ybuf[ iv[1] ] = ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + incrementOffsets( iv, dv2 ); + } + incrementOffsets( iv, dv3 ); + } + incrementOffsets( iv, dv4 ); + } + incrementOffsets( iv, dv5 ); + } + incrementOffsets( iv, dv6 ); + } + incrementOffsets( iv, dv7 ); + } + incrementOffsets( iv, dv8 ); + } + incrementOffsets( iv, dv9 ); + } +} + + +// EXPORTS // + +module.exports = unary10d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/10d_accessors.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/10d_accessors.js new file mode 100644 index 000000000000..c9062b9c5230 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/10d_accessors.js @@ -0,0 +1,318 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params, max-depth, max-statements */ + +'use strict'; + +// MODULES // + +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to callback function and assigns results to a provided output ndarray. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {boolean} isRowMajor - boolean indicating whether the input ndarray is row-major +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var accessors = require( '@stdlib/array/base/accessors' ); +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* var xbuf = toAccessorArray( 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 ] ) ); +* var ybuf = toAccessorArray( new Float64Array( [ 0.0, 0.0, 0.0 ] ) ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 12, 12, 12, 12, 12, 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 3, 3, 3, 3, 3, 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major', +* 'accessors': accessors( xbuf ).accessors +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major', +* 'accessors': accessors( ybuf ).accessors +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]; +* var cdims = [ 10, 11 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 12, 12, 12, 12, 12, 12, 12, 4 ]; +* +* // Perform a reduction: +* unary10d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, true, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ [ [ [ [ [ [ [ 8.0, 16.0, 24.0 ] ] ] ] ] ] ] ] ] ] +*/ +function unary10d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, isRowMajor, opts, hasOpts, clbk, thisArg ) { + var ybuf; + var set; + var dv0; + var dv1; + var dv2; + var dv3; + var dv4; + var dv5; + var dv6; + var dv7; + var dv8; + var dv9; + var sh; + var S0; + var S1; + var S2; + var S3; + var S4; + var S5; + var S6; + var S7; + var S8; + var S9; + var sv; + var iv; + var i0; + var i1; + var i2; + var i3; + var i4; + var i5; + var i6; + var i7; + var i8; + var i9; + var x; + var y; + var v; + var i; + var f; + + // Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop... + + // Cache a reference to the input ndarray: + x = arrays[ 0 ]; + + // Resolve the output ndarray and associated shape: + y = arrays[ 1 ]; + sh = y.shape; + + // Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments... + if ( isRowMajor ) { + // For row-major ndarrays, the last dimensions have the fastest changing indices... + S0 = sh[ 9 ]; + S1 = sh[ 8 ]; + S2 = sh[ 7 ]; + S3 = sh[ 6 ]; + S4 = sh[ 5 ]; + S5 = sh[ 4 ]; + S6 = sh[ 3 ]; + S7 = sh[ 2 ]; + S8 = sh[ 1 ]; + S9 = sh[ 0 ]; + dv0 = [ strides[9] ]; // offset increment for innermost loop + dv1 = [ strides[8] - ( S0*strides[9] ) ]; + dv2 = [ strides[7] - ( S1*strides[8] ) ]; + dv3 = [ strides[6] - ( S2*strides[7] ) ]; + dv4 = [ strides[5] - ( S3*strides[6] ) ]; + dv5 = [ strides[4] - ( S4*strides[5] ) ]; + dv6 = [ strides[3] - ( S5*strides[4] ) ]; + dv7 = [ strides[2] - ( S6*strides[3] ) ]; + dv8 = [ strides[1] - ( S7*strides[2] ) ]; + dv9 = [ strides[0] - ( S8*strides[1] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[9] ); + dv1.push( sv[8] - ( S0*sv[9] ) ); + dv2.push( sv[7] - ( S1*sv[8] ) ); + dv3.push( sv[6] - ( S2*sv[7] ) ); + dv4.push( sv[5] - ( S3*sv[6] ) ); + dv5.push( sv[4] - ( S4*sv[5] ) ); + dv6.push( sv[3] - ( S5*sv[4] ) ); + dv7.push( sv[2] - ( S6*sv[3] ) ); + dv8.push( sv[1] - ( S7*sv[2] ) ); + dv9.push( sv[0] - ( S8*sv[1] ) ); + } + } else { // order === 'column-major' + // For column-major ndarrays, the first dimensions have the fastest changing indices... + S0 = sh[ 0 ]; + S1 = sh[ 1 ]; + S2 = sh[ 2 ]; + S3 = sh[ 3 ]; + S4 = sh[ 4 ]; + S5 = sh[ 5 ]; + S6 = sh[ 6 ]; + S7 = sh[ 7 ]; + S8 = sh[ 8 ]; + S9 = sh[ 9 ]; + dv0 = [ strides[0] ]; // offset increment for innermost loop + dv1 = [ strides[1] - ( S0*strides[0] ) ]; + dv2 = [ strides[2] - ( S1*strides[1] ) ]; + dv3 = [ strides[3] - ( S2*strides[2] ) ]; + dv4 = [ strides[4] - ( S3*strides[3] ) ]; + dv5 = [ strides[5] - ( S4*strides[4] ) ]; + dv6 = [ strides[6] - ( S5*strides[5] ) ]; + dv7 = [ strides[7] - ( S6*strides[6] ) ]; + dv8 = [ strides[8] - ( S7*strides[7] ) ]; + dv9 = [ strides[9] - ( S8*strides[8] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[0] ); + dv1.push( sv[1] - ( S0*sv[0] ) ); + dv2.push( sv[2] - ( S1*sv[1] ) ); + dv3.push( sv[3] - ( S2*sv[2] ) ); + dv4.push( sv[4] - ( S3*sv[3] ) ); + dv5.push( sv[5] - ( S4*sv[4] ) ); + dv6.push( sv[6] - ( S5*sv[5] ) ); + dv7.push( sv[7] - ( S6*sv[6] ) ); + dv8.push( sv[8] - ( S7*sv[7] ) ); + dv9.push( sv[9] - ( S8*sv[8] ) ); + } + } + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + iv = offsets( arrays ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Cache accessors: + set = y.accessors[ 1 ]; + + // Iterate over the non-reduced ndarray dimensions... + for ( i9 = 0; i9 < S9; i9++ ) { + for ( i8 = 0; i8 < S8; i8++ ) { + for ( i7 = 0; i7 < S7; i7++ ) { + for ( i6 = 0; i6 < S6; i6++ ) { + for ( i5 = 0; i5 < S5; i5++ ) { + for ( i4 = 0; i4 < S4; i4++ ) { + for ( i3 = 0; i3 < S3; i3++ ) { + for ( i2 = 0; i2 < S2; i2++ ) { + for ( i1 = 0; i1 < S1; i1++ ) { + for ( i0 = 0; i0 < S0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ i9, i8, i7, i6, i5, i4, i3, i2, i1, i0 ], cdims, clbk, thisArg ); + set( ybuf, iv[1], ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ) ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + incrementOffsets( iv, dv2 ); + } + incrementOffsets( iv, dv3 ); + } + incrementOffsets( iv, dv4 ); + } + incrementOffsets( iv, dv5 ); + } + incrementOffsets( iv, dv6 ); + } + incrementOffsets( iv, dv7 ); + } + incrementOffsets( iv, dv8 ); + } + incrementOffsets( iv, dv9 ); + } +} + + +// EXPORTS // + +module.exports = unary10d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/10d_blocked.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/10d_blocked.js new file mode 100644 index 000000000000..f49f42f319b1 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/10d_blocked.js @@ -0,0 +1,428 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params, max-depth, max-statements, max-lines-per-function */ + +'use strict'; + +// MODULES // + +var loopOrder = require( '@stdlib/ndarray/base/unary-loop-interchange-order' ); +var blockSize = require( '@stdlib/ndarray/base/unary-tiling-block-size' ); +var takeIndexed = require( '@stdlib/array/base/take-indexed' ); +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to a callback function and assigns results to a provided output ndarray via loop blocking. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 12, 12, 12, 12, 12, 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 3, 3, 3, 3, 3, 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major' +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major' +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]; +* var cdims = [ 10, 11 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 12, 12, 12, 12, 12, 12, 12, 4 ]; +* +* // Perform a reduction: +* blockedunary10d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ [ [ [ [ [ [ [ 8.0, 16.0, 24.0 ] ] ] ] ] ] ] ] ] ] +*/ +function blockedunary10d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, opts, hasOpts, clbk, thisArg ) { + var bsize; + var ybuf; + var dv0; + var dv1; + var dv2; + var dv3; + var dv4; + var dv5; + var dv6; + var dv7; + var dv8; + var dv9; + var ov1; + var ov2; + var ov3; + var ov4; + var ov5; + var ov6; + var ov7; + var ov8; + var ov9; + var sh; + var s0; + var s1; + var s2; + var s3; + var s4; + var s5; + var s6; + var s7; + var s8; + var s9; + var sv; + var ov; + var iv; + var i0; + var i1; + var i2; + var i3; + var i4; + var i5; + var i6; + var i7; + var i8; + var i9; + var j0; + var j1; + var j2; + var j3; + var j4; + var j5; + var j6; + var j7; + var j8; + var j9; + var N; + var x; + var y; + var v; + var o; + var k; + var f; + + // Note on variable naming convention: S#, dv#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop... + + N = arrays.length; + x = arrays[ 0 ]; + y = arrays[ 1 ]; + + // Resolve the loop interchange order: + o = loopOrder( y.shape, strides, y.strides ); + sh = o.sh; + sv = [ o.sx, o.sy ]; + for ( k = 2; k < N; k++ ) { + sv.push( takeIndexed( arrays[k].strides, o.idx ) ); + } + // Determine the block size: + bsize = blockSize( x.dtype, y.dtype ); + + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + ov = offsets( arrays ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Cache offset increments for the innermost loop... + dv0 = []; + for ( k = 0; k < N; k++ ) { + dv0.push( sv[k][0] ); + } + // Initialize loop variables... + ov1 = zeros( N ); + ov2 = zeros( N ); + ov3 = zeros( N ); + ov4 = zeros( N ); + ov5 = zeros( N ); + ov6 = zeros( N ); + ov7 = zeros( N ); + ov8 = zeros( N ); + ov9 = zeros( N ); + dv1 = zeros( N ); + dv2 = zeros( N ); + dv3 = zeros( N ); + dv4 = zeros( N ); + dv5 = zeros( N ); + dv6 = zeros( N ); + dv7 = zeros( N ); + dv8 = zeros( N ); + dv9 = zeros( N ); + iv = zeros( N ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Iterate over blocks... + for ( j9 = sh[9]; j9 > 0; ) { + if ( j9 < bsize ) { + s9 = j9; + j9 = 0; + } else { + s9 = bsize; + j9 -= bsize; + } + for ( k = 0; k < N; k++ ) { + ov9[ k ] = ov[k] + ( j9*sv[k][9] ); + } + for ( j8 = sh[8]; j8 > 0; ) { + if ( j8 < bsize ) { + s8 = j8; + j8 = 0; + } else { + s8 = bsize; + j8 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv9 = sv[k][9] - ( s8*sv[k][8] ); + ov8[ k ] = ov9[k] + ( j8*sv[k][8] ); + } + for ( j7 = sh[7]; j7 > 0; ) { + if ( j7 < bsize ) { + s7 = j7; + j7 = 0; + } else { + s7 = bsize; + j7 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv8 = sv[k][8] - ( s7*sv[k][7] ); + ov7[ k ] = ov8[k] + ( j7*sv[k][7] ); + } + for ( j6 = sh[6]; j6 > 0; ) { + if ( j6 < bsize ) { + s6 = j6; + j6 = 0; + } else { + s6 = bsize; + j6 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv7 = sv[k][7] - ( s6*sv[k][6] ); + ov6[ k ] = ov7[k] + ( j6*sv[k][6] ); + } + for ( j5 = sh[5]; j5 > 0; ) { + if ( j5 < bsize ) { + s5 = j5; + j5 = 0; + } else { + s5 = bsize; + j5 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv6 = sv[k][6] - ( s5*sv[k][5] ); + ov5[ k ] = ov6[k] + ( j5*sv[k][5] ); + } + for ( j4 = sh[4]; j4 > 0; ) { + if ( j4 < bsize ) { + s4 = j4; + j4 = 0; + } else { + s4 = bsize; + j4 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv5 = sv[k][5] - ( s4*sv[k][4] ); + ov4[ k ] = ov5[k] + ( j4*sv[k][4] ); + } + for ( j3 = sh[3]; j3 > 0; ) { + if ( j3 < bsize ) { + s3 = j3; + j3 = 0; + } else { + s3 = bsize; + j3 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv4[ k ] = sv[k][4] - ( s3*sv[k][3] ); + ov3[ k ] = ov4[k] + ( j3*sv[k][3] ); + } + for ( j2 = sh[2]; j2 > 0; ) { + if ( j2 < bsize ) { + s2 = j2; + j2 = 0; + } else { + s2 = bsize; + j2 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv3[ k ] = sv[k][3] - ( s2*sv[k][2] ); + ov2[ k ] = ov3[k] + ( j2*sv[k][2] ); + } + for ( j1 = sh[1]; j1 > 0; ) { + if ( j1 < bsize ) { + s1 = j1; + j1 = 0; + } else { + s1 = bsize; + j1 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv2[ k ] = sv[k][2] - ( s1*sv[k][1] ); + ov1[ k ] = ov2[k] + ( j1*sv[k][1] ); + } + for ( j0 = sh[0]; j0 > 0; ) { + if ( j0 < bsize ) { + s0 = j0; + j0 = 0; + } else { + s0 = bsize; + j0 -= bsize; + } + // Compute index offsets and loop offset increments for the first ndarray elements in the current block... + for ( k = 0; k < N; k++ ) { + iv[ k ] = ov1[k] + ( j0*sv[k][0] ); + dv1[ k ] = sv[k][1] - ( s0*sv[k][0] ); + } + // Iterate over the non-reduced ndarray dimensions... + for ( i9 = 0; i9 < s9; i9++ ) { + for ( i8 = 0; i8 < s8; i8++ ) { + for ( i7 = 0; i7 < s7; i7++ ) { + for ( i6 = 0; i6 < s6; i6++ ) { + for ( i5 = 0; i5 < s5; i5++ ) { + for ( i4 = 0; i4 < s4; i4++ ) { + for ( i3 = 0; i3 < s3; i3++ ) { + for ( i2 = 0; i2 < s2; i2++ ) { + for ( i1 = 0; i1 < s1; i1++ ) { + for ( i0 = 0; i0 < s0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ j9+i9, j8+i8, j7+i7, j6+i6, j5+i5, j4+i4, j3+i3, j2+i2, j1+i1, j0+i0 ], cdims, clbk, thisArg ); + ybuf[ iv[1] ] = ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + incrementOffsets( iv, dv2 ); + } + incrementOffsets( iv, dv3 ); + } + incrementOffsets( iv, dv4 ); + } + incrementOffsets( iv, dv5 ); + } + incrementOffsets( iv, dv6 ); + } + incrementOffsets( iv, dv7 ); + } + incrementOffsets( iv, dv8 ); + } + incrementOffsets( iv, dv9 ); + } + } + } + } + } + } + } + } + } + } + } +} + + +// EXPORTS // + +module.exports = blockedunary10d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/10d_blocked_accessors.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/10d_blocked_accessors.js new file mode 100644 index 000000000000..8742d596d30d --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/10d_blocked_accessors.js @@ -0,0 +1,436 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params, max-depth, max-statements, max-lines-per-function */ + +'use strict'; + +// MODULES // + +var loopOrder = require( '@stdlib/ndarray/base/unary-loop-interchange-order' ); +var blockSize = require( '@stdlib/ndarray/base/unary-tiling-block-size' ); +var takeIndexed = require( '@stdlib/array/base/take-indexed' ); +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to a callback function and assigns results to a provided output ndarray via loop blocking. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var accessors = require( '@stdlib/array/base/accessors' ); +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 12, 12, 12, 12, 12, 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 3, 3, 3, 3, 3, 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major', +* 'accessors': accessors( xbuf ).accessors +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major', +* 'accessors': accessors( ybuf ).accessors +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]; +* var cdims = [ 10, 11 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 12, 12, 12, 12, 12, 12, 12, 4 ]; +* +* // Perform a reduction: +* blockedunary10d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ [ [ [ [ [ [ [ 8.0, 16.0, 24.0 ] ] ] ] ] ] ] ] ] ] +*/ +function blockedunary10d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, opts, hasOpts, clbk, thisArg ) { + var bsize; + var ybuf; + var set; + var dv0; + var dv1; + var dv2; + var dv3; + var dv4; + var dv5; + var dv6; + var dv7; + var dv8; + var dv9; + var ov1; + var ov2; + var ov3; + var ov4; + var ov5; + var ov6; + var ov7; + var ov8; + var ov9; + var sh; + var s0; + var s1; + var s2; + var s3; + var s4; + var s5; + var s6; + var s7; + var s8; + var s9; + var sv; + var ov; + var iv; + var i0; + var i1; + var i2; + var i3; + var i4; + var i5; + var i6; + var i7; + var i8; + var i9; + var j0; + var j1; + var j2; + var j3; + var j4; + var j5; + var j6; + var j7; + var j8; + var j9; + var N; + var x; + var y; + var v; + var o; + var k; + var f; + + // Note on variable naming convention: S#, dv#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop... + + N = arrays.length; + x = arrays[ 0 ]; + y = arrays[ 1 ]; + + // Resolve the loop interchange order: + o = loopOrder( y.shape, strides, y.strides ); + sh = o.sh; + sv = [ o.sx, o.sy ]; + for ( k = 2; k < N; k++ ) { + sv.push( takeIndexed( arrays[k].strides, o.idx ) ); + } + // Determine the block size: + bsize = blockSize( x.dtype, y.dtype ); + + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + ov = offsets( arrays ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Cache accessors: + set = y.accessors[ 1 ]; + + // Cache offset increments for the innermost loop... + dv0 = []; + for ( k = 0; k < N; k++ ) { + dv0.push( sv[k][0] ); + } + // Initialize loop variables... + ov1 = zeros( N ); + ov2 = zeros( N ); + ov3 = zeros( N ); + ov4 = zeros( N ); + ov5 = zeros( N ); + ov6 = zeros( N ); + ov7 = zeros( N ); + ov8 = zeros( N ); + ov9 = zeros( N ); + dv1 = zeros( N ); + dv2 = zeros( N ); + dv3 = zeros( N ); + dv4 = zeros( N ); + dv5 = zeros( N ); + dv6 = zeros( N ); + dv7 = zeros( N ); + dv8 = zeros( N ); + dv9 = zeros( N ); + iv = zeros( N ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Iterate over blocks... + for ( j9 = sh[9]; j9 > 0; ) { + if ( j9 < bsize ) { + s9 = j9; + j9 = 0; + } else { + s9 = bsize; + j9 -= bsize; + } + for ( k = 0; k < N; k++ ) { + ov9[ k ] = ov[k] + ( j9*sv[k][9] ); + } + for ( j8 = sh[8]; j8 > 0; ) { + if ( j8 < bsize ) { + s8 = j8; + j8 = 0; + } else { + s8 = bsize; + j8 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv9 = sv[k][9] - ( s8*sv[k][8] ); + ov8[ k ] = ov9[k] + ( j8*sv[k][8] ); + } + for ( j7 = sh[7]; j7 > 0; ) { + if ( j7 < bsize ) { + s7 = j7; + j7 = 0; + } else { + s7 = bsize; + j7 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv8 = sv[k][8] - ( s7*sv[k][7] ); + ov7[ k ] = ov8[k] + ( j7*sv[k][7] ); + } + for ( j6 = sh[6]; j6 > 0; ) { + if ( j6 < bsize ) { + s6 = j6; + j6 = 0; + } else { + s6 = bsize; + j6 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv7 = sv[k][7] - ( s6*sv[k][6] ); + ov6[ k ] = ov7[k] + ( j6*sv[k][6] ); + } + for ( j5 = sh[5]; j5 > 0; ) { + if ( j5 < bsize ) { + s5 = j5; + j5 = 0; + } else { + s5 = bsize; + j5 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv6 = sv[k][6] - ( s5*sv[k][5] ); + ov5[ k ] = ov6[k] + ( j5*sv[k][5] ); + } + for ( j4 = sh[4]; j4 > 0; ) { + if ( j4 < bsize ) { + s4 = j4; + j4 = 0; + } else { + s4 = bsize; + j4 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv5 = sv[k][5] - ( s4*sv[k][4] ); + ov4[ k ] = ov5[k] + ( j4*sv[k][4] ); + } + for ( j3 = sh[3]; j3 > 0; ) { + if ( j3 < bsize ) { + s3 = j3; + j3 = 0; + } else { + s3 = bsize; + j3 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv4[ k ] = sv[k][4] - ( s3*sv[k][3] ); + ov3[ k ] = ov4[k] + ( j3*sv[k][3] ); + } + for ( j2 = sh[2]; j2 > 0; ) { + if ( j2 < bsize ) { + s2 = j2; + j2 = 0; + } else { + s2 = bsize; + j2 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv3[ k ] = sv[k][3] - ( s2*sv[k][2] ); + ov2[ k ] = ov3[k] + ( j2*sv[k][2] ); + } + for ( j1 = sh[1]; j1 > 0; ) { + if ( j1 < bsize ) { + s1 = j1; + j1 = 0; + } else { + s1 = bsize; + j1 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv2[ k ] = sv[k][2] - ( s1*sv[k][1] ); + ov1[ k ] = ov2[k] + ( j1*sv[k][1] ); + } + for ( j0 = sh[0]; j0 > 0; ) { + if ( j0 < bsize ) { + s0 = j0; + j0 = 0; + } else { + s0 = bsize; + j0 -= bsize; + } + // Compute index offsets and loop offset increments for the first ndarray elements in the current block... + for ( k = 0; k < N; k++ ) { + iv[ k ] = ov1[k] + ( j0*sv[k][0] ); + dv1[ k ] = sv[k][1] - ( s0*sv[k][0] ); + } + // Iterate over the non-reduced ndarray dimensions... + for ( i9 = 0; i9 < s9; i9++ ) { + for ( i8 = 0; i8 < s8; i8++ ) { + for ( i7 = 0; i7 < s7; i7++ ) { + for ( i6 = 0; i6 < s6; i6++ ) { + for ( i5 = 0; i5 < s5; i5++ ) { + for ( i4 = 0; i4 < s4; i4++ ) { + for ( i3 = 0; i3 < s3; i3++ ) { + for ( i2 = 0; i2 < s2; i2++ ) { + for ( i1 = 0; i1 < s1; i1++ ) { + for ( i0 = 0; i0 < s0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ j9+i9, j8+i8, j7+i7, j6+i6, j5+i5, j4+i4, j3+i3, j2+i2, j1+i1, j0+i0 ], cdims, clbk, thisArg ); + set( ybuf, iv[1], ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ) ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + incrementOffsets( iv, dv2 ); + } + incrementOffsets( iv, dv3 ); + } + incrementOffsets( iv, dv4 ); + } + incrementOffsets( iv, dv5 ); + } + incrementOffsets( iv, dv6 ); + } + incrementOffsets( iv, dv7 ); + } + incrementOffsets( iv, dv8 ); + } + incrementOffsets( iv, dv9 ); + } + } + } + } + } + } + } + } + } + } + } +} + + +// EXPORTS // + +module.exports = blockedunary10d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/1d.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/1d.js new file mode 100644 index 000000000000..07248e86fc14 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/1d.js @@ -0,0 +1,188 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params */ + +'use strict'; + +// MODULES // + +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to callback function and assigns results to a provided output ndarray. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 3, 2, 2 ]; +* var ysh = [ 3 ]; +* +* // Define the array strides: +* var sx = [ 4, 2, 1 ]; +* var sy = [ 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major' +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major' +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0 ]; +* var cdims = [ 1, 2 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 4 ]; +* +* // Perform a reduction: +* unary1d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ 8.0, 16.0, 24.0 ] +*/ +function unary1d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, opts, hasOpts, clbk, thisArg ) { + var ybuf; + var dv0; + var sh; + var S0; + var iv; + var i0; + var x; + var y; + var v; + var i; + var f; + + // Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop... + + // Cache a reference to the input ndarray: + x = arrays[ 0 ]; + + // Resolve the output ndarray and associated shape: + y = arrays[ 1 ]; + sh = y.shape; + + // Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments... + S0 = sh[ 0 ]; + dv0 = [ strides[0] ]; + for ( i = 1; i < arrays.length; i++ ) { + dv0.push( arrays[i].strides[0] ); + } + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + iv = offsets( arrays ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Iterate over the non-reduced ndarray dimensions... + for ( i0 = 0; i0 < S0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ i0 ], cdims, clbk, thisArg ); + ybuf[ iv[1] ] = ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ); + incrementOffsets( iv, dv0 ); + } +} + + +// EXPORTS // + +module.exports = unary1d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/1d_accessors.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/1d_accessors.js new file mode 100644 index 000000000000..5057cd180254 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/1d_accessors.js @@ -0,0 +1,196 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params */ + +'use strict'; + +// MODULES // + +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to callback function and assigns results to a provided output ndarray. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var accessors = require( '@stdlib/array/base/accessors' ); +* var Float64Array = require( '@stdlib/array/float64' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* var xbuf = toAccessorArray( 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 ] ) ); +* var ybuf = toAccessorArray( new Float64Array( [ 0.0, 0.0, 0.0 ] ) ); +* +* // Define the array shapes: +* var xsh = [ 3, 2, 2 ]; +* var ysh = [ 3 ]; +* +* // Define the array strides: +* var sx = [ 4, 2, 1 ]; +* var sy = [ 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major', +* 'accessors': accessors( xbuf ).accessors +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major', +* 'accessors': accessors( ybuf ).accessors +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0 ]; +* var cdims = [ 1, 2 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 4 ]; +* +* // Perform a reduction: +* unary1d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ 8.0, 16.0, 24.0 ] +*/ +function unary1d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, opts, hasOpts, clbk, thisArg ) { + var ybuf; + var set; + var dv0; + var sh; + var S0; + var iv; + var i0; + var x; + var y; + var v; + var i; + var f; + + // Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop... + + // Cache a reference to the input ndarray: + x = arrays[ 0 ]; + + // Resolve the output ndarray and associated shape: + y = arrays[ 1 ]; + sh = y.shape; + + // Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments... + S0 = sh[ 0 ]; + dv0 = [ strides[0] ]; + for ( i = 1; i < arrays.length; i++ ) { + dv0.push( arrays[i].strides[0] ); + } + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + iv = offsets( arrays ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Cache accessors: + set = y.accessors[ 1 ]; + + // Iterate over the non-reduced ndarray dimensions... + for ( i0 = 0; i0 < S0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ i0 ], cdims, clbk, thisArg ); + set( ybuf, iv[1], ( hasOpts ) ? fcn( v, opts, f ): fcn( v, f ) ); + incrementOffsets( iv, dv0 ); + } +} + + +// EXPORTS // + +module.exports = unary1d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/2d.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/2d.js new file mode 100644 index 000000000000..5d5bec01642e --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/2d.js @@ -0,0 +1,214 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params */ + +'use strict'; + +// MODULES // + +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to callback function and assigns results to a provided output ndarray. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {boolean} isRowMajor - boolean indicating whether the input ndarray is row-major +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 3, 2, 2 ]; +* var ysh = [ 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 4, 2, 1 ]; +* var sy = [ 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major' +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major' +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1 ]; +* var cdims = [ 2, 3 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 4 ]; +* +* // Perform a reduction: +* unary2d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, true, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ 8.0, 16.0, 24.0 ] ] +*/ +function unary2d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, isRowMajor, opts, hasOpts, clbk, thisArg ) { + var ybuf; + var dv0; + var dv1; + var sh; + var S0; + var S1; + var sv; + var iv; + var i0; + var i1; + var x; + var y; + var v; + var i; + var f; + + // Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop... + + // Cache a reference to the input ndarray: + x = arrays[ 0 ]; + + // Resolve the output ndarray and associated shape: + y = arrays[ 1 ]; + sh = y.shape; + + // Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments... + if ( isRowMajor ) { + // For row-major ndarrays, the last dimensions have the fastest changing indices... + S0 = sh[ 1 ]; + S1 = sh[ 0 ]; + dv0 = [ strides[1] ]; // offset increment for innermost loop + dv1 = [ strides[0] - ( S0*strides[1] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[1] ); + dv1.push( sv[0] - ( S0*sv[1] ) ); + } + } else { // order === 'column-major' + // For column-major ndarrays, the first dimensions have the fastest changing indices... + S0 = sh[ 0 ]; + S1 = sh[ 1 ]; + dv0 = [ strides[0] ]; // offset increment for innermost loop + dv1 = [ strides[1] - ( S0*strides[0] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[0] ); + dv1.push( sv[1] - ( S0*sv[0] ) ); + } + } + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + iv = offsets( arrays ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Iterate over the non-reduced ndarray dimensions... + for ( i1 = 0; i1 < S1; i1++ ) { + for ( i0 = 0; i0 < S0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ i1, i0 ], cdims, clbk, thisArg ); + ybuf[ iv[1] ] = ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } +} + + +// EXPORTS // + +module.exports = unary2d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/2d_accessors.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/2d_accessors.js new file mode 100644 index 000000000000..e60f7bd8162f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/2d_accessors.js @@ -0,0 +1,222 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params */ + +'use strict'; + +// MODULES // + +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to callback function and assigns results to a provided output ndarray. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {boolean} isRowMajor - boolean indicating whether the input ndarray is row-major +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var accessors = require( '@stdlib/array/base/accessors' ); +* var Float64Array = require( '@stdlib/array/float64' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* var xbuf = toAccessorArray( 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 ] ) ); +* var ybuf = toAccessorArray( new Float64Array( [ 0.0, 0.0, 0.0 ] ) ); +* +* // Define the array shapes: +* var xsh = [ 1, 3, 2, 2 ]; +* var ysh = [ 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 4, 2, 1 ]; +* var sy = [ 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major', +* 'accessors': accessors( xbuf ).accessors +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major', +* 'accessors': accessors( xbuf ).accessors +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1 ]; +* var cdims = [ 2, 3 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 4 ]; +* +* // Perform a reduction: +* unary2d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, true, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ 6.0, 14.0, 22.0 ] ] +*/ +function unary2d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, isRowMajor, opts, hasOpts, clbk, thisArg ) { + var ybuf; + var dv0; + var dv1; + var set; + var sh; + var S0; + var S1; + var sv; + var iv; + var i0; + var i1; + var x; + var y; + var v; + var i; + var f; + + // Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop... + + // Cache a reference to the input ndarray: + x = arrays[ 0 ]; + + // Resolve the output ndarray and associated shape: + y = arrays[ 1 ]; + sh = y.shape; + + // Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments... + if ( isRowMajor ) { + // For row-major ndarrays, the last dimensions have the fastest changing indices... + S0 = sh[ 1 ]; + S1 = sh[ 0 ]; + dv0 = [ strides[1] ]; // offset increment for innermost loop + dv1 = [ strides[0] - ( S0*strides[1] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[1] ); + dv1.push( sv[0] - ( S0*sv[1] ) ); + } + } else { // order === 'column-major' + // For column-major ndarrays, the first dimensions have the fastest changing indices... + S0 = sh[ 0 ]; + S1 = sh[ 1 ]; + dv0 = [ strides[0] ]; // offset increment for innermost loop + dv1 = [ strides[1] - ( S0*strides[0] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[0] ); + dv1.push( sv[1] - ( S0*sv[0] ) ); + } + } + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + iv = offsets( arrays ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Cache accessors: + set = y.accessors[ 1 ]; + + // Iterate over the non-reduced ndarray dimensions... + for ( i1 = 0; i1 < S1; i1++ ) { + for ( i0 = 0; i0 < S0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ i1, i0 ], cdims, clbk, thisArg ); + set( ybuf, iv[1], ( hasOpts ) ? fcn( views, opts, f ) : fcn( views, f ) ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } +} + + +// EXPORTS // + +module.exports = unary2d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/2d_blocked.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/2d_blocked.js new file mode 100644 index 000000000000..8b1052e7754c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/2d_blocked.js @@ -0,0 +1,244 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params */ + +'use strict'; + +// MODULES // + +var loopOrder = require( '@stdlib/ndarray/base/unary-loop-interchange-order' ); +var blockSize = require( '@stdlib/ndarray/base/unary-tiling-block-size' ); +var takeIndexed = require( '@stdlib/array/base/take-indexed' ); +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to a callback function and assigns results to a provided output ndarray via loop blocking. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 3, 2, 2 ]; +* var ysh = [ 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 4, 2, 1 ]; +* var sy = [ 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major' +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major' +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1 ]; +* var cdims = [ 2, 3 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 4 ]; +* +* // Perform a reduction: +* blockedunary2d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ 8.0, 16.0, 24.0 ] ] +*/ +function blockedunary2d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, opts, hasOpts, clbk, thisArg ) { + var bsize; + var ybuf; + var dv0; + var dv1; + var ov1; + var sh; + var s0; + var s1; + var sv; + var ov; + var iv; + var i0; + var i1; + var j0; + var j1; + var N; + var x; + var y; + var v; + var o; + var k; + var f; + + // Note on variable naming convention: S#, dv#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop... + + N = arrays.length; + x = arrays[ 0 ]; + y = arrays[ 1 ]; + + // Resolve the loop interchange order: + o = loopOrder( y.shape, strides, y.strides ); + sh = o.sh; + sv = [ o.sx, o.sy ]; + for ( k = 2; k < N; k++ ) { + sv.push( takeIndexed( arrays[k].strides, o.idx ) ); + } + // Determine the block size: + bsize = blockSize( x.dtype, y.dtype ); + + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + ov = offsets( arrays ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Cache offset increments for the innermost loop... + dv0 = []; + for ( k = 0; k < N; k++ ) { + dv0.push( sv[k][0] ); + } + // Initialize loop variables... + ov1 = zeros( N ); + dv1 = zeros( N ); + iv = zeros( N ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Iterate over blocks... + for ( j1 = sh[1]; j1 > 0; ) { + if ( j1 < bsize ) { + s1 = j1; + j1 = 0; + } else { + s1 = bsize; + j1 -= bsize; + } + for ( k = 0; k < N; k++ ) { + ov1[ k ] = ov[k] + ( j1*sv[k][1] ); + } + for ( j0 = sh[0]; j0 > 0; ) { + if ( j0 < bsize ) { + s0 = j0; + j0 = 0; + } else { + s0 = bsize; + j0 -= bsize; + } + // Compute index offsets and loop offset increments for the first ndarray elements in the current block... + for ( k = 0; k < N; k++ ) { + iv[ k ] = ov1[k] + ( j0*sv[k][0] ); + dv1[ k ] = sv[k][1] - ( s0*sv[k][0] ); + } + // Iterate over the non-reduced ndarray dimensions... + for ( i1 = 0; i1 < s1; i1++ ) { + for ( i0 = 0; i0 < s0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ j1+i1, j0+i0 ], cdims, clbk, thisArg ); + ybuf[ iv[1] ] = ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + } + } +} + + +// EXPORTS // + +module.exports = blockedunary2d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/2d_blocked_accessors.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/2d_blocked_accessors.js new file mode 100644 index 000000000000..62927ae5dc09 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/2d_blocked_accessors.js @@ -0,0 +1,252 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params */ + +'use strict'; + +// MODULES // + +var loopOrder = require( '@stdlib/ndarray/base/unary-loop-interchange-order' ); +var blockSize = require( '@stdlib/ndarray/base/unary-tiling-block-size' ); +var takeIndexed = require( '@stdlib/array/base/take-indexed' ); +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to a callback function and assigns results to a provided output ndarray via loop blocking. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var accessors = require( '@stdlib/array/base/accessors' ); +* var Float64Array = require( '@stdlib/array/float64' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* var xbuf = toAccessorArray( 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 ] ) ); +* var ybuf = toAccessorArray( new Float64Array( [ 0.0, 0.0, 0.0 ] ) ); +* +* // Define the array shapes: +* var xsh = [ 1, 3, 2, 2 ]; +* var ysh = [ 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 4, 2, 1 ]; +* var sy = [ 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major', +* 'accessors': accessors( xbuf ).accessors +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major', +* 'accessors': accessors( ybuf ).accessors +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1 ]; +* var cdims = [ 2, 3 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 4 ]; +* +* // Perform a reduction: +* blockedunary2d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ 6.0, 14.0, 22.0 ] ] +*/ +function blockedunary2d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, opts, hasOpts, clbk, thisArg ) { + var bsize; + var ybuf; + var set; + var dv0; + var dv1; + var ov1; + var sh; + var s0; + var s1; + var sv; + var ov; + var iv; + var i0; + var i1; + var j0; + var j1; + var N; + var x; + var y; + var v; + var o; + var k; + var f; + + // Note on variable naming convention: S#, dv#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop... + + N = arrays.length; + x = arrays[ 0 ]; + y = arrays[ 1 ]; + + // Resolve the loop interchange order: + o = loopOrder( y.shape, strides, y.strides ); + sh = o.sh; + sv = [ o.sx, o.sy ]; + for ( k = 2; k < N; k++ ) { + sv.push( takeIndexed( arrays[k].strides, o.idx ) ); + } + // Determine the block size: + bsize = blockSize( x.dtype, y.dtype ); + + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + ov = offsets( arrays ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Cache accessors: + set = y.accessors[1]; + + // Cache offset increments for the innermost loop... + dv0 = []; + for ( k = 0; k < N; k++ ) { + dv0.push( sv[k][0] ); + } + // Initialize loop variables... + ov1 = zeros( N ); + dv1 = zeros( N ); + iv = zeros( N ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Iterate over blocks... + for ( j1 = sh[1]; j1 > 0; ) { + if ( j1 < bsize ) { + s1 = j1; + j1 = 0; + } else { + s1 = bsize; + j1 -= bsize; + } + for ( k = 0; k < N; k++ ) { + ov1[ k ] = ov[k] + ( j1*sv[k][1] ); + } + for ( j0 = sh[0]; j0 > 0; ) { + if ( j0 < bsize ) { + s0 = j0; + j0 = 0; + } else { + s0 = bsize; + j0 -= bsize; + } + // Compute index offsets and loop offset increments for the first ndarray elements in the current block... + for ( k = 0; k < N; k++ ) { + iv[ k ] = ov1[k] + ( j0*sv[k][0] ); + dv1[ k ] = sv[k][1] - ( s0*sv[k][0] ); + } + // Iterate over the non-reduced ndarray dimensions... + for ( i1 = 0; i1 < s1; i1++ ) { + for ( i0 = 0; i0 < s0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ j1+i1, j0+i0 ], cdims, clbk, thisArg ); + set( ybuf, iv[1], ( hasOpts ) ? fcn( views, opts, f ) : fcn( views, f ) ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + } + } +} + + +// EXPORTS // + +module.exports = blockedunary2d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/3d.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/3d.js new file mode 100644 index 000000000000..f87edbe15620 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/3d.js @@ -0,0 +1,225 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params */ + +'use strict'; + +// MODULES // + +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to callback function and assigns results to a provided output ndarray. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {boolean} isRowMajor - boolean indicating whether the input ndarray is row-major +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major' +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major' +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2 ]; +* var cdims = [ 3, 4 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 4 ]; +* +* // Perform a reduction: +* unary3d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, true, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ 8.0, 16.0, 24.0 ] ] ] +*/ +function unary3d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, isRowMajor, opts, hasOpts, clbk, thisArg ) { + var ybuf; + var dv0; + var dv1; + var dv2; + var sh; + var S0; + var S1; + var S2; + var sv; + var iv; + var i0; + var i1; + var i2; + var x; + var y; + var v; + var i; + var f; + + // Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop... + + // Cache a reference to the input ndarray: + x = arrays[ 0 ]; + + // Resolve the output ndarray and associated shape: + y = arrays[ 1 ]; + sh = y.shape; + + // Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments... + if ( isRowMajor ) { + // For row-major ndarrays, the last dimensions have the fastest changing indices... + S0 = sh[ 2 ]; + S1 = sh[ 1 ]; + S2 = sh[ 0 ]; + dv0 = [ strides[2] ]; // offset increment for innermost loop + dv1 = [ strides[1] - ( S0*strides[2] ) ]; + dv2 = [ strides[0] - ( S1*strides[1] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[2] ); + dv1.push( sv[1] - ( S0*sv[2] ) ); + dv2.push( sv[0] - ( S1*sv[1] ) ); + } + } else { // order === 'column-major' + // For column-major ndarrays, the first dimensions have the fastest changing indices... + S0 = sh[ 0 ]; + S1 = sh[ 1 ]; + S2 = sh[ 2 ]; + dv0 = [ strides[0] ]; // offset increment for innermost loop + dv1 = [ strides[1] - ( S0*strides[0] ) ]; + dv2 = [ strides[2] - ( S1*strides[1] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[0] ); + dv1.push( sv[1] - ( S0*sv[0] ) ); + dv2.push( sv[2] - ( S1*sv[1] ) ); + } + } + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + iv = offsets( arrays ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Iterate over the non-reduced ndarray dimensions... + for ( i2 = 0; i2 < S2; i2++ ) { + for ( i1 = 0; i1 < S1; i1++ ) { + for ( i0 = 0; i0 < S0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ i2, i1, i0 ], cdims, clbk, thisArg ); + ybuf[ iv[1] ] = ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + } +} + + +// EXPORTS // + +module.exports = unary3d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/3d_accessors.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/3d_accessors.js new file mode 100644 index 000000000000..9dd25c70a06b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/3d_accessors.js @@ -0,0 +1,233 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params */ + +'use strict'; + +// MODULES // + +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to callback function and assigns results to a provided output ndarray. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {boolean} isRowMajor - boolean indicating whether the input ndarray is row-major +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var accessors = require( '@stdlib/array/base/accessors' ); +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* var xbuf = toAccessorArray( 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 ] ) ); +* var ybuf = toAccessorArray( new Float64Array( [ 0.0, 0.0, 0.0 ] ) ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major', +* 'accessors': accessors( xbuf ).accessors +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major', +* 'accessors': accessors( ybuf ).accessors +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2 ]; +* var cdims = [ 3, 4 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 4 ]; +* +* // Perform a reduction: +* unary3d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, true, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ 8.0, 16.0, 24.0 ] ] ] +*/ +function unary3d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, isRowMajor, opts, hasOpts, clbk, thisArg ) { + var ybuf; + var set; + var dv0; + var dv1; + var dv2; + var sh; + var S0; + var S1; + var S2; + var sv; + var iv; + var i0; + var i1; + var i2; + var x; + var y; + var v; + var i; + var f; + + // Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop... + + // Cache a reference to the input ndarray: + x = arrays[ 0 ]; + + // Resolve the output ndarray and associated shape: + y = arrays[ 1 ]; + sh = y.shape; + + // Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments... + if ( isRowMajor ) { + // For row-major ndarrays, the last dimensions have the fastest changing indices... + S0 = sh[ 2 ]; + S1 = sh[ 1 ]; + S2 = sh[ 0 ]; + dv0 = [ strides[2] ]; // offset increment for innermost loop + dv1 = [ strides[1] - ( S0*strides[2] ) ]; + dv2 = [ strides[0] - ( S1*strides[1] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[2] ); + dv1.push( sv[1] - ( S0*sv[2] ) ); + dv2.push( sv[0] - ( S1*sv[1] ) ); + } + } else { // order === 'column-major' + // For column-major ndarrays, the first dimensions have the fastest changing indices... + S0 = sh[ 0 ]; + S1 = sh[ 1 ]; + S2 = sh[ 2 ]; + dv0 = [ strides[0] ]; // offset increment for innermost loop + dv1 = [ strides[1] - ( S0*strides[0] ) ]; + dv2 = [ strides[2] - ( S1*strides[1] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[0] ); + dv1.push( sv[1] - ( S0*sv[0] ) ); + dv2.push( sv[2] - ( S1*sv[1] ) ); + } + } + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + iv = offsets( arrays ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Cache accessors: + set = y.accessors[ 1 ]; + + // Iterate over the non-reduced ndarray dimensions... + for ( i2 = 0; i2 < S2; i2++ ) { + for ( i1 = 0; i1 < S1; i1++ ) { + for ( i0 = 0; i0 < S0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ i2, i1, i0 ], cdims, clbk, thisArg ); + set( ybuf, iv[1], ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ) ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + } +} + + +// EXPORTS // + +module.exports = unary3d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/3d_blocked.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/3d_blocked.js new file mode 100644 index 000000000000..b8d96d82efd0 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/3d_blocked.js @@ -0,0 +1,267 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params, max-depth */ + +'use strict'; + +// MODULES // + +var loopOrder = require( '@stdlib/ndarray/base/unary-loop-interchange-order' ); +var blockSize = require( '@stdlib/ndarray/base/unary-tiling-block-size' ); +var takeIndexed = require( '@stdlib/array/base/take-indexed' ); +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to a callback function and assigns results to a provided output ndarray via loop blocking. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major' +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major' +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2 ]; +* var cdims = [ 3, 4 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 4 ]; +* +* // Perform a reduction: +* blockedunary3d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ 8.0, 16.0, 24.0 ] ] ] +*/ +function blockedunary3d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, opts, hasOpts, clbk, thisArg ) { + var bsize; + var ybuf; + var dv0; + var dv1; + var dv2; + var ov1; + var ov2; + var sh; + var s0; + var s1; + var s2; + var sv; + var ov; + var iv; + var i0; + var i1; + var i2; + var j0; + var j1; + var j2; + var N; + var x; + var y; + var v; + var o; + var k; + var f; + + // Note on variable naming convention: S#, dv#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop... + + N = arrays.length; + x = arrays[ 0 ]; + y = arrays[ 1 ]; + + // Resolve the loop interchange order: + o = loopOrder( y.shape, strides, y.strides ); + sh = o.sh; + sv = [ o.sx, o.sy ]; + for ( k = 2; k < N; k++ ) { + sv.push( takeIndexed( arrays[k].strides, o.idx ) ); + } + // Determine the block size: + bsize = blockSize( x.dtype, y.dtype ); + + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + ov = offsets( arrays ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Cache offset increments for the innermost loop... + dv0 = []; + for ( k = 0; k < N; k++ ) { + dv0.push( sv[k][0] ); + } + // Initialize loop variables... + ov1 = zeros( N ); + ov2 = zeros( N ); + dv1 = zeros( N ); + dv2 = zeros( N ); + iv = zeros( N ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Iterate over blocks... + for ( j2 = sh[2]; j2 > 0; ) { + if ( j2 < bsize ) { + s2 = j2; + j2 = 0; + } else { + s2 = bsize; + j2 -= bsize; + } + for ( k = 0; k < N; k++ ) { + ov2[ k ] = ov[k] + ( j2*sv[k][2] ); + } + for ( j1 = sh[1]; j1 > 0; ) { + if ( j1 < bsize ) { + s1 = j1; + j1 = 0; + } else { + s1 = bsize; + j1 -= bsize; + } + for ( k = 0; k < N; k++ ) { + ov1[ k ] = ov2[k] + ( j1*sv[k][1] ); + dv2[ k ] = sv[k][2] - ( s1*sv[k][1] ); + } + for ( j0 = sh[0]; j0 > 0; ) { + if ( j0 < bsize ) { + s0 = j0; + j0 = 0; + } else { + s0 = bsize; + j0 -= bsize; + } + // Compute index offsets and loop offset increments for the first ndarray elements in the current block... + for ( k = 0; k < N; k++ ) { + iv[ k ] = ov1[k] + ( j0*sv[k][0] ); + dv1[ k ] = sv[k][1] - ( s0*sv[k][0] ); + } + // Iterate over the non-reduced ndarray dimensions... + for ( i2 = 0; i2 < s2; i2++ ) { + for ( i1 = 0; i1 < s1; i1++ ) { + for ( i0 = 0; i0 < s0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ j2+i2, j1+i1, j0+i0 ], cdims, clbk, thisArg ); + ybuf[ iv[1] ] = ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + incrementOffsets( iv, dv2 ); + } + } + } + } +} + + +// EXPORTS // + +module.exports = blockedunary3d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/3d_blocked_accessors.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/3d_blocked_accessors.js new file mode 100644 index 000000000000..97152360df56 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/3d_blocked_accessors.js @@ -0,0 +1,275 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params, max-depth */ + +'use strict'; + +// MODULES // + +var loopOrder = require( '@stdlib/ndarray/base/unary-loop-interchange-order' ); +var blockSize = require( '@stdlib/ndarray/base/unary-tiling-block-size' ); +var takeIndexed = require( '@stdlib/array/base/take-indexed' ); +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to a callback function and assigns results to a provided output ndarray via loop blocking. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var accessors = require( '@stdlib/array/base/accessors' ); +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* var xbuf = toAccessorArray( 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 ] ) ); +* var ybuf = toAccessorArray( new Float64Array( [ 0.0, 0.0, 0.0 ] ) ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major', +* 'accessors': accessors( xbuf ).accessors +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major', +* 'accessors': accessors( ybuf ).accessors +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2 ]; +* var cdims = [ 3, 4 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 4 ]; +* +* // Perform a reduction: +* blockedunary3d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ 8.0, 16.0, 24.0 ] ] ] +*/ +function blockedunary3d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, opts, hasOpts, clbk, thisArg ) { + var bsize; + var ybuf; + var dv0; + var dv1; + var dv2; + var ov1; + var ov2; + var set; + var sh; + var s0; + var s1; + var s2; + var sv; + var ov; + var iv; + var i0; + var i1; + var i2; + var j0; + var j1; + var j2; + var N; + var x; + var y; + var v; + var o; + var k; + var f; + + // Note on variable naming convention: S#, dv#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop... + + N = arrays.length; + x = arrays[ 0 ]; + y = arrays[ 1 ]; + + // Resolve the loop interchange order: + o = loopOrder( y.shape, strides, y.strides ); + sh = o.sh; + sv = [ o.sx, o.sy ]; + for ( k = 2; k < N; k++ ) { + sv.push( takeIndexed( arrays[k].strides, o.idx ) ); + } + // Determine the block size: + bsize = blockSize( x.dtype, y.dtype ); + + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + ov = offsets( arrays ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Cache accessors: + set = y.accessors[ 1 ]; + + // Cache offset increments for the innermost loop... + dv0 = []; + for ( k = 0; k < N; k++ ) { + dv0.push( sv[k][0] ); + } + // Initialize loop variables... + ov1 = zeros( N ); + ov2 = zeros( N ); + dv1 = zeros( N ); + dv2 = zeros( N ); + iv = zeros( N ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Iterate over blocks... + for ( j2 = sh[2]; j2 > 0; ) { + if ( j2 < bsize ) { + s2 = j2; + j2 = 0; + } else { + s2 = bsize; + j2 -= bsize; + } + for ( k = 0; k < N; k++ ) { + ov2[ k ] = ov[k] + ( j2*sv[k][2] ); + } + for ( j1 = sh[1]; j1 > 0; ) { + if ( j1 < bsize ) { + s1 = j1; + j1 = 0; + } else { + s1 = bsize; + j1 -= bsize; + } + for ( k = 0; k < N; k++ ) { + ov1[ k ] = ov2[k] + ( j1*sv[k][1] ); + dv2[ k ] = sv[k][2] - ( s1*sv[k][1] ); + } + for ( j0 = sh[0]; j0 > 0; ) { + if ( j0 < bsize ) { + s0 = j0; + j0 = 0; + } else { + s0 = bsize; + j0 -= bsize; + } + // Compute index offsets and loop offset increments for the first ndarray elements in the current block... + for ( k = 0; k < N; k++ ) { + iv[ k ] = ov1[k] + ( j0*sv[k][0] ); + dv1[ k ] = sv[k][1] - ( s0*sv[k][0] ); + } + // Iterate over the non-reduced ndarray dimensions... + for ( i2 = 0; i2 < s2; i2++ ) { + for ( i1 = 0; i1 < s1; i1++ ) { + for ( i0 = 0; i0 < s0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ j2+i2, j1+i1, j0+i0 ], cdims, clbk, thisArg ); + set( ybuf, iv[1], ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ) ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + incrementOffsets( iv, dv2 ); + } + } + } + } +} + + +// EXPORTS // + +module.exports = blockedunary3d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/4d.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/4d.js new file mode 100644 index 000000000000..2a214db69830 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/4d.js @@ -0,0 +1,237 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params */ + +'use strict'; + +// MODULES // + +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to callback function and assigns results to a provided output ndarray. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {boolean} isRowMajor - boolean indicating whether the input ndarray is row-major +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major' +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major' +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2, 3 ]; +* var cdims = [ 4, 5 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 12, 4 ]; +* +* // Perform a reduction: +* unary4d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, true, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ [ 8.0, 16.0, 24.0 ] ] ] ] +*/ +function unary4d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, isRowMajor, opts, hasOpts, clbk, thisArg ) { + var ybuf; + var dv0; + var dv1; + var dv2; + var dv3; + var sh; + var S0; + var S1; + var S2; + var S3; + var sv; + var iv; + var i0; + var i1; + var i2; + var i3; + var x; + var y; + var v; + var i; + var f; + + // Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop... + + // Cache a reference to the input ndarray: + x = arrays[ 0 ]; + + // Resolve the output ndarray and associated shape: + y = arrays[ 1 ]; + sh = y.shape; + + // Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments... + if ( isRowMajor ) { + // For row-major ndarrays, the last dimensions have the fastest changing indices... + S0 = sh[ 3 ]; + S1 = sh[ 2 ]; + S2 = sh[ 1 ]; + S3 = sh[ 0 ]; + dv0 = [ strides[3] ]; // offset increment for innermost loop + dv1 = [ strides[2] - ( S0*strides[3] ) ]; + dv2 = [ strides[1] - ( S1*strides[2] ) ]; + dv3 = [ strides[0] - ( S2*strides[1] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[3] ); + dv1.push( sv[2] - ( S0*sv[3] ) ); + dv2.push( sv[1] - ( S1*sv[2] ) ); + dv3.push( sv[0] - ( S2*sv[1]) ); + } + } else { // order === 'column-major' + // For column-major ndarrays, the first dimensions have the fastest changing indices... + S0 = sh[ 0 ]; + S1 = sh[ 1 ]; + S2 = sh[ 2 ]; + S3 = sh[ 3 ]; + dv0 = [ strides[0] ]; // offset increment for innermost loop + dv1 = [ strides[1] - ( S0*strides[0] ) ]; + dv2 = [ strides[2] - ( S1*strides[1] ) ]; + dv3 = [ strides[3] - ( S2*strides[2] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[0] ); + dv1.push( sv[1] - ( S0*sv[0] ) ); + dv2.push( sv[2] - ( S1*sv[1] ) ); + dv3.push( sv[3] - ( S2*sv[2] ) ); + } + } + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + iv = offsets( arrays ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Iterate over the non-reduced ndarray dimensions... + for ( i3 = 0; i3 < S3; i3++ ) { + for ( i2 = 0; i2 < S2; i2++ ) { + for ( i1 = 0; i1 < S1; i1++ ) { + for ( i0 = 0; i0 < S0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ i3, i2, i1, i0 ], cdims, clbk, thisArg ); + ybuf[ iv[1] ] = ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + incrementOffsets( iv, dv3 ); + } + } +} + + +// EXPORTS // + +module.exports = unary4d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/4d_accessors.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/4d_accessors.js new file mode 100644 index 000000000000..a5d147dfb3a1 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/4d_accessors.js @@ -0,0 +1,245 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params */ + +'use strict'; + +// MODULES // + +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to callback function and assigns results to a provided output ndarray. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {boolean} isRowMajor - boolean indicating whether the input ndarray is row-major +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var accessors = require( '@stdlib/array/base/accessors' ); +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* var xbuf = toAccessorArray( 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 ] ) ); +* var ybuf = toAccessorArray( new Float64Array( [ 0.0, 0.0, 0.0 ] ) ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major', +* 'accessors': accessors( xbuf ).accessors +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major', +* 'accessors': accessors( ybuf ).accessors +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2, 3 ]; +* var cdims = [ 4, 5 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 12, 4 ]; +* +* // Perform a reduction: +* unary4d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, true, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ [ 8.0, 16.0, 24.0 ] ] ] ] +*/ +function unary4d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, isRowMajor, opts, hasOpts, clbk, thisArg ) { + var ybuf; + var dv0; + var dv1; + var dv2; + var dv3; + var set; + var sh; + var S0; + var S1; + var S2; + var S3; + var sv; + var iv; + var i0; + var i1; + var i2; + var i3; + var x; + var y; + var v; + var i; + var f; + + // Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop... + + // Cache a reference to the input ndarray: + x = arrays[ 0 ]; + + // Resolve the output ndarray and associated shape: + y = arrays[ 1 ]; + sh = y.shape; + + // Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments... + if ( isRowMajor ) { + // For row-major ndarrays, the last dimensions have the fastest changing indices... + S0 = sh[ 3 ]; + S1 = sh[ 2 ]; + S2 = sh[ 1 ]; + S3 = sh[ 0 ]; + dv0 = [ strides[3] ]; // offset increment for innermost loop + dv1 = [ strides[2] - ( S0*strides[3] ) ]; + dv2 = [ strides[1] - ( S1*strides[2] ) ]; + dv3 = [ strides[0] - ( S2*strides[1] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[3] ); + dv1.push( sv[2] - ( S0*sv[3] ) ); + dv2.push( sv[1] - ( S1*sv[2] ) ); + dv3.push( sv[0] - ( S2*sv[1]) ); + } + } else { // order === 'column-major' + // For column-major ndarrays, the first dimensions have the fastest changing indices... + S0 = sh[ 0 ]; + S1 = sh[ 1 ]; + S2 = sh[ 2 ]; + S3 = sh[ 3 ]; + dv0 = [ strides[0] ]; // offset increment for innermost loop + dv1 = [ strides[1] - ( S0*strides[0] ) ]; + dv2 = [ strides[2] - ( S1*strides[1] ) ]; + dv3 = [ strides[3] - ( S2*strides[2] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[0] ); + dv1.push( sv[1] - ( S0*sv[0] ) ); + dv2.push( sv[2] - ( S1*sv[1] ) ); + dv3.push( sv[3] - ( S2*sv[2] ) ); + } + } + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + iv = offsets( arrays ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Cache accessors: + set = y.accessors[ 1 ]; + + // Iterate over the non-reduced ndarray dimensions... + for ( i3 = 0; i3 < S3; i3++ ) { + for ( i2 = 0; i2 < S2; i2++ ) { + for ( i1 = 0; i1 < S1; i1++ ) { + for ( i0 = 0; i0 < S0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ i3, i2, i1, i0 ], cdims, clbk, thisArg ); + set( ybuf, iv[1], ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ) ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + incrementOffsets( iv, dv3 ); + } + } +} + + +// EXPORTS // + +module.exports = unary4d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/4d_blocked.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/4d_blocked.js new file mode 100644 index 000000000000..bebc7b898b9f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/4d_blocked.js @@ -0,0 +1,290 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params, max-depth, max-statements */ + +'use strict'; + +// MODULES // + +var loopOrder = require( '@stdlib/ndarray/base/unary-loop-interchange-order' ); +var blockSize = require( '@stdlib/ndarray/base/unary-tiling-block-size' ); +var takeIndexed = require( '@stdlib/array/base/take-indexed' ); +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to a callback function and assigns results to a provided output ndarray via loop blocking. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major' +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major' +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2, 3 ]; +* var cdims = [ 4, 5 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 12, 4 ]; +* +* // Perform a reduction: +* blockedunary4d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ [ 8.0, 16.0, 24.0 ] ] ] ] +*/ +function blockedunary4d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, opts, hasOpts, clbk, thisArg ) { + var bsize; + var ybuf; + var dv0; + var dv1; + var dv2; + var dv3; + var ov1; + var ov2; + var ov3; + var sh; + var s0; + var s1; + var s2; + var s3; + var sv; + var ov; + var iv; + var i0; + var i1; + var i2; + var i3; + var j0; + var j1; + var j2; + var j3; + var N; + var x; + var y; + var v; + var o; + var k; + var f; + + // Note on variable naming convention: S#, dv#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop... + + N = arrays.length; + x = arrays[ 0 ]; + y = arrays[ 1 ]; + + // Resolve the loop interchange order: + o = loopOrder( y.shape, strides, y.strides ); + sh = o.sh; + sv = [ o.sx, o.sy ]; + for ( k = 2; k < N; k++ ) { + sv.push( takeIndexed( arrays[k].strides, o.idx ) ); + } + // Determine the block size: + bsize = blockSize( x.dtype, y.dtype ); + + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + ov = offsets( arrays ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Cache offset increments for the innermost loop... + dv0 = []; + for ( k = 0; k < N; k++ ) { + dv0.push( sv[k][0] ); + } + // Initialize loop variables... + ov1 = zeros( N ); + ov2 = zeros( N ); + ov3 = zeros( N ); + dv1 = zeros( N ); + dv2 = zeros( N ); + dv3 = zeros( N ); + iv = zeros( N ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Iterate over blocks... + for ( j3 = sh[3]; j3 > 0; ) { + if ( j3 < bsize ) { + s3 = j3; + j3 = 0; + } else { + s3 = bsize; + j3 -= bsize; + } + for ( k = 0; k < N; k++ ) { + ov3[ k ] = ov[k] + ( j3*sv[k][3] ); + } + for ( j2 = sh[2]; j2 > 0; ) { + if ( j2 < bsize ) { + s2 = j2; + j2 = 0; + } else { + s2 = bsize; + j2 -= bsize; + } + for ( k = 0; k < N; k++ ) { + ov2[ k ] = ov3[k] + ( j2*sv[k][2] ); + dv3[ k ] = sv[k][3] - ( s2*sv[k][2] ); + } + for ( j1 = sh[1]; j1 > 0; ) { + if ( j1 < bsize ) { + s1 = j1; + j1 = 0; + } else { + s1 = bsize; + j1 -= bsize; + } + for ( k = 0; k < N; k++ ) { + ov1[ k ] = ov2[k] + ( j1*sv[k][1] ); + dv2[ k ] = sv[k][2] - ( s1*sv[k][1] ); + } + for ( j0 = sh[0]; j0 > 0; ) { + if ( j0 < bsize ) { + s0 = j0; + j0 = 0; + } else { + s0 = bsize; + j0 -= bsize; + } + // Compute index offsets and loop offset increments for the first ndarray elements in the current block... + for ( k = 0; k < N; k++ ) { + iv[ k ] = ov1[k] + ( j0*sv[k][0] ); + dv1[ k ] = sv[k][1] - ( s0*sv[k][0] ); + } + // Iterate over the non-reduced ndarray dimensions... + for ( i3 = 0; i3 < s3; i3++ ) { + for ( i2 = 0; i2 < s2; i2++ ) { + for ( i1 = 0; i1 < s1; i1++ ) { + for ( i0 = 0; i0 < s0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ j3+i3, j2+i2, j1+i1, j0+i0 ], cdims, clbk, thisArg ); + ybuf[ iv[1] ] = ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + incrementOffsets( iv, dv2 ); + } + incrementOffsets( iv, dv3 ); + } + } + } + } + } +} + + +// EXPORTS // + +module.exports = blockedunary4d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/4d_blocked_accessors.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/4d_blocked_accessors.js new file mode 100644 index 000000000000..e9dd867f7709 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/4d_blocked_accessors.js @@ -0,0 +1,298 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params, max-depth, max-statements */ + +'use strict'; + +// MODULES // + +var loopOrder = require( '@stdlib/ndarray/base/unary-loop-interchange-order' ); +var blockSize = require( '@stdlib/ndarray/base/unary-tiling-block-size' ); +var takeIndexed = require( '@stdlib/array/base/take-indexed' ); +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to a callback function and assigns results to a provided output ndarray via loop blocking. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var accessors = require( '@stdlib/array/base/accessors' ); +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major', +* 'accessors': accessors( xbuf ).accessors +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major', +* 'accessors': accessors( ybuf ).accessors +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2, 3 ]; +* var cdims = [ 4, 5 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 12, 4 ]; +* +* // Perform a reduction: +* blockedunary4d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ [ 8.0, 16.0, 24.0 ] ] ] ] +*/ +function blockedunary4d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, opts, hasOpts, clbk, thisArg ) { + var bsize; + var ybuf; + var set; + var dv0; + var dv1; + var dv2; + var dv3; + var ov1; + var ov2; + var ov3; + var sh; + var s0; + var s1; + var s2; + var s3; + var sv; + var ov; + var iv; + var i0; + var i1; + var i2; + var i3; + var j0; + var j1; + var j2; + var j3; + var N; + var x; + var y; + var v; + var o; + var k; + var f; + + // Note on variable naming convention: S#, dv#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop... + + N = arrays.length; + x = arrays[ 0 ]; + y = arrays[ 1 ]; + + // Resolve the loop interchange order: + o = loopOrder( y.shape, strides, y.strides ); + sh = o.sh; + sv = [ o.sx, o.sy ]; + for ( k = 2; k < N; k++ ) { + sv.push( takeIndexed( arrays[k].strides, o.idx ) ); + } + // Determine the block size: + bsize = blockSize( x.dtype, y.dtype ); + + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + ov = offsets( arrays ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Cache accessors: + set = y.accessors[ 1 ]; + + // Cache offset increments for the innermost loop... + dv0 = []; + for ( k = 0; k < N; k++ ) { + dv0.push( sv[k][0] ); + } + // Initialize loop variables... + ov1 = zeros( N ); + ov2 = zeros( N ); + ov3 = zeros( N ); + dv1 = zeros( N ); + dv2 = zeros( N ); + dv3 = zeros( N ); + iv = zeros( N ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Iterate over blocks... + for ( j3 = sh[3]; j3 > 0; ) { + if ( j3 < bsize ) { + s3 = j3; + j3 = 0; + } else { + s3 = bsize; + j3 -= bsize; + } + for ( k = 0; k < N; k++ ) { + ov3[ k ] = ov[k] + ( j3*sv[k][3] ); + } + for ( j2 = sh[2]; j2 > 0; ) { + if ( j2 < bsize ) { + s2 = j2; + j2 = 0; + } else { + s2 = bsize; + j2 -= bsize; + } + for ( k = 0; k < N; k++ ) { + ov2[ k ] = ov3[k] + ( j2*sv[k][2] ); + dv3[ k ] = sv[k][3] - ( s2*sv[k][2] ); + } + for ( j1 = sh[1]; j1 > 0; ) { + if ( j1 < bsize ) { + s1 = j1; + j1 = 0; + } else { + s1 = bsize; + j1 -= bsize; + } + for ( k = 0; k < N; k++ ) { + ov1[ k ] = ov2[k] + ( j1*sv[k][1] ); + dv2[ k ] = sv[k][2] - ( s1*sv[k][1] ); + } + for ( j0 = sh[0]; j0 > 0; ) { + if ( j0 < bsize ) { + s0 = j0; + j0 = 0; + } else { + s0 = bsize; + j0 -= bsize; + } + // Compute index offsets and loop offset increments for the first ndarray elements in the current block... + for ( k = 0; k < N; k++ ) { + iv[ k ] = ov1[k] + ( j0*sv[k][0] ); + dv1[ k ] = sv[k][1] - ( s0*sv[k][0] ); + } + // Iterate over the non-reduced ndarray dimensions... + for ( i3 = 0; i3 < s3; i3++ ) { + for ( i2 = 0; i2 < s2; i2++ ) { + for ( i1 = 0; i1 < s1; i1++ ) { + for ( i0 = 0; i0 < s0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ j3+i3, j2+i2, j1+i1, j0+i0 ], cdims, clbk, thisArg ); + set( ybuf, iv[1], ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ) ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + incrementOffsets( iv, dv2 ); + } + incrementOffsets( iv, dv3 ); + } + } + } + } + } +} + + +// EXPORTS // + +module.exports = blockedunary4d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/5d.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/5d.js new file mode 100644 index 000000000000..c31e85f83e9c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/5d.js @@ -0,0 +1,250 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params */ + +'use strict'; + +// MODULES // + +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to callback function and assigns results to a provided output ndarray. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {boolean} isRowMajor - boolean indicating whether the input ndarray is row-major +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major' +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major' +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2, 3, 4 ]; +* var cdims = [ 5, 6 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 12, 12, 4 ]; +* +* // Perform a reduction: +* unary5d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, true, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ [ [ 8.0, 16.0, 24.0 ] ] ] ] ] +*/ +function unary5d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, isRowMajor, opts, hasOpts, clbk, thisArg ) { + var ybuf; + var dv0; + var dv1; + var dv2; + var dv3; + var dv4; + var sh; + var S0; + var S1; + var S2; + var S3; + var S4; + var sv; + var iv; + var i0; + var i1; + var i2; + var i3; + var i4; + var x; + var y; + var v; + var i; + var f; + + // Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop... + + // Cache a reference to the input ndarray: + x = arrays[ 0 ]; + + // Resolve the output ndarray and associated shape: + y = arrays[ 1 ]; + sh = y.shape; + + // Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments... + if ( isRowMajor ) { + // For row-major ndarrays, the last dimensions have the fastest changing indices... + S0 = sh[ 4 ]; + S1 = sh[ 3 ]; + S2 = sh[ 2 ]; + S3 = sh[ 1 ]; + S4 = sh[ 0 ]; + dv0 = [ strides[4] ]; // offset increment for innermost loop + dv1 = [ strides[3] - ( S0*strides[4] ) ]; + dv2 = [ strides[2] - ( S1*strides[3] ) ]; + dv3 = [ strides[1] - ( S2*strides[2] ) ]; + dv4 = [ strides[0] - ( S3*strides[1] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[4] ); + dv1.push( sv[3] - ( S0*sv[4] ) ); + dv2.push( sv[2] - ( S1*sv[3] ) ); + dv3.push( sv[1] - ( S2*sv[2] ) ); + dv4.push( sv[0] - ( S3*sv[1] ) ); + } + } else { // order === 'column-major' + // For column-major ndarrays, the first dimensions have the fastest changing indices... + S0 = sh[ 0 ]; + S1 = sh[ 1 ]; + S2 = sh[ 2 ]; + S3 = sh[ 3 ]; + S4 = sh[ 4 ]; + dv0 = [ strides[0] ]; // offset increment for innermost loop + dv1 = [ strides[1] - ( S0*strides[0] ) ]; + dv2 = [ strides[2] - ( S1*strides[1] ) ]; + dv3 = [ strides[3] - ( S2*strides[2] ) ]; + dv4 = [ strides[4] - ( S3*strides[3] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[0] ); + dv1.push( sv[1] - ( S0*sv[0] ) ); + dv2.push( sv[2] - ( S1*sv[1] ) ); + dv3.push( sv[3] - ( S2*sv[2] ) ); + dv4.push( sv[4] - ( S3*sv[3] ) ); + } + } + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + iv = offsets( arrays ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Iterate over the non-reduced ndarray dimensions... + for ( i4 = 0; i4 < S4; i4++ ) { + for ( i3 = 0; i3 < S3; i3++ ) { + for ( i2 = 0; i2 < S2; i2++ ) { + for ( i1 = 0; i1 < S1; i1++ ) { + for ( i0 = 0; i0 < S0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ i4, i3, i2, i1, i0 ], cdims, clbk, thisArg ); + ybuf[ iv[1] ] = ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + incrementOffsets( iv, dv2 ); + } + incrementOffsets( iv, dv3 ); + } + incrementOffsets( iv, dv4 ); + } +} + + +// EXPORTS // + +module.exports = unary5d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/5d_accessors.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/5d_accessors.js new file mode 100644 index 000000000000..e801be91d2ac --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/5d_accessors.js @@ -0,0 +1,258 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params */ + +'use strict'; + +// MODULES // + +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to callback function and assigns results to a provided output ndarray. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {boolean} isRowMajor - boolean indicating whether the input ndarray is row-major +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var accessors = require( '@stdlib/array/base/accessors' ); +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* var xbuf = toAccessorArray( 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 ] ) ); +* var ybuf = toAccessorArray( new Float64Array( [ 0.0, 0.0, 0.0 ] ) ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major', +* 'accessors': accessors( xbuf ).accessors +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major', +* 'accessors': accessors( ybuf ).accessors +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2, 3, 4 ]; +* var cdims = [ 5, 6 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 12, 12, 4 ]; +* +* // Perform a reduction: +* unary5d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, true, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ [ [ 8.0, 16.0, 24.0 ] ] ] ] ] +*/ +function unary5d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, isRowMajor, opts, hasOpts, clbk, thisArg ) { + var ybuf; + var set; + var dv0; + var dv1; + var dv2; + var dv3; + var dv4; + var sh; + var S0; + var S1; + var S2; + var S3; + var S4; + var sv; + var iv; + var i0; + var i1; + var i2; + var i3; + var i4; + var x; + var y; + var v; + var i; + var f; + + // Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop... + + // Cache a reference to the input ndarray: + x = arrays[ 0 ]; + + // Resolve the output ndarray and associated shape: + y = arrays[ 1 ]; + sh = y.shape; + + // Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments... + if ( isRowMajor ) { + // For row-major ndarrays, the last dimensions have the fastest changing indices... + S0 = sh[ 4 ]; + S1 = sh[ 3 ]; + S2 = sh[ 2 ]; + S3 = sh[ 1 ]; + S4 = sh[ 0 ]; + dv0 = [ strides[4] ]; // offset increment for innermost loop + dv1 = [ strides[3] - ( S0*strides[4] ) ]; + dv2 = [ strides[2] - ( S1*strides[3] ) ]; + dv3 = [ strides[1] - ( S2*strides[2] ) ]; + dv4 = [ strides[0] - ( S3*strides[1] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[4] ); + dv1.push( sv[3] - ( S0*sv[4] ) ); + dv2.push( sv[2] - ( S1*sv[3] ) ); + dv3.push( sv[1] - ( S2*sv[2] ) ); + dv4.push( sv[0] - ( S3*sv[1] ) ); + } + } else { // order === 'column-major' + // For column-major ndarrays, the first dimensions have the fastest changing indices... + S0 = sh[ 0 ]; + S1 = sh[ 1 ]; + S2 = sh[ 2 ]; + S3 = sh[ 3 ]; + S4 = sh[ 4 ]; + dv0 = [ strides[0] ]; // offset increment for innermost loop + dv1 = [ strides[1] - ( S0*strides[0] ) ]; + dv2 = [ strides[2] - ( S1*strides[1] ) ]; + dv3 = [ strides[3] - ( S2*strides[2] ) ]; + dv4 = [ strides[4] - ( S3*strides[3] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[0] ); + dv1.push( sv[1] - ( S0*sv[0] ) ); + dv2.push( sv[2] - ( S1*sv[1] ) ); + dv3.push( sv[3] - ( S2*sv[2] ) ); + dv4.push( sv[4] - ( S3*sv[3] ) ); + } + } + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + iv = offsets( arrays ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Cache accessors: + set = y.accessors[ 1 ]; + + // Iterate over the non-reduced ndarray dimensions... + for ( i4 = 0; i4 < S4; i4++ ) { + for ( i3 = 0; i3 < S3; i3++ ) { + for ( i2 = 0; i2 < S2; i2++ ) { + for ( i1 = 0; i1 < S1; i1++ ) { + for ( i0 = 0; i0 < S0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ i4, i3, i2, i1, i0 ], cdims, clbk, thisArg ); + set( ybuf, iv[1], ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ) ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + incrementOffsets( iv, dv2 ); + } + incrementOffsets( iv, dv3 ); + } + incrementOffsets( iv, dv4 ); + } +} + + +// EXPORTS // + +module.exports = unary5d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/5d_blocked.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/5d_blocked.js new file mode 100644 index 000000000000..9558dbf34110 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/5d_blocked.js @@ -0,0 +1,313 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params, max-depth, max-statements */ + +'use strict'; + +// MODULES // + +var loopOrder = require( '@stdlib/ndarray/base/unary-loop-interchange-order' ); +var blockSize = require( '@stdlib/ndarray/base/unary-tiling-block-size' ); +var takeIndexed = require( '@stdlib/array/base/take-indexed' ); +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to a callback function and assigns results to a provided output ndarray via loop blocking. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major' +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major' +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2, 3, 4 ]; +* var cdims = [ 5, 6 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 12, 12, 4 ]; +* +* // Perform a reduction: +* blockedunary5d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ [ [ 8.0, 16.0, 24.0 ] ] ] ] ] +*/ +function blockedunary5d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, opts, hasOpts, clbk, thisArg ) { + var bsize; + var ybuf; + var dv0; + var dv1; + var dv2; + var dv3; + var dv4; + var ov1; + var ov2; + var ov3; + var ov4; + var sh; + var s0; + var s1; + var s2; + var s3; + var s4; + var sv; + var ov; + var iv; + var i0; + var i1; + var i2; + var i3; + var i4; + var j0; + var j1; + var j2; + var j3; + var j4; + var N; + var x; + var y; + var v; + var o; + var k; + var f; + + // Note on variable naming convention: S#, dv#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop... + + N = arrays.length; + x = arrays[ 0 ]; + y = arrays[ 1 ]; + + // Resolve the loop interchange order: + o = loopOrder( y.shape, strides, y.strides ); + sh = o.sh; + sv = [ o.sx, o.sy ]; + for ( k = 2; k < N; k++ ) { + sv.push( takeIndexed( arrays[k].strides, o.idx ) ); + } + // Determine the block size: + bsize = blockSize( x.dtype, y.dtype ); + + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + ov = offsets( arrays ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Cache offset increments for the innermost loop... + dv0 = []; + for ( k = 0; k < N; k++ ) { + dv0.push( sv[k][0] ); + } + // Initialize loop variables... + ov1 = zeros( N ); + ov2 = zeros( N ); + ov3 = zeros( N ); + ov4 = zeros( N ); + dv1 = zeros( N ); + dv2 = zeros( N ); + dv3 = zeros( N ); + dv4 = zeros( N ); + iv = zeros( N ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Iterate over blocks... + for ( j4 = sh[4]; j4 > 0; ) { + if ( j4 < bsize ) { + s4 = j4; + j4 = 0; + } else { + s4 = bsize; + j4 -= bsize; + } + for ( k = 0; k < N; k++ ) { + ov4[ k ] = ov[k] + ( j4*sv[k][4] ); + } + for ( j3 = sh[3]; j3 > 0; ) { + if ( j3 < bsize ) { + s3 = j3; + j3 = 0; + } else { + s3 = bsize; + j3 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv4[ k ] = sv[k][4] - ( s3*sv[k][3] ); + ov3[ k ] = ov4[k] + ( j3*sv[k][3] ); + } + for ( j2 = sh[2]; j2 > 0; ) { + if ( j2 < bsize ) { + s2 = j2; + j2 = 0; + } else { + s2 = bsize; + j2 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv3[ k ] = sv[k][3] - ( s2*sv[k][2] ); + ov2[ k ] = ov3[k] + ( j2*sv[k][2] ); + } + for ( j1 = sh[1]; j1 > 0; ) { + if ( j1 < bsize ) { + s1 = j1; + j1 = 0; + } else { + s1 = bsize; + j1 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv2[ k ] = sv[k][2] - ( s1*sv[k][1] ); + ov1[ k ] = ov2[k] + ( j1*sv[k][1] ); + } + for ( j0 = sh[0]; j0 > 0; ) { + if ( j0 < bsize ) { + s0 = j0; + j0 = 0; + } else { + s0 = bsize; + j0 -= bsize; + } + // Compute index offsets and loop offset increments for the first ndarray elements in the current block... + for ( k = 0; k < N; k++ ) { + iv[ k ] = ov1[k] + ( j0*sv[k][0] ); + dv1[ k ] = sv[k][1] - ( s0*sv[k][0] ); + } + // Iterate over the non-reduced ndarray dimensions... + for ( i4 = 0; i4 < s4; i4++ ) { + for ( i3 = 0; i3 < s3; i3++ ) { + for ( i2 = 0; i2 < s2; i2++ ) { + for ( i1 = 0; i1 < s1; i1++ ) { + for ( i0 = 0; i0 < s0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ j4+i4, j3+i3, j2+i2, j1+i1, j0+i0 ], cdims, clbk, thisArg ); + ybuf[ iv[1] ] = ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + incrementOffsets( iv, dv2 ); + } + incrementOffsets( iv, dv3 ); + } + incrementOffsets( iv, dv4 ); + } + } + } + } + } + } +} + + +// EXPORTS // + +module.exports = blockedunary5d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/5d_blocked_accessors.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/5d_blocked_accessors.js new file mode 100644 index 000000000000..09c7312051ed --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/5d_blocked_accessors.js @@ -0,0 +1,321 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params, max-depth, max-statements */ + +'use strict'; + +// MODULES // + +var loopOrder = require( '@stdlib/ndarray/base/unary-loop-interchange-order' ); +var blockSize = require( '@stdlib/ndarray/base/unary-tiling-block-size' ); +var takeIndexed = require( '@stdlib/array/base/take-indexed' ); +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to a callback function and assigns results to a provided output ndarray via loop blocking. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var accessors = require( '@stdlib/array/base/accessors' ); +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major', +* 'accessors': accessors( xbuf ).accessors +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major', +* 'accessors': accessors( ybuf ).accessors +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2, 3, 4 ]; +* var cdims = [ 5, 6 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 12, 12, 4 ]; +* +* // Perform a reduction: +* blockedunary5d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ [ [ 8.0, 16.0, 24.0 ] ] ] ] ] +*/ +function blockedunary5d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, opts, hasOpts, clbk, thisArg ) { + var bsize; + var ybuf; + var set; + var dv0; + var dv1; + var dv2; + var dv3; + var dv4; + var ov1; + var ov2; + var ov3; + var ov4; + var sh; + var s0; + var s1; + var s2; + var s3; + var s4; + var sv; + var ov; + var iv; + var i0; + var i1; + var i2; + var i3; + var i4; + var j0; + var j1; + var j2; + var j3; + var j4; + var N; + var x; + var y; + var v; + var o; + var k; + var f; + + // Note on variable naming convention: S#, dv#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop... + + N = arrays.length; + x = arrays[ 0 ]; + y = arrays[ 1 ]; + + // Resolve the loop interchange order: + o = loopOrder( y.shape, strides, y.strides ); + sh = o.sh; + sv = [ o.sx, o.sy ]; + for ( k = 2; k < N; k++ ) { + sv.push( takeIndexed( arrays[k].strides, o.idx ) ); + } + // Determine the block size: + bsize = blockSize( x.dtype, y.dtype ); + + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + ov = offsets( arrays ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Cache accessors: + set = y.accessors[1]; + + // Cache offset increments for the innermost loop... + dv0 = []; + for ( k = 0; k < N; k++ ) { + dv0.push( sv[k][0] ); + } + // Initialize loop variables... + ov1 = zeros( N ); + ov2 = zeros( N ); + ov3 = zeros( N ); + ov4 = zeros( N ); + dv1 = zeros( N ); + dv2 = zeros( N ); + dv3 = zeros( N ); + dv4 = zeros( N ); + iv = zeros( N ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Iterate over blocks... + for ( j4 = sh[4]; j4 > 0; ) { + if ( j4 < bsize ) { + s4 = j4; + j4 = 0; + } else { + s4 = bsize; + j4 -= bsize; + } + for ( k = 0; k < N; k++ ) { + ov4[ k ] = ov[k] + ( j4*sv[k][4] ); + } + for ( j3 = sh[3]; j3 > 0; ) { + if ( j3 < bsize ) { + s3 = j3; + j3 = 0; + } else { + s3 = bsize; + j3 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv4[ k ] = sv[k][4] - ( s3*sv[k][3] ); + ov3[ k ] = ov4[k] + ( j3*sv[k][3] ); + } + for ( j2 = sh[2]; j2 > 0; ) { + if ( j2 < bsize ) { + s2 = j2; + j2 = 0; + } else { + s2 = bsize; + j2 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv3[ k ] = sv[k][3] - ( s2*sv[k][2] ); + ov2[ k ] = ov3[k] + ( j2*sv[k][2] ); + } + for ( j1 = sh[1]; j1 > 0; ) { + if ( j1 < bsize ) { + s1 = j1; + j1 = 0; + } else { + s1 = bsize; + j1 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv2[ k ] = sv[k][2] - ( s1*sv[k][1] ); + ov1[ k ] = ov2[k] + ( j1*sv[k][1] ); + } + for ( j0 = sh[0]; j0 > 0; ) { + if ( j0 < bsize ) { + s0 = j0; + j0 = 0; + } else { + s0 = bsize; + j0 -= bsize; + } + // Compute index offsets and loop offset increments for the first ndarray elements in the current block... + for ( k = 0; k < N; k++ ) { + iv[ k ] = ov1[k] + ( j0*sv[k][0] ); + dv1[ k ] = sv[k][1] - ( s0*sv[k][0] ); + } + // Iterate over the non-reduced ndarray dimensions... + for ( i4 = 0; i4 < s4; i4++ ) { + for ( i3 = 0; i3 < s3; i3++ ) { + for ( i2 = 0; i2 < s2; i2++ ) { + for ( i1 = 0; i1 < s1; i1++ ) { + for ( i0 = 0; i0 < s0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ j4+i4, j3+i3, j2+i2, j1+i1, j0+i0 ], cdims, clbk, thisArg ); + set( ybuf, iv[1], ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ) ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + incrementOffsets( iv, dv2 ); + } + incrementOffsets( iv, dv3 ); + } + incrementOffsets( iv, dv4 ); + } + } + } + } + } + } +} + + +// EXPORTS // + +module.exports = blockedunary5d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/6d.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/6d.js new file mode 100644 index 000000000000..85881fd61106 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/6d.js @@ -0,0 +1,262 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params, max-depth */ + +'use strict'; + +// MODULES // + +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to callback function and assigns results to a provided output ndarray. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {boolean} isRowMajor - boolean indicating whether the input ndarray is row-major +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 1, 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 1, 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 12, 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 3, 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major' +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major' +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2, 3, 4, 5 ]; +* var cdims = [ 6, 7 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 12, 12, 12, 4 ]; +* +* // Perform a reduction: +* unary6d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, true, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ [ [ [ 8.0, 16.0, 24.0 ] ] ] ] ] ] +*/ +function unary6d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, isRowMajor, opts, hasOpts, clbk, thisArg ) { + var ybuf; + var dv0; + var dv1; + var dv2; + var dv3; + var dv4; + var dv5; + var sh; + var S0; + var S1; + var S2; + var S3; + var S4; + var S5; + var sv; + var iv; + var i0; + var i1; + var i2; + var i3; + var i4; + var i5; + var x; + var y; + var v; + var i; + var f; + + // Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop... + + // Cache a reference to the input ndarray: + x = arrays[ 0 ]; + + // Resolve the output ndarray and associated shape: + y = arrays[ 1 ]; + sh = y.shape; + + // Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments... + if ( isRowMajor ) { + // For row-major ndarrays, the last dimensions have the fastest changing indices... + S0 = sh[ 5 ]; + S1 = sh[ 4 ]; + S2 = sh[ 3 ]; + S3 = sh[ 2 ]; + S4 = sh[ 1 ]; + S5 = sh[ 0 ]; + dv0 = [ strides[5] ]; // offset increment for innermost loop + dv1 = [ strides[4] - ( S0*strides[5] ) ]; + dv2 = [ strides[3] - ( S1*strides[4] ) ]; + dv3 = [ strides[2] - ( S2*strides[3] ) ]; + dv4 = [ strides[1] - ( S3*strides[2] ) ]; + dv5 = [ strides[0] - ( S4*strides[1] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[5] ); + dv1.push( sv[4] - ( S0*sv[5] ) ); + dv2.push( sv[3] - ( S1*sv[4] ) ); + dv3.push( sv[2] - ( S2*sv[3] ) ); + dv4.push( sv[1] - ( S3*sv[2] ) ); + dv5.push( sv[0] - ( S4*sv[1] ) ); + } + } else { // order === 'column-major' + // For column-major ndarrays, the first dimensions have the fastest changing indices... + S0 = sh[ 0 ]; + S1 = sh[ 1 ]; + S2 = sh[ 2 ]; + S3 = sh[ 3 ]; + S4 = sh[ 4 ]; + S5 = sh[ 5 ]; + dv0 = [ strides[0] ]; // offset increment for innermost loop + dv1 = [ strides[1] - ( S0*strides[0] ) ]; + dv2 = [ strides[2] - ( S1*strides[1] ) ]; + dv3 = [ strides[3] - ( S2*strides[2] ) ]; + dv4 = [ strides[4] - ( S3*strides[3] ) ]; + dv5 = [ strides[5] - ( S4*strides[4] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[0] ); + dv1.push( sv[1] - ( S0*sv[0] ) ); + dv2.push( sv[2] - ( S1*sv[1] ) ); + dv3.push( sv[3] - ( S2*sv[2] ) ); + dv4.push( sv[4] - ( S3*sv[3] ) ); + dv5.push( sv[5] - ( S4*sv[4] ) ); + } + } + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + iv = offsets( arrays ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Iterate over the non-reduced ndarray dimensions... + for ( i5 = 0; i5 < S5; i5++ ) { + for ( i4 = 0; i4 < S4; i4++ ) { + for ( i3 = 0; i3 < S3; i3++ ) { + for ( i2 = 0; i2 < S2; i2++ ) { + for ( i1 = 0; i1 < S1; i1++ ) { + for ( i0 = 0; i0 < S0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ i5, i4, i3, i2, i1, i0 ], cdims, clbk, thisArg ); + ybuf[ iv[1] ] = ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + incrementOffsets( iv, dv2 ); + } + incrementOffsets( iv, dv3 ); + } + incrementOffsets( iv, dv4 ); + } + incrementOffsets( iv, dv5 ); + } +} + + +// EXPORTS // + +module.exports = unary6d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/6d_accessors.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/6d_accessors.js new file mode 100644 index 000000000000..9fde07e65c88 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/6d_accessors.js @@ -0,0 +1,270 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params, max-depth */ + +'use strict'; + +// MODULES // + +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to callback function and assigns results to a provided output ndarray. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {boolean} isRowMajor - boolean indicating whether the input ndarray is row-major +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var accessors = require( '@stdlib/array/base/accessors' ); +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* var xbuf = toAccessorArray( 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 ] ) ); +* var ybuf = toAccessorArray( new Float64Array( [ 0.0, 0.0, 0.0 ] ) ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 1, 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 1, 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 12, 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 3, 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major', +* 'accessors': accessors( xbuf ).accessors +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major', +* 'accessors': accessors( ybuf ).accessors +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2, 3, 4, 5 ]; +* var cdims = [ 6, 7 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 12, 12, 12, 4 ]; +* +* // Perform a reduction: +* unary6d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, true, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ [ [ [ 8.0, 16.0, 24.0 ] ] ] ] ] ] +*/ +function unary6d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, isRowMajor, opts, hasOpts, clbk, thisArg ) { + var ybuf; + var set; + var dv0; + var dv1; + var dv2; + var dv3; + var dv4; + var dv5; + var sh; + var S0; + var S1; + var S2; + var S3; + var S4; + var S5; + var sv; + var iv; + var i0; + var i1; + var i2; + var i3; + var i4; + var i5; + var x; + var y; + var v; + var i; + var f; + + // Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop... + + // Cache a reference to the input ndarray: + x = arrays[ 0 ]; + + // Resolve the output ndarray and associated shape: + y = arrays[ 1 ]; + sh = y.shape; + + // Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments... + if ( isRowMajor ) { + // For row-major ndarrays, the last dimensions have the fastest changing indices... + S0 = sh[ 5 ]; + S1 = sh[ 4 ]; + S2 = sh[ 3 ]; + S3 = sh[ 2 ]; + S4 = sh[ 1 ]; + S5 = sh[ 0 ]; + dv0 = [ strides[5] ]; // offset increment for innermost loop + dv1 = [ strides[4] - ( S0*strides[5] ) ]; + dv2 = [ strides[3] - ( S1*strides[4] ) ]; + dv3 = [ strides[2] - ( S2*strides[3] ) ]; + dv4 = [ strides[1] - ( S3*strides[2] ) ]; + dv5 = [ strides[0] - ( S4*strides[1] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[5] ); + dv1.push( sv[4] - ( S0*sv[5] ) ); + dv2.push( sv[3] - ( S1*sv[4] ) ); + dv3.push( sv[2] - ( S2*sv[3] ) ); + dv4.push( sv[1] - ( S3*sv[2] ) ); + dv5.push( sv[0] - ( S4*sv[1] ) ); + } + } else { // order === 'column-major' + // For column-major ndarrays, the first dimensions have the fastest changing indices... + S0 = sh[ 0 ]; + S1 = sh[ 1 ]; + S2 = sh[ 2 ]; + S3 = sh[ 3 ]; + S4 = sh[ 4 ]; + S5 = sh[ 5 ]; + dv0 = [ strides[0] ]; // offset increment for innermost loop + dv1 = [ strides[1] - ( S0*strides[0] ) ]; + dv2 = [ strides[2] - ( S1*strides[1] ) ]; + dv3 = [ strides[3] - ( S2*strides[2] ) ]; + dv4 = [ strides[4] - ( S3*strides[3] ) ]; + dv5 = [ strides[5] - ( S4*strides[4] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[0] ); + dv1.push( sv[1] - ( S0*sv[0] ) ); + dv2.push( sv[2] - ( S1*sv[1] ) ); + dv3.push( sv[3] - ( S2*sv[2] ) ); + dv4.push( sv[4] - ( S3*sv[3] ) ); + dv5.push( sv[5] - ( S4*sv[4] ) ); + } + } + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + iv = offsets( arrays ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Cache accessors: + set = y.accessors[ 1 ]; + + // Iterate over the non-reduced ndarray dimensions... + for ( i5 = 0; i5 < S5; i5++ ) { + for ( i4 = 0; i4 < S4; i4++ ) { + for ( i3 = 0; i3 < S3; i3++ ) { + for ( i2 = 0; i2 < S2; i2++ ) { + for ( i1 = 0; i1 < S1; i1++ ) { + for ( i0 = 0; i0 < S0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ i5, i4, i3, i2, i1, i0 ], cdims, clbk, thisArg ); + set( ybuf, iv[1], ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ) ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + incrementOffsets( iv, dv2 ); + } + incrementOffsets( iv, dv3 ); + } + incrementOffsets( iv, dv4 ); + } + incrementOffsets( iv, dv5 ); + } +} + + +// EXPORTS // + +module.exports = unary6d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/6d_blocked.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/6d_blocked.js new file mode 100644 index 000000000000..47f21f5f20cf --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/6d_blocked.js @@ -0,0 +1,336 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params, max-depth, max-statements */ + +'use strict'; + +// MODULES // + +var loopOrder = require( '@stdlib/ndarray/base/unary-loop-interchange-order' ); +var blockSize = require( '@stdlib/ndarray/base/unary-tiling-block-size' ); +var takeIndexed = require( '@stdlib/array/base/take-indexed' ); +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to a callback function and assigns results to a provided output ndarray via loop blocking. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 1, 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 1, 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 12, 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 3, 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major' +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major' +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2, 3, 4, 5 ]; +* var cdims = [ 6, 7 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 12, 12, 12, 4 ]; +* +* // Perform a reduction: +* blockedunary6d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ [ [ [ 8.0, 16.0, 24.0 ] ] ] ] ] ] +*/ +function blockedunary6d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, opts, hasOpts, clbk, thisArg ) { + var bsize; + var ybuf; + var dv0; + var dv1; + var dv2; + var dv3; + var dv4; + var dv5; + var ov1; + var ov2; + var ov3; + var ov4; + var ov5; + var sh; + var s0; + var s1; + var s2; + var s3; + var s4; + var s5; + var sv; + var ov; + var iv; + var i0; + var i1; + var i2; + var i3; + var i4; + var i5; + var j0; + var j1; + var j2; + var j3; + var j4; + var j5; + var N; + var x; + var y; + var v; + var o; + var k; + var f; + + // Note on variable naming convention: S#, dv#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop... + + N = arrays.length; + x = arrays[ 0 ]; + y = arrays[ 1 ]; + + // Resolve the loop interchange order: + o = loopOrder( y.shape, strides, y.strides ); + sh = o.sh; + sv = [ o.sx, o.sy ]; + for ( k = 2; k < N; k++ ) { + sv.push( takeIndexed( arrays[k].strides, o.idx ) ); + } + // Determine the block size: + bsize = blockSize( x.dtype, y.dtype ); + + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + ov = offsets( arrays ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Cache offset increments for the innermost loop... + dv0 = []; + for ( k = 0; k < N; k++ ) { + dv0.push( sv[k][0] ); + } + // Initialize loop variables... + ov1 = zeros( N ); + ov2 = zeros( N ); + ov3 = zeros( N ); + ov4 = zeros( N ); + ov5 = zeros( N ); + dv1 = zeros( N ); + dv2 = zeros( N ); + dv3 = zeros( N ); + dv4 = zeros( N ); + dv5 = zeros( N ); + iv = zeros( N ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Iterate over blocks... + for ( j5 = sh[5]; j5 > 0; ) { + if ( j5 < bsize ) { + s5 = j5; + j5 = 0; + } else { + s5 = bsize; + j5 -= bsize; + } + for ( k = 0; k < N; k++ ) { + ov5[ k ] = ov[k] + ( j5*sv[k][5] ); + } + for ( j4 = sh[4]; j4 > 0; ) { + if ( j4 < bsize ) { + s4 = j4; + j4 = 0; + } else { + s4 = bsize; + j4 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv5 = sv[k][5] - ( s4*sv[k][4] ); + ov4[ k ] = ov5[k] + ( j4*sv[k][4] ); + } + for ( j3 = sh[3]; j3 > 0; ) { + if ( j3 < bsize ) { + s3 = j3; + j3 = 0; + } else { + s3 = bsize; + j3 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv4[ k ] = sv[k][4] - ( s3*sv[k][3] ); + ov3[ k ] = ov4[k] + ( j3*sv[k][3] ); + } + for ( j2 = sh[2]; j2 > 0; ) { + if ( j2 < bsize ) { + s2 = j2; + j2 = 0; + } else { + s2 = bsize; + j2 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv3[ k ] = sv[k][3] - ( s2*sv[k][2] ); + ov2[ k ] = ov3[k] + ( j2*sv[k][2] ); + } + for ( j1 = sh[1]; j1 > 0; ) { + if ( j1 < bsize ) { + s1 = j1; + j1 = 0; + } else { + s1 = bsize; + j1 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv2[ k ] = sv[k][2] - ( s1*sv[k][1] ); + ov1[ k ] = ov2[k] + ( j1*sv[k][1] ); + } + for ( j0 = sh[0]; j0 > 0; ) { + if ( j0 < bsize ) { + s0 = j0; + j0 = 0; + } else { + s0 = bsize; + j0 -= bsize; + } + // Compute index offsets and loop offset increments for the first ndarray elements in the current block... + for ( k = 0; k < N; k++ ) { + iv[ k ] = ov1[k] + ( j0*sv[k][0] ); + dv1[ k ] = sv[k][1] - ( s0*sv[k][0] ); + } + // Iterate over the non-reduced ndarray dimensions... + for ( i5 = 0; i5 < s5; i5++ ) { + for ( i4 = 0; i4 < s4; i4++ ) { + for ( i3 = 0; i3 < s3; i3++ ) { + for ( i2 = 0; i2 < s2; i2++ ) { + for ( i1 = 0; i1 < s1; i1++ ) { + for ( i0 = 0; i0 < s0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ j5+i5, j4+i4, j3+i3, j2+i2, j1+i1, j0+i0 ], cdims, clbk, thisArg ); + ybuf[ iv[1] ] = ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + incrementOffsets( iv, dv2 ); + } + incrementOffsets( iv, dv3 ); + } + incrementOffsets( iv, dv4 ); + } + incrementOffsets( iv, dv5 ); + } + } + } + } + } + } + } +} + + +// EXPORTS // + +module.exports = blockedunary6d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/6d_blocked_accessors.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/6d_blocked_accessors.js new file mode 100644 index 000000000000..eb15a2ef3bbb --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/6d_blocked_accessors.js @@ -0,0 +1,344 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params, max-depth, max-statements */ + +'use strict'; + +// MODULES // + +var loopOrder = require( '@stdlib/ndarray/base/unary-loop-interchange-order' ); +var blockSize = require( '@stdlib/ndarray/base/unary-tiling-block-size' ); +var takeIndexed = require( '@stdlib/array/base/take-indexed' ); +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to a callback function and assigns results to a provided output ndarray via loop blocking. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var accessors = require( '@stdlib/array/base/accessors' ); +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 1, 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 1, 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 12, 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 3, 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major', +* 'accessors': accessors( xbuf ).accessors +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major', +* 'accessors': accessors( ybuf ).accessors +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2, 3, 4, 5 ]; +* var cdims = [ 6, 7 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 12, 12, 12, 4 ]; +* +* // Perform a reduction: +* blockedunary6d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ [ [ [ 8.0, 16.0, 24.0 ] ] ] ] ] ] +*/ +function blockedunary6d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, opts, hasOpts, clbk, thisArg ) { + var bsize; + var ybuf; + var set; + var dv0; + var dv1; + var dv2; + var dv3; + var dv4; + var dv5; + var ov1; + var ov2; + var ov3; + var ov4; + var ov5; + var sh; + var s0; + var s1; + var s2; + var s3; + var s4; + var s5; + var sv; + var ov; + var iv; + var i0; + var i1; + var i2; + var i3; + var i4; + var i5; + var j0; + var j1; + var j2; + var j3; + var j4; + var j5; + var N; + var x; + var y; + var v; + var o; + var k; + var f; + + // Note on variable naming convention: S#, dv#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop... + + N = arrays.length; + x = arrays[ 0 ]; + y = arrays[ 1 ]; + + // Resolve the loop interchange order: + o = loopOrder( y.shape, strides, y.strides ); + sh = o.sh; + sv = [ o.sx, o.sy ]; + for ( k = 2; k < N; k++ ) { + sv.push( takeIndexed( arrays[k].strides, o.idx ) ); + } + // Determine the block size: + bsize = blockSize( x.dtype, y.dtype ); + + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + ov = offsets( arrays ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Cache accessors: + set = y.accessors[1]; + + // Cache offset increments for the innermost loop... + dv0 = []; + for ( k = 0; k < N; k++ ) { + dv0.push( sv[k][0] ); + } + // Initialize loop variables... + ov1 = zeros( N ); + ov2 = zeros( N ); + ov3 = zeros( N ); + ov4 = zeros( N ); + ov5 = zeros( N ); + dv1 = zeros( N ); + dv2 = zeros( N ); + dv3 = zeros( N ); + dv4 = zeros( N ); + dv5 = zeros( N ); + iv = zeros( N ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Iterate over blocks... + for ( j5 = sh[5]; j5 > 0; ) { + if ( j5 < bsize ) { + s5 = j5; + j5 = 0; + } else { + s5 = bsize; + j5 -= bsize; + } + for ( k = 0; k < N; k++ ) { + ov5[ k ] = ov[k] + ( j5*sv[k][5] ); + } + for ( j4 = sh[4]; j4 > 0; ) { + if ( j4 < bsize ) { + s4 = j4; + j4 = 0; + } else { + s4 = bsize; + j4 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv5 = sv[k][5] - ( s4*sv[k][4] ); + ov4[ k ] = ov5[k] + ( j4*sv[k][4] ); + } + for ( j3 = sh[3]; j3 > 0; ) { + if ( j3 < bsize ) { + s3 = j3; + j3 = 0; + } else { + s3 = bsize; + j3 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv4[ k ] = sv[k][4] - ( s3*sv[k][3] ); + ov3[ k ] = ov4[k] + ( j3*sv[k][3] ); + } + for ( j2 = sh[2]; j2 > 0; ) { + if ( j2 < bsize ) { + s2 = j2; + j2 = 0; + } else { + s2 = bsize; + j2 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv3[ k ] = sv[k][3] - ( s2*sv[k][2] ); + ov2[ k ] = ov3[k] + ( j2*sv[k][2] ); + } + for ( j1 = sh[1]; j1 > 0; ) { + if ( j1 < bsize ) { + s1 = j1; + j1 = 0; + } else { + s1 = bsize; + j1 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv2[ k ] = sv[k][2] - ( s1*sv[k][1] ); + ov1[ k ] = ov2[k] + ( j1*sv[k][1] ); + } + for ( j0 = sh[0]; j0 > 0; ) { + if ( j0 < bsize ) { + s0 = j0; + j0 = 0; + } else { + s0 = bsize; + j0 -= bsize; + } + // Compute index offsets and loop offset increments for the first ndarray elements in the current block... + for ( k = 0; k < N; k++ ) { + iv[ k ] = ov1[k] + ( j0*sv[k][0] ); + dv1[ k ] = sv[k][1] - ( s0*sv[k][0] ); + } + // Iterate over the non-reduced ndarray dimensions... + for ( i5 = 0; i5 < s5; i5++ ) { + for ( i4 = 0; i4 < s4; i4++ ) { + for ( i3 = 0; i3 < s3; i3++ ) { + for ( i2 = 0; i2 < s2; i2++ ) { + for ( i1 = 0; i1 < s1; i1++ ) { + for ( i0 = 0; i0 < s0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ j5+i5, j4+i4, j3+i3, j2+i2, j1+i1, j0+i0 ], cdims, clbk, thisArg ); + set( ybuf, iv[1], ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ) ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + incrementOffsets( iv, dv2 ); + } + incrementOffsets( iv, dv3 ); + } + incrementOffsets( iv, dv4 ); + } + incrementOffsets( iv, dv5 ); + } + } + } + } + } + } + } +} + + +// EXPORTS // + +module.exports = blockedunary6d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/7d.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/7d.js new file mode 100644 index 000000000000..c29cd06aed91 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/7d.js @@ -0,0 +1,274 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params, max-depth */ + +'use strict'; + +// MODULES // + +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to callback function and assigns results to a provided output ndarray. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {boolean} isRowMajor - boolean indicating whether the input ndarray is row-major +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 1, 1, 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 1, 1, 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 12, 12, 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 3, 3, 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major' +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major' +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2, 3, 4, 5, 6 ]; +* var cdims = [ 7, 8 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 12, 12, 12, 12, 4 ]; +* +* // Perform a reduction: +* unary7d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, true, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ [ [ [ [ 8.0, 16.0, 24.0 ] ] ] ] ] ] ] +*/ +function unary7d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, isRowMajor, opts, hasOpts, clbk, thisArg ) { + var ybuf; + var dv0; + var dv1; + var dv2; + var dv3; + var dv4; + var dv5; + var dv6; + var sh; + var S0; + var S1; + var S2; + var S3; + var S4; + var S5; + var S6; + var sv; + var iv; + var i0; + var i1; + var i2; + var i3; + var i4; + var i5; + var i6; + var x; + var y; + var v; + var i; + var f; + + // Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop... + + // Cache a reference to the input ndarray: + x = arrays[ 0 ]; + + // Resolve the output ndarray and associated shape: + y = arrays[ 1 ]; + sh = y.shape; + + // Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments... + if ( isRowMajor ) { + // For row-major ndarrays, the last dimensions have the fastest changing indices... + S0 = sh[ 6 ]; + S1 = sh[ 5 ]; + S2 = sh[ 4 ]; + S3 = sh[ 3 ]; + S4 = sh[ 2 ]; + S5 = sh[ 1 ]; + S6 = sh[ 0 ]; + dv0 = [ strides[6] ]; // offset increment for innermost loop + dv1 = [ strides[5] - ( S0*strides[6] ) ]; + dv2 = [ strides[4] - ( S1*strides[5] ) ]; + dv3 = [ strides[3] - ( S2*strides[4] ) ]; + dv4 = [ strides[2] - ( S3*strides[3] ) ]; + dv5 = [ strides[1] - ( S4*strides[2] ) ]; + dv6 = [ strides[0] - ( S5*strides[1] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[6] ); + dv1.push( sv[5] - ( S0*sv[6] ) ); + dv2.push( sv[4] - ( S1*sv[5] ) ); + dv3.push( sv[3] - ( S2*sv[4] ) ); + dv4.push( sv[2] - ( S3*sv[3] ) ); + dv5.push( sv[1] - ( S4*sv[2] ) ); + dv6.push( sv[0] - ( S5*sv[1] ) ); + } + } else { // order === 'column-major' + // For column-major ndarrays, the first dimensions have the fastest changing indices... + S0 = sh[ 0 ]; + S1 = sh[ 1 ]; + S2 = sh[ 2 ]; + S3 = sh[ 3 ]; + S4 = sh[ 4 ]; + S5 = sh[ 5 ]; + S6 = sh[ 6 ]; + dv0 = [ strides[0] ]; // offset increment for innermost loop + dv1 = [ strides[1] - ( S0*strides[0] ) ]; + dv2 = [ strides[2] - ( S1*strides[1] ) ]; + dv3 = [ strides[3] - ( S2*strides[2] ) ]; + dv4 = [ strides[4] - ( S3*strides[3] ) ]; + dv5 = [ strides[5] - ( S4*strides[4] ) ]; + dv6 = [ strides[6] - ( S5*strides[5] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[0] ); + dv1.push( sv[1] - ( S0*sv[0] ) ); + dv2.push( sv[2] - ( S1*sv[1] ) ); + dv3.push( sv[3] - ( S2*sv[2] ) ); + dv4.push( sv[4] - ( S3*sv[3] ) ); + dv5.push( sv[5] - ( S4*sv[4] ) ); + dv6.push( sv[6] - ( S5*sv[5] ) ); + } + } + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + iv = offsets( arrays ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Iterate over the non-reduced ndarray dimensions... + for ( i6 = 0; i6 < S6; i6++ ) { + for ( i5 = 0; i5 < S5; i5++ ) { + for ( i4 = 0; i4 < S4; i4++ ) { + for ( i3 = 0; i3 < S3; i3++ ) { + for ( i2 = 0; i2 < S2; i2++ ) { + for ( i1 = 0; i1 < S1; i1++ ) { + for ( i0 = 0; i0 < S0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ i6, i5, i4, i3, i2, i1, i0 ], cdims, clbk, thisArg ); + ybuf[ iv[1] ] = ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + incrementOffsets( iv, dv2 ); + } + incrementOffsets( iv, dv3 ); + } + incrementOffsets( iv, dv4 ); + } + incrementOffsets( iv, dv5 ); + } + incrementOffsets( iv, dv6 ); + } +} + + +// EXPORTS // + +module.exports = unary7d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/7d_accessors.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/7d_accessors.js new file mode 100644 index 000000000000..40a000d5466d --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/7d_accessors.js @@ -0,0 +1,282 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params, max-depth, max-statements */ + +'use strict'; + +// MODULES // + +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to callback function and assigns results to a provided output ndarray. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {boolean} isRowMajor - boolean indicating whether the input ndarray is row-major +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var accessors = require( '@stdlib/array/base/accessors' ); +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* var xbuf = toAccessorArray( 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 ] ) ); +* var ybuf = toAccessorArray( new Float64Array( [ 0.0, 0.0, 0.0 ] ) ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 1, 1, 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 1, 1, 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 12, 12, 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 3, 3, 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major', +* 'accessors': accessors( xbuf ).accessors +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major', +* 'accessors': accessors( ybuf ).accessors +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2, 3, 4, 5, 6 ]; +* var cdims = [ 7, 8 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 12, 12, 12, 12, 4 ]; +* +* // Perform a reduction: +* unary7d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, true, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ [ [ [ [ 8.0, 16.0, 24.0 ] ] ] ] ] ] ] +*/ +function unary7d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, isRowMajor, opts, hasOpts, clbk, thisArg ) { + var ybuf; + var set; + var dv0; + var dv1; + var dv2; + var dv3; + var dv4; + var dv5; + var dv6; + var sh; + var S0; + var S1; + var S2; + var S3; + var S4; + var S5; + var S6; + var sv; + var iv; + var i0; + var i1; + var i2; + var i3; + var i4; + var i5; + var i6; + var x; + var y; + var v; + var i; + var f; + + // Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop... + + // Cache a reference to the input ndarray: + x = arrays[ 0 ]; + + // Resolve the output ndarray and associated shape: + y = arrays[ 1 ]; + sh = y.shape; + + // Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments... + if ( isRowMajor ) { + // For row-major ndarrays, the last dimensions have the fastest changing indices... + S0 = sh[ 6 ]; + S1 = sh[ 5 ]; + S2 = sh[ 4 ]; + S3 = sh[ 3 ]; + S4 = sh[ 2 ]; + S5 = sh[ 1 ]; + S6 = sh[ 0 ]; + dv0 = [ strides[6] ]; // offset increment for innermost loop + dv1 = [ strides[5] - ( S0*strides[6] ) ]; + dv2 = [ strides[4] - ( S1*strides[5] ) ]; + dv3 = [ strides[3] - ( S2*strides[4] ) ]; + dv4 = [ strides[2] - ( S3*strides[3] ) ]; + dv5 = [ strides[1] - ( S4*strides[2] ) ]; + dv6 = [ strides[0] - ( S5*strides[1] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[6] ); + dv1.push( sv[5] - ( S0*sv[6] ) ); + dv2.push( sv[4] - ( S1*sv[5] ) ); + dv3.push( sv[3] - ( S2*sv[4] ) ); + dv4.push( sv[2] - ( S3*sv[3] ) ); + dv5.push( sv[1] - ( S4*sv[2] ) ); + dv6.push( sv[0] - ( S5*sv[1] ) ); + } + } else { // order === 'column-major' + // For column-major ndarrays, the first dimensions have the fastest changing indices... + S0 = sh[ 0 ]; + S1 = sh[ 1 ]; + S2 = sh[ 2 ]; + S3 = sh[ 3 ]; + S4 = sh[ 4 ]; + S5 = sh[ 5 ]; + S6 = sh[ 6 ]; + dv0 = [ strides[0] ]; // offset increment for innermost loop + dv1 = [ strides[1] - ( S0*strides[0] ) ]; + dv2 = [ strides[2] - ( S1*strides[1] ) ]; + dv3 = [ strides[3] - ( S2*strides[2] ) ]; + dv4 = [ strides[4] - ( S3*strides[3] ) ]; + dv5 = [ strides[5] - ( S4*strides[4] ) ]; + dv6 = [ strides[6] - ( S5*strides[5] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[0] ); + dv1.push( sv[1] - ( S0*sv[0] ) ); + dv2.push( sv[2] - ( S1*sv[1] ) ); + dv3.push( sv[3] - ( S2*sv[2] ) ); + dv4.push( sv[4] - ( S3*sv[3] ) ); + dv5.push( sv[5] - ( S4*sv[4] ) ); + dv6.push( sv[6] - ( S5*sv[5] ) ); + } + } + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + iv = offsets( arrays ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Cache accessors: + set = y.accessors[ 1 ]; + + // Iterate over the non-reduced ndarray dimensions... + for ( i6 = 0; i6 < S6; i6++ ) { + for ( i5 = 0; i5 < S5; i5++ ) { + for ( i4 = 0; i4 < S4; i4++ ) { + for ( i3 = 0; i3 < S3; i3++ ) { + for ( i2 = 0; i2 < S2; i2++ ) { + for ( i1 = 0; i1 < S1; i1++ ) { + for ( i0 = 0; i0 < S0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ i6, i5, i4, i3, i2, i1, i0 ], cdims, clbk, thisArg ); + set( ybuf, iv[1], ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ) ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + incrementOffsets( iv, dv2 ); + } + incrementOffsets( iv, dv3 ); + } + incrementOffsets( iv, dv4 ); + } + incrementOffsets( iv, dv5 ); + } + incrementOffsets( iv, dv6 ); + } +} + + +// EXPORTS // + +module.exports = unary7d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/7d_blocked.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/7d_blocked.js new file mode 100644 index 000000000000..1fe41020e919 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/7d_blocked.js @@ -0,0 +1,359 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params, max-depth, max-statements */ + +'use strict'; + +// MODULES // + +var loopOrder = require( '@stdlib/ndarray/base/unary-loop-interchange-order' ); +var blockSize = require( '@stdlib/ndarray/base/unary-tiling-block-size' ); +var takeIndexed = require( '@stdlib/array/base/take-indexed' ); +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to a callback function and assigns results to a provided output ndarray via loop blocking. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 1, 1, 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 1, 1, 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 12, 12, 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 3, 3, 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major' +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major' +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2, 3, 4, 5, 6 ]; +* var cdims = [ 7, 8 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 12, 12, 12, 12, 4 ]; +* +* // Perform a reduction: +* blockedunary7d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ [ [ [ [ 8.0, 16.0, 24.0 ] ] ] ] ] ] ] +*/ +function blockedunary7d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, opts, hasOpts, clbk, thisArg ) { + var bsize; + var ybuf; + var dv0; + var dv1; + var dv2; + var dv3; + var dv4; + var dv5; + var dv6; + var ov1; + var ov2; + var ov3; + var ov4; + var ov5; + var ov6; + var sh; + var s0; + var s1; + var s2; + var s3; + var s4; + var s5; + var s6; + var sv; + var ov; + var iv; + var i0; + var i1; + var i2; + var i3; + var i4; + var i5; + var i6; + var j0; + var j1; + var j2; + var j3; + var j4; + var j5; + var j6; + var N; + var x; + var y; + var v; + var o; + var k; + var f; + + // Note on variable naming convention: S#, dv#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop... + + N = arrays.length; + x = arrays[ 0 ]; + y = arrays[ 1 ]; + + // Resolve the loop interchange order: + o = loopOrder( y.shape, strides, y.strides ); + sh = o.sh; + sv = [ o.sx, o.sy ]; + for ( k = 2; k < N; k++ ) { + sv.push( takeIndexed( arrays[k].strides, o.idx ) ); + } + // Determine the block size: + bsize = blockSize( x.dtype, y.dtype ); + + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + ov = offsets( arrays ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Cache offset increments for the innermost loop... + dv0 = []; + for ( k = 0; k < N; k++ ) { + dv0.push( sv[k][0] ); + } + // Initialize loop variables... + ov1 = zeros( N ); + ov2 = zeros( N ); + ov3 = zeros( N ); + ov4 = zeros( N ); + ov5 = zeros( N ); + ov6 = zeros( N ); + dv1 = zeros( N ); + dv2 = zeros( N ); + dv3 = zeros( N ); + dv4 = zeros( N ); + dv5 = zeros( N ); + dv6 = zeros( N ); + iv = zeros( N ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Iterate over blocks... + for ( j6 = sh[6]; j6 > 0; ) { + if ( j6 < bsize ) { + s6 = j6; + j6 = 0; + } else { + s6 = bsize; + j6 -= bsize; + } + for ( k = 0; k < N; k++ ) { + ov6[ k ] = ov[k] + ( j6*sv[k][6] ); + } + for ( j5 = sh[5]; j5 > 0; ) { + if ( j5 < bsize ) { + s5 = j5; + j5 = 0; + } else { + s5 = bsize; + j5 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv6 = sv[k][6] - ( s5*sv[k][5] ); + ov5[ k ] = ov6[k] + ( j5*sv[k][5] ); + } + for ( j4 = sh[4]; j4 > 0; ) { + if ( j4 < bsize ) { + s4 = j4; + j4 = 0; + } else { + s4 = bsize; + j4 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv5 = sv[k][5] - ( s4*sv[k][4] ); + ov4[ k ] = ov5[k] + ( j4*sv[k][4] ); + } + for ( j3 = sh[3]; j3 > 0; ) { + if ( j3 < bsize ) { + s3 = j3; + j3 = 0; + } else { + s3 = bsize; + j3 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv4[ k ] = sv[k][4] - ( s3*sv[k][3] ); + ov3[ k ] = ov4[k] + ( j3*sv[k][3] ); + } + for ( j2 = sh[2]; j2 > 0; ) { + if ( j2 < bsize ) { + s2 = j2; + j2 = 0; + } else { + s2 = bsize; + j2 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv3[ k ] = sv[k][3] - ( s2*sv[k][2] ); + ov2[ k ] = ov3[k] + ( j2*sv[k][2] ); + } + for ( j1 = sh[1]; j1 > 0; ) { + if ( j1 < bsize ) { + s1 = j1; + j1 = 0; + } else { + s1 = bsize; + j1 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv2[ k ] = sv[k][2] - ( s1*sv[k][1] ); + ov1[ k ] = ov2[k] + ( j1*sv[k][1] ); + } + for ( j0 = sh[0]; j0 > 0; ) { + if ( j0 < bsize ) { + s0 = j0; + j0 = 0; + } else { + s0 = bsize; + j0 -= bsize; + } + // Compute index offsets and loop offset increments for the first ndarray elements in the current block... + for ( k = 0; k < N; k++ ) { + iv[ k ] = ov1[k] + ( j0*sv[k][0] ); + dv1[ k ] = sv[k][1] - ( s0*sv[k][0] ); + } + // Iterate over the non-reduced ndarray dimensions... + for ( i6 = 0; i6 < s6; i6++ ) { + for ( i5 = 0; i5 < s5; i5++ ) { + for ( i4 = 0; i4 < s4; i4++ ) { + for ( i3 = 0; i3 < s3; i3++ ) { + for ( i2 = 0; i2 < s2; i2++ ) { + for ( i1 = 0; i1 < s1; i1++ ) { + for ( i0 = 0; i0 < s0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ j6+i6, j5+i5, j4+i4, j3+i3, j2+i2, j1+i1, j0+i0 ], cdims, clbk, thisArg ); + ybuf[ iv[1] ] = ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + incrementOffsets( iv, dv2 ); + } + incrementOffsets( iv, dv3 ); + } + incrementOffsets( iv, dv4 ); + } + incrementOffsets( iv, dv5 ); + } + incrementOffsets( iv, dv6 ); + } + } + } + } + } + } + } + } +} + + +// EXPORTS // + +module.exports = blockedunary7d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/7d_blocked_accessors.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/7d_blocked_accessors.js new file mode 100644 index 000000000000..13247fed09f7 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/7d_blocked_accessors.js @@ -0,0 +1,367 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params, max-depth, max-statements */ + +'use strict'; + +// MODULES // + +var loopOrder = require( '@stdlib/ndarray/base/unary-loop-interchange-order' ); +var blockSize = require( '@stdlib/ndarray/base/unary-tiling-block-size' ); +var takeIndexed = require( '@stdlib/array/base/take-indexed' ); +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to a callback function and assigns results to a provided output ndarray via loop blocking. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var accessors = require( '@stdlib/array/base/accessors' ); +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 1, 1, 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 1, 1, 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 12, 12, 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 3, 3, 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major', +* 'accessors': accessors( xbuf ).accessors +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major', +* 'accessors': accessors( ybuf ).accessors +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2, 3, 4, 5, 6 ]; +* var cdims = [ 7, 8 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 12, 12, 12, 12, 4 ]; +* +* // Perform a reduction: +* blockedunary7d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ [ [ [ [ 8.0, 16.0, 24.0 ] ] ] ] ] ] ] +*/ +function blockedunary7d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, opts, hasOpts, clbk, thisArg ) { + var bsize; + var ybuf; + var set; + var dv0; + var dv1; + var dv2; + var dv3; + var dv4; + var dv5; + var dv6; + var ov1; + var ov2; + var ov3; + var ov4; + var ov5; + var ov6; + var sh; + var s0; + var s1; + var s2; + var s3; + var s4; + var s5; + var s6; + var sv; + var ov; + var iv; + var i0; + var i1; + var i2; + var i3; + var i4; + var i5; + var i6; + var j0; + var j1; + var j2; + var j3; + var j4; + var j5; + var j6; + var N; + var x; + var y; + var v; + var o; + var k; + var f; + + // Note on variable naming convention: S#, dv#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop... + + N = arrays.length; + x = arrays[ 0 ]; + y = arrays[ 1 ]; + + // Resolve the loop interchange order: + o = loopOrder( y.shape, strides, y.strides ); + sh = o.sh; + sv = [ o.sx, o.sy ]; + for ( k = 2; k < N; k++ ) { + sv.push( takeIndexed( arrays[k].strides, o.idx ) ); + } + // Determine the block size: + bsize = blockSize( x.dtype, y.dtype ); + + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + ov = offsets( arrays ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Cache accessors: + set = y.accessors[ 1 ]; + + // Cache offset increments for the innermost loop... + dv0 = []; + for ( k = 0; k < N; k++ ) { + dv0.push( sv[k][0] ); + } + // Initialize loop variables... + ov1 = zeros( N ); + ov2 = zeros( N ); + ov3 = zeros( N ); + ov4 = zeros( N ); + ov5 = zeros( N ); + ov6 = zeros( N ); + dv1 = zeros( N ); + dv2 = zeros( N ); + dv3 = zeros( N ); + dv4 = zeros( N ); + dv5 = zeros( N ); + dv6 = zeros( N ); + iv = zeros( N ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Iterate over blocks... + for ( j6 = sh[6]; j6 > 0; ) { + if ( j6 < bsize ) { + s6 = j6; + j6 = 0; + } else { + s6 = bsize; + j6 -= bsize; + } + for ( k = 0; k < N; k++ ) { + ov6[ k ] = ov[k] + ( j6*sv[k][6] ); + } + for ( j5 = sh[5]; j5 > 0; ) { + if ( j5 < bsize ) { + s5 = j5; + j5 = 0; + } else { + s5 = bsize; + j5 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv6 = sv[k][6] - ( s5*sv[k][5] ); + ov5[ k ] = ov6[k] + ( j5*sv[k][5] ); + } + for ( j4 = sh[4]; j4 > 0; ) { + if ( j4 < bsize ) { + s4 = j4; + j4 = 0; + } else { + s4 = bsize; + j4 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv5 = sv[k][5] - ( s4*sv[k][4] ); + ov4[ k ] = ov5[k] + ( j4*sv[k][4] ); + } + for ( j3 = sh[3]; j3 > 0; ) { + if ( j3 < bsize ) { + s3 = j3; + j3 = 0; + } else { + s3 = bsize; + j3 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv4[ k ] = sv[k][4] - ( s3*sv[k][3] ); + ov3[ k ] = ov4[k] + ( j3*sv[k][3] ); + } + for ( j2 = sh[2]; j2 > 0; ) { + if ( j2 < bsize ) { + s2 = j2; + j2 = 0; + } else { + s2 = bsize; + j2 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv3[ k ] = sv[k][3] - ( s2*sv[k][2] ); + ov2[ k ] = ov3[k] + ( j2*sv[k][2] ); + } + for ( j1 = sh[1]; j1 > 0; ) { + if ( j1 < bsize ) { + s1 = j1; + j1 = 0; + } else { + s1 = bsize; + j1 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv2[ k ] = sv[k][2] - ( s1*sv[k][1] ); + ov1[ k ] = ov2[k] + ( j1*sv[k][1] ); + } + for ( j0 = sh[0]; j0 > 0; ) { + if ( j0 < bsize ) { + s0 = j0; + j0 = 0; + } else { + s0 = bsize; + j0 -= bsize; + } + // Compute index offsets and loop offset increments for the first ndarray elements in the current block... + for ( k = 0; k < N; k++ ) { + iv[ k ] = ov1[k] + ( j0*sv[k][0] ); + dv1[ k ] = sv[k][1] - ( s0*sv[k][0] ); + } + // Iterate over the non-reduced ndarray dimensions... + for ( i6 = 0; i6 < s6; i6++ ) { + for ( i5 = 0; i5 < s5; i5++ ) { + for ( i4 = 0; i4 < s4; i4++ ) { + for ( i3 = 0; i3 < s3; i3++ ) { + for ( i2 = 0; i2 < s2; i2++ ) { + for ( i1 = 0; i1 < s1; i1++ ) { + for ( i0 = 0; i0 < s0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ j6+i6, j5+i5, j4+i4, j3+i3, j2+i2, j1+i1, j0+i0 ], cdims, clbk, thisArg ); + set( ybuf, iv[1], ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ) ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + incrementOffsets( iv, dv2 ); + } + incrementOffsets( iv, dv3 ); + } + incrementOffsets( iv, dv4 ); + } + incrementOffsets( iv, dv5 ); + } + incrementOffsets( iv, dv6 ); + } + } + } + } + } + } + } + } +} + + +// EXPORTS // + +module.exports = blockedunary7d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/8d.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/8d.js new file mode 100644 index 000000000000..6600c4676df3 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/8d.js @@ -0,0 +1,286 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params, max-depth, max-statements */ + +'use strict'; + +// MODULES // + +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to callback function and assigns results to a provided output ndarray. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {boolean} isRowMajor - boolean indicating whether the input ndarray is row-major +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 1, 1, 1, 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 1, 1, 1, 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 12, 12, 12, 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 3, 3, 3, 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major' +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major' +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2, 3, 4, 5, 6, 7 ]; +* var cdims = [ 8, 9 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 12, 12, 12, 12, 12, 4 ]; +* +* // Perform a reduction: +* unary8d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, true, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ [ [ [ [ [ 8.0, 16.0, 24.0 ] ] ] ] ] ] ] ] +*/ +function unary8d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, isRowMajor, opts, hasOpts, clbk, thisArg ) { + var ybuf; + var dv0; + var dv1; + var dv2; + var dv3; + var dv4; + var dv5; + var dv6; + var dv7; + var sh; + var S0; + var S1; + var S2; + var S3; + var S4; + var S5; + var S6; + var S7; + var sv; + var iv; + var i0; + var i1; + var i2; + var i3; + var i4; + var i5; + var i6; + var i7; + var x; + var y; + var v; + var i; + var f; + + // Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop... + + // Cache a reference to the input ndarray: + x = arrays[ 0 ]; + + // Resolve the output ndarray and associated shape: + y = arrays[ 1 ]; + sh = y.shape; + + // Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments... + if ( isRowMajor ) { + // For row-major ndarrays, the last dimensions have the fastest changing indices... + S0 = sh[ 7 ]; + S1 = sh[ 6 ]; + S2 = sh[ 5 ]; + S3 = sh[ 4 ]; + S4 = sh[ 3 ]; + S5 = sh[ 2 ]; + S6 = sh[ 1 ]; + S7 = sh[ 0 ]; + dv0 = [ strides[7] ]; // offset increment for innermost loop + dv1 = [ strides[6] - ( S0*strides[7] ) ]; + dv2 = [ strides[5] - ( S1*strides[6] ) ]; + dv3 = [ strides[4] - ( S2*strides[5] ) ]; + dv4 = [ strides[3] - ( S3*strides[4] ) ]; + dv5 = [ strides[2] - ( S4*strides[3] ) ]; + dv6 = [ strides[1] - ( S5*strides[2] ) ]; + dv7 = [ strides[0] - ( S6*strides[1] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[7] ); + dv1.push( sv[6] - ( S0*sv[7] ) ); + dv2.push( sv[5] - ( S1*sv[6] ) ); + dv3.push( sv[4] - ( S2*sv[5] ) ); + dv4.push( sv[3] - ( S3*sv[4] ) ); + dv5.push( sv[2] - ( S4*sv[3] ) ); + dv6.push( sv[1] - ( S5*sv[2] ) ); + dv7.push( sv[0] - ( S6*sv[1] ) ); + } + } else { // order === 'column-major' + // For column-major ndarrays, the first dimensions have the fastest changing indices... + S0 = sh[ 0 ]; + S1 = sh[ 1 ]; + S2 = sh[ 2 ]; + S3 = sh[ 3 ]; + S4 = sh[ 4 ]; + S5 = sh[ 5 ]; + S6 = sh[ 6 ]; + S7 = sh[ 7 ]; + dv0 = [ strides[0] ]; // offset increment for innermost loop + dv1 = [ strides[1] - ( S0*strides[0] ) ]; + dv2 = [ strides[2] - ( S1*strides[1] ) ]; + dv3 = [ strides[3] - ( S2*strides[2] ) ]; + dv4 = [ strides[4] - ( S3*strides[3] ) ]; + dv5 = [ strides[5] - ( S4*strides[4] ) ]; + dv6 = [ strides[6] - ( S5*strides[5] ) ]; + dv7 = [ strides[7] - ( S6*strides[6] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[0] ); + dv1.push( sv[1] - ( S0*sv[0] ) ); + dv2.push( sv[2] - ( S1*sv[1] ) ); + dv3.push( sv[3] - ( S2*sv[2] ) ); + dv4.push( sv[4] - ( S3*sv[3] ) ); + dv5.push( sv[5] - ( S4*sv[4] ) ); + dv6.push( sv[6] - ( S5*sv[5] ) ); + dv7.push( sv[7] - ( S6*sv[6] ) ); + } + } + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + iv = offsets( arrays ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Iterate over the non-reduced ndarray dimensions... + for ( i7 = 0; i7 < S7; i7++ ) { + for ( i6 = 0; i6 < S6; i6++ ) { + for ( i5 = 0; i5 < S5; i5++ ) { + for ( i4 = 0; i4 < S4; i4++ ) { + for ( i3 = 0; i3 < S3; i3++ ) { + for ( i2 = 0; i2 < S2; i2++ ) { + for ( i1 = 0; i1 < S1; i1++ ) { + for ( i0 = 0; i0 < S0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ i7, i6, i5, i4, i3, i2, i1, i0 ], cdims, clbk, thisArg ); + ybuf[ iv[1] ] = ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + incrementOffsets( iv, dv2 ); + } + incrementOffsets( iv, dv3 ); + } + incrementOffsets( iv, dv4 ); + } + incrementOffsets( iv, dv5 ); + } + incrementOffsets( iv, dv6 ); + } + incrementOffsets( iv, dv7 ); + } +} + + +// EXPORTS // + +module.exports = unary8d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/8d_accessors.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/8d_accessors.js new file mode 100644 index 000000000000..c448bc85c820 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/8d_accessors.js @@ -0,0 +1,294 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params, max-depth, max-statements */ + +'use strict'; + +// MODULES // + +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to callback function and assigns results to a provided output ndarray. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {boolean} isRowMajor - boolean indicating whether the input ndarray is row-major +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var accessors = require( '@stdlib/array/base/accessors' ); +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* var xbuf = toAccessorArray( 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 ] ) ); +* var ybuf = toAccessorArray( new Float64Array( [ 0.0, 0.0, 0.0 ] ) ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 1, 1, 1, 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 1, 1, 1, 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 12, 12, 12, 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 3, 3, 3, 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major', +* 'accessors': accessors( xbuf ).accessors +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major', +* 'accessors': accessors( ybuf ).accessors +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2, 3, 4, 5, 6, 7 ]; +* var cdims = [ 8, 9 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 12, 12, 12, 12, 12, 4 ]; +* +* // Perform a reduction: +* unary8d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, true, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ [ [ [ [ [ 8.0, 16.0, 24.0 ] ] ] ] ] ] ] ] +*/ +function unary8d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, isRowMajor, opts, hasOpts, clbk, thisArg ) { + var ybuf; + var set; + var dv0; + var dv1; + var dv2; + var dv3; + var dv4; + var dv5; + var dv6; + var dv7; + var sh; + var S0; + var S1; + var S2; + var S3; + var S4; + var S5; + var S6; + var S7; + var sv; + var iv; + var i0; + var i1; + var i2; + var i3; + var i4; + var i5; + var i6; + var i7; + var x; + var y; + var v; + var i; + var f; + + // Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop... + + // Cache a reference to the input ndarray: + x = arrays[ 0 ]; + + // Resolve the output ndarray and associated shape: + y = arrays[ 1 ]; + sh = y.shape; + + // Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments... + if ( isRowMajor ) { + // For row-major ndarrays, the last dimensions have the fastest changing indices... + S0 = sh[ 7 ]; + S1 = sh[ 6 ]; + S2 = sh[ 5 ]; + S3 = sh[ 4 ]; + S4 = sh[ 3 ]; + S5 = sh[ 2 ]; + S6 = sh[ 1 ]; + S7 = sh[ 0 ]; + dv0 = [ strides[7] ]; // offset increment for innermost loop + dv1 = [ strides[6] - ( S0*strides[7] ) ]; + dv2 = [ strides[5] - ( S1*strides[6] ) ]; + dv3 = [ strides[4] - ( S2*strides[5] ) ]; + dv4 = [ strides[3] - ( S3*strides[4] ) ]; + dv5 = [ strides[2] - ( S4*strides[3] ) ]; + dv6 = [ strides[1] - ( S5*strides[2] ) ]; + dv7 = [ strides[0] - ( S6*strides[1] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[7] ); + dv1.push( sv[6] - ( S0*sv[7] ) ); + dv2.push( sv[5] - ( S1*sv[6] ) ); + dv3.push( sv[4] - ( S2*sv[5] ) ); + dv4.push( sv[3] - ( S3*sv[4] ) ); + dv5.push( sv[2] - ( S4*sv[3] ) ); + dv6.push( sv[1] - ( S5*sv[2] ) ); + dv7.push( sv[0] - ( S6*sv[1] ) ); + } + } else { // order === 'column-major' + // For column-major ndarrays, the first dimensions have the fastest changing indices... + S0 = sh[ 0 ]; + S1 = sh[ 1 ]; + S2 = sh[ 2 ]; + S3 = sh[ 3 ]; + S4 = sh[ 4 ]; + S5 = sh[ 5 ]; + S6 = sh[ 6 ]; + S7 = sh[ 7 ]; + dv0 = [ strides[0] ]; // offset increment for innermost loop + dv1 = [ strides[1] - ( S0*strides[0] ) ]; + dv2 = [ strides[2] - ( S1*strides[1] ) ]; + dv3 = [ strides[3] - ( S2*strides[2] ) ]; + dv4 = [ strides[4] - ( S3*strides[3] ) ]; + dv5 = [ strides[5] - ( S4*strides[4] ) ]; + dv6 = [ strides[6] - ( S5*strides[5] ) ]; + dv7 = [ strides[7] - ( S6*strides[6] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[0] ); + dv1.push( sv[1] - ( S0*sv[0] ) ); + dv2.push( sv[2] - ( S1*sv[1] ) ); + dv3.push( sv[3] - ( S2*sv[2] ) ); + dv4.push( sv[4] - ( S3*sv[3] ) ); + dv5.push( sv[5] - ( S4*sv[4] ) ); + dv6.push( sv[6] - ( S5*sv[5] ) ); + dv7.push( sv[7] - ( S6*sv[6] ) ); + } + } + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + iv = offsets( arrays ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Cache accessors: + set = y.accessors[ 1 ]; + + // Iterate over the non-reduced ndarray dimensions... + for ( i7 = 0; i7 < S7; i7++ ) { + for ( i6 = 0; i6 < S6; i6++ ) { + for ( i5 = 0; i5 < S5; i5++ ) { + for ( i4 = 0; i4 < S4; i4++ ) { + for ( i3 = 0; i3 < S3; i3++ ) { + for ( i2 = 0; i2 < S2; i2++ ) { + for ( i1 = 0; i1 < S1; i1++ ) { + for ( i0 = 0; i0 < S0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ i7, i6, i5, i4, i3, i2, i1, i0 ], cdims, clbk, thisArg ); + set( ybuf, iv[1], ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ) ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + incrementOffsets( iv, dv2 ); + } + incrementOffsets( iv, dv3 ); + } + incrementOffsets( iv, dv4 ); + } + incrementOffsets( iv, dv5 ); + } + incrementOffsets( iv, dv6 ); + } + incrementOffsets( iv, dv7 ); + } +} + + +// EXPORTS // + +module.exports = unary8d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/8d_blocked.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/8d_blocked.js new file mode 100644 index 000000000000..88bb5d74c850 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/8d_blocked.js @@ -0,0 +1,382 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params, max-depth, max-statements, max-lines-per-function */ + +'use strict'; + +// MODULES // + +var loopOrder = require( '@stdlib/ndarray/base/unary-loop-interchange-order' ); +var blockSize = require( '@stdlib/ndarray/base/unary-tiling-block-size' ); +var takeIndexed = require( '@stdlib/array/base/take-indexed' ); +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to a callback function and assigns results to a provided output ndarray via loop blocking. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 1, 1, 1, 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 1, 1, 1, 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 12, 12, 12, 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 3, 3, 3, 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major' +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major' +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2, 3, 4, 5, 6, 7 ]; +* var cdims = [ 8, 9 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 12, 12, 12, 12, 12, 4 ]; +* +* // Perform a reduction: +* blockedunary8d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ [ [ [ [ [ 8.0, 16.0, 24.0 ] ] ] ] ] ] ] ] +*/ +function blockedunary8d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, opts, hasOpts, clbk, thisArg ) { + var bsize; + var ybuf; + var dv0; + var dv1; + var dv2; + var dv3; + var dv4; + var dv5; + var dv6; + var dv7; + var ov1; + var ov2; + var ov3; + var ov4; + var ov5; + var ov6; + var ov7; + var sh; + var s0; + var s1; + var s2; + var s3; + var s4; + var s5; + var s6; + var s7; + var sv; + var ov; + var iv; + var i0; + var i1; + var i2; + var i3; + var i4; + var i5; + var i6; + var i7; + var j0; + var j1; + var j2; + var j3; + var j4; + var j5; + var j6; + var j7; + var N; + var x; + var y; + var v; + var o; + var k; + var f; + + // Note on variable naming convention: S#, dv#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop... + + N = arrays.length; + x = arrays[ 0 ]; + y = arrays[ 1 ]; + + // Resolve the loop interchange order: + o = loopOrder( y.shape, strides, y.strides ); + sh = o.sh; + sv = [ o.sx, o.sy ]; + for ( k = 2; k < N; k++ ) { + sv.push( takeIndexed( arrays[k].strides, o.idx ) ); + } + // Determine the block size: + bsize = blockSize( x.dtype, y.dtype ); + + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + ov = offsets( arrays ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Cache offset increments for the innermost loop... + dv0 = []; + for ( k = 0; k < N; k++ ) { + dv0.push( sv[k][0] ); + } + // Initialize loop variables... + ov1 = zeros( N ); + ov2 = zeros( N ); + ov3 = zeros( N ); + ov4 = zeros( N ); + ov5 = zeros( N ); + ov6 = zeros( N ); + ov7 = zeros( N ); + dv1 = zeros( N ); + dv2 = zeros( N ); + dv3 = zeros( N ); + dv4 = zeros( N ); + dv5 = zeros( N ); + dv6 = zeros( N ); + dv7 = zeros( N ); + iv = zeros( N ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Iterate over blocks... + for ( j7 = sh[7]; j7 > 0; ) { + if ( j7 < bsize ) { + s7 = j7; + j7 = 0; + } else { + s7 = bsize; + j7 -= bsize; + } + for ( k = 0; k < N; k++ ) { + ov7[ k ] = ov[k] + ( j7*sv[k][7] ); + } + for ( j6 = sh[6]; j6 > 0; ) { + if ( j6 < bsize ) { + s6 = j6; + j6 = 0; + } else { + s6 = bsize; + j6 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv7 = sv[k][7] - ( s6*sv[k][6] ); + ov6[ k ] = ov7[k] + ( j6*sv[k][6] ); + } + for ( j5 = sh[5]; j5 > 0; ) { + if ( j5 < bsize ) { + s5 = j5; + j5 = 0; + } else { + s5 = bsize; + j5 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv6 = sv[k][6] - ( s5*sv[k][5] ); + ov5[ k ] = ov6[k] + ( j5*sv[k][5] ); + } + for ( j4 = sh[4]; j4 > 0; ) { + if ( j4 < bsize ) { + s4 = j4; + j4 = 0; + } else { + s4 = bsize; + j4 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv5 = sv[k][5] - ( s4*sv[k][4] ); + ov4[ k ] = ov5[k] + ( j4*sv[k][4] ); + } + for ( j3 = sh[3]; j3 > 0; ) { + if ( j3 < bsize ) { + s3 = j3; + j3 = 0; + } else { + s3 = bsize; + j3 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv4[ k ] = sv[k][4] - ( s3*sv[k][3] ); + ov3[ k ] = ov4[k] + ( j3*sv[k][3] ); + } + for ( j2 = sh[2]; j2 > 0; ) { + if ( j2 < bsize ) { + s2 = j2; + j2 = 0; + } else { + s2 = bsize; + j2 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv3[ k ] = sv[k][3] - ( s2*sv[k][2] ); + ov2[ k ] = ov3[k] + ( j2*sv[k][2] ); + } + for ( j1 = sh[1]; j1 > 0; ) { + if ( j1 < bsize ) { + s1 = j1; + j1 = 0; + } else { + s1 = bsize; + j1 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv2[ k ] = sv[k][2] - ( s1*sv[k][1] ); + ov1[ k ] = ov2[k] + ( j1*sv[k][1] ); + } + for ( j0 = sh[0]; j0 > 0; ) { + if ( j0 < bsize ) { + s0 = j0; + j0 = 0; + } else { + s0 = bsize; + j0 -= bsize; + } + // Compute index offsets and loop offset increments for the first ndarray elements in the current block... + for ( k = 0; k < N; k++ ) { + iv[ k ] = ov1[k] + ( j0*sv[k][0] ); + dv1[ k ] = sv[k][1] - ( s0*sv[k][0] ); + } + // Iterate over the non-reduced ndarray dimensions... + for ( i7 = 0; i7 < s7; i7++ ) { + for ( i6 = 0; i6 < s6; i6++ ) { + for ( i5 = 0; i5 < s5; i5++ ) { + for ( i4 = 0; i4 < s4; i4++ ) { + for ( i3 = 0; i3 < s3; i3++ ) { + for ( i2 = 0; i2 < s2; i2++ ) { + for ( i1 = 0; i1 < s1; i1++ ) { + for ( i0 = 0; i0 < s0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ j7+i7, j6+i6, j5+i5, j4+i4, j3+i3, j2+i2, j1+i1, j0+i0 ], cdims, clbk, thisArg ); + ybuf[ iv[1] ] = ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + incrementOffsets( iv, dv2 ); + } + incrementOffsets( iv, dv3 ); + } + incrementOffsets( iv, dv4 ); + } + incrementOffsets( iv, dv5 ); + } + incrementOffsets( iv, dv6 ); + } + incrementOffsets( iv, dv7 ); + } + } + } + } + } + } + } + } + } +} + + +// EXPORTS // + +module.exports = blockedunary8d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/8d_blocked_accessors.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/8d_blocked_accessors.js new file mode 100644 index 000000000000..6b506d41ac62 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/8d_blocked_accessors.js @@ -0,0 +1,390 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params, max-depth, max-statements, max-lines-per-function */ + +'use strict'; + +// MODULES // + +var loopOrder = require( '@stdlib/ndarray/base/unary-loop-interchange-order' ); +var blockSize = require( '@stdlib/ndarray/base/unary-tiling-block-size' ); +var takeIndexed = require( '@stdlib/array/base/take-indexed' ); +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to a callback function and assigns results to a provided output ndarray via loop blocking. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var accessors = require( '@stdlib/array/base/accessors' ); +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 1, 1, 1, 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 1, 1, 1, 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 12, 12, 12, 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 3, 3, 3, 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major', +* 'accessors': accessors( xbuf ).accessors +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major', +* 'accessors': accessors( ybuf ).accessors +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2, 3, 4, 5, 6, 7 ]; +* var cdims = [ 8, 9 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 12, 12, 12, 12, 12, 4 ]; +* +* // Perform a reduction: +* blockedunary8d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ [ [ [ [ [ 8.0, 16.0, 24.0 ] ] ] ] ] ] ] ] +*/ +function blockedunary8d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, opts, hasOpts, clbk, thisArg ) { + var bsize; + var ybuf; + var set; + var dv0; + var dv1; + var dv2; + var dv3; + var dv4; + var dv5; + var dv6; + var dv7; + var ov1; + var ov2; + var ov3; + var ov4; + var ov5; + var ov6; + var ov7; + var sh; + var s0; + var s1; + var s2; + var s3; + var s4; + var s5; + var s6; + var s7; + var sv; + var ov; + var iv; + var i0; + var i1; + var i2; + var i3; + var i4; + var i5; + var i6; + var i7; + var j0; + var j1; + var j2; + var j3; + var j4; + var j5; + var j6; + var j7; + var N; + var x; + var y; + var v; + var o; + var k; + var f; + + // Note on variable naming convention: S#, dv#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop... + + N = arrays.length; + x = arrays[ 0 ]; + y = arrays[ 1 ]; + + // Resolve the loop interchange order: + o = loopOrder( y.shape, strides, y.strides ); + sh = o.sh; + sv = [ o.sx, o.sy ]; + for ( k = 2; k < N; k++ ) { + sv.push( takeIndexed( arrays[k].strides, o.idx ) ); + } + // Determine the block size: + bsize = blockSize( x.dtype, y.dtype ); + + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + ov = offsets( arrays ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Cache accessors: + set = y.accessors[ 1 ]; + + // Cache offset increments for the innermost loop... + dv0 = []; + for ( k = 0; k < N; k++ ) { + dv0.push( sv[k][0] ); + } + // Initialize loop variables... + ov1 = zeros( N ); + ov2 = zeros( N ); + ov3 = zeros( N ); + ov4 = zeros( N ); + ov5 = zeros( N ); + ov6 = zeros( N ); + ov7 = zeros( N ); + dv1 = zeros( N ); + dv2 = zeros( N ); + dv3 = zeros( N ); + dv4 = zeros( N ); + dv5 = zeros( N ); + dv6 = zeros( N ); + dv7 = zeros( N ); + iv = zeros( N ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Iterate over blocks... + for ( j7 = sh[7]; j7 > 0; ) { + if ( j7 < bsize ) { + s7 = j7; + j7 = 0; + } else { + s7 = bsize; + j7 -= bsize; + } + for ( k = 0; k < N; k++ ) { + ov7[ k ] = ov[k] + ( j7*sv[k][7] ); + } + for ( j6 = sh[6]; j6 > 0; ) { + if ( j6 < bsize ) { + s6 = j6; + j6 = 0; + } else { + s6 = bsize; + j6 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv7 = sv[k][7] - ( s6*sv[k][6] ); + ov6[ k ] = ov7[k] + ( j6*sv[k][6] ); + } + for ( j5 = sh[5]; j5 > 0; ) { + if ( j5 < bsize ) { + s5 = j5; + j5 = 0; + } else { + s5 = bsize; + j5 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv6 = sv[k][6] - ( s5*sv[k][5] ); + ov5[ k ] = ov6[k] + ( j5*sv[k][5] ); + } + for ( j4 = sh[4]; j4 > 0; ) { + if ( j4 < bsize ) { + s4 = j4; + j4 = 0; + } else { + s4 = bsize; + j4 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv5 = sv[k][5] - ( s4*sv[k][4] ); + ov4[ k ] = ov5[k] + ( j4*sv[k][4] ); + } + for ( j3 = sh[3]; j3 > 0; ) { + if ( j3 < bsize ) { + s3 = j3; + j3 = 0; + } else { + s3 = bsize; + j3 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv4[ k ] = sv[k][4] - ( s3*sv[k][3] ); + ov3[ k ] = ov4[k] + ( j3*sv[k][3] ); + } + for ( j2 = sh[2]; j2 > 0; ) { + if ( j2 < bsize ) { + s2 = j2; + j2 = 0; + } else { + s2 = bsize; + j2 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv3[ k ] = sv[k][3] - ( s2*sv[k][2] ); + ov2[ k ] = ov3[k] + ( j2*sv[k][2] ); + } + for ( j1 = sh[1]; j1 > 0; ) { + if ( j1 < bsize ) { + s1 = j1; + j1 = 0; + } else { + s1 = bsize; + j1 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv2[ k ] = sv[k][2] - ( s1*sv[k][1] ); + ov1[ k ] = ov2[k] + ( j1*sv[k][1] ); + } + for ( j0 = sh[0]; j0 > 0; ) { + if ( j0 < bsize ) { + s0 = j0; + j0 = 0; + } else { + s0 = bsize; + j0 -= bsize; + } + // Compute index offsets and loop offset increments for the first ndarray elements in the current block... + for ( k = 0; k < N; k++ ) { + iv[ k ] = ov1[k] + ( j0*sv[k][0] ); + dv1[ k ] = sv[k][1] - ( s0*sv[k][0] ); + } + // Iterate over the non-reduced ndarray dimensions... + for ( i7 = 0; i7 < s7; i7++ ) { + for ( i6 = 0; i6 < s6; i6++ ) { + for ( i5 = 0; i5 < s5; i5++ ) { + for ( i4 = 0; i4 < s4; i4++ ) { + for ( i3 = 0; i3 < s3; i3++ ) { + for ( i2 = 0; i2 < s2; i2++ ) { + for ( i1 = 0; i1 < s1; i1++ ) { + for ( i0 = 0; i0 < s0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ j7+i7, j6+i6, j5+i5, j4+i4, j3+i3, j2+i2, j1+i1, j0+i0 ], cdims, clbk, thisArg ); + set( ybuf, iv[1], ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ) ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + incrementOffsets( iv, dv2 ); + } + incrementOffsets( iv, dv3 ); + } + incrementOffsets( iv, dv4 ); + } + incrementOffsets( iv, dv5 ); + } + incrementOffsets( iv, dv6 ); + } + incrementOffsets( iv, dv7 ); + } + } + } + } + } + } + } + } + } +} + + +// EXPORTS // + +module.exports = blockedunary8d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/9d.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/9d.js new file mode 100644 index 000000000000..90a4d09901a2 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/9d.js @@ -0,0 +1,298 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params, max-depth, max-statements */ + +'use strict'; + +// MODULES // + +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to callback function and assigns results to a provided output ndarray. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {boolean} isRowMajor - boolean indicating whether the input ndarray is row-major +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 1, 1, 1, 1, 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 1, 1, 1, 1, 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 12, 12, 12, 12, 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 3, 3, 3, 3, 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major' +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major' +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]; +* var cdims = [ 9, 10 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 12, 12, 12, 12, 12, 12, 4 ]; +* +* // Perform a reduction: +* unary9d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, true, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ [ [ [ [ [ [ 8.0, 16.0, 24.0 ] ] ] ] ] ] ] ] ] +*/ +function unary9d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, isRowMajor, opts, hasOpts, clbk, thisArg ) { + var ybuf; + var dv0; + var dv1; + var dv2; + var dv3; + var dv4; + var dv5; + var dv6; + var dv7; + var dv8; + var sh; + var S0; + var S1; + var S2; + var S3; + var S4; + var S5; + var S6; + var S7; + var S8; + var sv; + var iv; + var i0; + var i1; + var i2; + var i3; + var i4; + var i5; + var i6; + var i7; + var i8; + var x; + var y; + var v; + var i; + var f; + + // Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop... + + // Cache a reference to the input ndarray: + x = arrays[ 0 ]; + + // Resolve the output ndarray and associated shape: + y = arrays[ 1 ]; + sh = y.shape; + + // Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments... + if ( isRowMajor ) { + // For row-major ndarrays, the last dimensions have the fastest changing indices... + S0 = sh[ 8 ]; + S1 = sh[ 7 ]; + S2 = sh[ 6 ]; + S3 = sh[ 5 ]; + S4 = sh[ 4 ]; + S5 = sh[ 3 ]; + S6 = sh[ 2 ]; + S7 = sh[ 1 ]; + S8 = sh[ 0 ]; + dv0 = [ strides[8] ]; // offset increment for innermost loop + dv1 = [ strides[7] - ( S0*strides[8] ) ]; + dv2 = [ strides[6] - ( S1*strides[7] ) ]; + dv3 = [ strides[5] - ( S2*strides[6] ) ]; + dv4 = [ strides[4] - ( S3*strides[5] ) ]; + dv5 = [ strides[3] - ( S4*strides[4] ) ]; + dv6 = [ strides[2] - ( S5*strides[3] ) ]; + dv7 = [ strides[1] - ( S6*strides[2] ) ]; + dv8 = [ strides[0] - ( S7*strides[1] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[8] ); + dv1.push( sv[7] - ( S0*sv[8] ) ); + dv2.push( sv[6] - ( S1*sv[7] ) ); + dv3.push( sv[5] - ( S2*sv[6] ) ); + dv4.push( sv[4] - ( S3*sv[5] ) ); + dv5.push( sv[3] - ( S4*sv[4] ) ); + dv6.push( sv[2] - ( S5*sv[3] ) ); + dv7.push( sv[1] - ( S6*sv[2] ) ); + dv8.push( sv[0] - ( S7*sv[1] ) ); + } + } else { // order === 'column-major' + // For column-major ndarrays, the first dimensions have the fastest changing indices... + S0 = sh[ 0 ]; + S1 = sh[ 1 ]; + S2 = sh[ 2 ]; + S3 = sh[ 3 ]; + S4 = sh[ 4 ]; + S5 = sh[ 5 ]; + S6 = sh[ 6 ]; + S7 = sh[ 7 ]; + S8 = sh[ 8 ]; + dv0 = [ strides[0] ]; // offset increment for innermost loop + dv1 = [ strides[1] - ( S0*strides[0] ) ]; + dv2 = [ strides[2] - ( S1*strides[1] ) ]; + dv3 = [ strides[3] - ( S2*strides[2] ) ]; + dv4 = [ strides[4] - ( S3*strides[3] ) ]; + dv5 = [ strides[5] - ( S4*strides[4] ) ]; + dv6 = [ strides[6] - ( S5*strides[5] ) ]; + dv7 = [ strides[7] - ( S6*strides[6] ) ]; + dv8 = [ strides[8] - ( S7*strides[7] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[0] ); + dv1.push( sv[1] - ( S0*sv[0] ) ); + dv2.push( sv[2] - ( S1*sv[1] ) ); + dv3.push( sv[3] - ( S2*sv[2] ) ); + dv4.push( sv[4] - ( S3*sv[3] ) ); + dv5.push( sv[5] - ( S4*sv[4] ) ); + dv6.push( sv[6] - ( S5*sv[5] ) ); + dv7.push( sv[7] - ( S6*sv[6] ) ); + dv8.push( sv[8] - ( S7*sv[7] ) ); + } + } + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + iv = offsets( arrays ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Iterate over the non-reduced ndarray dimensions... + for ( i8 = 0; i8 < S8; i8++ ) { + for ( i7 = 0; i7 < S7; i7++ ) { + for ( i6 = 0; i6 < S6; i6++ ) { + for ( i5 = 0; i5 < S5; i5++ ) { + for ( i4 = 0; i4 < S4; i4++ ) { + for ( i3 = 0; i3 < S3; i3++ ) { + for ( i2 = 0; i2 < S2; i2++ ) { + for ( i1 = 0; i1 < S1; i1++ ) { + for ( i0 = 0; i0 < S0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ i8, i7, i6, i5, i4, i3, i2, i1, i0 ], cdims, clbk, thisArg ); + ybuf[ iv[1] ] = ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + incrementOffsets( iv, dv2 ); + } + incrementOffsets( iv, dv3 ); + } + incrementOffsets( iv, dv4 ); + } + incrementOffsets( iv, dv5 ); + } + incrementOffsets( iv, dv6 ); + } + incrementOffsets( iv, dv7 ); + } + incrementOffsets( iv, dv8 ); + } +} + + +// EXPORTS // + +module.exports = unary9d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/9d_accessors.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/9d_accessors.js new file mode 100644 index 000000000000..d295f439b96b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/9d_accessors.js @@ -0,0 +1,306 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params, max-depth, max-statements */ + +'use strict'; + +// MODULES // + +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to callback function and assigns results to a provided output ndarray. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {boolean} isRowMajor - boolean indicating whether the input ndarray is row-major +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var accessors = require( '@stdlib/array/base/accessors' ); +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* var xbuf = toAccessorArray( 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 ] ) ); +* var ybuf = toAccessorArray( new Float64Array( [ 0.0, 0.0, 0.0 ] ) ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 1, 1, 1, 1, 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 1, 1, 1, 1, 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 12, 12, 12, 12, 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 3, 3, 3, 3, 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major', +* 'accessors': accessors( xbuf ).accessors +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major', +* 'accessors': accessors( ybuf ).accessors +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]; +* var cdims = [ 9, 10 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 12, 12, 12, 12, 12, 12, 4 ]; +* +* // Perform a reduction: +* unary9d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, true, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ [ [ [ [ [ [ 8.0, 16.0, 24.0 ] ] ] ] ] ] ] ] ] +*/ +function unary9d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, isRowMajor, opts, hasOpts, clbk, thisArg ) { + var ybuf; + var set; + var dv0; + var dv1; + var dv2; + var dv3; + var dv4; + var dv5; + var dv6; + var dv7; + var dv8; + var sh; + var S0; + var S1; + var S2; + var S3; + var S4; + var S5; + var S6; + var S7; + var S8; + var sv; + var iv; + var i0; + var i1; + var i2; + var i3; + var i4; + var i5; + var i6; + var i7; + var i8; + var x; + var y; + var v; + var i; + var f; + + // Note on variable naming convention: S#, dv#, i# where # corresponds to the loop number, with `0` being the innermost loop... + + // Cache a reference to the input ndarray: + x = arrays[ 0 ]; + + // Resolve the output ndarray and associated shape: + y = arrays[ 1 ]; + sh = y.shape; + + // Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments... + if ( isRowMajor ) { + // For row-major ndarrays, the last dimensions have the fastest changing indices... + S0 = sh[ 8 ]; + S1 = sh[ 7 ]; + S2 = sh[ 6 ]; + S3 = sh[ 5 ]; + S4 = sh[ 4 ]; + S5 = sh[ 3 ]; + S6 = sh[ 2 ]; + S7 = sh[ 1 ]; + S8 = sh[ 0 ]; + dv0 = [ strides[8] ]; // offset increment for innermost loop + dv1 = [ strides[7] - ( S0*strides[8] ) ]; + dv2 = [ strides[6] - ( S1*strides[7] ) ]; + dv3 = [ strides[5] - ( S2*strides[6] ) ]; + dv4 = [ strides[4] - ( S3*strides[5] ) ]; + dv5 = [ strides[3] - ( S4*strides[4] ) ]; + dv6 = [ strides[2] - ( S5*strides[3] ) ]; + dv7 = [ strides[1] - ( S6*strides[2] ) ]; + dv8 = [ strides[0] - ( S7*strides[1] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[8] ); + dv1.push( sv[7] - ( S0*sv[8] ) ); + dv2.push( sv[6] - ( S1*sv[7] ) ); + dv3.push( sv[5] - ( S2*sv[6] ) ); + dv4.push( sv[4] - ( S3*sv[5] ) ); + dv5.push( sv[3] - ( S4*sv[4] ) ); + dv6.push( sv[2] - ( S5*sv[3] ) ); + dv7.push( sv[1] - ( S6*sv[2] ) ); + dv8.push( sv[0] - ( S7*sv[1] ) ); + } + } else { // order === 'column-major' + // For column-major ndarrays, the first dimensions have the fastest changing indices... + S0 = sh[ 0 ]; + S1 = sh[ 1 ]; + S2 = sh[ 2 ]; + S3 = sh[ 3 ]; + S4 = sh[ 4 ]; + S5 = sh[ 5 ]; + S6 = sh[ 6 ]; + S7 = sh[ 7 ]; + S8 = sh[ 8 ]; + dv0 = [ strides[0] ]; // offset increment for innermost loop + dv1 = [ strides[1] - ( S0*strides[0] ) ]; + dv2 = [ strides[2] - ( S1*strides[1] ) ]; + dv3 = [ strides[3] - ( S2*strides[2] ) ]; + dv4 = [ strides[4] - ( S3*strides[3] ) ]; + dv5 = [ strides[5] - ( S4*strides[4] ) ]; + dv6 = [ strides[6] - ( S5*strides[5] ) ]; + dv7 = [ strides[7] - ( S6*strides[6] ) ]; + dv8 = [ strides[8] - ( S7*strides[7] ) ]; // offset increment for outermost loop + for ( i = 1; i < arrays.length; i++ ) { + sv = arrays[ i ].strides; + dv0.push( sv[0] ); + dv1.push( sv[1] - ( S0*sv[0] ) ); + dv2.push( sv[2] - ( S1*sv[1] ) ); + dv3.push( sv[3] - ( S2*sv[2] ) ); + dv4.push( sv[4] - ( S3*sv[3] ) ); + dv5.push( sv[5] - ( S4*sv[4] ) ); + dv6.push( sv[6] - ( S5*sv[5] ) ); + dv7.push( sv[7] - ( S6*sv[6] ) ); + dv8.push( sv[8] - ( S7*sv[7] ) ); + } + } + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + iv = offsets( arrays ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Cache accessors: + set = y.accessors[ 1 ]; + + // Iterate over the non-reduced ndarray dimensions... + for ( i8 = 0; i8 < S8; i8++ ) { + for ( i7 = 0; i7 < S7; i7++ ) { + for ( i6 = 0; i6 < S6; i6++ ) { + for ( i5 = 0; i5 < S5; i5++ ) { + for ( i4 = 0; i4 < S4; i4++ ) { + for ( i3 = 0; i3 < S3; i3++ ) { + for ( i2 = 0; i2 < S2; i2++ ) { + for ( i1 = 0; i1 < S1; i1++ ) { + for ( i0 = 0; i0 < S0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ i8, i7, i6, i5, i4, i3, i2, i1, i0 ], cdims, clbk, thisArg ); + set( ybuf, iv[1], ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ) ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + incrementOffsets( iv, dv2 ); + } + incrementOffsets( iv, dv3 ); + } + incrementOffsets( iv, dv4 ); + } + incrementOffsets( iv, dv5 ); + } + incrementOffsets( iv, dv6 ); + } + incrementOffsets( iv, dv7 ); + } + incrementOffsets( iv, dv8 ); + } +} + + +// EXPORTS // + +module.exports = unary9d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/9d_blocked.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/9d_blocked.js new file mode 100644 index 000000000000..3721c25855d1 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/9d_blocked.js @@ -0,0 +1,405 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params, max-depth, max-statements, max-lines-per-function */ + +'use strict'; + +// MODULES // + +var loopOrder = require( '@stdlib/ndarray/base/unary-loop-interchange-order' ); +var blockSize = require( '@stdlib/ndarray/base/unary-tiling-block-size' ); +var takeIndexed = require( '@stdlib/array/base/take-indexed' ); +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to a callback function and assigns results to a provided output ndarray via loop blocking. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 1, 1, 1, 1, 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 1, 1, 1, 1, 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 12, 12, 12, 12, 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 3, 3, 3, 3, 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major' +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major' +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]; +* var cdims = [ 9, 10 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 12, 12, 12, 12, 12, 12, 4 ]; +* +* // Perform a reduction: +* blockedunary9d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ [ [ [ [ [ [ 8.0, 16.0, 24.0 ] ] ] ] ] ] ] ] ] +*/ +function blockedunary9d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, opts, hasOpts, clbk, thisArg ) { + var bsize; + var ybuf; + var dv0; + var dv1; + var dv2; + var dv3; + var dv4; + var dv5; + var dv6; + var dv7; + var dv8; + var ov1; + var ov2; + var ov3; + var ov4; + var ov5; + var ov6; + var ov7; + var ov8; + var sh; + var s0; + var s1; + var s2; + var s3; + var s4; + var s5; + var s6; + var s7; + var s8; + var sv; + var ov; + var iv; + var i0; + var i1; + var i2; + var i3; + var i4; + var i5; + var i6; + var i7; + var i8; + var j0; + var j1; + var j2; + var j3; + var j4; + var j5; + var j6; + var j7; + var j8; + var N; + var x; + var y; + var v; + var o; + var k; + var f; + + // Note on variable naming convention: S#, dv#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop... + + N = arrays.length; + x = arrays[ 0 ]; + y = arrays[ 1 ]; + + // Resolve the loop interchange order: + o = loopOrder( y.shape, strides, y.strides ); + sh = o.sh; + sv = [ o.sx, o.sy ]; + for ( k = 2; k < N; k++ ) { + sv.push( takeIndexed( arrays[k].strides, o.idx ) ); + } + // Determine the block size: + bsize = blockSize( x.dtype, y.dtype ); + + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + ov = offsets( arrays ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Cache offset increments for the innermost loop... + dv0 = []; + for ( k = 0; k < N; k++ ) { + dv0.push( sv[k][0] ); + } + // Initialize loop variables... + ov1 = zeros( N ); + ov2 = zeros( N ); + ov3 = zeros( N ); + ov4 = zeros( N ); + ov5 = zeros( N ); + ov6 = zeros( N ); + ov7 = zeros( N ); + ov8 = zeros( N ); + dv1 = zeros( N ); + dv2 = zeros( N ); + dv3 = zeros( N ); + dv4 = zeros( N ); + dv5 = zeros( N ); + dv6 = zeros( N ); + dv7 = zeros( N ); + dv8 = zeros( N ); + iv = zeros( N ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Iterate over blocks... + for ( j8 = sh[8]; j8 > 0; ) { + if ( j8 < bsize ) { + s8 = j8; + j8 = 0; + } else { + s8 = bsize; + j8 -= bsize; + } + for ( k = 0; k < N; k++ ) { + ov8[ k ] = ov[k] + ( j8*sv[k][8] ); + } + for ( j7 = sh[7]; j7 > 0; ) { + if ( j7 < bsize ) { + s7 = j7; + j7 = 0; + } else { + s7 = bsize; + j7 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv8 = sv[k][8] - ( s7*sv[k][7] ); + ov7[ k ] = ov8[k] + ( j7*sv[k][7] ); + } + for ( j6 = sh[6]; j6 > 0; ) { + if ( j6 < bsize ) { + s6 = j6; + j6 = 0; + } else { + s6 = bsize; + j6 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv7 = sv[k][7] - ( s6*sv[k][6] ); + ov6[ k ] = ov7[k] + ( j6*sv[k][6] ); + } + for ( j5 = sh[5]; j5 > 0; ) { + if ( j5 < bsize ) { + s5 = j5; + j5 = 0; + } else { + s5 = bsize; + j5 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv6 = sv[k][6] - ( s5*sv[k][5] ); + ov5[ k ] = ov6[k] + ( j5*sv[k][5] ); + } + for ( j4 = sh[4]; j4 > 0; ) { + if ( j4 < bsize ) { + s4 = j4; + j4 = 0; + } else { + s4 = bsize; + j4 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv5 = sv[k][5] - ( s4*sv[k][4] ); + ov4[ k ] = ov5[k] + ( j4*sv[k][4] ); + } + for ( j3 = sh[3]; j3 > 0; ) { + if ( j3 < bsize ) { + s3 = j3; + j3 = 0; + } else { + s3 = bsize; + j3 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv4[ k ] = sv[k][4] - ( s3*sv[k][3] ); + ov3[ k ] = ov4[k] + ( j3*sv[k][3] ); + } + for ( j2 = sh[2]; j2 > 0; ) { + if ( j2 < bsize ) { + s2 = j2; + j2 = 0; + } else { + s2 = bsize; + j2 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv3[ k ] = sv[k][3] - ( s2*sv[k][2] ); + ov2[ k ] = ov3[k] + ( j2*sv[k][2] ); + } + for ( j1 = sh[1]; j1 > 0; ) { + if ( j1 < bsize ) { + s1 = j1; + j1 = 0; + } else { + s1 = bsize; + j1 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv2[ k ] = sv[k][2] - ( s1*sv[k][1] ); + ov1[ k ] = ov2[k] + ( j1*sv[k][1] ); + } + for ( j0 = sh[0]; j0 > 0; ) { + if ( j0 < bsize ) { + s0 = j0; + j0 = 0; + } else { + s0 = bsize; + j0 -= bsize; + } + // Compute index offsets and loop offset increments for the first ndarray elements in the current block... + for ( k = 0; k < N; k++ ) { + iv[ k ] = ov1[k] + ( j0*sv[k][0] ); + dv1[ k ] = sv[k][1] - ( s0*sv[k][0] ); + } + // Iterate over the non-reduced ndarray dimensions... + for ( i8 = 0; i8 < s8; i8++ ) { + for ( i7 = 0; i7 < s7; i7++ ) { + for ( i6 = 0; i6 < s6; i6++ ) { + for ( i5 = 0; i5 < s5; i5++ ) { + for ( i4 = 0; i4 < s4; i4++ ) { + for ( i3 = 0; i3 < s3; i3++ ) { + for ( i2 = 0; i2 < s2; i2++ ) { + for ( i1 = 0; i1 < s1; i1++ ) { + for ( i0 = 0; i0 < s0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ j8+i8, j7+i7, j6+i6, j5+i5, j4+i4, j3+i3, j2+i2, j1+i1, j0+i0 ], cdims, clbk, thisArg ); + ybuf[ iv[1] ] = ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + incrementOffsets( iv, dv2 ); + } + incrementOffsets( iv, dv3 ); + } + incrementOffsets( iv, dv4 ); + } + incrementOffsets( iv, dv5 ); + } + incrementOffsets( iv, dv6 ); + } + incrementOffsets( iv, dv7 ); + } + incrementOffsets( iv, dv8 ); + } + } + } + } + } + } + } + } + } + } +} + + +// EXPORTS // + +module.exports = blockedunary9d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/9d_blocked_accessors.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/9d_blocked_accessors.js new file mode 100644 index 000000000000..3c641aac7d78 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/9d_blocked_accessors.js @@ -0,0 +1,413 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params, max-depth, max-statements, max-lines-per-function */ + +'use strict'; + +// MODULES // + +var loopOrder = require( '@stdlib/ndarray/base/unary-loop-interchange-order' ); +var blockSize = require( '@stdlib/ndarray/base/unary-tiling-block-size' ); +var takeIndexed = require( '@stdlib/array/base/take-indexed' ); +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var incrementOffsets = require( './increment_offsets.js' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to a callback function and assigns results to a provided output ndarray via loop blocking. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var accessors = require( '@stdlib/array/base/accessors' ); +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var zeros = require( '@stdlib/array/base/zeros' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 1, 1, 1, 1, 1, 1, 1, 3, 2, 2 ]; +* var ysh = [ 1, 1, 1, 1, 1, 1, 1, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 12, 12, 12, 12, 12, 12, 12, 4, 2, 1 ]; +* var sy = [ 3, 3, 3, 3, 3, 3, 3, 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major', +* 'accessors': accessors( xbuf ).accessors +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major', +* 'accessors': accessors( ybuf ).accessors +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]; +* var cdims = [ 9, 10 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 12, 12, 12, 12, 12, 12, 12, 4 ]; +* +* // Perform a reduction: +* blockedunary9d( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ [ [ [ [ [ [ 8.0, 16.0, 24.0 ] ] ] ] ] ] ] ] ] +*/ +function blockedunary9d( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, opts, hasOpts, clbk, thisArg ) { + var bsize; + var ybuf; + var set; + var dv0; + var dv1; + var dv2; + var dv3; + var dv4; + var dv5; + var dv6; + var dv7; + var dv8; + var ov1; + var ov2; + var ov3; + var ov4; + var ov5; + var ov6; + var ov7; + var ov8; + var sh; + var s0; + var s1; + var s2; + var s3; + var s4; + var s5; + var s6; + var s7; + var s8; + var sv; + var ov; + var iv; + var i0; + var i1; + var i2; + var i3; + var i4; + var i5; + var i6; + var i7; + var i8; + var j0; + var j1; + var j2; + var j3; + var j4; + var j5; + var j6; + var j7; + var j8; + var N; + var x; + var y; + var v; + var o; + var k; + var f; + + // Note on variable naming convention: S#, dv#, i#, j# where # corresponds to the loop number, with `0` being the innermost loop... + + N = arrays.length; + x = arrays[ 0 ]; + y = arrays[ 1 ]; + + // Resolve the loop interchange order: + o = loopOrder( y.shape, strides, y.strides ); + sh = o.sh; + sv = [ o.sx, o.sy ]; + for ( k = 2; k < N; k++ ) { + sv.push( takeIndexed( arrays[k].strides, o.idx ) ); + } + // Determine the block size: + bsize = blockSize( x.dtype, y.dtype ); + + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + ov = offsets( arrays ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Cache accessors: + set = y.accessors[ 1 ]; + + // Cache offset increments for the innermost loop... + dv0 = []; + for ( k = 0; k < N; k++ ) { + dv0.push( sv[k][0] ); + } + // Initialize loop variables... + ov1 = zeros( N ); + ov2 = zeros( N ); + ov3 = zeros( N ); + ov4 = zeros( N ); + ov5 = zeros( N ); + ov6 = zeros( N ); + ov7 = zeros( N ); + ov8 = zeros( N ); + dv1 = zeros( N ); + dv2 = zeros( N ); + dv3 = zeros( N ); + dv4 = zeros( N ); + dv5 = zeros( N ); + dv6 = zeros( N ); + dv7 = zeros( N ); + dv8 = zeros( N ); + iv = zeros( N ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Iterate over blocks... + for ( j8 = sh[8]; j8 > 0; ) { + if ( j8 < bsize ) { + s8 = j8; + j8 = 0; + } else { + s8 = bsize; + j8 -= bsize; + } + for ( k = 0; k < N; k++ ) { + ov8[ k ] = ov[k] + ( j8*sv[k][8] ); + } + for ( j7 = sh[7]; j7 > 0; ) { + if ( j7 < bsize ) { + s7 = j7; + j7 = 0; + } else { + s7 = bsize; + j7 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv8 = sv[k][8] - ( s7*sv[k][7] ); + ov7[ k ] = ov8[k] + ( j7*sv[k][7] ); + } + for ( j6 = sh[6]; j6 > 0; ) { + if ( j6 < bsize ) { + s6 = j6; + j6 = 0; + } else { + s6 = bsize; + j6 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv7 = sv[k][7] - ( s6*sv[k][6] ); + ov6[ k ] = ov7[k] + ( j6*sv[k][6] ); + } + for ( j5 = sh[5]; j5 > 0; ) { + if ( j5 < bsize ) { + s5 = j5; + j5 = 0; + } else { + s5 = bsize; + j5 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv6 = sv[k][6] - ( s5*sv[k][5] ); + ov5[ k ] = ov6[k] + ( j5*sv[k][5] ); + } + for ( j4 = sh[4]; j4 > 0; ) { + if ( j4 < bsize ) { + s4 = j4; + j4 = 0; + } else { + s4 = bsize; + j4 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv5 = sv[k][5] - ( s4*sv[k][4] ); + ov4[ k ] = ov5[k] + ( j4*sv[k][4] ); + } + for ( j3 = sh[3]; j3 > 0; ) { + if ( j3 < bsize ) { + s3 = j3; + j3 = 0; + } else { + s3 = bsize; + j3 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv4[ k ] = sv[k][4] - ( s3*sv[k][3] ); + ov3[ k ] = ov4[k] + ( j3*sv[k][3] ); + } + for ( j2 = sh[2]; j2 > 0; ) { + if ( j2 < bsize ) { + s2 = j2; + j2 = 0; + } else { + s2 = bsize; + j2 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv3[ k ] = sv[k][3] - ( s2*sv[k][2] ); + ov2[ k ] = ov3[k] + ( j2*sv[k][2] ); + } + for ( j1 = sh[1]; j1 > 0; ) { + if ( j1 < bsize ) { + s1 = j1; + j1 = 0; + } else { + s1 = bsize; + j1 -= bsize; + } + for ( k = 0; k < N; k++ ) { + dv2[ k ] = sv[k][2] - ( s1*sv[k][1] ); + ov1[ k ] = ov2[k] + ( j1*sv[k][1] ); + } + for ( j0 = sh[0]; j0 > 0; ) { + if ( j0 < bsize ) { + s0 = j0; + j0 = 0; + } else { + s0 = bsize; + j0 -= bsize; + } + // Compute index offsets and loop offset increments for the first ndarray elements in the current block... + for ( k = 0; k < N; k++ ) { + iv[ k ] = ov1[k] + ( j0*sv[k][0] ); + dv1[ k ] = sv[k][1] - ( s0*sv[k][0] ); + } + // Iterate over the non-reduced ndarray dimensions... + for ( i8 = 0; i8 < s8; i8++ ) { + for ( i7 = 0; i7 < s7; i7++ ) { + for ( i6 = 0; i6 < s6; i6++ ) { + for ( i5 = 0; i5 < s5; i5++ ) { + for ( i4 = 0; i4 < s4; i4++ ) { + for ( i3 = 0; i3 < s3; i3++ ) { + for ( i2 = 0; i2 < s2; i2++ ) { + for ( i1 = 0; i1 < s1; i1++ ) { + for ( i0 = 0; i0 < s0; i0++ ) { + setViewOffsets( views, iv ); + v[ 0 ] = strategy( views[ 0 ] ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, [ j8+i8, j7+i7, j6+i6, j5+i5, j4+i4, j3+i3, j2+i2, j1+i1, j0+i0 ], cdims, clbk, thisArg ); + set( ybuf, iv[1], ( hasOpts ) ? fcn( v, opts, f ) : fcn( v, f ) ); + incrementOffsets( iv, dv0 ); + } + incrementOffsets( iv, dv1 ); + } + incrementOffsets( iv, dv2 ); + } + incrementOffsets( iv, dv3 ); + } + incrementOffsets( iv, dv4 ); + } + incrementOffsets( iv, dv5 ); + } + incrementOffsets( iv, dv6 ); + } + incrementOffsets( iv, dv7 ); + } + incrementOffsets( iv, dv8 ); + } + } + } + } + } + } + } + } + } + } +} + + +// EXPORTS // + +module.exports = blockedunary9d; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/callback_wrapper.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/callback_wrapper.js new file mode 100644 index 000000000000..eb54a7488f2a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/callback_wrapper.js @@ -0,0 +1,72 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var put = require( '@stdlib/array/base/put' ); +var ind2sub = require( '@stdlib/ndarray/base/ind2sub' ).assign; +var zeros = require( '@stdlib/array/base/zeros' ); + + +// VARIABLES // + +var MODE = 'throw'; + + +// MAIN // + +/** +* Wraps a provided callback function. +* +* @private +* @param {ndarray} arr - input ndarray +* @param {ndarray} view - reduced view of the input ndarray +* @param {NonNegativeIntegerArray} idx - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - loop dimensions +* @param {NonNegativeIntegerArray} lidx - current loop iteration indices +* @param {NonNegativeIntegerArray} cdims - core dimensions +* @param {Function} clbk - callback function +* @param {thisArg} thisArg - callback execution context +* @returns {Function} callback wrapper +*/ +function wrap( arr, view, idx, ldims, lidx, cdims, clbk, thisArg ) { + var cidx = zeros( cdims.length ); // workspace for storing core iteration indices + put( idx, ldims, lidx, MODE ); + return wrapper; + + /** + * Invokes a callback function. + * + * @private + * @param {*} v - value + * @param {NonNegativeInteger} aidx - current array element index + * @returns {*} result + */ + function wrapper( v, aidx ) { + ind2sub( view.shape, view.strides, view.offset, view.order, aidx, MODE, cidx ); // eslint-disable-line max-len + put( idx, cdims, cidx, MODE ); + return clbk.call( thisArg, v, idx.slice(), arr ); + } +} + + +// EXPORTS // + +module.exports = wrap; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/factory.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/factory.js new file mode 100644 index 000000000000..0f2f7c6e7192 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/factory.js @@ -0,0 +1,124 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isFunction = require( '@stdlib/assert/is-function' ); +var format = require( '@stdlib/string/format' ); +var reduce = require( './main.js' ); + + +// MAIN // + +/** +* Return a function for performing a reduction over a list of specified dimensions in an input ndarray via a one-dimensional strided array reduction function accepting a callback and assigning results to a provided output ndarray. +* +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @throws {TypeError} first argument must be a function +* @returns {Function} function for performing a reduction +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 3, 2, 2 ]; +* var ysh = [ 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 4, 2, 1 ]; +* var sy = [ 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major' +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major' +* }; +* +* // Create a function for performing a reduction over subarrays: +* var max = factory( maxBy ); +* // returns +* +* // Perform a reduction: +* max( [ x, y ], [ 2, 3 ], clbk ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ 8.0, 16.0, 24.0 ] ] +*/ +function factory( fcn ) { + if ( !isFunction( fcn ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', fcn ) ); + } + return reducer; + + /** + * 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. + * + * @private + * @param {ArrayLikeObject} arrays - array-like object containing ndarrays + * @param {IntegerArray} dims - list of dimensions over which to perform a reduction + * @param {Options} [options] - function options + * @param {Function} clbk - callback function + * @param {thisArg} [thisArg] - callback execution context + * @returns {void} + */ + function reducer( arrays, dims, options, clbk, thisArg ) { + var nargs = arguments.length; + if ( nargs < 4 ) { + return reduce( fcn, arrays, dims, options ); + } + if ( nargs === 4 ) { + return reduce( fcn, arrays, dims, options, clbk ); + } + return reduce( fcn, arrays, dims, options, clbk, thisArg ); + } +} + + +// EXPORTS // + +module.exports = factory; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/increment_offsets.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/increment_offsets.js new file mode 100644 index 000000000000..7d525521b721 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/increment_offsets.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Increments index offsets according to a list of increments. +* +* ## Notes +* +* - This function mutates the list of index offsets. +* +* @private +* @param {NonNegativeIntegerArray} offsets - list of index offsets +* @param {NonNegativeIntegerArray} inc - list of increments +* @returns {NonNegativeIntegerArray} updated offsets +*/ +function incrementOffsets( offsets, inc ) { + var i; + for ( i = 0; i < offsets.length; i++ ) { + offsets[ i ] += inc[ i ]; + } + return offsets; +} + + +// EXPORTS // + +module.exports = incrementOffsets; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/index.js new file mode 100644 index 000000000000..b833fe48d29d --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/index.js @@ -0,0 +1,151 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Perform a reduction over a list of specified dimensions in an input ndarray via a one-dimensional strided array reduction function accepting a callback and assign results to a provided output ndarray. +* +* @module @stdlib/ndarray/base/unary-reduce-strided1d-by +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* var unaryReduceStrided1dBy = require( '@stdlib/ndarray/base/unary-reduce-strided1d-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 3, 2, 2 ]; +* var ysh = [ 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 4, 2, 1 ]; +* var sy = [ 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major' +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major' +* }; +* +* // Perform a reduction: +* unaryReduceStrided1dBy( wrapper, [ x, y ], [ 2, 3 ] ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ 10.0, 26.0, 42.0 ] ] +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* var unaryReduceStrided1dBy = require( '@stdlib/ndarray/base/unary-reduce-strided1d-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 3, 2, 2 ]; +* var ysh = [ 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 4, 2, 1 ]; +* var sy = [ 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major' +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major' +* }; +* +* // Create a function for performing a reduction over subarrays: +* var max = unaryReduceStrided1dBy.factory( maxBy ); +* // returns +* +* // Perform a reduction: +* max( [ x, y ], [ 2, 3 ], clbk ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ 10.0, 26.0, 42.0 ] ] +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './main.js' ); +var factory = require( './factory.js' ); + + +// MAIN // + +setReadOnly( main, 'factory', factory ); + + +// EXPORTS // + +module.exports = main; + +// exports: { "factory": "main.factory" } diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/initialize_array_views.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/initialize_array_views.js new file mode 100644 index 000000000000..f9f52252bed3 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/initialize_array_views.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Initialize ndarray-like objects for representing zero-dimensional sub-array views of ancillary ndarray arguments. +* +* ## Notes +* +* - This function ignores the first two ndarray-like objects, which are assumed to be the input and output ndarray, respectively. +* - This function mutates the provided output array. +* +* @private +* @param {ArrayLikeObject} arrays - list of ndarray-like objects +* @param {Array} out - output array +* @returns {Array} output array +*/ +function initializeViews( arrays, out ) { + var v; + var i; + + for ( i = 2; i < arrays.length; i++ ) { + v = arrays[ i ]; + out.push({ + 'dtype': v.dtype, + 'data': v.data, + 'shape': [], + 'strides': [ 0 ], + 'offset': v.offset, + 'order': v.order + }); + } + return out; +} + + +// EXPORTS // + +module.exports = initializeViews; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/main.js new file mode 100644 index 000000000000..bdb2c586e247 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/main.js @@ -0,0 +1,493 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len */ + +'use strict'; + +// MODULES // + +var isFunction = require( '@stdlib/assert/is-function' ); +var ndarray2object = require( '@stdlib/ndarray/base/ndarraylike2object' ); +var normalizeIndices = require( '@stdlib/ndarray/base/to-unique-normalized-indices' ); +var indicesComplement = require( '@stdlib/array/base/indices-complement' ); +var takeIndexed2 = require( '@stdlib/array/base/take-indexed2' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var iterationOrder = require( '@stdlib/ndarray/base/iteration-order' ); +var strides2order = require( '@stdlib/ndarray/base/strides2order' ); +var numel = require( '@stdlib/ndarray/base/numel' ); +var join = require( '@stdlib/array/base/join' ); +var format = require( '@stdlib/string/format' ); +var initializeViews = require( './initialize_array_views.js' ); +var reshapeStrategy = require( './reshape_strategy.js' ); +var blockedaccessorunary2d = require( './2d_blocked_accessors.js' ); +var blockedaccessorunary3d = require( './3d_blocked_accessors.js' ); +var blockedaccessorunary4d = require( './4d_blocked_accessors.js' ); +var blockedaccessorunary5d = require( './5d_blocked_accessors.js' ); +var blockedaccessorunary6d = require( './6d_blocked_accessors.js' ); +var blockedaccessorunary7d = require( './7d_blocked_accessors.js' ); +var blockedaccessorunary8d = require( './8d_blocked_accessors.js' ); +var blockedaccessorunary9d = require( './9d_blocked_accessors.js' ); +var blockedaccessorunary10d = require( './10d_blocked_accessors.js' ); +var blockedunary2d = require( './2d_blocked.js' ); +var blockedunary3d = require( './3d_blocked.js' ); +var blockedunary4d = require( './4d_blocked.js' ); +var blockedunary5d = require( './5d_blocked.js' ); +var blockedunary6d = require( './6d_blocked.js' ); +var blockedunary7d = require( './7d_blocked.js' ); +var blockedunary8d = require( './8d_blocked.js' ); +var blockedunary9d = require( './9d_blocked.js' ); +var blockedunary10d = require( './10d_blocked.js' ); +var accessorunary0d = require( './0d_accessors.js' ); +var accessorunary1d = require( './1d_accessors.js' ); +var accessorunary2d = require( './2d_accessors.js' ); +var accessorunary3d = require( './3d_accessors.js' ); +var accessorunary4d = require( './4d_accessors.js' ); +var accessorunary5d = require( './5d_accessors.js' ); +var accessorunary6d = require( './6d_accessors.js' ); +var accessorunary7d = require( './7d_accessors.js' ); +var accessorunary8d = require( './8d_accessors.js' ); +var accessorunary9d = require( './9d_accessors.js' ); +var accessorunary10d = require( './10d_accessors.js' ); +var accessorunarynd = require( './nd_accessors.js' ); +var unary0d = require( './0d.js' ); +var unary1d = require( './1d.js' ); +var unary2d = require( './2d.js' ); +var unary3d = require( './3d.js' ); +var unary4d = require( './4d.js' ); +var unary5d = require( './5d.js' ); +var unary6d = require( './6d.js' ); +var unary7d = require( './7d.js' ); +var unary8d = require( './8d.js' ); +var unary9d = require( './9d.js' ); +var unary10d = require( './10d.js' ); +var unarynd = require( './nd.js' ); + + +// VARIABLES // + +var UNARY = [ + unary0d, + unary1d, + unary2d, + unary3d, + unary4d, + unary5d, + unary6d, + unary7d, + unary8d, + unary9d, + unary10d +]; +var ACCESSOR_UNARY = [ + accessorunary0d, + accessorunary1d, + accessorunary2d, + accessorunary3d, + accessorunary4d, + accessorunary5d, + accessorunary6d, + accessorunary7d, + accessorunary8d, + accessorunary9d, + accessorunary10d +]; +var BLOCKED_UNARY = [ + blockedunary2d, // 0 + blockedunary3d, + blockedunary4d, + blockedunary5d, + blockedunary6d, + blockedunary7d, + blockedunary8d, + blockedunary9d, + blockedunary10d // 8 +]; +var BLOCKED_ACCESSOR_UNARY = [ + blockedaccessorunary2d, // 0 + blockedaccessorunary3d, + blockedaccessorunary4d, + blockedaccessorunary5d, + blockedaccessorunary6d, + blockedaccessorunary7d, + blockedaccessorunary8d, + blockedaccessorunary9d, + blockedaccessorunary10d // 8 +]; +var MAX_DIMS = UNARY.length - 1; + + +// MAIN // + +/** +* Performs a reduction over a list of specified dimensions in an input ndarray via a one-dimensional strided array reduction function accepting a callback and assigns results to a provided output ndarray. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {ArrayLikeObject} arrays - array-like object containing ndarrays +* @param {IntegerArray} dims - list of dimensions over which to perform a reduction +* @param {Options} [options] - function options +* @param {Function} clbk - callback function +* @param {*} [thisArg] - callback execution context +* @throws {Error} arrays must have the expected number of dimensions +* @throws {RangeError} dimension indices must not exceed input ndarray bounds +* @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions +* @throws {Error} must provide unique dimension indices +* @throws {Error} arrays must have the same loop dimension sizes +* @returns {void} +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 3, 2, 2 ]; +* var ysh = [ 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 4, 2, 1 ]; +* var sy = [ 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major' +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major' +* }; +* +* // Perform a reduction: +* unaryReduceStrided1dBy( maxBy, [ x, y ], [ 2, 3 ], clbk ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ 8.0, 16.0, 24.0 ] ] +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 3, 2, 2 ]; +* var ysh = []; +* +* // Define the array strides: +* var sx = [ 12, 4, 2, 1 ]; +* var sy = [ 0 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major' +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major' +* }; +* +* // Perform a reduction: +* unaryReduceStrided1dBy( maxBy, [ x, y ], [ 0, 1, 2, 3 ], clbk ); +* +* var v = y.data; +* // returns [ 24.0 ] +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 3, 2, 2 ]; +* var ysh = [ 3, 2, 2 ]; +* +* // Define the array strides: +* var sx = [ 4, 2, 1 ]; +* var sy = [ 4, 2, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major' +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major' +* }; +* +* // Perform a reduction: +* unaryReduceStrided1dBy( maxBy, [ x, y ], [], clbk ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ [ 2.0, 4.0 ], [ 6.0, 8.0 ] ], [ [ 10.0, 12.0 ], [ 14.0, 16.0 ] ], [ [ 18.0, 20.0 ], [ 22.0, 24.0 ] ] ] +*/ +function unaryReduceStrided1dBy( fcn, arrays, dims, options, clbk, thisArg ) { // eslint-disable-line max-statements + var workspace; + var strategy; + var views; + var ndims; + var ldims; + var nargs; + var opts; + var arr; + var tmp; + var len; + var shx; + var shc; + var shl; + var iox; + var ioy; + var ord; + var FLG; + var ctx; + var sc; + var sl; + var sy; + var cb; + var d; + var s; + var N; + var M; + var K; + var x; + var y; + var i; + var j; + + nargs = arguments.length; + FLG = false; + + // Case: ( fcn, arrays, dims, clbk ) + if ( nargs < 5 ) { + opts = {}; + cb = options; + } + // Case: ( fcn, arrays, dims, options, clbk, thisArg ) + else if ( nargs > 5 ) { + FLG = true; + opts = options; + cb = clbk; + ctx = thisArg; + } + // Case: ( fcn, arrays, dims, clbk, thisArg ) + else if ( isFunction( options ) ) { + opts = {}; + cb = options; + ctx = clbk; + } + // Case: ( fcn, arrays, dims, options, clbk ) + else { + FLG = true; + opts = options; + cb = clbk; + } + // Standardize ndarray meta data... + N = arrays.length; + arr = []; + for ( i = 0; i < N; i++ ) { + arr.push( ndarray2object( arrays[ i ] ) ); + } + // Cache references to the input and output arrays: + x = arr[ 0 ]; + y = arr[ 1 ]; + + // Resolve the number of input array dimensions: + shx = x.shape; + ndims = shx.length; + + // Verify that we've been provided a list of unique dimension indices... + M = dims.length; + d = normalizeIndices( dims, ndims-1 ); + if ( d === null ) { + throw new RangeError( format( 'invalid argument. Third argument contains an out-of-bounds dimension index. Value: [%s].', join( dims, ',' ) ) ); + } + d.sort(); + if ( d.length !== M ) { + throw new Error( format( 'invalid argument. Third argument must contain a list of unique dimension indices. Value: [%s].', join( dims, ',' ) ) ); + } + // Check whether we've been provided a valid number of dimensions to reduce... + if ( M > ndims ) { + 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, ',' ) ) ); + } + // Verify that provided ndarrays have the expected number of dimensions... + K = ndims - M; + for ( i = 1; i < N; i++ ) { + if ( arr[ i ].shape.length !== K ) { + 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 ) ); + } + } + // Initialize a workspace for storing iteration indices: + workspace = zeros( ndims ); + + // Resolve the non-reduced ("loop") dimensions and associated strides: + ldims = indicesComplement( shx.length, d ); + tmp = takeIndexed2( shx, x.strides, ldims ); + shl = tmp[ 0 ]; + sl = tmp[ 1 ]; + + // Resolve the reduced ("core") dimensions and associated strides: + tmp = takeIndexed2( shx, x.strides, d ); + shc = tmp[ 0 ]; + sc = tmp[ 1 ]; + + // Verify that the provided arrays have the same loop dimensions... + len = 1; // number of elements + for ( i = 0; i < K; i++ ) { + s = shl[ i ]; + for ( j = 1; j < N; j++ ) { + if ( s !== arr[ j ].shape[ i ] ) { + throw new Error( format( 'invalid argument. Non-reduced dimensions must be consistent across all provided arrays. Input array shape: [%s]. Non-reduced dimension indices: [%s]. Non-reduced dimensions: [%s]. Array shape: [%s] (index: %d).', join( shx, ',' ), join( ldims, ',' ), join( shl, ',' ), join( arr[ j ].shape, ',' ), j ) ); + } + } + // Note that, if one of the dimensions is `0`, the length will be `0`... + len *= s; + } + // Check whether we were provided empty ndarrays... + if ( len === 0 || ( shc.length && numel( shc ) === 0 ) ) { + return; + } + // Initialize ndarray-like objects for representing sub-array views... + views = [ + { + 'dtype': x.dtype, + 'data': x.data, + 'shape': shc, + 'strides': sc, + 'offset': x.offset, + 'order': x.order + } + ]; + initializeViews( arr, views ); + + // Determine the strategy for reshaping sub-array views of the input array prior to performing a reduction: + strategy = reshapeStrategy( views[ 0 ] ); + + // Determine whether we can avoid iteration altogether... + if ( K === 0 ) { + if ( y.accessorProtocol ) { + return ACCESSOR_UNARY[ K ]( fcn, arr, strategy, workspace, ldims, d, opts, FLG, cb, ctx ); + } + return UNARY[ K ]( fcn, arr, strategy, workspace, ldims, d, opts, FLG, cb, ctx ); + } + // Determine whether we only have one loop dimension and can thus readily perform one-dimensional iteration... + if ( K === 1 ) { + if ( y.accessorProtocol ) { + return ACCESSOR_UNARY[ K ]( fcn, arr, strategy, workspace, views, ldims, d, sl, opts, FLG, cb, ctx ); + } + return UNARY[ K ]( fcn, arr, strategy, views, workspace, ldims, d, sl, opts, FLG, cb, ctx ); + } + sy = y.strides; + iox = iterationOrder( sl ); // +/-1 + ioy = iterationOrder( sy ); // +/-1 + + // Determine whether we can avoid blocked iteration... + ord = strides2order( sl ); + if ( iox !== 0 && ioy !== 0 && ord === strides2order( sy ) && K <= MAX_DIMS ) { + // So long as iteration for each respective array always moves in the same direction (i.e., no mixed sign strides) and the memory layouts are the same, we can leverage cache-optimal (i.e., normal) nested loops without resorting to blocked iteration... + if ( y.accessorProtocol ) { + return ACCESSOR_UNARY[ K ]( fcn, arr, strategy, views, workspace, ldims, d, sl, ord === 1, opts, FLG, cb, ctx ); + } + return UNARY[ K ]( fcn, arr, strategy, views, workspace, ldims, d, sl, ord === 1, opts, FLG, cb, ctx ); + } + // At this point, we're either dealing with non-contiguous n-dimensional arrays, high dimensional n-dimensional arrays, and/or arrays having differing memory layouts, so our only hope is that we can still perform blocked iteration... + + // Determine whether we can perform blocked iteration... + if ( K <= MAX_DIMS ) { + if ( y.accessorProtocol ) { + return BLOCKED_ACCESSOR_UNARY[ K-2 ]( fcn, arr, strategy, views, workspace, ldims, d, sl, opts, FLG, cb, ctx ); + } + return BLOCKED_UNARY[ K-2 ]( fcn, arr, strategy, views, workspace, ldims, d, sl, opts, FLG, cb, ctx ); + } + // Fall-through to linear view iteration without regard for how data is stored in memory (i.e., take the slow path)... + if ( y.accessorProtocol ) { + return accessorunarynd( fcn, arr, strategy, views, workspace, ldims, d, sl, opts, FLG, cb, ctx ); + } + unarynd( fcn, arr, strategy, views, workspace, ldims, d, sl, opts, FLG, cb, ctx ); +} + + +// EXPORTS // + +module.exports = unaryReduceStrided1dBy; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/nd.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/nd.js new file mode 100644 index 000000000000..7700bb844e3e --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/nd.js @@ -0,0 +1,200 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params */ + +'use strict'; + +// MODULES // + +var numel = require( '@stdlib/ndarray/base/numel' ); +var vind2bind = require( '@stdlib/ndarray/base/vind2bind' ); +var ind2sub = require( '@stdlib/ndarray/base/ind2sub' ); +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// VARIABLES // + +var MODE = 'throw'; + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to a callback function and assigns results to a provided output ndarray. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* 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 ] ); +* var ybuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); +* +* // Define the array shapes: +* var xsh = [ 1, 3, 2, 2 ]; +* var ysh = [ 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 4, 2, 1 ]; +* var sy = [ 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major' +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major' +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1 ]; +* var cdims = [ 2, 3 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 4 ]; +* +* // Perform a reduction: +* unarynd( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ 8.0, 16.0, 24.0 ] ] +*/ +function unarynd( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, opts, hasOpts, clbk, thisArg ) { + var ybuf; + var len; + var arr; + var sub; + var sh; + var iv; + var io; + var N; + var x; + var y; + var v; + var i; + var j; + var f; + + N = arrays.length; + + // Cache a reference to the input ndarray: + x = arrays[ 0 ]; + + // Resolve the output ndarray and associated shape: + y = arrays[ 1 ]; + sh = y.shape; + + // Compute the total number of elements over which to iterate: + len = numel( sh ); + + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + iv = offsets( arrays ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Iterate based on the linear **view** index, regardless as to how the data is stored in memory... + io = zeros( N ); + for ( i = 0; i < len; i++ ) { + for ( j = 0; j < N; j++ ) { + arr = arrays[ j ]; + io[ j ] = vind2bind( sh, arr.strides, iv[ j ], arr.order, i, MODE ); + } + setViewOffsets( views, io ); + v[ 0 ] = strategy( views[ 0 ] ); + sub = ind2sub( sh, strides, 0, x.order, i, MODE ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, sub, cdims, clbk, thisArg ); + ybuf[ io[1] ] = ( hasOpts ) ? fcn( v, f, opts ) : fcn( v, f ); + } +} + + +// EXPORTS // + +module.exports = unarynd; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/nd_accessors.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/nd_accessors.js new file mode 100644 index 000000000000..c41c30adba1f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/nd_accessors.js @@ -0,0 +1,208 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len, max-params */ + +'use strict'; + +// MODULES // + +var numel = require( '@stdlib/ndarray/base/numel' ); +var vind2bind = require( '@stdlib/ndarray/base/vind2bind' ); +var ind2sub = require( '@stdlib/ndarray/base/ind2sub' ); +var copyIndexed = require( '@stdlib/array/base/copy-indexed' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var setViewOffsets = require( './set_view_offsets.js' ); +var offsets = require( './offsets.js' ); +var wrap = require( './callback_wrapper.js' ); + + +// VARIABLES // + +var MODE = 'throw'; + + +// MAIN // + +/** +* Performs a reduction over an input ndarray according to a callback function and assigns results to a provided output ndarray. +* +* @private +* @param {Function} fcn - wrapper for a one-dimensional strided array reduction function +* @param {Array} arrays - ndarrays +* @param {Function} strategy - input ndarray reshape strategy +* @param {Array} views - initialized ndarray-like objects representing sub-array views +* @param {NonNegativeIntegerArray} ibuf - workspace for storing iteration indices +* @param {NonNegativeIntegerArray} ldims - list of loop dimensions +* @param {NonNegativeIntegerArray} cdims - list of "core" dimensions +* @param {IntegerArray} strides - loop dimension strides for the input ndarray +* @param {Options} opts - function options +* @param {boolean} hasOpts - boolean indicating whether to pass an options argument to a reduction function +* @param {Function} clbk - callback function +* @param {*} thisArg - callback exection context +* @returns {void} +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var accessors = require( '@stdlib/array/base/accessors' ); +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +* var maxBy = require( '@stdlib/stats/base/ndarray/max-by' ); +* +* function clbk( value ) { +* return value * 2.0; +* } +* +* // Create data buffers: +* var xbuf = toAccessorArray( 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 ] ) ); +* var ybuf = toAccessorArray( new Float64Array( [ 0.0, 0.0, 0.0 ] ) ); +* +* // Define the array shapes: +* var xsh = [ 1, 3, 2, 2 ]; +* var ysh = [ 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 12, 4, 2, 1 ]; +* var sy = [ 3, 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create an input ndarray-like object: +* var x = { +* 'dtype': 'float64', +* 'data': xbuf, +* 'shape': xsh, +* 'strides': sx, +* 'offset': ox, +* 'order': 'row-major', +* 'accessors': accessors( xbuf ).accessors +* }; +* +* // Create an output ndarray-like object: +* var y = { +* 'dtype': 'float64', +* 'data': ybuf, +* 'shape': ysh, +* 'strides': sy, +* 'offset': oy, +* 'order': 'row-major', +* 'accessors': accessors( ybuf ).accessors +* }; +* +* // Initialize ndarray-like objects representing sub-array views: +* var views = [ +* { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': x.offset, +* 'order': x.order +* } +* ]; +* +* // Define a reshape strategy: +* function strategy( x ) { +* return { +* 'dtype': x.dtype, +* 'data': x.data, +* 'shape': [ 4 ], +* 'strides': [ 1 ], +* 'offset': x.offset, +* 'order': x.order +* }; +* } +* +* // Create a workspace array for storing iteration indices: +* var ibuf = zeros( xsh.length ); +* +* // Define the loop and core dimensions: +* var ldims = [ 0, 1 ]; +* var cdims = [ 2, 3 ]; +* +* // Resolve the loop dimension strides for the input array: +* var slx = [ 12, 4 ]; +* +* // Perform a reduction: +* unarynd( maxBy, [ x, y ], strategy, views, ibuf, ldims, cdims, slx, {}, false, clbk, {} ); +* +* var arr = ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ); +* // returns [ [ 6.0, 14.0, 22.0 ] ] +*/ +function unarynd( fcn, arrays, strategy, views, ibuf, ldims, cdims, strides, opts, hasOpts, clbk, thisArg ) { + var ybuf; + var len; + var arr; + var sub; + var set; + var sh; + var iv; + var io; + var N; + var x; + var y; + var v; + var i; + var j; + var f; + + N = arrays.length; + + // Cache a reference to the input ndarray: + x = arrays[ 0 ]; + + // Resolve the output ndarray and associated shape: + y = arrays[ 1 ]; + sh = y.shape; + + // Compute the total number of elements over which to iterate: + len = numel( sh ); + + // Resolve a list of pointers to the first indexed elements in the respective ndarrays: + iv = offsets( arrays ); + + // Shallow copy the list of views to an internal array so that we can update with reshaped views without impacting the original list of views: + v = copyIndexed( views ); + + // Cache a reference to the output ndarray buffer: + ybuf = y.data; + + // Cache accessors: + set = y.accessors[ 1 ]; + + // Iterate based on the linear **view** index, regardless as to how the data is stored in memory... + io = zeros( N ); + for ( i = 0; i < len; i++ ) { + for ( j = 0; j < N; j++ ) { + arr = arrays[ j ]; + io[ j ] = vind2bind( sh, arr.strides, iv[ j ], arr.order, i, MODE ); + } + setViewOffsets( views, io ); + v[ 0 ] = strategy( views[ 0 ] ); + sub = ind2sub( sh, strides, 0, x.order, i, MODE ); + f = wrap( x.ref, views[ 0 ], ibuf, ldims, sub, cdims, clbk, thisArg ); + set( ybuf, io[1], ( hasOpts ) ? fcn( views, opts, f ) : fcn( views, f ) ); + } +} + + +// EXPORTS // + +module.exports = unarynd; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/offsets.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/offsets.js new file mode 100644 index 000000000000..a606628f5f3d --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/offsets.js @@ -0,0 +1,42 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Resolves index offsets from a list of ndarray-like objects. +* +* @private +* @param {ArrayLikeObject} arrays - list of ndarray-like objects +* @returns {NonNegativeIntegerArray} list of offsets +*/ +function offsets( arrays ) { + var out = []; + var i; + for ( i = 0; i < arrays.length; i++ ) { + out.push( arrays[ i ].offset ); + } + return out; +} + + +// EXPORTS // + +module.exports = offsets; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/reshape_strategy.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/reshape_strategy.js new file mode 100644 index 000000000000..aa8118f8f85a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/reshape_strategy.js @@ -0,0 +1,260 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var iterationOrder = require( '@stdlib/ndarray/base/iteration-order' ); +var minmaxViewBufferIndex = require( '@stdlib/ndarray/base/minmax-view-buffer-index' ).assign; +var ndarraylike2object = require( '@stdlib/ndarray/base/ndarraylike2object' ); +var assign = require( '@stdlib/ndarray/base/assign' ); +var ndarraylike2ndarray = require( '@stdlib/ndarray/base/ndarraylike2ndarray' ); +var emptyLike = require( '@stdlib/ndarray/base/empty-like' ); + + +// FUNCTIONS // + +/** +* Returns an input ndarray. +* +* @private +* @param {ndarrayLike} x - input ndarray +* @returns {ndarrayLike} input ndarray +*/ +function identity( x ) { + return x; +} + +/** +* Broadcasts a zero-dimensional ndarray to a one-dimensional ndarray view containing a single element. +* +* @private +* @param {ndarrayLike} x - input ndarray +* @returns {ndarrayLike} broadcasted ndarray view +*/ +function broadcast( x ) { + // NOTE: the following properties must be set in the exact same order as in `x` in order to ensure that the returned object has the same hidden shape as the input ndarray-like object... + return { + 'dtype': x.dtype, + 'data': x.data, + 'shape': [ 1 ], + 'strides': [ 0 ], + 'offset': x.offset, + 'order': x.order + }; +} + +/** +* Returns a function which returns an ndarray view in which the singleton dimensions are removed from an input ndarray having only a single non-singleton dimension. +* +* @private +* @param {ndarrayLike} arr - original ndarray +* @param {NonNegativeInteger} index - index of the non-singleton dimension +* @returns {Function} function for returning an ndarray view +*/ +function squeeze( arr, index ) { + var sh = [ arr.shape[ index ] ]; + var sx = [ arr.strides[ index ] ]; + return reshape; + + /** + * Returns an ndarray view in which the singleton dimensions are removed from an input ndarray having only a single non-singleton dimension. + * + * @private + * @param {ndarrayLike} x - input ndarray + * @returns {ndarrayLike} a squeezed ndarray view + */ + function reshape( x ) { + // NOTE: the following properties must be set in the exact same order as in `arr` in order to ensure that the returned object has the same hidden shape as the input ndarray-like object... + return { + 'dtype': x.dtype, + 'data': x.data, + 'shape': sh, + 'strides': sx, + 'offset': x.offset, + 'order': x.order + }; + } +} + +/** +* Returns a function which returns a one-dimensional ndarray view of a contiguous input ndarray having more than one dimension. +* +* @private +* @param {NonNegativeInteger} len - number of elements in an ndarray +* @param {integer} iox - iteration order +* @returns {Function} function for returning a one-dimensional ndarray view +*/ +function contiguous( len, iox ) { + var xmmv; + var ind; + var sh; + var sx; + + // Resolve the index of the min/max view buffer element which is the first indexed element... + if ( iox === 1 ) { + ind = 0; + } else { + ind = 1; + } + // Initialize an array for storing the min/max view buffer elements: + xmmv = [ 0, 0 ]; // [ min, max ] + + // Initialize the output one-dimensional view's shape and strides: + sh = [ len ]; + sx = [ iox ]; + + return reshape; + + /** + * Returns a one-dimensional ndarray view of a contiguous input ndarray having more than one dimension. + * + * @private + * @param {ndarrayLike} x - input ndarray + * @returns {ndarrayLike} a one-dimensional ndarray view + */ + function reshape( x ) { + // Resolve the minimum and maximum linear indices in the underlying data buffer which are accessible to the input ndarray view: + minmaxViewBufferIndex( x.shape, x.strides, x.offset, xmmv ); + + // NOTE: the following properties must be set in the exact same order as in `x` in order to ensure that the returned object has the same hidden shape as the input ndarray-like object... + return { + 'dtype': x.dtype, + 'data': x.data, + 'shape': sh, + 'strides': sx, + 'offset': xmmv[ ind ], // the index of the first indexed element + 'order': x.order + }; + } +} + +/** +* Returns a function which copies an input ndarray to a contiguous ndarray workspace. +* +* @private +* @param {NonNegativeInteger} len - number of elements in an ndarray +* @param {ndarrayLike} workspace - ndarray workspace +* @returns {Function} function which copies an input ndarray to a contiguous ndarray workspace +*/ +function copy( len, workspace ) { + // NOTE: the following properties must be set in the exact same order as in the input ndarray-like object in order to ensure that the returned object has the same hidden shape... + var view = { + 'dtype': workspace.dtype, + 'data': workspace.data, + 'shape': [ len ], + 'strides': [ 1 ], + 'offset': workspace.offset, + 'order': workspace.order + }; + return reshape; + + /** + * Copies an input ndarray to a contiguous ndarray workspace and returns a one-dimensional workspace view. + * + * @private + * @param {ndarrayLike} x - input ndarray + * @returns {ndarrayLike} one-dimensional workspace view + */ + function reshape( x ) { + assign( [ x, workspace ] ); + return view; + } +} + + +// MAIN // + +/** +* Returns a function for reshaping input ndarrays which have the same data type, shape, and strides as a provided ndarray. +* +* @private +* @param {ndarrayLike} x - input ndarray +* @param {string} x.dtype - input ndarray data type +* @param {Collection} x.data - input ndarray data buffer +* @param {NonNegativeIntegerArray} x.shape - input ndarray shape +* @param {IntegerArray} x.strides - input ndarray strides +* @param {NonNegativeInteger} x.offset - input ndarray index offset +* @param {string} x.order - input ndarray memory layout +* @returns {Function} function implementing a reshape strategy +*/ +function strategy( x ) { + var ndims; + var xmmv; + var len; + var iox; + var sh; + var ns; + var i; + + // Resolve the number of array dimensions: + sh = x.shape; + ndims = sh.length; + + // Check whether the ndarray is zero-dimensional... + if ( ndims === 0 ) { + return broadcast; + } + // Check whether the ndarray is already one-dimensional... + if ( ndims === 1 ) { + return identity; + } + // Determine the number of singleton dimensions... + len = 1; // number of elements + ns = 0; // number of singleton dimensions + for ( i = 0; i < ndims; i++ ) { + // Check whether the current dimension is a singleton dimension... + if ( sh[ i ] === 1 ) { + ns += 1; + } + len *= sh[ i ]; + } + // Determine whether the ndarray has only **one** non-singleton dimension (e.g., ndims=4, shape=[10,1,1,1]) so that we can simply create an ndarray view without the singleton dimensions... + if ( ns === ndims-1 ) { + // Get the index of the non-singleton dimension... + for ( i = 0; i < ndims; i++ ) { + if ( sh[ i ] !== 1 ) { + break; + } + } + return squeeze( x, i ); + } + iox = iterationOrder( x.strides ); // +/-1 + + // Determine whether we can avoid copying data... + if ( iox !== 0 ) { + // Determine the minimum and maximum linear indices which are accessible by the ndarray view: + xmmv = minmaxViewBufferIndex( sh, x.strides, x.offset, [ 0, 0 ] ); + + // Determine whether we can ignore shape (and strides) and create a new one-dimensional ndarray view... + if ( len === ( xmmv[1]-xmmv[0]+1 ) ) { + return contiguous( len, iox ); + } + // The ndarray is non-contiguous, so we cannot directly interpret as a one-dimensional ndarray... + + // Fall-through to copying to a workspace ndarray... + } + // At this point, we're dealing with a non-contiguous multi-dimensional ndarray, so we need to copy to a contiguous workspace: + return copy( len, ndarraylike2object( emptyLike( ndarraylike2ndarray( x ) ) ) ); // eslint-disable-line max-len +} + + +// EXPORTS // + +module.exports = strategy; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/set_view_offsets.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/set_view_offsets.js new file mode 100644 index 000000000000..32628cee8817 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/lib/set_view_offsets.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Sets view offsets according to a list of index offsets. +* +* ## Notes +* +* - This function skips the second element in the list of index offsets, as that is assumed to correspond to the output ndarray which does not have a corresponding view. Meaning, the list of views is expected to have `N` elements, and the list of index offsets is expected to have `N+1` elements. +* - This function mutates the provides view objects. +* +* @private +* @param {Array} views - list of ndarray-like objects representing ndarray views +* @param {NonNegativeIntegerArray} offsets - list of index offsets +* @returns {Array} updated views +*/ +function setViewOffsets( views, offsets ) { + var i; + var j; + for ( i = 0, j = 0; i < offsets.length; i++ ) { + if ( i === 1 ) { // note: expected to correspond to the output ndarray which does not have a corresponding view + continue; + } + views[ j ].offset = offsets[ i ]; + j += 1; + } + return views; +} + + +// EXPORTS // + +module.exports = setViewOffsets; diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/package.json b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/package.json new file mode 100644 index 000000000000..56ba48e24f3f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/package.json @@ -0,0 +1,64 @@ +{ + "name": "@stdlib/ndarray/base/unary-reduce-strided1d-by", + "version": "0.0.0", + "description": "Perform a reduction over a list of specified dimensions in an input ndarray via a one-dimensional strided array reduction function accepting a callback and assign results to a provided output ndarray.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "base", + "strided", + "array", + "ndarray", + "unary", + "reduce", + "reduction", + "accumulate", + "accumulation", + "vector" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/test/test.js b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/test/test.js new file mode 100644 index 000000000000..fd1a72b4f6a1 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unary-reduce-strided1d-by/test/test.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var unaryReduceStrided1dBy = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof unaryReduceStrided1dBy, 'function', 'main export is a function' ); + t.end(); +}); + +// FIXME: add tests