Skip to content

Commit bd86c8c

Browse files
committed
feat: add blas/ext/base/ndarray/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 899b839 commit bd86c8c

File tree

10 files changed

+863
-0
lines changed

10 files changed

+863
-0
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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 one-dimensional ndarray with linearly spaced values over a specified interval.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var glinspace = require( '@stdlib/blas/ext/base/ndarray/glinspace' );
37+
```
38+
39+
#### glinspace( arrays )
40+
41+
Fills a one-dimensional ndarray with linearly spaced values over a specified interval.
42+
43+
```javascript
44+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
45+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
46+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
47+
48+
var xbuf = [ 0.0, 0.0, 0.0, 0.0 ];
49+
var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
50+
51+
var start = scalar2ndarray( 0.0, {
52+
'dtype': 'generic'
53+
});
54+
55+
var end = scalar2ndarray( 3.0, {
56+
'dtype': 'generic'
57+
});
58+
59+
var endpoint = scalar2ndarray( true, {
60+
'dtype': 'bool'
61+
});
62+
63+
var out = glinspace( [ x, start, end, endpoint ] );
64+
// returns <ndarray>
65+
66+
var arr = ndarray2array( out );
67+
// returns [ 0.0, 1.0, 2.0, 3.0 ]
68+
```
69+
70+
The function has the following parameters:
71+
72+
- **arrays**: array-like object containing the following ndarrays in order:
73+
74+
1. a one-dimensional input ndarray.
75+
2. a zero-dimensional ndarray specifying the start of the interval.
76+
3. a zero-dimensional ndarray specifying the end of the interval.
77+
4. a zero-dimensional ndarray specifying whether to include the end of the interval when writing values to the input ndarray.
78+
79+
</section>
80+
81+
<!-- /.usage -->
82+
83+
<section class="notes">
84+
85+
## Notes
86+
87+
- Let `M` be the number of generated values. The spacing between values is thus given by
88+
89+
```text
90+
Δ = (end-start)/(M-1)
91+
```
92+
93+
- If an input ndarray has a single element and the function is supposed to include the end of the interval, the set of values written to an input ndarray only includes the end of the interval, but not the start of the interval.
94+
95+
- Otherwise, when an input ndarray has a single element and the function is not supposed to include the end of the interval, the set of values written to an input ndarray only includes the start of the interval, but not the end of the interval.
96+
97+
- If the start of the interval is less than end of the interval, the set of values written to an input ndarray will be written in ascending order, and, if the start of the interval is greater than the end of the interval, the set of written values will be in descending order.
98+
99+
- When an input ndarray contains at least two values and the function is supposed to include the end of the interval, the set of values written to an input ndarray is guaranteed to include the start and end interval values. Beware, however, that values between the interval bounds are subject to floating-point rounding errors.
100+
101+
- The input ndarray is **mutated**.
102+
103+
</section>
104+
105+
<!-- /.notes -->
106+
107+
<section class="examples">
108+
109+
## Examples
110+
111+
<!-- eslint no-undef: "error" -->
112+
113+
```javascript
114+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
115+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
116+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
117+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
118+
var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' );
119+
var glinspace = require( '@stdlib/blas/ext/base/ndarray/glinspace' );
120+
121+
var xbuf = discreteUniform( 10, -100, 100, {
122+
'dtype': 'generic'
123+
});
124+
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
125+
console.log( ndarray2array( x ) );
126+
127+
var start = scalar2ndarray( 0.0, {
128+
'dtype': 'generic'
129+
});
130+
console.log( 'Start: %d', ndarraylike2scalar( start ) );
131+
132+
var end = scalar2ndarray( 100.0, {
133+
'dtype': 'generic'
134+
});
135+
console.log( 'Stop: %d', ndarraylike2scalar( end ) );
136+
137+
var endpoint = scalar2ndarray( true, {
138+
'dtype': 'bool'
139+
});
140+
console.log( 'Endpoint: %s', ndarraylike2scalar( endpoint ) );
141+
142+
glinspace( [ x, start, end, endpoint ] );
143+
console.log( ndarray2array( x ) );
144+
```
145+
146+
</section>
147+
148+
<!-- /.examples -->
149+
150+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
151+
152+
<section class="related">
153+
154+
</section>
155+
156+
<!-- /.related -->
157+
158+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
159+
160+
<section class="links">
161+
162+
</section>
163+
164+
<!-- /.links -->
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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 pow = require( '@stdlib/math/base/special/pow' );
26+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
27+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
28+
var pkg = require( './../package.json' ).name;
29+
var glinspace = require( './../lib' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'generic'
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 endpoint;
50+
var start;
51+
var stop;
52+
var xbuf;
53+
var x;
54+
55+
xbuf = uniform( len, 0.0, 100.0, options );
56+
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
57+
58+
start = scalar2ndarray( 0.0, options );
59+
stop = scalar2ndarray( 100.0, options );
60+
endpoint = scalar2ndarray( true, {
61+
'dtype': 'bool'
62+
});
63+
64+
return benchmark;
65+
66+
function benchmark( b ) {
67+
var out;
68+
var i;
69+
70+
b.tic();
71+
for ( i = 0; i < b.iterations; i++ ) {
72+
out = glinspace( [ x, start, stop, endpoint ] );
73+
if ( typeof out !== 'object' ) {
74+
b.fail( 'should return an ndarray' );
75+
}
76+
}
77+
b.toc();
78+
if ( xbuf[ i%len ] !== xbuf[ i%len ] ) {
79+
b.fail( 'should not return NaN' );
80+
}
81+
b.pass( 'benchmark finished' );
82+
b.end();
83+
}
84+
}
85+
86+
87+
// MAIN //
88+
89+
/**
90+
* Main execution sequence.
91+
*
92+
* @private
93+
*/
94+
function main() {
95+
var len;
96+
var min;
97+
var max;
98+
var f;
99+
var i;
100+
101+
min = 1; // 10^min
102+
max = 6; // 10^max
103+
104+
for ( i = min; i <= max; i++ ) {
105+
len = pow( 10, i );
106+
f = createBenchmark( len );
107+
bench( pkg+':len='+len, f );
108+
}
109+
}
110+
111+
main();
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
{{alias}}( arrays )
3+
Fills a one-dimensional ndarray with linearly spaced values over a specified
4+
interval.
5+
6+
If an input ndarray has a single element and the function is supposed to
7+
include the end of the interval, the set of values written to an input
8+
ndarray only includes the end of the interval, but not the start of the
9+
interval.
10+
11+
Otherwise, when an input ndarray has a single element and the function is
12+
not supposed to include the end of the interval, the set of values written
13+
to an input ndarray only includes the start of the interval, but not the end
14+
of the interval.
15+
16+
If the start of the interval is less than end of the interval, the set of
17+
values written to an input ndarray will be written in ascending order, and,
18+
if the start of the interval is greater than the end of the interval, the
19+
set of written values will be in descending order.
20+
21+
When an input ndarray contains at least two values and the function is
22+
supposed to include the end of the interval, the set of values written to an
23+
input ndarray is guaranteed to include the start and end interval values.
24+
Beware, however, that values between the interval bounds are subject to
25+
floating-point rounding errors.
26+
27+
Parameters
28+
----------
29+
arrays: ArrayLikeObject<ndarray>
30+
Array-like object containing the following ndarrays in order:
31+
32+
- a one-dimensional input ndarray.
33+
- a zero-dimensional ndarray specifying the start of the interval.
34+
- a zero-dimensional ndarray specifying the end of the interval.
35+
- a zero-dimensional ndarray specifying whether to include the end of
36+
the interval when writing values to the input ndarray.
37+
38+
Returns
39+
-------
40+
out: ndarray
41+
Input ndarray.
42+
43+
Examples
44+
--------
45+
// Create the input ndarray:
46+
> var opts = { 'dtype': 'generic' };
47+
> var buf = [ 0.0, 0.0, 0.0, 0.0 ];
48+
> var x = {{alias:@stdlib/ndarray/array}}( buf, opts );
49+
50+
// Specify the start of the interval:
51+
> var start = {{alias:@stdlib/ndarray/from-scalar}}( 0.0, opts );
52+
53+
// Specify the end of the interval:
54+
> var stop = {{alias:@stdlib/ndarray/from-scalar}}( 3.0, opts );
55+
56+
// Specify whether to include the endpoint:
57+
> opts = { 'dtype': 'bool' };
58+
> var endpoint = {{alias:@stdlib/ndarray/from-scalar}}( true, opts );
59+
60+
// Fill the input ndarray:
61+
> {{alias}}( [ x, start, stop, endpoint ] )
62+
<ndarray>
63+
64+
> {{alias:@stdlib/ndarray/to-array}}( x )
65+
[ 0.0, 1.0, 2.0, 3.0 ]
66+
67+
See Also
68+
--------

0 commit comments

Comments
 (0)