Skip to content

Commit 3767255

Browse files
committed
feat: add ndarray/base/spread-dimensions
--- 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 d11aaf3 commit 3767255

File tree

11 files changed

+1400
-0
lines changed

11 files changed

+1400
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
> Expand the shape of an array to a specified dimensionality by spreading its 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/base/spread-dimensions' );
41+
```
42+
43+
#### spreadDimensions( ndims, x, dims )
44+
45+
Expands the shape of an array to a specified dimensionality by spreading its dimensions to specified dimension indices and inserting dimensions of size one for the remaining dimensions.
46+
47+
```javascript
48+
var array = require( '@stdlib/ndarray/array' );
49+
50+
// Create a 2x2 ndarray:
51+
var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
52+
// returns <ndarray>
53+
54+
// Prepend a singleton dimension:
55+
var y = spreadDimensions( 3, x, [ 1, 2 ] );
56+
// returns <ndarray>
57+
58+
var sh = y.shape;
59+
// returns [ 1, 2, 2 ]
60+
61+
// Append a singleton dimension:
62+
y = spreadDimensions( 3, x, [ 0, 1 ] );
63+
// returns <ndarray>
64+
65+
sh = y.shape;
66+
// returns [ 2, 2, 1 ]
67+
68+
// Insert a singleton dimension:
69+
y = spreadDimensions( 3, x, [ 0, 2 ] );
70+
// returns <ndarray>
71+
72+
sh = y.shape;
73+
// returns [ 2, 1, 2 ]
74+
```
75+
76+
</section>
77+
78+
<!-- /.usage -->
79+
80+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
81+
82+
<section class="notes">
83+
84+
## Notes
85+
86+
- Each provided dimension index must reside on the interval `[-ndims, ndims-1]`. If provided a negative dimension index, the position at which to place a respective dimension is computed as `ndims + index`.
87+
- Provided dimension indices must resolve to normalized dimension indices arranged in ascending order.
88+
89+
</section>
90+
91+
<!-- /.notes -->
92+
93+
<!-- Package usage examples. -->
94+
95+
<section class="examples">
96+
97+
## Examples
98+
99+
<!-- eslint no-undef: "error" -->
100+
101+
```javascript
102+
var array = require( '@stdlib/ndarray/array' );
103+
var numel = require( '@stdlib/ndarray/base/numel' );
104+
var ind2sub = require( '@stdlib/ndarray/ind2sub' );
105+
var spreadDimensions = require( '@stdlib/ndarray/base/spread-dimensions' );
106+
107+
// Create a 2-dimensional array:
108+
var x = array( [ [ 1, 2 ], [ 3, 4 ] ], {
109+
'order': 'column-major'
110+
});
111+
// returns <ndarray>
112+
113+
// Spread dimensions:
114+
var y = spreadDimensions( 5, x, [ 1, 3 ] );
115+
// returns <ndarray>
116+
117+
// Retrieve the shape:
118+
var sh = y.shape;
119+
// returns [ 1, 2, 1, 2, 1 ]
120+
121+
// Retrieve the number of elements:
122+
var N = numel( sh );
123+
124+
// Loop through the array elements...
125+
var i;
126+
for ( i = 0; i < N; i++ ) {
127+
console.log( 'Y[%s] = %d', ind2sub( sh, i ).join( ', ' ), y.iget( i ) );
128+
}
129+
```
130+
131+
</section>
132+
133+
<!-- /.examples -->
134+
135+
<!-- 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. -->
136+
137+
<section class="references">
138+
139+
</section>
140+
141+
<!-- /.references -->
142+
143+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
144+
145+
<section class="related">
146+
147+
</section>
148+
149+
<!-- /.related -->
150+
151+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
152+
153+
<section class="links">
154+
155+
</section>
156+
157+
<!-- /.links -->
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 pkg = require( './../package.json' ).name;
29+
var spreadDimensions = require( './../lib' );
30+
31+
32+
// MAIN //
33+
34+
bench( pkg+'::base_ndarray,2d', function benchmark( b ) {
35+
var strides;
36+
var values;
37+
var buffer;
38+
var offset;
39+
var dtype;
40+
var shape;
41+
var order;
42+
var out;
43+
var i;
44+
45+
dtype = 'float64';
46+
buffer = new Float64Array( 2 );
47+
shape = [ 2 ];
48+
strides = [ 1 ];
49+
offset = 0;
50+
order = 'row-major';
51+
52+
values = [
53+
ndarrayBase( dtype, buffer, shape, strides, offset, order ),
54+
ndarrayBase( dtype, buffer, shape, strides, offset, order ),
55+
ndarrayBase( dtype, buffer, shape, strides, offset, order ),
56+
ndarrayBase( dtype, buffer, shape, strides, offset, order ),
57+
ndarrayBase( dtype, buffer, shape, strides, offset, order )
58+
];
59+
60+
b.tic();
61+
for ( i = 0; i < b.iterations; i++ ) {
62+
out = spreadDimensions( 2, values[ i%values.length ], [ 1 ] );
63+
if ( typeof out !== 'object' ) {
64+
b.fail( 'should return an object' );
65+
}
66+
}
67+
b.toc();
68+
if ( !isndarrayLike( out ) ) {
69+
b.fail( 'should return an ndarray' );
70+
}
71+
b.pass( 'benchmark finished' );
72+
b.end();
73+
});
74+
75+
bench( pkg+'::ndarray,2d', function benchmark( b ) {
76+
var strides;
77+
var values;
78+
var buffer;
79+
var offset;
80+
var dtype;
81+
var shape;
82+
var order;
83+
var out;
84+
var i;
85+
86+
dtype = 'float64';
87+
buffer = new Float64Array( 2 );
88+
shape = [ 2 ];
89+
strides = [ 1 ];
90+
offset = 0;
91+
order = 'row-major';
92+
93+
values = [
94+
ndarray( dtype, buffer, shape, strides, offset, order ),
95+
ndarray( dtype, buffer, shape, strides, offset, order ),
96+
ndarray( dtype, buffer, shape, strides, offset, order ),
97+
ndarray( dtype, buffer, shape, strides, offset, order ),
98+
ndarray( dtype, buffer, shape, strides, offset, order )
99+
];
100+
101+
b.tic();
102+
for ( i = 0; i < b.iterations; i++ ) {
103+
out = spreadDimensions( 2, values[ i%values.length ], [ 1 ] );
104+
if ( typeof out !== 'object' ) {
105+
b.fail( 'should return an object' );
106+
}
107+
}
108+
b.toc();
109+
if ( !isndarrayLike( out ) ) {
110+
b.fail( 'should return an ndarray' );
111+
}
112+
b.pass( 'benchmark finished' );
113+
b.end();
114+
});
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 array = require( '@stdlib/ndarray/array' );
25+
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
26+
var pkg = require( './../package.json' ).name;
27+
var spreadDimensions = require( './../lib' );
28+
29+
30+
// FUNCTIONS //
31+
32+
/**
33+
* Creates a benchmark function.
34+
*
35+
* @private
36+
* @param {PositiveInteger} ndims - number of dimensions
37+
* @returns {Function} benchmark function
38+
*/
39+
function createBenchmark( ndims ) {
40+
var x = array( [ 1, 2, 3, 4 ] );
41+
return benchmark;
42+
43+
/**
44+
* Benchmark function.
45+
*
46+
* @private
47+
* @param {Benchmark} b - benchmark instance
48+
*/
49+
function benchmark( b ) {
50+
var out;
51+
var i;
52+
53+
b.tic();
54+
for ( i = 0; i < b.iterations; i++ ) {
55+
out = spreadDimensions( ndims, x, [ i%ndims ] );
56+
if ( typeof out !== 'object' ) {
57+
b.fail( 'should return an object' );
58+
}
59+
}
60+
b.toc();
61+
if ( !isndarrayLike( out ) ) {
62+
b.fail( 'should return an ndarray' );
63+
}
64+
b.pass( 'benchmark finished' );
65+
b.end();
66+
}
67+
}
68+
69+
70+
// MAIN //
71+
72+
/**
73+
* Main execution sequence.
74+
*
75+
* @private
76+
*/
77+
function main() {
78+
var min;
79+
var max;
80+
var f;
81+
var i;
82+
83+
min = 2;
84+
max = 10;
85+
86+
for ( i = min; i <= max; i++ ) {
87+
f = createBenchmark( i );
88+
bench( pkg+'::ndarray:ndims='+i, f );
89+
}
90+
}
91+
92+
main();

0 commit comments

Comments
 (0)