Skip to content

Commit f53f0cf

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/base/done-to
PR-URL: #11161 Closes: stdlib-js/metr-issue-tracker#218 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent e146f6e commit f53f0cf

33 files changed

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