Skip to content

Commit 5cb89cc

Browse files
committed
Auto-generated commit
1 parent 1c40ca5 commit 5cb89cc

File tree

11 files changed

+1798
-0
lines changed

11 files changed

+1798
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
### Features
1212

13+
- [`63c4c05`](https://github.com/stdlib-js/stdlib/commit/63c4c057c630dc607b336c37e1cfc4b5e6ceac3d) - add `ndarray/base/unflatten` [(#10706)](https://github.com/stdlib-js/stdlib/pull/10706)
1314
- [`ef49a3e`](https://github.com/stdlib-js/stdlib/commit/ef49a3e72514684846fcf77b42c084e080ad5902) - add `removeSingletonDimensions` to namespace
1415
- [`c7633e9`](https://github.com/stdlib-js/stdlib/commit/c7633e9c62c55e333ad23d7f240fcab45fd25d98) - add `ndarray/remove-singleton-dimensions` [(#10807)](https://github.com/stdlib-js/stdlib/pull/10807)
1516
- [`3a4c3c1`](https://github.com/stdlib-js/stdlib/commit/3a4c3c1c8985cdd90373832b303a818d8e42785d) - add `rotr90` to namespace
@@ -838,6 +839,7 @@ A total of 49 issues were closed in this release:
838839

839840
<details>
840841

842+
- [`63c4c05`](https://github.com/stdlib-js/stdlib/commit/63c4c057c630dc607b336c37e1cfc4b5e6ceac3d) - **feat:** add `ndarray/base/unflatten` [(#10706)](https://github.com/stdlib-js/stdlib/pull/10706) _(by Muhammad Haris, Athan Reines, stdlib-bot)_
841843
- [`5795b39`](https://github.com/stdlib-js/stdlib/commit/5795b39e0f0f1d7ccae3ea1ed43809b7ff78b77d) - **fix:** determine layout based on physical layout _(by Athan Reines)_
842844
- [`c20c5d6`](https://github.com/stdlib-js/stdlib/commit/c20c5d602a0f897e86d7fbd83180fec84c535984) - **fix:** determine layout based on physical layout _(by Athan Reines)_
843845
- [`ef49a3e`](https://github.com/stdlib-js/stdlib/commit/ef49a3e72514684846fcf77b42c084e080ad5902) - **feat:** add `removeSingletonDimensions` to namespace _(by Athan Reines)_

base/unflatten/README.md

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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 view of an input ndarray 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/base/unflatten' );
41+
```
42+
43+
#### unflatten( x, dim, sizes, writable )
44+
45+
Returns a view of an input ndarray in which a specified dimension 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 out = unflatten( x, 0, [ 2, 3 ], false );
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+
- **writable**: boolean indicating whether a returned ndarray should be writable.
63+
64+
</section>
65+
66+
<!-- /.usage -->
67+
68+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
69+
70+
<section class="notes">
71+
72+
## Notes
73+
74+
- The `writable` parameter **only** applies to ndarray constructors supporting **read-only** instances.
75+
76+
</section>
77+
78+
<!-- /.notes -->
79+
80+
<!-- Package usage examples. -->
81+
82+
<section class="examples">
83+
84+
## Examples
85+
86+
<!-- eslint no-undef: "error" -->
87+
88+
```javascript
89+
var uniform = require( '@stdlib/random/discrete-uniform' );
90+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
91+
var unflatten = require( '@stdlib/ndarray/base/unflatten' );
92+
93+
var x = uniform( [ 12 ], -100, 100 );
94+
console.log( ndarray2array( x ) );
95+
96+
var out = unflatten( x, 0, [ 3, 4 ], false );
97+
console.log( ndarray2array( out ) );
98+
```
99+
100+
</section>
101+
102+
<!-- /.examples -->
103+
104+
<!-- 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. -->
105+
106+
<section class="references">
107+
108+
</section>
109+
110+
<!-- /.references -->
111+
112+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
113+
114+
<section class="related">
115+
116+
</section>
117+
118+
<!-- /.related -->
119+
120+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
121+
122+
<section class="links">
123+
124+
</section>
125+
126+
<!-- /.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( './../../../base/ctor' );
26+
var ndarray = require( './../../../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 unflatten = 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 = unflatten( values[ i%values.length ], 1, sizes, false );
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 = unflatten( values[ i%values.length ], 1, sizes, false );
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 = unflatten( values[ i%values.length ], 1, sizes, false );
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 = unflatten( values[ i%values.length ], 1, sizes, false );
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+
});

base/unflatten/docs/repl.txt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
{{alias}}( x, dim, sizes, writable )
3+
Returns a view of an input ndarray in which a specified dimension is
4+
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+
writable: boolean
20+
Boolean indicating whether the returned ndarray should be writable.
21+
22+
Returns
23+
-------
24+
out: ndarray
25+
Output array.
26+
27+
Examples
28+
--------
29+
> var x = {{alias:@stdlib/ndarray/array}}( [ 1, 2, 3, 4, 5, 6 ] )
30+
<ndarray>[ 1, 2, 3, 4, 5, 6 ]
31+
> var out = {{alias}}( x, 0, [ 2, 3 ], false )
32+
<ndarray>[ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
33+
34+
See Also
35+
--------

0 commit comments

Comments
 (0)