Skip to content

feat: add lapack/base/dlacn2 #7438

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
362 changes: 362 additions & 0 deletions lib/node_modules/@stdlib/lapack/base/dlacn2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,362 @@
<!--

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

-->

# dlacn2

> LAPACK routine to estimate the one-norm of a square matrix `A`, using reverse communication for evaluating matrix-vector products.

<section class="usage">

## Usage

```javascript
var dlacn2 = require( '@stdlib/lapack/base/dlacn2' );
```

#### dlacn2( N, V, X, ISGN, EST, KASE, ISAVE )

Estimates the one-norm of a square matrix `A`, using alternative indexing semantics and reverse communication for evaluating matrix-vector products.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Estimates the one-norm of a square matrix `A`, using alternative indexing semantics and reverse communication for evaluating matrix-vector products.
Estimates the one-norm of a square matrix `A`, using reverse communication for evaluating matrix-vector products.


```javascript
var Int32Array = require( '@stdlib/array/int32' );
var Float64Array = require( '@stdlib/array/float64' );

var V = new Float64Array( [ 5.0, 3.0, 1.0, 5.0 ] );
var X = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
var ISGN = new Int32Array( [ 1, 1, 1, 1 ] );
var EST = new Float64Array( [ 10 ] );
var KASE = new Int32Array( [ 1 ] );
var ISAVE = new Int32Array( [ 2, 3, 1 ] );

dlacn2( 4, V, X, ISGN, EST, KASE, ISAVE );
// X => <Float64Array>[ 0.0, 0.0, 0.0, 1.0 ]
// V => <Float64Array>[ 5.0, 3.0, 1.0, 5.0 ]
// EST => <Float64Array>[ 10.0 ]
// KASE => <Int32Array>[ 1 ]
```

The function has the following parameters:

- **N**: number of rows/columns in `A`.
- **V**: workspace [`Float64Array`][mdn-float64array] having `N` indexed elements, used internally to store intermediate vectors.
- **X**: input/output [`Float64Array`][mdn-float64array] having `N` indexed elements, contains the current or next matrix-vector product.
- **ISGN**: [`Int32Array`][mdn-int32array] having `N` indexed elements, stores the sign of each element in `X` during iterations.
- **EST**: single-element [`Float64Array`][mdn-float64array], on output, contains the estimated one-norm of the matrix `A`.
- **KASE**: single-element [`Int32Array`][mdn-int32array] that controls the reverse communication.
- **ISAVE**: [`Int32Array`][mdn-int32array] having 3 indexed elements, used internally to maintain state across multiple calls.
Comment on lines +58 to +63
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- **V**: workspace [`Float64Array`][mdn-float64array] having `N` indexed elements, used internally to store intermediate vectors.
- **X**: input/output [`Float64Array`][mdn-float64array] having `N` indexed elements, contains the current or next matrix-vector product.
- **ISGN**: [`Int32Array`][mdn-int32array] having `N` indexed elements, stores the sign of each element in `X` during iterations.
- **EST**: single-element [`Float64Array`][mdn-float64array], on output, contains the estimated one-norm of the matrix `A`.
- **KASE**: single-element [`Int32Array`][mdn-int32array] that controls the reverse communication.
- **ISAVE**: [`Int32Array`][mdn-int32array] having 3 indexed elements, used internally to maintain state across multiple calls.
- **V**: workspace [`Float64Array`][mdn-float64array] having `N` indexed elements. This workspace is used internally to store intermediate vectors.
- **X**: input/output [`Float64Array`][mdn-float64array] having `N` indexed elements. This array contains the current or next matrix-vector product.
- **ISGN**: [`Int32Array`][mdn-int32array] having `N` indexed elements. This array stores the sign of each element in `X` during iterations.
- **EST**: single-element [`Float64Array`][mdn-float64array], on output. This array contains the estimated one-norm of the matrix `A`.
- **KASE**: single-element [`Int32Array`][mdn-int32array] that controls the reverse communication.
- **ISAVE**: [`Int32Array`][mdn-int32array] having 3 indexed elements. This array is used internally to maintain state across multiple calls.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and below.


The reverse communication takes place using `KASE`, it may have any of these values:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The reverse communication takes place using `KASE`, it may have any of these values:
The reverse communication takes place using `KASE`. The single-element in `KASE` may have the following values:


- `0`: estimation is complete.
- `1`: caller must compute `A * X` and store the result back in `X`.
- `2`: caller must compute `A^T * X` (transpose) and store the result back in `X`.

