QR and Householder kernels
Julia versions of the LAPACK and PLASMA kernels used by tiled QR factorizations. Most routines come in two forms: a low-level one with explicit sizes and workspace that mirrors the LAPACK signature, and a convenience one that infers them. These routines are not exported, except parfb! and pemv!; call the others as NextLA.geqrt!, etc.
| Kernel | Purpose |
|---|---|
larfg! | generate an elementary Householder reflector |
larf! | apply one reflector |
larft! | form the triangular factor T of a block reflector |
larfb! | apply a block reflector |
geqr2! | unblocked QR |
geqrt! | blocked QR with compact-WY T factors |
unmqr! | apply Q or Qᵀ from geqrt! |
tsqrt! / tsmqr! | QR of a triangle on top of a square tile, and applying its Q |
ttqrt! / ttmqr! | QR of a triangle on top of a triangle, and applying its Q |
parfb!, pamm!, pemv! | pentagonal block-reflector application and its building blocks |
Plane (Givens) rotations, lartg, are on the Rotations page.
Householder reflectors
NextLA.larfg! — Method
larfg!(n, alpha, x, incx, tau)Generate an elementary reflector H such that: H * [alpha; x] = [beta; 0]
where H = I - tau * v * v^H, v = [1; x/scale], and beta = -sign(alpha) * ||[alpha; x]||
This routine generates a complex elementary Householder reflector H of order n, such that when applied to the vector [alpha; x], it zeros out the x portion and produces [beta; 0] where beta has the same magnitude as the original vector.
Arguments
n: Order of the reflector (length of full vector [alpha; x])alpha: Scalar element, the first component of the vectorx: Vector of length n-1, remaining components of the vectorincx: Ignored; all ofxis used with unit stride (kept for the LAPACK calling sequence)tau: Output scalar factor for the reflector
Returns
alpha: Modified to contain beta (the new first component)tau: Scalar factor such that H = I - tau * v * v^H
Algorithm
The algorithm handles potential under/overflow carefully by scaling when necessary. The reflector is chosen so that the reflection introduces no unnecessary amplification of round-off errors.
Special cases:
- If x = 0 and imag(alpha) = 0, then tau = 0 (no reflection needed)
- If n ≤ 1, then tau = 0 (trivial case)
Mathematical Details
For the elementary reflector H = I - tau * v * v^H where v = [1; u]:
- tau = (beta - alpha) / beta for real case
- tau = (beta - Re(alpha))/beta - i*Im(alpha)/beta for complex case
- The vector u replaces x on output
Note
This is a low-level LAPACK-style computational routine. Input validation should be performed by higher-level interfaces.
Examples
julia> alpha = rand(); x = rand(1023); y = [alpha; x];
julia> beta, tau = NextLA.larfg!(1024, alpha, x, 1, 0.0); # x now holds v[2:end]
julia> v = [1; x];
julia> y - tau * v * (v' * y) ≈ [beta; zeros(1023)] # H * y = [beta; 0]
trueNextLA.larfg! — Method
larfg!(alpha, x, incx, tau) -> (alpha, tau)Generate an elementary reflector H such that H * [alpha; x] = [beta; 0].
This is a short form of larfg!(n, alpha, x, incx, tau) with n = length(x) + 1. It computes a Householder reflector H = I - tau * v * v^H with v = [1; x_out].
Arguments
alpha: First component of the vectorx: Remaining components; overwritten with v[2:end]incx: Ignored; all ofxis used with unit stridetau: Input scalar; its value is not used
Returns
alpha: The new first component (beta)tau: Scalar factor of the elementary reflector
If x is empty, alpha is returned unchanged and tau is zero.
Examples
julia> alpha = rand(); x = rand(1023); y = [alpha; x];
julia> beta, tau = NextLA.larfg!(alpha, x, 1, 0.0); # x now holds v[2:end]
julia> v = [1; x];
julia> y - tau * v * (v' * y) ≈ [beta; zeros(1023)] # H * y = [beta; 0]
trueMathematical Background
Creates H such that H * [alpha; x] = [beta; 0; 0; ...] with beta = -sign(real(alpha)) * ||[alpha; x]||. The sign is chosen to avoid cancellation. On return, x holds v[2:end].
NextLA.ilazlc — Method
ilazlc(m, n, a) -> IntFind the index of the last non-zero column in an m-by-n matrix. Scans from column n backwards to column 1, checking all rows in each column for non-zero elements.
Arguments
m: Number of rows in matrix an: Number of columns in matrix aa: Matrix to scan
Returns
- Index of last column containing at least one non-zero element, or 0 if all elements are zero
NextLA.ilazlr — Method
ilazlr(m, n, a) -> IntFind the index of the last non-zero row in an m-by-n matrix. Scans all columns to determine the effective row dimension.
Arguments
m: Number of rows in matrix an: Number of columns in matrix aa: Matrix to scan
Returns
- Index of last row containing at least one non-zero element, or 0 if all elements are zero
NextLA.larf! — Method
larf!(side, v, incv, tau, C) -> CApply an elementary reflector H to a matrix C, where H = I - tau * v * v^H.
This is a high-level interface to the elementary reflector application routine. The reflector can be applied from either the left (HC) or right (CH) side.
Arguments
side: Character specifying application side ('L' for left, 'R' for right)v: Vector defining the elementary reflectorincv: Increment for the elements of v (typically 1)tau: Scalar factor for the reflectorC: Matrix to be transformed in-place
Returns
- The modified matrix
C
Input Validation
- For side='L': length(v) must equal number of rows in C
- For side='R': length(v) must equal number of columns in C
- Otherwise, or if
sideis not 'L' or 'R', throwsArgumentError
Examples
julia> using LinearAlgebra
julia> C = rand(1024, 1024); v = rand(1024); tau = 2 / (v' * v);
julia> HC = (I - tau * v * v') * C;
julia> NextLA.larf!('L', v, 1, tau, C); # C := H * C
julia> C ≈ HC
trueNextLA.larf! — Method
larf!(side, m, n, v, incv, tau, c, work)Apply an elementary reflector H to a m-by-n matrix C from either the left or the right.
H = I - tau * v * v^H
where tau is a scalar and v is a vector.
Arguments
side: Character specifying the side of application- 'L': apply H from the left (H * C)
- 'R': apply H from the right (C * H)
m: Number of rows in matrix Cn: Number of columns in matrix Cv: Array containing the elementary reflector vectorincv: Increment for the elements of v (typically 1)tau: Scalar factor for the elementary reflectorc: m-by-n matrix to be modified in-placework: Workspace array
Algorithm
The reflector is applied with full sizes: all m (side = 'L') or n (side = 'R') entries of v and the whole of C are used. There is no scan for trailing zeros, so no scalar indexing is needed on a GPU. (ilazlc and ilazlr, which did that scan, are no longer called.)
For side = 'L': Computes C := H * C = (I - tau * v * v^H) * C For side = 'R': Computes C := C * H = C * (I - tau * v * v^H)
Notes
This is a low-level computational routine used internally by higher-level QR factorization algorithms. The workspace array must be properly allocated.
NextLA.larft! — Method
larft!(direct, storev, V, tau, T_mat)Form the triangular factor T of a complex block reflector H from elementary reflectors and their scalar factors.
This is a high-level interface that determines n and k from the size of V. It writes T into T_mat and returns nothing. The block reflector H has the form: H = I - V * T * V^H
Arguments
direct: Character indicating the order of elementary reflector products- 'F': H = H(1) H(2) ... H(k) (Forward)
- 'B': H = H(k) ... H(2) H(1) (Backward)
storev: Character indicating how reflector vectors are stored in V- 'C': Columnwise storage (V is n-by-k)
- 'R': Rowwise storage (V is k-by-n)
V: Matrix containing the elementary reflector vectorstau: Vector containing scalar factors of the elementary reflectorsT_mat: k-by-k matrix where the triangular factor T is written
Returns
nothing.T_matholds T: upper triangular for direct = 'F', lower triangular for direct = 'B'.
Input Validation
- Matrix V and vector tau must have compatible dimensions
- For 'C' storage: size(V,2) must equal length(tau)
- For 'R' storage: size(V,1) must equal length(tau)
- Otherwise throws
ArgumentError
Mathematical Background
The triangular factor T enables efficient block operations. Instead of applying k individual reflectors H(1), H(2), ..., H(k), the block reflector H = I - VTV^H can be applied in O(n²k) operations rather than O(nk²).
Examples
julia> using LinearAlgebra
julia> A = rand(1024, 128); F = copy(A); tau = zeros(128);
julia> NextLA.geqr2!(F, tau); # 128 reflectors below the diagonal of F
julia> T = zeros(128, 128); NextLA.larft!('F', 'C', F, tau, T);
julia> V = tril(F, -1) + Matrix(I, 1024, 128);
julia> (I - V * UpperTriangular(T) * V') * [triu(F[1:128, :]); zeros(896, 128)] ≈ A
trueNextLA.larft! — Method
larft!(direct, storev, n, k, v, tau, T_mat)Form the triangular factor T of a complex block reflector H of order n, where H is defined as a product of k elementary reflectors.
The block reflector H has the form: H = I - V * T * V^H
where V is n-by-k and contains the elementary reflector vectors, and T is the k-by-k triangular factor computed by this routine: upper triangular for direct = 'F', lower triangular for direct = 'B'.
Implemented as a KernelAbstractions kernel (single work item), runs on any KA backend (CPU, CUDA, ROCm, oneAPI, Metal) without CPU copies.
Arguments
direct: Character indicating the order of the elementary reflectors- 'F': H = H(1) H(2) ... H(k) (Forward)
- 'B': H = H(k) ... H(2) H(1) (Backward)
storev: Character indicating how the reflector vectors are stored in V- 'C': Columnwise storage (V is n-by-k)
- 'R': Rowwise storage (V is k-by-n)
n: Order of the reflector Hk: Number of elementary reflectors (order of T)v: Matrix containing the elementary reflector vectorstau: Array containing the scalar factors of the elementary reflectorsT_mat: k-by-k matrix where the triangular factor T will be stored
Writes T into T_mat and returns nothing.
Algorithm
The algorithm computes T such that H = I - V * T * V^H where each column (or row) of V represents an elementary reflector. The triangular structure ensures efficient application of the block reflector.
For forward direction (direct='F'):
- T[i,i] = tau[i] (diagonal elements)
- T[j,i] = -tau[i] * V[i,j] * T[j,j:i-1] for j < i (upper triangular part)
For backward direction (direct='B'):
- T[i,i] = tau[i] (diagonal elements)
- T[j,i] = -tau[i] * V[j,i] * T[i+1:j,i] for j > i (lower triangular part)
Notes
This is the core computational routine for forming block reflector coefficients. The matrix T enables efficient application of multiple reflectors simultaneously.
NextLA.larfb! — Method
larfb!(side, trans, direct, storev, V, T, C)Apply a complex block reflector H or its conjugate transpose H^H to a matrix C.
This is a high-level interface that automatically computes required dimensions and allocates workspace for the block reflector application.
The block reflector H has the form: H = I - V * T * V^H
where V contains k elementary reflector vectors and T is an upper triangular block reflector coefficient matrix.
Arguments
side: Character specifying which side to apply the reflector- 'L': Apply H from the left (HC or H^HC)
- 'R': Apply H from the right (CH or CH^H)
trans: Character specifying which form to apply- 'N': Apply H (no conjugate transpose)
- 'C': Apply H^H (conjugate transpose)
direct: Character indicating how H is formed from elementary reflectors- 'F': H = H(1) H(2) ... H(k) (Forward); the only value implemented
- 'B': H = H(k) ... H(2) H(1) (Backward); not implemented, throws
ArgumentError
storev: Character indicating how reflector vectors are stored in V- 'C': Reflector vectors stored columnwise in V; the only value implemented
- 'R': Reflector vectors stored rowwise in V; not implemented, throws
ArgumentError
V: Matrix containing the elementary reflector vectorsT: Upper triangular k×k matrix with block reflector coefficientsC: m×n matrix to be transformed in-place
Algorithm
Applies the block reflector efficiently by:
- Computing W = C^H * V (or W = C * V for right multiplication)
- Multiplying by the triangular matrix T: W := W * T (or W * T^H)
- Applying rank-k update: C := C - V * W^H (or C - W * V^H)
The algorithm exploits the triangular structure of the reflector matrix to minimize computational cost.
Examples
julia> using LinearAlgebra
julia> A = rand(1024, 128); V = copy(A); T = zeros(128, 128);
julia> NextLA.geqrt!(V, T); # one block of 128 reflectors
julia> C = rand(1024, 1024); C0 = copy(C);
julia> NextLA.larfb!('L', 'C', 'F', 'C', V, T, C); # C := Qᴴ * C
julia> C ≈ qr(A).Q' * C0
trueNextLA.larfb! — Method
larfb!(side, trans, direct, storev, m, n, k, v, ldv, t, c, work)Applies complex block reflector H or its transpose H^H to m-by-n matrix C from either the left or the right Implemented with Julia internal functions for matrix multiplication
Arguments
- 'side':
- 'L' : apply H or H^H from the left
- 'R' : apply H or H^H from the right
- 'trans':
- 'N' : apply H
- 'C' : apply H^H
- 'direct': indicates how H is formed from product of elementary reflectors
- 'F' : H = H(1) H(2) ... H(k) (Forward); the only value implemented
- 'B' : H = H(k) ... H(2) H(1) (Backward); not implemented, throws
ArgumentError
- 'storev': indcicates how the vectors which define the elementary reflectors are stored
- 'C' : columnwise; the only value implemented
- 'R' : rowwise; not implemented, throws
ArgumentError
- 'm': the number of rows of matrix c
- 'n': the number of columns of matrix c
- 'k': the order of marix t (= the number of elementary reflectors whose roduct defines the block reflector)
- 'v': dimension - (ldv, k) if storev = 'C' - (ldv, m) if storev = 'R' and side = 'L' - (ldv, n) if storev = 'R' and side = 'R'
- 'ldv': the leading dimension of array v
- if storev = 'C' and side = 'L', ldv >= max(1,m)
- if storev = 'C' and side = 'R', ldv >= max(1,n)
- if storev = 'R', ldv >= k
- 't': dimension (ldv, k), the triangular k-by-k matrix t in representation of the block reflector
- 'c':
- on entry m-by-n matrix
- on exit, overwritten by HC or H^HC or CH or CH^H
- 'work': dimension (ldwork, k)
Tile QR
NextLA.geqr2! — Method
geqr2!(A, tau)Helper function for unblocked QR factorization using Householder reflectors. Allocates the workspace and calls geqr2!(m, n, A, tau, work).
Arguments
A: Input matrix (m × n), modified in place to contain Q and R factorstau: Output vector of scalar factors (length min(m,n)), filled in place
Returns
nothing. The results are inAandtau.
Examples
julia> using LinearAlgebra
julia> A = rand(1024, 256); F = copy(A); tau = zeros(256);
julia> NextLA.geqr2!(F, tau);
julia> G = LinearAlgebra.QR(F, tau); # same storage as LAPACK geqr2
julia> G.Q * G.R ≈ A
trueNextLA.geqr2! — Method
geqr2!(m, n, A, tau, work)Compute unblocked QR factorization of an m-by-n matrix A using Householder reflectors. The matrix A is overwritten with the Q and R factors.
Arguments
m: Number of rows in matrix An: Number of columns in matrix AA: Input matrix (m × n), modified in place to contain Q and R factorstau: Output vector of scalar factors (length min(m,n))work: Workspace vector (length n)
Returns nothing. Negative m or n throws ArgumentError.
Algorithm
Uses Householder reflectors H(i) to zero out elements below the diagonal. For each column i, generates H(i) and applies it to remaining columns.
Implemented as a KernelAbstractions kernel (single work item), runs on any KA backend (CPU, CUDA, ROCm, oneAPI, Metal) without CPU copies.
NextLA.geqrt! — Method
geqrt!(A, T)Helper function for blocked QR factorization. Computes A = Q*R where Q is orthogonal and R is upper triangular.
Arguments
A: Input matrix (m × n), modified in place to contain R in upper triangle and Q factors belowT: Output matrix (ib × n) for the block reflector factors, filled in place. The block size isib = size(T, 1).
Returns
nothing. The results are inAandT. The scalar factorstauare computed in a temporary vector and not returned.
Examples
julia> using LinearAlgebra
julia> A = rand(1024, 1024); F = copy(A); T = zeros(64, 1024); # inner block size 64
julia> NextLA.geqrt!(F, T);
julia> G = LinearAlgebra.QRCompactWY(F, T); # same storage as LAPACK geqrt
julia> G.Q * G.R ≈ A
trueNextLA.geqrt! — Method
geqrt!(m, n, ib, A, T_matrix, tau, work)Compute blocked QR factorization of an m-by-n matrix A using block size ib. The matrix A is overwritten with the Q and R factors, and T contains the triangular factor of the block reflector.
Arguments
m: Number of rows in matrix An: Number of columns in matrix Aib: Block size for the factorization (must be > 0 if m,n > 0)A: Input matrix (m × n), modified in place to contain Q and R factorsT: Output triangular block reflector matrix (ib × n)tau: Output vector of scalar factors (length n)work: Workspace vector (length ib × n)
Algorithm
Uses a block algorithm that processes ib columns at a time. For each block, performs unblocked QR and then applies the block reflector to the remaining columns.
NextLA.unmqr! — Method
unmqr!(side, trans, A, T, C) -> CApply orthogonal matrix Q from QR factorization to matrix C.
This is a high-level interface that automatically determines dimensions and allocates workspace to apply the orthogonal factor Q from a QR factorization to a general matrix C.
Arguments
side: Character specifying application side- 'L': Apply Q from left (QC or Q^HC)
- 'R': Apply Q from right (CQ or CQ^H)
trans: Character specifying transpose operation- 'N': Apply Q (no transpose)
- 'C': Apply Q^H (conjugate transpose)
A: QR factorization result from geqrt! (contains reflector vectors)T: Block reflector coefficient matrix from geqrt!. The block size isib = size(T, 1).C: Matrix to transform (modified in-place)
Returns
- Modified matrix
Cafter applying the orthogonal transformation
Input Validation
- Matrix dimensions must be compatible with the QR factorization
- Block size ib = size(T, 1) must be positive
- For side='L': number of rows of C must match Q dimension
- For side='R': number of columns of C must match Q dimension
Algorithm
Uses the blocked compact WY representation to apply Q efficiently through matrix-matrix operations rather than individual elementary reflectors.
Examples
julia> using LinearAlgebra
julia> A = rand(1024, 1024); F = copy(A); T = zeros(64, 1024);
julia> NextLA.geqrt!(F, T);
julia> C = rand(1024, 1024); C0 = copy(C);
julia> NextLA.unmqr!('L', 'C', F, T, C); # C := Qᴴ * C
julia> C ≈ qr(A).Q' * C0
trueNextLA.unmqr! — Method
unmqr!(side, trans, m, n, k, ib, A, lda, T_matrix, C, work)Apply orthogonal matrix Q (or Q^H) from a QR factorization to a general matrix C.
Overwrites the general m-by-n matrix C with: side = 'L' side = 'R' trans = 'N' Q * C C * Q trans = 'C' Q^H * C C * Q^H
where Q is a unitary matrix defined as the product of k elementary reflectors: Q = H(1) H(2) ... H(k)
as returned by geqrt!. Q is of order m if side = 'L' and of order n if side = 'R'.
Arguments
side: Character specifying which side to apply Q- 'L': Apply Q or Q^H from the left
- 'R': Apply Q or Q^H from the right
trans: Character specifying transpose operation- 'N': No transpose, apply Q
- 'C': Conjugate transpose, apply Q^H
m: Number of rows of matrix C (≥ 0)n: Number of columns of matrix C (≥ 0)k: Number of elementary reflectors defining Q- If side = 'L': m ≥ k ≥ 0
- If side = 'R': n ≥ k ≥ 0
ib: Inner block size (≥ 0)A: Matrix of dimension (lda, k) containing reflector vectors The i-th column contains the vector defining elementary reflector H(i), as returned by geqrt! in the first k columnslda: Leading dimension of array A- If side = 'L': lda ≥ max(1,m)
- If side = 'R': lda ≥ max(1,n)
T: ib×k triangular factor of the block reflector T is upper triangular by blocks (economic storage)C: m×n matrix to be transformed (modified in-place)work: Workspace array
Algorithm
The routine applies Q using the compact WY representation stored in A and T. It processes the elementary reflectors in blocks of size ib, using efficient block operations (larfb!) for high performance.
The order of applying blocks depends on side and trans parameters to ensure numerical stability and efficiency.
Notes
This is a core computational routine for applying orthogonal transformations from QR factorizations. It is typically called by higher-level interfaces.
NextLA.tsqrt! — Method
tsqrt!(A1, A2, T)Compute QR factorization of a triangular-pentagonal matrix using block algorithm.
This is a high-level interface that automatically allocates workspace and computes the QR factorization of the combined matrix [A1; A2] where A1 is upper triangular and A2 is general.
Arguments
A1: n×n upper triangular matrix (modified in-place to store R factor)A2: m×n general matrix (modified in-place to store reflector vectors)T: ib×n matrix, filled in place with the block reflector coefficients. The block size isib = size(T, 1)(typically 32-64 for good performance).
Returns
nothing.A1holds R,A2holds the reflector vectors andTholds the block reflector coefficients. The scalar factorstauare computed in a temporary vector and not returned.
Input Validation
- A1 should be square (n×n) and A2 should have the same number of columns (m×n); these are not checked
- Block size ib = size(T, 1) must be positive; otherwise throws
ArgumentError
Algorithm Notes
Uses blocked algorithm for efficiency with large matrices. The compact WY representation (stored in T) enables efficient application of the Q factor.
Examples
julia> using LinearAlgebra
julia> A1 = triu(rand(256, 256)); A2 = rand(1024, 256); M = [A1; A2];
julia> T = zeros(32, 256); # inner block size 32
julia> NextLA.tsqrt!(A1, A2, T); # R in A1, reflectors in A2
julia> UpperTriangular(A1) ≈ qr(M).R
trueNextLA.tsqrt! — Method
tsqrt!(m, n, ib, A1, A2, T, tau, work)Compute the QR factorization of an (m+n)-by-n triangular-pentagonal matrix using the compact WY representation.
This routine computes the QR factorization of a triangular-pentagonal matrix: [ A1 ] [ A2 ] where A1 is n-by-n upper triangular and A2 is m-by-n general.
The factorization has the form: [ A1 ] = Q * [ R ] [ A2 ] [ 0 ] where Q is orthogonal and R is upper triangular.
Arguments
m: Number of rows of the pentagonal part A2n: Number of columns of the triangular-pentagonal matrixib: Block size for the compact WY representationA1: n×n upper triangular matrix (modified in-place)A2: m×n general matrix (modified in-place)T: ib×n matrix to store block reflector coefficientstau: Vector of length n to store reflector scalar factorswork: Workspace array of length ib×n
Algorithm
The algorithm proceeds in blocks of size ib:
- For each block, generate elementary reflectors to zero the pentagonal part
- Apply reflectors to remaining columns using efficient block updates
- Store reflector coefficients in compact WY form in matrix T
The compact WY representation allows for efficient application of the orthogonal factor Q using block operations.
Input Validation
m, n and ib must be non-negative; otherwise throws ArgumentError. Returns nothing.
Notes
This is a low-level computational routine typically called by higher-level QR factorization interfaces. The matrices A1, A2 are modified in-place to store the R factor and reflector vectors respectively.
NextLA.tsmqr! — Method
tsmqr!(side, trans, m1, n1, m2, n2, k, ib, A1, A2, V, T, work)Apply orthogonal matrix Q (or Q^H) stored as compact WY representation to a triangular-pentagonal matrix [A1; A2].
This routine applies a block orthogonal transformation represented in compact WY form (stored in V and T) to the combined matrix [A1; A2] where A1 is triangular and A2 is pentagonal.
Arguments
side: Character indicating side of multiplication- 'L': Apply Q from the left (Q[A1; A2] or Q^H[A1; A2])
- 'R': Apply Q from the right ([A1 A2]Q or [A1 A2]Q^H)
trans: Character indicating whether to transpose Q- 'N': Apply Q (no transpose)
- 'C': Apply Q^H (conjugate transpose)
- 'T': Apply Q^T (transpose, same as 'C' for complex)
m1,n1: Dimensions of triangular matrix A1m2,n2: Dimensions of pentagonal matrix A2k: Number of elementary reflectors (columns of V)ib: Block size for compact WY representationA1: Triangular part of the matrix (modified in-place)A2: Pentagonal part of the matrix (modified in-place)V: Matrix containing reflector vectorsT: Upper triangular block reflector coefficient matrixwork: Workspace array
Algorithm
The transformation Q is applied using the compact WY representation: Q = I - V * T * V^H
The algorithm processes the reflectors in blocks of size ib, applying each block using efficient matrix operations (parfb! routine).
Input Validation
Validates all dimension parameters and leading dimension requirements for proper matrix storage and computation.
Notes
This is a core computational routine for applying orthogonal transformations in blocked QR algorithms. The compact WY form enables efficient block updates.
NextLA.tsmqr! — Method
tsmqr!(side, trans, A1, A2, V, T)Apply orthogonal matrix Q (stored in compact WY form) to triangular-pentagonal matrices.
This is a high-level interface that automatically determines dimensions and allocates workspace for applying block orthogonal transformations to the combined matrix [A1; A2].
Arguments
side: Character indicating multiplication side- 'L': Apply Q from left (Q[A1; A2] or Q^H[A1; A2])
- 'R': Apply Q from right ([A1 A2]Q or [A1 A2]Q^H)
trans: Character indicating transpose operation- 'N': Apply Q (no transpose)
- 'C': Apply Q^H (conjugate transpose)
A1: Triangular part of matrix (modified in-place)A2: Pentagonal part of matrix (modified in-place)V: Matrix containing elementary reflector vectorsT: Upper triangular block reflector coefficient matrix. The block size isib = size(T, 1).
Returns
nothing.A1andA2are transformed in place.
Input Validation
- For side='L': n2 must equal n1 (same number of columns)
- For side='R': m2 must equal m1 (same number of rows)
- Block size ib = size(T, 1) must be positive
- Otherwise throws
ArgumentError
Algorithm
Uses blocked approach to apply the orthogonal transformation Q = I - VTV^H efficiently. The compact WY representation enables high-performance matrix-matrix operations instead of multiple vector operations.
Examples
julia> using LinearAlgebra
julia> A1 = triu(rand(256, 256)); A2 = rand(1024, 256); M = [A1; A2];
julia> T = zeros(32, 256); NextLA.tsqrt!(A1, A2, T);
julia> C1 = rand(256, 1024); C2 = rand(1024, 1024); C = [C1; C2];
julia> NextLA.tsmqr!('L', 'C', C1, C2, A2, T); # [C1; C2] := Qᴴ * [C1; C2]
julia> [C1; C2] ≈ qr(M).Q' * C
trueNextLA.ttqrt! — Method
ttqrt!(A, B, T_mat) -> nothingHelper for triangular-triangular QR factorization.
Arguments
A: Upper triangular matrix (m × n), only upper triangle is accessedB: Upper triangular matrix (m2 × n2), m2 and n2 must equal size of AT_mat: ib × nb block reflector matrix
Returns
- Modified
AandBmatrices in-place
Examples
julia> using LinearAlgebra
julia> A1 = triu(rand(256, 256)); A2 = triu(rand(256, 256)); M = [A1; A2];
julia> T = zeros(32, 256); # inner block size 32
julia> NextLA.ttqrt!(A1, A2, T); # R in A1, reflectors in A2
julia> UpperTriangular(A1) ≈ qr(M).R
trueNextLA.ttmqr! — Method
ttmqr!(side, trans, A1, A2, V, T)Helper function for triangular-trapezoidal matrix transformation.
Arguments
side: 'L' (left) or 'R' (right)trans: 'N' (no transpose) or 'C' (conjugate transpose)A1: Upper triangular matrix to be updatedA2: Trapezoidal matrix to be updatedV: Reflector vectors matrixT: Block reflector matrix. The block size isib = size(T, 1).
Returns
nothing.A1andA2are transformed in place.
Examples
julia> using LinearAlgebra
julia> A1 = triu(rand(256, 256)); A2 = triu(rand(256, 256)); M = [A1; A2];
julia> T = zeros(32, 256); NextLA.ttqrt!(A1, A2, T);
julia> C1 = rand(256, 1024); C2 = rand(256, 1024); C = [C1; C2];
julia> NextLA.ttmqr!('L', 'C', C1, C2, A2, T); # [C1; C2] := Qᴴ * [C1; C2]
julia> [C1; C2] ≈ qr(M).Q' * C
truePentagonal kernels
NextLA.parfb! — Method
parfb!(side, trans, direct, storev, m1, n1, m2, n2, k, l, A1, A2, V, T, work)Apply a block reflector or its transpose/adjoint to a general matrix using parallel algorithms.
This function applies the block reflector H or its transpose/adjoint to two matrix blocks simultaneously, making it efficient for parallel QR factorization algorithms. It performs operations of the form:
- C₁ := H^op · C₁ (left multiplication)
- C₁ := C₁ · H^op (right multiplication)
where H is represented in compact form by matrices V and T, and op can be N (no operation), T (transpose), or C (conjugate transpose).
Arguments
side::Char: Determines the side of multiplication ('L' for left, 'R' for right)trans::Char: Operation to apply ('N' for none, 'T' for transpose, 'C' for conjugate transpose)direct::Char: Direction of reflector storage ('F' for forward, 'B' for backward)storev::Char: Storage format of reflectors ('C' for columnwise, 'R' for rowwise)m1::Int: Number of rows in first matrix block A1n1::Int: Number of columns in first matrix block A1m2::Int: Number of rows in second matrix block A2n2::Int: Number of columns in second matrix block A2k::Int: Number of elementary reflectorsl::Int: Size of the triangular part of V: its lastlrows (storev = 'C') or columns (storev = 'R') are triangular. Use 0 when V is a full rectangle, as fromtsqrt!. Must satisfy 0 ≤ l ≤ k.A1::Matrix: First m1×n1 matrix block to be transformed (modified in-place)A2::Matrix: Second m2×n2 matrix block to be transformed (modified in-place)V::Matrix: Matrix containing elementary reflectors in compact formT::Matrix: Upper triangular factor matrixwork::Matrix: Workspace matrix, at least k×n2 (side = 'L') or m2×k (side = 'R')
Returns
nothing. There is no status code: bad input throwsArgumentError.
Algorithm
The function uses the compact WY representation where H = I - V·T·V^H, performing efficient block operations to apply the transformation to both matrix blocks simultaneously.
Implementation Notes
- Modifies A1 and A2 in-place for efficiency
- Uses optimized BLAS-3 operations for performance
- Handles different storage formats and operation types
- Validates all input parameters with descriptive error messages
Examples
julia> using LinearAlgebra
julia> A1 = triu(rand(256, 256)); A2 = rand(1024, 256); M = [A1; A2];
julia> T = zeros(256, 256); NextLA.tsqrt!(A1, A2, T); # one block: T is 256×256
julia> C1 = rand(256, 1024); C2 = rand(1024, 1024); C = [C1; C2]; W = zeros(256, 1024);
julia> NextLA.parfb!('L', 'C', 'F', 'C', 256, 1024, 1024, 1024, 256, 0, C1, C2, A2, T, W);
julia> [C1; C2] ≈ qr(M).Q' * C
trueNextLA.parfb! — Method
parfb!(side, trans, direct, storev, A1, A2, V, T)Applies a block reflector or its transpose to a pair of matrices A1 and A2. This is a simplified interface that takes k = size(T, 1) and l = size(V, 2), allocates the workspace and calls the long form.
Because it sets l = size(V, 2), it treats V as triangular (as from ttqrt!). It gives wrong results when V is a full rectangle, as from tsqrt!. For that case call the long form parfb!(side, trans, direct, storev, m1, n1, m2, n2, k, l, A1, A2, V, T, work) with l = 0.
Arguments
side::Char: Determines the side of multiplication ('L' for left, 'R' for right)trans::Char: Operation to apply ('N' for none, 'T' for transpose, 'C' for conjugate transpose)direct::Char: Direction of reflector storage ('F' for forward, 'B' for backward)storev::Char: Storage format of reflectors ('C' for columnwise, 'R' for rowwise)A1::Matrix: First matrix to be updated (modified in-place)A2::Matrix: Second matrix to be updated (modified in-place)V::Matrix: Matrix containing the elementary reflectorsT::Matrix: Upper triangular matrix of the block reflector
Returns
nothing.A1andA2are updated in place.
Examples
julia> using LinearAlgebra
julia> A1 = triu(rand(256, 256)); A2 = triu(rand(256, 256)); M = [A1; A2];
julia> T = zeros(256, 256); NextLA.ttqrt!(A1, A2, T); # one block; V = A2 is triangular
julia> C1 = rand(256, 1024); C2 = rand(256, 1024); C = [C1; C2];
julia> NextLA.parfb!('L', 'C', 'F', 'C', C1, C2, A2, T); # [C1; C2] := Qᴴ * [C1; C2]
julia> [C1; C2] ≈ qr(M).Q' * C
trueNextLA.pamm! — Method
pamm!(op, side, storev, direct, m, n, k, l, A1, A2, V, W)Parallel matrix multiplication kernel for block reflector applications.
This routine performs specialized matrix operations needed in blocked orthogonal factorizations. It computes either:
- W = A1 + op(V) * A2 (when op='W')
- A2 = A2 - op(V) * W (when op='A')
where op(V) is V, V^H, V^T depending on the storage and direction parameters.
Arguments
op: Operation type- 'W': Compute W = A1 + op(V) * A2 or W = A1 + A2 * op(V)
- 'A': Update A2 = A2 - op(V) * W or A2 = A2 - W * op(V)
side: Which side V is applied- 'L': Left multiplication (op(V) * A2)
- 'R': Right multiplication (A2 * op(V))
storev: How reflector vectors are stored in V- 'C': Columnwise storage
- 'R': Rowwise storage
direct: Direction of reflector product- 'F': Forward (H = H₁H₂...Hₖ)
- 'B': Backward (H = HₖHₖ₋₁...H₁)
m,n: Dimensions of matrices A1, A2, Wk: Number of elementary reflectorsl: Number of columns/rows in triangular part of VA1: First input matrixA2: Second input/output matrixV: Matrix containing reflector vectorsW: Workspace/output matrix
Algorithm
The routine handles all combinations of storage formats and application sides efficiently by dispatching to specialized kernels. Each kernel exploits the structure of the reflector matrix V (triangular + rectangular parts) to minimize computational cost.
Input Validation
All parameters are validated for correctness. Dimensions must be non-negative and leading dimensions must meet minimum requirements.
Notes
This is a low-level computational kernel used internally by blocked QR and LQ factorization routines. It is optimized for performance with specific memory access patterns.
Examples
julia> A1 = rand(256, 1024); A2 = rand(1024, 1024); V = rand(1024, 256); W = zeros(256, 1024);
julia> NextLA.pamm!('W', 'L', 'C', 'F', 1024, 1024, 256, 0, A1, A2, V, W); # W := A1 + Vᴴ * A2
julia> W ≈ A1 + V' * A2
trueNextLA.pemv! — Method
pemv!(trans, storev, m, n, l, alpha, A, X, beta, Y, work)Perform panel matrix-vector multiplication with optimized block algorithms.
This function implements efficient matrix-vector multiplication for structured panels, commonly used in block QR factorization algorithms. It performs operations of the form: Y := alpha * op(A) * X + beta * Y
where op(A) can be A, A^T, or A^H depending on the trans parameter, and A is a pentagonal panel: for storev = 'C' its last l rows are upper triangular, and for storev = 'R' its last l columns are lower triangular. Entries outside the triangle in that part are not read.
Only storev = 'C' with trans = 'T' or 'C', and storev = 'R' with trans = 'N', are accepted. Other combinations throw ArgumentError.
Arguments
trans::Char: Transpose operation ('N' for none, 'T' for transpose, 'C' for conjugate transpose)storev::Char: Storage format for vectors ('C' for columnwise, 'R' for rowwise)m::Int: Number of rows in matrix An::Int: Number of columns in matrix Al::Int: Panel size (must be ≤ min(m,n))alpha: Scalar multiplier for the matrix-vector productA::Matrix: Input matrix of size m×nX::Vector: Input vector (modified in-place)beta: Scalar multiplier for the output vector YY::Vector: Output vector (modified in-place)work::Vector: Workspace array for intermediate computations
Returns
- No status code; the result is in
Y, and the return value is not meaningful. Bad input throwsArgumentError.
Algorithm
The function uses block-structured algorithms that partition the matrix and vectors to take advantage of cache locality and vectorization, particularly effective for panel-based factorizations.
Implementation Notes
- Optimized for different storage formats (columnwise vs rowwise)
- Uses BLAS-3 operations where possible for performance
- Handles edge cases with l=1 efficiently
- Validates input parameters with descriptive error messages
Examples
julia> using LinearAlgebra
julia> A = rand(1024, 256); x = rand(1024); y = rand(256); y0 = copy(y); work = zeros(1024);
julia> NextLA.pemv!('C', 'C', 1024, 256, 256, 2.0, A, x, 0.5, y, work); # last 256 rows: upper triangle only
julia> P = [A[1:768, :]; triu(A[769:1024, :])];
julia> y ≈ 2.0 * P' * x + 0.5 * y0
trueNextLA.pemv! — Method
pemv!(trans, storev, alpha, A, x, beta, y)Performs panel matrix-vector multiplication with automatic workspace allocation. This is a simplified interface that takes m, n = size(A) and l = min(m, n), allocates the workspace and calls the long form.
Arguments
trans::Char: Transpose operation ('N' for none, 'T' for transpose, 'C' for conjugate transpose)storev::Char: Storage format for vectors ('C' for columnwise, 'R' for rowwise)alpha: Scalar multiplier for op(A)*x (required; no default)A::Matrix: Matrix for multiplicationx::Vector: Input vectorbeta: Scalar multiplier for y (required; no default)y::Vector: Output vector (modified in-place)
Returns
- The result is in
y; the return value is not meaningful.
Examples
julia> using LinearAlgebra
julia> A = rand(1024, 256); x = rand(1024); y = rand(256); y0 = copy(y);
julia> NextLA.pemv!('C', 'C', 2.0, A, x, 0.5, y); # l = 256: last 256 rows, upper triangle only
julia> P = [A[1:768, :]; triu(A[769:1024, :])];
julia> y ≈ 2.0 * P' * x + 0.5 * y0
trueUsage examples
Each example stands on its own and ends with a check that prints true.
Least squares with a blocked QR: geqrt! factors A, unmqr! applies Qᴴ to the right-hand side, and a triangular solve with R finishes:
using NextLA, LinearAlgebra
m, n, ib = 2048, 1024, 64
A = rand(m, n)
b = rand(m)
F = copy(A)
T = zeros(ib, n)
NextLA.geqrt!(F, T) # R in triu(F), reflectors below it
c = reshape(copy(b), m, 1)
NextLA.unmqr!('L', 'C', F, T, c) # c := Qᴴ * b
x = c[1:n, :]
trsm!('L', 'U', 'N', 'N', 1.0, F[1:n, :], x) # solve R x = Qᴴ b
norm(A' * (A * x - b)) / norm(A' * b) < 1e-10 # least-squares condition
# output
trueOne blocked QR step built from the Householder kernels: geqr2! factors a 128-column panel, larft! forms its block reflector and larfb! applies it to the trailing columns:
using NextLA, LinearAlgebra
n, nb = 1024, 128
A0 = rand(n, n)
A = copy(A0)
panel, rest = view(A, :, 1:nb), view(A, :, nb+1:n)
tau = zeros(nb)
NextLA.geqr2!(panel, tau) # unblocked QR of the panel
T = zeros(nb, nb)
NextLA.larft!('F', 'C', panel, tau, T) # H = I - V * T * Vᴴ
NextLA.larfb!('L', 'C', 'F', 'C', panel, T, rest) # rest := Hᴴ * rest
triu(A[1:nb, :]) ≈ qr(A0).R[1:nb, :] # the first 128 rows of R
# output
trueTile QR of a 1024 by 256 panel stored as four 256 by 256 tiles (a flat tree): geqrt! factors the top tile and tsqrt! folds each tile below into its R; then unmqr! and tsmqr! apply Qᴴ to other columns in the same order. R and the top rows of Qᴴ C match LinearAlgebra.qr up to the signs of the rows:
using NextLA, LinearAlgebra
nb, ib = 256, 32 # tile size, inner block size
P = rand(4nb, nb) # the panel
C = rand(4nb, 1024) # columns to update with Qᴴ
A, B = copy(P), copy(C)
tile(X, i) = view(X, (i-1)*nb+1:i*nb, :)
Ts = [zeros(ib, nb) for _ in 1:4]
NextLA.geqrt!(tile(A, 1), Ts[1])
for i in 2:4
NextLA.tsqrt!(tile(A, 1), tile(A, i), Ts[i])
end
NextLA.unmqr!('L', 'C', tile(A, 1), Ts[1], tile(B, 1))
for i in 2:4
NextLA.tsmqr!('L', 'C', tile(B, 1), tile(B, i), tile(A, i), Ts[i])
end
F = qr(P)
abs.(triu(tile(A, 1))) ≈ abs.(F.R) && abs.(tile(B, 1)) ≈ abs.((F.Q' * C)[1:nb, :])
# output
trueThe binary-tree variant on two tiles: each tile is factored with geqrt!, then ttqrt! merges the two triangles and ttmqr! applies the merge:
using NextLA, LinearAlgebra
nb, ib = 256, 32
P = rand(2nb, nb)
C = rand(2nb, 1024)
A, B = copy(P), copy(C)
top, bot = 1:nb, nb+1:2nb
T1, T2, T12 = zeros(ib, nb), zeros(ib, nb), zeros(ib, nb)
NextLA.geqrt!(view(A, top, :), T1)
NextLA.geqrt!(view(A, bot, :), T2)
R2 = triu(A[bot, :]) # ttqrt! needs zeros below the second triangle
NextLA.ttqrt!(view(A, top, :), R2, T12)
NextLA.unmqr!('L', 'C', view(A, top, :), T1, view(B, top, :))
NextLA.unmqr!('L', 'C', view(A, bot, :), T2, view(B, bot, :))
NextLA.ttmqr!('L', 'C', view(B, top, :), view(B, bot, :), R2, T12)
F = qr(P)
abs.(triu(A[top, :])) ≈ abs.(F.R) && abs.(B[top, :]) ≈ abs.((F.Q' * C)[1:nb, :])
# output
trueThe kernels are KernelAbstractions code and take device arrays directly. This block is not run when the documentation is built:
using NextLA, CUDA, LinearAlgebra
A = rand(1024, 1024)
C = rand(1024, 1024)
dF, dT, dC = CuArray(A), CUDA.zeros(Float64, 64, 1024), CuArray(C)
NextLA.geqrt!(dF, dT)
NextLA.unmqr!('L', 'C', dF, dT, dC) # dC := Qᴴ * C on the GPU
Array(dC) ≈ qr(A).Q' * C