|
| 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 | +# flatten |
| 22 | + |
| 23 | +> Return a flattened copy of an input [ndarray][@stdlib/ndarray/ctor]. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +</section> |
| 28 | + |
| 29 | +<!-- /.intro --> |
| 30 | + |
| 31 | +<section class="usage"> |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +```javascript |
| 36 | +var flatten = require( '@stdlib/ndarray/flatten' ); |
| 37 | +``` |
| 38 | + |
| 39 | +#### flatten( x\[, options] ) |
| 40 | + |
| 41 | +Returns a flattened copy of an input [ndarray][@stdlib/ndarray/ctor]. |
| 42 | + |
| 43 | +```javascript |
| 44 | +var array = require( '@stdlib/ndarray/array' ); |
| 45 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 46 | + |
| 47 | +var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); |
| 48 | +// returns <ndarray> |
| 49 | + |
| 50 | +var y = flatten( x ); |
| 51 | +// returns <ndarray> |
| 52 | + |
| 53 | +var arr = ndarray2array( y ); |
| 54 | +// returns [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] |
| 55 | +``` |
| 56 | + |
| 57 | +The function accepts the following arguments: |
| 58 | + |
| 59 | +- **x**: input [ndarray][@stdlib/ndarray/ctor]. |
| 60 | +- **options**: function options (_optional_). |
| 61 | + |
| 62 | +The function accepts the following options: |
| 63 | + |
| 64 | +- **order**: order in which input [ndarray][@stdlib/ndarray/ctor] elements should be flattened. Must be one of the following: |
| 65 | + |
| 66 | + - `'row-major'`: flatten elements in lexicographic order. For example, given a two-dimensional input [ndarray][@stdlib/ndarray/ctor] (i.e., a matrix), flattening in lexicographic order means flattening the input [ndarray][@stdlib/ndarray/ctor] row-by-row. |
| 67 | + - `'column-major'`: flatten elements in colexicographic order. For example, given a two-dimensional input [ndarray][@stdlib/ndarray/ctor] (i.e., a matrix), flattening in colexicographic order means flattening the input [ndarray][@stdlib/ndarray/ctor] column-by-column. |
| 68 | + - `'any'`: flatten according to the physical layout of the input [ndarray][@stdlib/ndarray/ctor] data in memory, regardless of the stated [order][@stdlib/ndarray/orders] of the input [ndarray][@stdlib/ndarray/ctor]. |
| 69 | + - `'same'`: flatten according to the stated [order][@stdlib/ndarray/orders] of the input [ndarray][@stdlib/ndarray/ctor]. |
| 70 | + |
| 71 | + Default: `'row-major'`. |
| 72 | + |
| 73 | +- **depth**: maximum number of input [ndarray][@stdlib/ndarray/ctor] dimensions to flatten. |
| 74 | + |
| 75 | +By default, the function flattens all dimensions of the input [ndarray][@stdlib/ndarray/ctor]. To flatten to a desired depth, specify the `depth` option. |
| 76 | + |
| 77 | +```javascript |
| 78 | +var array = require( '@stdlib/ndarray/array' ); |
| 79 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 80 | + |
| 81 | +var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); |
| 82 | +// returns <ndarray> |
| 83 | + |
| 84 | +var y = flatten( x, { |
| 85 | + 'depth': 1 |
| 86 | +}); |
| 87 | +// returns <ndarray> |
| 88 | + |
| 89 | +var arr = ndarray2array( y ); |
| 90 | +// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] |
| 91 | +``` |
| 92 | + |
| 93 | +By default, the input [ndarray][@stdlib/ndarray/ctor] is flattened in lexicographic order. To flatten elements in a different order, specify the `order` option. |
| 94 | + |
| 95 | +```javascript |
| 96 | +var array = require( '@stdlib/ndarray/array' ); |
| 97 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 98 | + |
| 99 | +var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); |
| 100 | +// returns <ndarray> |
| 101 | + |
| 102 | +var y = flatten( x, { |
| 103 | + 'order': 'column-major' |
| 104 | +}); |
| 105 | +// returns <ndarray> |
| 106 | + |
| 107 | +var arr = ndarray2array( y ); |
| 108 | +// returns [ 1.0, 3.0, 5.0, 2.0, 4.0, 6.0 ] |
| 109 | +``` |
| 110 | + |
| 111 | +</section> |
| 112 | + |
| 113 | +<!-- /.usage --> |
| 114 | + |
| 115 | +<section class="notes"> |
| 116 | + |
| 117 | +## Notes |
| 118 | + |
| 119 | +- The function **always** returns a copy of input [ndarray][@stdlib/ndarray/ctor] data, even when an input [ndarray][@stdlib/ndarray/ctor] already has the desired number of dimensions. |
| 120 | + |
| 121 | +</section> |
| 122 | + |
| 123 | +<!-- /.notes --> |
| 124 | + |
| 125 | +<section class="examples"> |
| 126 | + |
| 127 | +## Examples |
| 128 | + |
| 129 | +<!-- eslint no-undef: "error" --> |
| 130 | + |
| 131 | +```javascript |
| 132 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 133 | +var array = require( '@stdlib/ndarray/array' ); |
| 134 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 135 | +var flatten = require( '@stdlib/ndarray/flatten' ); |
| 136 | + |
| 137 | +var xbuf = discreteUniform( 12, -100, 100, { |
| 138 | + 'dtype': 'generic' |
| 139 | +}); |
| 140 | + |
| 141 | +var x = array( xbuf, { |
| 142 | + 'shape': [ 2, 2, 3 ], |
| 143 | + 'dtype': 'generic' |
| 144 | +}); |
| 145 | +console.log( ndarray2array( x ) ); |
| 146 | + |
| 147 | +var y = flatten( x ); |
| 148 | +console.log( ndarray2array( y ) ); |
| 149 | +``` |
| 150 | + |
| 151 | +</section> |
| 152 | + |
| 153 | +<!-- /.examples --> |
| 154 | + |
| 155 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 156 | + |
| 157 | +<section class="related"> |
| 158 | + |
| 159 | +</section> |
| 160 | + |
| 161 | +<!-- /.related --> |
| 162 | + |
| 163 | +<section class="links"> |
| 164 | + |
| 165 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/ndarray/tree/main/ctor |
| 166 | + |
| 167 | +[@stdlib/ndarray/orders]: https://github.com/stdlib-js/ndarray/tree/main/orders |
| 168 | + |
| 169 | +<!-- <related-links> --> |
| 170 | + |
| 171 | +<!-- </related-links> --> |
| 172 | + |
| 173 | +</section> |
| 174 | + |
| 175 | +<!-- /.links --> |
0 commit comments