Skip to content

Commit e97b816

Browse files
committed
feat: add blas/ext/unitspace
1 parent 013a23b commit e97b816

21 files changed

Lines changed: 3217 additions & 0 deletions
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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` (where number and complex number primitives are assumed to be `'float64'` and `'complex128'`, respectively).
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+
63+
var x = unitspace( [ 2, 3 ], start );
64+
// returns <ndarray>[ [ 1.0, 2.0, 3.0 ], [ 5.0, 6.0, 7.0 ] ]
65+
```
66+
67+
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.
68+
69+
```javascript
70+
var x = unitspace( [ 2, 2 ], 1.0, {
71+
'dims': [ 0, 1 ]
72+
});
73+
// returns <ndarray>[ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
74+
```
75+
76+
To specify the output [ndarray][@stdlib/ndarray/ctor] [data type][@stdlib/ndarray/dtypes], provide a `dtype` option.
77+
78+
```javascript
79+
var x = unitspace( [ 4 ], 1.0, {
80+
'dtype': 'float32'
81+
});
82+
// returns <ndarray>[ 1.0, 2.0, 3.0, 4.0 ]
83+
```
84+
85+
#### unitspace.assign( x, start\[, options] )
86+
87+
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.
88+
89+
```javascript
90+
var zeros = require( '@stdlib/ndarray/zeros' );
91+
92+
var x = zeros( [ 4 ] );
93+
// returns <ndarray>
94+
95+
var out = unitspace.assign( x, 1.0 );
96+
// returns <ndarray>[ 1.0, 2.0, 3.0, 4.0 ]
97+
98+
var bool = ( x === out );
99+
// returns true
100+
```
101+
102+
The function has the following parameters:
103+
104+
- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a numeric or "generic" [data type][@stdlib/ndarray/dtypes].
105+
- **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].
106+
- **options**: function options (_optional_).
107+
108+
The function accepts the following options:
109+
110+
- **dims**: list of dimensions over which to perform operation. If not provided, the function generates linearly spaced values along the last dimension. Default: `[-1]`.
111+
112+
</section>
113+
114+
<!-- /.usage -->
115+
116+
<section class="notes">
117+
118+
## Notes
119+
120+
- 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.
121+
122+
- The `start` argument is cast to the data type of the output [ndarray][@stdlib/ndarray/ctor].
123+
124+
- 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.
125+
126+
</section>
127+
128+
<!-- /.notes -->
129+
130+
<section class="examples">
131+
132+
## Examples
133+
134+
<!-- eslint no-undef: "error" -->
135+
136+
```javascript
137+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
138+
var unitspace = require( '@stdlib/blas/ext/unitspace' );
139+
140+
// Create a vector of starting values:
141+
var start = unitspace( [ 5 ], 1 );
142+
143+
// Create a grid:
144+
var out = unitspace( [ 5, 5 ], start );
145+
console.log( ndarray2array( out ) );
146+
147+
// Generate values over multiple dimensions:
148+
out = unitspace( [ 5, 5 ], 1, {
149+
'dims': [ 0, 1 ]
150+
});
151+
console.log( ndarray2array( out ) );
152+
153+
// Generate values over multiple dimensions in column-major order:
154+
out = unitspace( [ 5, 5 ], 1, {
155+
'dims': [ 0, 1 ],
156+
'order': 'column-major'
157+
});
158+
console.log( ndarray2array( out ) );
159+
```
160+
161+
</section>
162+
163+
<!-- /.examples -->
164+
165+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
166+
167+
<section class="related">
168+
169+
</section>
170+
171+
<!-- /.related -->
172+
173+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
174+
175+
<section class="links">
176+
177+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
178+
179+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
180+
181+
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes
182+
183+
</section>
184+
185+
<!-- /.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)