diff --git a/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/README.md b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/README.md new file mode 100644 index 000000000000..6c68c0488dcb --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/README.md @@ -0,0 +1,138 @@ + + +# unflattenShape + +> Expand a dimension over multiple dimensions. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var unflattenShape = require( '@stdlib/ndarray/base/unflatten-shape' ); +``` + +#### unflattenShape( shape, dim, sizes ) + +Expands a dimension over multiple dimensions. + +```javascript +var sh = unflattenShape( [ 6, 2, 1 ], 0, [ 3, 2 ] ); +// returns [ 3, 2, 2, 1 ] +``` + +The function accepts the following parameters: + +- **shape**: array shape. +- **dim**: dimension to be unflattened. If provided an integer less than zero, the dimension index is resolved relative to the last dimension, with the last dimension corresponding to the value `-1`. +- **sizes**: new shape of the unflattened dimension. + +#### unflattenShape.assign( shape, dim, sizes, out ) + +Expands a dimension over multiple dimensions and assigns results to a provided output array. + +```javascript +var o = [ 0, 0, 0, 0 ]; + +var out = unflattenShape.assign( [ 6, 2, 1 ], 0, [ 3, 2 ], o ); +// returns [ 3, 2, 2, 1 ] + +var bool = ( out === o ); +// returns true +``` + +The function accepts the following parameters: + +- **shape**: array shape. +- **dim**: dimension to be unflattened. If provided an integer less than zero, the dimension index is resolved relative to the last dimension, with the last dimension corresponding to the value `-1`. +- **sizes**: new shape of the unflattened dimension. +- **out**: output array. + +
+ + + + + +
+ +
+ + + + + +
+ +## Examples + + + +```javascript +var unflattenShape = require( '@stdlib/ndarray/base/unflatten-shape' ); + +var out = unflattenShape( [ 2, 4, 1 ], 1, [ 2, 2 ] ); +// returns [ 2, 2, 2, 1 ] + +out = unflattenShape( [ 2, 4, 1 ], 1, [ 2, 1, 2 ] ); +// returns [ 2, 2, 1, 2, 1 ] + +out = unflattenShape( [ 2, 4, 1 ], 1, [ 2, 1, 1, 2 ] ); +// returns [ 2, 2, 1, 1, 2, 1 ] +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/benchmark/benchmark.js new file mode 100644 index 000000000000..be0f41d2683c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/benchmark/benchmark.js @@ -0,0 +1,79 @@ +/** +* @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 isArray = require( '@stdlib/assert/is-array' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var unflattenShape = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var shape; + var sizes; + var out; + var i; + + shape = [ 5, 9, 3, 4, 2 ]; + sizes = [ 1, 1, 3 ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = unflattenShape( shape, 2, sizes ); + if ( out.length !== 7 ) { + b.fail( 'should have expected length' ); + } + } + b.toc(); + if ( !isArray( out ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:assign', pkg ), function benchmark( b ) { + var shape; + var sizes; + var out; + var i; + + shape = [ 5, 9, 3, 4, 2 ]; + sizes = [ 1, 1, 3 ]; + out = [ 0, 0, 0, 0, 0, 0, 0 ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = unflattenShape.assign( shape, 2, sizes, out ); + if ( out.length !== 7 ) { + b.fail( 'should have expected length' ); + } + } + b.toc(); + if ( !isArray( out ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/docs/repl.txt new file mode 100644 index 000000000000..0181ef48f1f7 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/docs/repl.txt @@ -0,0 +1,67 @@ + +{{alias}}( shape, dim, sizes ) + Expands a dimension over multiple dimensions. + + Parameters + ---------- + shape: ArrayLike + Array shape. + + dim: integer + Dimension to be unflattened. If less than zero, the index is resolved + relative to the last dimension, with the last dimension corresponding to + the value `-1`. + + sizes: ArrayLike + New shape of the unflattened dimension. + + Returns + ------- + out: Array + Unflattened shape. + + Examples + -------- + > var sh = [ 6, 2, 1 ]; + > var sizes = [ 3, 2 ]; + > var out = {{alias}}( sh, 0, sizes ) + [ 3, 2, 2, 1 ] + + +{{alias}}.assign( shape, dim, sizes, out ) + Expands a dimension over multiple dimensions and assigns results to a + provided output array. + + Parameters + ---------- + shape: ArrayLike + Array shape. + + dim: integer + Dimension to be unflattened. If less than zero, the index is resolved + relative to the last dimension, with the last dimension corresponding to + the value `-1`. + + sizes: ArrayLike + New shape of the unflattened dimension. + + out: Array|TypedArray|Object + Output array. + + Returns + ------- + out: Array|TypedArray|Object + Output array. + + Examples + -------- + > var sh = [ 6, 2, 1 ]; + > var sizes = [ 3, 2 ]; + > var o = [ 0, 0, 0, 0 ]; + > var out = {{alias}}.assign( sh, 0, sizes, o ) + [ 3, 2, 2, 1 ] + > var bool = ( o === out ) + true + + See Also + -------- diff --git a/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/docs/types/index.d.ts new file mode 100644 index 000000000000..b5ba5ecfe585 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/docs/types/index.d.ts @@ -0,0 +1,90 @@ +/* +* @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'; + +/** +* Interface describing `unflattenShape`. +*/ +interface Routine { + /** + * Expands a dimension over multiple dimensions. + * + * @param shape - array shape + * @param dim - dimension to be unflattened + * @param sizes - new shape of the unflattened dimension + * @returns unflattened shape + * + * @example + * var sh = unflattenShape( [ 6, 2, 1 ], 0, [ 3, 2 ] ); + * // returns [ 3, 2, 2, 1 ] + */ + ( shape: ArrayLike, dim: number, sizes: ArrayLike ): Array; + + /** + * Expands a dimension over multiple dimensions. + * + * @param shape - array shape + * @param dim - dimension to be unflattened + * @param sizes - new shape of the unflattened dimension + * @param out - output array + * @returns unflattened shape + * + * @example + * var o = [ 0, 0, 0, 0 ]; + * + * var out = unflattenShape.assign( [ 6, 2, 1 ], 0, [ 3, 2 ], o ); + * // returns [ 3, 2, 2, 1 ] + * + * var bool = ( out === o ); + * // returns true + */ + assign = ArrayLike>( shape: ArrayLike, dim: number, sizes: ArrayLike, out: T ): T; +} + +/** +* Expands a dimension over multiple dimensions. +* +* @param shape - array shape +* @param dim - dimension to be unflattened +* @param sizes - new shape of the unflattened dimension +* @returns unflattened shape +* +* @example +* var sh = unflattenShape( [ 6, 2, 1 ], 0, [ 3, 2 ] ); +* // returns [ 3, 2, 2, 1 ] +* +* @example +* var o = [ 0, 0, 0, 0 ]; +* +* var out = unflattenShape.assign( [ 6, 2, 1 ], 0, [ 3, 2 ], o ); +* // returns [ 3, 2, 2, 1 ] +* +* var bool = ( out === o ); +* // returns true +*/ +declare var unflattenShape: Routine; + + +// EXPORTS // + +export = unflattenShape; diff --git a/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/docs/types/test.ts new file mode 100644 index 000000000000..04a70768f9ed --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/docs/types/test.ts @@ -0,0 +1,133 @@ +/* +* @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 unflattenShape = require( './index' ); + + +// TESTS // + +// The function returns an array of numbers... +{ + unflattenShape( [ 6, 2, 1 ], 1, [ 3, 2 ] ); // $ExpectType number[] +} + +// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers... +{ + unflattenShape( true, 1, [ 3, 2 ] ); // $ExpectError + unflattenShape( false, 1, [ 3, 2 ] ); // $ExpectError + unflattenShape( null, 1, [ 3, 2 ] ); // $ExpectError + unflattenShape( undefined, 1, [ 3, 2 ] ); // $ExpectError + unflattenShape( '5', 1, [ 3, 2 ] ); // $ExpectError + unflattenShape( [ '1', '2' ], 1, [ 3, 2 ] ); // $ExpectError + unflattenShape( {}, 1, [ 3, 2 ] ); // $ExpectError + unflattenShape( ( x: number ): number => x, 1, [ 3, 2 ] ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a number... +{ + unflattenShape( [ 6, 2, 1 ], true, [ 3, 2 ] ); // $ExpectError + unflattenShape( [ 6, 2, 1 ], false, [ 3, 2 ] ); // $ExpectError + unflattenShape( [ 6, 2, 1 ], null, [ 3, 2 ] ); // $ExpectError + unflattenShape( [ 6, 2, 1 ], undefined, [ 3, 2 ] ); // $ExpectError + unflattenShape( [ 6, 2, 1 ], '5', [ 3, 2 ] ); // $ExpectError + unflattenShape( [ 6, 2, 1 ], [ '1', '2' ], [ 3, 2 ] ); // $ExpectError + unflattenShape( [ 6, 2, 1 ], {}, [ 3, 2 ] ); // $ExpectError + unflattenShape( [ 6, 2, 1 ], ( x: number ): number => x, [ 3, 2 ] ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not an array-like object containing numbers... +{ + unflattenShape( [ 6, 2, 1 ], 1, true ); // $ExpectError + unflattenShape( [ 6, 2, 1 ], 1, false ); // $ExpectError + unflattenShape( [ 6, 2, 1 ], 1, null ); // $ExpectError + unflattenShape( [ 6, 2, 1 ], 1, undefined ); // $ExpectError + unflattenShape( [ 6, 2, 1 ], 1, '5' ); // $ExpectError + unflattenShape( [ 6, 2, 1 ], 1, [ '1', '2' ] ); // $ExpectError + unflattenShape( [ 6, 2, 1 ], 1, {} ); // $ExpectError + unflattenShape( [ 6, 2, 1 ], 1, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + unflattenShape(); // $ExpectError + unflattenShape( [ 6, 2, 1 ] ); // $ExpectError + unflattenShape( [ 6, 2, 1 ], 1 ); // $ExpectError + unflattenShape( [ 6, 2, 1 ], 1, [ 3, 2 ], {} ); // $ExpectError +} + +// The `assign` method returns an array of numbers... +{ + unflattenShape.assign( [ 6, 2, 1 ], 1, [ 3, 2 ], [ 0, 0, 0, 0 ] ); // $ExpectType number[] +} + +// The compiler throws an error if the `assign` method is provided a first argument which is not an array-like object containing numbers... +{ + unflattenShape.assign( true, 1, [ 3, 2 ], [ 0, 0, 0, 0 ] ); // $ExpectError + unflattenShape.assign( false, 1, [ 3, 2 ], [ 0, 0, 0, 0 ] ); // $ExpectError + unflattenShape.assign( null, 1, [ 3, 2 ], [ 0, 0, 0, 0 ] ); // $ExpectError + unflattenShape.assign( undefined, 1, [ 3, 2 ], [ 0, 0, 0, 0 ] ); // $ExpectError + unflattenShape.assign( '5', 1, [ 3, 2 ], [ 0, 0, 0, 0 ] ); // $ExpectError + unflattenShape.assign( [ '1', '2' ], 1, [ 3, 2 ], [ 0, 0, 0, 0 ] ); // $ExpectError + unflattenShape.assign( {}, 1, [ 3, 2 ], [ 0, 0, 0, 0 ] ); // $ExpectError + unflattenShape.assign( ( x: number ): number => x, 1, [ 3, 2 ], [ 0, 0, 0, 0 ] ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a second argument which is not a number... +{ + unflattenShape.assign( [ 6, 2, 1 ], true, [ 3, 2 ], [ 0, 0, 0, 0 ] ); // $ExpectError + unflattenShape.assign( [ 6, 2, 1 ], false, [ 3, 2 ], [ 0, 0, 0, 0 ] ); // $ExpectError + unflattenShape.assign( [ 6, 2, 1 ], null, [ 3, 2 ], [ 0, 0, 0, 0 ] ); // $ExpectError + unflattenShape.assign( [ 6, 2, 1 ], undefined, [ 3, 2 ], [ 0, 0, 0, 0 ] ); // $ExpectError + unflattenShape.assign( [ 6, 2, 1 ], '5', [ 3, 2 ], [ 0, 0, 0, 0 ] ); // $ExpectError + unflattenShape.assign( [ 6, 2, 1 ], [ '1', '2' ], [ 3, 2 ], [ 0, 0, 0, 0 ] ); // $ExpectError + unflattenShape.assign( [ 6, 2, 1 ], {}, [ 3, 2 ], [ 0, 0, 0, 0 ] ); // $ExpectError + unflattenShape.assign( [ 6, 2, 1 ], ( x: number ): number => x, [ 3, 2 ], [ 0, 0, 0, 0 ] ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a third argument which is not an array-like object containing numbers... +{ + unflattenShape.assign( [ 6, 2, 1 ], 1, true, [ 0, 0, 0, 0 ] ); // $ExpectError + unflattenShape.assign( [ 6, 2, 1 ], 1, false, [ 0, 0, 0, 0 ] ); // $ExpectError + unflattenShape.assign( [ 6, 2, 1 ], 1, null, [ 0, 0, 0, 0 ] ); // $ExpectError + unflattenShape.assign( [ 6, 2, 1 ], 1, undefined, [ 0, 0, 0, 0 ] ); // $ExpectError + unflattenShape.assign( [ 6, 2, 1 ], 1, '5', [ 0, 0, 0, 0 ] ); // $ExpectError + unflattenShape.assign( [ 6, 2, 1 ], 1, [ '1', '2' ], [ 0, 0, 0, 0 ] ); // $ExpectError + unflattenShape.assign( [ 6, 2, 1 ], 1, {}, [ 0, 0, 0, 0 ] ); // $ExpectError + unflattenShape.assign( [ 6, 2, 1 ], 1, ( x: number ): number => x, [ 0, 0, 0, 0 ] ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a fourth argument which is not an array-like object containing numbers... +{ + unflattenShape.assign( [ 6, 2, 1 ], 1, [ 3, 2 ], true ); // $ExpectError + unflattenShape.assign( [ 6, 2, 1 ], 1, [ 3, 2 ], false ); // $ExpectError + unflattenShape.assign( [ 6, 2, 1 ], 1, [ 3, 2 ], null ); // $ExpectError + unflattenShape.assign( [ 6, 2, 1 ], 1, [ 3, 2 ], undefined ); // $ExpectError + unflattenShape.assign( [ 6, 2, 1 ], 1, [ 3, 2 ], '5' ); // $ExpectError + unflattenShape.assign( [ 6, 2, 1 ], 1, [ 3, 2 ], [ '1', '2' ] ); // $ExpectError + unflattenShape.assign( [ 6, 2, 1 ], 1, [ 3, 2 ], {} ); // $ExpectError + unflattenShape.assign( [ 6, 2, 1 ], 1, [ 3, 2 ], ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an unsupported number of arguments... +{ + unflattenShape.assign(); // $ExpectError + unflattenShape.assign( [ 6, 2, 1 ] ); // $ExpectError + unflattenShape.assign( [ 6, 2, 1 ], 1 ); // $ExpectError + unflattenShape.assign( [ 6, 2, 1 ], 1, [ 3, 2 ] ); // $ExpectError + unflattenShape.assign( [ 6, 2, 1 ], 1, [ 3, 2 ], [ 0, 0, 0, 0 ], {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/examples/index.js new file mode 100644 index 000000000000..1308652521c6 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/examples/index.js @@ -0,0 +1,33 @@ +/** +* @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 unflattenShape = require( './../lib' ); + +var out = unflattenShape( [ 2, 4, 1 ], 1, [ 2, 2 ] ); +console.log( out ); +// => [ 2, 2, 2, 1 ] + +out = unflattenShape( [ 2, 4, 1 ], 1, [ 2, 1, 2 ] ); +console.log( out ); +// => [ 2, 2, 1, 2, 1 ] + +out = unflattenShape( [ 2, 4, 1 ], 1, [ 2, 1, 1, 2 ] ); +console.log( out ); +// => [ 2, 2, 1, 1, 2, 1 ] diff --git a/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/lib/assign.js b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/lib/assign.js new file mode 100644 index 000000000000..f53c05124f7f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/lib/assign.js @@ -0,0 +1,90 @@ +/** +* @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 numel = require( '@stdlib/ndarray/base/numel' ); +var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' ); +var join = require( '@stdlib/array/base/join' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Expands a dimension over multiple dimensions. +* +* @param {NonNegativeIntegerArray} shape - array shape +* @param {integer} dim - dimension to be unflattened +* @param {NonNegativeIntegerArray} sizes - new shape of the unflattened dimension +* @param {NonNegativeIntegerArray} out - output array +* @throws {RangeError} second argument is out-of-bounds +* @throws {RangeError} product of the sizes must be equal to the size of the dimension to be unflattened +* @returns {NonNegativeIntegerArray} unflattened shape +* +* @example +* var o = [ 0, 0, 0, 0 ]; +* +* var out = unflattenShape( [ 6, 2, 1 ], 0, [ 3, 2 ], o ); +* // returns [ 3, 2, 2, 1 ] +* +* var bool = ( out === o ); +* // returns true +*/ +function unflattenShape( shape, dim, sizes, out ) { + var S1; + var S2; + var N; + var d; + var i; + var j; + + S1 = shape.length; + d = normalizeIndex( dim, S1-1 ); + if ( d < 0 ) { + throw new RangeError( format( 'invalid argument. Dimension index exceeds the number of dimensions. Number of dimensions: %d. Value: `%d`.', S1, dim ) ); + } + S2 = sizes.length; + N = numel( sizes ); + if ( N !== shape[ d ] ) { + throw new RangeError( format( 'invalid argument. Product of the sizes must be equal to the size of the dimension to be unflattened. Dimension: %d. Size: %d. Value: `[%s]`.', d, shape[ d ], join( sizes, ', ' ) ) ); + } + for ( i = 0; i < d; i++ ) { + out[ i ] = shape[ i ]; + } + j = 0; + for ( ; i < d+S2; i++ ) { + out[ i ] = sizes[ j ]; + j += 1; + } + j = d + 1; + if ( j < S1 ) { + for ( ; i < S1+S2-1; i++ ) { + out[ i ] = shape[ j ]; + j += 1; + } + } + return out; +} + + +// EXPORTS // + +module.exports = unflattenShape; diff --git a/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/lib/index.js new file mode 100644 index 000000000000..cda38fd25023 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/lib/index.js @@ -0,0 +1,49 @@ +/** +* @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'; + +/** +* Expand a dimension over multiple dimensions. +* +* @module @stdlib/ndarray/base/unflatten-shape +* +* @example +* var unflattenShape = require( '@stdlib/ndarray/base/unflatten-shape' ); +* +* var sh = unflattenShape( [ 6, 2, 1 ], 0, [ 3, 2 ] ); +* // returns [ 3, 2, 2, 1 ] +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './main.js' ); +var assign = require( './assign.js' ); + + +// MAIN // + +setReadOnly( main, 'assign', assign ); + + +// EXPORTS // + +module.exports = main; + +// exports: { "assign": "main.assign" } diff --git a/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/lib/main.js new file mode 100644 index 000000000000..1b0c1f8c2414 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/lib/main.js @@ -0,0 +1,52 @@ +/** +* @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 zeros = require( '@stdlib/array/base/zeros' ); +var assign = require( './assign.js' ); + + +// MAIN // + +/** +* Expands a dimension over multiple dimensions. +* +* @param {NonNegativeIntegerArray} shape - array shape +* @param {integer} dim - dimension to be unflattened +* @param {NonNegativeIntegerArray} sizes - new shape of the unflattened dimension +* @throws {RangeError} second argument is out-of-bounds +* @throws {RangeError} product of the sizes must be equal to the size of the dimension to be unflattened +* @returns {NonNegativeIntegerArray} unflattened shape +* +* @example +* var sh = unflattenShape( [ 6, 2, 1 ], 0, [ 3, 2 ] ); +* // returns [ 3, 2, 2, 1 ] +*/ +function unflattenShape( shape, dim, sizes ) { + var out = zeros( shape.length + sizes.length - 1 ); + assign( shape, dim, sizes, out ); + return out; +} + + +// EXPORTS // + +module.exports = unflattenShape; diff --git a/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/package.json b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/package.json new file mode 100644 index 000000000000..3e35ce17a032 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/package.json @@ -0,0 +1,68 @@ +{ + "name": "@stdlib/ndarray/base/unflatten-shape", + "version": "0.0.0", + "description": "Expand a dimension over multiple 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", + "shape", + "unflatten", + "reshape", + "multidimensional", + "array", + "utilities", + "utility", + "utils", + "util" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/test/test.assign.js b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/test/test.assign.js new file mode 100644 index 000000000000..0c22d0b0194f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/test/test.assign.js @@ -0,0 +1,185 @@ +/** +* @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 assign = require( './../lib/assign.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof assign, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a second argument which is out-of-bounds', function test( t ) { + var values; + var shape; + var sizes; + var out; + var i; + + shape = [ 4, 2 ]; + sizes = [ 2, 2 ]; + out = [ 0, 0, 0 ]; + + values = [ + -3, + -4, + 2, + 3, + 4 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + assign( shape, value, sizes, out ); + }; + } +}); + +tape( 'the function throws an error if provided a third argument which is incompatible with the size of a specified dimension', function test( t ) { + var values; + var shape; + var sizes; + var out; + var i; + + shape = [ 4, 2 ]; + sizes = [ 2, 2 ]; + out = [ 0, 0, 0 ]; + + values = [ + 0, + 1, + 2, + 3, + 5, + 6 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sizes[ 0 ] = value; + assign( shape, 0, value, out ); + }; + } +}); + +tape( 'the function expands a dimension over multiple dimensions and assigns results to an output array', function test( t ) { + var expected; + var actual; + var shape; + var sizes; + var out; + + shape = [ 4, 2 ]; + sizes = [ 2, 2 ]; + expected = [ 2, 2, 2 ]; + out = [ 0, 0, 0 ]; + actual = assign( shape, 0, sizes, out ); + + t.strictEqual( out, actual, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + shape = [ 4, 2 ]; + sizes = [ 2, 2 ]; + expected = [ 2, 2, 2 ]; + out = [ 0, 0, 0 ]; + actual = assign( shape, -2, sizes, out ); + + t.strictEqual( out, actual, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + shape = [ 2, 1, 10 ]; + sizes = [ 2, 5 ]; + expected = [ 2, 1, 2, 5 ]; + out = [ 0, 0, 0, 0 ]; + actual = assign( shape, 2, sizes, out ); + + t.strictEqual( out, actual, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + shape = [ 2, 1, 10 ]; + sizes = [ 2, 5 ]; + expected = [ 2, 1, 2, 5 ]; + out = [ 0, 0, 0, 0 ]; + actual = assign( shape, -1, sizes, out ); + + t.strictEqual( out, actual, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + shape = [ 1, 4, 3, 2 ]; + sizes = [ 1, 3 ]; + expected = [ 1, 4, 1, 3, 2 ]; + out = [ 0, 0, 0, 0, 0 ]; + actual = assign( shape, 2, sizes, out ); + + t.strictEqual( out, actual, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + shape = [ 1, 4, 3, 2 ]; + sizes = [ 1, 3 ]; + expected = [ 1, 4, 1, 3, 2 ]; + out = [ 0, 0, 0, 0, 0 ]; + actual = assign( shape, -2, sizes, out ); + + t.strictEqual( out, actual, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + shape = [ 12 ]; + sizes = [ 2, 2, 3 ]; + expected = [ 2, 2, 3 ]; + out = [ 0, 0, 0 ]; + actual = assign( shape, 0, sizes, out ); + + t.strictEqual( out, actual, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + shape = [ 12 ]; + sizes = [ 2, 2, 3 ]; + expected = [ 2, 2, 3 ]; + out = [ 0, 0, 0 ]; + actual = assign( shape, -1, sizes, out ); + + t.strictEqual( out, actual, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/test/test.js b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/test/test.js new file mode 100644 index 000000000000..b3adc48dacb9 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/test/test.js @@ -0,0 +1,39 @@ +/** +* @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 isMethod = require( '@stdlib/assert/is-method' ); +var unflattenShape = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof unflattenShape, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( isMethod( unflattenShape, 'assign' ), true, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/test/test.main.js b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/test/test.main.js new file mode 100644 index 000000000000..700ab50c4389 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/test/test.main.js @@ -0,0 +1,173 @@ +/** +* @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 isArray = require( '@stdlib/assert/is-array' ); +var unflattenShape = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof unflattenShape, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a second argument which is out-of-bounds', function test( t ) { + var values; + var shape; + var sizes; + var i; + + shape = [ 4, 2 ]; + sizes = [ 2, 2 ]; + + values = [ + -3, + -4, + 2, + 3, + 4 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + unflattenShape( shape, value, sizes ); + }; + } +}); + +tape( 'the function throws an error if provided a third argument which is incompatible with the size of a specified dimension', function test( t ) { + var values; + var shape; + var sizes; + var i; + + shape = [ 4, 2 ]; + sizes = [ 2, 2 ]; + + values = [ + 0, + 1, + 2, + 3, + 5, + 6 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sizes[ 0 ] = value; + unflattenShape( shape, 0, value ); + }; + } +}); + +tape( 'the function expands a dimension over multiple dimensions', function test( t ) { + var expected; + var actual; + var shape; + var sizes; + + shape = [ 4, 2 ]; + sizes = [ 2, 2 ]; + expected = [ 2, 2, 2 ]; + actual = unflattenShape( shape, 0, sizes ); + + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + shape = [ 4, 2 ]; + sizes = [ 2, 2 ]; + expected = [ 2, 2, 2 ]; + actual = unflattenShape( shape, -2, sizes ); + + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + shape = [ 2, 1, 10 ]; + sizes = [ 2, 5 ]; + expected = [ 2, 1, 2, 5 ]; + actual = unflattenShape( shape, 2, sizes ); + + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + shape = [ 2, 1, 10 ]; + sizes = [ 2, 5 ]; + expected = [ 2, 1, 2, 5 ]; + actual = unflattenShape( shape, -1, sizes ); + + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + shape = [ 1, 4, 3, 2 ]; + sizes = [ 1, 3 ]; + expected = [ 1, 4, 1, 3, 2 ]; + actual = unflattenShape( shape, 2, sizes ); + + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + shape = [ 1, 4, 3, 2 ]; + sizes = [ 1, 3 ]; + expected = [ 1, 4, 1, 3, 2 ]; + actual = unflattenShape( shape, -2, sizes ); + + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + shape = [ 12 ]; + sizes = [ 2, 2, 3 ]; + expected = [ 2, 2, 3 ]; + actual = unflattenShape( shape, 0, sizes ); + + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + shape = [ 12 ]; + sizes = [ 2, 2, 3 ]; + expected = [ 2, 2, 3 ]; + actual = unflattenShape( shape, -1, sizes ); + + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +});