-
-
Notifications
You must be signed in to change notification settings - Fork 907
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
aayush0325
wants to merge
34
commits into
stdlib-js:develop
Choose a base branch
from
aayush0325:lapack-dlatrs
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 12bc140
Merge remote-tracking branch 'upstream/develop' into lapack-dlatrs
stdlib-bot 7870b48
feat: add main export
aayush0325 51984dc
test: initial tests
aayush0325 ee429f0
test: add lower triangular cases
aayush0325 1184745
test: cleaning up
aayush0325 025d5f8
test: add transpose cases
aayush0325 d90cd03
test: add more cases for unit diagonal
aayush0325 2fc31d0
test: mmore tests
aayush0325 826c766
test: add scaled tests
aayush0325 b72dd17
chore: remove tape.only
aayush0325 1959bfe
test: more tests
aayush0325 68e1c03
fix: fix implementation and add tests
aayush0325 19f2ae1
Merge remote-tracking branch 'upstream/develop' into lapack-dlatrs
stdlib-bot cd7ce0d
test: add tests
aayush0325 9e42ef5
test: add tests
aayush0325 82bab69
test: add tests
aayush0325 7dc23e4
test: add large values tests
aayush0325 f60f4c4
test: add ndarray tests
aayush0325 12322bb
docs: fix typo
kgryte c7f21b8
test: add offset tests
aayush0325 475a274
test: add negative stride test
aayush0325 d159785
test: add large stride tests
aayush0325 6dbee4c
test: add mixed strides test
aayush0325 8147ab4
feat: add examples and benchmarks
aayush0325 cc6b47a
docs: add index.d.ts file
aayush0325 c8e2e0b
docs: add ts test file
aayush0325 4af0a15
docs: add README
aayush0325 1f97555
docs: add README
aayush0325 6f9c1d8
chore: cleanup
aayush0325 162195b
docs: add repl.txt
aayush0325 940a493
docs: add comments
aayush0325 f526e9c
chore: copyright years
aayush0325 53c5b8f
test: add tests
aayush0325 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 --> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This general change is to prevent copy-paste errors.