From 3ffd0460016f5ae462004d02f456ca2922bc62a7 Mon Sep 17 00:00:00 2001 From: iampratik13 Date: Sat, 26 Sep 2026 19:10:30 +0530 Subject: [PATCH] feat: add ndarray/base/kernels/generic/unary-strided2d/strategy --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown_pkg_readmes status: passed - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../unary-strided2d/strategy/README.md | 201 ++++++++++ .../unary-strided2d/strategy/docs/repl.txt | 43 ++ .../strategy/examples/index.js | 114 ++++++ .../unary-strided2d/strategy/lib/index.js | 50 +++ .../unary-strided2d/strategy/lib/main.js | 367 ++++++++++++++++++ .../unary-strided2d/strategy/package.json | 61 +++ .../unary-strided2d/strategy/test/test.js | 35 ++ 7 files changed, 871 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy/README.md b/lib/node_modules/@stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy/README.md new file mode 100644 index 000000000000..68c186d173cf --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy/README.md @@ -0,0 +1,201 @@ + + +# strategy + +> Return an object containing methods for reshaping an ndarray as a two-dimensional strided array view. + +
+ +
+ + + +
+ +## Usage + +```javascript +var strategy = require( '@stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy' ); +``` + +#### strategy( x ) + +Returns an object containing methods for reshaping an ndarray as a two-dimensional strided array view. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); + +var x = { + 'dtype': 'float64', + 'data': new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ), + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': 0, + 'order': 'row-major' +}; + +var out = strategy( x ); +// returns {...} +``` + +The function accepts the following arguments: + +- **x**: ndarray [descriptor][@stdlib/ndarray/base/descriptor]. + +
+ + + +
+ +## Notes + +- The function returns an object having the following properties: + + - **input**: function providing a strategy for reshaping an input ndarray to a two-dimensional input ndarray view **prior** to applying a two-dimensional strided array function. + - **output**: function providing a strategy for reshaping the results of a two-dimensional strided array function to the shape of an input ndarray **after** applying a two-dimensional strided array function. This function should only be invoked for input ndarrays which are written to by the two-dimensional strided array function. + +
+ + + +
+ +## Examples + + + + + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +var gtriu = require( '@stdlib/blas/ext/base/ndarray/gtriu' ); +var resolveKernel = require( '@stdlib/ndarray/base/kernels/generic/unary-strided2d/unblocked' ); +var strategy = require( '@stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy' ); + +// 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 = [ 1, 1, 1, 1, 3, 2, 2 ]; +var ysh = [ 1, 1, 1, 1, 3, 2, 2 ]; + +// Define the array strides: +var sx = [ 12, 12, 12, 12, 4, 2, 1 ]; +var sy = [ 12, 12, 12, 12, 4, 2, 1 ]; + +// Define the index offsets: +var ox = 0; +var oy = 0; + +// Create an input ndarray descriptor: +var x = { + 'dtype': 'float64', + 'data': xbuf, + 'shape': xsh, + 'strides': sx, + 'offset': ox, + 'order': 'row-major' +}; + +// Create an ndarray descriptor for the diagonal below which to ignore: +var k = { + 'dtype': 'float64', + 'data': new Float64Array( [ 0.0 ] ), + 'shape': [ 1, 1, 1, 1, 3 ], + 'strides': [ 0, 0, 0, 0, 0 ], + 'offset': 0, + 'order': 'row-major' +}; + +// Create an output ndarray descriptor: +var y = { + 'dtype': 'float64', + 'data': ybuf, + 'shape': ysh, + 'strides': sy, + 'offset': oy, + 'order': 'row-major' +}; + +// Initialize ndarray descriptors representing subarray views: +var views = [ + { + 'dtype': x.dtype, + 'data': x.data, + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': x.offset, + 'order': x.order + }, + { + 'dtype': y.dtype, + 'data': y.data, + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': y.offset, + 'order': y.order + }, + { + 'dtype': k.dtype, + 'data': k.data, + 'shape': [], + 'strides': [ 0 ], + 'offset': k.offset, + 'order': k.order + } +]; + +// Resolve input/output strategies when iterating over subarray views: +var strategyX = strategy( views[ 0 ] ); +var strategyY = strategy( views[ 1 ] ); + +// Resolve a kernel: +var kernel = resolveKernel( 5 ); + +// Apply strided function: +kernel( gtriu, [ x, y, k ], views, [ 1, 1, 1, 1, 3 ], [ 12, 12, 12, 12, 4 ], [ 12, 12, 12, 12, 4 ], strategyX, strategyY, {} ); + +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/kernels/generic/unary-strided2d/strategy/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy/docs/repl.txt new file mode 100644 index 000000000000..f5fea104220c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy/docs/repl.txt @@ -0,0 +1,43 @@ + +{{alias}}( x ) + Returns an object containing methods for reshaping an ndarray as a two- + dimensional strided array view. + + Parameters + ---------- + x: Object + Descriptor. + + Returns + ------- + out: Object + Object containing an input and output strategy. + + out.input: Function + Function providing a strategy for reshaping an input ndarray to a two- + dimensional input ndarray view prior to applying a two-dimensional + strided array function. + + out.output: Function + Function providing a strategy for reshaping the results of a two- + dimensional strided array function to the shape of an input ndarray + after applying a two-dimensional strided array function. This function + should only be invoked for input ndarrays which are written to by the + two-dimensional strided array function. + + Examples + -------- + > var x = { + ... 'dtype': 'float64', + ... 'data': new {{alias:@stdlib/array/float64}}( [ 1.0, 2.0, 3.0, 4.0 ] ), + ... 'shape': [ 2, 2 ], + ... 'strides': [ 2, 1 ], + ... 'offset': 0, + ... 'order': 'row-major' + ... }; + > var o = {{alias}}( x ) + {...} + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy/examples/index.js new file mode 100644 index 000000000000..cba7a5e12356 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy/examples/index.js @@ -0,0 +1,114 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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'; + +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); +var gtriu = require( '@stdlib/blas/ext/base/ndarray/gtriu' ); +var resolveKernel = require( '@stdlib/ndarray/base/kernels/generic/unary-strided2d/unblocked' ); +var strategy = require( './../lib' ); + +// 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 = [ 1, 1, 1, 1, 3, 2, 2 ]; +var ysh = [ 1, 1, 1, 1, 3, 2, 2 ]; + +// Define the array strides: +var sx = [ 12, 12, 12, 12, 4, 2, 1 ]; +var sy = [ 12, 12, 12, 12, 4, 2, 1 ]; + +// Define the index offsets: +var ox = 0; +var oy = 0; + +// Create an input ndarray descriptor: +var x = { + 'dtype': 'float64', + 'data': xbuf, + 'shape': xsh, + 'strides': sx, + 'offset': ox, + 'order': 'row-major' +}; + +// Create an ndarray descriptor for the diagonal below which to ignore: +var k = { + 'dtype': 'float64', + 'data': new Float64Array( [ 0.0 ] ), + 'shape': [ 1, 1, 1, 1, 3 ], + 'strides': [ 0, 0, 0, 0, 0 ], + 'offset': 0, + 'order': 'row-major' +}; + +// Create an output ndarray descriptor: +var y = { + 'dtype': 'float64', + 'data': ybuf, + 'shape': ysh, + 'strides': sy, + 'offset': oy, + 'order': 'row-major' +}; + +// Initialize ndarray descriptors representing subarray views: +var views = [ + { + 'dtype': x.dtype, + 'data': x.data, + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': x.offset, + 'order': x.order + }, + { + 'dtype': y.dtype, + 'data': y.data, + 'shape': [ 2, 2 ], + 'strides': [ 2, 1 ], + 'offset': y.offset, + 'order': y.order + }, + { + 'dtype': k.dtype, + 'data': k.data, + 'shape': [], + 'strides': [ 0 ], + 'offset': k.offset, + 'order': k.order + } +]; + +// Resolve input/output strategies when iterating over subarray views: +var strategyX = strategy( views[ 0 ] ); +var strategyY = strategy( views[ 1 ] ); + +// Resolve a kernel: +var kernel = resolveKernel( 5 ); + +// Apply strided function: +kernel( gtriu, [ x, y, k ], views, [ 1, 1, 1, 1, 3 ], [ 12, 12, 12, 12, 4 ], [ 12, 12, 12, 12, 4 ], strategyX, strategyY, {} ); + +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/kernels/generic/unary-strided2d/strategy/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy/lib/index.js new file mode 100644 index 000000000000..d1b33c72dc31 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy/lib/index.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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'; + +/** +* Return an object containing methods for reshaping an ndarray as a two-dimensional strided array view. +* +* @module @stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var strategy = require( '@stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy' ); +* +* var x = { +* 'dtype': 'float64', +* 'data': new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ), +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': 0, +* 'order': 'row-major' +* }; +* +* var out = strategy( x ); +* // returns {...} +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy/lib/main.js new file mode 100644 index 000000000000..c27c4939958a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy/lib/main.js @@ -0,0 +1,367 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 ndarraylike2descriptor = require( '@stdlib/ndarray/base/ndarraylike2descriptor' ); +var isRowMajorString = require( '@stdlib/ndarray/base/assert/is-row-major-string' ); +var strides2order = require( '@stdlib/ndarray/base/strides2order' ); +var assign = require( '@stdlib/ndarray/base/assign' ); +var descriptor = require( '@stdlib/ndarray/base/descriptor' ); +var empty = require( '@stdlib/ndarray/base/empty' ); + + +// FUNCTIONS // + +/** +* Returns an input ndarray. +* +* @private +* @param {Object} x - input ndarray descriptor +* @returns {Object} input ndarray descriptor +*/ +function identity( x ) { + return x; +} + +/** +* Broadcasts an ndarray containing a single element to a two-dimensional ndarray view containing a single element. +* +* @private +* @param {Object} x - input ndarray descriptor +* @returns {Object} broadcasted ndarray view descriptor +*/ +function broadcast( x ) { + return descriptor( x.dtype, x.data, [ 1, 1 ], [ 0, 0 ], x.offset, x.order ); +} + +/** +* Returns a function which returns a two-dimensional ndarray view of an input ndarray having only one non-singleton dimension. +* +* ## Notes +* +* - The returned view is a row vector (i.e., a singleton dimension is prepended to the non-singleton dimension). +* +* @private +* @param {Object} arr - original ndarray descriptor +* @param {integer} index - index of the non-singleton dimension +* @returns {Function} function for returning a two-dimensional ndarray view +*/ +function vector( arr, index ) { + var sh = [ 1, arr.shape[ index ] ]; + var sx = [ arr.strides[ index ], arr.strides[ index ] ]; + return reshape; + + /** + * Returns a two-dimensional ndarray view of an input ndarray having only one non-singleton dimension. + * + * @private + * @param {Object} x - input ndarray descriptor + * @returns {Object} a two-dimensional ndarray view descriptor + */ + function reshape( x ) { + return descriptor( x.dtype, x.data, sh, sx, x.offset, x.order ); + } +} + +/** +* Returns a function which returns an ndarray view in which the singleton dimensions are removed from an input ndarray having exactly two non-singleton dimensions. +* +* @private +* @param {Object} arr - original ndarray descriptor +* @param {Array} indices - indices of the non-singleton dimensions +* @returns {Function} function for returning an ndarray view +*/ +function squeeze( arr, indices ) { + var sh = [ arr.shape[ indices[0] ], arr.shape[ indices[1] ] ]; + var sx = [ arr.strides[ indices[0] ], arr.strides[ indices[1] ] ]; + return reshape; + + /** + * Returns an ndarray view in which the singleton dimensions are removed from an input ndarray having exactly two non-singleton dimensions. + * + * @private + * @param {Object} x - input ndarray descriptor + * @returns {Object} a squeezed ndarray view descriptor + */ + function reshape( x ) { + return descriptor( x.dtype, x.data, sh, sx, x.offset, x.order ); + } +} + +/** +* Returns a function which returns a two-dimensional ndarray view of a contiguous input ndarray having more than two dimensions. +* +* ## Notes +* +* - For row-major ndarrays, all but the last non-singleton dimension are collapsed into the "row" dimension. For column-major ndarrays, all but the first non-singleton dimension are collapsed into the "column" dimension. +* +* @private +* @param {Object} arr - original ndarray descriptor +* @param {Array} indices - indices of the non-singleton dimensions +* @param {integer} len - number of elements in an ndarray +* @param {integer} iox - iteration order +* @param {boolean} rowMajor - boolean indicating whether an ndarray is stored in row-major order +* @returns {Function} function for returning a two-dimensional ndarray view +*/ +function contiguous( arr, indices, len, iox, rowMajor ) { + var xmmv; + var ind; + var sh; + var sx; + var M; + var N; + + // 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 two-dimensional view's shape and strides... + if ( rowMajor ) { + N = arr.shape[ indices[ indices.length-1 ] ]; + M = len / N; + sx = [ iox*N, iox ]; + } else { + M = arr.shape[ indices[ 0 ] ]; + N = len / M; + sx = [ iox, iox*M ]; + } + sh = [ M, N ]; + + return reshape; + + /** + * Returns a two-dimensional ndarray view of a contiguous input ndarray having more than two dimensions. + * + * @private + * @param {Object} x - input ndarray descriptor + * @returns {Object} a two-dimensional ndarray view descriptor + */ + 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: `xmmv[ ind ]` corresponds to the index of the first indexed element + return descriptor( x.dtype, x.data, sh, sx, xmmv[ ind ], x.order ); + } +} + +/** +* Returns a function which copies an input ndarray to a contiguous ndarray workspace. +* +* @private +* @param {Object} arr - original ndarray descriptor +* @param {Array} indices - indices of the non-singleton dimensions +* @param {integer} len - number of elements in an ndarray +* @param {Object} workspace - ndarray workspace descriptor +* @param {boolean} rowMajor - boolean indicating whether an ndarray is stored in row-major order +* @returns {Function} function which copies an input ndarray to a contiguous ndarray workspace +*/ +function copyToWorkspace( arr, indices, len, workspace, rowMajor ) { + var view; + var sh; + var sx; + var M; + var N; + + // Initialize the workspace's two-dimensional view's shape and strides... + if ( rowMajor ) { + N = arr.shape[ indices[ indices.length-1 ] ]; + M = len / N; + sx = [ N, 1 ]; + } else { + M = arr.shape[ indices[ 0 ] ]; + N = len / M; + sx = [ 1, M ]; + } + sh = [ M, N ]; + + view = descriptor( workspace.dtype, workspace.data, sh, sx, workspace.offset, workspace.order ); // eslint-disable-line max-len + return reshape; + + /** + * Copies an input ndarray to a contiguous ndarray workspace and returns a two-dimensional workspace view. + * + * @private + * @param {Object} x - input ndarray descriptor + * @returns {Object} two-dimensional workspace view descriptor + */ + function reshape( x ) { + assign( [ x, workspace ] ); + return view; + } +} + +/** +* Returns a function which copies from a contiguous ndarray workspace to an input ndarray. +* +* @private +* @param {Object} workspace - ndarray workspace descriptor +* @returns {Function} function which copies from a contiguous ndarray workspace to an input ndarray +*/ +function copyFromWorkspace( workspace ) { + return copy; + + /** + * Copies from a contiguous ndarray workspace to an input ndarray. + * + * @private + * @param {Object} x - input ndarray descriptor + * @returns {Object} input ndarray descriptor + */ + function copy( x ) { + assign( [ workspace, x ] ); + return x; + } +} + + +// MAIN // + +/** +* Returns an object containing methods for reshaping an ndarray as a two-dimensional strided array view. +* +* @param {Object} x - ndarray descriptor +* @param {*} x.dtype - ndarray data type +* @param {Collection} x.data - ndarray data buffer +* @param {Array} x.shape - ndarray shape +* @param {Array} x.strides - ndarray strides +* @param {integer} x.offset - ndarray index offset +* @param {string} x.order - ndarray memory layout +* @returns {Object} object containing methods implementing a reshape strategy +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* +* var x = { +* 'dtype': 'float64', +* 'data': new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ), +* 'shape': [ 2, 2 ], +* 'strides': [ 2, 1 ], +* 'offset': 0, +* 'order': 'row-major' +* }; +* +* var out = strategy( x ); +* // returns {...} +*/ +function strategy( x ) { + var workspace; + var rowMajor; + var indices; + var ndims; + var xmmv; + var len; + var iox; + var ord; + var sh; + var i; + + // Resolve the number of array dimensions: + sh = x.shape; + ndims = sh.length; + + // Check whether the ndarray is already two-dimensional... + if ( ndims === 2 ) { + return { + 'input': identity, + 'output': identity + }; + } + // Resolve the indices of the non-singleton dimensions... + len = 1; // number of elements + indices = []; // indices of non-singleton dimensions + for ( i = 0; i < ndims; i++ ) { + // Check whether the current dimension is a non-singleton dimension... + if ( sh[ i ] !== 1 ) { + indices.push( i ); + } + len *= sh[ i ]; + } + // Determine whether the ndarray only contains a single element (e.g., ndims=0 or ndims=3, shape=[1,1,1]) so that we can simply broadcast to a two-dimensional ndarray view... + if ( indices.length === 0 ) { + return { + 'input': broadcast, + 'output': identity + }; + } + // Determine whether the ndarray has only **one** non-singleton dimension (e.g., ndims=1 or ndims=4, shape=[1,1,5,1]) so that we can simply create a row vector view... + if ( indices.length === 1 ) { + return { + 'input': vector( x, indices[ 0 ] ), + 'output': identity + }; + } + // Determine whether the ndarray has exactly **two** non-singleton dimensions (e.g., ndims=4, shape=[10,1,5,1]) so that we can simply create an ndarray view without the singleton dimensions... + if ( indices.length === 2 ) { + return { + 'input': squeeze( x, indices ), + 'output': identity + }; + } + // Resolve the memory layout which determines which dimensions are collapsed: + rowMajor = isRowMajorString( x.order ); + + 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 the ndarray is contiguous... + if ( len === ( xmmv[1]-xmmv[0]+1 ) ) { + // In order to collapse dimensions, the ndarray elements must be arranged in memory according to the ndarray's memory layout; otherwise, the collapsed dimensions would fail to have uniform strides... + ord = strides2order( x.strides ); // 0=none, 1=row-major, 2=column-major, 3=both + + // Determine whether we can ignore shape (and strides) and create a new two-dimensional ndarray view... + if ( ord === 3 || ( rowMajor && ord === 1 ) || ( !rowMajor && ord === 2 ) ) { // eslint-disable-line max-len + return { + 'input': contiguous( x, indices, len, iox, rowMajor ), + 'output': identity + }; + } + // The ndarray elements are not arranged in memory according to the ndarray's memory layout, so we cannot directly interpret as a two-dimensional ndarray... + + // Fall-through to copying to a workspace ndarray... + } + // The ndarray is non-contiguous, so we cannot directly interpret as a two-dimensional ndarray... + + // Fall-through to copying to a workspace ndarray... + } + // At this point, we're dealing with an ndarray which cannot be interpreted as a two-dimensional ndarray view, so we need to copy to a contiguous workspace: + workspace = ndarraylike2descriptor( empty( x.dtype, x.shape, x.order ) ); + return { + 'input': copyToWorkspace( x, indices, len, workspace, rowMajor ), + 'output': copyFromWorkspace( workspace ) + }; +} + + +// EXPORTS // + +module.exports = strategy; diff --git a/lib/node_modules/@stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy/package.json b/lib/node_modules/@stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy/package.json new file mode 100644 index 000000000000..7102c936f20a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy/package.json @@ -0,0 +1,61 @@ +{ + "name": "@stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy", + "version": "0.0.0", + "description": "Return an object containing methods for reshaping an ndarray as a two-dimensional strided array view.", + "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", + "ndarray", + "unary", + "workspace", + "strategy", + "util" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy/test/test.js b/lib/node_modules/@stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy/test/test.js new file mode 100644 index 000000000000..5424549718c9 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/kernels/generic/unary-strided2d/strategy/test/test.js @@ -0,0 +1,35 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 strategy = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof strategy, 'function', 'main export is a function' ); + t.end(); +}); + +// FIXME: add tests