Skip to content

Commit 10ffd72

Browse files
committed
feat: add blas/ext/base/glinspace
--- 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: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - 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 e937de1 commit 10ffd72

15 files changed

Lines changed: 1946 additions & 0 deletions

File tree

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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+
# glinspace
22+
23+
> Fill a strided array with linearly spaced values over a specified interval.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var glinspace = require( '@stdlib/blas/ext/base/glinspace' );
31+
```
32+
33+
#### glinspace( N, start, stop, endpoint, x, strideX )
34+
35+
Fills a strided array with linearly spaced values over a specified interval.
36+
37+
```javascript
38+
var x = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
39+
40+
glinspace( x.length, 0.0, 7.0, true, x, 1 );
41+
// x => [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ]
42+
```
43+
44+
The function has the following parameters:
45+
46+
- **N**: number of indexed elements.
47+
- **start**: start of interval.
48+
- **stop**: end of interval.
49+
- **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)`.
50+
- **x**: input array.
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 x = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
57+
58+
glinspace( 4, 1.0, 5.0, false, x, 2 );
59+
// x => [ 1.0, 0.0, 2.0, 0.0, 3.0, 0.0, 4.0, 0.0 ]
60+
```
61+
62+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
63+
64+
```javascript
65+
var Float64Array = require( '@stdlib/array/float64' );
66+
67+
// Initial array...
68+
var x0 = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
69+
70+
// Create an offset view...
71+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
72+
73+
// Fill every other element...
74+
glinspace( 3, 1.0, 3.0, true, x1, 2 );
75+
// x0 => <Float64Array>[ 0.0, 1.0, 0.0, 2.0, 0.0, 3.0 ]
76+
```
77+
78+
#### glinspace.ndarray( N, start, stop, endpoint, x, strideX, offsetX )
79+
80+
Fills a strided array with linearly spaced values over a specified interval using alternative indexing semantics.
81+
82+
```javascript
83+
var x = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
84+
85+
glinspace.ndarray( x.length, 0.0, 7.0, true, x, 1, 0 );
86+
// x => [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ]
87+
```
88+
89+
The function has the following additional parameters:
90+
91+
- **offsetX**: starting index.
92+
93+
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:
94+
95+
```javascript
96+
var x = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
97+
98+
glinspace.ndarray( 3, 1.0, 3.0, true, x, 1, x.length-3 );
99+
// x => [ 0.0, 0.0, 0.0, 1.0, 2.0, 3.0 ]
100+
```
101+
102+
</section>
103+
104+
<!-- /.usage -->
105+
106+
<section class="notes">
107+
108+
## Notes
109+
110+
- 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
111+
112+
```text
113+
Δ = (stop-start)/(M-1)
114+
```
115+
116+
- When the number of generated values is greater than `1` and `endpoint` is `true`, the set of values written to a provided input array is guaranteed to include the `start` and `stop` values. Beware, however, that values between `start` and `stop` are subject to floating-point rounding errors. Hence,
117+
118+
```javascript
119+
var x = [ 0.0, 0.0, 0.0 ];
120+
121+
glinspace( 3, 0.0, 1.0, true, x, 1 );
122+
// x =>[ 0.0, ~0.5, 1.0 ]
123+
```
124+
125+
where `x[1]` is only guaranteed to be approximately equal to `0.5`.
126+
127+
- When `N = 1` and `endpoint` is `false`, only the `start` value is written to a provided input array. When `N = 1` and `endpoint` is `true`, only the `stop` value is written to a provided input array.
128+
129+
- If `start < stop`, values are written to a provided input array in ascending order; otherwise, values are written in descending order.
130+
131+
- If `N <= 0`, both functions return `x` unchanged.
132+
133+
</section>
134+
135+
<!-- /.notes -->
136+
137+
<section class="examples">
138+
139+
## Examples
140+
141+
<!-- eslint no-undef: "error" -->
142+
143+
```javascript
144+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
145+
var glinspace = require( '@stdlib/blas/ext/base/glinspace' );
146+
147+
var x = discreteUniform( 10, -100, 100, {
148+
'dtype': 'generic'
149+
});
150+
console.log( x );
151+
152+
glinspace( x.length, 0.0, 10.0, true, x, 1 );
153+
console.log( x );
154+
```
155+
156+
</section>
157+
158+
<!-- /.examples -->
159+
160+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
161+
162+
<section class="related">
163+
164+
</section>
165+
166+
<!-- /.related -->
167+
168+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
169+
170+
<section class="links">
171+
172+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
173+
174+
</section>
175+
176+
<!-- /.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 isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var pkg = require( './../package.json' ).name;
28+
var glinspace = require( './../lib' );
29+
30+
31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'generic'
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 = glinspace( x.length, 0.0, i, true, x, 1 );
58+
if ( isnan( y[ i%x.length ] ) ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
}
62+
b.toc();
63+
if ( isnan( 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();
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var pkg = require( './../package.json' ).name;
28+
var glinspace = require( './../lib/ndarray.js' );
29+
30+
31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'generic'
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 = glinspace( x.length, 0.0, i, true, x, 1, 0 );
58+
if ( isnan( y[ i%x.length ] ) ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
}
62+
b.toc();
63+
if ( isnan( 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+':ndarray:len='+len, f );
88+
}
89+
}
90+
91+
main();

0 commit comments

Comments
 (0)