-
-
Notifications
You must be signed in to change notification settings - Fork 836
feat: add array/base/reshape
#5639
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
079eeae
feat: add array/base/reshape
headlessNode 9a0737c
fix: apply suggestions from code review
headlessNode 26166e1
refactor: apply suggestions from code review
headlessNode 38ec46f
refactor: apply suggestions from code review
headlessNode b63180b
test: add additional tests
headlessNode 805670a
Apply suggestions from code review
kgryte 5ec7da2
Apply suggestions from code review
kgryte bd678c9
Apply suggestions from code review
kgryte c28d497
style: fix missing blank line
kgryte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<!-- | ||
|
||
@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. | ||
|
||
--> | ||
|
||
# reshape | ||
|
||
> Reshape a nested array into another nested array having a desired shape. | ||
|
||
<section class="usage"> | ||
|
||
## 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 ] ] | ||
``` | ||
|
||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- **x**: input array. | ||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- **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 ] ] | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<section class="notes"> | ||
|
||
## Notes | ||
|
||
- The function assumes that `fromShape` and `toShape` describe arrays having the same number of elements. | ||
|
||
</section> | ||
|
||
<!-- /.notes --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```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 ] ] | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.examples --> | ||
|
||
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
||
<section class="related"> | ||
|
||
</section> | ||
|
||
<!-- /.related --> | ||
|
||
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="links"> | ||
|
||
<!-- <related-links> --> | ||
|
||
<!-- </related-links> --> | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
206 changes: 206 additions & 0 deletions
206
lib/node_modules/@stdlib/array/base/reshape/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ) { | ||
headlessNode marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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(); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<integer> | ||
Input array shape. | ||
|
||
toShape: Array<integer> | ||
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 | ||
-------- |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.