Skip to content

Commit 4049c73

Browse files
headlessNodekgryte
andauthored
feat: add ndarray/base/unflatten-shape
PR-URL: #10441 Closes: stdlib-js/metr-issue-tracker#177 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent ceaea25 commit 4049c73

File tree

13 files changed

+1196
-0
lines changed

13 files changed

+1196
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
# unflattenShape
22+
23+
> Expand a dimension 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 unflattenShape = require( '@stdlib/ndarray/base/unflatten-shape' );
41+
```
42+
43+
#### unflattenShape( shape, dim, sizes )
44+
45+
Expands a dimension over multiple dimensions.
46+
47+
```javascript
48+
var sh = unflattenShape( [ 6, 2, 1 ], 0, [ 3, 2 ] );
49+
// returns [ 3, 2, 2, 1 ]
50+
```
51+
52+
The function accepts the following parameters:
53+
54+
- **shape**: array shape.
55+
- **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`.
56+
- **sizes**: new shape of the unflattened dimension.
57+
58+
#### unflattenShape.assign( shape, dim, sizes, out )
59+
60+
Expands a dimension over multiple dimensions and assigns results to a provided output array.
61+
62+
```javascript
63+
var o = [ 0, 0, 0, 0 ];
64+
65+
var out = unflattenShape.assign( [ 6, 2, 1 ], 0, [ 3, 2 ], o );
66+
// returns [ 3, 2, 2, 1 ]
67+
68+
var bool = ( out === o );
69+
// returns true
70+
```
71+
72+
The function accepts the following parameters:
73+
74+
- **shape**: array shape.
75+
- **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`.
76+
- **sizes**: new shape of the unflattened dimension.
77+
- **out**: output array.
78+
79+
</section>
80+
81+
<!-- /.usage -->
82+
83+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
84+
85+
<section class="notes">
86+
87+
</section>
88+
89+
<!-- /.notes -->
90+
91+
<!-- Package usage examples. -->
92+
93+
<section class="examples">
94+
95+
## Examples
96+
97+
<!-- eslint no-undef: "error" -->
98+
99+
```javascript
100+
var unflattenShape = require( '@stdlib/ndarray/base/unflatten-shape' );
101+
102+
var out = unflattenShape( [ 2, 4, 1 ], 1, [ 2, 2 ] );
103+
// returns [ 2, 2, 2, 1 ]
104+
105+
out = unflattenShape( [ 2, 4, 1 ], 1, [ 2, 1, 2 ] );
106+
// returns [ 2, 2, 1, 2, 1 ]
107+
108+
out = unflattenShape( [ 2, 4, 1 ], 1, [ 2, 1, 1, 2 ] );
109+
// returns [ 2, 2, 1, 1, 2, 1 ]
110+
```
111+
112+
</section>
113+
114+
<!-- /.examples -->
115+
116+
<!-- 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. -->
117+
118+
<section class="references">
119+
120+
</section>
121+
122+
<!-- /.references -->
123+
124+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
125+
126+
<section class="related">
127+
128+
</section>
129+
130+
<!-- /.related -->
131+
132+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
133+
134+
<section class="links">
135+
136+
</section>
137+
138+
<!-- /.links -->
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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 isArray = require( '@stdlib/assert/is-array' );
25+
var format = require( '@stdlib/string/format' );
26+
var pkg = require( './../package.json' ).name;
27+
var unflattenShape = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var shape;
34+
var sizes;
35+
var out;
36+
var i;
37+
38+
shape = [ 5, 9, 3, 4, 2 ];
39+
sizes = [ 1, 1, 3 ];
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
out = unflattenShape( shape, 2, sizes );
44+
if ( out.length !== 7 ) {
45+
b.fail( 'should have expected length' );
46+
}
47+
}
48+
b.toc();
49+
if ( !isArray( out ) ) {
50+
b.fail( 'should return an array' );
51+
}
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
});
55+
56+
bench( format( '%s:assign', pkg ), function benchmark( b ) {
57+
var shape;
58+
var sizes;
59+
var out;
60+
var i;
61+
62+
shape = [ 5, 9, 3, 4, 2 ];
63+
sizes = [ 1, 1, 3 ];
64+
out = [ 0, 0, 0, 0, 0, 0, 0 ];
65+
66+
b.tic();
67+
for ( i = 0; i < b.iterations; i++ ) {
68+
out = unflattenShape.assign( shape, 2, sizes, out );
69+
if ( out.length !== 7 ) {
70+
b.fail( 'should have expected length' );
71+
}
72+
}
73+
b.toc();
74+
if ( !isArray( out ) ) {
75+
b.fail( 'should return an array' );
76+
}
77+
b.pass( 'benchmark finished' );
78+
b.end();
79+
});
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
{{alias}}( shape, dim, sizes )
3+
Expands a dimension over multiple dimensions.
4+
5+
Parameters
6+
----------
7+
shape: ArrayLike<number>
8+
Array shape.
9+
10+
dim: integer
11+
Dimension to be unflattened. If less than zero, the index is resolved
12+
relative to the last dimension, with the last dimension corresponding to
13+
the value `-1`.
14+
15+
sizes: ArrayLike<number>
16+
New shape of the unflattened dimension.
17+
18+
Returns
19+
-------
20+
out: Array<number>
21+
Unflattened shape.
22+
23+
Examples
24+
--------
25+
> var sh = [ 6, 2, 1 ];
26+
> var sizes = [ 3, 2 ];
27+
> var out = {{alias}}( sh, 0, sizes )
28+
[ 3, 2, 2, 1 ]
29+
30+
31+
{{alias}}.assign( shape, dim, sizes, out )
32+
Expands a dimension over multiple dimensions and assigns results to a
33+
provided output array.
34+
35+
Parameters
36+
----------
37+
shape: ArrayLike<number>
38+
Array shape.
39+
40+
dim: integer
41+
Dimension to be unflattened. If less than zero, the index is resolved
42+
relative to the last dimension, with the last dimension corresponding to
43+
the value `-1`.
44+
45+
sizes: ArrayLike<number>
46+
New shape of the unflattened dimension.
47+
48+
out: Array|TypedArray|Object
49+
Output array.
50+
51+
Returns
52+
-------
53+
out: Array|TypedArray|Object
54+
Output array.
55+
56+
Examples
57+
--------
58+
> var sh = [ 6, 2, 1 ];
59+
> var sizes = [ 3, 2 ];
60+
> var o = [ 0, 0, 0, 0 ];
61+
> var out = {{alias}}.assign( sh, 0, sizes, o )
62+
[ 3, 2, 2, 1 ]
63+
> var bool = ( o === out )
64+
true
65+
66+
See Also
67+
--------
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 { ArrayLike } from '@stdlib/types/array';
24+
25+
/**
26+
* Interface describing `unflattenShape`.
27+
*/
28+
interface Routine {
29+
/**
30+
* Expands a dimension over multiple dimensions.
31+
*
32+
* @param shape - array shape
33+
* @param dim - dimension to be unflattened
34+
* @param sizes - new shape of the unflattened dimension
35+
* @returns unflattened shape
36+
*
37+
* @example
38+
* var sh = unflattenShape( [ 6, 2, 1 ], 0, [ 3, 2 ] );
39+
* // returns [ 3, 2, 2, 1 ]
40+
*/
41+
( shape: ArrayLike<number>, dim: number, sizes: ArrayLike<number> ): Array<number>;
42+
43+
/**
44+
* Expands a dimension over multiple dimensions.
45+
*
46+
* @param shape - array shape
47+
* @param dim - dimension to be unflattened
48+
* @param sizes - new shape of the unflattened dimension
49+
* @param out - output array
50+
* @returns unflattened shape
51+
*
52+
* @example
53+
* var o = [ 0, 0, 0, 0 ];
54+
*
55+
* var out = unflattenShape.assign( [ 6, 2, 1 ], 0, [ 3, 2 ], o );
56+
* // returns [ 3, 2, 2, 1 ]
57+
*
58+
* var bool = ( out === o );
59+
* // returns true
60+
*/
61+
assign<T extends ArrayLike<number> = ArrayLike<number>>( shape: ArrayLike<number>, dim: number, sizes: ArrayLike<number>, out: T ): T;
62+
}
63+
64+
/**
65+
* Expands a dimension over multiple dimensions.
66+
*
67+
* @param shape - array shape
68+
* @param dim - dimension to be unflattened
69+
* @param sizes - new shape of the unflattened dimension
70+
* @returns unflattened shape
71+
*
72+
* @example
73+
* var sh = unflattenShape( [ 6, 2, 1 ], 0, [ 3, 2 ] );
74+
* // returns [ 3, 2, 2, 1 ]
75+
*
76+
* @example
77+
* var o = [ 0, 0, 0, 0 ];
78+
*
79+
* var out = unflattenShape.assign( [ 6, 2, 1 ], 0, [ 3, 2 ], o );
80+
* // returns [ 3, 2, 2, 1 ]
81+
*
82+
* var bool = ( out === o );
83+
* // returns true
84+
*/
85+
declare var unflattenShape: Routine;
86+
87+
88+
// EXPORTS //
89+
90+
export = unflattenShape;

0 commit comments

Comments
 (0)