`V` is over written by `A * W` where `EST` contains `norm( A ) / norm( W )`. (W is not returned).

`ISAVE` has the following three elements:

- the first indexed element of `ISAVE` is used to determine the control flow for the algorithm.
- the second indexed element of `ISAVE` holds the index of the largest absolute value in `X`.
- the third indexed element of `ISAVE` counts the number of refinement iterations in the algorithm.

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 Int32Array = require( '@stdlib/array/int32' );
var Float64Array = require( '@stdlib/array/float64' );

// Initial arrays...
var V0 = new Float64Array( [ 0.0, 5.0, 3.0, 1.0, 5.0 ] );
var X0 = new Float64Array( [ 0.0, 1.0, 2.0, 3.0, 4.0 ] );
var ISGN0 = new Int32Array( [ 0, 1, 1, 1, 1 ] );
var EST0 = new Float64Array( [ 0.0, 10.0 ] );
var KASE0 = new Int32Array( [ 0, 1 ] );
var ISAVE0 = new Int32Array( [ 0, 2, 3, 1 ] );

// Create offset views...
var V = new Float64Array( V0.buffer, V0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
var X = new Float64Array( X0.buffer, X0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
var ISGN = new Int32Array( ISGN0.buffer, ISGN0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
var EST = new Float64Array( EST0.buffer, EST0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
var KASE = new Int32Array( KASE0.buffer, KASE0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
var ISAVE = new Int32Array( ISAVE0.buffer, ISAVE0.BYTES_PER_ELEMENT*1 ); // start at 2nd element

dlacn2( 4, V, X, ISGN, EST, KASE, ISAVE );
// X0 => <Float64Array>[ 0.0, 0.0, 0.0, 0.0, 1.0 ]
// V0 => <Float64Array>[ 0.0, 5.0, 3.0, 1.0, 5.0 ]
// EST0 => <Float64Array>[ 0.0, 10.0 ]
// KASE0 => <Int32Array>[ 0, 1 ]
```

<!-- lint disable maximum-heading-length -->

#### dlacn2.ndarray( N, V, sv, ov, X, sx, ox, ISGN, sisgn, oisgn, EST, oe, KASE, ok, ISAVE, sisave, oisave )

Estimates the one-norm of a square matrix `A`, using alternative indexing semantics and reverse communication for evaluating matrix-vector products.

```javascript
var Int32Array = require( '@stdlib/array/int32' );
var Float64Array = require( '@stdlib/array/float64' );

var V = new Float64Array( [ 5.0, 3.0, 1.0, 5.0 ] );
var X = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
var ISGN = new Int32Array( [ 1, 1, 1, 1 ] );
var EST = new Float64Array( [ 10 ] );
var KASE = new Int32Array( [ 1 ] );
var ISAVE = new Int32Array( [ 2, 3, 1 ] );

dlacn2.ndarray( 4, V, 1, 0, X, 1, 0, ISGN, 1, 0, EST, 0, KASE, 0, ISAVE, 1, 0 );
// X => <Float64Array>[ 0.0, 0.0, 0.0, 1.0 ]
// V => <Float64Array>[ 5.0, 3.0, 1.0, 5.0 ]
// EST => <Float64Array>[ 10.0 ]
// KASE => <Int32Array>[ 1 ]
```

The function has the following parameters:

- **N**: number of rows/columns in `A`.
- **V**: workspace [`Float64Array`][mdn-float64array] having `N` indexed elements, used internally to store intermediate vectors.
- **sv**: stride length for `V`.
- **ov**: starting index for `V`.
- **X**: input/output [`Float64Array`][mdn-float64array] having `N` indexed elements, contains the current or next matrix-vector product.
- **sx**: stride length for `X`.
- **ox**: starting index for `X`.
- **ISGN**: [`Int32Array`][mdn-int32array] having `N` indexed elements, stores the sign of each element in `X` during iterations.
- **sisgn**: stride length for `ISGN`.
- **oisgn**: starting index for `ISGN`.
- **EST**: single-element [`Float64Array`][mdn-float64array], on output, contains the estimated one-norm of the matrix `A`.
- **oe**: starting index for `EST`.
- **KASE**: single-element [`Int32Array`][mdn-int32array] that controls the reverse communication.
- **ok**: starting index for `KASE`.
- **ISAVE**: [`Int32Array`][mdn-int32array] having 3 indexed elements, used internally to maintain state across multiple calls.
- **sisave**: stride length for `ISAVE`.
- **oisave**: starting index for `ISAVE`.

The reverse communication takes place using `KASE`, it may have any of these values:

- `0`: estimation is complete.
- `1`: caller must compute `A * X` and store the result back in `X`.
- `2`: caller must compute `A^T * X` (transpose) and store the result back in `X`.

`V` is over written by `A * W` where `EST` contains `norm( A ) / norm( W )`. (W is not returned).

`ISAVE` has the following three elements:

- the first indexed element of `ISAVE` is used to determine the control flow for the algorithm.
- the second indexed element of `ISAVE` holds the index of the largest absolute value in `X`.
- the third indexed element of `ISAVE` counts the number of refinement iterations in the algorithm.

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,

<!-- eslint-disable max-len -->

```javascript
var Int32Array = require( '@stdlib/array/int32' );
var Float64Array = require( '@stdlib/array/float64' );

var V = new Float64Array( [ 0.0, 5.0, 3.0, 1.0, 5.0 ] );
var X = new Float64Array( [ 0.0, 1.0, 2.0, 3.0, 4.0 ] );
var ISGN = new Int32Array( [ 0, 1, 1, 1, 1 ] );
var EST = new Float64Array( [ 0.0, 10.0 ] );
var KASE = new Int32Array( [ 0, 1 ] );
var ISAVE = new Int32Array( [ 0, 2, 3, 1 ] );

dlacn2.ndarray( 4, V, 1, 1, X, 1, 1, ISGN, 1, 1, EST, 1, KASE, 1, ISAVE, 1, 1 );
// X => <Float64Array>[ 0.0, 0.0, 0.0, 0.0, 1.0 ]
// V => <Float64Array>[ 0.0, 5.0, 3.0, 1.0, 5.0 ]
// EST => <Float64Array>[ 0.0, 10.0 ]
// KASE => <Int32Array>[ 0, 1 ]
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- `dlacn2()` corresponds to the [LAPACK][LAPACK] function [`dlacn2`][lapack-dlacn2].

</section>

<!-- /.notes -->

<section class="examples">

## Examples

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

<!-- eslint-disable array-element-newline -->

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var Int32Array = require( '@stdlib/array/int32' );
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
var dgemv = require( '@stdlib/blas/base/dgemv' );
var dcopy = require( '@stdlib/blas/base/dcopy' );
var dlacn2 = require( '@stdlib/lapack/base/dlacn2' );

// Specify matrix meta data:
var shape = [ 4, 4 ];
var strides = [ 4, 1 ];
var offset = 0;
var order = 'row-major';

// Create a matrix stored in linear memory:
var A = new Float64Array([
1.0, -2.0, 0.0, 0.0,
3.0, 4.0, -5.0, 0.0,
0.0, 6.0, 7.0, -8.0,
0.0, 0.0, 9.0, 10.0
]);

console.log( ndarray2array( A, shape, strides, offset, order ) );

var KASE = new Int32Array( 1 );
var EST = new Float64Array( 1 );
var ISGN = new Int32Array( 4 );
var ISAVE = new Int32Array( 3 );
var X = new Float64Array( 4 );
var V = new Float64Array( 4 );

var work = new Float64Array( 4 );

while ( true ) {
dlacn2( 4, V, X, ISGN, EST, KASE, ISAVE );

if ( KASE[ 0 ] === 0 ) {
break;
}
else if ( KASE[ 0 ] === 1 ) {
dgemv( 'row-major', 'no-transpose', shape[ 0 ], shape[ 1 ], 1.0, A, strides[ 0 ], X, 1, 0, work, 1 );
dcopy( shape[ 0 ], work, 1, X, 1 );
} else if ( KASE[ 0 ] === 2 ) {
dgemv( 'row-major', 'transpose', shape[ 0 ], shape[ 1 ], 1.0, A, strides[ 0 ], X, 1, 0, work, 1 );
dcopy( shape[ 0 ], work, 1, X, 1 );
}
}

console.log( 'estimated norm: ', EST[ 0 ] );
console.log( 'V: ', V );
```

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

[lapack]: https://www.netlib.org/lapack/explore-html/

[lapack-dlacn2]: https://netlib.org/lapack/explore-html/d6/d2d/group__lacn2_gabc1e7463e250c9e43596bfcbfa83c1de.html#gabc1e7463e250c9e43596bfcbfa83c1de

[mdn-float64array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float64Array

[mdn-int32array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int32Array

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

</section>

<!-- /.links -->
Loading