Skip to content

Commit c5c1625

Browse files
committed
feat: add blas/base/cdotu
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent ba3d6e8 commit c5c1625

File tree

14 files changed

+1774
-0
lines changed

14 files changed

+1774
-0
lines changed
Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# cdotu
22+
23+
> Calculate the [dot product][dot-product] of two single-precision complex floating-point vectors.
24+
25+
<section class="intro">
26+
27+
The [dot product][dot-product] (or scalar product) is defined as
28+
29+
<!-- <equation class="equation" label="eq:dot_product" align="center" raw="\mathbf{zx}\cdot\mathbf{zy} = \sum_{i=0}^{N-1} zx_i zy_i = zx_0 zy_0 + zx_1 zy_1 + \ldots + zx_{N-1} zy_{N-1}" alt="Dot product definition."> -->
30+
31+
```math
32+
\mathbf{zx}\cdot\mathbf{zy} = \sum_{i=0}^{N-1} zx_i zy_i = zx_0 zy_0 + zx_1 zy_1 + \ldots + zx_{N-1} zy_{N-1}
33+
```
34+
35+
<!-- </equation> -->
36+
37+
</section>
38+
39+
<!-- /.intro -->
40+
41+
<section class="usage">
42+
43+
## Usage
44+
45+
```javascript
46+
var cdotu = require( '@stdlib/blas/base/cdotu' );
47+
```
48+
49+
#### cdotu( N, zx, strideX, zy, strideY )
50+
51+
Calculates the dot product of vectors `zx` and `zy`.
52+
53+
```javascript
54+
var Complex64Array = require( '@stdlib/array/complex64' );
55+
var real = require( '@stdlib/complex/float32/real' );
56+
var imag = require( '@stdlib/complex/float32/imag' );
57+
58+
var zx = new Complex64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 7.0 ] );
59+
var zy = new Complex64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0, 9.0 ] );
60+
61+
var z = cdotu( 3, zx, 1, zy, 1 );
62+
// returns <Complex64>
63+
64+
var re = real( z );
65+
// returns -52.0
66+
67+
var im = imag( z );
68+
// returns 82.0
69+
```
70+
71+
The function has the following parameters:
72+
73+
- **N**: number of indexed elements.
74+
- **zx**: input [`Complex64Array`][@stdlib/array/complex64].
75+
- **strideX**: index increment for `zx`.
76+
- **zy**: input [`Complex64Array`][@stdlib/array/complex64].
77+
- **strideY**: index increment for `zy`.
78+
79+
The `N` and strides parameters determine which elements in the strided arrays are accessed at runtime. For example, to calculate the dot product of every other value in `zx` and the first `N` elements of `zy` in reverse order,
80+
81+
```javascript
82+
var Complex64Array = require( '@stdlib/array/complex64' );
83+
var real = require( '@stdlib/complex/float32/real' );
84+
var imag = require( '@stdlib/complex/float32/imag' );
85+
86+
var zx = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
87+
var zy = new Complex64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
88+
89+
var z = cdotu( 2, zx, 2, zy, -1 );
90+
// returns <Complex64>
91+
92+
var re = real( z );
93+
// returns -2.0
94+
95+
var im = imag( z );
96+
// returns 14.0
97+
```
98+
99+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
100+
101+
<!-- eslint-disable stdlib/capitalized-comments -->
102+
103+
```javascript
104+
var Complex64Array = require( '@stdlib/array/complex64' );
105+
var real = require( '@stdlib/complex/float32/real' );
106+
var imag = require( '@stdlib/complex/float32/imag' );
107+
108+
// Initial arrays...
109+
var zx0 = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
110+
var zy0 = new Complex64Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
111+
112+
// Create offset views...
113+
var zx1 = new Complex64Array( zx0.buffer, zx0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
114+
var zy1 = new Complex64Array( zy0.buffer, zy0.BYTES_PER_ELEMENT*2 ); // start at 3th element
115+
116+
var z = cdotu( 1, zx1, 1, zy1, 1 );
117+
// returns <Complex64>
118+
119+
var re = real( z );
120+
// returns -15.0
121+
122+
var im = imag( z );
123+
// returns 80.0
124+
```
125+
126+
#### cdotu.ndarray( N, zx, strideX, offsetX, zy, strideY, offsetY )
127+
128+
Calculates the dot product of `zx` and `zy` using alternative indexing semantics.
129+
130+
```javascript
131+
var Complex64Array = require( '@stdlib/array/complex64' );
132+
var real = require( '@stdlib/complex/float32/real' );
133+
var imag = require( '@stdlib/complex/float32/imag' );
134+
135+
var zx = new Complex64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 7.0 ] );
136+
var zy = new Complex64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0, 9.0 ] );
137+
138+
var z = cdotu.ndarray( zx.length, zx, 1, 0, zy, 1, 0 );
139+
// returns <Complex64>
140+
141+
var re = real( z );
142+
// returns -52.0
143+
144+
var im = imag( z );
145+
// returns 82.0
146+
```
147+
148+
The function has the following additional parameters:
149+
150+
- **offsetX**: starting index for `zx`.
151+
- **offsetY**: starting index for `zy`.
152+
153+
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 calculate the dot product of every other value in `zx` starting from the second value with the last 2 elements in `zy` in reverse order
154+
155+
```javascript
156+
var Complex64Array = require( '@stdlib/array/complex64' );
157+
var real = require( '@stdlib/complex/float32/real' );
158+
var imag = require( '@stdlib/complex/float32/imag' );
159+
160+
var zx = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
161+
var zy = new Complex64Array( [ 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0 ] ); // eslint-disable-line max-len
162+
163+
var z = cdotu.ndarray( 2, zx, 2, 1, zy, -1, zy.length-1 );
164+
// returns <Complex64>
165+
166+
var re = real( z );
167+
// returns -40.0
168+
169+
var im = imag( z );
170+
// returns 310.0
171+
```
172+
173+
</section>
174+
175+
<!-- /.usage -->
176+
177+
<section class="notes">
178+
179+
## Notes
180+
181+
- If `N <= 0`, both functions return `0.0 + 0.0i`.
182+
- `cdotu()` corresponds to the [BLAS][blas] level 1 function [`cdotu`][cdotu].
183+
184+
</section>
185+
186+
<!-- /.notes -->
187+
188+
<section class="examples">
189+
190+
## Examples
191+
192+
<!-- eslint no-undef: "error" -->
193+
194+
```javascript
195+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
196+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
197+
var filledarrayBy = require( '@stdlib/array/filled-by' );
198+
var cdotu = require( '@stdlib/blas/base/cdotu' );
199+
200+
function rand() {
201+
return new Complex64( discreteUniform( 0, 10 ), discreteUniform( -5, 5 ) );
202+
}
203+
204+
var zx = filledarrayBy( 10, 'complex64', rand );
205+
console.log( zx.toString() );
206+
207+
var zy = filledarrayBy( 10, 'complex64', rand );
208+
console.log( zy.toString() );
209+
210+
var out = cdotu.ndarray( zx.length, zx, 1, 0, zy, -1, zy.length-1 );
211+
console.log( out.toString() );
212+
```
213+
214+
</section>
215+
216+
<!-- /.examples -->
217+
218+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
219+
220+
<section class="related">
221+
222+
</section>
223+
224+
<!-- /.related -->
225+
226+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
227+
228+
<section class="links">
229+
230+
[dot-product]: https://en.wikipedia.org/wiki/Dot_product
231+
232+
[blas]: http://www.netlib.org/blas
233+
234+
[cdotu]: https://www.netlib.org/lapack/explore-html/d7/d7b/cdotu_8f_source.html
235+
236+
[@stdlib/array/complex64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex64
237+
238+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
239+
240+
<!-- <related-links> -->
241+
242+
<!-- </related-links> -->
243+
244+
</section>
245+
246+
<!-- /.links -->
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 Complex64Array = require( '@stdlib/array/complex64' );
28+
var pkg = require( './../package.json' ).name;
29+
var cdotu = require( './../lib/cdotu.js' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'float32'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Creates a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} len - array length
46+
* @returns {Function} benchmark function
47+
*/
48+
function createBenchmark( len ) {
49+
var zx;
50+
var zy;
51+
52+
zx = new Complex64Array( uniform( len*2, -100.0, 100.0, options ) );
53+
zy = new Complex64Array( uniform( len*2, -100.0, 100.0, options ) );
54+
return benchmark;
55+
56+
/**
57+
* Benchmark function.
58+
*
59+
* @private
60+
* @param {Benchmark} b - benchmark instance
61+
*/
62+
function benchmark( b ) {
63+
var d;
64+
var i;
65+
66+
b.tic();
67+
for ( i = 0; i < b.iterations; i++ ) {
68+
d = cdotu( zx.length, zx, 1, zy, 1 );
69+
if ( isnan( d ) ) {
70+
b.fail( 'should not return NaN' );
71+
}
72+
}
73+
b.toc();
74+
if ( isnan( d ) ) {
75+
b.fail( 'should not return NaN' );
76+
}
77+
b.pass( 'benchmark finished' );
78+
b.end();
79+
}
80+
}
81+
82+
83+
// MAIN //
84+
85+
/**
86+
* Main execution sequence.
87+
*
88+
* @private
89+
*/
90+
function main() {
91+
var len;
92+
var min;
93+
var max;
94+
var f;
95+
var i;
96+
97+
min = 1; // 10^min
98+
max = 5; // 10^max
99+
100+
for ( i = min; i <= max; i++ ) {
101+
len = pow( 10, i );
102+
f = createBenchmark( len );
103+
bench( pkg+':len='+len, f );
104+
}
105+
}
106+
107+
main();

0 commit comments

Comments
 (0)