Skip to content

Commit 40de89d

Browse files
headlessNodekgryte
andauthored
feat: add array/base/reshape
PR-URL: #5639 Closes: stdlib-js/metr-issue-tracker#20 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Muhammad Haris <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent ba0637b commit 40de89d

File tree

10 files changed

+1215
-0
lines changed

10 files changed

+1215
-0
lines changed
+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# reshape
22+
23+
> Reshape a nested array into another nested array having a desired shape.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var reshape = require( '@stdlib/array/base/reshape' );
31+
```
32+
33+
#### reshape( x, fromShape, toShape, colexicographic )
34+
35+
Reshapes a nested array into another nested array having a desired shape.
36+
37+
```javascript
38+
var x = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ];
39+
40+
var out = reshape( x, [ 2, 3 ], [ 3, 2 ], false );
41+
// returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ]
42+
```
43+
44+
- **x**: input array.
45+
- **fromShape**: input array shape.
46+
- **toShape**: output array shape.
47+
- **colexicographic**: boolean indicating whether to reshape the array in colexicographic order.
48+
49+
To reshape in colexicographic order, set the `colexicographic` argument to `true`.
50+
51+
```javascript
52+
var x = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ];
53+
54+
var out = reshape( x, [ 2, 3 ], [ 3, 2 ], true );
55+
// [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
56+
```
57+
58+
</section>
59+
60+
<!-- /.usage -->
61+
62+
<section class="notes">
63+
64+
## Notes
65+
66+
- The function assumes that `fromShape` and `toShape` describe arrays having the same number of elements.
67+
68+
</section>
69+
70+
<!-- /.notes -->
71+
72+
<section class="examples">
73+
74+
## Examples
75+
76+
<!-- eslint no-undef: "error" -->
77+
78+
```javascript
79+
var reshape = require( '@stdlib/array/base/reshape' );
80+
81+
var x = [
82+
[ 1, 2, 3, 4 ],
83+
[ 5, 6, 7, 8 ],
84+
[ 9, 10, 11, 12 ]
85+
];
86+
87+
var out = reshape( x, [ 3, 4 ], [ 4, 3 ], false );
88+
// returns [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ], [ 10, 11, 12 ] ]
89+
90+
out = reshape( x, [ 3, 4 ], [ 6, 2 ], false );
91+
// returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ], [ 9, 10 ], [ 11, 12 ] ]
92+
93+
out = reshape( x, [ 3, 4 ], [ 1, 12 ], false );
94+
// returns [ [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ]
95+
96+
out = reshape( x, [ 3, 4 ], [ 12, 1 ], false );
97+
// returns [ [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ], [ 6 ], [ 7 ], [ 8 ], [ 9 ], [ 10 ], [ 11 ], [ 12 ] ]
98+
```
99+
100+
</section>
101+
102+
<!-- /.examples -->
103+
104+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
105+
106+
<section class="related">
107+
108+
</section>
109+
110+
<!-- /.related -->
111+
112+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
113+
114+
<section class="links">
115+
116+
<!-- <related-links> -->
117+
118+
<!-- </related-links> -->
119+
120+
</section>
121+
122+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isArray = require( '@stdlib/assert/is-array' );
25+
var onesnd = require( '@stdlib/array/base/onesnd' );
26+
var pkg = require( './../package.json' ).name;
27+
var reshape = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+':ndims=2,size=100,lexicographic', function benchmark( b ) {
33+
var x;
34+
var i;
35+
var v;
36+
37+
x = onesnd( [ 10, 10 ] );
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
v = reshape( x, [ 10, 10 ], [ 1, 100 ], false );
42+
if ( typeof v !== 'object' ) {
43+
b.fail( 'should return an array' );
44+
}
45+
}
46+
b.toc();
47+
if ( !isArray( v ) ) {
48+
b.fail( 'should return an array' );
49+
}
50+
b.pass( 'benchmark finished' );
51+
b.end();
52+
});
53+
54+
bench( pkg+':ndims=2,size=100,colexicographic', function benchmark( b ) {
55+
var x;
56+
var i;
57+
var v;
58+
59+
x = onesnd( [ 10, 10 ] );
60+
61+
b.tic();
62+
for ( i = 0; i < b.iterations; i++ ) {
63+
v = reshape( x, [ 10, 10 ], [ 1, 100 ], true );
64+
if ( typeof v !== 'object' ) {
65+
b.fail( 'should return an array' );
66+
}
67+
}
68+
b.toc();
69+
if ( !isArray( v ) ) {
70+
b.fail( 'should return an array' );
71+
}
72+
b.pass( 'benchmark finished' );
73+
b.end();
74+
});
75+
76+
bench( pkg+':ndims=3,size=100,lexicographic', function benchmark( b ) {
77+
var x;
78+
var i;
79+
var v;
80+
81+
x = onesnd( [ 4, 5, 5 ] );
82+
83+
b.tic();
84+
for ( i = 0; i < b.iterations; i++ ) {
85+
v = reshape( x, [ 4, 5, 5 ], [ 1, 1, 100 ], false );
86+
if ( typeof v !== 'object' ) {
87+
b.fail( 'should return an array' );
88+
}
89+
}
90+
b.toc();
91+
if ( !isArray( v ) ) {
92+
b.fail( 'should return an array' );
93+
}
94+
b.pass( 'benchmark finished' );
95+
b.end();
96+
});
97+
98+
bench( pkg+':ndims=3,size=100,colexicographic', function benchmark( b ) {
99+
var x;
100+
var i;
101+
var v;
102+
103+
x = onesnd( [ 4, 5, 5 ] );
104+
105+
b.tic();
106+
for ( i = 0; i < b.iterations; i++ ) {
107+
v = reshape( x, [ 4, 5, 5 ], [ 1, 1, 100 ], true );
108+
if ( typeof v !== 'object' ) {
109+
b.fail( 'should return an array' );
110+
}
111+
}
112+
b.toc();
113+
if ( !isArray( v ) ) {
114+
b.fail( 'should return an array' );
115+
}
116+
b.pass( 'benchmark finished' );
117+
b.end();
118+
});
119+
120+
bench( pkg+':ndims=4,size=100,lexicographic', function benchmark( b ) {
121+
var x;
122+
var i;
123+
var v;
124+
125+
x = onesnd( [ 5, 2, 5, 2 ] );
126+
127+
b.tic();
128+
for ( i = 0; i < b.iterations; i++ ) {
129+
v = reshape( x, [ 5, 2, 5, 2 ], [ 1, 1, 1, 100 ], false );
130+
if ( typeof v !== 'object' ) {
131+
b.fail( 'should return an array' );
132+
}
133+
}
134+
b.toc();
135+
if ( !isArray( v ) ) {
136+
b.fail( 'should return an array' );
137+
}
138+
b.pass( 'benchmark finished' );
139+
b.end();
140+
});
141+
142+
bench( pkg+':ndims=4,size=100,colexicographic', function benchmark( b ) {
143+
var x;
144+
var i;
145+
var v;
146+
147+
x = onesnd( [ 5, 2, 5, 2 ] );
148+
149+
b.tic();
150+
for ( i = 0; i < b.iterations; i++ ) {
151+
v = reshape( x, [ 5, 2, 5, 2 ], [ 1, 1, 1, 100 ], true );
152+
if ( typeof v !== 'object' ) {
153+
b.fail( 'should return an array' );
154+
}
155+
}
156+
b.toc();
157+
if ( !isArray( v ) ) {
158+
b.fail( 'should return an array' );
159+
}
160+
b.pass( 'benchmark finished' );
161+
b.end();
162+
});
163+
164+
bench( pkg+':ndims=5,size=100,lexicographic', function benchmark( b ) {
165+
var x;
166+
var i;
167+
var v;
168+
169+
x = onesnd( [ 5, 2, 1, 5, 2 ] );
170+
171+
b.tic();
172+
for ( i = 0; i < b.iterations; i++ ) {
173+
v = reshape( x, [ 5, 2, 1, 5, 2 ], [ 1, 1, 1, 1, 100 ], false );
174+
if ( typeof v !== 'object' ) {
175+
b.fail( 'should return an array' );
176+
}
177+
}
178+
b.toc();
179+
if ( !isArray( v ) ) {
180+
b.fail( 'should return an array' );
181+
}
182+
b.pass( 'benchmark finished' );
183+
b.end();
184+
});
185+
186+
bench( pkg+':ndims=5,size=100,colexicographic', function benchmark( b ) {
187+
var x;
188+
var i;
189+
var v;
190+
191+
x = onesnd( [ 5, 2, 1, 5, 2 ] );
192+
193+
b.tic();
194+
for ( i = 0; i < b.iterations; i++ ) {
195+
v = reshape( x, [ 5, 2, 1, 5, 2 ], [ 1, 1, 1, 1, 100 ], true );
196+
if ( typeof v !== 'object' ) {
197+
b.fail( 'should return an array' );
198+
}
199+
}
200+
b.toc();
201+
if ( !isArray( v ) ) {
202+
b.fail( 'should return an array' );
203+
}
204+
b.pass( 'benchmark finished' );
205+
b.end();
206+
});
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
{{alias}}( x, fromShape, toShape, colexicographic )
3+
Reshapes a nested array into another nested array having a desired shape.
4+
5+
Parameters
6+
----------
7+
x: Array
8+
Input n-dimensional nested array.
9+
10+
fromShape: Array<integer>
11+
Input array shape.
12+
13+
toShape: Array<integer>
14+
Output array shape.
15+
16+
colexicographic: boolean
17+
Specifies whether to reshape the array in colexicographic order.
18+
19+
Returns
20+
-------
21+
out: Array
22+
Output array.
23+
24+
Examples
25+
--------
26+
> var x = [ [ 1, 2 ], [ 3, 4 ] ];
27+
> var out = {{alias}}( x, [ 2, 2 ], [ 4, 1 ], false )
28+
[ [ 1 ], [ 2 ], [ 3 ], [ 4 ] ]
29+
30+
> var x = [ [ 1, 2 ], [ 3, 4 ] ];
31+
> var out = {{alias}}( x, [ 2, 2 ], [ 4, 1 ], true )
32+
[ [ 1 ], [ 3 ], [ 2 ], [ 4 ] ]
33+
34+
See Also
35+
--------

0 commit comments

Comments
 (0)