### 18.3 Matrix Factorizations

— Loadable Function: R = chol (A)
— Loadable Function: [R, p] = chol (A)
— Loadable Function: [R, p, Q] = chol (S)
— Loadable Function: [R, p, Q] = chol (S, 'vector')
— Loadable Function: [L, ...] = chol (..., 'lower')

> Compute the Cholesky factor, R, of the symmetric positive definite matrix A, where

>               R' * R = A.

> Called with one output argument `chol` fails if A or S is not positive definite. With two or more output arguments p flags whether the matrix was positive definite and `chol` does not fail. A zero value indicated that the matrix was positive definite and the R gives the factorization, and p will have a positive value otherwise.

> If called with 3 outputs then a sparsity preserving row/column permutation is applied to A prior to the factorization. That is R is the factorization of A`(`Q`,`Q`)` such that

>               R' * R = Q' * A * Q.

> The sparsity preserving permutation is generally returned as a matrix. However, given the flag 'vector', Q will be returned as a vector such that

>               R' * R = A(Q, Q).

> Called with either a sparse or full matrix and using the 'lower' flag, `chol` returns the lower triangular factorization such that

>               L * L' = A.

> In general the lower triangular factorization is significantly faster for sparse matrices.

> See also: cholinv, chol2inv.

— Loadable Function: cholinv (A)

> Use the Cholesky factorization to compute the inverse of the symmetric positive definite matrix A.

> See also: chol, chol2inv, inv.

— Loadable Function: chol2inv (U)

> Invert a symmetric, positive definite square matrix from its Cholesky decomposition, U. Note that U should be an upper-triangular matrix with positive diagonal elements. `chol2inv (`U`)` provides `inv (`U`'*`U`)` but it is much faster than using `inv`.

> See also: chol, cholinv, inv.

— Loadable Function: [R1, info] = cholupdate (R, u, op)

> Update or downdate a Cholesky factorization. Given an upper triangular matrix R and a column vector u, attempt to determine another upper triangular matrix R1 such that

> - R1'*R1 = R'*R + u*u' if op is "+"
> - R1'*R1 = R'*R - u*u' if op is "-"

> If op is "-", info is set to

> - 0 if the downdate was successful,
> - 1 if R'*R - u*u' is not positive definite,
> - 2 if R is singular.

> If info is not present, an error message is printed in cases 1 and 2.

> See also: chol, qrupdate.

— Loadable Function: R1 = cholinsert (R, j, u)
— Loadable Function: [R1, info] = cholinsert (R, j, u)

> Given a Cholesky factorization of a real symmetric or complex Hermitian positive definite matrix A = R'*R, R upper triangular, return the Cholesky factorization of A1, where A1(p,p) = A, A1(:,j) = A1(j,:)' = u and p = [1:j-1,j+1:n+1]. u(j) should be positive. On return, info is set to

> - 0 if the insertion was successful,
> - 1 if A1 is not positive definite,
> - 2 if R is singular.

> If info is not present, an error message is printed in cases 1 and 2.

> See also: chol, cholupdate, choldelete.

— Loadable Function: R1 = choldelete (R, j)

> Given a Cholesky factorization of a real symmetric or complex Hermitian positive definite matrix A = R'*R, R upper triangular, return the Cholesky factorization of A(p,p), where p = [1:j-1,j+1:n+1].

> See also: chol, cholupdate, cholinsert.

— Loadable Function: R1 = cholshift (R, i, j)

> Given a Cholesky factorization of a real symmetric or complex Hermitian positive definite matrix A = R'*R, R upper triangular, return the Cholesky factorization of A(p,p), where p is the permutation
> `p = [1:i-1, shift(i:j, 1), j+1:n]` if i < j
> or
> `p = [1:j-1, shift(j:i,-1), i+1:n]` if j < i.

> See also: chol, cholinsert, choldelete.

— Loadable Function: H = hess (A)
— Loadable Function: [P, H] = hess (A)

> Compute the Hessenberg decomposition of the matrix A.

