Skip to content

Commit 079eeae

Browse files
committed
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 ---
1 parent 5470254 commit 079eeae

File tree

10 files changed

+1098
-0
lines changed

10 files changed

+1098
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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 an n-dimensional nested array into another m-dimensional 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 an n-dimensional nested array into another m-dimensional 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+
To reshape in colexicographic order, provide a third argument equal to `true`.
45+
46+
```javascript
47+
var x = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ];
48+
49+
var out = reshape( x, [ 2, 3 ], [ 3, 2 ], true );
50+
// [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
51+
```
52+
53+
</section>
54+
55+
<!-- /.usage -->
56+
57+
<section class="notes">
58+
59+
## Notes
60+
61+
- The function assumes that the `fromShape` and the `toShape` have the same number of dimensions.
62+
63+
</section>
64+
65+
<!-- /.notes -->
66+
67+
<section class="examples">
68+
69+
## Examples
70+
71+
<!-- eslint no-undef: "error" -->
72+
73+
```javascript
74+
var reshape = require( '@stdlib/array/base/reshape' );
75+
76+
var x = [
77+
[ 1, 2, 3, 4 ],
78+
[ 5, 6, 7, 8 ],
79+
[ 9, 10, 11, 12 ]
80+
];
81+
82+
var out = reshape( x, [ 3, 4 ], [ 4, 3 ], false );
83+
// returns [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ], [ 10, 11, 12 ] ]
84+
85+
out = reshape( x, [ 3, 4 ], [ 6, 2 ], false );
86+
// returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ], [ 9, 10 ], [ 11, 12 ] ]
87+
88+
out = reshape( x, [ 3, 4 ], [ 1, 12 ], false );
89+
// returns [ [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ]
90+
91+
out = reshape( x, [ 3, 4 ], [ 12, 1 ], false );
92+
// returns [ [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ], [ 6 ], [ 7 ], [ 8 ], [ 9 ], [ 10 ], [ 11 ], [ 12 ] ]
93+
```
94+
95+
</section>
96+
97+
<!-- /.examples -->
98+
99+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
100+
101+
<section class="related">
102+
103+
</section>
104+
105+
<!-- /.related -->
106+
107+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
108+
109+
<section class="links">
110+
111+
<!-- <related-links> -->
112+
113+
<!-- </related-links> -->
114+
115+
</section>
116+
117+
<!-- /.links -->
Lines changed: 206 additions & 0 deletions
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 ], [ 5, 20 ], 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 ], [ 5, 20 ], 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=125,lexicographic', function benchmark( b ) {
77+
var x;
78+
var i;
79+
var v;
80+
81+
x = onesnd( [ 5, 5, 5 ] );
82+
83+
b.tic();
84+
for ( i = 0; i < b.iterations; i++ ) {
85+
v = reshape( x, [ 5, 5, 5 ], [ 1, 1, 125 ], 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=125,colexicographic', function benchmark( b ) {
99+
var x;
100+
var i;
101+
var v;
102+
103+
x = onesnd( [ 5, 5, 5 ] );
104+
105+
b.tic();
106+
for ( i = 0; i < b.iterations; i++ ) {
107+
v = reshape( x, [ 5, 5, 5 ], [ 1, 1, 125 ], 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=144,lexicographic', function benchmark( b ) {
121+
var x;
122+
var i;
123+
var v;
124+
125+
x = onesnd( [ 4, 3, 4, 3 ] );
126+
127+
b.tic();
128+
for ( i = 0; i < b.iterations; i++ ) {
129+
v = reshape( x, [ 4, 3, 4, 3 ], [ 1, 1, 12, 12 ], 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=144,colexicographic', function benchmark( b ) {
143+
var x;
144+
var i;
145+
var v;
146+
147+
x = onesnd( [ 4, 3, 4, 3 ] );
148+
149+
b.tic();
150+
for ( i = 0; i < b.iterations; i++ ) {
151+
v = reshape( x, [ 4, 3, 4, 3 ], [ 1, 1, 12, 12 ], 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=108,lexicographic', function benchmark( b ) {
165+
var x;
166+
var i;
167+
var v;
168+
169+
x = onesnd( [ 2, 2, 3, 3, 3 ] );
170+
171+
b.tic();
172+
for ( i = 0; i < b.iterations; i++ ) {
173+
v = reshape( x, [ 2, 2, 3, 3, 3 ], [ 4, 1, 9, 3 ], 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=108,colexicographic', function benchmark( b ) {
187+
var x;
188+
var i;
189+
var v;
190+
191+
x = onesnd( [ 2, 2, 3, 3, 3 ] );
192+
193+
b.tic();
194+
for ( i = 0; i < b.iterations; i++ ) {
195+
v = reshape( x, [ 2, 2, 3, 3, 3 ], [ 4, 1, 9, 3 ], 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+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
{{alias}}( x, fromShape, toShape, colexicographic )
3+
Reshapes an n-dimensional nested array into another m-dimensional having a
4+
desired shape.
5+
6+
Parameters
7+
----------
8+
x: Array
9+
Input n-dimensional nested array.
10+
11+
fromShape: Array<integer>
12+
Input array shape.
13+
14+
toShape: Array<integer>
15+
Output array shape.
16+
17+
colexicographic: boolean
18+
Specifies whether to reshape the array in colexicographic order.
19+
20+
Returns
21+
-------
22+
out: Array
23+
Output array.
24+
25+
Examples
26+
--------
27+
> var x = [ [ 1, 2 ], [ 3, 4 ] ];
28+
> var out = {{alias}}( x, [ 2, 2 ], [ 4, 1 ], false )
29+
[ [ 1 ], [ 2 ], [ 3 ], [ 4 ] ]
30+
31+
> var x = [ [ 1, 2 ], [ 3, 4 ] ];
32+
> var out = {{alias}}( x, [ 2, 2 ], [ 4, 1 ], true )
33+
[ [ 1 ], [ 3 ], [ 2 ], [ 4 ] ]
34+
35+
See Also
36+
--------

0 commit comments

Comments
 (0)