Skip to content

Commit 5192af7

Browse files
authored
feat: add ndarray/to-unflattened
PR-URL: #11891 Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#408
1 parent 02b39b4 commit 5192af7

10 files changed

Lines changed: 1028 additions & 0 deletions

File tree

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) 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+
# toUnflattened
22+
23+
> Return a new [`ndarray`][@stdlib/ndarray/ctor] in which a specified dimension of an input [`ndarray`][@stdlib/ndarray/ctor] is expanded over multiple dimensions.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro section element. -->
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 toUnflattened = require( '@stdlib/ndarray/to-unflattened' );
41+
```
42+
43+
#### toUnflattened( x, dim, sizes )
44+
45+
Returns a new [`ndarray`][@stdlib/ndarray/ctor] in which a specified dimension of an input [`ndarray`][@stdlib/ndarray/ctor] is expanded over multiple dimensions.
46+
47+
```javascript
48+
var array = require( '@stdlib/ndarray/array' );
49+
50+
var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
51+
// returns <ndarray>[ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]
52+
53+
var y = toUnflattened( x, 0, [ 2, 3 ] );
54+
// returns <ndarray>[ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ]
55+
```
56+
57+
The function accepts the following arguments:
58+
59+
- **x**: input [`ndarray`][@stdlib/ndarray/ctor].
60+
- **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`.
61+
- **sizes**: new shape of the unflattened dimension.
62+
63+
</section>
64+
65+
<!-- /.usage -->
66+
67+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
68+
69+
<section class="notes">
70+
71+
</section>
72+
73+
<!-- /.notes -->
74+
75+
<!-- Package usage examples. -->
76+
77+
<section class="examples">
78+
79+
## Examples
80+
81+
<!-- eslint no-undef: "error" -->
82+
83+
```javascript
84+
var uniform = require( '@stdlib/random/discrete-uniform' );
85+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
86+
var toUnflattened = require( '@stdlib/ndarray/to-unflattened' );
87+
88+
var x = uniform( [ 12 ], -100, 100 );
89+
console.log( ndarray2array( x ) );
90+
91+
var y = toUnflattened( x, 0, [ 3, 4 ] );
92+
console.log( ndarray2array( y ) );
93+
```
94+
95+
</section>
96+
97+
<!-- /.examples -->
98+
99+
<!-- 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. -->
100+
101+
<section class="references">
102+
103+
</section>
104+
105+
<!-- /.references -->
106+
107+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
108+
109+
<section class="related">
110+
111+
</section>
112+
113+
<!-- /.related -->
114+
115+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
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: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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 Float64Array = require( '@stdlib/array/float64' );
25+
var ndarrayBase = require( '@stdlib/ndarray/base/ctor' );
26+
var ndarray = require( '@stdlib/ndarray/ctor' );
27+
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var toUnflattened = require( './../lib' );
31+
32+
33+
// MAIN //
34+
35+
bench( format( '%s:ctor=base,ndims=1', pkg ), function benchmark( b ) {
36+
var strides;
37+
var buffer;
38+
var offset;
39+
var dtype;
40+
var order;
41+
var shape;
42+
var sizes;
43+
var out;
44+
var i;
45+
var x;
46+
47+
dtype = 'float64';
48+
buffer = new Float64Array( 24 );
49+
shape = [ 24 ];
50+
strides = [ 1 ];
51+
offset = 0;
52+
order = 'row-major';
53+
sizes = [ 4, 6 ];
54+
55+
x = ndarrayBase( dtype, buffer, shape, strides, offset, order );
56+
57+
b.tic();
58+
for ( i = 0; i < b.iterations; i++ ) {
59+
out = toUnflattened( x, 0, sizes );
60+
if ( typeof out !== 'object' ) {
61+
b.fail( 'should return an object' );
62+
}
63+
}
64+
b.toc();
65+
if ( !isndarrayLike( out ) ) {
66+
b.fail( 'should return an ndarray' );
67+
}
68+
b.pass( 'benchmark finished' );
69+
b.end();
70+
});
71+
72+
bench( format( '%s:ctor=ndarray,ndims=1', pkg ), function benchmark( b ) {
73+
var strides;
74+
var buffer;
75+
var offset;
76+
var dtype;
77+
var order;
78+
var shape;
79+
var sizes;
80+
var out;
81+
var i;
82+
var x;
83+
84+
dtype = 'float64';
85+
buffer = new Float64Array( 24 );
86+
shape = [ 24 ];
87+
strides = [ 1 ];
88+
offset = 0;
89+
order = 'row-major';
90+
sizes = [ 4, 6 ];
91+
92+
x = ndarray( dtype, buffer, shape, strides, offset, order );
93+
94+
b.tic();
95+
for ( i = 0; i < b.iterations; i++ ) {
96+
out = toUnflattened( x, 0, sizes );
97+
if ( typeof out !== 'object' ) {
98+
b.fail( 'should return an object' );
99+
}
100+
}
101+
b.toc();
102+
if ( !isndarrayLike( out ) ) {
103+
b.fail( 'should return an ndarray' );
104+
}
105+
b.pass( 'benchmark finished' );
106+
b.end();
107+
});
108+
109+
bench( format( '%s:ctor=base,ndims=2', pkg ), function benchmark( b ) {
110+
var strides;
111+
var buffer;
112+
var offset;
113+
var dtype;
114+
var order;
115+
var shape;
116+
var sizes;
117+
var out;
118+
var i;
119+
var x;
120+
121+
dtype = 'float64';
122+
buffer = new Float64Array( 24 );
123+
shape = [ 24, 1 ];
124+
strides = [ 1, 24 ];
125+
offset = 0;
126+
order = 'row-major';
127+
sizes = [ 4, 6 ];
128+
129+
x = ndarrayBase( dtype, buffer, shape, strides, offset, order );
130+
131+
b.tic();
132+
for ( i = 0; i < b.iterations; i++ ) {
133+
out = toUnflattened( x, 0, sizes );
134+
if ( typeof out !== 'object' ) {
135+
b.fail( 'should return an object' );
136+
}
137+
}
138+
b.toc();
139+
if ( !isndarrayLike( out ) ) {
140+
b.fail( 'should return an ndarray' );
141+
}
142+
b.pass( 'benchmark finished' );
143+
b.end();
144+
});
145+
146+
bench( format( '%s:ctor=ndarray,ndims=2', pkg ), function benchmark( b ) {
147+
var strides;
148+
var buffer;
149+
var offset;
150+
var dtype;
151+
var order;
152+
var shape;
153+
var sizes;
154+
var out;
155+
var i;
156+
var x;
157+
158+
dtype = 'float64';
159+
buffer = new Float64Array( 24 );
160+
shape = [ 24, 1 ];
161+
strides = [ 1, 24 ];
162+
offset = 0;
163+
order = 'row-major';
164+
sizes = [ 4, 6 ];
165+
166+
x = ndarray( dtype, buffer, shape, strides, offset, order );
167+
168+
b.tic();
169+
for ( i = 0; i < b.iterations; i++ ) {
170+
out = toUnflattened( x, 0, sizes );
171+
if ( typeof out !== 'object' ) {
172+
b.fail( 'should return an object' );
173+
}
174+
}
175+
b.toc();
176+
if ( !isndarrayLike( out ) ) {
177+
b.fail( 'should return an ndarray' );
178+
}
179+
b.pass( 'benchmark finished' );
180+
b.end();
181+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
{{alias}}( x, dim, sizes )
3+
Returns a new ndarray in which a specified dimension of an input ndarray
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+
--------
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 { ndarray } from '@stdlib/types/ndarray';
24+
import { Collection } from '@stdlib/types/array';
25+
26+
/**
27+
* Returns a new ndarray in which a specified dimension of an input ndarray 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 ndarray
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 = toUnflattened( x, 0, [ 2, 3 ] );
41+
* // returns <ndarray>[ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ]
42+
*/
43+
declare function toUnflattened<U extends ndarray = ndarray>( x: U, dim: number, sizes: Collection<number> ): U;
44+
45+
46+
// EXPORTS //
47+
48+
export = toUnflattened;

0 commit comments

Comments
 (0)