> The Hessenberg decomposition is P`*` H`*` P`' =` A where P is a square unitary matrix (P`' *` P`= I`, using complex-conjugate transposition) and H is upper Hessenberg (H`(i, j) = 0 forall i >= j+1)`.

> The Hessenberg decomposition is usually used as the first step in an eigenvalue computation, but has other applications as well (see Golub, Nash, and Van Loan, IEEE Transactions on Automatic Control, 1979).

— Loadable Function: [L, U] = lu (A)
— Loadable Function: [L, U, P] = lu (A)
— Loadable Function: [L, U, P, Q] = lu (S)
— Loadable Function: [L, U, P, Q, R] = lu (S)
— Loadable Function: [...] = lu (S, thres)
— Loadable Function: y = lu (...)
— Loadable Function: [...] = lu (..., 'vector')

> Compute the LU decomposition of A. If A is full subroutines from lapack are used and if A is sparse then umfpack is used. The result is returned in a permuted form, according to the optional return value P. For example, given the matrix `a = [1, 2; 3, 4]`,

>               [l, u, p] = lu (a)

> returns

>               l =

>                 1.00000  0.00000
>                 0.33333  1.00000

>               u =

>                 3.00000  4.00000
>                 0.00000  0.66667

>               p =

>                 0  1
>                 1  0

> The matrix is not required to be square.

> When called with two or three output arguments and a spare input matrix, `lu` does not attempt to perform sparsity preserving column permutations. Called with a fourth output argument, the sparsity preserving column transformation Q is returned, such that P`*` A`*` Q`=` L`*` U.

> Called with a fifth output argument and a sparse input matrix, `lu` attempts to use a scaling factor R on the input matrix such that P`* (`R`\` A`) *` Q`=` L`*` U. This typically leads to a sparser and more stable factorization.

> An additional input argument thres, that defines the pivoting threshold can be given. thres can be a scalar, in which case it defines the umfpack pivoting tolerance for both symmetric and unsymmetric cases. If thres is a 2-element vector, then the first element defines the pivoting tolerance for the unsymmetric umfpack pivoting strategy and the second for the symmetric strategy. By default, the values defined by `spparms` are used ([0.1, 0.001]).

> Given the string argument 'vector', `lu` returns the values of P and Q as vector values, such that for full matrix, A`(`P`,:) =` L`*` U, and R`(`P`,:) *` A`(:,` Q`) =` L`*` U.

> With two output arguments, returns the permuted forms of the upper and lower triangular matrices, such that A`=` L`*` U. With one output argument y, then the matrix returned by the lapack routines is returned. If the input matrix is sparse then the matrix L is embedded into U to give a return value similar to the full case. For both full and sparse matrices, `lu` loses the permutation information.

— Loadable Function: [L, U] = luupdate (L, U, x, y)
— Loadable Function: [L, U, P] = luupdate (L, U, P, x, y)

> Given an LU factorization of a real or complex matrix A = L*U, L lower unit trapezoidal and U upper trapezoidal, return the LU factorization of A + x*y.', where x and y are column vectors (rank-1 update) or matrices with equal number of columns (rank-k update). Optionally, row-pivoted updating can be used by supplying a row permutation (pivoting) matrix P; in that case, an updated permutation matrix is returned. Note that if L, U, P is a pivoted LU factorization as obtained by `lu`:

>                 [L, U, P] = lu (A);

> then a factorization of A`+`x`*`y`.'` can be obtained either as

>                 [L1, U1] = lu (L, U, P*x, y)

> or

>                 [L1, U1, P1] = lu (L, U, P, x, y)

> The first form uses the unpivoted algorithm, which is faster, but less stable. The second form uses a slower pivoted algorithm, which is more stable.

> The matrix case is done as a sequence of rank-1 updates; thus, for large enough k, it will be both faster and more accurate to recompute the factorization from scratch.

> See also: lu, qrupdate, cholupdate.

— Loadable Function: [Q, R, P] = qr (A)
— Loadable Function: [Q, R, P] = qr (A, '0')

> Compute the QR factorization of A, using standard lapack subroutines. For example, given the matrix A`= [1, 2; 3, 4]`,

>               [Q, R] = qr (A)

> returns

>               Q =

>                 -0.31623  -0.94868
>                 -0.94868   0.31623

>               R =

>                 -3.16228  -4.42719
>                  0.00000  -0.63246

