Skip to content

Commit 737511b

Browse files
authored
feat: add ndarray/base/to-unflattened
PR-URL: #11493 Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#179
1 parent 122105d commit 737511b

File tree

10 files changed

+1097
-0
lines changed

10 files changed

+1097
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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 in which a specified dimension of an input ndarray 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 toUnflattened = require( '@stdlib/ndarray/base/to-unflattened' );
41+
```
42+
43+
#### toUnflattened( x, dim, sizes )
44+
45+
Returns a new ndarray in which a specified dimension of an input ndarray 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.
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/base/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+
</section>
120+
121+
<!-- /.links -->
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
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=2,order=row-major', pkg ), function benchmark( b ) {
36+
var strides;
37+
var values;
38+
var buffer;
39+
var offset;
40+
var dtype;
41+
var shape;
42+
var order;
43+
var sizes;
44+
var out;
45+
var i;
46+
47+
dtype = 'float64';
48+
buffer = new Float64Array( 24 );
49+
shape = [ 2, 12 ];
50+
strides = [ 12, 1 ];
51+
offset = 0;
52+
order = 'row-major';
53+
sizes = [ 3, 4 ];
54+
55+
values = [
56+
ndarrayBase( dtype, buffer, shape, strides, offset, order ),
57+
ndarrayBase( dtype, buffer, shape, strides, offset, order ),
58+
ndarrayBase( dtype, buffer, shape, strides, offset, order ),
59+
ndarrayBase( dtype, buffer, shape, strides, offset, order ),
60+
ndarrayBase( dtype, buffer, shape, strides, offset, order )
61+
];
62+
63+
b.tic();
64+
for ( i = 0; i < b.iterations; i++ ) {
65+
out = toUnflattened( values[ i%values.length ], 1, sizes );
66+
if ( typeof out !== 'object' ) {
67+
b.fail( 'should return an object' );
68+
}
69+
}
70+
b.toc();
71+
if ( !isndarrayLike( out ) ) {
72+
b.fail( 'should return an ndarray' );
73+
}
74+
b.pass( 'benchmark finished' );
75+
b.end();
76+
});
77+
78+
bench( format( '%s:ctor=ndarray,ndims=2,order=row-major', pkg ), function benchmark( b ) {
79+
var strides;
80+
var values;
81+
var buffer;
82+
var offset;
83+
var dtype;
84+
var shape;
85+
var order;
86+
var sizes;
87+
var out;
88+
var i;
89+
90+
dtype = 'float64';
91+
buffer = new Float64Array( 24 );
92+
shape = [ 2, 12 ];
93+
strides = [ 12, 1 ];
94+
offset = 0;
95+
order = 'row-major';
96+
sizes = [ 3, 4 ];
97+
98+
values = [
99+
ndarray( dtype, buffer, shape, strides, offset, order ),
100+
ndarray( dtype, buffer, shape, strides, offset, order ),
101+
ndarray( dtype, buffer, shape, strides, offset, order ),
102+
ndarray( dtype, buffer, shape, strides, offset, order ),
103+
ndarray( dtype, buffer, shape, strides, offset, order )
104+
];
105+
106+
b.tic();
107+
for ( i = 0; i < b.iterations; i++ ) {
108+
out = toUnflattened( values[ i%values.length ], 1, sizes );
109+
if ( typeof out !== 'object' ) {
110+
b.fail( 'should return an object' );
111+
}
112+
}
113+
b.toc();
114+
if ( !isndarrayLike( out ) ) {
115+
b.fail( 'should return an ndarray' );
116+
}
117+
b.pass( 'benchmark finished' );
118+
b.end();
119+
});
120+
121+
bench( format( '%s:ctor=base,ndims=2,order=column-major', pkg ), function benchmark( b ) {
122+
var strides;
123+
var values;
124+
var buffer;
125+
var offset;
126+
var dtype;
127+
var shape;
128+
var order;
129+
var sizes;
130+
var out;
131+
var i;
132+
133+
dtype = 'float64';
134+
buffer = new Float64Array( 24 );
135+
shape = [ 2, 12 ];
136+
strides = [ 1, 2 ];
137+
offset = 0;
138+
order = 'column-major';
139+
sizes = [ 3, 4 ];
140+
141+
values = [
142+
ndarrayBase( dtype, buffer, shape, strides, offset, order ),
143+
ndarrayBase( dtype, buffer, shape, strides, offset, order ),
144+
ndarrayBase( dtype, buffer, shape, strides, offset, order ),
145+
ndarrayBase( dtype, buffer, shape, strides, offset, order ),
146+
ndarrayBase( dtype, buffer, shape, strides, offset, order )
147+
];
148+
149+
b.tic();
150+
for ( i = 0; i < b.iterations; i++ ) {
151+
out = toUnflattened( values[ i%values.length ], 1, sizes );
152+
if ( typeof out !== 'object' ) {
153+
b.fail( 'should return an object' );
154+
}
155+
}
156+
b.toc();
157+
if ( !isndarrayLike( out ) ) {
158+
b.fail( 'should return an ndarray' );
159+
}
160+
b.pass( 'benchmark finished' );
161+
b.end();
162+
});
163+
164+
bench( format( '%s:ctor=ndarray,ndims=2,order=column-major', pkg ), function benchmark( b ) {
165+
var strides;
166+
var values;
167+
var buffer;
168+
var offset;
169+
var dtype;
170+
var shape;
171+
var order;
172+
var sizes;
173+
var out;
174+
var i;
175+
176+
dtype = 'float64';
177+
buffer = new Float64Array( 24 );
178+
shape = [ 2, 12 ];
179+
strides = [ 1, 2 ];
180+
offset = 0;
181+
order = 'column-major';
182+
sizes = [ 3, 4 ];
183+
184+
values = [
185+
ndarray( dtype, buffer, shape, strides, offset, order ),
186+
ndarray( dtype, buffer, shape, strides, offset, order ),
187+
ndarray( dtype, buffer, shape, strides, offset, order ),
188+
ndarray( dtype, buffer, shape, strides, offset, order ),
189+
ndarray( dtype, buffer, shape, strides, offset, order )
190+
];
191+
192+
b.tic();
193+
for ( i = 0; i < b.iterations; i++ ) {
194+
out = toUnflattened( values[ i%values.length ], 1, sizes );
195+
if ( typeof out !== 'object' ) {
196+
b.fail( 'should return an object' );
197+
}
198+
}
199+
b.toc();
200+
if ( !isndarrayLike( out ) ) {
201+
b.fail( 'should return an ndarray' );
202+
}
203+
b.pass( 'benchmark finished' );
204+
b.end();
205+
});
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 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+
--------
33+

0 commit comments

Comments
 (0)