Skip to content

Commit b5a289b

Browse files
headlessNodekgryte
andauthored
feat: add ndarray/unflatten
PR-URL: #11781 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#407
1 parent 06e1536 commit b5a289b

10 files changed

Lines changed: 904 additions & 0 deletions

File tree

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# unflatten
22+
23+
> Return a read-only view of an input [ndarray][@stdlib/ndarray/ctor] in which a specified dimension is expanded over multiple dimensions.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var unflatten = require( '@stdlib/ndarray/unflatten' );
41+
```
42+
43+
#### unflatten( x, dim, sizes )
44+
45+
Returns a read-only view of an input [ndarray][@stdlib/ndarray/ctor] in which a specified dimension is expanded over multiple dimensions.
46+
47+
```javascript
48+
var array = require( '@stdlib/ndarray/array' );
49+
50+
// Create a 1-dimensional ndarray:
51+
var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
52+
// returns <ndarray>[ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]
53+
54+
// Unflatten the first dimension:
55+
var y = unflatten( x, 0, [ 2, 3 ] );
56+
// returns <ndarray>[ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ]
57+
```
58+
59+
The function accepts the following arguments:
60+
61+
- **x**: input [ndarray][@stdlib/ndarray/ctor].
62+
- **dim**: dimension to be unflattened. 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`.
63+
- **sizes**: new shape of the unflattened dimension.
64+
65+
</section>
66+
67+
<!-- /.usage -->
68+
69+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
70+
71+
<section class="notes">
72+
73+
</section>
74+
75+
<!-- /.notes -->
76+
77+
<!-- Package usage examples. -->
78+
79+
<section class="examples">
80+
81+
## Examples
82+
83+
<!-- eslint no-undef: "error" -->
84+
85+
```javascript
86+
var uniform = require( '@stdlib/random/uniform' );
87+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
88+
var unflatten = require( '@stdlib/ndarray/unflatten' );
89+
90+
var x = uniform( [ 12 ], -10.0, 10.0 );
91+
console.log( ndarray2array( x ) );
92+
93+
var y = unflatten( x, 0, [ 3, 4 ] );
94+
console.log( ndarray2array( y ) );
95+
```
96+
97+
</section>
98+
99+
<!-- /.examples -->
100+
101+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
102+
103+
<section class="references">
104+
105+
</section>
106+
107+
<!-- /.references -->
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 for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
118+
119+
<section class="links">
120+
121+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
122+
123+
<!-- <related-links> -->
124+
125+
<!-- </related-links> -->
126+
127+
</section>
128+
129+
<!-- /.links -->
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 empty = require( '@stdlib/ndarray/empty' );
26+
var format = require( '@stdlib/string/format' );
27+
var pkg = require( './../package.json' ).name;
28+
var unflatten = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( format( '%s::1d', pkg ), function benchmark( b ) {
34+
var values;
35+
var v;
36+
var i;
37+
38+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
39+
40+
values = [
41+
empty( [ 6 ], { 'dtype': 'float64' } ),
42+
empty( [ 6 ], { 'dtype': 'float32' } ),
43+
empty( [ 6 ], { 'dtype': 'int32' } ),
44+
empty( [ 6 ], { 'dtype': 'complex128' } ),
45+
empty( [ 6 ], { 'dtype': 'generic' } )
46+
];
47+
48+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
49+
50+
b.tic();
51+
for ( i = 0; i < b.iterations; i++ ) {
52+
v = unflatten( values[ i%values.length ], 0, [ 2, 3 ] );
53+
if ( typeof v !== 'object' ) {
54+
b.fail( 'should return an object' );
55+
}
56+
}
57+
b.toc();
58+
if ( !isndarrayLike( v ) ) {
59+
b.fail( 'should return an ndarray' );
60+
}
61+
b.pass( 'benchmark finished' );
62+
b.end();
63+
});
64+
65+
bench( format( '%s::2d', pkg ), function benchmark( b ) {
66+
var values;
67+
var v;
68+
var i;
69+
70+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
71+
72+
values = [
73+
empty( [ 2, 6 ], { 'dtype': 'float64' } ),
74+
empty( [ 2, 6 ], { 'dtype': 'float32' } ),
75+
empty( [ 2, 6 ], { 'dtype': 'int32' } ),
76+
empty( [ 2, 6 ], { 'dtype': 'complex128' } ),
77+
empty( [ 2, 6 ], { 'dtype': 'generic' } )
78+
];
79+
80+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
81+
82+
b.tic();
83+
for ( i = 0; i < b.iterations; i++ ) {
84+
v = unflatten( values[ i%values.length ], 1, [ 2, 3 ] );
85+
if ( typeof v !== 'object' ) {
86+
b.fail( 'should return an object' );
87+
}
88+
}
89+
b.toc();
90+
if ( !isndarrayLike( v ) ) {
91+
b.fail( 'should return an ndarray' );
92+
}
93+
b.pass( 'benchmark finished' );
94+
b.end();
95+
});
96+
97+
bench( format( '%s::3d', pkg ), function benchmark( b ) {
98+
var values;
99+
var v;
100+
var i;
101+
102+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
103+
104+
values = [
105+
empty( [ 2, 2, 6 ], { 'dtype': 'float64' } ),
106+
empty( [ 2, 2, 6 ], { 'dtype': 'float32' } ),
107+
empty( [ 2, 2, 6 ], { 'dtype': 'int32' } ),
108+
empty( [ 2, 2, 6 ], { 'dtype': 'complex128' } ),
109+
empty( [ 2, 2, 6 ], { 'dtype': 'generic' } )
110+
];
111+
112+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
113+
114+
b.tic();
115+
for ( i = 0; i < b.iterations; i++ ) {
116+
v = unflatten( values[ i%values.length ], 2, [ 2, 3 ] );
117+
if ( typeof v !== 'object' ) {
118+
b.fail( 'should return an object' );
119+
}
120+
}
121+
b.toc();
122+
if ( !isndarrayLike( v ) ) {
123+
b.fail( 'should return an ndarray' );
124+
}
125+
b.pass( 'benchmark finished' );
126+
b.end();
127+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
{{alias}}( x, dim, sizes )
3+
Returns a read-only view of an input ndarray in which a specified dimension
4+
is expanded over multiple dimensions.
5+
6+
Parameters
7+
----------
8+
x: ndarray
9+
Input array.
10+
11+
dim: integer
12+
Dimension to be unflattened. If provided an integer less than zero,
13+
the dimension index is resolved relative to the last dimension, with
14+
the last dimension corresponding to the value `-1`.
15+
16+
sizes: ArrayLikeObject<integer>
17+
New shape of the unflattened dimension.
18+
19+
Returns
20+
-------
21+
out: ndarray
22+
Output array.
23+
24+
Examples
25+
--------
26+
> var x = {{alias:@stdlib/ndarray/array}}( [ 1, 2, 3, 4, 5, 6 ] )
27+
<ndarray>[ 1, 2, 3, 4, 5, 6 ]
28+
> var y = {{alias}}( x, 0, [ 2, 3 ] )
29+
<ndarray>[ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
30+
31+
See Also
32+
--------
33+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { typedndarray } from '@stdlib/types/ndarray';
24+
import { Collection } from '@stdlib/types/array';
25+
26+
/**
27+
* Returns a read-only view of an input ndarray in which a specified dimension is expanded over multiple dimensions.
28+
*
29+
* @param x - input array
30+
* @param dim - dimension to be unflattened
31+
* @param sizes - new shape of the unflattened dimension
32+
* @returns output array
33+
*
34+
* @example
35+
* var array = require( '@stdlib/ndarray/array' );
36+
*
37+
* var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
38+
* // returns <ndarray>[ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]
39+
*
40+
* var y = unflatten( x, 0, [ 2, 3 ] );
41+
* // returns <ndarray>[ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ]
42+
*/
43+
declare function unflatten<T = unknown, U extends typedndarray<T> = typedndarray<T>>( x: U, dim: number, sizes: Collection<number> ): U;
44+
45+
46+
// EXPORTS //
47+
48+
export = unflatten;

0 commit comments

Comments
 (0)