> The `qr` factorization has applications in the solution of least squares problems

>               min norm(A x - b)

> for overdetermined systems of equations (i.e., A is a tall, thin matrix). The QR factorization is Q`*` Q`=` A where Q is an orthogonal matrix and R is upper triangular.

> If given a second argument of '0', `qr` returns an economy-sized QR factorization, omitting zero rows of R and the corresponding columns of Q.

> If the matrix A is full, the permuted QR factorization `[`Q`,` R`,` P`] = qr (`A`)` forms the QR factorization such that the diagonal entries of R are decreasing in magnitude order. For example, given the matrix `a = [1, 2; 3, 4]`,

>               [Q, R, P] = qr (A)

> returns

>               Q =

>                 -0.44721  -0.89443
>                 -0.89443   0.44721

>               R =

>                 -4.47214  -3.13050
>                  0.00000   0.44721

>               P =

>                  0  1
>                  1  0

> The permuted `qr` factorization `[`Q`,` R`,` P`] = qr (`A`)` factorization allows the construction of an orthogonal basis of `span (A)`.

> If the matrix A is sparse, then compute the sparse QR factorization of A, using CSparse. As the matrix Q is in general a full matrix, this function returns the Q-less factorization R of A, such that R`= chol (`A`' *` A`)`.

> If the final argument is the scalar `0` and the number of rows is larger than the number of columns, then an economy factorization is returned. That is R will have only `size (`A`,1)` rows.

