Skip to content

feat: add blas/base/zher #7119

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 7 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
269 changes: 269 additions & 0 deletions lib/node_modules/@stdlib/blas/base/zher/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.

-->

# zher

> 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 zher = require( '@stdlib/blas/base/zher' );
```

#### zher( 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 Complex128Array = require( '@stdlib/array/complex128' );

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

zher( 'row-major', 'lower', x.length, 2.0, x, 1, A, 2 );
// A => <Complex128Array>[ 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 [`Complex128Array`][@stdlib/array/complex128].
- **strideX**: index increment for `x`.
- **A**: input matrix stored in linear memory as [`Complex128Array`][@stdlib/array/complex128].
- **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 Complex128Array = require( '@stdlib/array/complex128' );

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

zher( 'row-major', 'lower', x.length, 2.0, x, -1, A, 2 );
// A => <Complex128Array>[ 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 Complex128Array = require( '@stdlib/array/complex128' );
var Complex128 = require( '@stdlib/complex/float64/ctor' );

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

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

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

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

#### zher.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 Complex128Array = require( '@stdlib/array/complex128' );

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

zher.ndarray( 'lower', x.length, 2.0, x, 1, 0, A, 2, 1, 0 );
// A => <Complex128Array>[ 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 Complex128Array = require( '@stdlib/array/complex128' );

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

zher.ndarray( 'lower', x.length, 2.0, x, -1, 1, A, 2, 1, 0 );
// A => <Complex128Array>[ 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.
- `zher()` corresponds to the [BLAS][blas] level 2 function [`zher`][zher].

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

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

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

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

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

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

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

[zher]: 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/complex128]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex128

</section>

<!-- /.links -->
111 changes: 111 additions & 0 deletions lib/node_modules/@stdlib/blas/base/zher/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 isnan = require( '@stdlib/math/base/assert/is-nan' );
var pow = require( '@stdlib/math/base/special/pow' );
var Complex128Array = require( '@stdlib/array/complex128' );
var pkg = require( './../package.json' ).name;
var zher = require( './../lib/zher.js' );


// VARIABLES //

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


// 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 Complex128Array( xbuf.buffer );
Abuf = uniform( (len*len)*2, -100.0, 100.0, options );
A = new Complex128Array( 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++ ) {
zher( 'row-major', 'lower', len, 2.0, x, 1, A, len );
if ( isnan( Abuf[ i%(len*2) ] ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( 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