Skip to content

Commit 1aff763

Browse files
headlessNodekgryte
andauthored
feat: add ndarray/spread-dimensions
PR-URL: #9424 Closes: stdlib-js/metr-issue-tracker#114 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent aebc415 commit 1aff763

File tree

10 files changed

+926
-0
lines changed

10 files changed

+926
-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+
# spreadDimensions
22+
23+
> Return a read-only view of an input [ndarray][@stdlib/ndarray/ctor] where the dimensions of the input [ndarray][@stdlib/ndarray/ctor] are expanded to a specified dimensionality by spreading dimensions to specified dimension indices and inserting dimensions of size one for the remaining 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 spreadDimensions = require( '@stdlib/ndarray/spread-dimensions' );
41+
```
42+
43+
#### spreadDimensions( ndims, x, dims )
44+
45+
Returns a read-only view of an input [ndarray][@stdlib/ndarray/ctor] where the dimensions of the input [ndarray][@stdlib/ndarray/ctor] are expanded to a specified dimensionality by spreading dimensions to specified dimension indices and inserting dimensions of size one for the remaining dimensions.
46+
47+
```javascript
48+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
49+
var array = require( '@stdlib/ndarray/array' );
50+
51+
// Create a 2x2 ndarray:
52+
var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
53+
// returns <ndarray>[ [ 1, 2 ], [ 3, 4 ] ]
54+
55+
// Prepend a singleton dimension:
56+
var y = spreadDimensions( 3, x, [ 1, 2 ] );
57+
// returns <ndarray>[ [ [ 1, 2 ], [ 3, 4 ] ] ]
58+
59+
// Append a singleton dimension:
60+
y = spreadDimensions( 3, x, [ 0, 1 ] );
61+
// returns <ndarray>[ [ [ 1 ], [ 2 ] ], [ [ 3 ], [ 4 ] ] ]
62+
63+
// Insert a singleton dimension:
64+
y = spreadDimensions( 3, x, [ 0, 2 ] );
65+
// returns <ndarray>[ [ [ 1, 2 ] ], [ [ 3, 4 ] ] ]
66+
```
67+
68+
The function accepts the following arguments:
69+
70+
- **ndims**: number of dimensions in the output [ndarray][@stdlib/ndarray/ctor]. Must be greater than or equal to the number of dimensions in the input [ndarray][@stdlib/ndarray/ctor].
71+
- **x**: input [ndarray][@stdlib/ndarray/ctor].
72+
- **dims**: dimension indices specifying where to place the dimensions of the input [ndarray][@stdlib/ndarray/ctor]. Must resolve to normalized indices arranged in ascending order.
73+
74+
</section>
75+
76+
<!-- /.usage -->
77+
78+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
79+
80+
<section class="notes">
81+
82+
</section>
83+
84+
<!-- /.notes -->
85+
86+
<!-- Package usage examples. -->
87+
88+
<section class="examples">
89+
90+
## Examples
91+
92+
<!-- eslint no-undef: "error" -->
93+
94+
```javascript
95+
var uniform = require( '@stdlib/random/uniform' );
96+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
97+
var spreadDimensions = require( '@stdlib/ndarray/spread-dimensions' );
98+
99+
var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 );
100+
console.log( ndarray2array( x ) );
101+
102+
var y = spreadDimensions( 5, x, [ 0, 1, 2 ] );
103+
console.log( ndarray2array( y ) );
104+
```
105+
106+
</section>
107+
108+
<!-- /.examples -->
109+
110+
<!-- 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. -->
111+
112+
<section class="references">
113+
114+
</section>
115+
116+
<!-- /.references -->
117+
118+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
119+
120+
<section class="related">
121+
122+
</section>
123+
124+
<!-- /.related -->
125+
126+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
127+
128+
<section class="links">
129+
130+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
131+
132+
<!-- <related-links> -->
133+
134+
<!-- </related-links> -->
135+
136+
</section>
137+
138+
<!-- /.links -->
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
25+
var empty = require( '@stdlib/ndarray/empty' );
26+
var format = require( '@stdlib/string/format' );
27+
var pkg = require( './../package.json' ).name;
28+
var spreadDimensions = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( format( '%s::1d', pkg ), function benchmark( b ) {
34+
var values;
35+
var v;
36+
var i;
37+
38+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
39+
40+
values = [
41+
empty( [ 2 ], { 'dtype': 'float64' } ),
42+
empty( [ 2 ], { 'dtype': 'float32' } ),
43+
empty( [ 2 ], { 'dtype': 'int32' } ),
44+
empty( [ 2 ], { 'dtype': 'complex128' } ),
45+
empty( [ 2 ], { 'dtype': 'generic' } )
46+
];
47+
48+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
49+
50+
b.tic();
51+
for ( i = 0; i < b.iterations; i++ ) {
52+
v = spreadDimensions( 3, values[ i%values.length ], [ 1 ] );
53+
if ( typeof v !== 'object' ) {
54+
b.fail( 'should return an object' );
55+
}
56+
}
57+
b.toc();
58+
if ( !isndarrayLike( v ) ) {
59+
b.fail( 'should return an ndarray' );
60+
}
61+
b.pass( 'benchmark finished' );
62+
b.end();
63+
});
64+
65+
bench( format( '%s::2d', pkg ), function benchmark( b ) {
66+
var values;
67+
var v;
68+
var i;
69+
70+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
71+
72+
values = [
73+
empty( [ 2, 2 ], { 'dtype': 'float64' } ),
74+
empty( [ 2, 2 ], { 'dtype': 'float32' } ),
75+
empty( [ 2, 2 ], { 'dtype': 'int32' } ),
76+
empty( [ 2, 2 ], { 'dtype': 'complex128' } ),
77+
empty( [ 2, 2 ], { 'dtype': 'generic' } )
78+
];
79+
80+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
81+
82+
b.tic();
83+
for ( i = 0; i < b.iterations; i++ ) {
84+
v = spreadDimensions( 4, values[ i%values.length ], [ 1, 2 ] );
85+
if ( typeof v !== 'object' ) {
86+
b.fail( 'should return an object' );
87+
}
88+
}
89+
b.toc();
90+
if ( !isndarrayLike( v ) ) {
91+
b.fail( 'should return an ndarray' );
92+
}
93+
b.pass( 'benchmark finished' );
94+
b.end();
95+
});
96+
97+
bench( format( '%s::3d', pkg ), function benchmark( b ) {
98+
var values;
99+
var v;
100+
var i;
101+
102+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
103+
104+
values = [
105+
empty( [ 2, 2, 2 ], { 'dtype': 'float64' } ),
106+
empty( [ 2, 2, 2 ], { 'dtype': 'float32' } ),
107+
empty( [ 2, 2, 2 ], { 'dtype': 'int32' } ),
108+
empty( [ 2, 2, 2 ], { 'dtype': 'complex128' } ),
109+
empty( [ 2, 2, 2 ], { 'dtype': 'generic' } )
110+
];
111+
112+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
113+
114+
b.tic();
115+
for ( i = 0; i < b.iterations; i++ ) {
116+
v = spreadDimensions( 5, values[ i%values.length ], [ 1, 2, 3 ] );
117+
if ( typeof v !== 'object' ) {
118+
b.fail( 'should return an object' );
119+
}
120+
}
121+
b.toc();
122+
if ( !isndarrayLike( v ) ) {
123+
b.fail( 'should return an ndarray' );
124+
}
125+
b.pass( 'benchmark finished' );
126+
b.end();
127+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
{{alias}}( ndims, x, dims )
3+
Returns a read-only view of an input ndarray where the dimensions of the
4+
input ndarray are expanded to a specified dimensionality by spreading
5+
dimensions to specified dimension indices and inserting dimensions of size
6+
one for the remaining dimensions.
7+
8+
Parameters
9+
----------
10+
ndims: integer
11+
Number of dimensions in the output ndarray. Must be greater than or
12+
equal to the number of dimensions in the input ndarray.
13+
14+
x: ndarray
15+
Input ndarray.
16+
17+
dims: ArrayLikeObject<integer>
18+
Dimension indices at which to spread array dimensions. Must resolve to
19+
normalized indices arranged in ascending order. If provided an integer
20+
less than zero, the dimension index is resolved relative to the last
21+
dimension, with the last dimension corresponding to the value `-1`.
22+
23+
Returns
24+
-------
25+
out: ndarray
26+
A read-only view of an input ndarray where the dimensions of the input
27+
ndarray are expanded to a specified dimensionality.
28+
29+
Examples
30+
--------
31+
> var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] )
32+
<ndarray>[ [ 1, 2 ], [ 3, 4 ] ]
33+
> var y = {{alias}}( 3, x, [ 0, 1 ] )
34+
<ndarray>[ [ [ 1 ], [ 2 ] ], [ [ 3 ], [ 4 ] ] ]
35+
36+
See Also
37+
--------
38+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 { typedndarray } from '@stdlib/types/ndarray';
24+
import { Collection } from '@stdlib/types/array';
25+
26+
/**
27+
* Returns a read-only view of an input ndarray where the dimensions of the input ndarray are expanded to a specified dimensionality by spreading dimensions to specified dimension indices and inserting dimensions of size one for the remaining dimensions.
28+
*
29+
* @param ndims - number of dimensions in the output array
30+
* @param x - input ndarray
31+
* @param dims - dimension indices
32+
* @returns output array
33+
*
34+
* @example
35+
* var ndarray = require( '@stdlib/ndarray/ctor' );
36+
* var ndarray2array = require( '@stdlib/ndarray/to-array' );
37+
*
38+
* var buffer = [ 1.0, 2.0, 3.0, 4.0 ];
39+
* var shape = [ 2, 2 ];
40+
* var strides = [ 2, 1 ];
41+
* var offset = 0;
42+
*
43+
* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' );
44+
* // returns <ndarray>[ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
45+
*
46+
* var y = spreadDimensions( 5, x, [ 1, 3 ] );
47+
* // returns <ndarray>[ [ [ [ [ 1.0 ], [ 2.0 ] ] ], [ [ [ 3.0 ], [ 4.0 ] ] ] ] ]
48+
*/
49+
declare function spreadDimensions<T = unknown, U extends typedndarray<T> = typedndarray<T>>( ndims: number, x: U, dims: Collection<number> ): U;
50+
51+
52+
// EXPORTS //
53+
54+
export = spreadDimensions;

0 commit comments

Comments
 (0)