Skip to content

Commit 076b86b

Browse files
authored
feat: add blas/base/ndarray/zaxpy
PR-URL: #11507 Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent c28307a commit 076b86b

File tree

10 files changed

+1022
-0
lines changed

10 files changed

+1022
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
# zaxpy
22+
23+
> Multiply a one-dimensional double-precision complex floating-point ndarray `x` by a constant `alpha` and add the result to a one-dimensional double-precision complex floating-point ndarray `y`.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var zaxpy = require( '@stdlib/blas/base/ndarray/zaxpy' );
37+
```
38+
39+
#### zaxpy( arrays )
40+
41+
Multiplies a one-dimensional double-precision complex floating-point ndarray `x` by a constant `alpha` and adds the result to a one-dimensional double-precision complex floating-point ndarray `y`.
42+
43+
```javascript
44+
var Complex128Array = require( '@stdlib/array/complex128' );
45+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
46+
var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' );
47+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
48+
49+
var xbuf = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
50+
var x = new ndarray( 'complex128', xbuf, [ 3 ], [ 1 ], 0, 'row-major' );
51+
52+
var ybuf = new Complex128Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
53+
var y = new ndarray( 'complex128', ybuf, [ 3 ], [ 1 ], 0, 'row-major' );
54+
55+
var alpha = scalar2ndarray( new Complex128( 1.0, 2.0 ), 'complex128', 'row-major' );
56+
var z = zaxpy( [ x, y, alpha ] );
57+
// returns <ndarray>[ <Complex128>[ -2.0, 5.0 ], <Complex128>[ -4.0, 11.0 ], <Complex128>[ -6.0, 17.0 ] ]
58+
59+
var bool = ( y === z );
60+
// returns true
61+
```
62+
63+
The function has the following parameters:
64+
65+
- **arrays**: array-like object containing an input ndarray, an output ndarray, and a zero-dimensional ndarray containing a scalar constant.
66+
67+
</section>
68+
69+
<!-- /.usage -->
70+
71+
<section class="notes">
72+
73+
</section>
74+
75+
<!-- /.notes -->
76+
77+
<section class="examples">
78+
79+
## Examples
80+
81+
<!-- eslint no-undef: "error" -->
82+
83+
```javascript
84+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
85+
var Complex128Array = require( '@stdlib/array/complex128' );
86+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
87+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
88+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
89+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
90+
var zaxpy = require( '@stdlib/blas/base/ndarray/zaxpy' );
91+
92+
var opts = {
93+
'dtype': 'float64'
94+
};
95+
96+
var xbuf = new Complex128Array( discreteUniform( 10, 0, 100, opts ) );
97+
var x = new ndarray( 'complex128', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
98+
console.log( ndarray2array( x ) );
99+
100+
var ybuf = new Complex128Array( discreteUniform( xbuf.length*2, 0, 10, opts ) );
101+
var y = new ndarray( 'complex128', ybuf, [ ybuf.length ], [ 1 ], 0, 'row-major' );
102+
console.log( ndarray2array( y ) );
103+
104+
var alpha = scalar2ndarray( new Complex128( 1.0, 2.0 ), {
105+
'dtype': 'complex128'
106+
});
107+
var out = zaxpy( [ x, y, alpha ] );
108+
console.log( ndarray2array( out ) );
109+
```
110+
111+
</section>
112+
113+
<!-- /.examples -->
114+
115+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
116+
117+
<section class="related">
118+
119+
</section>
120+
121+
<!-- /.related -->
122+
123+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
124+
125+
<section class="links">
126+
127+
</section>
128+
129+
<!-- /.links -->
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var real = require( '@stdlib/complex/float64/real' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
29+
var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' );
30+
var Complex128Array = require( '@stdlib/array/complex128' );
31+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
32+
var format = require( '@stdlib/string/format' );
33+
var pkg = require( './../package.json' ).name;
34+
var zaxpy = require( './../lib' );
35+
36+
37+
// VARIABLES //
38+
39+
var options = {
40+
'dtype': 'complex128'
41+
};
42+
43+
44+
// FUNCTIONS //
45+
46+
/**
47+
* Creates a benchmark function.
48+
*
49+
* @private
50+
* @param {PositiveInteger} len - array length
51+
* @returns {Function} benchmark function
52+
*/
53+
function createBenchmark( len ) {
54+
var alpha;
55+
var xbuf;
56+
var ybuf;
57+
var x;
58+
var y;
59+
60+
xbuf = uniform( len*2, -100.0, 100.0, {
61+
'dtype': 'float64'
62+
});
63+
x = new ndarray( options.dtype, new Complex128Array( xbuf ), [ len ], [ 1 ], 0, 'row-major' );
64+
65+
ybuf = uniform( len*2, -100.0, 100.0, {
66+
'dtype': 'float64'
67+
});
68+
y = new ndarray( options.dtype, new Complex128Array( ybuf ), [ len ], [ 1 ], 0, 'row-major' );
69+
70+
alpha = scalar2ndarray( new Complex128( 1.0, 2.0 ), options.dtype, 'row-major' );
71+
72+
return benchmark;
73+
74+
/**
75+
* Benchmark function.
76+
*
77+
* @private
78+
* @param {Benchmark} b - benchmark instance
79+
*/
80+
function benchmark( b ) {
81+
var z;
82+
var i;
83+
84+
b.tic();
85+
for ( i = 0; i < b.iterations; i++ ) {
86+
z = zaxpy( [ x, y, alpha ] );
87+
if ( typeof z !== 'object' ) {
88+
b.fail( 'should not return NaN' );
89+
}
90+
}
91+
b.toc();
92+
if ( isnan( real( z.get( i%len ) ) ) ) {
93+
b.fail( 'should not return NaN' );
94+
}
95+
b.pass( 'benchmark finished' );
96+
b.end();
97+
}
98+
}
99+
100+
101+
// MAIN //
102+
103+
/**
104+
* Main execution sequence.
105+
*
106+
* @private
107+
*/
108+
function main() {
109+
var len;
110+
var min;
111+
var max;
112+
var f;
113+
var i;
114+
115+
min = 1; // 10^min
116+
max = 6; // 10^max
117+
118+
for ( i = min; i <= max; i++ ) {
119+
len = pow( 10, i );
120+
f = createBenchmark( len );
121+
bench( format( '%s:len=%d', pkg, len ), f );
122+
}
123+
}
124+
125+
main();
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
{{alias}}( arrays )
3+
Multiplies a one-dimensional double-precision complex floating-point
4+
ndarray `x` by a constant `alpha` and adds the result to a one-dimensional
5+
double-precision complex floating-point ndarray `y`.
6+
7+
If provided an empty input ndarray, the function returns the output ndarray
8+
unchanged.
9+
10+
Parameters
11+
----------
12+
arrays: ArrayLikeObject<ndarray>
13+
Array-like object containing an input ndarray, an output ndarray, and
14+
a zero-dimensional ndarray containing a scalar constant `alpha`.
15+
16+
Returns
17+
-------
18+
out: ndarray
19+
Output ndarray.
20+
21+
Examples
22+
--------
23+
> var xbuf = new {{alias:@stdlib/array/complex128}}( [ 4.0, 2.0, -3.0, 5.0 ] );
24+
> var ybuf = new {{alias:@stdlib/array/complex128}}( [ 2.0, 6.0, -1.0, -4.0 ] );
25+
> var dt = 'complex128';
26+
> var sh = [ xbuf.length ];
27+
> var st = [ 1 ];
28+
> var oo = 0;
29+
> var ord = 'row-major';
30+
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, st, oo, ord );
31+
> var y = new {{alias:@stdlib/ndarray/ctor}}( dt, ybuf, sh, st, oo, ord );
32+
> var ca = new {{alias:@stdlib/complex/float64/ctor}}( 1.0, 2.0 );
33+
> var alpha = {{alias:@stdlib/ndarray/base/from-scalar}}( ca, dt, ord );
34+
35+
> {{alias}}( [ x, y, alpha ] );
36+
> y
37+
<ndarray>[ <Complex128>[ 2.0, 16.0 ], <Complex128>[ -14.0, -5.0 ] ]
38+
39+
See Also
40+
--------
41+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
* Multiplies a one-dimensional double-precision complex floating-point ndarray `x` by a constant `alpha` and adds the result to a one-dimensional double-precision complex floating-point ndarray `y`.
27+
*
28+
* @param arrays - array-like object containing an input ndarray, an output ndarray, and a zero-dimensional ndarray containing a scalar constant
29+
* @returns output ndarray
30+
*
31+
* @example
32+
* var Complex128Array = require( '@stdlib/array/complex128' );
33+
* var Complex128 = require( '@stdlib/complex/float64/ctor' );
34+
* var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' );
35+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
36+
*
37+
* var xbuf = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 ] );
38+
* var x = new ndarray( 'complex128', xbuf, [ 5 ], [ 1 ], 0, 'row-major' );
39+
*
40+
* var ybuf = new Complex128Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
41+
* var y = new ndarray( 'complex128', ybuf, [ 5 ], [ 1 ], 0, 'row-major' );
42+
*
43+
* var alpha = scalar2ndarray( new Complex128( 1.0, 2.0 ), 'complex128', 'row-major' );
44+
*
45+
* var z = zaxpy( [ x, y, alpha ] );
46+
* // returns <ndarray>[ <Complex128>[ -2.0, 5.0 ], <Complex128>[ -4.0, 11.0 ], <Complex128>[ -6.0, 17.0 ], <Complex128>[ -8.0, 23.0 ], <Complex128>[ -10.0, 29.0 ] ]
47+
*
48+
* var bool = ( z === y );
49+
* // returns true
50+
*/
51+
declare function zaxpy( arrays: [ complex128ndarray, complex128ndarray, complex128ndarray ] ): complex128ndarray;
52+
53+
54+
// EXPORTS //
55+
56+
export = zaxpy;

0 commit comments

Comments
 (0)