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..7661b8d9de8e --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/README.md @@ -0,0 +1,134 @@ + + +# unflattenShape + +> Unflatten a shape over multiple dimensions. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var unflattenShape = require( '@stdlib/ndarray/base/unflatten-shape' ); +``` + +#### unflattenShape( shape, dim, sizes ) + +Unflattens a shape 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. +- **sizes**: new shape of the unflattened dimension. + +#### unflattenShape.assign( shape, dim, sizes, out ) + +Unflattens a shape 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. +- **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 ] + +console.log( 'Unflattened Shape: ', out ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + 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..3a4c3db6a3dd --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/docs/repl.txt @@ -0,0 +1,63 @@ + +{{alias}}( shape, dim, sizes ) + Unflattens a shape over multiple dimensions. + + Parameters + ---------- + shape: ArrayLike + Array shape. + + dim: integer + Dimension to be unflattened. + + 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 ) + Unflattens a shape over multiple dimensions and assigns results to a + provided output array. + + Parameters + ---------- + shape: ArrayLike + Array shape. + + dim: integer + Dimension to be unflattened. + + 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..a898d3589f28 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/docs/types/index.d.ts @@ -0,0 +1,88 @@ +/* +* @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 + +/// + +/** +* Interface describing `unflattenShape` +*/ +interface Routine { + /** + * Unflattens a shape 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: Array, dim: number, sizes: Array ): Array; + + /** + * Unflattens a shape 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( shape: Array, dim: number, sizes: Array, out: Array ): Array; +} + +/** +* Unflattens a shape 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..a717baf520b2 --- /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 that 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 that 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 that 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 that 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 that 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 that 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 that 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..ce65d138d2c5 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/examples/index.js @@ -0,0 +1,26 @@ +/** +* @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 ] ); +// returns [ 2, 2, 2, 1 ] + +console.log( 'Unflattened Shape: ', out ); 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..f09780db2f45 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/lib/assign.js @@ -0,0 +1,78 @@ +/** +* @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 flattenShape = require( '@stdlib/ndarray/base/flatten-shape' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Unflattens a shape over multiple dimensions. +* +* @param {NonNegativeIntegerArray} shape - array shape +* @param {NonNegativeInteger} dim - dimension to be unflattened +* @param {NonNegativeIntegerArray} sizes - new shape of the unflattened dimension +* @param {NonNegativeIntegerArray} out - output array +* @throws {RangeError} - product of the sizes array must be equal to 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 s; + var i; + var j; + var k; + + s = flattenShape( sizes, sizes.length-1 ); + if ( shape[ dim ] !== s[ 0 ] ) { + throw new RangeError( format( 'invalid argument. Product of the sizes array must be equal to the dimension to be unflattened. Dim: `%d`. Product: `%d`.', shape[dim], s[0] ) ); + } + for ( i = 0; i < dim; i++ ) { + out[ i ] = shape[ i ]; + } + j = 0; + k = dim + sizes.length - 1; + for ( i = dim; i <= k; i++ ) { + out[ i ] = sizes[ j ]; + j += 1; + } + j = shape.length - 1; + for ( i = out.length-1; i > k; 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..de35a6a31a7c --- /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'; + +/** +* Unflatten a shape 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..1cb72f589960 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/lib/main.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'; + +// MODULES // + +var zeros = require( '@stdlib/array/base/zeros' ); +var assign = require( './assign.js' ); + + +// MAIN // + +/** +* Unflattens a shape over multiple dimensions. +* +* @param {NonNegativeIntegerArray} shape - array shape +* @param {NonNegativeInteger} dim - dimension to be unflattened +* @param {NonNegativeIntegerArray} sizes - new shape of the unflattened dimension +* @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..ce7134828c01 --- /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": "Unflatten a shape 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..72271c1cc191 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/test/test.assign.js @@ -0,0 +1,83 @@ +/** +* @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 un-flattens a shape 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 length' ); + 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 length' ); + 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 length' ); + 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 length' ); + 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..edb80d01fac4 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/test/test.js @@ -0,0 +1,64 @@ +/** +* @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(); +}); + +tape( 'the function throws an error if provided a `sizes` argument whose product does not equal the size of the dimension to un-flatten', function test( t ) { + var values; + var i; + + values = [ + [ 2, 5 ], + [ 1, 3 ], + [ 3, 2, 1 ], + [ 0 ], + [ 1 ], + [ 1, 2, 3 ] + ]; + + 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( [ 6, 2, 1 ], 1, value ); + }; + } +}); 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..8d085356be34 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/unflatten-shape/test/test.main.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 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 un-flattens a shape 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 length' ); + 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 length' ); + 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 length' ); + 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 length' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +});