Skip to content

Commit bad4f4c

Browse files
authored
feat: add blas/ext/base/cunitspace
PR-URL: #11708 Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#428
1 parent bf821ca commit bad4f4c

33 files changed

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

0 commit comments

Comments
 (0)