Skip to content

Commit 40421e2

Browse files
committed
feat: add ndarray/flatten-by
1 parent 7891615 commit 40421e2

File tree

10 files changed

+2709
-0
lines changed

10 files changed

+2709
-0
lines changed
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
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+
# flattenBy
22+
23+
> Apply a callback to elements in an input [ndarray][@stdlib/ndarray/ctor] and return a flattened output [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 flattenBy = require( '@stdlib/ndarray/flatten-by' );
37+
```
38+
39+
#### flattenBy( x\[, options], fcn[, thisArg] )
40+
41+
Applies a callback to elements in an input [ndarray][@stdlib/ndarray/ctor] and returns a flattened output [ndarray][@stdlib/ndarray/ctor].
42+
43+
```javascript
44+
var array = require( '@stdlib/ndarray/array' );
45+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
46+
47+
function scale( value ) {
48+
return value * 2.0;
49+
}
50+
51+
var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] );
52+
// returns <ndarray>
53+
54+
var y = flatten( x, scale );
55+
// returns <ndarray>
56+
57+
var arr = ndarray2array( y );
58+
// returns [ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ]
59+
```
60+
61+
The function accepts the following arguments:
62+
63+
- **x**: input [ndarray][@stdlib/ndarray/ctor].
64+
- **options**: function options (_optional_).
65+
- **fcn**: callback function.
66+
- **thisArg**: callback execution context.
67+
68+
The function accepts the following options:
69+
70+
- **order**: order in which input [ndarray][@stdlib/ndarray/ctor] elements should be flattened. Must be one of the following:
71+
72+
- `'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.
73+
- `'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.
74+
- `'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].
75+
- `'same'`: flatten according to the stated [order][@stdlib/ndarray/orders] of the input [ndarray][@stdlib/ndarray/ctor].
76+
77+
Default: `'row-major'`.
78+
79+
- **depth**: maximum number of input [ndarray][@stdlib/ndarray/ctor] dimensions to flatten.
80+
81+
By default, the function flattens all dimensions of the input [ndarray][@stdlib/ndarray/ctor]. To flatten to a desired depth, specify the `depth` option.
82+
83+
```javascript
84+
var array = require( '@stdlib/ndarray/array' );
85+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
86+
87+
function scale( value ) {
88+
return value * 2.0;
89+
}
90+
91+
var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] );
92+
// returns <ndarray>
93+
94+
var y = flatten( x, {
95+
'depth': 1
96+
});
97+
// returns <ndarray>
98+
99+
var arr = ndarray2array( y );
100+
// returns [ [ 2.0, 4.0 ], [ 6.0, 8.0 ], [ 10.0, 12.0 ] ]
101+
```
102+
103+
By default, the input [ndarray][@stdlib/ndarray/ctor] is flattened in lexicographic order. To flatten elements in a different order, specify the `order` option.
104+
105+
```javascript
106+
var array = require( '@stdlib/ndarray/array' );
107+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
108+
109+
function scale( value ) {
110+
return value * 2.0;
111+
}
112+
113+
var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] );
114+
// returns <ndarray>
115+
116+
var y = flatten( x, {
117+
'order': 'column-major'
118+
});
119+
// returns <ndarray>
120+
121+
var arr = ndarray2array( y );
122+
// returns [ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ]
123+
```
124+
125+
To set the callback function execution context, provide a `thisArg`.
126+
127+
<!-- eslint-disable no-invalid-this, max-len -->
128+
129+
```javascript
130+
var array = require( '@stdlib/ndarray/array' );
131+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
132+
133+
function scale( value ) {
134+
this.count += 1;
135+
return value * 2.0;
136+
}
137+
138+
var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] );
139+
// returns <ndarray>
140+
141+
var ctx = {
142+
'count': 0
143+
};
144+
145+
var y = flatten( x, scale, ctx );
146+
// returns <ndarray>
147+
148+
var arr = ndarray2array( y );
149+
// returns [ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ]
150+
151+
var count = ctx.count;
152+
// returns 6
153+
```
154+
155+
</section>
156+
157+
<!-- /.usage -->
158+
159+
<section class="notes">
160+
161+
## Notes
162+
163+
- 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.
164+
165+
- The callback function is provided the following arguments:
166+
167+
- **value**: current array element.
168+
- **indices**: current array element indices.
169+
- **arr**: the input [ndarray][@stdlib/ndarray/ctor].
170+
171+
</section>
172+
173+
<!-- /.notes -->
174+
175+
<section class="examples">
176+
177+
## Examples
178+
179+
<!-- eslint no-undef: "error" -->
180+
181+
```javascript
182+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
183+
var array = require( '@stdlib/ndarray/array' );
184+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
185+
var flattenBy = require( '@stdlib/ndarray/flatten-by' );
186+
187+
function scale( value ) {
188+
return value * 2.0;
189+
}
190+
191+
var xbuf = discreteUniform( 12, -100, 100, {
192+
'dtype': 'generic'
193+
});
194+
195+
var x = array( xbuf, {
196+
'shape': [ 2, 2, 3 ],
197+
'dtype': 'generic'
198+
});
199+
console.log( ndarray2array( x ) );
200+
201+
var y = flatten( x, scale );
202+
console.log( ndarray2array( y ) );
203+
```
204+
205+
</section>
206+
207+
<!-- /.examples -->
208+
209+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
210+
211+
<section class="related">
212+
213+
</section>
214+
215+
<!-- /.related -->
216+
217+
<section class="links">
218+
219+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
220+
221+
[@stdlib/ndarray/orders]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/orders
222+
223+
<!-- <related-links> -->
224+
225+
<!-- </related-links> -->
226+
227+
</section>
228+
229+
<!-- /.links -->

0 commit comments

Comments
 (0)