Skip to content

Commit 30f0d38

Browse files
committed
feat: add blas/ext/base/slinspace
--- 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: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - 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 c46348e commit 30f0d38

33 files changed

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

0 commit comments

Comments
 (0)