Skip to content

Commit 7d65943

Browse files
aman-095kgryte
andauthored
feat: add blas/base/caxpy
PR-URL: #2121 Ref: #2039 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent aefd3bd commit 7d65943

File tree

15 files changed

+2739
-0
lines changed

15 files changed

+2739
-0
lines changed
Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
> Scale a single-precision complex floating-point vector by a single-precision complex floating-point constant and add the result to a single-precision complex floating-point vector.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var caxpy = require( '@stdlib/blas/base/caxpy' );
31+
```
32+
33+
#### caxpy( N, ca, cx, strideX, cy, strideY )
34+
35+
Scales values from `cx` by `ca` and adds the result to `cy`.
36+
37+
```javascript
38+
var Complex64Array = require( '@stdlib/array/complex64' );
39+
var Complex64 = require('@stdlib/complex/float32/ctor');
40+
var realf = require( '@stdlib/complex/realf' );
41+
var imagf = require( '@stdlib/complex/imagf' );
42+
43+
var cx = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
44+
var cy = new Complex64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
45+
var ca = new Complex64( 2.0, 2.0 );
46+
47+
caxpy( 3, ca, cx, 1, cy, 1 );
48+
49+
var z = cy.get( 0 );
50+
// returns <Complex64>
51+
52+
var re = realf( z );
53+
// returns -1.0
54+
55+
var im = imagf( z );
56+
// returns 7.0
57+
```
58+
59+
The function has the following parameters:
60+
61+
- **N**: number of indexed elements.
62+
- **ca**: scalar [`Complex64`][@stdlib/complex/float32/ctor] constant.
63+
- **cx**: first input [`Complex64Array`][@stdlib/array/complex64].
64+
- **strideX**: index increment for `cx`.
65+
- **cy**: second input [`Complex64Array`][@stdlib/array/complex64].
66+
- **strideY**: index increment for `cy`.
67+
68+
The `N` and stride parameters determine how values from `cx` are scaled by `ca` and added to `cy`. For example, to scale every other value in `cx` by `ca` and add the result to every other value of `cy`,
69+
70+
```javascript
71+
var Complex64Array = require( '@stdlib/array/complex64' );
72+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
73+
var realf = require( '@stdlib/complex/realf' );
74+
var imagf = require( '@stdlib/complex/imagf' );
75+
76+
var cx = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
77+
var cy = new Complex64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
78+
var ca = new Complex64( 2.0, 2.0 );
79+
80+
caxpy( 2, ca, cx, 2, cy, 2 );
81+
82+
var z = cy.get( 0 );
83+
// returns <Complex64>
84+
85+
var re = realf( z );
86+
// returns -1.0
87+
88+
var im = imagf( z );
89+
// returns 7.0
90+
```
91+
92+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
93+
94+
<!-- eslint-disable stdlib/capitalized-comments -->
95+
96+
```javascript
97+
var Complex64Array = require( '@stdlib/array/complex64' );
98+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
99+
var realf = require( '@stdlib/complex/realf' );
100+
var imagf = require( '@stdlib/complex/imagf' );
101+
102+
// Initial arrays...
103+
var cx0 = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
104+
var cy0 = new Complex64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
105+
106+
// Define a scalar constant:
107+
var ca = new Complex64( 2.0, 2.0 );
108+
109+
// Create offset views...
110+
var cx1 = new Complex64Array( cx0.buffer, cx0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
111+
var cy1 = new Complex64Array( cy0.buffer, cy0.BYTES_PER_ELEMENT*2 ); // start at 3rd element
112+
113+
// Scales values of `cx0` by `ca` starting from second index and add the result to `cy0` starting from third index...
114+
caxpy( 2, ca, cx1, 1, cy1, 1 );
115+
116+
var z = cy0.get( 2 );
117+
// returns <Complex64>
118+
119+
var re = realf( z );
120+
// returns -1.0
121+
122+
var im = imagf( z );
123+
// returns 15.0
124+
```
125+
126+
#### caxpy.ndarray( N, ca, cx, strideX, offsetX, cy, strideY, offsetY )
127+
128+
Scales values from `cx` by `ca` and adds the result to `cy` using alternative indexing semantics.
129+
130+
```javascript
131+
var Complex64Array = require( '@stdlib/array/complex64' );
132+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
133+
var realf = require( '@stdlib/complex/realf' );
134+
var imagf = require( '@stdlib/complex/imagf' );
135+
136+
var cx = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
137+
var cy = new Complex64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
138+
var ca = new Complex64( 2.0, 2.0 );
139+
140+
caxpy.ndarray( 3, ca, cx, 1, 0, cy, 1, 0 );
141+
142+
var z = cy.get( 0 );
143+
// returns <Complex64>
144+
145+
var re = realf( z );
146+
// returns -1.0
147+
148+
var im = imagf( z );
149+
// returns 7.0
150+
```
151+
152+
The function has the following additional parameters:
153+
154+
- **offsetX**: starting index for `cx`.
155+
- **offsetY**: starting index for `cy`.
156+
157+
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 scale values in the first input strided array starting from the second element and add the result to the second input array starting from the second element,
158+
159+
```javascript
160+
var Complex64Array = require( '@stdlib/array/complex64' );
161+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
162+
var realf = require( '@stdlib/complex/realf' );
163+
var imagf = require( '@stdlib/complex/imagf' );
164+
165+
var cx = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
166+
var cy = new Complex64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
167+
var ca = new Complex64( 2.0, 2.0 );
168+
169+
caxpy.ndarray( 3, ca, cx, 1, 1, cy, 1, 1 );
170+
171+
var z = cy.get( 3 );
172+
// returns <Complex64>
173+
174+
var re = realf( z );
175+
// returns -1.0
176+
177+
var im = imagf( z );
178+
// returns 31.0
179+
```
180+
181+
</section>
182+
183+
<!-- /.usage -->
184+
185+
<section class="notes">
186+
187+
## Notes
188+
189+
- If `N <= 0`, both functions return `cy` unchanged.
190+
- `caxpy()` corresponds to the [BLAS][blas] level 1 function [`caxpy`][caxpy].
191+
192+
</section>
193+
194+
<!-- /.notes -->
195+
196+
<section class="examples">
197+
198+
## Examples
199+
200+
<!-- eslint no-undef: "error" -->
201+
202+
```javascript
203+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
204+
var filledarrayBy = require( '@stdlib/array/filled-by' );
205+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
206+
var ccopy = require( '@stdlib/blas/base/ccopy' );
207+
var zeros = require( '@stdlib/array/zeros' );
208+
var logEach = require( '@stdlib/console/log-each' );
209+
var caxpy = require( '@stdlib/blas/base/caxpy' );
210+
211+
function rand() {
212+
return new Complex64( discreteUniform( 0, 10 ), discreteUniform( -5, 5 ) );
213+
}
214+
215+
var cx = filledarrayBy( 10, 'complex64', rand );
216+
var cy = filledarrayBy( 10, 'complex64', rand );
217+
var cyc = ccopy( cy.length, cy, 1, zeros( cy.length, 'complex64' ), 1 );
218+
219+
var ca = new Complex64( 2.0, 2.0 );
220+
221+
// Scale values from `cx` by `ca` and add the result to `cy`:
222+
caxpy( cx.length, ca, cx, 1, cy, 1 );
223+
224+
// Print the results:
225+
logEach( '(%s)*(%s) + (%s) = %s', ca, cx, cyc, cy );
226+
```
227+
228+
</section>
229+
230+
<!-- /.examples -->
231+
232+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
233+
234+
<section class="related">
235+
236+
</section>
237+
238+
<!-- /.related -->
239+
240+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
241+
242+
<section class="links">
243+
244+
[blas]: http://www.netlib.org/blas
245+
246+
[caxpy]: https://www.netlib.org/lapack/explore-html/d5/d4b/group__axpy_ga0b7bac1f4d42514074a48f14f5f9caa0.html#ga0b7bac1f4d42514074a48f14f5f9caa0
247+
248+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
249+
250+
[@stdlib/array/complex64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex64
251+
252+
[@stdlib/complex/float32/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/complex/float32/ctor
253+
254+
</section>
255+
256+
<!-- /.links -->
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 Complex64Array = require( '@stdlib/array/complex64' );
28+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
29+
var reinterpret = require( '@stdlib/strided/base/reinterpret-complex64' );
30+
var pkg = require( './../package.json' ).name;
31+
var caxpy = require( './../lib/caxpy.js' );
32+
33+
34+
// VARIABLES //
35+
36+
var options = {
37+
'dtype': 'float32'
38+
};
39+
40+
41+
// FUNCTIONS //
42+
43+
/**
44+
* Creates a benchmark function.
45+
*
46+
* @private
47+
* @param {PositiveInteger} len - array length
48+
* @returns {Function} benchmark function
49+
*/
50+
function createBenchmark( len ) {
51+
var viewY;
52+
var ca;
53+
var cx;
54+
var cy;
55+
56+
cx = new Complex64Array( uniform( len*2, -100.0, 100.0, options ) );
57+
cy = new Complex64Array( uniform( len*2, -100.0, 100.0, options ) );
58+
59+
viewY = reinterpret( cy, 0 );
60+
61+
ca = new Complex64( 1.0, 0.0 );
62+
63+
return benchmark;
64+
65+
/**
66+
* Benchmark function.
67+
*
68+
* @private
69+
* @param {Benchmark} b - benchmark instance
70+
*/
71+
function benchmark( b ) {
72+
var i;
73+
74+
b.tic();
75+
for ( i = 0; i < b.iterations; i++ ) {
76+
caxpy( cx.length, ca, cx, 1, cy, 1 );
77+
if ( isnanf( viewY[ i%(len*2) ] ) ) {
78+
b.fail( 'should not return NaN' );
79+
}
80+
}
81+
b.toc();
82+
if ( isnanf( viewY[ i%(len*2) ] ) ) {
83+
b.fail( 'should not return NaN' );
84+
}
85+
b.pass( 'benchmark finished' );
86+
b.end();
87+
}
88+
}
89+
90+
91+
// MAIN //
92+
93+
/**
94+
* Main execution sequence.
95+
*
96+
* @private
97+
*/
98+
function main() {
99+
var len;
100+
var min;
101+
var max;
102+
var f;
103+
var i;
104+
105+
min = 1; // 10^min
106+
max = 6; // 10^max
107+
108+
for ( i = min; i <= max; i++ ) {
109+
len = pow( 10, i );
110+
f = createBenchmark( len );
111+
bench( pkg+':len='+len, f );
112+
}
113+
}
114+
115+
main();

0 commit comments

Comments
 (0)