From 1650091e228fcb256289d01f4406a8ce473b5a57 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sun, 22 Feb 2026 00:14:16 +0500 Subject: [PATCH 1/2] feat: add ndarray/base/maybe-broadcast-array-except-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 --- --- .../README.md | 158 ++++++ .../benchmark/benchmark.js | 291 +++++++++++ .../docs/repl.txt | 44 ++ .../docs/types/index.d.ts | 61 +++ .../docs/types/test.ts | 80 +++ .../examples/index.js | 50 ++ .../lib/index.js | 44 ++ .../lib/main.js | 81 +++ .../package.json | 68 +++ .../test/test.js | 472 ++++++++++++++++++ 10 files changed, 1349 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/README.md b/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/README.md new file mode 100644 index 000000000000..f428aa8f20e7 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/README.md @@ -0,0 +1,158 @@ + + +# maybeBroadcastArrayExceptDimensions + +> Broadcast an [ndarray][@stdlib/ndarray/base/ctor] to a specified shape while keeping a list of specified dimensions unchanged if and only if the specified shape differs from the provided [ndarray][@stdlib/ndarray/base/ctor]'s shape. + + + +
+ +
+ + + + + +
+ +## Usage + + + +```javascript +var maybeBroadcastArrayExceptDimensions = require( '@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions' ); +``` + +#### maybeBroadcastArrayExceptDimensions( arr, shape, dims ) + +Broadcasts an [ndarray][@stdlib/ndarray/base/ctor] to a specified shape while keeping a list of specified dimensions unchanged if and only if the specified shape differs from the provided [ndarray][@stdlib/ndarray/base/ctor]'s shape. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +// Create a 2x2 ndarray: +var x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); +// returns [ [ 1, 2 ], [ 3, 4 ] ] + +// Perform broadcasting: +var y = maybeBroadcastArrayExceptDimensions( x, [ 3, 2, 2 ], [ -1 ] ); +// returns [ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 1, 2 ], [ 3, 4 ] ], [ [ 1, 2 ], [ 3, 4 ] ] ] +``` + +The function accepts the following arguments: + +- **arr**: input [ndarray][@stdlib/ndarray/base/ctor]. +- **shape**: target shape. +- **dims**: list of dimensions to exclude from broadcasting. Should be a list of negative integers. + +
+ + + + + +
+ +## Notes + +- The function throws an error if a provided [ndarray][@stdlib/ndarray/base/ctor] is [incompatible][@stdlib/ndarray/base/broadcast-shapes] with a provided shape. +- If a provided [ndarray][@stdlib/ndarray/base/ctor] has the same shape as the specified shape, the function returns the provided [ndarray][@stdlib/ndarray/base/ctor]. +- The returned [ndarray][@stdlib/ndarray/base/ctor] is a "base" [ndarray][@stdlib/ndarray/base/ctor], and, thus, the returned [ndarray][@stdlib/ndarray/base/ctor] does not perform bounds checking or afford any of the guarantees of the non-base [ndarray][@stdlib/ndarray/ctor] constructor. The primary intent of this function is to broadcast an ndarray-like object within internal implementations and to do so with minimal overhead. + +
+ + + + + +
+ +## Examples + + + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var numel = require( '@stdlib/ndarray/base/numel' ); +var ind2sub = require( '@stdlib/ndarray/ind2sub' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var maybeBroadcastArrayExceptDimensions = require( '@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions' ); + +// Create a 1x3 array: +var x = array( [ [ 1, 2, 3 ] ] ); +// returns [ [ 1, 2, 3 ] ] + +// Broadcast the array to 2x1x3: +var y = maybeBroadcastArrayExceptDimensions( x, [ 2, 2, 3 ], [ -2 ] ); +// returns [ [ [ 1, 2, 3 ] ], [ [ 1, 2, 3 ] ] ] + +// Retrieve the shape: +var sh = getShape( y ); +// returns [ 2, 1, 3 ] + +// Retrieve the number of elements: +var N = numel( sh ); + +// Loop through the array elements... +var sub; +var v; +var i; +for ( i = 0; i < N; i++ ) { + v = y.iget( i ); + sub = ind2sub( sh, i ); + console.log( 'Y[%s] = %d', sub.join( ', ' ), v ); +} +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/benchmark/benchmark.js new file mode 100644 index 000000000000..10c979e81a2c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/benchmark/benchmark.js @@ -0,0 +1,291 @@ +/** +* @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/ndarray/base/maybe-broadcast-array-except-dimensions' ); +var pkg = require( './../package.json' ).name; +var maybeBroadcastArrayExceptDimensions = 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( 4 ); + shape = [ 2, 2 ]; + strides = [ 2, 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 = maybeBroadcastArrayExceptDimensions( values[ i%values.length ], [ 2, 2, 2 ], [ -1 ] ); // eslint-disable-line max-len + 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::base_ndarray,2d,same_shape', 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( 4 ); + shape = [ 2, 2 ]; + strides = [ 2, 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 = maybeBroadcastArrayExceptDimensions( values[ i%values.length ], shape, [ -1 ] ); // eslint-disable-line max-len + 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( 4 ); + shape = [ 2, 2 ]; + strides = [ 2, 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 = maybeBroadcastArrayExceptDimensions( values[ i%values.length ], [ 2, 2, 2 ], [ -1 ] ); // eslint-disable-line max-len + 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,same_shape', 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( 4 ); + shape = [ 2, 2 ]; + strides = [ 2, 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 = maybeBroadcastArrayExceptDimensions( values[ i%values.length ], shape, [ -1 ] ); // eslint-disable-line max-len + 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_like,2d', pkg ), function benchmark( b ) { + var strides; + var values; + var buffer; + var offset; + var dtype; + var shape; + var order; + var out; + var obj; + var i; + + dtype = 'float64'; + buffer = new Float64Array( 4 ); + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + values = []; + for ( i = 0; i < 5; i++ ) { + obj = { + 'dtype': dtype, + 'data': buffer, + 'shape': shape, + 'strides': strides, + 'offset': offset, + 'order': order + }; + values.push( obj ); + } + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = maybeBroadcastArrayExceptDimensions( values[ i%values.length ], [ 2, 2, 2 ], [ -1 ] ); // eslint-disable-line max-len + 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_like,2d,same_shape', pkg ), function benchmark( b ) { + var strides; + var values; + var buffer; + var offset; + var dtype; + var shape; + var order; + var out; + var obj; + var i; + + dtype = 'float64'; + buffer = new Float64Array( 4 ); + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + values = []; + for ( i = 0; i < 5; i++ ) { + obj = { + 'dtype': dtype, + 'data': buffer, + 'shape': shape, + 'strides': strides, + 'offset': offset, + 'order': order + }; + values.push( obj ); + } + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = maybeBroadcastArrayExceptDimensions( values[ i%values.length ], shape, [ -1 ] ); // eslint-disable-line max-len + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( out.shape[ 0 ] !== shape[ 0 ] ) { + b.fail( 'unexpected result' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/docs/repl.txt new file mode 100644 index 000000000000..27921a693549 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/docs/repl.txt @@ -0,0 +1,44 @@ + +{{alias}}( arr, shape, dims ) + Broadcasts an ndarray to a specified shape while keeping a list of specified + dimensions unchanged if and only if the specified shape differs from the + provided ndarray's shape. + + If a provided ndarray has the same shape as the specified shape, the + function returns the provided ndarray. + + The returned array is a "base" ndarray, and, thus, the returned array does + not perform bounds checking or afford any of the guarantees of the non-base + ndarray constructor. The primary intent of this function is to broadcast an + ndarray-like object within internal implementations and to do so with + minimal overhead. + + The function throws an error if a provided ndarray is incompatible with a + provided shape. + + Parameters + ---------- + arr: ndarray + Input array. + + shape: ArrayLikeObject + Desired shape. + + dims: Array + List of dimensions to exclude from broadcasting. Should be a list of + negative integers. + + Returns + ------- + out: ndarray + Broadcasted array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2, 3 ] ] ) + [ [ 1, 2, 3 ] ] + > var y = {{alias}}( x, [ 2, 2, 3 ], [ -2 ] ) + [ [ [ 1, 2, 3 ] ], [ [ 1, 2, 3 ] ] ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/docs/types/index.d.ts new file mode 100644 index 000000000000..15539d8c07c8 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/docs/types/index.d.ts @@ -0,0 +1,61 @@ +/* +* @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 { ArrayLike } from '@stdlib/types/array'; +import { ndarray } from '@stdlib/types/ndarray'; + +/** +* Broadcasts an ndarray to a specified shape if and only if the specified shape differs from the provided ndarray's shape. +* +* ## Notes +* +* - The function expects that each index in the list of dimensions is negative in order to ensure that indices correspond to the same relative position in the output ndarray shape. For example, given an input ndarray shape `[2,X1,X2]` and a desired shape `[6,7,2,Y1,Y2]`, a list of negative dimensions `[-2,-1]` correctly maps the unchanged dimensions `X` in the input ndarray to ignored dimensions `Y` in the provided target shape. Nonnegative indices, however, afford no such mapping. For example, the list of dimensions `[1,2]` corresponds to `[X1,X2]` in the input ndarray shape, but to `[7,2]` in the target shape, which is not desired. By expecting negative indices, we avoid confusion and ensure that users always refer to dimensions relative to the last broadcasted dimension. +* - The function throws an error if a provided ndarray is incompatible with a provided shape. +* - If a provided ndarray has the same shape as the specified shape, the function returns the provided ndarray. +* - The returned array is a view on the input array data buffer. The view is typically **not** contiguous. As more than one element of a returned view may refer to the same memory location, writing to the view may affect multiple elements. If you need to write to the returned array, copy the array before performing operations which may mutate elements. +* - The returned array is a "base" ndarray, and, thus, the returned array does not perform bounds checking or afford any of the guarantees of the non-base ndarray constructor. The primary intent of this function is to broadcast an ndarray-like object within internal implementations and to do so with minimal overhead. +* +* @param arr - input array +* @param shape - desired shape +* @param dims - list of dimensions to exclude from broadcasting +* @throws input array cannot have more dimensions than the desired shape +* @throws broadcasted dimensions in the input array and desired shape must be broadcast compatible +* @throws dimension indices must not exceed desired shape bounds +* @throws must provide unique dimension indices +* @returns broadcasted array +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var getShape = require( '@stdlib/ndarray/shape' ); +* +* var x = array( [ [ 1, 2, 3 ] ] ); +* // returns [ [ 1, 2, 3 ] ] +* +* var y = maybeBroadcastArrayExceptDimensions( x, [ 2, 2, 3 ], [ -2 ] ); +* // returns [ [ [ 1, 2, 3 ] ], [ [ 1, 2, 3 ] ] ] +*/ +declare function maybeBroadcastArrayExceptDimensions( arr: ndarray, shape: ArrayLike, dims: ArrayLike ): ndarray; + + +// EXPORTS // + +export = maybeBroadcastArrayExceptDimensions; diff --git a/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/docs/types/test.ts new file mode 100644 index 000000000000..2aed4e1b3346 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/docs/types/test.ts @@ -0,0 +1,80 @@ +/* +* @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. +*/ + +/// + +import zeros = require( '@stdlib/ndarray/zeros' ); +import maybeBroadcastArrayExceptDimensions = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 2, 2 ] ); + + maybeBroadcastArrayExceptDimensions( x, [ 2, 2, 2 ], [ -2 ] ); // $ExpectType ndarray +} + +// The compiler throws an error if the function is not provided a first argument which is an ndarray... +{ + maybeBroadcastArrayExceptDimensions( '5', [ 2, 2, 2 ], [ -2 ] ); // $ExpectError + maybeBroadcastArrayExceptDimensions( 5, [ 2, 2, 2 ], [ -2 ] ); // $ExpectError + maybeBroadcastArrayExceptDimensions( true, [ 2, 2, 2 ], [ -2 ] ); // $ExpectError + maybeBroadcastArrayExceptDimensions( false, [ 2, 2, 2 ], [ -2 ] ); // $ExpectError + maybeBroadcastArrayExceptDimensions( null, [ 2, 2, 2 ], [ -2 ] ); // $ExpectError + maybeBroadcastArrayExceptDimensions( {}, [ 2, 2, 2 ], [ -2 ] ); // $ExpectError + maybeBroadcastArrayExceptDimensions( [ '5' ], [ 2, 2, 2 ], [ -2 ] ); // $ExpectError + maybeBroadcastArrayExceptDimensions( ( x: number ): number => x, [ 2, 2, 2 ], [ -2 ] ); // $ExpectError +} + +// The compiler throws an error if the function is not provided a second argument which is an array-like object containing numbers... +{ + const x = zeros( [ 2, 2 ] ); + + maybeBroadcastArrayExceptDimensions( x, '5', [ -2 ] ); // $ExpectError + maybeBroadcastArrayExceptDimensions( x, 5, [ -2 ] ); // $ExpectError + maybeBroadcastArrayExceptDimensions( x, true, [ -2 ] ); // $ExpectError + maybeBroadcastArrayExceptDimensions( x, false, [ -2 ] ); // $ExpectError + maybeBroadcastArrayExceptDimensions( x, null, [ -2 ] ); // $ExpectError + maybeBroadcastArrayExceptDimensions( x, {}, [ -2 ] ); // $ExpectError + maybeBroadcastArrayExceptDimensions( x, [ '5' ], [ -2 ] ); // $ExpectError + maybeBroadcastArrayExceptDimensions( x, ( x: number ): number => x, [ -2 ] ); // $ExpectError +} + +// The compiler throws an error if the function is not provided a third argument which is not an array-like object containing numbers... +{ + const x = zeros( [ 2, 2 ] ); + + maybeBroadcastArrayExceptDimensions( x, [ 2, 2, 2 ], '5' ); // $ExpectError + maybeBroadcastArrayExceptDimensions( x, [ 2, 2, 2 ], 5 ); // $ExpectError + maybeBroadcastArrayExceptDimensions( x, [ 2, 2, 2 ], true ); // $ExpectError + maybeBroadcastArrayExceptDimensions( x, [ 2, 2, 2 ], false ); // $ExpectError + maybeBroadcastArrayExceptDimensions( x, [ 2, 2, 2 ], null ); // $ExpectError + maybeBroadcastArrayExceptDimensions( x, [ 2, 2, 2 ], [ '5' ] ); // $ExpectError + maybeBroadcastArrayExceptDimensions( x, [ 2, 2, 2 ], ( 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 ] ); + + maybeBroadcastArrayExceptDimensions(); // $ExpectError + maybeBroadcastArrayExceptDimensions( x ); // $ExpectError + maybeBroadcastArrayExceptDimensions( x, [ 1, 2, 3 ], [ -2 ], {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/examples/index.js new file mode 100644 index 000000000000..c571feba8086 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/examples/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'; + +var array = require( '@stdlib/ndarray/array' ); +var numel = require( '@stdlib/ndarray/base/numel' ); +var ind2sub = require( '@stdlib/ndarray/ind2sub' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var maybeBroadcastArrayExceptDimensions = require( './../lib' ); + +// Create a 1x3 array: +var x = array( [ [ 1, 2, 3 ] ] ); +// returns [ [ 1, 2, 3 ] ] + +// Broadcast the array to 2x1x3: +var y = maybeBroadcastArrayExceptDimensions( x, [ 2, 2, 3 ], [ -2 ] ); +// returns [ [ [ 1, 2, 3 ] ], [ [ 1, 2, 3 ] ] ] + +// Retrieve the shape: +var sh = getShape( y ); +// returns [ 2, 1, 3 ] + +// Retrieve the number of elements: +var N = numel( sh ); + +// Loop through the array elements... +var sub; +var v; +var i; +for ( i = 0; i < N; i++ ) { + v = y.iget( i ); + sub = ind2sub( sh, i ); + console.log( 'Y[%s] = %d', sub.join( ', ' ), v ); +} diff --git a/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/lib/index.js new file mode 100644 index 000000000000..ffb965c2381f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-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'; + +/** +* Broadcast an ndarray to a specified shape while keeping a list of specified dimensions unchanged if and only if the specified shape differs from the provided ndarray's shape. +* +* @module @stdlib/ndarray/base/maybe-broadcast-array-except-dimensions +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var maybeBroadcastArrayExceptDimensions = require( '@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions' ); +* +* var x = array( [ [ 1, 2, 3 ] ] ); +* // returns [ [ 1, 2, 3 ] ] +* +* var y = maybeBroadcastArrayExceptDimensions( x, [ 2, 2, 3 ], [ -2 ] ); +* // returns [ [ [ 1, 2, 3 ] ], [ [ 1, 2, 3 ] ] ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/lib/main.js new file mode 100644 index 000000000000..2cea8c9545e7 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/lib/main.js @@ -0,0 +1,81 @@ +/** +* @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 broadcast = require( '@stdlib/ndarray/base/broadcast-array-except-dimensions' ); +var getShape = require( '@stdlib/ndarray/base/shape' ); + + +// MAIN // + +/** +* Broadcasts an ndarray to a specified shape while keeping a list of specified dimensions unchanged if and only if the specified shape differs from the provided ndarray's shape. +* +* ## Notes +* +* - The function expects that each index in the list of dimensions is negative in order to ensure that indices correspond to the same relative position in the output ndarray shape. For example, given an input ndarray shape `[2,X1,X2]` and a desired shape `[6,7,2,Y1,Y2]`, a list of negative dimensions `[-2,-1]` correctly maps the unchanged dimensions `X` in the input ndarray to ignored dimensions `Y` in the provided target shape. Nonnegative indices, however, afford no such mapping. For example, the list of dimensions `[1,2]` corresponds to `[X1,X2]` in the input ndarray shape, but to `[7,2]` in the target shape, which is not desired. By expecting negative indices, we avoid confusion and ensure that users always refer to dimensions relative to the last broadcasted dimension. +* +* @param {ndarray} arr - input array +* @param {NonNegativeIntegerArray} shape - desired shape +* @param {NegativeIntegerArray} dims - list of dimensions to exclude from broadcasting +* @throws {Error} input array cannot have more dimensions than the desired shape +* @throws {Error} broadcasted dimensions in the input array and desired shape must be broadcast compatible +* @throws {RangeError} dimension indices must not exceed desired shape bounds +* @throws {Error} must provide unique dimension indices +* @returns {ndarray} broadcasted array +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var getShape = require( '@stdlib/ndarray/shape' ); +* +* var x = array( [ [ 1, 2, 3 ] ] ); +* // returns [ [ 1, 2, 3 ] ] +* +* var y = maybeBroadcastArrayExceptDimensions( x, [ 2, 2, 3 ], [ -2 ] ); +* // returns [ [ [ 1, 2, 3 ] ], [ [ 1, 2, 3 ] ] ] +*/ +function maybeBroadcastArrayExceptDimensions( arr, shape, dims ) { + var sh; + var N; + var i; + + N = shape.length; + sh = getShape( arr, false ); + + // Check whether we need to broadcast the input array... + if ( sh.length === N ) { + for ( i = 0; i < N; i++ ) { + // Check whether dimensions match... + if ( sh[ i ] !== shape[ i ] ) { + // We found a mismatched dimension; delegate to `broadcast` to ensure that the input array is broadcast compatible with the desired array shape... + return broadcast( arr, shape, dims ); + } + } + return arr; + } + // If we are provided an array having a different rank (i.e., number of dimensions) than the desired shape, assume we need to broadcast, delegating to `broadcast` to ensure that the input array is broadcast compatible with the desired array shape... + return broadcast( arr, shape, dims ); +} + + +// EXPORTS // + +module.exports = maybeBroadcastArrayExceptDimensions; diff --git a/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/package.json b/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/package.json new file mode 100644 index 000000000000..160ea7f438f7 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/package.json @@ -0,0 +1,68 @@ +{ + "name": "@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions", + "version": "0.0.0", + "description": "Broadcast an ndarray to a specified shape while keeping a list of specified dimensions unchanged if and only if the specified shape differs from the provided ndarray's shape.", + "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", + "broadcast", + "broadcasting", + "reshape", + "multidimensional", + "array", + "utilities", + "utility", + "utils", + "util" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/test/test.js b/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/test/test.js new file mode 100644 index 000000000000..cc2389f118de --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/test/test.js @@ -0,0 +1,472 @@ +/** +* @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 getData = require( '@stdlib/ndarray/data-buffer' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var maybeBroadcastArrayExceptDimensions = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof maybeBroadcastArrayExceptDimensions, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a desired shape which has fewer dimensions than the input array', function test( t ) { + var values; + var x; + var i; + + x = array({ + 'shape': [ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 ] + }); + + values = [ + [], + [ 2 ], + [ 2, 2 ], + [ 2, 2, 2 ], + [ 2, 2, 2, 2 ], + [ 2, 2, 2, 2, 2 ], + [ 2, 2, 2, 2, 2, 2 ], + [ 2, 2, 2, 2, 2, 2, 2 ], + [ 2, 2, 2, 2, 2, 2, 2, 2 ], + [ 2, 2, 2, 2, 2, 2, 2, 2, 2 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided a shape with '+values[ i ].length+' dimension' ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + maybeBroadcastArrayExceptDimensions( x, value, [ -1 ] ); + }; + } +}); + +tape( 'the function throws an error if provided a desired shape having a dimension whose size is less than the corresponding dimension in the input array', function test( t ) { + var values; + var x; + var i; + + x = array({ + 'shape': [ 2, 10, 10 ] + }); + + values = [ + [ 10, 10, 10, 1 ], + [ 10, 10, 10, 2 ], + [ 10, 10, 10, 9 ], + [ 10, 10, 1, 10 ], + [ 10, 10, 2, 10 ], + [ 10, 10, 9, 10 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided shape ('+values[ i ].join( ',')+')' ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + maybeBroadcastArrayExceptDimensions( x, value, [ -3 ] ); + }; + } +}); + +tape( 'the function throws an error if provided a desired shape and an input array shape which are broadcast-incompatible', function test( t ) { + var values; + var x; + var i; + + x = array({ + 'shape': [ 2, 10, 10 ] + }); + + values = [ + [ 10, 10, 20 ], + [ 10, 100, 10 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided shape ('+values[ i ].join( ',')+')' ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + maybeBroadcastArrayExceptDimensions( x, value, [ -3 ] ); + }; + } +}); + +tape( 'the function returns a "base" ndarray instance', function test( t ) { + var x; + var y; + + x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); + y = maybeBroadcastArrayExceptDimensions( x, [ 2, 2, 2 ], [ -2 ] ); + + t.notEqual( y, x, 'returns new instance' ); + t.strictEqual( y instanceof ndarray, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a view over the input array data buffer', function test( t ) { + var x; + var y; + + x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); + y = maybeBroadcastArrayExceptDimensions( x, [ 2, 2, 2 ], [ -2 ] ); + + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + + t.end(); +}); + +tape( 'the function broadcasts an input array (row-major)', function test( t ) { + var expected; + var actual; + var data; + var x; + var y; + var v; + var i; + + data = [ 1, 2, 3, 4 ]; + x = array( data, { + 'dtype': 'generic', + 'shape': [ 2, 2 ], + 'order': 'row-major' + }); + + y = maybeBroadcastArrayExceptDimensions( x, [ 5, 10, 2 ], [ -2 ] ); + + actual = getShape( y ); + expected = [ 5, 2, 2 ]; + t.deepEqual( actual, expected, 'returns expected shape' ); + + for ( i = 0; i < expected[ 0 ]; i++ ) { + v = y.get( i, 0, 0 ); + t.strictEqual( v, data[ 0 ], 'returns expected value for element ('+i+',0,0)' ); + + v = y.get( i, 0, 1 ); + t.strictEqual( v, data[ 1 ], 'returns expected value for element ('+i+',0,1)' ); + + v = y.get( i, 1, 0 ); + t.strictEqual( v, data[ 2 ], 'returns expected value for element ('+i+',1,0)' ); + + v = y.get( i, 1, 1 ); + t.strictEqual( v, data[ 3 ], 'returns expected value for element ('+i+',1,1)' ); + } + t.end(); +}); + +tape( 'the function broadcasts an input array (row-major; strides)', function test( t ) { + var expected; + var actual; + var data; + var x; + var y; + var v; + var i; + + data = [ 1, 2, 3, 4 ]; + x = ndarray( 'generic', data, [ 2, 2 ], [ -2, -1 ], 3, 'row-major' ); + + y = maybeBroadcastArrayExceptDimensions( x, [ 5, 10, 2 ], [ -2 ] ); + + actual = getShape( y ); + expected = [ 5, 2, 2 ]; + t.deepEqual( actual, expected, 'returns expected shape' ); + + for ( i = 0; i < expected[ 0 ]; i++ ) { + v = y.get( i, 0, 0 ); + t.strictEqual( v, data[ 3 ], 'returns expected value for element ('+i+',0,0)' ); + + v = y.get( i, 0, 1 ); + t.strictEqual( v, data[ 2 ], 'returns expected value for element ('+i+',0,1)' ); + + v = y.get( i, 1, 0 ); + t.strictEqual( v, data[ 1 ], 'returns expected value for element ('+i+',1,0)' ); + + v = y.get( i, 1, 1 ); + t.strictEqual( v, data[ 0 ], 'returns expected value for element ('+i+',1,1)' ); + } + t.end(); +}); + +tape( 'the function broadcasts an input array (row-major; strides)', function test( t ) { + var expected; + var actual; + var data; + var x; + var y; + var v; + var i; + + data = [ 1, 2, 3, 4 ]; + x = ndarray( 'generic', data, [ 2, 2 ], [ -2, 1 ], 2, 'row-major' ); + + y = maybeBroadcastArrayExceptDimensions( x, [ 5, 10, 2 ], [ -2 ] ); + + actual = getShape( y ); + expected = [ 5, 2, 2 ]; + t.deepEqual( actual, expected, 'returns expected shape' ); + + for ( i = 0; i < expected[ 0 ]; i++ ) { + v = y.get( i, 0, 0 ); + t.strictEqual( v, data[ 2 ], 'returns expected value for element ('+i+',0,0)' ); + + v = y.get( i, 0, 1 ); + t.strictEqual( v, data[ 3 ], 'returns expected value for element ('+i+',0,1)' ); + + v = y.get( i, 1, 0 ); + t.strictEqual( v, data[ 0 ], 'returns expected value for element ('+i+',1,0)' ); + + v = y.get( i, 1, 1 ); + t.strictEqual( v, data[ 1 ], 'returns expected value for element ('+i+',1,1)' ); + } + t.end(); +}); + +tape( 'the function broadcasts an input array (row-major; non-contiguous)', function test( t ) { + var expected; + var actual; + var data; + var x; + var y; + var v; + var i; + + data = [ 1, 2, 3, 4, 5, 6, 7, 8 ]; + x = ndarray( 'generic', data, [ 2, 2 ], [ 4, 2 ], 1, 'row-major' ); + + y = maybeBroadcastArrayExceptDimensions( x, [ 5, 10, 2 ], [ -2 ] ); + + actual = getShape( y ); + expected = [ 5, 2, 2 ]; + t.deepEqual( actual, expected, 'returns expected shape' ); + + for ( i = 0; i < expected[ 0 ]; i++ ) { + v = y.get( i, 0, 0 ); + t.strictEqual( v, data[ 1 ], 'returns expected value for element ('+i+',0,0)' ); + + v = y.get( i, 0, 1 ); + t.strictEqual( v, data[ 3 ], 'returns expected value for element ('+i+',0,1)' ); + + v = y.get( i, 1, 0 ); + t.strictEqual( v, data[ 5 ], 'returns expected value for element ('+i+',1,0)' ); + + v = y.get( i, 1, 1 ); + t.strictEqual( v, data[ 7 ], 'returns expected value for element ('+i+',1,1)' ); + } + t.end(); +}); + +tape( 'the function broadcasts an input array (column-major)', function test( t ) { + var expected; + var actual; + var data; + var x; + var y; + var v; + var i; + + data = [ 1, 2, 3, 4 ]; + x = array( data, { + 'dtype': 'generic', + 'shape': [ 2, 2 ], + 'order': 'column-major' + }); + + y = maybeBroadcastArrayExceptDimensions( x, [ 5, 10, 2 ], [ -2 ] ); + + actual = getShape( y ); + expected = [ 5, 2, 2 ]; + t.deepEqual( actual, expected, 'returns expected shape' ); + + for ( i = 0; i < expected[ 0 ]; i++ ) { + v = y.get( i, 0, 0 ); + t.strictEqual( v, data[ 0 ], 'returns expected value for element ('+i+',0,0)' ); + + v = y.get( i, 0, 1 ); + t.strictEqual( v, data[ 2 ], 'returns expected value for element ('+i+',0,1)' ); + + v = y.get( i, 1, 0 ); + t.strictEqual( v, data[ 1 ], 'returns expected value for element ('+i+',1,0)' ); + + v = y.get( i, 1, 1 ); + t.strictEqual( v, data[ 3 ], 'returns expected value for element ('+i+',1,1)' ); + } + t.end(); +}); + +tape( 'the function broadcasts an input array (same shape)', function test( t ) { + var expected; + var actual; + var data; + var x; + var y; + var v; + var i; + + data = [ 1, 2, 3, 4, 5, 6, 7, 8 ]; + x = array( data, { + 'dtype': 'generic', + 'shape': [ 2, 2, 2 ], + 'order': 'row-major' + }); + + y = maybeBroadcastArrayExceptDimensions( x, [ 2, 2, 2 ], [ -2 ] ); + + actual = getShape( y ); + expected = [ 2, 2, 2 ]; + t.deepEqual( actual, expected, 'returns expected shape' ); + + // Should return the same instance: + t.strictEqual( y, x, 'returns expected value' ); + + for ( i = 0; i < expected[ 0 ]; i++ ) { + v = y.get( i, 0, 0 ); + t.strictEqual( v, x.get( i, 0, 0 ), 'returns expected value for element ('+i+',0,0)' ); + + v = y.get( i, 0, 1 ); + t.strictEqual( v, x.get( i, 0, 1 ), 'returns expected value for element ('+i+',0,1)' ); + + v = y.get( i, 1, 0 ); + t.strictEqual( v, x.get( i, 1, 0 ), 'returns expected value for element ('+i+',1,0)' ); + + v = y.get( i, 1, 1 ); + t.strictEqual( v, x.get( i, 1, 1 ), 'returns expected value for element ('+i+',1,1)' ); + } + t.end(); +}); + +tape( 'the function broadcasts an input array (same number of dimensions)', function test( t ) { + var expected; + var actual; + var data; + var x; + var y; + var v; + var i; + + data = [ 1, 2, 3, 4 ]; + x = array( data, { + 'dtype': 'generic', + 'shape': [ 2, 1, 2 ], + 'order': 'row-major' + }); + + y = maybeBroadcastArrayExceptDimensions( x, [ 2, 2, 2 ], [ -2 ] ); + + actual = getShape( y ); + expected = [ 2, 1, 2 ]; + t.deepEqual( actual, expected, 'returns expected shape' ); + + for ( i = 0; i < expected[ 0 ]; i++ ) { + v = y.get( i, 0, 0 ); + t.strictEqual( v, x.get( i, 0, 0 ), 'returns expected value for element ('+i+',0,0)' ); + + v = y.get( i, 0, 1 ); + t.strictEqual( v, x.get( i, 0, 1 ), 'returns expected value for element ('+i+',0,1)' ); + } + t.end(); +}); + +tape( 'the function broadcasts an input array (singleton dimension)', function test( t ) { + var expected; + var actual; + var data; + var x; + var y; + var v; + var i; + + data = [ 1, 2 ]; + x = array( data, { + 'dtype': 'generic', + 'shape': [ 1, 2 ], + 'order': 'row-major' + }); + + y = maybeBroadcastArrayExceptDimensions( x, [ 5, 2, 2 ], [ -2 ] ); + + actual = getShape( y ); + expected = [ 5, 1, 2 ]; + t.deepEqual( actual, expected, 'returns expected shape' ); + + for ( i = 0; i < expected[ 0 ]; i++ ) { + v = y.get( i, 0, 0 ); + t.strictEqual( v, data[ 0 ], 'returns expected value for element ('+i+',0,0)' ); + + v = y.get( i, 0, 1 ); + t.strictEqual( v, data[ 1 ], 'returns expected value for element ('+i+',0,1)' ); + + v = y.get( i, 0, 0 ); + t.strictEqual( v, data[ 0 ], 'returns expected value for element ('+i+',0,0)' ); + + v = y.get( i, 0, 1 ); + t.strictEqual( v, data[ 1 ], 'returns expected value for element ('+i+',0,1)' ); + } + t.end(); +}); + +tape( 'the function broadcasts an input array (0-dimensional array)', function test( t ) { + var expected; + var actual; + var data; + var x; + var y; + var v; + var i; + + data = [ 1 ]; + x = ndarray( 'generic', data, [], [ 0 ], 0, 'row-major' ); + + expected = [ 5, 2, 2 ]; + y = maybeBroadcastArrayExceptDimensions( x, expected, [] ); + + actual = getShape( y ); + t.deepEqual( actual, expected, 'returns expected shape' ); + + for ( i = 0; i < expected[ 0 ]; i++ ) { + v = y.get( i, 0, 0 ); + t.strictEqual( v, data[ 0 ], 'returns expected value for element ('+i+',0,0)' ); + + v = y.get( i, 0, 1 ); + t.strictEqual( v, data[ 0 ], 'returns expected value for element ('+i+',0,1)' ); + + v = y.get( i, 1, 0 ); + t.strictEqual( v, data[ 0 ], 'returns expected value for element ('+i+',1,0)' ); + + v = y.get( i, 1, 1 ); + t.strictEqual( v, data[ 0 ], 'returns expected value for element ('+i+',1,1)' ); + } + t.end(); +}); From b8806c27530e721b8f9a3395e3bfffc9990262ab Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sun, 22 Feb 2026 00:31:32 +0500 Subject: [PATCH 2/2] fix: format import --- 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: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - 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: na - task: lint_license_headers status: passed --- --- .../benchmark/benchmark.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/benchmark/benchmark.js index 10c979e81a2c..ab12aca2a626 100644 --- a/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/ndarray/base/maybe-broadcast-array-except-dimensions/benchmark/benchmark.js @@ -25,7 +25,7 @@ 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/ndarray/base/maybe-broadcast-array-except-dimensions' ); +var format = require( '@stdlib/string/format' ); var pkg = require( './../package.json' ).name; var maybeBroadcastArrayExceptDimensions = require( './../lib' );