-
-
Notifications
You must be signed in to change notification settings - Fork 883
docs: add band matrix storage format documentation in the README of lapack/base
#8083
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
12
commits into
stdlib-js:develop
Choose a base branch
from
aayush0325:intro-lapack
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.
+207
−0
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d04bbf9
docs: add banded matrices intro section in lapack/base
aayush0325 5e9bfc5
docs: update copy
kgryte a165f27
docs: update copy
kgryte c418f0c
docs: update copy
kgryte 1d53da9
docs: update copy
kgryte 3983026
docs: add missing period
kgryte 461abb7
docs: fix copy
kgryte 4a61392
style: align ampersands
kgryte 939a824
style: remove trailing whitespace
kgryte b284d11
chore: remove newlines
aayush0325 d41266a
docs: add example
aayush0325 1bc74d4
fix: fix search and replace error
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
Some comments aren't visible on the classic Files Changed page.
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 |
---|---|---|
|
@@ -22,6 +22,213 @@ limitations under the License. | |
|
||
> Base (i.e., lower-level) linear algebra package (LAPACK) routines. | ||
|
||
<section class="intro"> | ||
|
||
## Band Storage | ||
|
||
Many LAPACK routines work with banded matrices, which are stored compactly in two-dimensional arrays arranged in linear memory in order to save memory and improve computational efficiency. The following are the different band storage formats used throughout LAPACK. | ||
|
||
### General Band Matrix Storage | ||
|
||
A general band matrix of size `M`-by-`N` with `KL` subdiagonals and `KU` superdiagonals is stored in a two-dimensional array `AB` with `KL+KU+1` rows and `N` columns. | ||
|
||
**Storage Mapping:** | ||
|
||
- Columns of the original matrix are stored in corresponding columns of the array `AB`. | ||
- Diagonals of the matrix are stored in rows of the array `AB`. | ||
- Element `A[i, j]` from the original matrix is stored in `AB[KU+1+i-j, j]`. | ||
- Valid range for `i`: `max(1, j-KU) <= i <= min(M, j+KL)`. | ||
|
||
#### Example | ||
|
||
Let `M = N = 5`, `KL = 2`, and `KU = 1`. Given an original band matrix `A` | ||
|
||
<!-- <equation class="equation" label="eq:band_matrix_a" align="center" raw="A = \left[\begin{array}{rrrrr} a_{11} & a_{12} & 0 & 0 & 0 \\ a_{21} & a_{22} & a_{23} & 0 & 0 \\ a_{31} & a_{32} & a_{33} & a_{34} & 0 \\ 0 & a_{42} & a_{43} & a_{44} & a_{45} \\ 0 & 0 & a_{53} & a_{54} & a_{55} \end{array}\right]" alt="Representation of band matrix A."> --> | ||
|
||
```math | ||
A = \left[ | ||
\begin{array}{rrrrr} | ||
a_{11} & a_{12} & 0 & 0 & 0 \\ | ||
a_{21} & a_{22} & a_{23} & 0 & 0 \\ | ||
a_{31} & a_{32} & a_{33} & a_{34} & 0 \\ | ||
0 & a_{42} & a_{43} & a_{44} & a_{45} \\ | ||
0 & 0 & a_{53} & a_{54} & a_{55} | ||
\end{array} | ||
\right] | ||
``` | ||
|
||
<!-- </equation> --> | ||
|
||
the band storage matrix `AB` is then | ||
|
||
<!-- <equation class="equation" label="eq:band_storage_ab" align="center" raw="AB = \left[\begin{array}{rrrrr} * & a_{12} & a_{23} & a_{34} & a_{45} \\ a_{11} & a_{22} & a_{33} & a_{44} & a_{55} \\ a_{21} & a_{32} & a_{43} & a_{54} & * \\ a_{31} & a_{42} & a_{53} & * & * \end{array}\right]" alt="Band storage representation of matrix A."> --> | ||
|
||
```math | ||
AB = \left[ | ||
\begin{array}{rrrrr} | ||
* & a_{12} & a_{23} & a_{34} & a_{45} \\ | ||
a_{11} & a_{22} & a_{33} & a_{44} & a_{55} \\ | ||
a_{21} & a_{32} & a_{43} & a_{54} & * \\ | ||
a_{31} & a_{42} & a_{53} & * & * | ||
\end{array} | ||
\right] | ||
``` | ||
|
||
<!-- </equation> --> | ||
|
||
`AB` is a 4×5 matrix as `KL+KU+1 = 2+1+1 = 4`. Elements marked `*` need not be set and are not referenced by LAPACK routines. | ||
|
||
**Note:** When a band matrix is supplied for LU factorization, space must be allowed to store an additional `KL` superdiagonals, which are generated by fill-in as a result of row interchanges. This means that the matrix is stored according to the above scheme, but with `KL + KU` superdiagonals. | ||
|
||
### Triangular Band Matrices | ||
|
||
Triangular band matrices are stored in the same format as general band matrices: | ||
|
||
- If `KL = 0`, the matrix is upper triangular. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to actually show matrices which are upper and lower triangular here. |
||
- If `KU = 0`, the matrix is lower triangular. | ||
|
||
### Symmetric or Hermitian Band Matrices | ||
|
||
For symmetric or Hermitian band matrices with `KD` subdiagonals or superdiagonals, only the upper or lower triangle (as specified by an `UPLO` parameter) needs to be stored. | ||
|
||
**Upper Triangle Storage (UPLO = 'U'):** | ||
|
||
- Element `A[i, j]` is stored in `AB[KD+1+i-j, j]`. | ||
- Valid range for `i`: `max(1, j-KD) <= i <= j`. | ||
|
||
**Lower Triangle Storage (UPLO = 'L'):** | ||
|
||
- Element `A[i, j]` is stored in `AB[1+i-j, j]`. | ||
- Valid range for `i`: `j <= i <= min(N, j+KD)`. | ||
|
||
#### Example | ||
|
||
Let `N = 5` and `KD = 2`. Given the following matrix `A`, | ||
|
||
<!-- <equation class="equation" label="eq:symmetric_upper_a" align="center" raw="A = \left[\begin{array}{rrrrr} a_{11} & a_{12} & a_{13} & 0 & 0 \\ {a_{12}} & a_{22} & a_{23} & a_{24} & 0 \\ {a_{13}} & {a_{23}} & a_{33} & a_{34} & a_{35} \\ 0 & {a_{24}} & {a_{34}} & a_{44} & a_{45} \\ 0 & 0 & {a_{35}} & {a_{45}} & a_{55} \end{array}\right]" alt="Representation of symmetric band matrix A (upper triangle)."> --> | ||
|
||
```math | ||
A = \left[ | ||
\begin{array}{rrrrr} | ||
a_{11} & a_{12} & a_{13} & 0 & 0 \\ | ||
{a_{12}} & a_{22} & a_{23} & a_{24} & 0 \\ | ||
{a_{13}} & {a_{23}} & a_{33} & a_{34} & a_{35} \\ | ||
0 & {a_{24}} & {a_{34}} & a_{44} & a_{45} \\ | ||
0 & 0 & {a_{35}} & {a_{45}} & a_{55} | ||
\end{array} | ||
\right] | ||
``` | ||
|
||
<!-- </equation> --> | ||
|
||
the band storage matrix `AB` when `UPLO = 'U'` (i.e., upper triangle) is | ||
|
||
<!-- <equation class="equation" label="eq:symmetric_upper_ab" align="center" raw="AB = \left[\begin{array}{rrrrr} * & * & a_{13} & a_{24} & a_{35} \\ * & a_{12} & a_{23} & a_{34} & a_{45} \\ a_{11} & a_{22} & a_{33} & a_{44} & a_{55} \end{array}\right]" alt="Band storage representation of symmetric matrix A (upper triangle)."> --> | ||
|
||
```math | ||
AB = \left[ | ||
\begin{array}{rrrrr} | ||
* & * & a_{13} & a_{24} & a_{35} \\ | ||
* & a_{12} & a_{23} & a_{34} & a_{45} \\ | ||
a_{11} & a_{22} & a_{33} & a_{44} & a_{55} | ||
\end{array} | ||
\right] | ||
``` | ||
|
||
<!-- </equation> --> | ||
|
||
`AB` is a 3×5 matrix as `KD+1 = 2+1 = 3`. Similarly, given the following matrix `A`, | ||
|
||
<!-- <equation class="equation" label="eq:symmetric_lower_a" align="center" raw="A = \left[\begin{array}{rrrrr} a_{11} & {a_{21}} & {a_{31}} & 0 & 0 \\ a_{21} & a_{22} & {a_{32}} & {a_{42}} & 0 \\ a_{31} & a_{32} & a_{33} & {a_{43}} & {a_{53}} \\ 0 & a_{42} & a_{43} & a_{44} & {a_{54}} \\ 0 & 0 & a_{53} & a_{54} & a_{55} \end{array}\right]" alt="Representation of symmetric band matrix A (lower triangle)."> --> | ||
|
||
```math | ||
A = \left[ | ||
\begin{array}{rrrrr} | ||
a_{11} & {a_{21}} & {a_{31}} & 0 & 0 \\ | ||
a_{21} & a_{22} & {a_{32}} & {a_{42}} & 0 \\ | ||
a_{31} & a_{32} & a_{33} & {a_{43}} & {a_{53}} \\ | ||
0 & a_{42} & a_{43} & a_{44} & {a_{54}} \\ | ||
0 & 0 & a_{53} & a_{54} & a_{55} | ||
\end{array} | ||
\right] | ||
``` | ||
|
||
<!-- </equation> --> | ||
|
||
the band storage matrix `AB` when `UPLO = 'L'` (i.e., lower triangle) is | ||
|
||
<!-- <equation class="equation" label="eq:symmetric_lower_ab" align="center" raw="AB = \left[\begin{array}{rrrrr} a_{11} & a_{22} & a_{33} & a_{44} & a_{55} \\ a_{21} & a_{32} & a_{43} & a_{54} & * \\ a_{31} & a_{42} & a_{53} & * & * \end{array}\right]" alt="Band storage representation of symmetric matrix A (lower triangle)."> --> | ||
|
||
```math | ||
AB = \left[ | ||
\begin{array}{rrrrr} | ||
a_{11} & a_{22} & a_{33} & a_{44} & a_{55} \\ | ||
a_{21} & a_{32} & a_{43} & a_{54} & * \\ | ||
a_{31} & a_{42} & a_{53} & * & * | ||
\end{array} | ||
\right] | ||
``` | ||
|
||
`AB` is a 3×5 matrix as `KD+1 = 2+1 = 3`. | ||
|
||
<!-- </equation> --> | ||
|
||
### Example | ||
|
||
Consider a 4×4 general band matrix with `KL = 2` subdiagonals and `KU = 1` superdiagonal: | ||
|
||
<!-- <equation class="equation" label="eq:band_matrix_numeric_a" align="center" raw="A = \left[\begin{array}{rrrr} 1.0 & 2.0 & 0.0 & 0.0 \\ 3.0 & 4.0 & 5.0 & 0.0 \\ 6.0 & 7.0 & 8.0 & 9.0 \\ 0.0 & 10.0 & 11.0 & 12.0 \end{array}\right]" alt="Representation of band matrix A with numeric values."> --> | ||
|
||
```math | ||
A = \left[ | ||
\begin{array}{rrrr} | ||
1.0 & 2.0 & 0.0 & 0.0 \\ | ||
3.0 & 4.0 & 5.0 & 0.0 \\ | ||
6.0 & 7.0 & 8.0 & 9.0 \\ | ||
0.0 & 10.0 & 11.0 & 12.0 | ||
\end{array} | ||
\right] | ||
``` | ||
|
||
<!-- </equation> --> | ||
|
||
#### Band Storage Representation | ||
|
||
The band storage matrix `AB` has dimensions `(KL+KU+1) × N = (2+1+1) × 4 = 4 × 4`: | ||
|
||
<!-- <equation class="equation" label="eq:band_storage_numeric_a" align="center" raw="AB = \left[\begin{array}{rrrr} * & 2.0 & 5.0 & 9.0 \\ 1.0 & 4.0 & 8.0 & 12.0 \\ 3.0 & 7.0 & 11.0 & * \\ 6.0 & 10.0 & * & * \end{array}\right]" alt="Band storage representation of matrix A with numeric values."> --> | ||
|
||
```math | ||
AB = \left[ | ||
\begin{array}{rrrr} | ||
* & 2.0 & 5.0 & 9.0 \\ | ||
1.0 & 4.0 & 8.0 & 12.0 \\ | ||
3.0 & 7.0 & 11.0 & * \\ | ||
6.0 & 10.0 & * & * | ||
\end{array} | ||
\right] | ||
``` | ||
|
||
<!-- </equation> --> | ||
|
||
Here's how to represent this band matrix in JavaScript using `Float64Array`: | ||
|
||
##### Row-Major Layout | ||
|
||
```javascript | ||
var AB = new Float64Array( [ 0.0, 2.0, 5.0, 9.0, 1.0, 4.0, 8.0, 12.0, 3.0, 7.0, 11.0, 0.0, 6.0, 10.0, 0.0, 0.0 ] ); | ||
``` | ||
|
||
##### Column-Major Layout | ||
|
||
```javascript | ||
var AB = new Float64Array( [ 0.0, 1.0, 3.0, 6.0, 2.0, 4.0, 7.0, 10.0, 5.0, 8.0, 11.0, 0.0, 9.0, 12.0, 0.0, 0.0 ] ); | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.intro --> | ||
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
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.
Overall, this is looking good. It's a more readable version of https://netlib.org/lapack/lug/node124.html. That stated, one thing which needs to be changed is that currently everything is written using one-based indexing. We need to update everything to zero-based indexing.