Skip to content

Commit ef06e88

Browse files
feat: add blas/ext/base/ndarray/zxpy
PR-URL: #13037 Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#734
1 parent ce9ed9a commit ef06e88

10 files changed

Lines changed: 943 additions & 0 deletions

File tree

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
# zxpy
22+
23+
> Add elements of a one-dimensional double-precision complex floating-point ndarray to the corresponding elements of a second one-dimensional double-precision complex floating-point ndarray and assign the results to the second ndarray.
24+
25+
<section class="intro">
26+
27+
This BLAS extension implements the operation
28+
29+
<!-- <equation class="equation" label="eq:xpy" align="center" raw="\mathbf{y} = \mathbf{x} + \mathbf{y}" alt="Equation for xpy operation."> -->
30+
31+
```math
32+
\mathbf{y} = \mathbf{x} + \mathbf{y}
33+
```
34+
35+
<!-- </equation> -->
36+
37+
This API is a specialized version of the package [`@stdlib/blas/ext/base/ndarray/zaxpby`][@stdlib/blas/ext/base/ndarray/zaxpby] with `α = 1` and `β = 1` and performs element-wise addition between two complex vectors.
38+
39+
</section>
40+
41+
<!-- /.intro -->
42+
43+
<section class="usage">
44+
45+
## Usage
46+
47+
```javascript
48+
var zxpy = require( '@stdlib/blas/ext/base/ndarray/zxpy' );
49+
```
50+
51+
#### zxpy( arrays )
52+
53+
Adds elements of a one-dimensional double-precision complex floating-point ndarray to the corresponding elements of a second one-dimensional double-precision complex floating-point ndarray and assigns the results to the second ndarray.
54+
55+
```javascript
56+
var Complex128Vector = require( '@stdlib/ndarray/vector/complex128' );
57+
58+
var x = new Complex128Vector( [ 1.0, 2.0, 3.0, -1.0, 0.0, 1.0 ] );
59+
var y = new Complex128Vector( [ 2.0, 1.0, -1.0, 3.0, 4.0, 0.0 ] );
60+
61+
zxpy( [ x, y ] );
62+
// y => <ndarray>[ <Complex128>[ 3.0, 3.0 ], <Complex128>[ 2.0, 2.0 ], <Complex128>[ 4.0, 1.0 ] ]
63+
```
64+
65+
The function has the following parameters:
66+
67+
- **arrays**: array-like object containing the following ndarrays:
68+
69+
- a one-dimensional input ndarray.
70+
- a one-dimensional output ndarray.
71+
72+
</section>
73+
74+
<!-- /.usage -->
75+
76+
<section class="notes">
77+
78+
## Notes
79+
80+
- The output ndarray is modified **in-place** (i.e., the output ndarray is **mutated**).
81+
82+
</section>
83+
84+
<!-- /.notes -->
85+
86+
<section class="examples">
87+
88+
## Examples
89+
90+
<!-- eslint no-undef: "error" -->
91+
92+
```javascript
93+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
94+
var Complex128Vector = require( '@stdlib/ndarray/vector/complex128' );
95+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
96+
var zxpy = require( '@stdlib/blas/ext/base/ndarray/zxpy' );
97+
98+
var opts = {
99+
'dtype': 'float64'
100+
};
101+
102+
var x = new Complex128Vector( discreteUniform( 20, -100, 100, opts ) );
103+
console.log( ndarray2array( x ) );
104+
105+
var y = new Complex128Vector( discreteUniform( 20, -100, 100, opts ) );
106+
console.log( ndarray2array( y ) );
107+
108+
zxpy( [ x, y ] );
109+
console.log( ndarray2array( y ) );
110+
```
111+
112+
</section>
113+
114+
<!-- /.examples -->
115+
116+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
117+
118+
<section class="related">
119+
120+
</section>
121+
122+
<!-- /.related -->
123+
124+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
125+
126+
<section class="links">
127+
128+
[@stdlib/blas/ext/base/ndarray/zaxpby]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/@stdlib/blas/ext/base/ndarray/zaxpby
129+
130+
</section>
131+
132+
<!-- /.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 uniform = require( '@stdlib/random/array/uniform' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var Complex128Vector = require( '@stdlib/ndarray/vector/complex128' );
27+
var format = require( '@stdlib/string/format' );
28+
var pkg = require( './../package.json' ).name;
29+
var zxpy = require( './../lib' );
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Creates a benchmark function.
36+
*
37+
* @private
38+
* @param {PositiveInteger} len - ndarray length
39+
* @returns {Function} benchmark function
40+
*/
41+
function createBenchmark( len ) {
42+
var xbuf;
43+
var ybuf;
44+
var x;
45+
var y;
46+
47+
xbuf = uniform( len*2, -100.0, 100.0, {
48+
'dtype': 'float64'
49+
});
50+
ybuf = uniform( len*2, -100.0, 100.0, {
51+
'dtype': 'float64'
52+
});
53+
x = new Complex128Vector( xbuf.buffer );
54+
y = new Complex128Vector( ybuf.buffer );
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 = zxpy( [ x, y ] );
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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
{{alias}}( arrays )
3+
Adds elements of a one-dimensional double-precision complex floating-point
4+
ndarray to the corresponding elements of a second one-dimensional double-
5+
precision complex floating-point ndarray and assigns the results to the
6+
second ndarray.
7+
8+
The output ndarray is modified *in-place* (i.e., the output ndarray is
9+
*mutated*).
10+
11+
Parameters
12+
----------
13+
arrays: ArrayLikeObject<ndarray>
14+
Array-like object containing the following ndarrays:
15+
16+
- a one-dimensional input ndarray.
17+
- a one-dimensional output ndarray.
18+
19+
Returns
20+
-------
21+
out: ndarray
22+
Output ndarray.
23+
24+
Examples
25+
--------
26+
> var bufX = [ 1.0, 2.0, 3.0, -1.0, 0.0, 1.0 ];
27+
> var bufY = [ 2.0, 1.0, -1.0, 3.0, 4.0, 0.0 ];
28+
> var x = new {{alias:@stdlib/ndarray/vector/complex128}}( bufX );
29+
> var y = new {{alias:@stdlib/ndarray/vector/complex128}}( bufY );
30+
> {{alias}}( [ x, y ] )
31+
<ndarray>[ <Complex128>[ 3,3 ], <Complex128>[ 2,2 ], <Complex128>[ 4,1 ] ]
32+
33+
See Also
34+
--------
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
* Adds elements of a one-dimensional double-precision complex floating-point ndarray to the corresponding elements of a second one-dimensional double-precision complex floating-point ndarray and assigns the results to the second ndarray.
27+
*
28+
* ## Notes
29+
*
30+
* - The function expects the following ndarrays:
31+
*
32+
* - a one-dimensional input ndarray.
33+
* - a one-dimensional output ndarray.
34+
*
35+
* @param arrays - array-like object containing ndarrays
36+
* @returns output ndarray
37+
*
38+
* @example
39+
* var Complex128Vector = require( '@stdlib/ndarray/vector/complex128' );
40+
*
41+
* var x = new Complex128Vector( [ 1.0, 2.0, 3.0, -1.0, 0.0, 1.0 ] );
42+
* var y = new Complex128Vector( [ 2.0, 1.0, -1.0, 3.0, 4.0, 0.0 ] );
43+
*
44+
* var out = zxpy( [ x, y ] );
45+
* // returns <ndarray>[ <Complex128>[ 3.0, 3.0 ], <Complex128>[ 2.0, 2.0 ], <Complex128>[ 4.0, 1.0 ] ]
46+
*/
47+
declare function zxpy( arrays: [ complex128ndarray, complex128ndarray ] ): complex128ndarray;
48+
49+
50+
// EXPORTS //
51+
52+
export = zxpy;
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 zxpy = 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 y = zeros( [ 10 ], {
33+
'dtype': 'complex128'
34+
});
35+
36+
zxpy( [ x, y ] ); // $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+
zxpy( '10' ); // $ExpectError
42+
zxpy( 5 ); // $ExpectError
43+
zxpy( true ); // $ExpectError
44+
zxpy( false ); // $ExpectError
45+
zxpy( null ); // $ExpectError
46+
zxpy( undefined ); // $ExpectError
47+
zxpy( [] ); // $ExpectError
48+
zxpy( {} ); // $ExpectError
49+
zxpy( ( 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 y = zeros( [ 10 ], {
58+
'dtype': 'complex128'
59+
});
60+
61+
zxpy(); // $ExpectError
62+
zxpy( [ x, y ], {} ); // $ExpectError
63+
}

0 commit comments

Comments
 (0)