Skip to content

Commit cd84c58

Browse files
committed
feat: add ndarray/base/unflatten-shape
--- 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 4afe7e2 commit cd84c58

13 files changed

Lines changed: 997 additions & 0 deletions

File tree

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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+
> Unflatten a shape 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+
Unflattens a shape 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.
56+
- **sizes**: new shape of the unflattened dimension.
57+
58+
#### unflattenShape.assign( shape, dim, sizes, out )
59+
60+
Unflattens a shape 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.
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+
console.log( 'Unflattened Shape: ', out );
106+
```
107+
108+
</section>
109+
110+
<!-- /.examples -->
111+
112+
<!-- 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. -->
113+
114+
<section class="references">
115+
116+
</section>
117+
118+
<!-- /.references -->
119+
120+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
121+
122+
<section class="related">
123+
124+
</section>
125+
126+
<!-- /.related -->
127+
128+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
129+
130+
<section class="links">
131+
132+
</section>
133+
134+
<!-- /.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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
{{alias}}( shape, dim, sizes )
3+
Unflattens a shape over multiple dimensions.
4+
5+
Parameters
6+
----------
7+
shape: ArrayLike
8+
Array shape.
9+
10+
dim: integer
11+
Dimension to be unflattened.
12+
13+
sizes: ArrayLike
14+
New shape of the unflattened dimension.
15+
16+
Returns
17+
-------
18+
out: Array
19+
Unflattened shape.
20+
21+
Examples
22+
--------
23+
> var sh = [ 6, 2, 1 ];
24+
> var sizes = [ 3, 2 ];
25+
> var out = {{alias}}( sh, 0, sizes )
26+
[ 3, 2, 2, 1 ]
27+
28+
29+
{{alias}}.assign( shape, dim, sizes, out )
30+
Unflattens a shape over multiple dimensions and assigns results to a
31+
provided output array.
32+
33+
Parameters
34+
----------
35+
shape: ArrayLike
36+
Array shape.
37+
38+
dim: integer
39+
Dimension to be unflattened.
40+
41+
sizes: ArrayLike
42+
New shape of the unflattened dimension.
43+
44+
out: Array|TypedArray|Object
45+
Output array.
46+
47+
Returns
48+
-------
49+
out: Array|TypedArray|Object
50+
Output array.
51+
52+
Examples
53+
--------
54+
> var sh = [ 6, 2, 1 ];
55+
> var sizes = [ 3, 2 ];
56+
> var o = [ 0, 0, 0, 0 ];
57+
> var out = {{alias}}.assign( sh, 0, sizes, o )
58+
[ 3, 2, 2, 1 ]
59+
> var bool = ( o === out )
60+
true
61+
62+
See Also
63+
--------
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
/**
24+
* Interface describing `unflattenShape`
25+
*/
26+
interface Routine {
27+
/**
28+
* Unflattens a shape over multiple dimensions.
29+
*
30+
* @param shape - array shape
31+
* @param dim - dimension to be unflattened
32+
* @param sizes - new shape of the unflattened dimension
33+
* @returns unflattened shape
34+
*
35+
* @example
36+
* var sh = unflattenShape( [ 6, 2, 1 ], 0, [ 3, 2 ] );
37+
* // returns [ 3, 2, 2, 1 ]
38+
*/
39+
( shape: Array<number>, dim: number, sizes: Array<number> ): Array<number>;
40+
41+
/**
42+
* Unflattens a shape over multiple dimensions.
43+
*
44+
* @param shape - array shape
45+
* @param dim - dimension to be unflattened
46+
* @param sizes - new shape of the unflattened dimension
47+
* @param out - output array
48+
* @returns unflattened shape
49+
*
50+
* @example
51+
* var o = [ 0, 0, 0, 0 ];
52+
*
53+
* var out = unflattenShape.assign( [ 6, 2, 1 ], 0, [ 3, 2 ], o );
54+
* // returns [ 3, 2, 2, 1 ]
55+
*
56+
* var bool = ( out === o );
57+
* // returns true
58+
*/
59+
assign( shape: Array<number>, dim: number, sizes: Array<number>, out: Array<number> ): Array<number>;
60+
}
61+
62+
/**
63+
* Unflattens a shape over multiple dimensions.
64+
*
65+
* @param shape - array shape
66+
* @param dim - dimension to be unflattened
67+
* @param sizes - new shape of the unflattened dimension
68+
* @returns unflattened shape
69+
*
70+
* @example
71+
* var sh = unflattenShape( [ 6, 2, 1 ], 0, [ 3, 2 ] );
72+
* // returns [ 3, 2, 2, 1 ]
73+
*
74+
* @example
75+
* var o = [ 0, 0, 0, 0 ];
76+
*
77+
* var out = unflattenShape.assign( [ 6, 2, 1 ], 0, [ 3, 2 ], o );
78+
* // returns [ 3, 2, 2, 1 ]
79+
*
80+
* var bool = ( out === o );
81+
* // returns true
82+
*/
83+
declare var unflattenShape: Routine;
84+
85+
86+
// EXPORTS //
87+
88+
export = unflattenShape;

0 commit comments

Comments
 (0)