Skip to content

Commit 6eba8e3

Browse files
committed
feat: add blas/base/ndarray/caxpy
1 parent 93c9868 commit 6eba8e3

10 files changed

Lines changed: 1012 additions & 0 deletions

File tree

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
# caxpy
22+
23+
> Multiply a one-dimensional single-precision complex floating-point ndarray `x` by a constant `alpha` and add the result to a one-dimensional single-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 caxpy = require( '@stdlib/blas/base/ndarray/caxpy' );
37+
```
38+
39+
#### caxpy( arrays )
40+
41+
Multiplies a one-dimensional single-precision complex floating-point ndarray `x` by a constant `alpha` and adds the result to a one-dimensional single-precision complex floating-point ndarray `y`.
42+
43+
```javascript
44+
var Complex64Array = require( '@stdlib/array/complex64' );
45+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
46+
var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' );
47+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
48+
49+
var xbuf = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 ] );
50+
var x = new ndarray( 'complex64', xbuf, [ 5 ], [ 1 ], 0, 'row-major' );
51+
52+
var ybuf = new Complex64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
53+
var y = new ndarray( 'complex64', ybuf, [ 5 ], [ 1 ], 0, 'row-major' );
54+
55+
var alpha = scalar2ndarray( new Complex64( 5.0, 5.0 ), 'complex64', 'row-major' );
56+
var z = caxpy( [ x, y, alpha ] );
57+
// returns <ndarray>[ 1.0, 16.0, -1.0, 36.0, -3.0, 56.0, -5.0, 76.0, -7.0, 96.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 scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
86+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
87+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
88+
var caxpy = require( '@stdlib/blas/base/ndarray/caxpy' );
89+
90+
var opts = {
91+
'dtype': 'complex64'
92+
};
93+
94+
var xbuf = discreteUniform( 10, 0, 100, opts );
95+
var x = new ndarray( opts.dtype, xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
96+
console.log( ndarray2array( x ) );
97+
98+
var ybuf = discreteUniform( xbuf.length, 0, 10, opts );
99+
var y = new ndarray( opts.dtype, ybuf, [ ybuf.length ], [ 1 ], 0, 'row-major' );
100+
console.log( ndarray2array( y ) );
101+
102+
var alpha = scalar2ndarray( 5.0, opts );
103+
var out = caxpy( [ x, y, alpha ] );
104+
console.log( ndarray2array( out ) );
105+
```
106+
107+
</section>
108+
109+
<!-- /.examples -->
110+
111+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
112+
113+
<section class="related">
114+
115+
</section>
116+
117+
<!-- /.related -->
118+
119+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
120+
121+
<section class="links">
122+
123+
</section>
124+
125+
<!-- /.links -->
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
28+
var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' );
29+
var format = require( '@stdlib/string/format' );
30+
var Complex64Array = require( '@stdlib/array/complex64' );
31+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
32+
var pkg = require( './../package.json' ).name;
33+
var caxpy = require( './../lib' );
34+
35+
36+
// VARIABLES //
37+
38+
var options = {
39+
'dtype': 'complex64'
40+
};
41+
42+
43+
// FUNCTIONS //
44+
45+
/**
46+
* Creates a benchmark function.
47+
*
48+
* @private
49+
* @param {PositiveInteger} len - array length
50+
* @returns {Function} benchmark function
51+
*/
52+
function createBenchmark( len ) {
53+
var alpha;
54+
var xbuf;
55+
var ybuf;
56+
var x;
57+
var y;
58+
59+
var uniformOptions = {
60+
'dtype': 'float32'
61+
};
62+
63+
xbuf = new Complex64Array( uniform( len*2, -100.0, 100.0, uniformOptions ) );
64+
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
65+
66+
ybuf = new Complex64Array( uniform( len*2, -100.0, 100.0, uniformOptions ) );
67+
y = new ndarray( options.dtype, ybuf, [ len ], [ 1 ], 0, 'row-major' );
68+
69+
alpha = scalar2ndarray( new Complex64(5.0, 5.0), options.dtype, 'row-major' );
70+
71+
return benchmark;
72+
73+
/**
74+
* Benchmark function.
75+
*
76+
* @private
77+
* @param {Benchmark} b - benchmark instance
78+
*/
79+
function benchmark( b ) {
80+
var z;
81+
var i;
82+
83+
b.tic();
84+
for ( i = 0; i < b.iterations; i++ ) {
85+
z = caxpy( [ x, y, alpha ] );
86+
if ( typeof z !== 'object' ) {
87+
b.fail( 'should not return NaN' );
88+
}
89+
}
90+
b.toc();
91+
if ( isnanf( z.get( i%len ).re ) ) {
92+
b.fail( 'should not return NaN' );
93+
}
94+
b.pass( 'benchmark finished' );
95+
b.end();
96+
}
97+
}
98+
99+
100+
// MAIN //
101+
102+
/**
103+
* Main execution sequence.
104+
*
105+
* @private
106+
*/
107+
function main() {
108+
var len;
109+
var min;
110+
var max;
111+
var f;
112+
var i;
113+
114+
min = 1; // 10^min
115+
max = 6; // 10^max
116+
117+
for ( i = min; i <= max; i++ ) {
118+
len = pow( 10, i );
119+
f = createBenchmark( len );
120+
bench( format( '%s:len=%d', pkg, len ), f );
121+
}
122+
}
123+
124+
main();
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
{{alias}}( arrays )
3+
Multiplies a one-dimensional single-precision complex floating-point
4+
ndarray `x` by a constant `alpha` and adds the result to a one-dimensional
5+
single-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/complex64}}( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] );
24+
> var ybuf = new {{alias:@stdlib/array/complex64}}( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] );
25+
> var dt = 'complex64';
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 alpha = {{alias:@stdlib/ndarray/base/from-scalar}}( 5.0, dt, ord );
33+
34+
> {{alias}}( [ x, y, alpha ] );
35+
> y
36+
<ndarray>[ 22.0, 36.0, -41.0, 6.0, -12.0, -13.0, 12.0, 44.0, 0.0, 54.0 ]
37+
38+
See Also
39+
--------
40+
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 { complex64ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Multiplies a one-dimensional single-precision complex floating-point ndarray `x` by a constant `alpha` and adds the result to a one-dimensional single-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 Complex64Array = require( '@stdlib/array/complex64' );
33+
* var Complex64 = require( '@stdlib/complex/float32/ctor' );
34+
* var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' );
35+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
36+
*
37+
* var xbuf = new Complex64Array( [ 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( 'complex64', xbuf, [ 5 ], [ 1 ], 0, 'row-major' );
39+
*
40+
* var ybuf = new Complex64Array( [ 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( 'complex64', ybuf, [ 5 ], [ 1 ], 0, 'row-major' );
42+
*
43+
* var alpha = scalar2ndarray( new Complex64( 5.0, 5.0 ), 'complex64', 'row-major' );
44+
*
45+
* var z = caxpy( [ x, y, alpha ] );
46+
* // returns <ndarray>[ 1.0, 16.0, -1.0, 36.0, -3.0, 56.0, -5.0, 76.0, -7.0, 96.0 ]
47+
*
48+
* var bool = ( z === y );
49+
* // returns true
50+
*/
51+
declare function caxpy( arrays: [ complex64ndarray, complex64ndarray, complex64ndarray ] ): complex64ndarray;
52+
53+
54+
// EXPORTS //
55+
56+
export = caxpy;

0 commit comments

Comments
 (0)