Skip to content

Commit cfc58ab

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/base/ndarray/zunitspace
PR-URL: #11730 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#434
1 parent e5f860f commit cfc58ab

10 files changed

Lines changed: 952 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)