Skip to content

Commit 5e2dad8

Browse files
feat: add blas/ext/base/zwapx
PR-URL: #13026 Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#738
1 parent 4154fdc commit 5e2dad8

33 files changed

Lines changed: 5017 additions & 0 deletions
Lines changed: 374 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,374 @@
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+
# zwapx
22+
23+
> Add a scalar constant to each element in a double-precision complex floating-point strided array `x` and assign the results to elements in a double-precision complex floating-point strided array `w`.
24+
25+
<section class="intro">
26+
27+
This BLAS extension implements the operation
28+
29+
<!-- <equation class="equation" label="eq:wapx" align="center" raw="\mathbf{w} = \mathbf{x} + \alpha" alt="Equation for wapx operation."> -->
30+
31+
```math
32+
\mathbf{w} = \mathbf{x} + \alpha
33+
```
34+
35+
<!-- </equation> -->
36+
37+
This API is complementary to the package [`@stdlib/blas/ext/base/zapx`][@stdlib/blas/ext/base/zapx], which performs an in-place update.
38+
39+
</section>
40+
41+
<!-- /.intro -->
42+
43+
<section class="usage">
44+
45+
## Usage
46+
47+
```javascript
48+
var zwapx = require( '@stdlib/blas/ext/base/zwapx' );
49+
```
50+
51+
#### zwapx( N, alpha, x, strideX, w, strideW )
52+
53+
Adds a scalar constant to each element in a double-precision complex floating-point strided array `x` and assigns the results to elements in a double-precision complex floating-point strided array `w`.
54+
55+
```javascript
56+
var Complex128Array = require( '@stdlib/array/complex128' );
57+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
58+
59+
var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
60+
var w = new Complex128Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
61+
var alpha = new Complex128( 5.0, 3.0 );
62+
63+
zwapx( x.length, alpha, x, 1, w, 1 );
64+
// w => <Complex128Array>[ 6.0, 5.0, 8.0, 7.0, 10.0, 9.0, 12.0, 11.0 ]
65+
```
66+
67+
The function has the following parameters:
68+
69+
- **N**: number of indexed elements.
70+
- **alpha**: scalar constant.
71+
- **x**: input [`Complex128Array`][@stdlib/array/complex128].
72+
- **strideX**: stride length for `x`.
73+
- **w**: output [`Complex128Array`][@stdlib/array/complex128].
74+
- **strideW**: stride length for `w`.
75+
76+
The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to add `alpha` to every other element in `x` and assign the results to every other element in `w`:
77+
78+
```javascript
79+
var Complex128Array = require( '@stdlib/array/complex128' );
80+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
81+
82+
var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
83+
var w = new Complex128Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
84+
85+
var alpha = new Complex128( 5.0, 3.0 );
86+
87+
zwapx( 2, alpha, x, 2, w, 2 );
88+
// w => <Complex128Array>[ 6.0, 5.0, 0.0, 0.0, 10.0, 9.0, 0.0, 0.0 ]
89+
```
90+
91+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
92+
93+
<!-- eslint-disable max-len -->
94+
95+
```javascript
96+
var Complex128Array = require( '@stdlib/array/complex128' );
97+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
98+
99+
// Initial arrays...
100+
var x0 = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 ] );
101+
var w0 = new Complex128Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
102+
103+
// Create offset views...
104+
var x1 = new Complex128Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
105+
var w1 = new Complex128Array( w0.buffer, w0.BYTES_PER_ELEMENT*2 ); // start at 3rd element
106+
107+
var alpha = new Complex128( 5.0, 3.0 );
108+
109+
zwapx( 3, alpha, x1, 1, w1, 1 );
110+
// w0 => <Complex128Array>[ 0.0, 0.0, 0.0, 0.0, 8.0, 7.0, 10.0, 9.0, 12.0, 11.0 ]
111+
```
112+
113+
#### zwapx.ndarray( N, alpha, x, strideX, offsetX, w, strideW, offsetW )
114+
115+
Adds a scalar constant to each element in a double-precision complex floating-point strided array `x` and assigns the results to elements in a double-precision complex floating-point strided array `w` using alternative indexing semantics.
116+
117+
```javascript
118+
var Complex128Array = require( '@stdlib/array/complex128' );
119+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
120+
121+
var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
122+
var w = new Complex128Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
123+
var alpha = new Complex128( 5.0, 3.0 );
124+
125+
zwapx.ndarray( x.length, alpha, x, 1, 0, w, 1, 0 );
126+
// w => <Complex128Array>[ 6.0, 5.0, 8.0, 7.0, 10.0, 9.0, 12.0, 11.0 ]
127+
```
128+
129+
The function has the following additional parameters:
130+
131+
- **offsetX**: starting index for `x`.
132+
- **offsetW**: starting index for `w`.
133+
134+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on starting indices. For example, to add `alpha` to the last three elements of `x` and assign the results to the last three elements of `w`:
135+
136+
<!-- eslint-disable max-len -->
137+
138+
```javascript
139+
var Complex128Array = require( '@stdlib/array/complex128' );
140+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
141+
142+
var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 ] );
143+
var w = new Complex128Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
144+
145+
var alpha = new Complex128( 5.0, 3.0 );
146+
147+
zwapx.ndarray( 3, alpha, x, 1, x.length-3, w, 1, w.length-3 );
148+
// w => <Complex128Array>[ 0.0, 0.0, 0.0, 0.0, 10.0, 9.0, 12.0, 11.0, 14.0, 13.0 ]
149+
```
150+
151+
</section>
152+
153+
<!-- /.usage -->
154+
155+
<section class="notes">
156+
157+
## Notes
158+
159+
- If `N <= 0`, both functions return `w` unchanged.
160+
161+
</section>
162+
163+
<!-- /.notes -->
164+
165+
<section class="examples">
166+
167+
## Examples
168+
169+
<!-- eslint no-undef: "error" -->
170+
171+
```javascript
172+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
173+
var Complex128Array = require( '@stdlib/array/complex128' );
174+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
175+
var logEach = require( '@stdlib/console/log-each' );
176+
var zwapx = require( '@stdlib/blas/ext/base/zwapx' );
177+
178+
var xbuf = discreteUniform( 20, -100, 100, {
179+
'dtype': 'float64'
180+
});
181+
var wbuf = discreteUniform( 20, -100, 100, {
182+
'dtype': 'float64'
183+
});
184+
var x = new Complex128Array( xbuf.buffer );
185+
var w = new Complex128Array( wbuf.buffer );
186+
var alpha = new Complex128( 5.0, 3.0 );
187+
188+
zwapx( x.length, alpha, x, 1, w, 1 );
189+
logEach( '%s', w );
190+
```
191+
192+
</section>
193+
194+
<!-- /.examples -->
195+
196+
<!-- C interface documentation. -->
197+
198+
* * *
199+
200+
<section class="c">
201+
202+
## C APIs
203+
204+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
205+
206+
<section class="intro">
207+
208+
</section>
209+
210+
<!-- /.intro -->
211+
212+
<!-- C usage documentation. -->
213+
214+
<section class="usage">
215+
216+
### Usage
217+
218+
```c
219+
#include "stdlib/blas/ext/base/zwapx.h"
220+
```
221+
222+
#### stdlib_strided_zwapx( N, alpha, \*X, strideX, \*W, strideW )
223+
224+
Adds a scalar constant to each element in a double-precision complex floating-point strided array `X` and assigns the results to elements in a double-precision complex floating-point strided array `W`.
225+
226+
```c
227+
#include "stdlib/complex/float64/ctor.h"
228+
229+
const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
230+
double w[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
231+
const stdlib_complex128_t alpha = stdlib_complex128( 5.0, 3.0 );
232+
233+
stdlib_strided_zwapx( 4, alpha, (const stdlib_complex128_t *)x, 1, (stdlib_complex128_t *)w, 1 );
234+
```
235+
236+
The function accepts the following arguments:
237+
238+
- **N**: `[in] CBLAS_INT` number of indexed elements.
239+
- **alpha**: `[in] stdlib_complex128_t` scalar constant.
240+
- **X**: `[in] stdlib_complex128_t*` input array.
241+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
242+
- **W**: `[out] stdlib_complex128_t*` output array.
243+
- **strideW**: `[in] CBLAS_INT` stride length for `W`.
244+
245+
```c
246+
void stdlib_strided_zwapx( const CBLAS_INT N, const stdlib_complex128_t alpha, const stdlib_complex128_t *X, const CBLAS_INT strideX, stdlib_complex128_t *W, const CBLAS_INT strideW );
247+
```
248+
249+
<!-- lint disable maximum-heading-length -->
250+
251+
#### stdlib_strided_zwapx_ndarray( N, alpha, \*X, strideX, offsetX, \*W, strideW, offsetW )
252+
253+
<!-- lint enable maximum-heading-length -->
254+
255+
Adds a scalar constant to each element in a double-precision complex floating-point strided array `X` and assigns the results to elements in a double-precision complex floating-point strided array `W` using alternative indexing semantics.
256+
257+
```c
258+
#include "stdlib/complex/float64/ctor.h"
259+
260+
const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
261+
double w[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
262+
const stdlib_complex128_t alpha = stdlib_complex128( 5.0, 3.0 );
263+
264+
stdlib_strided_zwapx_ndarray( 4, alpha, (const stdlib_complex128_t *)x, 1, 0, (stdlib_complex128_t *)w, 1, 0 );
265+
```
266+
267+
The function accepts the following arguments:
268+
269+
- **N**: `[in] CBLAS_INT` number of indexed elements.
270+
- **alpha**: `[in] stdlib_complex128_t` scalar constant.
271+
- **X**: `[in] stdlib_complex128_t*` input array.
272+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
273+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
274+
- **W**: `[out] stdlib_complex128_t*` output array.
275+
- **strideW**: `[in] CBLAS_INT` stride length for `W`.
276+
- **offsetW**: `[in] CBLAS_INT` starting index for `W`.
277+
278+
```c
279+
void stdlib_strided_zwapx_ndarray( const CBLAS_INT N, const stdlib_complex128_t alpha, const stdlib_complex128_t *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, stdlib_complex128_t *W, const CBLAS_INT strideW, const CBLAS_INT offsetW );
280+
```
281+
282+
</section>
283+
284+
<!-- /.usage -->
285+
286+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
287+
288+
<section class="notes">
289+
290+
</section>
291+
292+
<!-- /.notes -->
293+
294+
<!-- C API usage examples. -->
295+
296+
<section class="examples">
297+
298+
### Examples
299+
300+
```c
301+
#include "stdlib/blas/ext/base/zwapx.h"
302+
#include "stdlib/complex/float64/ctor.h"
303+
#include "stdlib/complex/float64/real.h"
304+
#include "stdlib/complex/float64/imag.h"
305+
#include <stdio.h>
306+
307+
int main( void ) {
308+
// Create strided arrays:
309+
const stdlib_complex128_t x[] = {
310+
stdlib_complex128( 1.0, 2.0 ),
311+
stdlib_complex128( 3.0, 4.0 ),
312+
stdlib_complex128( 5.0, 6.0 ),
313+
stdlib_complex128( 7.0, 8.0 )
314+
};
315+
stdlib_complex128_t w[] = {
316+
stdlib_complex128( 0.0, 0.0 ),
317+
stdlib_complex128( 0.0, 0.0 ),
318+
stdlib_complex128( 0.0, 0.0 ),
319+
stdlib_complex128( 0.0, 0.0 )
320+
};
321+
322+
// Specify the number of indexed elements:
323+
const int N = 4;
324+
325+
// Specify strides:
326+
const int strideX = 1;
327+
const int strideW = 1;
328+
329+
// Define a scalar constant:
330+
stdlib_complex128_t alpha = stdlib_complex128( 5.0, 3.0 );
331+
332+
// Add a constant to each element in `x` and assign to `w`:
333+
stdlib_strided_zwapx( N, alpha, x, strideX, w, strideW );
334+
335+
// Print the result:
336+
for ( int i = 0; i < N; i++ ) {
337+
printf( "w[ %i ] = %lf + %lfi\n", i, stdlib_complex128_real( w[ i ] ), stdlib_complex128_imag( w[ i ] ) );
338+
}
339+
}
340+
```
341+
342+
</section>
343+
344+
<!-- /.examples -->
345+
346+
</section>
347+
348+
<!-- /.c -->
349+
350+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
351+
352+
<section class="related">
353+
354+
</section>
355+
356+
<!-- /.related -->
357+
358+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
359+
360+
<section class="links">
361+
362+
[@stdlib/array/complex128]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex128
363+
364+
[@stdlib/blas/ext/base/zapx]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/zapx
365+
366+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
367+
368+
<!-- <related-links> -->
369+
370+
<!-- </related-links> -->
371+
372+
</section>
373+
374+
<!-- /.links -->

0 commit comments

Comments
 (0)