|
| 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 --> |
0 commit comments