Skip to content

Commit 520cf07

Browse files
committed
Merge branch 'develop' of https://github.com/stdlib-js/stdlib into develop
2 parents f35cb6e + 85ffc1a commit 520cf07

File tree

34 files changed

+4329
-1
lines changed

34 files changed

+4329
-1
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+
# zaxpy
22+
23+
> Scale a double-precision complex floating-point vector by a double-precision complex floating-point constant and add the result to a double-precision complex floating-point vector.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var zaxpy = require( '@stdlib/blas/base/zaxpy' );
31+
```
32+
33+
#### zaxpy( N, za, zx, strideX, zy, strideY )
34+
35+
Scales values from `zx` by `za` and adds the result to `zy`.
36+
37+
```javascript
38+
var Complex128Array = require( '@stdlib/array/complex128' );
39+
var Complex128 = require('@stdlib/complex/float64/ctor');
40+
var real = require( '@stdlib/complex/real' );
41+
var imag = require( '@stdlib/complex/imag' );
42+
43+
var zx = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
44+
var zy = new Complex128Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
45+
var za = new Complex128( 2.0, 2.0 );
46+
47+
zaxpy( 3, za, zx, 1, zy, 1 );
48+
49+
var z = zy.get( 0 );
50+
// returns <Complex128>
51+
52+
var re = real( z );
53+
// returns -1.0
54+
55+
var im = imag( z );
56+
// returns 7.0
57+
```
58+
59+
The function has the following parameters:
60+
61+
- **N**: number of indexed elements.
62+
- **za**: scalar [`Complex128`][@stdlib/complex/float64/ctor] constant.
63+
- **zx**: first input [`Complex128Array`][@stdlib/array/complex128].
64+
- **strideX**: index increment for `zx`.
65+
- **zy**: second input [`Complex128Array`][@stdlib/array/complex128].
66+
- **strideY**: index increment for `zy`.
67+
68+
The `N` and stride parameters determine how values from `zx` are scaled by `za` and added to `zy`. For example, to scale every other value in `zx` by `za` and add the result to every other value of `zy`,
69+
70+
```javascript
71+
var Complex128Array = require( '@stdlib/array/complex128' );
72+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
73+
var real = require( '@stdlib/complex/real' );
74+
var imag = require( '@stdlib/complex/imag' );
75+
76+
var zx = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
77+
var zy = new Complex128Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
78+
var za = new Complex128( 2.0, 2.0 );
79+
80+
zaxpy( 2, za, zx, 2, zy, 2 );
81+
82+
var z = zy.get( 0 );
83+
// returns <Complex128>
84+
85+
var re = real( z );
86+
// returns -1.0
87+
88+
var im = imag( 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 Complex128Array = require( '@stdlib/array/complex128' );
98+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
99+
var real = require( '@stdlib/complex/real' );
100+
var imag = require( '@stdlib/complex/imag' );
101+
102+
// Initial arrays...
103+
var zx0 = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
104+
var zy0 = new Complex128Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
105+
106+
// Define a scalar constant:
107+
var za = new Complex128( 2.0, 2.0 );
108+
109+
// Create offset views...
110+
var zx1 = new Complex128Array( zx0.buffer, zx0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
111+
var zy1 = new Complex128Array( zy0.buffer, zy0.BYTES_PER_ELEMENT*2 ); // start at 3rd element
112+
113+
// Scales values of `zx0` by `za` starting from second index and add the result to `zy0` starting from third index...
114+
zaxpy( 2, za, zx1, 1, zy1, 1 );
115+
116+
var z = zy0.get( 2 );
117+
// returns <Complex128>
118+
119+
var re = real( z );
120+
// returns -1.0
121+
122+
var im = imag( z );
123+
// returns 15.0
124+
```
125+
126+
#### zaxpy.ndarray( N, za, zx, strideX, offsetX, zy, strideY, offsetY )
127+
128+
Scales values from `zx` by `za` and adds the result to `zy` using alternative indexing semantics.
129+
130+
```javascript
131+
var Complex128Array = require( '@stdlib/array/complex128' );
132+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
133+
var real = require( '@stdlib/complex/real' );
134+
var imag = require( '@stdlib/complex/imag' );
135+
136+
var zx = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
137+
var zy = new Complex128Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
138+
var za = new Complex128( 2.0, 2.0 );
139+
140+
zaxpy.ndarray( 3, za, zx, 1, 0, zy, 1, 0 );
141+
142+
var z = zy.get( 0 );
143+
// returns <Complex128>
144+
145+
var re = real( z );
146+
// returns -1.0
147+
148+
var im = imag( z );
149+
// returns 7.0
150+
```
151+
152+
The function has the following additional parameters:
153+
154+
- **offsetX**: starting index for `zx`.
155+
- **offsetY**: starting index for `zy`.
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 Complex128Array = require( '@stdlib/array/complex128' );
161+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
162+
var real = require( '@stdlib/complex/real' );
163+
var imag = require( '@stdlib/complex/imag' );
164+
165+
var zx = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
166+
var zy = new Complex128Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
167+
var za = new Complex128( 2.0, 2.0 );
168+
169+
zaxpy.ndarray( 3, za, zx, 1, 1, zy, 1, 1 );
170+
171+
var z = zy.get( 3 );
172+
// returns <Complex128>
173+
174+
var re = real( z );
175+
// returns -1.0
176+
177+
var im = imag( 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 `zy` unchanged.
190+
- `zaxpy()` corresponds to the [BLAS][blas] level 1 function [`zaxpy`][zaxpy].
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 Complex128 = require( '@stdlib/complex/float64/ctor' );
206+
var zcopy = require( '@stdlib/blas/base/zcopy' );
207+
var zeros = require( '@stdlib/array/zeros' );
208+
var logEach = require( '@stdlib/console/log-each' );
209+
var zaxpy = require( '@stdlib/blas/base/zaxpy' );
210+
211+
function rand() {
212+
return new Complex128( discreteUniform( 0, 10 ), discreteUniform( -5, 5 ) );
213+
}
214+
215+
var zx = filledarrayBy( 10, 'complex128', rand );
216+
var zy = filledarrayBy( 10, 'complex128', rand );
217+
var zyc = zcopy( zy.length, zy, 1, zeros( zy.length, 'complex128' ), 1 );
218+
219+
var za = new Complex128( 2.0, 2.0 );
220+
221+
// Scale values from `zx` by `za` and add the result to `zy`:
222+
zaxpy( zx.length, za, zx, 1, zy, 1 );
223+
224+
// Print the results:
225+
logEach( '(%s)*(%s) + (%s) = %s', za, zx, zyc, zy );
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+
[zaxpy]: 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/complex128]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex128
251+
252+
[@stdlib/complex/float64/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/complex/float64/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 isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var Complex128Array = require( '@stdlib/array/complex128' );
28+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
29+
var reinterpret = require( '@stdlib/strided/base/reinterpret-complex128' );
30+
var pkg = require( './../package.json' ).name;
31+
var zaxpy = require( './../lib/zaxpy.js' );
32+
33+
34+
// VARIABLES //
35+
36+
var options = {
37+
'dtype': 'float64'
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 za;
53+
var zx;
54+
var zy;
55+
56+
zx = new Complex128Array( uniform( len*2, -100.0, 100.0, options ) );
57+
zy = new Complex128Array( uniform( len*2, -100.0, 100.0, options ) );
58+
59+
viewY = reinterpret( zy, 0 );
60+
61+
za = new Complex128( 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+
zaxpy( zx.length, za, zx, 1, zy, 1 );
77+
if ( isnan( viewY[ i%(len*2) ] ) ) {
78+
b.fail( 'should not return NaN' );
79+
}
80+
}
81+
b.toc();
82+
if ( isnan( 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)