> If an additional matrix B is supplied, then `qr` returns C, where C`=` Q`' *` B. This allows the least squares approximation of A`\` B to be calculated as

>               [C,R] = spqr (A,B)
>               x = R \ C

— Loadable Function: [Q1, R1] = qrupdate (Q, R, u, v)

> Given a QR factorization of a real or complex matrix A = Q*R, Q unitary and R upper trapezoidal, return the QR factorization of A + u*v', where u and v are column vectors (rank-1 update) or matrices with equal number of columns (rank-k update). Notice that the latter case is done as a sequence of rank-1 updates; thus, for k large enough, it will be both faster and more accurate to recompute the factorization from scratch.

> The QR factorization supplied may be either full (Q is square) or economized (R is square).

> See also: qr, qrinsert, qrdelete.

— Loadable Function: [Q1, R1] = qrinsert (Q, R, j, x, orient)

> Given a QR factorization of a real or complex matrix A = Q*R, Q unitary and R upper trapezoidal, return the QR factorization of [A(:,1:j-1) x A(:,j:n)], where u is a column vector to be inserted into A (if orient is `"col"`), or the QR factorization of [A(1:j-1,:);x;A(:,j:n)], where x is a row vector to be inserted into A (if orient is `"row"`).

> The default value of orient is `"col"`. If orient is `"col"`, u may be a matrix and j an index vector resulting in the QR factorization of a matrix B such that B(:,j) gives u and B(:,j) = [] gives A. Notice that the latter case is done as a sequence of k insertions; thus, for k large enough, it will be both faster and more accurate to recompute the factorization from scratch.

> If orient is `"col"`, the QR factorization supplied may be either full (Q is square) or economized (R is square).

> If orient is `"row"`, full factorization is needed.

> See also: qr, qrupdate, qrdelete.

— Loadable Function: [Q1, R1] = qrdelete (Q, R, j, orient)

> Given a QR factorization of a real or complex matrix A = Q*R, Q unitary and R upper trapezoidal, return the QR factorization of [A(:,1:j-1) A(:,j+1:n)], i.e., A with one column deleted (if orient is "col"), or the QR factorization of [A(1:j-1,:);A(:,j+1:n)], i.e., A with one row deleted (if orient is "row").

> The default value of orient is "col".

> If orient is `"col"`, j may be an index vector resulting in the QR factorization of a matrix B such that A(:,j) = [] gives B. Notice that the latter case is done as a sequence of k deletions; thus, for k large enough, it will be both faster and more accurate to recompute the factorization from scratch.

> If orient is `"col"`, the QR factorization supplied may be either full (Q is square) or economized (R is square).

> If orient is `"row"`, full factorization is needed.

> See also: qr, qrinsert, qrupdate.

— Loadable Function: [Q1, R1] = qrshift (Q, R, i, j)

> Given a QR factorization of a real or complex matrix A = Q*R, Q unitary and R upper trapezoidal, return the QR factorization of A(:,p), where p is the permutation
> `p = [1:i-1, shift(i:j, 1), j+1:n]` if i < j
> or
> `p = [1:j-1, shift(j:i,-1), i+1:n]` if j < i.

> See also: qr, qrinsert, qrdelete.

— Loadable Function: lambda = qz (A, B)
— Loadable Function: lambda = qz (A, B, opt)

> QZ decomposition of the generalized eigenvalue problem (A x = s B x). There are three ways to call this function:

> 1. lambda`= qz (`A`,` B`)`

>    Computes the generalized eigenvalues lambda of (A - s B).

> 2. `[AA, BB, Q, Z, V, W,` lambda`] = qz (`A`,` B`)`

>    Computes QZ decomposition, generalized eigenvectors, and generalized eigenvalues of (A - s B)

>                           A * V = B * V * diag (lambda)
>                           W' * A = diag (lambda) * W' * B
>                           AA = Q * A * Z, BB = Q * B * Z

>    with Q and Z orthogonal (unitary)= I

> 3. `[AA,BB,Z{,` lambda`}] = qz (`A`,` B`,` opt`)`

>    As in form [2], but allows ordering of generalized eigenpairs for (e.g.) solution of discrete time algebraic Riccati equations. Form 3 is not available for complex matrices, and does not compute the generalized eigenvectors V, W, nor the orthogonal matrix Q.

>    opt
>      for ordering eigenvalues of the GEP pencil. The leading block of the revised pencil contains all eigenvalues that satisfy:
>      "N"
>        = unordered (default)
>      "S"
>        = small: leading block has all |lambda| ≤ 1
>      "B"
>        = big: leading block has all |lambda| ≥ 1
>      "-"
>        = negative real part: leading block has all eigenvalues in the open left half-plane
>      "+"
>        = non-negative real part: leading block has all eigenvalues in the closed right half-plane

> Note: `qz` performs permutation balancing, but not scaling (see doc-balance). The order of output arguments was selected for compatibility with matlab.

> See also: balance, eig, schur.

— Function File: [aa, bb, q, z] = qzhess (A, B)

> Compute the Hessenberg-triangular decomposition of the matrix pencil `(`A`,` B`)`, returning aa`=` q`*` A`*` z, bb`=` q`*` B`*` z, with q and z orthogonal. For example:

>               [aa, bb, q, z] = qzhess ([1, 2; 3, 4], [5, 6; 7, 8])
>                    ⇒ aa = [ -3.02244, -4.41741;  0.92998,  0.69749 ]
>                    ⇒ bb = [ -8.60233, -9.99730;  0.00000, -0.23250 ]
>                    ⇒  q = [ -0.58124, -0.81373; -0.81373,  0.58124 ]
>                    ⇒  z = [ 1, 0; 0, 1 ]

> The Hessenberg-triangular decomposition is the first step in Moler and Stewart's QZ decomposition algorithm.

> Algorithm taken from Golub and Van Loan, Matrix Computations, 2nd edition.

— Loadable Function: S = schur (A)
— Loadable Function: S = schur (A, "real")
— Loadable Function: S = schur (A, "complex")
— Loadable Function: S = schur (A, opt)
— Loadable Function: [U, S] = schur (A, ...)

> Compute the Schur decomposition of A

>               S = U' * A * U

> where U is a unitary matrix (U`'*` U is identity) and S is upper triangular. The eigenvalues of A (and S) are the diagonal elements of S. If the matrix A is real, then the real Schur decomposition is computed, in which the matrix U is orthogonal and S is block upper triangular with blocks of size at most `2 x 2` along the diagonal. The diagonal elements of S (or the eigenvalues of the `2 x 2` blocks, when appropriate) are the eigenvalues of A and S.

> The default for real matrices is a real Schur decomposition. A complex decomposition may be forced by passing the flag "complex".

