Skip to content

Commit 8df204f

Browse files
committed
document qr
1 parent cda4b0d commit 8df204f

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

doc/specs/stdlib_linalg.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,54 @@ Exceptions trigger an `error stop`.
899899
{!example/linalg/example_determinant2.f90!}
900900
```
901901

902+
## `qr` - Compute the QR factorization of a matrix.
903+
904+
### Status
905+
906+
Experimental
907+
908+
### Description
909+
910+
This subroutine computes the QR factorization of a `real` or `complex` matrix: \( A = Q R \). where \( Q \)
911+
is orthonormal and \( R \) is upper-triangular. Matrix \( A \) has size `[m,n]`, with \( m \ge n \).
912+
913+
The results are returned in output matrices \( Q \) and \(R \), that have the same type and kind as \( A \).
914+
Given `k = min(m,n)`, one can write \( A = \( Q_1 Q_2 \) \cdot \( \frac{R_1}{0}\) \).
915+
Because the lower rows of \( R \) are zeros, the user may require to return full matrices (provide `shape(Q)==[m,m]`, `shape(R)==[m,n]`)
916+
or reduced matrices: \( A = Q_1 R_1 \) (provide `shape(Q)==[m,k]`, `shape(R)==[k,n]`).
917+
918+
### Syntax
919+
920+
`call ` [[stdlib_linalg(module):qr(interface)]] `(a, q, r, [, storage] [, overwrite_a] [, err])`
921+
922+
### Arguments
923+
924+
`a`: Shall be a rank-2 `real` or `complex` array containing the coefficient matrix of size `[m,n]`. It is normally an `intent(in)` argument. If `overwrite_a=.true.`, it is an `intent(inout)` argument, and is destroyed upon return.
925+
926+
`q`: Shall be a rank-2 array of the same kind as `a`, containing the orthonormal matrix `q`. It is an `intent(out)` argument. It should have shape either `[m,m]` or `[m,k]` whether the full or the reduced problem is sought for.
927+
928+
`r`: Shall be a rank-2 array of the same kind as `a`, containing the upper triangular matrix `r`. It is an `intent(out)` argument. It should have shape either `[m,n]` or `[k,n]` whether the full or the reduced problem is sought for.
929+
930+
`storage` (optional): Shall be a rank-1 array of the same type and kind as `a`, providing working storage for the solver. It minimum size can be determined with a call to [[stdlib_linalg(module):qr_space(interface)]]. It is an `intent(inout)` argument.
931+
932+
`overwrite_a` (optional): Shall be an input `logical` flag (default: `.false.`). If `.true.`, input matrix `A` will be used as temporary storage and overwritten. This avoids internal data allocation. This is an `intent(in)` argument.
933+
934+
`err` (optional): Shall be a `type(linalg_state_type)` value. This is an `intent(out)` argument.
935+
936+
### Return value
937+
938+
Returns the QR factorization matrices into the \( Q \) and \( R \) arguments.
939+
940+
Raises `LINALG_VALUE_ERROR` if any of the matrices has invalid or unsuitable size for the full/reduced problem.
941+
Raises `LINALG_ERROR` on insufficient user storage space.
942+
If the state argument `err` is not present, exceptions trigger an `error stop`.
943+
944+
### Example
945+
946+
```fortran
947+
{!example/linalg/example_qr.f90!}
948+
```
949+
902950
## `qr_space` - Compute internal working space requirements for the QR factorization.
903951

904952
### Status

src/stdlib_linalg.fypp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,28 @@ module stdlib_linalg
454454

455455
! QR factorization of rank-2 array A
456456
interface qr
457+
!! version: experimental
458+
!!
459+
!! Computes the QR factorization of matrix \( A = Q R \).
460+
!! ([Specification](../page/specs/stdlib_linalg.html#qr-compute-the-qr-factorization-of-a-matrix))
461+
!!
462+
!!### Summary
463+
!! Compute the QR factorization of a `real` or `complex` matrix: \( A = Q R \), where \( Q \) is orthonormal
464+
!! and \( R \) is upper-triangular. Matrix \( A \) has size `[m,n]`, with \( m\ge n \).
465+
!!
466+
!!### Description
467+
!!
468+
!! This interface provides methods for computing the QR factorization of a matrix.
469+
!! Supported data types include `real` and `complex`. If a pre-allocated work space
470+
!! is provided, no internal memory allocations take place when using this interface.
471+
!!
472+
!! Given `k = min(m,n)`, one can write \( A = \( Q_1 Q_2 \) \cdot \( \frac{R_1}{0}\) \).
473+
!! The user may want the full problem (provide `shape(Q)==[m,m]`, `shape(R)==[m,n]`) or the reduced
474+
!! problem only: \( A = Q_1 R_1 \) (provide `shape(Q)==[m,k]`, `shape(R)==[k,n]`).
475+
!!
476+
!!@note The solution is based on LAPACK's QR factorization (`*GEQRF`) and ordered matrix output (`*ORGQR`, `*UNGQR`).
477+
!!@note BLAS/LAPACK backends do not currently support extended precision (``xdp``).
478+
!!
457479
#:for rk,rt,ri in RC_KINDS_TYPES
458480
#:if rk!="xdp"
459481
pure module subroutine stdlib_linalg_${ri}$_qr(a,q,r,overwrite_a,storage,err)

0 commit comments

Comments
 (0)