Skip to content

feat: add blas/base/cher #7088

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

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
Open
269 changes: 269 additions & 0 deletions lib/node_modules/@stdlib/blas/base/cher/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
<!--

@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.

-->

# cher

> Perform the hermitian rank 1 operation `A = α*x*x**H + A`, where `α` is a real scalar, `x` is an `N` element vector and `A` is an `N` by `N` hermitian matrix.

<section class="usage">

## Usage

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

#### cher( order, uplo, N, α, x, strideX, A, LDA )

Performs the hermitian rank 1 operation `A = α*x*x**H + A`, where `α` is a real scalar, `x` is an `N` element vector and `A` is an `N` by `N` hermitian matrix.

```javascript
var Complex64Array = require( '@stdlib/array/complex64' );

var x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
var A = new Complex64Array( [ 1.0, 0.0, 0.0, 0.0, 2.0, 3.0, 4.0, 0.0 ] );

cher( 'row-major', 'lower', x.length, 2.0, x, 1, A, 2 );
// A => <Complex64Array>[ 11.0, 0.0, 0.0, 0.0, 24.0, -1.0, 54.0, 0.0 ]
```

The function has the following parameters:

- **order**: storage layout.
- **uplo**: specifies whether the upper or lower triangular part of the matrix `A` is supplied.
- **N**: specifies the order of the matrix `A`.
- **α**: scalar constant.
- **x**: input vector [`Complex64Array`][@stdlib/array/complex64].
- **strideX**: index increment for `x`.
- **A**: input matrix stored in linear memory as [`Complex64Array`][@stdlib/array/complex64].
- **LDA**: stride of the first dimension of `A` (a.k.a., leading dimension of the matrix `A`).

The stride parameters determine how elements in the input arrays are accessed at runtime. For example, to iterate over the elements of `x` in reverse order,

```javascript
var Complex64Array = require( '@stdlib/array/complex64' );

var x = new Complex64Array( [ 3.0, 4.0, 1.0, 2.0 ] );
var A = new Complex64Array( [ 1.0, 0.0, 0.0, 0.0, 2.0, 3.0, 4.0, 0.0 ] );

cher( 'row-major', 'lower', x.length, 2.0, x, -1, A, 2 );
// A => <Complex64Array>[ 11.0, 0.0, 0.0, 0.0, 24.0, -1.0, 54.0, 0.0 ]
```

Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.

<!-- eslint-disable stdlib/capitalized-comments -->

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

// Initial array:
var x0 = new Complex64Array( [ 0.0, 0.0, 1.0, 2.0, 3.0, 4.0 ] );

// Define a input matrix:
var A = new Complex64Array( [ 1.0, 0.0, 0.0, 0.0, 2.0, 3.0, 4.0, 0.0 ] );

// Create an offset view:
var x1 = new Complex64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element

cher( 'row-major', 'lower', x1.length, 2.0, x1, 1, A, 2 );
// A => <Complex64Array>[ 11.0, 0.0, 0.0, 0.0, 24.0, -1.0, 54.0, 0.0 ]
```

#### cher.ndarray( uplo, N, α, x, strideX, offsetX, A, strideA1, strideA2, offsetA )

Performs the hermitian rank 1 operation `A = α*x*x**H + A`, where `α` is a real scalar, `x` is an `N` element vector and `A` is an `N` by `N` hermitian matrix using alternative indexing semantics.

```javascript
var Complex64Array = require( '@stdlib/array/complex64' );

var x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
var A = new Complex64Array( [ 1.0, 0.0, 0.0, 0.0, 2.0, 3.0, 4.0, 0.0 ] );

cher.ndarray( 'lower', x.length, 2.0, x, 1, 0, A, 2, 1, 0 );
// A => <Complex64Array>[ 11.0, 0.0, 0.0, 0.0, 24.0, -1.0, 54.0, 0.0 ]
```

The function has the following additional parameters:

- **strideA1**: stride of the first dimension of `A`.
- **strideA2**: stride of the second dimension of `A`.
- **offsetX**: starting index for `x`.
- **offsetA**: starting index for `A`.

While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to scale every other value in the input strided array starting from the second element,

```javascript
var Complex64Array = require( '@stdlib/array/complex64' );

var x = new Complex64Array( [ 3.0, 4.0, 1.0, 2.0 ] );
var A = new Complex64Array( [ 1.0, 0.0, 0.0, 0.0, 2.0, 3.0, 4.0, 0.0 ] );

cher.ndarray( 'lower', x.length, 2.0, x, -1, 1, A, 2, 1, 0 );
// A => <Complex64Array>[ 11.0, 0.0, 0.0, 0.0, 24.0, -1.0, 54.0, 0.0 ]
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- If `N = 0` or `α = 0.0`, both functions return `x` unchanged.
- `cher()` corresponds to the [BLAS][blas] level 2 function [`cher`][cher].

</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 logEach = require( '@stdlib/console/log-each' );
var cher = require( '@stdlib/blas/base/cher' );

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

var x = filledarrayBy( 3, 'complex64', rand );
console.log( x.get( 0 ).toString() );

var A = filledarrayBy( 9, 'complex64', rand );
console.log( A.get( 0 ).toString() );

cher( 'row-major', 'upper', 3, 1.0, x, 1, A, 3 );

// Print the results:
logEach( '(%s)', A );

cher.ndarray( 'upper', 3, 1.0, x, 1, 0, A, 3, 1, 0 );

// Print the results:
logEach( '(%s)', A );
```

</section>

<!-- /.examples -->

<!-- C interface documentation. -->

* * *

<section class="c">

## C APIs

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- C usage documentation. -->

<section class="usage">

### Usage

```c
TODO
```

#### TODO

TODO.

```c
TODO
```

TODO

```c
TODO
```

</section>

<!-- /.usage -->

<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

</section>

<!-- /.notes -->

<!-- C API usage examples. -->

<section class="examples">

### Examples

```c
TODO
```

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

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

<section class="related">

</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">

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

[cher]: https://www.netlib.org/lapack/explore-html/dc/d82/group__her_gae92bde9f7f6f83c0edaa96f4f2bb2bc3.html

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

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

</section>

<!-- /.links -->
111 changes: 111 additions & 0 deletions lib/node_modules/@stdlib/blas/base/cher/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**
* @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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var pow = require( '@stdlib/math/base/special/pow' );
var Complex64Array = require( '@stdlib/array/complex64' );
var pkg = require( './../package.json' ).name;
var cher = require( './../lib/cher.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 xbuf;
var Abuf;
var x;
var A;

xbuf = uniform( len*2, -100.0, 100.0, options );
x = new Complex64Array( xbuf.buffer );
Abuf = uniform( (len*len)*2, -100.0, 100.0, options );
A = new Complex64Array( Abuf.buffer );

return benchmark;

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

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
cher( 'row-major', 'lower', len, 2.0, x, 1, A, len );
if ( isnanf( Abuf[ i%(len*2) ] ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnanf( Abuf[ i%(len*2) ] ) ) {
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 = 4; // 10^max

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

main();
Loading