Skip to content

Commit 5da5402

Browse files
committed
feat: add ndarray/flatten-from
--- 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 ---
1 parent 4520512 commit 5da5402

File tree

10 files changed

+2915
-0
lines changed

10 files changed

+2915
-0
lines changed
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
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+
# flattenFrom
22+
23+
> Return a copy of an input [ndarray][@stdlib/ndarray/ctor], where all dimensions of the input [ndarray][@stdlib/ndarray/ctor] are flattened starting from a specific dimension.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var flattenFrom = require( '@stdlib/ndarray/flatten-from' );
37+
```
38+
39+
#### flattenFrom( x, dim\[, options] )
40+
41+
Returns a copy of an input [ndarray][@stdlib/ndarray/ctor], where all dimensions of the input [ndarray][@stdlib/ndarray/ctor] are flattened starting from a specific dimension.
42+
43+
```javascript
44+
var array = require( '@stdlib/ndarray/array' );
45+
var shape = require( '@stdlib/ndarray/shape' );
46+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
47+
48+
var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] );
49+
// returns <ndarray>
50+
51+
var shx = shape( x );
52+
// returns [ 3, 1, 2 ]
53+
54+
var y = flattenFrom( x, 1 );
55+
// returns <ndarray>
56+
57+
var shy = shape( y );
58+
// returns [ 3, 2 ]
59+
60+
var arr = ndarray2array( y );
61+
// returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ]
62+
```
63+
64+
The function accepts the following arguments:
65+
66+
- **x**: input [ndarray][@stdlib/ndarray/ctor].
67+
- **dim**: dimension to start flattening from. If provided an integer less than zero, the dimension index is resolved relative to the last dimension, with the last dimension corresponding to the value `-1`.
68+
- **options**: function options (_optional_).
69+
70+
The function accepts the following options:
71+
72+
- **order**: order in which input [ndarray][@stdlib/ndarray/ctor] elements should be flattened. Must be one of the following:
73+
74+
- `'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.
75+
- `'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.
76+
- `'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].
77+
- `'same'`: flatten according to the stated [order][@stdlib/ndarray/orders] of the input [ndarray][@stdlib/ndarray/ctor].
78+
79+
Default: `'row-major'`.
80+
81+
- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. By default, the function returns an [ndarray][@stdlib/ndarray/ctor] having the same [data type][@stdlib/ndarray/dtypes] as a provided input [ndarray][@stdlib/ndarray/ctor].
82+
83+
By default, the input [ndarray][@stdlib/ndarray/ctor] is flattened in lexicographic order. To flatten elements in a different order, specify the `order` option.
84+
85+
```javascript
86+
var array = require( '@stdlib/ndarray/array' );
87+
var shape = require( '@stdlib/ndarray/shape' );
88+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
89+
90+
var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] );
91+
// returns <ndarray>
92+
93+
var shx = shape( x );
94+
// returns [ 3, 1, 2 ]
95+
96+
var y = flattenFrom( x, 0, {
97+
'order': 'column-major'
98+
});
99+
// returns <ndarray>
100+
101+
var shy = shape( y );
102+
// returns [ 6 ]
103+
104+
var arr = ndarray2array( y );
105+
// returns [ 1.0, 3.0, 5.0, 2.0, 4.0, 6.0 ]
106+
```
107+
108+
By default, the output ndarray [data type][@stdlib/ndarray/dtypes] is inferred from the input [ndarray][@stdlib/ndarray/ctor]. To return an ndarray with a different [data type][@stdlib/ndarray/dtypes], specify the `dtype` option.
109+
110+
```javascript
111+
var array = require( '@stdlib/ndarray/array' );
112+
var dtype = require( '@stdlib/ndarray/dtype' );
113+
var shape = require( '@stdlib/ndarray/shape' );
114+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
115+
116+
var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] );
117+
// returns <ndarray>
118+
119+
var shx = shape( x );
120+
// returns [ 3, 1, 2 ]
121+
122+
var y = flattenFrom( x, 0, {
123+
'dtype': 'float32'
124+
});
125+
// returns <ndarray>
126+
127+
var shy = shape( y );
128+
// returns [ 6 ]
129+
130+
var dt = dtype( y );
131+
// returns 'float32'
132+
133+
var arr = ndarray2array( y );
134+
// returns [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]
135+
```
136+
137+
</section>
138+
139+
<!-- /.usage -->
140+
141+
<section class="notes">
142+
143+
## Notes
144+
145+
- 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.
146+
147+
</section>
148+
149+
<!-- /.notes -->
150+
151+
<section class="examples">
152+
153+
## Examples
154+
155+
<!-- eslint no-undef: "error" -->
156+
157+
```javascript
158+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
159+
var array = require( '@stdlib/ndarray/array' );
160+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
161+
var flattenFrom = require( '@stdlib/ndarray/flatten-from' );
162+
163+
var xbuf = discreteUniform( 12, -100, 100, {
164+
'dtype': 'generic'
165+
});
166+
167+
var x = array( xbuf, {
168+
'shape': [ 2, 2, 3 ],
169+
'dtype': 'generic'
170+
});
171+
console.log( ndarray2array( x ) );
172+
173+
var y = flattenFrom( x, 1 );
174+
console.log( ndarray2array( y ) );
175+
```
176+
177+
</section>
178+
179+
<!-- /.examples -->
180+
181+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
182+
183+
<section class="related">
184+
185+
</section>
186+
187+
<!-- /.related -->
188+
189+
<section class="links">
190+
191+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
192+
193+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
194+
195+
[@stdlib/ndarray/orders]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/orders
196+
197+
<!-- <related-links> -->
198+
199+
<!-- </related-links> -->
200+
201+
</section>
202+
203+
<!-- /.links -->

0 commit comments

Comments
 (0)