Skip to content

feat: add lapack/base/dlatrs #7275

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 34 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
1f6e76b
feat: add lapack/base/dlatrs
aayush0325 Jun 8, 2025
12bc140
Merge remote-tracking branch 'upstream/develop' into lapack-dlatrs
stdlib-bot Jun 8, 2025
7870b48
feat: add main export
aayush0325 Jun 9, 2025
51984dc
test: initial tests
aayush0325 Jun 9, 2025
ee429f0
test: add lower triangular cases
aayush0325 Jun 10, 2025
1184745
test: cleaning up
aayush0325 Jun 10, 2025
025d5f8
test: add transpose cases
aayush0325 Jun 10, 2025
d90cd03
test: add more cases for unit diagonal
aayush0325 Jun 10, 2025
2fc31d0
test: mmore tests
aayush0325 Jun 11, 2025
826c766
test: add scaled tests
aayush0325 Jun 11, 2025
b72dd17
chore: remove tape.only
aayush0325 Jun 11, 2025
1959bfe
test: more tests
aayush0325 Jun 11, 2025
68e1c03
fix: fix implementation and add tests
aayush0325 Jun 11, 2025
19f2ae1
Merge remote-tracking branch 'upstream/develop' into lapack-dlatrs
stdlib-bot Jun 11, 2025
cd7ce0d
test: add tests
aayush0325 Jun 11, 2025
9e42ef5
test: add tests
aayush0325 Jun 11, 2025
82bab69
test: add tests
aayush0325 Jun 11, 2025
7dc23e4
test: add large values tests
aayush0325 Jun 12, 2025
f60f4c4
test: add ndarray tests
aayush0325 Jun 13, 2025
12322bb
docs: fix typo
kgryte Jun 13, 2025
c7f21b8
test: add offset tests
aayush0325 Jun 13, 2025
475a274
test: add negative stride test
aayush0325 Jun 13, 2025
d159785
test: add large stride tests
aayush0325 Jun 13, 2025
6dbee4c
test: add mixed strides test
aayush0325 Jun 13, 2025
8147ab4
feat: add examples and benchmarks
aayush0325 Jun 15, 2025
cc6b47a
docs: add index.d.ts file
aayush0325 Jun 15, 2025
c8e2e0b
docs: add ts test file
aayush0325 Jun 15, 2025
4af0a15
docs: add README
aayush0325 Jun 16, 2025
1f97555
docs: add README
aayush0325 Jun 16, 2025
6f9c1d8
chore: cleanup
aayush0325 Jun 16, 2025
162195b
docs: add repl.txt
aayush0325 Jun 16, 2025
940a493
docs: add comments
aayush0325 Jun 16, 2025
f526e9c
chore: copyright years
aayush0325 Jun 16, 2025
53c5b8f
test: add tests
aayush0325 Jun 16, 2025
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
342 changes: 342 additions & 0 deletions lib/node_modules/@stdlib/lapack/base/dlatrs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,342 @@
<!--

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

-->

# dlatrs

> LAPACK routine to solve a triangular system of equations with the scale factor set to prevent overflow.

<section class="intro">

The `dlatrs` routine solves a triangular system of linear equations with a scaling factor to prevent overflow. It can handle either the original system or its transpose:

<!-- <equation class="equation" label="eq:triangular_system_linear_equations" align="center" raw="A X = s B \quad \text{or} \quad A^{T} X = s B" alt="Triangular system of linear equations solved by `dlatrs`."> -->

```math
A X = s B \quad \text{or} \quad A^{T} X = s B
```

<!-- </equation> -->

where:

- `A` is an upper or lower triangular matrix,
- `X` is the solution vector,
- `B` is the right-hand side vector,
- `s` is a scaling factor ( 0 < `s` < 1 ) chosen to avoid numerical overflow.

The routine aims to compute a solution `x` such that the magnitude of all elements in `X` remains safely below the machine overflow threshold.

If it determines that solving the unscaled system would not lead to overflow, it calls the Level 2 BLAS routine `dtrsv` for a standard triangular solve. Otherwise, `dlatrs` applies additional scaling and iterative techniques to avoid overflow and maintain numerical stability.

