From 50c5f826a97e6237e6970cfbff05073c95287164 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sun, 26 Apr 2026 01:38:15 +0500 Subject: [PATCH 1/2] feat: add ndarray/base/append-singleton-dimensions --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: 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: passed - 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: passed - task: lint_license_headers status: passed --- --- .../append-singleton-dimensions/README.md | 133 ++++++++++++++++++ .../benchmark/benchmark.js | 115 +++++++++++++++ .../benchmark/benchmark.ndims.js | 93 ++++++++++++ .../append-singleton-dimensions/docs/repl.txt | 30 ++++ .../docs/types/index.d.ts | 47 +++++++ .../docs/types/test.ts | 87 ++++++++++++ .../examples/index.js | 29 ++++ .../append-singleton-dimensions/lib/index.js | 44 ++++++ .../append-singleton-dimensions/lib/main.js | 87 ++++++++++++ .../append-singleton-dimensions/package.json | 69 +++++++++ .../append-singleton-dimensions/test/test.js | 92 ++++++++++++ 11 files changed, 826 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/benchmark/benchmark.ndims.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/README.md b/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/README.md new file mode 100644 index 000000000000..ff64b4aacf72 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/README.md @@ -0,0 +1,133 @@ + + +# appendSingletonDimensions + +> Append singleton dimensions. + + + +
+ +
+ + + + + +
+ +## Usage + + + +```javascript +var appendSingletonDimensions = require( '@stdlib/ndarray/base/append-singleton-dimensions' ); +``` + +#### appendSingletonDimensions( x, n, writable ) + +Returns an ndarray with a specified number of appended singleton dimensions (i.e., dimensions whose size is equal to `1`). + + + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +// Create a 2x2 ndarray: +var x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); +// returns [ [ 1, 2 ], [ 3, 4 ] ] + +// Append singleton dimensions: +var y = appendSingletonDimensions( x, 3, false ); +// returns [ [ [ [ [ 1 ] ] ], [ [ [ 2 ] ] ] ], [ [ [ [ 3 ] ] ], [ [ [ 4 ] ] ] ] ] +``` + +The function accepts the following arguments: + +- **x**: input ndarray. +- **n**: number of singleton dimensions to append. +- **writable**: boolean indicating whether a returned ndarray should be writable. + +
+ + + + + +
+ +## Notes + +- The `writable` parameter **only** applies to ndarray constructors supporting **read-only** instances. + +
+ + + + + +
+ +## Examples + + + + + +```javascript +var uniform = require( '@stdlib/random/uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var appendSingletonDimensions = require( '@stdlib/ndarray/base/append-singleton-dimensions' ); + +var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); + +var y = appendSingletonDimensions( x, 3, false ); +console.log( ndarray2array( y ) ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/benchmark/benchmark.js new file mode 100644 index 000000000000..ad3661ec340d --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/benchmark/benchmark.js @@ -0,0 +1,115 @@ +/** +* @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 bench = require( '@stdlib/bench' ); +var Float64Array = require( '@stdlib/array/float64' ); +var ndarrayBase = require( '@stdlib/ndarray/base/ctor' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var appendSingletonDimensions = require( './../lib' ); + + +// MAIN // + +bench( format( '%s::base_ndarray,2d', pkg ), function benchmark( b ) { + var strides; + var values; + var buffer; + var offset; + var dtype; + var shape; + var order; + var out; + var i; + + dtype = 'float64'; + buffer = new Float64Array( 2 ); + shape = [ 2 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + values = [ + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = appendSingletonDimensions( values[ i%values.length ], 1, false ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::ndarray,2d', pkg ), function benchmark( b ) { + var strides; + var values; + var buffer; + var offset; + var dtype; + var shape; + var order; + var out; + var i; + + dtype = 'float64'; + buffer = new Float64Array( 2 ); + shape = [ 2 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + values = [ + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = appendSingletonDimensions( values[ i%values.length ], 1, false ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/benchmark/benchmark.ndims.js b/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/benchmark/benchmark.ndims.js new file mode 100644 index 000000000000..7095a546f10c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/benchmark/benchmark.ndims.js @@ -0,0 +1,93 @@ +/** +* @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 bench = require( '@stdlib/bench' ); +var array = require( '@stdlib/ndarray/array' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var appendSingletonDimensions = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} ndims - number of singleton dimensions +* @returns {Function} benchmark function +*/ +function createBenchmark( ndims ) { + var x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = appendSingletonDimensions( x, ndims, false ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var min; + var max; + var f; + var i; + + min = 1; + max = 10; + + for ( i = min; i <= max; i++ ) { + f = createBenchmark( i ); + bench( format( '%s::ndarray,2d:singleton_dimensions=%d', pkg, i ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/docs/repl.txt new file mode 100644 index 000000000000..d358aa69a1a1 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/docs/repl.txt @@ -0,0 +1,30 @@ + +{{alias}}( x, n, writable ) + Returns an array with a specified number of appended singleton dimensions. + + Parameters + ---------- + x: ndarray + Input array. + + n: integer + Number of singleton dimensions to append. + + writable: boolean + Boolean indicating whether a returned array should be writable. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] ) + [ [ 1, 2 ], [ 3, 4 ] ] + > var y = {{alias}}( x, 1, false ) + [ [ [ 1 ], [ 2 ] ], [ [ 3 ], [ 4 ] ] ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/docs/types/index.d.ts new file mode 100644 index 000000000000..23e350efc6d1 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/docs/types/index.d.ts @@ -0,0 +1,47 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { ndarray } from '@stdlib/types/ndarray'; + +/** +* Returns an array with a specified number of appended singleton dimensions. +* +* @param x - input array +* @param n - number of singleton dimensions to append +* @param writable - boolean indicating whether a returned array should be writable +* @returns output array +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); +* // returns [ [ 1, 2 ], [ 3, 4 ] ] +* +* var y = appendSingletonDimensions( x, 3, false ); +* // returns [ [ [ [ [ 1 ] ] ], [ [ [ 2 ] ] ] ], [ [ [ [ 3 ] ] ], [ [ [ 4 ] ] ] ] ] +*/ +declare function appendSingletonDimensions( x: T, n: number, writable: boolean ): T; + + +// EXPORTS // + +export = appendSingletonDimensions; diff --git a/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/docs/types/test.ts new file mode 100644 index 000000000000..6d9588a8760a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/docs/types/test.ts @@ -0,0 +1,87 @@ +/* +* @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 space-in-parens */ + +import zeros = require( '@stdlib/ndarray/zeros' ); +import appendSingletonDimensions = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + appendSingletonDimensions( x, 3, false ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the function is not provided a first argument which is an ndarray... +{ + appendSingletonDimensions( '5', 3, false ); // $ExpectError + appendSingletonDimensions( 5, 3, false ); // $ExpectError + appendSingletonDimensions( true, 3, false ); // $ExpectError + appendSingletonDimensions( false, 3, false ); // $ExpectError + appendSingletonDimensions( null, 3, false ); // $ExpectError + appendSingletonDimensions( {}, 3, false ); // $ExpectError + appendSingletonDimensions( [ '5' ], 3, false ); // $ExpectError + appendSingletonDimensions( ( x: number ): number => x, 3, false ); // $ExpectError +} + +// The compiler throws an error if the function is not provided a second argument which is a number... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + appendSingletonDimensions( x, '5', false ); // $ExpectError + appendSingletonDimensions( x, true, false ); // $ExpectError + appendSingletonDimensions( x, false, false ); // $ExpectError + appendSingletonDimensions( x, null, false ); // $ExpectError + appendSingletonDimensions( x, {}, false ); // $ExpectError + appendSingletonDimensions( x, [ '5' ], false ); // $ExpectError + appendSingletonDimensions( x, ( x: number ): number => x, false ); // $ExpectError +} + +// The compiler throws an error if the function is not provided a third argument which is a boolean... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + appendSingletonDimensions( x, 3, '5' ); // $ExpectError + appendSingletonDimensions( x, 3, 5 ); // $ExpectError + appendSingletonDimensions( x, 3, null ); // $ExpectError + appendSingletonDimensions( x, 3, {} ); // $ExpectError + appendSingletonDimensions( x, 3, [ '5' ] ); // $ExpectError + appendSingletonDimensions( x, 3, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + appendSingletonDimensions(); // $ExpectError + appendSingletonDimensions( x ); // $ExpectError + appendSingletonDimensions( x, 3 ); // $ExpectError + appendSingletonDimensions( x, 3, false, [ 1, 2, 3 ], [ 2, 3 ] ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/examples/index.js new file mode 100644 index 000000000000..2bbf568d90fb --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/examples/index.js @@ -0,0 +1,29 @@ +/** +* @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'; + +var uniform = require( '@stdlib/random/uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var appendSingletonDimensions = require( './../lib' ); + +var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); + +var y = appendSingletonDimensions( x, 3, false ); +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/lib/index.js new file mode 100644 index 000000000000..56134df470ed --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/lib/index.js @@ -0,0 +1,44 @@ +/** +* @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'; + +/** +* Append singleton dimensions. +* +* @module @stdlib/ndarray/base/append-singleton-dimensions +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var appendSingletonDimensions = require( '@stdlib/ndarray/base/append-singleton-dimensions' ); +* +* var x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); +* // returns [ [ 1, 2 ], [ 3, 4 ] ] +* +* var y = appendSingletonDimensions( x, 3, false ); +* // returns [ [ [ [ [ 1 ] ] ], [ [ [ 2 ] ] ] ], [ [ [ [ 3 ] ] ], [ [ [ 4 ] ] ] ] ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/lib/main.js new file mode 100644 index 000000000000..a3d7f189c93d --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/lib/main.js @@ -0,0 +1,87 @@ +/** +* @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 getDType = require( '@stdlib/ndarray/base/dtype' ); +var getShape = require( '@stdlib/ndarray/base/shape' ); +var getStrides = require( '@stdlib/ndarray/base/strides' ); +var getOffset = require( '@stdlib/ndarray/base/offset' ); +var getOrder = require( '@stdlib/ndarray/base/order' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); + + +// MAIN // + +/** +* Returns an array with a specified number of appended singleton dimensions. +* +* @param {ndarray} x - input array +* @param {NonNegativeInteger} n - number of singleton dimensions to append +* @param {boolean} writable - boolean indicating whether a returned array should be writable +* @returns {ndarray} output array +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); +* // returns [ [ 1, 2 ], [ 3, 4 ] ] +* +* var y = appendSingletonDimensions( x, 3, false ); +* // returns [ [ [ [ [ 1 ] ] ], [ [ [ 2 ] ] ] ], [ [ [ [ 3 ] ] ], [ [ [ 4 ] ] ] ] ] +*/ +function appendSingletonDimensions( x, n, writable ) { + var strides; + var shape; + var sh; + var st; + var N; + var i; + + sh = getShape( x, false ); + st = getStrides( x, false ); + N = sh.length; + + strides = []; + shape = []; + + // Copy existing dimensions... + for ( i = 0; i < N; i++ ) { + shape.push( sh[ i ] ); + strides.push( st[ i ] ); + } + // Append singleton dimensions... + for ( i = 0; i < n; i++ ) { + shape.push( 1 ); + if ( N > 0 ) { + strides.push( st[ N-1 ] ); + } else { + strides.push( st[ 0 ] ); + } + } + return new x.constructor( getDType( x ), getData( x ), shape, strides, getOffset( x ), getOrder( x ), { // eslint-disable-line max-len + 'readonly': !writable + }); +} + + +// EXPORTS // + +module.exports = appendSingletonDimensions; diff --git a/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/package.json b/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/package.json new file mode 100644 index 000000000000..c79850bb8d94 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/ndarray/base/append-singleton-dimensions", + "version": "0.0.0", + "description": "Append singleton dimensions.", + "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": { + "benchmark": "./benchmark", + "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", + "stdtypes", + "types", + "base", + "ndarray", + "expand", + "broadcast", + "reshape", + "singleton", + "multidimensional", + "array", + "utilities", + "utility", + "utils", + "util" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/test/test.js b/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/test/test.js new file mode 100644 index 000000000000..da4f981bd772 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/test/test.js @@ -0,0 +1,92 @@ +/** +* @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 array = require( '@stdlib/ndarray/array' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var getStrides = require( '@stdlib/ndarray/strides' ); +var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' ); +var appendSingletonDimensions = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof appendSingletonDimensions, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function appends singleton dimensions', function test( t ) { + var x; + var y; + + x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); + + y = appendSingletonDimensions( x, 3, false ); + + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 1, 1, 1 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( y ), true, 'returns expected value' ); + + y = appendSingletonDimensions( x, 3, true ); + + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2, 1, 1, 1 ], 'returns expected value' ); + t.strictEqual( getData( y ), x.data, 'returns expected value' ); + t.strictEqual( isReadOnly( y ), false, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function appends singleton dimensions (base; row-major)', function test( t ) { + var x; + var y; + + x = ndarray( 'generic', [ 1, 2, 3, 4 ], [ 2, 1, 2 ], [ 2, 2, 1 ], 0, 'row-major' ); + y = appendSingletonDimensions( x, 3, false ); + + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 1, 2, 1, 1, 1 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 2, 2, 1, 1, 1, 1 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + + t.end(); +}); + +tape( 'the function appends singleton dimensions (base; column-major)', function test( t ) { + var x; + var y; + + x = ndarray( 'generic', [ 1, 2, 3, 4 ], [ 2, 1, 2 ], [ 1, 2, 2 ], 0, 'column-major' ); + y = appendSingletonDimensions( x, 3, false ); + + t.notEqual( y, x, 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 1, 2, 1, 1, 1 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 1, 2, 2, 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + + t.end(); +}); From f33c77b999ac1878a2abdb0fc3077422a72a8be2 Mon Sep 17 00:00:00 2001 From: Athan Date: Sat, 25 Apr 2026 15:57:48 -0700 Subject: [PATCH 2/2] refactor: simplify loop Signed-off-by: Athan --- .../ndarray/base/append-singleton-dimensions/lib/main.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/lib/main.js index a3d7f189c93d..a17e302dd336 100644 --- a/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/base/append-singleton-dimensions/lib/main.js @@ -53,6 +53,7 @@ function appendSingletonDimensions( x, n, writable ) { var sh; var st; var N; + var s; var i; sh = getShape( x, false ); @@ -68,13 +69,10 @@ function appendSingletonDimensions( x, n, writable ) { strides.push( st[ i ] ); } // Append singleton dimensions... + s = ( N > 0 ) ? st[ N-1 ] : st[ 0 ]; for ( i = 0; i < n; i++ ) { shape.push( 1 ); - if ( N > 0 ) { - strides.push( st[ N-1 ] ); - } else { - strides.push( st[ 0 ] ); - } + strides.push( s ); } return new x.constructor( getDType( x ), getData( x ), shape, strides, getOffset( x ), getOrder( x ), { // eslint-disable-line max-len 'readonly': !writable