Skip to content

Commit 9f702bf

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/unitspace
PR-URL: #11802 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#435
1 parent bd4ba4e commit 9f702bf

21 files changed

Lines changed: 3210 additions & 0 deletions
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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+
# unitspace
22+
23+
> Return a new [ndarray][@stdlib/ndarray/ctor] filled with linearly spaced numeric elements which increment by `1` starting from a specified value along one or more [ndarray][@stdlib/ndarray/ctor] dimensions.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var unitspace = require( '@stdlib/blas/ext/unitspace' );
31+
```
32+
33+
#### unitspace( shape, start\[, options] )
34+
35+
Returns a new [ndarray][@stdlib/ndarray/ctor] filled with linearly spaced numeric elements which increment by `1` starting from a specified value along one or more [ndarray][@stdlib/ndarray/ctor] dimensions.
36+
37+
```javascript
38+
var x = unitspace( [ 4 ], 1.0 );
39+
// returns <ndarray>[ 1.0, 2.0, 3.0, 4.0 ]
40+
```
41+
42+
The function has the following parameters:
43+
44+
- **shape**: array shape.
45+
- **start**: starting value. May be either a number, a complex number, or an [ndarray][@stdlib/ndarray/ctor] having a numeric or "generic" [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, a start [ndarray][@stdlib/ndarray/ctor] must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input shape, a start [ndarray][@stdlib/ndarray/ctor] must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor].
46+
- **options**: function options (_optional_).
47+
48+
The function accepts the following options:
49+
50+
- **dims**: list of dimensions over which to perform operation. If not provided, the function generates linearly spaced values along the last dimension. Default: `[-1]`.
51+
- **dtype**: output [ndarray][@stdlib/ndarray/ctor] [data type][@stdlib/ndarray/dtypes]. Must be a numeric or "generic" [data type][@stdlib/ndarray/dtypes]. If a data type is provided, `start` is cast to the specified data type. If a data type is not provided, the default output array data type is the same as the data type of `start`.
52+
- **order**: specifies whether an [ndarray][@stdlib/ndarray/ctor] is `'row-major'` (C-style) or `'column-major'` (Fortran-style). If `start` is a scalar value, the default order is `'row-major'`. If `start` is an [ndarray][@stdlib/ndarray/ctor], the default order is the same as the memory layout of `start`.
53+
- **mode**: specifies how to handle indices which exceed array dimensions (see [`ndarray`][@stdlib/ndarray/ctor]). Default: `'throw'`.
54+
- **submode**: a mode array which specifies for each dimension how to handle subscripts which exceed array dimensions (see [`ndarray`][@stdlib/ndarray/ctor]). If provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. Default: `[ options.mode ]`.
55+
56+
When provided a scalar or zero-dimensional [ndarray][@stdlib/ndarray/ctor] `start` argument, the value is broadcast across all elements in the shape defined by the complement of those dimensions specified by `options.dims`. To specify separate sub-array starting values, provide a non-zero-dimensional [ndarray][@stdlib/ndarray/ctor] argument.
57+
58+
```javascript
59+
var array = require( '@stdlib/ndarray/array' );
60+
61+
var start = array( [ 1.0, 5.0 ] );
62+
// returns <ndarray>[ 1.0, 5.0 ]
63+
64+
var x = unitspace( [ 2, 3 ], start );
65+
// returns <ndarray>[ [ 1.0, 2.0, 3.0 ], [ 5.0, 6.0, 7.0 ] ]
66+
```
67+
68+
By default, the function generates linearly spaced values along the last dimension of an output [ndarray][@stdlib/ndarray/ctor]. To perform the operation over specific dimensions, provide a `dims` option.
69+
70+
```javascript
71+
var x = unitspace( [ 2, 2 ], 1.0, {
72+
'dims': [ 0, 1 ]
73+
});
74+
// returns <ndarray>[ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
75+
```
76+
77+
To specify the output [ndarray][@stdlib/ndarray/ctor] [data type][@stdlib/ndarray/dtypes], provide a `dtype` option.
78+
79+
```javascript
80+
var x = unitspace( [ 4 ], 1.0, {
81+
'dtype': 'float32'
82+
});
83+
// returns <ndarray>[ 1.0, 2.0, 3.0, 4.0 ]
84+
```
85+
86+
#### unitspace.assign( x, start\[, options] )
87+
88+
Fills an [ndarray][@stdlib/ndarray/ctor] with linearly spaced numeric elements which increment by `1` starting from a specified value along one or more [ndarray][@stdlib/ndarray/ctor] dimensions.
89+
90+
```javascript
91+
var zeros = require( '@stdlib/ndarray/zeros' );
92+
93+
var x = zeros( [ 4 ] );
94+
// returns <ndarray>[ 0.0, 0.0, 0.0, 0.0 ]
95+
96+
var out = unitspace.assign( x, 1.0 );
97+
// returns <ndarray>[ 1.0, 2.0, 3.0, 4.0 ]
98+
99+
var bool = ( x === out );
100+
// returns true
101+
```
102+
103+
The function has the following parameters:
104+
105+
- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a numeric or "generic" [data type][@stdlib/ndarray/dtypes].
106+
- **start**: starting value. May be either a number, a complex number, or an [ndarray][@stdlib/ndarray/ctor] having a numeric or "generic" [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, a start [ndarray][@stdlib/ndarray/ctor] must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], a start [ndarray][@stdlib/ndarray/ctor] must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor].
107+
- **options**: function options (_optional_).
108+
109+
The function accepts the following options:
110+
111+
- **dims**: list of dimensions over which to perform operation. If not provided, the function generates linearly spaced values along the last dimension. Default: `[-1]`.
112+
113+
</section>
114+
115+
<!-- /.usage -->
116+
117+
<section class="notes">
118+
119+
## Notes
120+
121+
- When writing to a complex floating-point output [ndarray][@stdlib/ndarray/ctor], a real-valued `start` value is treated as a complex number having a real component equaling the provided value and having an imaginary component equaling zero.
122+
- The `start` argument is cast to the data type of the output [ndarray][@stdlib/ndarray/ctor].
123+
- The function iterates over [ndarray][@stdlib/ndarray/ctor] elements according to the memory layout of an output [ndarray][@stdlib/ndarray/ctor]. Accordingly, performance degradation is possible when operating over multiple dimensions of a large non-contiguous multi-dimensional output [ndarray][@stdlib/ndarray/ctor]. In such scenarios, one may want to copy an output [ndarray][@stdlib/ndarray/ctor] to contiguous memory before filling with linearly spaced values.
124+
125+
</section>
126+
127+
<!-- /.notes -->
128+
129+
<section class="examples">
130+
131+
## Examples
132+
133+
<!-- eslint no-undef: "error" -->
134+
135+
```javascript
136+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
137+
var unitspace = require( '@stdlib/blas/ext/unitspace' );
138+
139+
// Create a vector of starting values:
140+
var start = unitspace( [ 5 ], 1 );
141+
142+
// Create a grid:
143+
var out = unitspace( [ 5, 5 ], start );
144+
console.log( ndarray2array( out ) );
145+
146+
// Generate values over multiple dimensions:
147+
out = unitspace( [ 5, 5 ], 1, {
148+
'dims': [ 0, 1 ]
149+
});
150+
console.log( ndarray2array( out ) );
151+
152+
// Generate values over multiple dimensions in column-major order:
153+
out = unitspace( [ 5, 5 ], 1, {
154+
'dims': [ 0, 1 ],
155+
'order': 'column-major'
156+
});
157+
console.log( ndarray2array( out ) );
158+
```
159+
160+
</section>
161+
162+
<!-- /.examples -->
163+
164+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
165+
166+
<section class="related">
167+
168+
</section>
169+
170+
<!-- /.related -->
171+
172+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
173+
174+
<section class="links">
175+
176+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
177+
178+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
179+
180+
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes
181+
182+
</section>
183+
184+
<!-- /.links -->
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var zeros = require( '@stdlib/ndarray/zeros' );
27+
var format = require( '@stdlib/string/format' );
28+
var pkg = require( './../package.json' ).name;
29+
var assign = require( './../lib/assign.js' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'float64'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Creates a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} len - array length
46+
* @returns {Function} benchmark function
47+
*/
48+
function createBenchmark( len ) {
49+
var x = zeros( [ len ], options );
50+
return benchmark;
51+
52+
/**
53+
* Benchmark function.
54+
*
55+
* @private
56+
* @param {Benchmark} b - benchmark instance
57+
*/
58+
function benchmark( b ) {
59+
var o;
60+
var i;
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
o = assign( x, i );
65+
if ( typeof o !== 'object' ) {
66+
b.fail( 'should return an ndarray' );
67+
}
68+
}
69+
b.toc();
70+
if ( isnan( x.get( i%len ) ) ) {
71+
b.fail( 'should not return NaN' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
}
76+
}
77+
78+
79+
// MAIN //
80+
81+
/**
82+
* Main execution sequence.
83+
*
84+
* @private
85+
*/
86+
function main() {
87+
var len;
88+
var min;
89+
var max;
90+
var f;
91+
var i;
92+
93+
min = 1; // 10^min
94+
max = 6; // 10^max
95+
96+
for ( i = min; i <= max; i++ ) {
97+
len = pow( 10, i );
98+
f = createBenchmark( len );
99+
bench( format( '%s:assign:dtype=%s,len=%d', pkg, options.dtype, len ), f );
100+
}
101+
}
102+
103+
main();
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var format = require( '@stdlib/string/format' );
27+
var pkg = require( './../package.json' ).name;
28+
var unitspace = require( './../lib' );
29+
30+
31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'float64'
35+
};
36+
37+
38+
// FUNCTIONS //
39+
40+
/**
41+
* Creates a benchmark function.
42+
*
43+
* @private
44+
* @param {PositiveInteger} len - array length
45+
* @returns {Function} benchmark function
46+
*/
47+
function createBenchmark( len ) {
48+
return benchmark;
49+
50+
/**
51+
* Benchmark function.
52+
*
53+
* @private
54+
* @param {Benchmark} b - benchmark instance
55+
*/
56+
function benchmark( b ) {
57+
var o;
58+
var i;
59+
60+
b.tic();
61+
for ( i = 0; i < b.iterations; i++ ) {
62+
o = unitspace( [ len ], i, options );
63+
if ( typeof o !== 'object' ) {
64+
b.fail( 'should return an ndarray' );
65+
}
66+
}
67+
b.toc();
68+
if ( isnan( o.get( i%len ) ) ) {
69+
b.fail( 'should not return NaN' );
70+
}
71+
b.pass( 'benchmark finished' );
72+
b.end();
73+
}
74+
}
75+
76+
77+
// MAIN //
78+
79+
/**
80+
* Main execution sequence.
81+
*
82+
* @private
83+
*/
84+
function main() {
85+
var len;
86+
var min;
87+
var max;
88+
var f;
89+
var i;
90+
91+
min = 1; // 10^min
92+
max = 6; // 10^max
93+
94+
for ( i = min; i <= max; i++ ) {
95+
len = pow( 10, i );
96+
f = createBenchmark( len );
97+
bench( format( '%s:dtype=%s,len=%d', pkg, options.dtype, len ), f );
98+
}
99+
}
100+
101+
main();

0 commit comments

Comments
 (0)