In the presence of a singular matrix `A` (i.e., `A( j, j )` = 0 for some `j`), the scaling factor `s` is set to zero, and a non trivial solution to the homogeneous system:

<!-- <equation class="equation" label="eq:homogeneous_system" align="center" raw="A X = 0" alt="Non trivial solution to homogeneous system."> -->

```math
A X = 0
```

<!-- </equation> -->

is returned instead.

To ensure stability during the solve, `dlatrs` relies on several internal estimates, including:

- the off diagonal column norms,
- the scaling factor `s`,
- and an estimate of the maximum element in the intermediate vector `X`.

The algorithm may scale `X` at various stages using a factor `scale`, such that the final computed result satisfies:

<!-- <equation class="equation" label="eq:scale_factor" align="center" raw="x \leftarrow scale \cdot x" alt="Vector `X` being scaled by a scale factor `s`."> -->

```math
x \leftarrow scale \cdot x
```

<!-- </equation> -->

This ensures both correctness and numerical safety when solving the triangular system under limited-precision arithmetic.

</section>

<!-- /.intro -->

<section class="usage">

## Usage

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

#### dlatrs( order, uplo, trans, diag, normin, N, A, LDA, X, CNORM )

Solves a triangular system of equations with the scale factor set to prevent overflow.

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

var A = new Float64Array( [ 2.0, 1.0, -1.0, 0.0, 3.0, 2.0, 0.0, 0.0, 4.0 ] ); // => [ [ 2.0, 1.0, -1.0 ], [ 0.0, 3.0, 2.0 ], [ 0.0, 0.0, 4.0 ] ]
var X = new Float64Array( [ 5.0, 10.0, 20.0 ] );
var CNORM = new Float64Array( 3 );

var scale = dlatrs( 'row-major', 'upper', 'no-transpose', 'non-unit', 'no', 3, A, 3, X, CNORM );
// returns 1.0
// X => <Float64Array>[ 5.0, 0.0, 5.0 ]
// CNORM => <Float64Array>[ 0.0, 1.0, 3.0 ]
```

The function has the following parameters:

- **order**: storage layout.
- **uplo**: specifies whether `A` is an upper or lower triangular matrix.
- **trans**: specifies whether `A` should be transposed, conjugate-transposed, or not transposed.
- **diag**: specifies whether `A` has a unit diagonal.
- **normin**: specifies whether `CNORM` has the column norms on entry or not, should be either `yes` or `no`.
- **N**: number of elements along each dimension of `A`.
- **A**: input matrix stored in linear memory as a [`Float64Array`][mdn-float64array].
- **LDA**: stride of the first dimension of `A` (a.k.a., leading dimension of the matrix `A`).
- **X**: input vector [`Float64Array`][mdn-float64array] describing the right hand side `B`, should have `N` elements.
- **CNORM**: [`Float64Array`][mdn-float64array] used to store the column norms of `A`, should have `N` elements.

`X` is overwritten by the solution vector on the left hand side. If `normin` is `yes` then `CNORM` is an input parameter and it should contain the off diagonal column norms of `A`, else `CNORM` is overwritten by the off diagonal column norms computed by the routine

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, max-len -->

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

// Initial arrays...
var A0 = new Float64Array( [ 9999.0, 2.0, 1.0, -1.0, 0.0, 3.0, 2.0, 0.0, 0.0, 4.0 ] ); // => [ [ 2.0, 1.0, -1.0 ], [ 0.0, 3.0, 2.0 ], [ 0.0, 0.0, 4.0 ] ]
var X0 = new Float64Array( [ 9999.0, 5.0, 10.0, 20.0 ] );
var CNORM0 = new Float64Array( 4 );

// Create offset views...
var A = new Float64Array( A0.buffer, A0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
var X = new Float64Array( X0.buffer, X0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
var CNORM = new Float64Array( CNORM0.buffer, CNORM0.BYTES_PER_ELEMENT*1 ); // start at 2nd element

var scale = dlatrs( 'row-major', 'upper', 'no-transpose', 'non-unit', 'no', 3, A, 3, X, CNORM );
// returns 1.0
// X0 => <Float64Array>[ 9999.0, 5.0, 0.0, 5.0 ]
// CNORM0 => <Float64Array>[ 0.0, 0.0, 1.0, 3.0 ]
```

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

