Skip to content

Commit 58a6c2d

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/base/ndarray/dunitspace
PR-URL: #11727 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#431
1 parent 10c631c commit 58a6c2d

10 files changed

Lines changed: 826 additions & 0 deletions

File tree

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
# dunitspace
22+
23+
> Fill a one-dimensional double-precision floating-point ndarray with linearly spaced numeric elements which increment by `1` starting from a specified value.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var dunitspace = require( '@stdlib/blas/ext/base/ndarray/dunitspace' );
37+
```
38+
39+
#### dunitspace( arrays )
40+
41+
Fills a one-dimensional double-precision floating-point ndarray with linearly spaced numeric elements which increment by `1` starting from a specified value.
42+
43+
```javascript
44+
var Float64Vector = require( '@stdlib/ndarray/vector/float64' );
45+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
46+
47+
var x = new Float64Vector( [ 0.0, 0.0, 0.0, 0.0 ] );
48+
// returns <ndarray>[ 0.0, 0.0, 0.0, 0.0 ]
49+
50+
var start = scalar2ndarray( 3.0, {
51+
'dtype': 'float64'
52+
});
53+
54+
var out = dunitspace( [ x, start ] );
55+
// returns <ndarray>[ 3.0, 4.0, 5.0, 6.0 ]
56+
```
57+
58+
The function has the following parameters:
59+
60+
- **arrays**: array-like object containing the following ndarrays:
61+
62+
- a one-dimensional input ndarray.
63+
- a zero-dimensional ndarray containing a starting value.
64+
65+
</section>
66+
67+
<!-- /.usage -->
68+
69+
<section class="notes">
70+
71+
## Notes
72+
73+
- The input ndarray is modified **in-place** (i.e., the input ndarray is **mutated**).
74+
75+
</section>
76+
77+
<!-- /.notes -->
78+
79+
<section class="examples">
80+
81+
## Examples
82+
83+
<!-- eslint no-undef: "error" -->
84+
85+
```javascript
86+
var zeros = require( '@stdlib/ndarray/zeros' );
87+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
88+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
89+
var dunitspace = require( '@stdlib/blas/ext/base/ndarray/dunitspace' );
90+
91+
var opts = {
92+
'dtype': 'float64'
93+
};
94+
95+
var x = zeros( [ 10 ], opts );
96+
console.log( ndarray2array( x ) );
97+
98+
var start = scalar2ndarray( 3.0, opts );
99+
100+
dunitspace( [ x, start ] );
101+
console.log( ndarray2array( x ) );
102+
```
103+
104+
</section>
105+
106+
<!-- /.examples -->
107+
108+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
109+
110+
<section class="related">
111+
112+
</section>
113+
114+
<!-- /.related -->
115+
116+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
117+
118+
<section class="links">
119+
120+
</section>
121+
122+
<!-- /.links -->
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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 zeros = require( '@stdlib/ndarray/zeros' );
25+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
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 dunitspace = require( './../lib' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'float64'
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 start;
50+
var x;
51+
52+
start = scalar2ndarray( 3.0, options );
53+
x = zeros( [ len ], options );
54+
55+
return benchmark;
56+
57+
/**
58+
* Benchmark function.
59+
*
60+
* @private
61+
* @param {Benchmark} b - benchmark instance
62+
*/
63+
function benchmark( b ) {
64+
var out;
65+
var i;
66+
67+
b.tic();
68+
for ( i = 0; i < b.iterations; i++ ) {
69+
out = dunitspace( [ x, start ] );
70+
if ( typeof out !== 'object' ) {
71+
b.fail( 'should return an ndarray' );
72+
}
73+
}
74+
b.toc();
75+
if ( typeof out !== 'object' ) {
76+
b.fail( 'should return an ndarray' );
77+
}
78+
b.pass( 'benchmark finished' );
79+
b.end();
80+
}
81+
}
82+
83+
84+
// MAIN //
85+
86+
/**
87+
* Main execution sequence.
88+
*
89+
* @private
90+
*/
91+
function main() {
92+
var len;
93+
var min;
94+
var max;
95+
var f;
96+
var i;
97+
98+
min = 1; // 10^min
99+
max = 6; // 10^max
100+
101+
for ( i = min; i <= max; i++ ) {
102+
len = pow( 10, i );
103+
f = createBenchmark( len );
104+
bench( format( '%s:len=%d', pkg, len ), f );
105+
}
106+
}
107+
108+
main();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
{{alias}}( arrays )
3+
Fills a one-dimensional double-precision floating-point ndarray with
4+
linearly spaced numeric elements which increment by `1` starting from a
5+
specified value.
6+
7+
The input ndarray is modified *in-place* (i.e., the input ndarray is
8+
*mutated*).
9+
10+
Parameters
11+
----------
12+
arrays: ArrayLikeObject<ndarray>
13+
Array-like object containing the following ndarrays:
14+
15+
- a one-dimensional input ndarray.
16+
- a zero-dimensional ndarray containing a starting value.
17+
18+
Returns
19+
-------
20+
out: ndarray
21+
Input ndarray.
22+
23+
Examples
24+
--------
25+
> var x = new {{alias:@stdlib/ndarray/vector/float64}}( [ 0.0, 0.0, 0.0, 0.0 ] );
26+
> var start = {{alias:@stdlib/ndarray/from-scalar}}( 3.0, { 'dtype': 'float64' } );
27+
> {{alias}}( [ x, start ] )
28+
<ndarray>[ 3.0, 4.0, 5.0, 6.0 ]
29+
30+
See Also
31+
--------
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { float64ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Fills a one-dimensional double-precision floating-point ndarray with linearly spaced numeric elements which increment by `1` starting from a specified value.
27+
*
28+
* ## Notes
29+
*
30+
* - The function expects the following ndarrays:
31+
*
32+
* - a one-dimensional input ndarray.
33+
* - a zero-dimensional ndarray containing a starting value.
34+
*
35+
* @param arrays - array-like object containing ndarrays
36+
* @returns input ndarray
37+
*
38+
* @example
39+
* var Float64Vector = require( '@stdlib/ndarray/vector/float64' );
40+
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
41+
*
42+
* var x = new Float64Vector( [ 0.0, 0.0, 0.0, 0.0 ] );
43+
* // returns <ndarray>[ 0.0, 0.0, 0.0, 0.0 ]
44+
*
45+
* var start = scalar2ndarray( 3.0, {
46+
* 'dtype': 'float64'
47+
* });
48+
*
49+
* var out = dunitspace( [ x, start ] );
50+
* // returns <ndarray>[ 3.0, 4.0, 5.0, 6.0 ]
51+
*/
52+
declare function dunitspace( arrays: [ float64ndarray, float64ndarray ] ): float64ndarray;
53+
54+
55+
// EXPORTS //
56+
57+
export = dunitspace;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
/* eslint-disable space-in-parens */
20+
21+
import zeros = require( '@stdlib/ndarray/zeros' );
22+
import dunitspace = require( './index' );
23+
24+
25+
// TESTS //
26+
27+
// The function returns an ndarray...
28+
{
29+
const x = zeros( [ 10 ], {
30+
'dtype': 'float64'
31+
});
32+
const start = zeros( [], {
33+
'dtype': 'float64'
34+
});
35+
36+
dunitspace( [ x, start ] ); // $ExpectType float64ndarray
37+
}
38+
39+
// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays...
40+
{
41+
dunitspace( '10' ); // $ExpectError
42+
dunitspace( 10 ); // $ExpectError
43+
dunitspace( true ); // $ExpectError
44+
dunitspace( false ); // $ExpectError
45+
dunitspace( null ); // $ExpectError
46+
dunitspace( undefined ); // $ExpectError
47+
dunitspace( [] ); // $ExpectError
48+
dunitspace( {} ); // $ExpectError
49+
dunitspace( ( x: number ): number => x ); // $ExpectError
50+
}
51+
52+
// The compiler throws an error if the function is provided an unsupported number of arguments...
53+
{
54+
const x = zeros( [ 10 ], {
55+
'dtype': 'float64'
56+
});
57+
const start = zeros( [], {
58+
'dtype': 'float64'
59+
});
60+
61+
dunitspace(); // $ExpectError
62+
dunitspace( [ x, start ], {} ); // $ExpectError
63+
}

0 commit comments

Comments
 (0)