From 079eeae413a3c0f3bb8c0a7f32dfbeafd062a2ab Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sat, 1 Mar 2025 19:01:02 +0000 Subject: [PATCH 1/9] feat: add array/base/reshape --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: passed --- --- .../@stdlib/array/base/reshape/README.md | 117 ++++++ .../array/base/reshape/benchmark/benchmark.js | 206 +++++++++++ .../@stdlib/array/base/reshape/docs/repl.txt | 36 ++ .../array/base/reshape/docs/types/index.d.ts | 57 +++ .../array/base/reshape/docs/types/test.ts | 97 +++++ .../array/base/reshape/examples/index.js | 43 +++ .../@stdlib/array/base/reshape/lib/index.js | 42 +++ .../@stdlib/array/base/reshape/lib/main.js | 99 +++++ .../@stdlib/array/base/reshape/package.json | 64 ++++ .../@stdlib/array/base/reshape/test/test.js | 337 ++++++++++++++++++ 10 files changed, 1098 insertions(+) create mode 100644 lib/node_modules/@stdlib/array/base/reshape/README.md create mode 100644 lib/node_modules/@stdlib/array/base/reshape/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/array/base/reshape/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/array/base/reshape/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/array/base/reshape/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/array/base/reshape/examples/index.js create mode 100644 lib/node_modules/@stdlib/array/base/reshape/lib/index.js create mode 100644 lib/node_modules/@stdlib/array/base/reshape/lib/main.js create mode 100644 lib/node_modules/@stdlib/array/base/reshape/package.json create mode 100644 lib/node_modules/@stdlib/array/base/reshape/test/test.js 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..2d845807bb87 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/reshape/README.md @@ -0,0 +1,117 @@ + + +# reshape + +> Reshape an n-dimensional nested array into another m-dimensional having a desired shape. + +
+ +## Usage + +```javascript +var reshape = require( '@stdlib/array/base/reshape' ); +``` + +#### reshape( x, fromShape, toShape, colexicographic ) + +Reshapes an n-dimensional nested array into another m-dimensional 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 ] ] +``` + +To reshape in colexicographic order, provide a third argument equal 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 the `fromShape` and the `toShape` have the same number of dimensions. + +
+ + + +
+ +## 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..a527e6f1696e --- /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 ], [ 5, 20 ], 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 ], [ 5, 20 ], 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=125,lexicographic', function benchmark( b ) { + var x; + var i; + var v; + + x = onesnd( [ 5, 5, 5 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = reshape( x, [ 5, 5, 5 ], [ 1, 1, 125 ], 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=125,colexicographic', function benchmark( b ) { + var x; + var i; + var v; + + x = onesnd( [ 5, 5, 5 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = reshape( x, [ 5, 5, 5 ], [ 1, 1, 125 ], 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=144,lexicographic', function benchmark( b ) { + var x; + var i; + var v; + + x = onesnd( [ 4, 3, 4, 3 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = reshape( x, [ 4, 3, 4, 3 ], [ 1, 1, 12, 12 ], 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=144,colexicographic', function benchmark( b ) { + var x; + var i; + var v; + + x = onesnd( [ 4, 3, 4, 3 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = reshape( x, [ 4, 3, 4, 3 ], [ 1, 1, 12, 12 ], 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=108,lexicographic', function benchmark( b ) { + var x; + var i; + var v; + + x = onesnd( [ 2, 2, 3, 3, 3 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = reshape( x, [ 2, 2, 3, 3, 3 ], [ 4, 1, 9, 3 ], 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=108,colexicographic', function benchmark( b ) { + var x; + var i; + var v; + + x = onesnd( [ 2, 2, 3, 3, 3 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = reshape( x, [ 2, 2, 3, 3, 3 ], [ 4, 1, 9, 3 ], 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..75c4f7ccf1ed --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/reshape/docs/repl.txt @@ -0,0 +1,36 @@ + +{{alias}}( x, fromShape, toShape, colexicographic ) + Reshapes an n-dimensional nested array into another m-dimensional 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..cd61dc64c8db --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/reshape/docs/types/index.d.ts @@ -0,0 +1,57 @@ +/* +* @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'; + +/** +* n-dimensional 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 + +/** +* Reshapes an n-dimensional nested array into another m-dimensional having a desired shape. +* +* ## Notes +* +* - The function assumes that `fromShape` and `toShape` have same the number of dimensions. +* +* @param x - input n-dimensional 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 m-dimensional nested array +* +* @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 ] ] +*/ +declare function reshape( x: ArrayND, fromShape: Shape, toShape: Shape, colexicographic: boolean ): Array; + + +// 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..678ef6fc17a0 --- /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 number[] + reshape( x, [ 2, 2 ], [ 4, 1 ], true ); // $ExpectType number[] + + reshape( [ [ '1', '2' ], [ '3', '4' ] ], [ 2, 2 ], [ 4, 1 ], false ); // $ExpectType string[] + reshape( [ [ '1', '2' ], [ '3', '4' ] ], [ 2, 2 ], [ 4, 1 ], true ); // $ExpectType string[] +} + +// 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..eddd5c97312a --- /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 an n-dimensional nested array into another m-dimensional 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..0514b5d23523 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/reshape/lib/main.js @@ -0,0 +1,99 @@ +/** +* @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; + var j; + + 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 ( j = 0; j < S; j++ ) { + subArray = recurse( array, ndims, shape, d, index ); + out.push( subArray ); + index += stepSize; + } + return out; +} + + +// MAIN // + +/** +* Reshape an n-dimensional nested array into another m-dimensional having a desired shape. +* +* @param {Array} x - input n-dimensional 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 m-dimensional nested array +* +* @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 ] ] +*/ +function reshape( x, fromShape, toShape, colexicographic ) { + var f; + + 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..6636dd00e863 --- /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 an n-dimensional nested array into another m-dimensional 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..bc048b805674 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/reshape/test/test.js @@ -0,0 +1,337 @@ +/** +* @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 ], [ 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' ); + + 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 ], [ 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' ); + + 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 ], [ 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 ], [ 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' ); + + 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 ], [ 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' ); + + 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 ], [ 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(); +}); From 9a0737c4583b278c3b04fbe18bee5445ea195cda Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sun, 2 Mar 2025 11:19:51 +0500 Subject: [PATCH 2/9] fix: apply suggestions from code review Signed-off-by: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> --- .../@stdlib/array/base/reshape/docs/types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index cd61dc64c8db..ed1a29b8769b 100644 --- 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 @@ -49,7 +49,7 @@ type ArrayND = Array1D | Array2D | Array3D | Array4D | Array5D * 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 ): Array; +declare function reshape( x: ArrayND, fromShape: Shape, toShape: Shape, colexicographic: boolean ): ArrayND; // EXPORTS // From 26166e1e311a74c901e0b54bed22772f409493d4 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sun, 6 Apr 2025 08:10:07 +0000 Subject: [PATCH 3/9] refactor: apply suggestions from code review --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../@stdlib/array/base/reshape/README.md | 8 ++++---- .../@stdlib/array/base/reshape/docs/repl.txt | 3 +-- .../@stdlib/array/base/reshape/docs/types/index.d.ts | 12 +++++------- .../@stdlib/array/base/reshape/docs/types/test.ts | 10 +++++----- .../@stdlib/array/base/reshape/lib/index.js | 2 +- .../@stdlib/array/base/reshape/lib/main.js | 11 ++++------- .../@stdlib/array/base/reshape/package.json | 2 +- .../@stdlib/array/base/reshape/test/test.js | 4 ++-- 8 files changed, 23 insertions(+), 29 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/reshape/README.md b/lib/node_modules/@stdlib/array/base/reshape/README.md index 2d845807bb87..0f6f1d5e0ac7 100644 --- a/lib/node_modules/@stdlib/array/base/reshape/README.md +++ b/lib/node_modules/@stdlib/array/base/reshape/README.md @@ -20,7 +20,7 @@ limitations under the License. # reshape -> Reshape an n-dimensional nested array into another m-dimensional having a desired shape. +> Reshape a nested array into another nested array having a desired shape.
@@ -32,7 +32,7 @@ var reshape = require( '@stdlib/array/base/reshape' ); #### reshape( x, fromShape, toShape, colexicographic ) -Reshapes an n-dimensional nested array into another m-dimensional having a desired shape. +Reshapes a nested array into another nested array having a desired shape. ```javascript var x = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]; @@ -41,7 +41,7 @@ var out = reshape( x, [ 2, 3 ], [ 3, 2 ], false ); // returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] ``` -To reshape in colexicographic order, provide a third argument equal to `true`. +To reshape in colexicographic order, set the `colexicographic` argument to `true`. ```javascript var x = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]; @@ -58,7 +58,7 @@ var out = reshape( x, [ 2, 3 ], [ 3, 2 ], true ); ## Notes -- The function assumes that the `fromShape` and the `toShape` have the same number of dimensions. +- The function assumes that the `fromShape` and the `toShape` have the same number of elements.
diff --git a/lib/node_modules/@stdlib/array/base/reshape/docs/repl.txt b/lib/node_modules/@stdlib/array/base/reshape/docs/repl.txt index 75c4f7ccf1ed..a01bac3a5083 100644 --- a/lib/node_modules/@stdlib/array/base/reshape/docs/repl.txt +++ b/lib/node_modules/@stdlib/array/base/reshape/docs/repl.txt @@ -1,7 +1,6 @@ {{alias}}( x, fromShape, toShape, colexicographic ) - Reshapes an n-dimensional nested array into another m-dimensional having a - desired shape. + Reshapes a nested array into another nested array having a desired shape. Parameters ---------- 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 index ed1a29b8769b..3c52583bfe35 100644 --- 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 @@ -24,26 +24,24 @@ import { Array1D, Array2D, Array3D, Array4D, Array5D, Array6D, Array7D, Array8D, import { Shape } from '@stdlib/types/ndarray'; /** -* n-dimensional nested array. +* 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 /** -* Reshapes an n-dimensional nested array into another m-dimensional having a desired shape. +* Reshape a nested array into another nested array having a desired shape. * * ## Notes * -* - The function assumes that `fromShape` and `toShape` have same the number of dimensions. +* - The function assumes that `fromShape` and `toShape` have same the number of elements. * -* @param x - input n-dimensional nested array +* @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 m-dimensional nested array +* @returns output nested array * * @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 ); 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 index 678ef6fc17a0..099c56515b9a 100644 --- a/lib/node_modules/@stdlib/array/base/reshape/docs/types/test.ts +++ b/lib/node_modules/@stdlib/array/base/reshape/docs/types/test.ts @@ -25,11 +25,11 @@ import reshape = require( './index' ); { const x = [ [ 1, 2 ], [ 3, 4 ] ]; - reshape( x, [ 2, 2 ], [ 4, 1 ], false ); // $ExpectType number[] - reshape( x, [ 2, 2 ], [ 4, 1 ], true ); // $ExpectType number[] + 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 string[] - reshape( [ [ '1', '2' ], [ '3', '4' ] ], [ 2, 2 ], [ 4, 1 ], true ); // $ExpectType string[] + 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... @@ -76,7 +76,7 @@ import reshape = require( './index' ); { const x = [ [ 1, 2 ], [ 3, 4 ] ]; - reshape( x, [ 2, 2 ], [ 4, 1], '' ); // $ExpectError + 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 diff --git a/lib/node_modules/@stdlib/array/base/reshape/lib/index.js b/lib/node_modules/@stdlib/array/base/reshape/lib/index.js index eddd5c97312a..8b01ceb84f23 100644 --- a/lib/node_modules/@stdlib/array/base/reshape/lib/index.js +++ b/lib/node_modules/@stdlib/array/base/reshape/lib/index.js @@ -19,7 +19,7 @@ 'use strict'; /** -* Reshape an n-dimensional nested array into another m-dimensional having a desired shape. +* Reshape a nested array into another nested array having a desired shape. * * @module @stdlib/array/base/reshape * diff --git a/lib/node_modules/@stdlib/array/base/reshape/lib/main.js b/lib/node_modules/@stdlib/array/base/reshape/lib/main.js index 0514b5d23523..7e53ca10e6c6 100644 --- a/lib/node_modules/@stdlib/array/base/reshape/lib/main.js +++ b/lib/node_modules/@stdlib/array/base/reshape/lib/main.js @@ -44,7 +44,6 @@ function recurse( array, ndims, shape, dim, index ) { var d; var S; var i; - var j; S = shape[ dim ]; if ( dim === ndims - 1 ) { @@ -58,7 +57,7 @@ function recurse( array, ndims, shape, dim, index ) { } out = []; - for ( j = 0; j < S; j++ ) { + for ( i = 0; i < S; i++ ) { subArray = recurse( array, ndims, shape, d, index ); out.push( subArray ); index += stepSize; @@ -70,17 +69,15 @@ function recurse( array, ndims, shape, dim, index ) { // MAIN // /** -* Reshape an n-dimensional nested array into another m-dimensional having a desired shape. +* Reshape a nested array into another nested array having a desired shape. * -* @param {Array} x - input n-dimensional nested array +* @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 m-dimensional nested array +* @returns {Array} output nested array * * @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 ); diff --git a/lib/node_modules/@stdlib/array/base/reshape/package.json b/lib/node_modules/@stdlib/array/base/reshape/package.json index 6636dd00e863..5f754287b4ff 100644 --- a/lib/node_modules/@stdlib/array/base/reshape/package.json +++ b/lib/node_modules/@stdlib/array/base/reshape/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/array/base/reshape", "version": "0.0.0", - "description": "Reshape an n-dimensional nested array into another m-dimensional having a desired shape.", + "description": "Reshape a nested array into another nested array having a desired shape.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors", diff --git a/lib/node_modules/@stdlib/array/base/reshape/test/test.js b/lib/node_modules/@stdlib/array/base/reshape/test/test.js index bc048b805674..a02452c369e6 100644 --- a/lib/node_modules/@stdlib/array/base/reshape/test/test.js +++ b/lib/node_modules/@stdlib/array/base/reshape/test/test.js @@ -116,7 +116,7 @@ tape( 'the function returns a filled nested array (4d, lexicographic)', function x = [ [ [ - [ 1, 2, 3, 4] + [ 1, 2, 3, 4 ] ], [ [ 5, 6, 7, 8 ] @@ -268,7 +268,7 @@ tape( 'the function returns a filled nested array (4d, colexicographic)', functi x = [ [ [ - [ 1, 2, 3, 4] + [ 1, 2, 3, 4 ] ], [ [ 5, 6, 7, 8 ] From 38ec46f3fa11c684723ba6b66e7ca0cf78fd4c5f Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sun, 6 Apr 2025 08:25:03 +0000 Subject: [PATCH 4/9] refactor: apply suggestions from code review --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../array/base/reshape/benchmark/benchmark.js | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/reshape/benchmark/benchmark.js b/lib/node_modules/@stdlib/array/base/reshape/benchmark/benchmark.js index a527e6f1696e..bfb7e88f8ad3 100644 --- a/lib/node_modules/@stdlib/array/base/reshape/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/array/base/reshape/benchmark/benchmark.js @@ -38,7 +38,7 @@ bench( pkg+':ndims=2,size=100,lexicographic', function benchmark( b ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - v = reshape( x, [ 10, 10 ], [ 5, 20 ], false ); + v = reshape( x, [ 10, 10 ], [ 1, 100 ], false ); if ( typeof v !== 'object' ) { b.fail( 'should return an array' ); } @@ -60,7 +60,7 @@ bench( pkg+':ndims=2,size=100,colexicographic', function benchmark( b ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - v = reshape( x, [ 10, 10 ], [ 5, 20 ], true ); + v = reshape( x, [ 10, 10 ], [ 1, 100 ], true ); if ( typeof v !== 'object' ) { b.fail( 'should return an array' ); } @@ -73,16 +73,16 @@ bench( pkg+':ndims=2,size=100,colexicographic', function benchmark( b ) { b.end(); }); -bench( pkg+':ndims=3,size=125,lexicographic', function benchmark( b ) { +bench( pkg+':ndims=3,size=100,lexicographic', function benchmark( b ) { var x; var i; var v; - x = onesnd( [ 5, 5, 5 ] ); + x = onesnd( [ 4, 5, 5 ] ); b.tic(); for ( i = 0; i < b.iterations; i++ ) { - v = reshape( x, [ 5, 5, 5 ], [ 1, 1, 125 ], false ); + v = reshape( x, [ 4, 5, 5 ], [ 1, 1, 100 ], false ); if ( typeof v !== 'object' ) { b.fail( 'should return an array' ); } @@ -95,16 +95,16 @@ bench( pkg+':ndims=3,size=125,lexicographic', function benchmark( b ) { b.end(); }); -bench( pkg+':ndims=3,size=125,colexicographic', function benchmark( b ) { +bench( pkg+':ndims=3,size=100,colexicographic', function benchmark( b ) { var x; var i; var v; - x = onesnd( [ 5, 5, 5 ] ); + x = onesnd( [ 4, 5, 5 ] ); b.tic(); for ( i = 0; i < b.iterations; i++ ) { - v = reshape( x, [ 5, 5, 5 ], [ 1, 1, 125 ], true ); + v = reshape( x, [ 4, 5, 5 ], [ 1, 1, 100 ], true ); if ( typeof v !== 'object' ) { b.fail( 'should return an array' ); } @@ -117,16 +117,16 @@ bench( pkg+':ndims=3,size=125,colexicographic', function benchmark( b ) { b.end(); }); -bench( pkg+':ndims=4,size=144,lexicographic', function benchmark( b ) { +bench( pkg+':ndims=4,size=100,lexicographic', function benchmark( b ) { var x; var i; var v; - x = onesnd( [ 4, 3, 4, 3 ] ); + x = onesnd( [ 5, 2, 5, 2 ] ); b.tic(); for ( i = 0; i < b.iterations; i++ ) { - v = reshape( x, [ 4, 3, 4, 3 ], [ 1, 1, 12, 12 ], false ); + v = reshape( x, [ 5, 2, 5, 2 ], [ 1, 1, 1, 100 ], false ); if ( typeof v !== 'object' ) { b.fail( 'should return an array' ); } @@ -139,16 +139,16 @@ bench( pkg+':ndims=4,size=144,lexicographic', function benchmark( b ) { b.end(); }); -bench( pkg+':ndims=4,size=144,colexicographic', function benchmark( b ) { +bench( pkg+':ndims=4,size=100,colexicographic', function benchmark( b ) { var x; var i; var v; - x = onesnd( [ 4, 3, 4, 3 ] ); + x = onesnd( [ 5, 2, 5, 2 ] ); b.tic(); for ( i = 0; i < b.iterations; i++ ) { - v = reshape( x, [ 4, 3, 4, 3 ], [ 1, 1, 12, 12 ], true ); + v = reshape( x, [ 5, 2, 5, 2 ], [ 1, 1, 1, 100 ], true ); if ( typeof v !== 'object' ) { b.fail( 'should return an array' ); } @@ -161,16 +161,16 @@ bench( pkg+':ndims=4,size=144,colexicographic', function benchmark( b ) { b.end(); }); -bench( pkg+':ndims=5,size=108,lexicographic', function benchmark( b ) { +bench( pkg+':ndims=5,size=100,lexicographic', function benchmark( b ) { var x; var i; var v; - x = onesnd( [ 2, 2, 3, 3, 3 ] ); + x = onesnd( [ 5, 2, 1, 5, 2 ] ); b.tic(); for ( i = 0; i < b.iterations; i++ ) { - v = reshape( x, [ 2, 2, 3, 3, 3 ], [ 4, 1, 9, 3 ], false ); + v = reshape( x, [ 5, 2, 1, 5, 2 ], [ 1, 1, 1, 1, 100 ], false ); if ( typeof v !== 'object' ) { b.fail( 'should return an array' ); } @@ -183,16 +183,16 @@ bench( pkg+':ndims=5,size=108,lexicographic', function benchmark( b ) { b.end(); }); -bench( pkg+':ndims=5,size=108,colexicographic', function benchmark( b ) { +bench( pkg+':ndims=5,size=100,colexicographic', function benchmark( b ) { var x; var i; var v; - x = onesnd( [ 2, 2, 3, 3, 3 ] ); + x = onesnd( [ 5, 2, 1, 5, 2 ] ); b.tic(); for ( i = 0; i < b.iterations; i++ ) { - v = reshape( x, [ 2, 2, 3, 3, 3 ], [ 4, 1, 9, 3 ], true ); + v = reshape( x, [ 5, 2, 1, 5, 2 ], [ 1, 1, 1, 1, 100 ], true ); if ( typeof v !== 'object' ) { b.fail( 'should return an array' ); } From b63180b9a2e7eb3707fdef94a1827eabd09efa7b Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sun, 6 Apr 2025 08:49:15 +0000 Subject: [PATCH 5/9] test: add additional tests --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../@stdlib/array/base/reshape/test/test.js | 120 ++++++++++++++++++ 1 file changed, 120 insertions(+) diff --git a/lib/node_modules/@stdlib/array/base/reshape/test/test.js b/lib/node_modules/@stdlib/array/base/reshape/test/test.js index a02452c369e6..aeafec2350bd 100644 --- a/lib/node_modules/@stdlib/array/base/reshape/test/test.js +++ b/lib/node_modules/@stdlib/array/base/reshape/test/test.js @@ -39,6 +39,10 @@ tape( 'the function returns a reshaped nested array (2d, lexicographic)', functi 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' ); @@ -47,6 +51,14 @@ tape( 'the function returns a reshaped nested array (2d, lexicographic)', functi 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(); }); @@ -66,6 +78,14 @@ tape( 'the function returns a reshaped nested array (3d, lexicographic)', functi ] ]; + 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 ] @@ -105,6 +125,19 @@ tape( 'the function returns a reshaped nested array (3d, lexicographic)', functi 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(); }); @@ -124,6 +157,23 @@ tape( 'the function returns a filled nested array (4d, lexicographic)', function ] ]; + 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 = [ [ [ @@ -191,6 +241,10 @@ tape( 'the function returns a reshaped nested array (2d, colexicographic)', func 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' ); @@ -199,6 +253,28 @@ tape( 'the function returns a reshaped nested array (2d, colexicographic)', func 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(); }); @@ -218,6 +294,14 @@ tape( 'the function returns a reshaped nested array (3d, colexicographic)', func ] ]; + 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 ] @@ -257,6 +341,25 @@ tape( 'the function returns a reshaped nested array (3d, colexicographic)', func 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(); }); @@ -276,6 +379,23 @@ tape( 'the function returns a filled nested array (4d, colexicographic)', functi ] ]; + 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 = [ [ [ From 805670ad143f2eea26ae234a5cfa94cf1c42f381 Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 8 Apr 2025 21:37:19 -0700 Subject: [PATCH 6/9] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/base/reshape/README.md | 6 +++++- lib/node_modules/@stdlib/array/base/reshape/lib/main.js | 4 +--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/reshape/README.md b/lib/node_modules/@stdlib/array/base/reshape/README.md index 0f6f1d5e0ac7..7c6cd89d0141 100644 --- a/lib/node_modules/@stdlib/array/base/reshape/README.md +++ b/lib/node_modules/@stdlib/array/base/reshape/README.md @@ -40,6 +40,10 @@ 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`. @@ -58,7 +62,7 @@ var out = reshape( x, [ 2, 3 ], [ 3, 2 ], true ); ## Notes -- The function assumes that the `fromShape` and the `toShape` have the same number of elements. +- The function assumes that `fromShape` and `toShape` have the same number of elements. diff --git a/lib/node_modules/@stdlib/array/base/reshape/lib/main.js b/lib/node_modules/@stdlib/array/base/reshape/lib/main.js index 7e53ca10e6c6..9483f6c2eadf 100644 --- a/lib/node_modules/@stdlib/array/base/reshape/lib/main.js +++ b/lib/node_modules/@stdlib/array/base/reshape/lib/main.js @@ -84,9 +84,7 @@ function recurse( array, ndims, shape, dim, index ) { * // returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] */ function reshape( x, fromShape, toShape, colexicographic ) { - var f; - - f = flatten( x, fromShape, colexicographic ); + var f = flatten( x, fromShape, colexicographic ); return recurse( f, toShape.length, toShape, 0, 0 ); } From 5ec7da271c31b1a195eb113534a6c8519c10304b Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 8 Apr 2025 21:38:59 -0700 Subject: [PATCH 7/9] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/base/reshape/README.md | 2 +- .../@stdlib/array/base/reshape/docs/types/index.d.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/reshape/README.md b/lib/node_modules/@stdlib/array/base/reshape/README.md index 7c6cd89d0141..ab382d134f10 100644 --- a/lib/node_modules/@stdlib/array/base/reshape/README.md +++ b/lib/node_modules/@stdlib/array/base/reshape/README.md @@ -62,7 +62,7 @@ var out = reshape( x, [ 2, 3 ], [ 3, 2 ], true ); ## Notes -- The function assumes that `fromShape` and `toShape` have the same number of elements. +- The function assumes that `fromShape` and `toShape` specify arrays having the same number of elements. 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 index 3c52583bfe35..8d0eac14dc89 100644 --- 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 @@ -33,7 +33,7 @@ type ArrayND = Array1D | Array2D | Array3D | Array4D | Array5D * * ## Notes * -* - The function assumes that `fromShape` and `toShape` have same the number of elements. +* - The function assumes that `fromShape` and `toShape` specify arrays have same the number of elements. * * @param x - input nested array * @param fromShape - shape of the input array From bd678c9d932b0cfbb240985d7e56a06d7a0f8bf7 Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 8 Apr 2025 21:39:45 -0700 Subject: [PATCH 8/9] Apply suggestions from code review Signed-off-by: Athan --- lib/node_modules/@stdlib/array/base/reshape/README.md | 2 +- .../@stdlib/array/base/reshape/docs/types/index.d.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/reshape/README.md b/lib/node_modules/@stdlib/array/base/reshape/README.md index ab382d134f10..fa08ce47e675 100644 --- a/lib/node_modules/@stdlib/array/base/reshape/README.md +++ b/lib/node_modules/@stdlib/array/base/reshape/README.md @@ -62,7 +62,7 @@ var out = reshape( x, [ 2, 3 ], [ 3, 2 ], true ); ## Notes -- The function assumes that `fromShape` and `toShape` specify arrays having the same number of elements. +- The function assumes that `fromShape` and `toShape` describe arrays having the same number of elements. 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 index 8d0eac14dc89..40bf2afd07ab 100644 --- 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 @@ -33,7 +33,7 @@ type ArrayND = Array1D | Array2D | Array3D | Array4D | Array5D * * ## Notes * -* - The function assumes that `fromShape` and `toShape` specify arrays have same the number of elements. +* - 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 From c28d4971a56a5fb3d5b77033e8c48a4a3ec5927a Mon Sep 17 00:00:00 2001 From: Athan Date: Tue, 8 Apr 2025 21:41:30 -0700 Subject: [PATCH 9/9] style: fix missing blank line Signed-off-by: Athan --- lib/node_modules/@stdlib/array/base/reshape/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/node_modules/@stdlib/array/base/reshape/README.md b/lib/node_modules/@stdlib/array/base/reshape/README.md index fa08ce47e675..e747f6cc0fc6 100644 --- a/lib/node_modules/@stdlib/array/base/reshape/README.md +++ b/lib/node_modules/@stdlib/array/base/reshape/README.md @@ -40,6 +40,7 @@ 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.