Skip to content

Commit 47177aa

Browse files
authored
feat: add blas/ext/base/sunitspace
PR-URL: #11681 Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#427
1 parent 7368bff commit 47177aa

33 files changed

Lines changed: 3400 additions & 0 deletions
Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
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+
# sunitspace
22+
23+
> Fill a single-precision floating-point strided array with linearly spaced numeric elements which increment by `1` starting from a specified value.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var sunitspace = require( '@stdlib/blas/ext/base/sunitspace' );
31+
```
32+
33+
#### sunitspace( N, start, x, strideX )
34+
35+
Fills a single-precision floating-point strided array with linearly spaced numeric elements which increment by `1` starting from a specified value.
36+
37+
```javascript
38+
var Float32Array = require( '@stdlib/array/float32' );
39+
40+
var x = new Float32Array( [ 0.0, 0.0, 0.0, 0.0 ] );
41+
42+
sunitspace( x.length, 3.0, x, 1 );
43+
// x => <Float32Array>[ 3.0, 4.0, 5.0, 6.0 ]
44+
```
45+
46+
The function has the following parameters:
47+
48+
- **N**: number of indexed elements.
49+
- **start**: starting value.
50+
- **x**: input [`Float32Array`][@stdlib/array/float32].
51+
- **strideX**: stride length.
52+
53+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to fill every other element:
54+
55+
```javascript
56+
var Float32Array = require( '@stdlib/array/float32' );
57+
58+
var x = new Float32Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
59+
60+
sunitspace( 3, 3.0, x, 2 );
61+
// x => <Float32Array>[ 3.0, 0.0, 4.0, 0.0, 5.0, 0.0 ]
62+
```
63+
64+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
65+
66+
```javascript
67+
var Float32Array = require( '@stdlib/array/float32' );
68+
69+
// Initial array...
70+
var x0 = new Float32Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
71+
72+
// Create an offset view...
73+
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
74+
75+
// Fill every other element...
76+
sunitspace( 3, 3.0, x1, 2 );
77+
// x0 => <Float32Array>[ 0.0, 3.0, 0.0, 4.0, 0.0, 5.0 ]
78+
```
79+
80+
#### sunitspace.ndarray( N, start, x, strideX, offsetX )
81+
82+
Fills a single-precision floating-point strided array with linearly spaced numeric elements which increment by `1` starting from a specified value using alternative indexing semantics.
83+
84+
```javascript
85+
var Float32Array = require( '@stdlib/array/float32' );
86+
87+
var x = new Float32Array( [ 0.0, 0.0, 0.0, 0.0 ] );
88+
89+
sunitspace.ndarray( x.length, 3.0, x, 1, 0 );
90+
// x => <Float32Array>[ 3.0, 4.0, 5.0, 6.0 ]
91+
```
92+
93+
The function has the following additional parameters:
94+
95+
- **offsetX**: starting index.
96+
97+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to access only the last three elements:
98+
99+
```javascript
100+
var Float32Array = require( '@stdlib/array/float32' );
101+
102+
var x = new Float32Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
103+
104+
sunitspace.ndarray( 3, 3.0, x, 1, x.length-3 );
105+
// x => <Float32Array>[ 0.0, 0.0, 0.0, 3.0, 4.0, 5.0 ]
106+
```
107+
108+
</section>
109+
110+
<!-- /.usage -->
111+
112+
<section class="notes">
113+
114+
## Notes
115+
116+
- If `N <= 0`, both functions return `x` unchanged.
117+
118+
</section>
119+
120+
<!-- /.notes -->
121+
122+
<section class="examples">
123+
124+
## Examples
125+
126+
<!-- eslint no-undef: "error" -->
127+
128+
```javascript
129+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
130+
var sunitspace = require( '@stdlib/blas/ext/base/sunitspace' );
131+
132+
var x = discreteUniform( 10, -100, 100, {
133+
'dtype': 'float32'
134+
});
135+
console.log( x );
136+
137+
sunitspace( x.length, 0.0, x, 1 );
138+
console.log( x );
139+
```
140+
141+
</section>
142+
143+
<!-- /.examples -->
144+
145+
<!-- C interface documentation. -->
146+
147+
* * *
148+
149+
<section class="c">
150+
151+
## C APIs
152+
153+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
154+
155+
<section class="intro">
156+
157+
</section>
158+
159+
<!-- /.intro -->
160+
161+
<!-- C usage documentation. -->
162+
163+
<section class="usage">
164+
165+
### Usage
166+
167+
```c
168+
#include "stdlib/blas/ext/base/sunitspace.h"
169+
```
170+
171+
#### stdlib_strided_sunitspace( N, start, \*X, strideX )
172+
173+
Fills a single-precision floating-point strided array with linearly spaced numeric elements which increment by `1` starting from a specified value.
174+
175+
```c
176+
float x[] = { 0.0f, 0.0f, 0.0f, 0.0f };
177+
178+
stdlib_strided_sunitspace( 4, 3.0f, x, 1 );
179+
```
180+
181+
The function accepts the following arguments:
182+
183+
- **N**: `[in] CBLAS_INT` number of indexed elements.
184+
- **start**: `[in] float` starting value.
185+
- **X**: `[out] float*` input array.
186+
- **strideX**: `[in] CBLAS_INT` stride length.
187+
188+
```c
189+
void API_SUFFIX(stdlib_strided_sunitspace)( const CBLAS_INT N, const float start, float *X, const CBLAS_INT strideX );
190+
```
191+
192+
#### stdlib_strided_sunitspace_ndarray( N, start, \*X, strideX, offsetX )
193+
194+
Fills a single-precision floating-point strided array with linearly spaced numeric elements which increment by `1` starting from a specified value using alternative indexing semantics.
195+
196+
```c
197+
float x[] = { 0.0f, 0.0f, 0.0f, 0.0f };
198+
199+
stdlib_strided_sunitspace_ndarray( 4, 3.0f, x, 1, 0 );
200+
```
201+
202+
The function accepts the following arguments:
203+
204+
- **N**: `[in] CBLAS_INT` number of indexed elements.
205+
- **start**: `[in] float` starting value.
206+
- **X**: `[out] float*` input array.
207+
- **strideX**: `[in] CBLAS_INT` stride length.
208+
- **offsetX**: `[in] CBLAS_INT` starting index.
209+
210+
```c
211+
void API_SUFFIX(stdlib_strided_sunitspace_ndarray)( const CBLAS_INT N, const float start, float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
212+
```
213+
214+
</section>
215+
216+
<!-- /.usage -->
217+
218+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
219+
220+
<section class="notes">
221+
222+
</section>
223+
224+
<!-- /.notes -->
225+
226+
<!-- C API usage examples. -->
227+
228+
<section class="examples">
229+
230+
### Examples
231+
232+
```c
233+
#include "stdlib/blas/ext/base/sunitspace.h"
234+
#include <stdio.h>
235+
236+
int main( void ) {
237+
// Create a strided array:
238+
float x[] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
239+
240+
// Specify the number of indexed elements:
241+
const int N = 8;
242+
243+
// Specify a stride:
244+
const int strideX = 1;
245+
246+
// Fill the array:
247+
stdlib_strided_sunitspace( N, 3.0f, x, strideX );
248+
249+
// Print the result:
250+
for ( int i = 0; i < 8; i++ ) {
251+
printf( "x[ %i ] = %f\n", i, x[ i ] );
252+
}
253+
}
254+
```
255+
256+
</section>
257+
258+
<!-- /.examples -->
259+
260+
</section>
261+
262+
<!-- /.c -->
263+
264+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
265+
266+
<section class="related">
267+
268+
</section>
269+
270+
<!-- /.related -->
271+
272+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
273+
274+
<section class="links">
275+
276+
[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32
277+
278+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
279+
280+
</section>
281+
282+
<!-- /.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 uniform = require( '@stdlib/random/array/uniform' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var format = require( '@stdlib/string/format' );
28+
var pkg = require( './../package.json' ).name;
29+
var sunitspace = require( './../lib/sunitspace.js' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'float32'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Create 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 = uniform( len, -10.0, 10.0, 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 y;
60+
var i;
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
y = sunitspace( x.length, 0.0, x, 1 );
65+
if ( isnanf( y[ i%x.length ] ) ) {
66+
b.fail( 'should not return NaN' );
67+
}
68+
}
69+
b.toc();
70+
if ( isnanf( y[ i%x.length ] ) ) {
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:len=%d', pkg, len ), f );
100+
}
101+
}
102+
103+
main();

0 commit comments

Comments
 (0)