> The eigenvalues are optionally ordered along the diagonal according to the value of opt. opt`= "a"` indicates that all eigenvalues with negative real parts should be moved to the leading block of S (used in `are`), opt`= "d"` indicates that all eigenvalues with magnitude less than one should be moved to the leading block of S (used in `dare`), and opt`= "u"`, the default, indicates that no ordering of eigenvalues should occur. The leading k columns of U always span the A-invariant subspace corresponding to the k leading eigenvalues of S.

> The Schur decomposition is used to compute eigenvalues of a square matrix, and has applications in the solution of algebraic Riccati equations in control (see `are` and `dare`).

> See also: rsf2csf.

— Function File: [U, T] = rsf2csf (UR, TR)

> Convert a real, upper quasi-triangular Schur form TR to a complex, upper triangular Schur form T.

> Note that the following relations hold:

> UR`*` TR`*` UR`' =` U`*` T`*` U`'` and U`' *` U is the identity matrix I.

> Note also that U and T are not unique.

> See also: schur.

— Function File: angle = subspace (A, B)

> Determine the largest principal angle between two subspaces spanned by the columns of matrices A and B.

— Loadable Function: s = svd (A)
— Loadable Function: [U, S, V] = svd (A)
— Loadable Function: [U, S, V] = svd (A, econ)

> Compute the singular value decomposition of A

>               A = U*S*V'

> The function `svd` normally returns only the vector of singular values. When called with three return values, it computes U, S, and V. For example,

>               svd (hilb (3))

> returns

>               ans =

>                 1.4083189
>                 0.1223271
>                 0.0026873

> and

>               [u, s, v] = svd (hilb (3))

> returns

>               u =

>                 -0.82704   0.54745   0.12766
>                 -0.45986  -0.52829  -0.71375
>                 -0.32330  -0.64901   0.68867

>               s =

>                 1.40832  0.00000  0.00000
>                 0.00000  0.12233  0.00000
>                 0.00000  0.00000  0.00269

>               v =

>                 -0.82704   0.54745   0.12766
>                 -0.45986  -0.52829  -0.71375
>                 -0.32330  -0.64901   0.68867

> If given a second argument, `svd` returns an economy-sized decomposition, eliminating the unnecessary rows or columns of U or V.

> See also: svd_driver, svds, eig.

— Loadable Function: val = svd_driver ()
— Loadable Function: old_val = svd_driver (new_val)

> Query or set the underlying lapack driver used by `svd`. Currently recognized values are "gesvd" and "gesdd". The default is "gesvd".

> See also: svd.

— Function File: [housv, beta, zer] = housh (x, j, z)

> Compute Householder reflection vector housv to reflect x to be the j-th column of identity, i.e.,

>               (I - beta*housv*housv')x =  norm(x)*e(j) if x(1) < 0,
>               (I - beta*housv*housv')x = -norm(x)*e(j) if x(1) >= 0

> Inputs

> x
>   vector
> j
>   index into vector
> z
>   threshold for zero (usually should be the number 0)

> Outputs (see Golub and Van Loan):

> beta
>   If beta = 0, then no reflection need be applied (zer set to 0)
> housv
>   householder vector

— Function File: [u, h, nu] = krylov (A, V, k, eps1, pflg)

> Construct an orthogonal basis u of block Krylov subspace

>               [v a*v a^2*v ... a^(k+1)*v]

> Using Householder reflections to guard against loss of orthogonality.

> If V is a vector, then h contains the Hessenberg matrix such that `a*u == u*h+rk*ek'`, in which `rk = a*u(:,k)-u*h(:,k)`, and `ek'` is the vector `[0, 0, ..., 1]` of length `k`. Otherwise, h is meaningless.

> If V is a vector and k is greater than `length(A)-1`, then h contains the Hessenberg matrix such that `a*u == u*h`.

> The value of nu is the dimension of the span of the Krylov subspace (based on eps1).

> If b is a vector and k is greater than m-1, then h contains the Hessenberg decomposition of A.

> The optional parameter eps1 is the threshold for zero. The default value is 1e-12.

> If the optional parameter pflg is nonzero, row pivoting is used to improve numerical behavior. The default value is 0.

> Reference: A. Hodel, P. Misra, Partial Pivoting in the Computation of Krylov Subspaces of Large Sparse Systems, Proceedings of the 42nd IEEE Conference on Decision and Control, December 2003.