diff --git a/lib/node_modules/@stdlib/array/base/reshape/README.md b/lib/node_modules/@stdlib/array/base/reshape/README.md new file mode 100644 index 000000000000..e747f6cc0fc6 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/reshape/README.md @@ -0,0 +1,122 @@ + + +# reshape + +> Reshape a nested array into another nested array having a desired shape. + +
+ +## Usage + +```javascript +var reshape = require( '@stdlib/array/base/reshape' ); +``` + +#### reshape( x, fromShape, toShape, colexicographic ) + +Reshapes a nested array into another nested array having a desired shape. + +```javascript +var x = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]; + +var out = reshape( x, [ 2, 3 ], [ 3, 2 ], false ); +// returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] +``` + +- **x**: input array. +- **fromShape**: input array shape. +- **toShape**: output array shape. +- **colexicographic**: boolean indicating whether to reshape the array in colexicographic order. + +To reshape in colexicographic order, set the `colexicographic` argument to `true`. + +```javascript +var x = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]; + +var out = reshape( x, [ 2, 3 ], [ 3, 2 ], true ); +// [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ] +``` + +
+ + + +
+ +## Notes + +- The function assumes that `fromShape` and `toShape` describe arrays having the same number of elements. + +
+ + + +
+ +## Examples + + + +```javascript +var reshape = require( '@stdlib/array/base/reshape' ); + +var x = [ + [ 1, 2, 3, 4 ], + [ 5, 6, 7, 8 ], + [ 9, 10, 11, 12 ] +]; + +var out = reshape( x, [ 3, 4 ], [ 4, 3 ], false ); +// returns [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ], [ 10, 11, 12 ] ] + +out = reshape( x, [ 3, 4 ], [ 6, 2 ], false ); +// returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ], [ 9, 10 ], [ 11, 12 ] ] + +out = reshape( x, [ 3, 4 ], [ 1, 12 ], false ); +// returns [ [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ] + +out = reshape( x, [ 3, 4 ], [ 12, 1 ], false ); +// returns [ [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ], [ 6 ], [ 7 ], [ 8 ], [ 9 ], [ 10 ], [ 11 ], [ 12 ] ] +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/array/base/reshape/benchmark/benchmark.js b/lib/node_modules/@stdlib/array/base/reshape/benchmark/benchmark.js new file mode 100644 index 000000000000..bfb7e88f8ad3 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/reshape/benchmark/benchmark.js @@ -0,0 +1,206 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 onesnd = require( '@stdlib/array/base/onesnd' ); +var pkg = require( './../package.json' ).name; +var reshape = require( './../lib' ); + + +// MAIN // + +bench( pkg+':ndims=2,size=100,lexicographic', function benchmark( b ) { + var x; + var i; + var v; + + x = onesnd( [ 10, 10 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = reshape( x, [ 10, 10 ], [ 1, 100 ], false ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( v ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':ndims=2,size=100,colexicographic', function benchmark( b ) { + var x; + var i; + var v; + + x = onesnd( [ 10, 10 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = reshape( x, [ 10, 10 ], [ 1, 100 ], true ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( v ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':ndims=3,size=100,lexicographic', function benchmark( b ) { + var x; + var i; + var v; + + x = onesnd( [ 4, 5, 5 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = reshape( x, [ 4, 5, 5 ], [ 1, 1, 100 ], false ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( v ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':ndims=3,size=100,colexicographic', function benchmark( b ) { + var x; + var i; + var v; + + x = onesnd( [ 4, 5, 5 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = reshape( x, [ 4, 5, 5 ], [ 1, 1, 100 ], true ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( v ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':ndims=4,size=100,lexicographic', function benchmark( b ) { + var x; + var i; + var v; + + x = onesnd( [ 5, 2, 5, 2 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = reshape( x, [ 5, 2, 5, 2 ], [ 1, 1, 1, 100 ], false ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( v ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':ndims=4,size=100,colexicographic', function benchmark( b ) { + var x; + var i; + var v; + + x = onesnd( [ 5, 2, 5, 2 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = reshape( x, [ 5, 2, 5, 2 ], [ 1, 1, 1, 100 ], true ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( v ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':ndims=5,size=100,lexicographic', function benchmark( b ) { + var x; + var i; + var v; + + x = onesnd( [ 5, 2, 1, 5, 2 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = reshape( x, [ 5, 2, 1, 5, 2 ], [ 1, 1, 1, 1, 100 ], false ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( v ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':ndims=5,size=100,colexicographic', function benchmark( b ) { + var x; + var i; + var v; + + x = onesnd( [ 5, 2, 1, 5, 2 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = reshape( x, [ 5, 2, 1, 5, 2 ], [ 1, 1, 1, 1, 100 ], true ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( v ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/base/reshape/docs/repl.txt b/lib/node_modules/@stdlib/array/base/reshape/docs/repl.txt new file mode 100644 index 000000000000..a01bac3a5083 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/reshape/docs/repl.txt @@ -0,0 +1,35 @@ + +{{alias}}( x, fromShape, toShape, colexicographic ) + Reshapes a nested array into another nested array having a desired shape. + + Parameters + ---------- + x: Array + Input n-dimensional nested array. + + fromShape: Array + Input array shape. + + toShape: Array + Output array shape. + + colexicographic: boolean + Specifies whether to reshape the array in colexicographic order. + + Returns + ------- + out: Array + Output array. + + Examples + -------- + > var x = [ [ 1, 2 ], [ 3, 4 ] ]; + > var out = {{alias}}( x, [ 2, 2 ], [ 4, 1 ], false ) + [ [ 1 ], [ 2 ], [ 3 ], [ 4 ] ] + + > var x = [ [ 1, 2 ], [ 3, 4 ] ]; + > var out = {{alias}}( x, [ 2, 2 ], [ 4, 1 ], true ) + [ [ 1 ], [ 3 ], [ 2 ], [ 4 ] ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/array/base/reshape/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/base/reshape/docs/types/index.d.ts new file mode 100644 index 000000000000..40bf2afd07ab --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/reshape/docs/types/index.d.ts @@ -0,0 +1,55 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 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 { Array1D, Array2D, Array3D, Array4D, Array5D, Array6D, Array7D, Array8D, Array9D, Array10D } from '@stdlib/types/array'; +import { Shape } from '@stdlib/types/ndarray'; + +/** +* Nested array. +*/ +type ArrayND = Array1D | Array2D | Array3D | Array4D | Array5D | Array6D | Array7D | Array8D | Array9D | Array10D; // WARNING: arbitrarily limited to 10 dimensions, which should be fine for most practical purposes + +/** +* Reshape a nested array into another nested array having a desired shape. +* +* ## Notes +* +* - The function assumes that `fromShape` and `toShape` describe arrays have same the number of elements. +* +* @param x - input nested array +* @param fromShape - shape of the input array +* @param toShape - shape of the output array +* @param colexicographic - specifies whether to reshape the array in colexicographic order +* @returns output nested array +* +* @example +* var x = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]; +* +* var out = reshape( x, [ 2, 3 ], [ 3, 2 ], false ); +* // returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] +*/ +declare function reshape( x: ArrayND, fromShape: Shape, toShape: Shape, colexicographic: boolean ): ArrayND; + + +// EXPORTS // + +export = reshape; diff --git a/lib/node_modules/@stdlib/array/base/reshape/docs/types/test.ts b/lib/node_modules/@stdlib/array/base/reshape/docs/types/test.ts new file mode 100644 index 000000000000..099c56515b9a --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/reshape/docs/types/test.ts @@ -0,0 +1,97 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 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 reshape = require( './index' ); + + +// TESTS // + +// The function returns an array... +{ + const x = [ [ 1, 2 ], [ 3, 4 ] ]; + + reshape( x, [ 2, 2 ], [ 4, 1 ], false ); // $ExpectType ArrayND + reshape( x, [ 2, 2 ], [ 4, 1 ], true ); // $ExpectType ArrayND + + reshape( [ [ '1', '2' ], [ '3', '4' ] ], [ 2, 2 ], [ 4, 1 ], false ); // $ExpectType ArrayND + reshape( [ [ '1', '2' ], [ '3', '4' ] ], [ 2, 2 ], [ 4, 1 ], true ); // $ExpectType ArrayND +} + +// The compiler throws an error if the function is provided a first argument which is not an array-like object... +{ + reshape( 1, [ 2, 2 ], [ 4, 1 ], false ); // $ExpectError + reshape( true, [ 2, 2 ], [ 4, 1 ], false ); // $ExpectError + reshape( false, [ 2, 2 ], [ 4, 1 ], false ); // $ExpectError + reshape( null, [ 2, 2 ], [ 4, 1 ], false ); // $ExpectError + reshape( void 0, [ 2, 2 ], [ 4, 1 ], false ); // $ExpectError + reshape( {}, [ 2, 2 ], [ 4, 1 ], false ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... +{ + const x = [ [ 1, 2 ], [ 3, 4 ] ]; + + reshape( x, '', [ 4, 1 ], false ); // $ExpectError + reshape( x, 1, [ 4, 1 ], false ); // $ExpectError + reshape( x, true, [ 4, 1 ], false ); // $ExpectError + reshape( x, false, [ 4, 1 ], false ); // $ExpectError + reshape( x, null, [ 4, 1 ], false ); // $ExpectError + reshape( x, void 0, [ 4, 1 ], false ); // $ExpectError + reshape( x, {}, [ 4, 1 ], false ); // $ExpectError + reshape( x, [ 1, '2', 3 ], [ 4, 1 ], false ); // $ExpectError + reshape( x, ( x: number ): number => x, [ 4, 1 ], false ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not an array-like object containing numbers... +{ + const x = [ [ 1, 2 ], [ 3, 4 ] ]; + + reshape( x, [ 2, 2 ], '', false ); // $ExpectError + reshape( x, [ 2, 2 ], 1, false ); // $ExpectError + reshape( x, [ 2, 2 ], true, false ); // $ExpectError + reshape( x, [ 2, 2 ], false, false ); // $ExpectError + reshape( x, [ 2, 2 ], null, false ); // $ExpectError + reshape( x, [ 2, 2 ], void 0, false ); // $ExpectError + reshape( x, [ 2, 2 ], {}, false ); // $ExpectError + reshape( x, [ 2, 2 ], [ 4, '1' ], false ); // $ExpectError + reshape( x, [ 2, 2 ], ( x: number ): number => x, false ); // $ExpectError +} + +// The compiler throws an error if the function is provided a fourth argument which is not a boolean... +{ + const x = [ [ 1, 2 ], [ 3, 4 ] ]; + + reshape( x, [ 2, 2 ], [ 4, 1 ], '' ); // $ExpectError + reshape( x, [ 2, 2 ], [ 4, 1], 1 ); // $ExpectError + reshape( x, [ 2, 2 ], [ 4, 1], null ); // $ExpectError + reshape( x, [ 2, 2 ], [ 4, 1], void 0 ); // $ExpectError + reshape( x, [ 2, 2 ], [ 4, 1], {} ); // $ExpectError + reshape( x, [ 2, 2 ], [ 4, 1], [] ); // $ExpectError + reshape( x, [ 2, 2 ], [ 4, 1], ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = [ [ 1, 2 ], [ 3, 4 ] ]; + + reshape(); // $ExpectError + reshape( x ); // $ExpectError + reshape( x, [ 2, 2 ] ); // $ExpectError + reshape( x, [ 2, 2 ], [ 4, 1 ] ); // $ExpectError + reshape( x, [ 2, 2 ], [ 4, 1 ], false, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/array/base/reshape/examples/index.js b/lib/node_modules/@stdlib/array/base/reshape/examples/index.js new file mode 100644 index 000000000000..af5b88494084 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/reshape/examples/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 reshape = require( './../lib' ); + +var x = [ + [ 1, 2, 3, 4 ], + [ 5, 6, 7, 8 ], + [ 9, 10, 11, 12 ] +]; + +var out = reshape( x, [ 3, 4 ], [ 4, 3 ], false ); +console.log( out ); +// => [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ], [ 10, 11, 12 ] ] + +out = reshape( x, [ 3, 4 ], [ 6, 2 ], false ); +console.log( out ); +// => [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ], [ 9, 10 ], [ 11, 12 ] ] + +out = reshape( x, [ 3, 4 ], [ 1, 12 ], false ); +console.log( out ); +// => [ [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ] + +out = reshape( x, [ 3, 4 ], [ 12, 1 ], false ); +console.log( out ); +// => [ [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ], [ 6 ], [ 7 ], [ 8 ], [ 9 ], [ 10 ], [ 11 ], [ 12 ] ] diff --git a/lib/node_modules/@stdlib/array/base/reshape/lib/index.js b/lib/node_modules/@stdlib/array/base/reshape/lib/index.js new file mode 100644 index 000000000000..8b01ceb84f23 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/reshape/lib/index.js @@ -0,0 +1,42 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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'; + +/** +* Reshape a nested array into another nested array having a desired shape. +* +* @module @stdlib/array/base/reshape +* +* @example +* var reshape = require( '@stdlib/array/base/reshape' ); +* +* var x = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]; +* +* var out = reshape( x, [ 2, 3 ], [ 3, 2 ], false ); +* // returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/array/base/reshape/lib/main.js b/lib/node_modules/@stdlib/array/base/reshape/lib/main.js new file mode 100644 index 000000000000..9483f6c2eadf --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/reshape/lib/main.js @@ -0,0 +1,94 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 flatten = require( '@stdlib/array/base/flatten' ); +var slice = require( '@stdlib/array/base/slice' ); + + +// FUNCTIONS // + +/** +* Recursive reshapes an array. +* +* @private +* @param {Array} array - input n-dimensional nested array +* @param {NonNegativeInteger} ndims - number of dimensions +* @param {NonNegativeIntegerArray} shape - array shape +* @param {NonNegativeInteger} dim - dimension index +* @param {NonNegativeInteger} index - sub-array index +* @returns {Array} output m-dimensional nested array +*/ +function recurse( array, ndims, shape, dim, index ) { + var stepSize; + var subArray; + var out; + var d; + var S; + var i; + + S = shape[ dim ]; + if ( dim === ndims - 1 ) { + return slice( array, index, index + S ); + } + + d = dim + 1; + stepSize = 1; + for ( i = d; i < shape.length; i++ ) { + stepSize *= shape[ i ]; + } + + out = []; + for ( i = 0; i < S; i++ ) { + subArray = recurse( array, ndims, shape, d, index ); + out.push( subArray ); + index += stepSize; + } + return out; +} + + +// MAIN // + +/** +* Reshape a nested array into another nested array having a desired shape. +* +* @param {Array} x - input nested array +* @param {NonNegativeIntegerArray} fromShape - shape of the input array +* @param {NonNegativeIntegerArray} toShape - shape of the output array +* @param {boolean} colexicographic - specifies whether to reshape the array in colexicographic order +* @returns {Array} output nested array +* +* @example +* var x = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]; +* +* var out = reshape( x, [ 2, 3 ], [ 3, 2 ], false ); +* // returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] +*/ +function reshape( x, fromShape, toShape, colexicographic ) { + var f = flatten( x, fromShape, colexicographic ); + return recurse( f, toShape.length, toShape, 0, 0 ); +} + + +// EXPORTS // + +module.exports = reshape; diff --git a/lib/node_modules/@stdlib/array/base/reshape/package.json b/lib/node_modules/@stdlib/array/base/reshape/package.json new file mode 100644 index 000000000000..5f754287b4ff --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/reshape/package.json @@ -0,0 +1,64 @@ +{ + "name": "@stdlib/array/base/reshape", + "version": "0.0.0", + "description": "Reshape a nested array into another nested array having a desired 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", + "data", + "structure", + "array", + "generic", + "reshape", + "nested", + "transform", + "shape" + ] +} diff --git a/lib/node_modules/@stdlib/array/base/reshape/test/test.js b/lib/node_modules/@stdlib/array/base/reshape/test/test.js new file mode 100644 index 000000000000..aeafec2350bd --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/reshape/test/test.js @@ -0,0 +1,457 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 reshape = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof reshape, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns a reshaped nested array (2d, lexicographic)', function test( t ) { + var expected; + var actual; + var x; + + x = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]; + + expected = [ 1, 2, 3, 4, 5, 6 ]; + actual = reshape( x, [ 2, 3 ], [ 6 ], false ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ]; + actual = reshape( x, [ 2, 3 ], [ 3, 2 ], false ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ [ 1, 2, 3, 4, 5, 6 ] ]; + actual = reshape( x, [ 2, 3 ], [ 1, 6 ], false ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] ]; + actual = reshape( x, [ 2, 3 ], [ 1, 3, 2 ], false ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ [ [ [ 1, 2 ] ], [ [ 3, 4 ] ], [ [ 5, 6 ] ] ] ]; + actual = reshape( x, [ 2, 3 ], [ 1, 3, 1, 2 ], false ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a reshaped nested array (3d, lexicographic)', function test( t ) { + var expected; + var actual; + var x; + + x = [ + [ + [ 1, 2 ], + [ 3, 4 ] + ], + [ + [ 5, 6 ], + [ 7, 8 ] + ] + ]; + + expected = [ 1, 2, 3, 4, 5, 6, 7, 8 ]; + actual = reshape( x, [ 2, 2, 2 ], [ 8 ], false ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ] ]; + actual = reshape( x, [ 2, 2, 2 ], [ 4, 2 ], false ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ + [ + [ 1, 2 ] + ], + [ + [ 3, 4 ] + ], + [ + [ 5, 6 ] + ], + [ + [ 7, 8 ] + ] + ]; + actual = reshape( x, [ 2, 2, 2 ], [ 4, 1, 2 ], false ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ + [ + [ 1, 2, 3, 4 ] + ], + [ + [ 5, 6, 7, 8 ] + ] + ]; + actual = reshape( x, [ 2, 2, 2 ], [ 2, 1, 4 ], false ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ + [ + [ 1, 2 ], + [ 3, 4 ], + [ 5, 6 ], + [ 7, 8 ] + ] + ]; + actual = reshape( x, [ 2, 2, 2 ], [ 1, 4, 2 ], false ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ + [ + [ + [ 1, 2 ], + [ 3, 4 ], + [ 5, 6 ], + [ 7, 8 ] + ] + ] + ]; + actual = reshape( x, [ 2, 2, 2 ], [ 1, 1, 4, 2 ], false ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a filled nested array (4d, lexicographic)', function test( t ) { + var expected; + var actual; + var x; + + x = [ + [ + [ + [ 1, 2, 3, 4 ] + ], + [ + [ 5, 6, 7, 8 ] + ] + ] + ]; + + expected = [ 1, 2, 3, 4, 5, 6, 7, 8 ]; + actual = reshape( x, [ 1, 2, 1, 4 ], [ 8 ], false ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ] ]; + actual = reshape( x, [ 1, 2, 1, 4 ], [ 2, 4 ], false ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ + [ + [ 1, 2, 3, 4 ], + [ 5, 6, 7, 8 ] + ] + ]; + actual = reshape( x, [ 1, 2, 1, 4 ], [ 1, 2, 4 ], false ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ + [ + [ + [ 1, 2 ], + [ 3, 4 ] + ], + [ + [ 5, 6 ], + [ 7, 8 ] + ] + ] + ]; + actual = reshape( x, [ 1, 2, 1, 4 ], [ 1, 2, 2, 2 ], false ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ + [ + [ + [ 1, 2 ], + [ 3, 4 ] + ] + ], + [ + [ + [ 5, 6 ], + [ 7, 8 ] + ] + ] + ]; + actual = reshape( x, [ 1, 2, 1, 4 ], [ 2, 1, 2, 2 ], false ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ + [ + [ + [ 1 ], + [ 2 ] + ], + [ + [ 3 ], + [ 4 ] + ] + ], + [ + [ + [ 5 ], + [ 6 ] + ], + [ + [ 7 ], + [ 8 ] + ] + ] + ]; + actual = reshape( x, [ 1, 2, 1, 4 ], [ 2, 2, 2, 1 ], false ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a reshaped nested array (2d, colexicographic)', function test( t ) { + var expected; + var actual; + var x; + + x = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]; + + expected = [ 1, 4, 2, 5, 3, 6 ]; + actual = reshape( x, [ 2, 3 ], [ 6 ], true ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]; + actual = reshape( x, [ 2, 3 ], [ 3, 2 ], true ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ [ 1, 4, 2, 5, 3, 6 ] ]; + actual = reshape( x, [ 2, 3 ], [ 1, 6 ], true ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ + [ + [ 1, 4, 2 ], + [ 5, 3, 6 ] + ] + ]; + actual = reshape( x, [ 2, 3 ], [ 1, 2, 3 ], true ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ + [ + [ + [ 1, 4, 2 ] + ], + [ + [ 5, 3, 6 ] + ] + ] + ]; + actual = reshape( x, [ 2, 3 ], [ 1, 2, 1, 3 ], true ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a reshaped nested array (3d, colexicographic)', function test( t ) { + var expected; + var actual; + var x; + + x = [ + [ + [ 1, 2 ], + [ 3, 4 ] + ], + [ + [ 5, 6 ], + [ 7, 8 ] + ] + ]; + + expected = [ 1, 5, 3, 7, 2, 6, 4, 8]; + actual = reshape( x, [ 2, 2, 2 ], [ 8 ], true ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ [ 1, 5, 3, 7 ], [ 2, 6, 4, 8 ] ]; + actual = reshape( x, [ 2, 2, 2 ], [ 2, 4 ], true ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ + [ + [ 1, 5 ] + ], + [ + [ 3, 7 ] + ], + [ + [ 2, 6 ] + ], + [ + [ 4, 8 ] + ] + ]; + actual = reshape( x, [ 2, 2, 2 ], [ 4, 1, 2 ], true ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ + [ + [ 1, 5, 3, 7 ] + ], + [ + [ 2, 6, 4, 8 ] + ] + ]; + actual = reshape( x, [ 2, 2, 2 ], [ 2, 1, 4 ], true ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ + [ + [ 1, 5 ], + [ 3, 7 ], + [ 2, 6 ], + [ 4, 8 ] + ] + ]; + actual = reshape( x, [ 2, 2, 2 ], [ 1, 4, 2 ], true ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ + [ + [ + [ 1, 5 ] + ], + [ + [ 3, 7 ] + ], + [ + [ 2, 6 ] + ], + [ + [ 4, 8 ] + ] + ] + ]; + actual = reshape( x, [ 2, 2, 2 ], [ 1, 4, 1, 2 ], true ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a filled nested array (4d, colexicographic)', function test( t ) { + var expected; + var actual; + var x; + + x = [ + [ + [ + [ 1, 2, 3, 4 ] + ], + [ + [ 5, 6, 7, 8 ] + ] + ] + ]; + + expected = [ 1, 5, 2, 6, 3, 7, 4, 8 ]; + actual = reshape( x, [ 1, 2, 1, 4 ], [ 8 ], true ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ [ 1, 5, 2, 6 ], [ 3, 7, 4, 8 ] ]; + actual = reshape( x, [ 1, 2, 1, 4 ], [ 2, 4 ], true ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ + [ + [ 1, 5, 2, 6 ], + [ 3, 7, 4, 8 ] + ] + ]; + actual = reshape( x, [ 1, 2, 1, 4 ], [ 1, 2, 4 ], true ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ + [ + [ + [ 1, 5 ], + [ 2, 6 ] + ], + [ + [ 3, 7 ], + [ 4, 8 ] + ] + ] + ]; + actual = reshape( x, [ 1, 2, 1, 4 ], [ 1, 2, 2, 2 ], true ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ + [ + [ + [ 1, 5 ], + [ 2, 6 ] + ] + ], + [ + [ + [ 3, 7 ], + [ 4, 8 ] + ] + ] + ]; + actual = reshape( x, [ 1, 2, 1, 4 ], [ 2, 1, 2, 2 ], true ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ + [ + [ + [ 1 ], + [ 5 ] + ], + [ + [ 2 ], + [ 6 ] + ] + ], + [ + [ + [ 3 ], + [ 7 ] + ], + [ + [ 4 ], + [ 8 ] + ] + ] + ]; + actual = reshape( x, [ 1, 2, 1, 4 ], [ 2, 2, 2, 1 ], true ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +});