Skip to content

Commit a71f1bd

Browse files
committed
feat: add ndarra/flatten
--- 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 3f8059a commit a71f1bd

File tree

10 files changed

+964
-0
lines changed

10 files changed

+964
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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, depth )
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+
// Create an input ndarray:
48+
var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] );
49+
// returns <ndarray>
50+
51+
var y = flatten( x, 2 );
52+
// returns <ndarray>
53+
54+
var arr = ndarray2array( y );
55+
// returns [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]
56+
```
57+
58+
The function accepts the following arguments:
59+
60+
- **x**: input [`ndarray`][@stdlib/ndarray/ctor].
61+
- **depth**: a non-negative integer specifying the number of input [`ndarray`][@stdlib/ndarray/ctor] dimensions to flatten.
62+
63+
</section>
64+
65+
<!-- /.usage -->
66+
67+
<section class="notes">
68+
69+
## Notes
70+
71+
- The provided input [`ndarray`][@stdlib/ndarray/ctor] **must** have more than one-dimension.
72+
73+
</section>
74+
75+
<!-- /.notes -->
76+
77+
<section class="examples">
78+
79+
## Examples
80+
81+
<!-- eslint no-undef: "error" -->
82+
83+
```javascript
84+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
85+
var array = require( '@stdlib/ndarray/array' );
86+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
87+
var flatten = require( '@stdlib/ndarray/flatten' );
88+
89+
var xbuf = discreteUniform( 12, -100, 100, {
90+
'dtype': 'generic'
91+
});
92+
93+
// Create an input ndarray:
94+
var x = array( xbuf, {
95+
'shape': [ 2, 2, 3 ],
96+
'dtype': 'generic'
97+
});
98+
console.log( ndarray2array( x ) );
99+
100+
// Flatten the input ndarray:
101+
var y = flatten( x, 2 );
102+
console.log( ndarray2array( y ) );
103+
```
104+
105+
</section>
106+
107+
<!-- /.examples -->
108+
109+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
110+
111+
<section class="related">
112+
113+
</section>
114+
115+
<!-- /.related -->
116+
117+
<section class="links">
118+
119+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
120+
121+
<!-- <related-links> -->
122+
123+
<!-- </related-links> -->
124+
125+
</section>
126+
127+
<!-- /.links -->
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
25+
var zeros = require( '@stdlib/ndarray/zeros' );
26+
var pkg = require( './../package.json' ).name;
27+
var flatten = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+'::2d', function benchmark( b ) {
33+
var values;
34+
var y;
35+
var i;
36+
var j;
37+
38+
values = [
39+
zeros( 'float64', [ 2, 2 ], 'row-major' ),
40+
zeros( 'float32', [ 2, 2 ], 'row-major' ),
41+
zeros( 'int32', [ 2, 2 ], 'row-major' ),
42+
zeros( 'complex128', [ 2, 2 ], 'row-major' ),
43+
zeros( 'generic', [ 2, 2 ], 'row-major' )
44+
];
45+
46+
b.tic();
47+
for ( i = 0; i < b.iterations; i++ ) {
48+
j = i % values.length;
49+
y = flatten( values[ j ], 1 );
50+
if ( typeof y !== 'object' ) {
51+
b.fail( 'should return an ndarray' );
52+
}
53+
}
54+
b.toc();
55+
if ( !isndarrayLike( y ) ) {
56+
b.fail( 'should return an ndarray' );
57+
}
58+
b.pass( 'benchmark finished' );
59+
b.end();
60+
});
61+
62+
bench( pkg+'::3d', function benchmark( b ) {
63+
var values;
64+
var y;
65+
var i;
66+
var j;
67+
68+
values = [
69+
zeros( 'float64', [ 2, 2, 2 ], 'row-major' ),
70+
zeros( 'float32', [ 2, 2, 2 ], 'row-major' ),
71+
zeros( 'int32', [ 2, 2, 2 ], 'row-major' ),
72+
zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ),
73+
zeros( 'generic', [ 2, 2, 2 ], 'row-major' )
74+
];
75+
76+
b.tic();
77+
for ( i = 0; i < b.iterations; i++ ) {
78+
j = i % values.length;
79+
y = flatten( values[ j ], 2 );
80+
if ( typeof y !== 'object' ) {
81+
b.fail( 'should return an ndarray' );
82+
}
83+
}
84+
b.toc();
85+
if ( !isndarrayLike( y ) ) {
86+
b.fail( 'should return an ndarray' );
87+
}
88+
b.pass( 'benchmark finished' );
89+
b.end();
90+
});
91+
92+
bench( pkg+'::4d', function benchmark( b ) {
93+
var values;
94+
var y;
95+
var i;
96+
var j;
97+
98+
values = [
99+
zeros( 'float64', [ 2, 2, 2, 2 ], 'row-major' ),
100+
zeros( 'float32', [ 2, 2, 2, 2 ], 'row-major' ),
101+
zeros( 'int32', [ 2, 2, 2, 2 ], 'row-major' ),
102+
zeros( 'complex128', [ 2, 2, 2, 2 ], 'row-major' ),
103+
zeros( 'generic', [ 2, 2, 2, 2 ], 'row-major' )
104+
];
105+
106+
b.tic();
107+
for ( i = 0; i < b.iterations; i++ ) {
108+
j = i % values.length;
109+
y = flatten( values[ j ], 3 );
110+
if ( typeof y !== 'object' ) {
111+
b.fail( 'should return an ndarray' );
112+
}
113+
}
114+
b.toc();
115+
if ( !isndarrayLike( y ) ) {
116+
b.fail( 'should return an ndarray' );
117+
}
118+
b.pass( 'benchmark finished' );
119+
b.end();
120+
});
121+
122+
bench( pkg+'::5d', function benchmark( b ) {
123+
var values;
124+
var y;
125+
var i;
126+
var j;
127+
128+
values = [
129+
zeros( 'float64', [ 2, 2, 2, 2, 2 ], 'row-major' ),
130+
zeros( 'float32', [ 2, 2, 2, 2, 2 ], 'row-major' ),
131+
zeros( 'int32', [ 2, 2, 2, 2, 2 ], 'row-major' ),
132+
zeros( 'complex128', [ 2, 2, 2, 2, 2 ], 'row-major' ),
133+
zeros( 'generic', [ 2, 2, 2, 2, 2 ], 'row-major' )
134+
];
135+
136+
b.tic();
137+
for ( i = 0; i < b.iterations; i++ ) {
138+
j = i % values.length;
139+
y = flatten( values[ j ], 4 );
140+
if ( typeof y !== 'object' ) {
141+
b.fail( 'should return an ndarray' );
142+
}
143+
}
144+
b.toc();
145+
if ( !isndarrayLike( y ) ) {
146+
b.fail( 'should return an ndarray' );
147+
}
148+
b.pass( 'benchmark finished' );
149+
b.end();
150+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
{{alias}}( x, depth )
3+
Returns a flattened copy of an input ndarray.
4+
5+
The `depth` must be a valid non-negative integer.
6+
7+
Parameters
8+
----------
9+
x: ndarray
10+
Input ndarray.
11+
12+
depth: integer
13+
A non-negative integer specifying the number of input ndarray dimensions
14+
to flatten.
15+
16+
Returns
17+
-------
18+
out: ndarray
19+
Output ndarray.
20+
21+
Examples
22+
--------
23+
> var x = {{alias:@stdlib/ndarray/array}}( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] );
24+
> var y = {{alias}}( x, 1 );
25+
> var arr = {{alias:@stdlib/ndarray/to-array}}( y )
26+
[ 1.0, 2.0, 3.0, 4.0 ]
27+
28+
See Also
29+
--------
30+

0 commit comments

Comments
 (0)