Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add javascript implementation of blas/base/cdotc #4752

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
233 changes: 233 additions & 0 deletions lib/node_modules/@stdlib/blas/base/cdotc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
<!--

@license Apache-2.0

Copyright (c) 2025 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# cdotc

> Calculate the dot product of two single-precision complex vectors.

<section class="intro">

The [dot product][dot-product] (or scalar product) is defined as

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var cdotc = require( '@stdlib/blas/base/cdotc' );
```

#### cdotc( N, x, strideX, y, strideY )

Calculates the dot product of complex vectors `x` and `y`.

```javascript
var Complex64Array = require( '@stdlib/array/complex64' );
var Complex64 = require( '@stdlib/complex/float32/ctor' );
var realf = require( '@stdlib/complex/float32/real' );
var imagf = require( '@stdlib/complex/float32/imag' );

var x = new Complex64Array( [ 7.0, -8.0, -1.0, -9.0 ] );
var y = new Complex64Array( [ 6.0, -6.0, -9.0, 5.0 ] );

var out = cdotc( x.length, x, 1, y, 1 );
// returns <Complex64>

var re = realf( out );
// returns 54

var im = imagf( out );
// returns -80
```

The function has the following parameters:

- **N**: number of indexed elements.
- **x**: input [`Complex64Array`][@stdlib/array/complex64].
- **strideX**: index increment for `x`.
- **y**: input [`Complex64Array`][@stdlib/array/complex64].
- **strideY**: index increment for `y`.

The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to calculate the dot product of every other value in `x` and the first `N` elements of `y` in reverse order,

```javascript
var Complex64Array = require( '@stdlib/array/complex64' );
var Complex64 = require( '@stdlib/complex/float32/ctor' );
var realf = require( '@stdlib/complex/float32/real' );
var imagf = require( '@stdlib/complex/float32/imag' );

var x = new Complex64Array( [ -1.0, -9.0, 2.0, -8.0 ] );
var y = new Complex64Array( [ -5.0, 1.0, -6.0, 7.0 ] );

var out = cdotc( x.length, x, 1, y, -1 );
// returns <Complex64>

var re = realf( out );
// returns -75

var im = imagf( out );
// returns -99
```

#### cdotc.ndarray( N, x, strideX, offsetX, y, strideY, offsetY )

Calculates the dot product of `x` and `y` using alternative indexing semantics.

```javascript
var Complex64Array = require( '@stdlib/array/complex64' );
var Complex64 = require( '@stdlib/complex/float32/ctor' );
var realf = require( '@stdlib/complex/float32/real' );
var imagf = require( '@stdlib/complex/float32/imag' );

var x = new Complex64Array( [ 7.0, -8.0, -1.0, -9.0 ] );
var y = new Complex64Array( [ 6.0, -6.0, -9.0, 5.0 ] );

var out = cdotc.ndarray( x.length, x, 1, 0, y, 1, 0 );
// returns <Complex64>

var re = realf( out );
// returns 54

var im = imagf( out );
// returns -80
```

The function has the following additional parameters:

- **offsetX**: starting index for `x`.
- **offsetY**: starting index for `y`.

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 `x` starting from the second value with the last 2 elements in `y` in reverse order

```javascript
var Complex64Array = require( '@stdlib/array/complex64' );
var Complex64 = require( '@stdlib/complex/float32/ctor' );
var realf = require( '@stdlib/complex/float32/real' );
var imagf = require( '@stdlib/complex/float32/imag' );

var x = new Complex64Array( [ 7.0, -8.0, -1.0, -9.0 ] );
var y = new Complex64Array( [ 6.0, -6.0, -9.0, 5.0 ] );

var out = cdotc.ndarray( x.length, x, 1, 0, y, -1, y.length-1 );
// returns <Complex64>

var re = realf( out );
// returns -55

var im = imagf( out );
// returns 23
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- If `N <= 0`, both functions return complex64 with real and imagnary as `0.0`.
- `cdotc()` corresponds to the [BLAS][blas] level 1 function [`cdotc`][cdotc].

</section>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
var filledarrayBy = require( '@stdlib/array/filled-by' );
var Complex64 = require( '@stdlib/complex/float32/ctor' );
var cdotc = require( '@stdlib/blas/base/cdotc' );

function rand() {
return new Complex64( discreteUniform( 0, 10 ), discreteUniform( 1, 5 ) );
}

var cx = filledarrayBy( 10, 'complex64', rand );
console.log( cx.toString() );

var cy = filledarrayBy( 10, 'complex64', rand );
console.log( cy.toString() );

// Perform dot product of cx and cy
var out = cdotc.ndarray( cx.length, cx, 1, 0, cy, -1, cy.length-1 );
console.log( out );
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

* * *

## See Also

- <span class="package-name">[`@stdlib/blas/base/zdotc`][@stdlib/blas/base/zdotc]</span><span class="delimiter">: </span><span class="description">calculate the dot product of two double-precision complex vectors.</span>
- <span class="package-name">[`@stdlib/blas/base/cdotu`][@stdlib/blas/base/cdotu]</span><span class="delimiter">: </span><span class="description">calculate the dot product without conjugate of x of two single-precision complex vectors.</span>
- <span class="package-name">[`@stdlib/blas/base/zdotu`][@stdlib/blas/base/zdotu]</span><span class="delimiter">: </span><span class="description">calculate the dot product without conjugate of x of two double-precision complex vectors.</span>
- <span class="package-name">[`@stdlib/blas/cdotc`][@stdlib/blas/cdotc]</span><span class="delimiter">: </span><span class="description">calculate the dot product of two single-precision comple vectors.</span>

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[dot-product]: https://en.wikipedia.org/wiki/Dot_product

[blas]: http://www.netlib.org/blas

[cdotc]: http://www.netlib.org/lapack/explore-html/df/d28/group__single__blas__level1.html

[@stdlib/array/complex64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex64

[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray

<!-- <related-links> -->

[@stdlib/blas/base/zdotc]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/zdotc

[@stdlib/blas/base/cdotu]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/cdotu

[@stdlib/blas/base/zdotu]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/zdotu

[@stdlib/blas/cdotc]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/cdotc

<!-- </related-links> -->

</section>

<!-- /.links -->
110 changes: 110 additions & 0 deletions lib/node_modules/@stdlib/blas/base/cdotc/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var uniform = require( '@stdlib/random/array/uniform' );
var pow = require( '@stdlib/math/base/special/pow' );
var Complex64Array = require( '@stdlib/array/complex64' );
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var realf = require( '@stdlib/complex/float32/real' );
var imagf = require( '@stdlib/complex/float32/imag' );
var pkg = require( './../package.json' ).name;
var cdotc = require( './../lib/cdotc.js' );


// VARIABLES //

var options = {
'dtype': 'float32'
};


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var cx;
var cy;

cx = new Complex64Array( uniform( len*2, -100.0, 100.0, options ) );
cy = new Complex64Array( uniform( len*2, -100.0, 100.0, options ) );

return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var out;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = cdotc( cx.length, cx, 1, cy, 1 );
if ( isnanf( realf( out ) ) || isnanf( imagf( out ) )) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnanf( realf( out ) ) || isnanf( imagf( out ) )) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var len;
var min;
var max;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );
f = createBenchmark( len );
bench( pkg+':len='+len, f );
}
}

main();
Loading
Loading