#### dlatrs.ndarray( uplo, trans, diag, normin, N, A, sa1, sa2, oa, X, sx, ox, CNORM, sc, oc )

Solves a triangular system of equations with the scale factor set to prevent overflow using alternative indexing semantics.

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

var A = new Float64Array( [ 2.0, 1.0, -1.0, 0.0, 3.0, 2.0, 0.0, 0.0, 4.0 ] ); // => [ [ 2.0, 1.0, -1.0 ], [ 0.0, 3.0, 2.0 ], [ 0.0, 0.0, 4.0 ] ]
var X = new Float64Array( [ 5.0, 10.0, 20.0 ] );
var CNORM = new Float64Array( 3 );

var scale = dlatrs.ndarray( 'upper', 'no-transpose', 'non-unit', 'no', 3, A, 3, 1, 0, X, 1, 0, CNORM, 1, 0 );
// returns 1.0
// X => <Float64Array>[ 5.0, 0.0, 5.0 ]
// CNORM => <Float64Array>[ 0.0, 1.0, 3.0 ]
```

The function has the following additional parameters:

- **sa1**: stride of the first dimensiosn of `A`.
- **sa2**: stride of the second dimension of `A`.
- **oa**: starting index for `A`.
- **sx**: stride length for `X`.
- **ox**: starting index for `X`.
- **sc**: stride length for `CNORM`.
- **oc**: starting index for `CNORM`.

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

var A = new Float64Array( [ 9999.0, 2.0, 1.0, -1.0, 0.0, 3.0, 2.0, 0.0, 0.0, 4.0 ] ); // => [ [ 2.0, 1.0, -1.0 ], [ 0.0, 3.0, 2.0 ], [ 0.0, 0.0, 4.0 ] ]
var X = new Float64Array( [ 9999.0, 5.0, 10.0, 20.0 ] );
var CNORM = new Float64Array( 4 );

var scale = dlatrs.ndarray( 'upper', 'no-transpose', 'non-unit', 'no', 3, A, 3, 1, 1, X, 1, 1, CNORM, 1, 1 );
// returns 1.0
// X => <Float64Array>[ 9999.0, 5.0, 0.0, 5.0 ]
// CNORM => <Float64Array>[ 0.0, 0.0, 1.0, 3.0 ]
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- Both functions mutate the input arrays `X` and `CNORM` (if `normin` = `no`).
- `dlatrs()` corresponds to the [LAPACK][LAPACK] routine [`dlatrs`][lapack-dlatrs].

</section>

<!-- /.notes -->

<section class="examples">

## Examples

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

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var numel = require( '@stdlib/ndarray/base/numel' );
var uniform = require( '@stdlib/random/array/uniform' );
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
var dlatrs = require( '@stdlib/lapack/base/dlatrs' );

// Specify matrix meta data:
var shape = [ 3, 3 ];
var strides = [ 3, 1 ];
var offset = 0;
var N = numel( shape );
var order = 'row-major';

// Create a matrix stored in linear memory:
var A = uniform( N, 2.0, 10.0, {
'dtype': 'float64'
});

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

var X = uniform( shape[ 0 ], 0.0, 10.0, {
'dtype': 'float64'
});

var CNORM = new Float64Array( shape[ 0 ] );

var scale = dlatrs( order, 'upper', 'no-transpose', 'non-unit', 'no', shape[ 0 ], A, strides[ 0 ], X, CNORM );
console.log( ndarray2array( A, shape, strides, offset, order ) );
console.log( 'scaling factor: ', scale );
```

</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-dlatrs]: https://www.netlib.org/lapack/explore-html-3.6.1/d8/dc3/dlatrs_8f_aab36439db8d657622a1d296b89d4acc0.html

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

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

</section>

<!-- /.links -->
Loading