fortran-lapack
This package provides precision-agnostic, high-level linear algebra APIs for real and complex arguments in Modern Fortran. The APIs are similar to NumPy/SciPy operations, and leverage a Modern Fortran implementation of the Reference-LAPACK library.
A full and standardized implementation of the present library has been integrated into the Fortran Standard Library, and as such, most users should seek to access the functionality from stdlib. The present library is kept in place for those who seek a compact implementation of it.
Precision kinds and fpm features
sp (32-bit) and dp (64-bit) are always built. The wider kinds are fpm features, so a build pays
only for the precisions it asks for:
| feature |
kind |
BLAS/LAPACK initials |
in the default profile |
quad |
qp, 128-bit |
q real, w complex |
yes |
xdp |
xdp, 80-bit extended |
x real, y complex |
no |
quad is part of the default profile, so fpm build, --profile debug and --profile release
carry sp, dp and qp, as every earlier release did. The named profiles select the other
combinations:
fpm test # sp, dp, qp
fpm test --profile lean # sp and dp only, a third fewer source lines
fpm test --profile allkinds # sp, dp, xdp, qp -- x86_64 only
fpm test --profile external # sp, dp, qp, against the reference BLAS and LAPACK
80-bit extended precision is a property of x86 hardware rather than of the language: on aarch64
selected_real_kind(18) returns the 128-bit kind, so a build that asks for xdp there stops in
la_constants instead of compiling a second copy of the quadruple-precision code.
From a consumer’s manifest:
[dependencies]
# default: sp, dp, qp -- nothing to write
fortran-lapack = { git = "https://github.com/perazz/fortran-lapack" }
# pin the 2.0.0 release
fortran-lapack = { git = "https://github.com/perazz/fortran-lapack", tag = "2.0.0" }
# sp and dp only
fortran-lapack = { git = "https://github.com/perazz/fortran-lapack", profile = "lean" }
# add 80-bit extended precision
fortran-lapack = { git = "https://github.com/perazz/fortran-lapack", features = ["quad", "xdp"] }
features = [...] replaces the default set instead of adding to it, so a consumer that wants xdp
as well as qp lists both.
la_constants declares sp, dp, xdp and qp whether or not the build carries them: a kind
left out is -1, so use la_constants, only: qp keeps compiling. The logical parameters
la_with_qp and la_with_xdp say which of the two are real.
Outside fpm, the same choice is made with the preprocessor: the generated sources under src/ are
.F90, the tests under test/ carry cpp directives too, so the whole tree wants -cpp, with
-DLA_WITH_QP for quadruple precision and -DLA_WITH_XDP for extended precision.
Browse API
All procedures work with all types (real, complex) and kinds (32, 64, 128-bit floats, and the
80-bit extended floats of the xdp feature).
The subroutines are pure and take an optional err argument: without it, an error stops execution.
A function that can fail has two forms: without err it is pure, takes its matrices as
intent(in), and stops with error stop on an error; with err it returns the error state instead,
and is not pure, since a pure function cannot have an intent(out) argument. An err that is
itself an absent optional argument cannot be passed to the second form. The operators .inv.,
.det. and .pinv. are pure forms, and stop on an error.
[chol](@ref la_cholesky::chol) - Cholesky factorization of a matrix (function).
Syntax
c = chol(a [, lower] [, other_zeroed])
c = chol(a [, lower] [, other_zeroed], err)
Description
This function computes the Cholesky factorization of a real symmetric or complex Hermitian matrix \f$ A \f$:
\f[
A = L L^T = U^T U
\f]
where \f$ L \f$ is a lower triangular matrix and \f$ U \f$ is an upper triangular matrix.
The function returns the factorized matrix as a new allocation, without modifying the input matrix.
Without err it is pure; with err it returns errors instead of stopping, and is not pure.
Arguments
a: A real or complex matrix of size \f$ [n,n] \f$, representing the symmetric/Hermitian input matrix.
lower (optional): A logical flag indicating whether the lower (\f$ L \f$) or upper (\f$ U \f$) triangular factor should be computed. Defaults to lower = .true..
other_zeroed (optional): A logical flag determining whether the unused half of the returned matrix should be explicitly zeroed. Defaults to other_zeroed = .true..
err (optional): A [type(la_state)](@ref la_state_type::la_state) variable that receives the error state.
Return value
c: A real or complex matrix of size \f$ [n,n] \f$, containing the Cholesky factors. The returned matrix is triangular (upper or lower, as selected).
Errors
- Raises [LINALG_VALUE_ERROR](@ref la_state_type::linalg_value_error) if the input matrix ihas invalid size.
- Raises [LINALG_ERROR](@ref la_state_type::linalg_error) if numerical instability prevents factorization.
- If
err is not present, exceptions trigger an error stop.
Notes
- The function is based on LAPACK’s [POTRF](@ref la_lapack::potrf) routines.
- This function allocates a new matrix to store the factorization. For an in-place version, use [cholesky](@ref la_cholesky::cholesky).
[cholesky](@ref la_cholesky::cholesky) - Cholesky factorization of a matrix (subroutine).
Syntax
call cholesky(a [, c] [, lower] [, other_zeroed])
Description
This subroutine computes the Cholesky factorization of a real symmetric or complex Hermitian matrix \f$ A \f$:
\f[
A = L L^T = U^T U
\f]
where \f$ L \f$ is a lower triangular matrix and \f$ U \f$ is an upper triangular matrix. The factorization is performed in-place, modifying the input matrix a,
or on a pre-allocated matrix c with the same type and kind as a.
Arguments
a: A real or complex matrix of size \f$ [n,n] \f$, representing the symmetric/Hermitian input matrix. if c is not provided, on return it contains the Cholesky factorization.
c (optional): A matrix of size \f$ [n,n] \f$, of the same type and kind as a, containing the Cholesky factorization. If provided, a is unchanged.
lower (optional): A logical flag indicating whether the lower (\f$ L \f$) or upper (\f$ U \f$) triangular factor should be computed. Defaults to lower = .true..
other_zeroed (optional): A logical flag determining whether the unused half of the matrix should be explicitly zeroed. Defaults to other_zeroed = .true..
Errors
- Raises [LINALG_VALUE_ERROR](@ref la_state_type::linalg_value_error) if the input matrix is not positive definite.
- Raises [LINALG_ERROR](@ref la_state_type::linalg_error) if numerical instability prevents factorization.
- If error handling is not provided, exceptions will trigger an
error stop.
Notes
- The returned Cholesky factorization matrix is triangular (upper or lower, as selected).
- The subroutine is based on LAPACK’s [POTRF](@ref la_lapack::potrf) routines.
- This subroutine modifies the input matrix in-place. For a version that returns a newly allocated matrix, use [chol](@ref la_cholesky::chol).
[eig](@ref la_eig::eig) - Eigendecomposition of a square matrix.
Syntax
call eig(a [, b] [, lambda] [, right] [, left] [, overwrite_a] [, overwrite_b] [, err])
Description
This interface provides methods for computing the eigenvalues and eigenvectors of a real or complex matrix.
It supports both standard and generalized eigenvalue problems, allowing for the decomposition of a matrix A alone or a pair of matrices (A, B) in the generalized case.
Given a square matrix \f$ A \f$, this routine computes its eigenvalues \f$ \lambda \f$ and, optionally, its right or left eigenvectors:
\f[
A v = \lambda v
\f]
where \f$ v \f$ represents an eigenvector corresponding to eigenvalue \f$ \lambda \f$.
In the generalized eigenvalue problem case, the routine solves:
\f[
A v = \lambda B v
\f]
The computation supports both real and complex matrices. If requested, eigenvectors are returned as additional output arguments. The function provides options to allow in-place modification of A and B for performance optimization.
Note: The solution is based on LAPACK’s [GEEV](@ref la_lapack::geev) and [GGEV](@ref la_lapack::ggev) routines.
Arguments
a: A real or complex matrix of size \f$[n,n]\f$, representing the input matrix to be decomposed.
b (optional): A real or complex matrix of size \f$[n,n]\f$, same type and kind as a, representing the second matrix in the generalized eigenvalue problem.
lambda: A complex or real array of length \f$ n \f$, containing the computed eigenvalues.
right (optional): A complex matrix of size \f$[n,n]\f$ containing the right eigenvectors as columns.
left (optional): A complex matrix of size \f$[n,n]\f$ containing the left eigenvectors as columns.
overwrite_a (optional): A logical flag indicating whether A can be overwritten for performance optimization.
overwrite_b (optional): A logical flag indicating whether B can be overwritten (only in the generalized case).
err (optional): A [type(la_state)](@ref la_state_type::la_state) variable to handle errors. If not provided, execution will stop on errors.
Errors
- Raises [LINALG_VALUE_ERROR](@ref la_state_type::linalg_value_error) if the matrices have invalid/incompatible sizes.
- Raises [LINALG_ERROR](@ref la_state_type::linalg_error) if the eigendecomposition fails.
- If
err is not provided, exceptions will trigger an error stop.
Notes
- The computed eigenvectors are normalized.
- If computing real eigenvalues, an error is returned if eigenvalues have nonzero imaginary parts.
- This routine is based on LAPACK’s [GEEV](@ref la_lapack::geev) and [GGEV](@ref la_lapack::ggev) routines.
- Overwriting
A or B can improve performance but destroys the original matrix data.
- This subroutine is
pure.
[eigh](@ref la_eig::eigh) - Eigendecomposition of a real symmetric or complex Hermitian matrix.
Syntax
call eigh(a, lambda [, vectors] [, upper_a] [, overwrite_a] [, err])
Description
This interface provides methods for computing the eigenvalues and optionally the eigenvectors of a real symmetric or complex Hermitian matrix.
Given a real symmetric or complex Hermitian matrix \f$ A \f$, this routine computes its eigenvalues \f$ \lambda \f$ and, optionally, its right eigenvectors:
\f[
A v = \lambda v
\f]
where \f$ v \f$ represents an eigenvector corresponding to eigenvalue \f$ \lambda \f$.
The computation supports both real and complex matrices, and the eigenvectors, if requested, are returned as orthonormal vectors.
Note: The solution is based on LAPACK’s [SYEVD](@ref la_lapack::syevd) and [HEEVD](@ref la_lapack::heevd) routines.
Arguments
a: A real or complex matrix of size \f$[n,n]\f$, representing the input matrix to be decomposed. The matrix is overwritten with the eigenvalues on output.
lambda: A real array of length \f$ n \f$, with the same kind as a, containing the computed eigenvalues.
vectors (optional): A matrix of size \f$[n,n]\f$, with the same type and kind as a, containing the right eigenvectors stored as columns.
overwrite_a (optional): A logical flag indicating whether the matrix A can be overwritten for performance optimization.
upper_a (optional): A logical flag indicating whether the upper half of matrix A should be used for computation (default is the lower half).
err (optional): A [type(la_state)](@ref la_state_type::la_state) variable to handle errors. If not provided, execution will stop on errors.
Errors
- Raises [LINALG_VALUE_ERROR](@ref la_state_type::linalg_value_error) if the matrices have invalid/incompatible sizes.
- Raises [LINALG_ERROR](@ref la_state_type::linalg_error) if the eigendecomposition fails.
- If
err is not provided, exceptions will trigger an error stop.
Notes
- The computed eigenvectors are orthonormal.
- Eigenvalues are real for symmetric or Hermitian matrices.
- This routine is based on LAPACK’s [SYEVD](@ref la_lapack::syevd) and [HEEVD](@ref la_lapack::heevd) routines.
- Overwriting the matrix
A can improve performance but destroys the original matrix data.
- This subroutine is
pure.
[eigvals](@ref la_eig::eigvals) - Eigenvalues of a square matrix (interface).
Syntax
lambda = eigvals(a [, b] [, err])
Description
This interface provides methods for computing the eigenvalues of a real or complex square matrix.
It supports both standard and generalized eigenvalue problems.
- In the standard eigenvalue problem, the function computes the eigenvalues \f$\lambda\f$ of the matrix \f$A\f$ such that:
\f[
A v = \lambda v
\f]
where \f$v\f$ is the eigenvector corresponding to eigenvalue \f$\lambda\f$.
- In the generalized eigenvalue problem, the function solves:
\f[
A v = \lambda B v
\f]
where \f$A\f$ and \f$B\f$ are the input matrices and \f$\lambda\f$ is the eigenvalue.
The function returns an array of eigenvalues computed for the input matrix \f$A\f$, and optionally the matrix \f$B\f$ for the generalized case.
Note: The solution is based on LAPACK’s [GEEV](@ref la_lapack::geev) and [GGEV](@ref la_lapack::ggev) routines.
Arguments
a: A real or complex matrix of size \f$[n,n]\f$, representing the input matrix to be decomposed.
b (optional, generalized case): A matrix of size \f$[n,n]\f$ and same type and kind as a, representing the second matrix in the generalized eigenvalue problem.
err (optional): A [type(la_state)](@ref la_state_type::la_state) variable to handle errors. If not provided, execution will stop on errors.
Return value
lambda: A complex array of eigenvalues, computed from the input matrix \f$A\f$ (and \f$B\f$ if in the generalized case).
Errors
- Raises [LINALG_VALUE_ERROR](@ref la_state_type::linalg_value_error) if the matrices have invalid/incompatible sizes.
- Raises [LINALG_ERROR](@ref la_state_type::linalg_error) if the eigendecomposition fails.
- If
err is not provided, exceptions will trigger an error stop.
Notes
- The eigenvalues are returned in a complex array, even for real matrices.
- For the generalized eigenvalue problem, matrix \f$B\f$ must be provided. Neither \f$A\f$ nor \f$B\f$ is modified.
- This routine is based on LAPACK’s [GEEV](@ref la_lapack::geev) and [GGEV](@ref la_lapack::ggev) routines.
- The form without
err is pure, so it can be used inside pure procedures and do concurrent blocks.
[eigvalsh](@ref la_eig::eigvalsh) - Eigenvalues of a real symmetric or complex Hermitian matrix.
Syntax
lambda = eigvalsh(a [, upper_a] [, err])
Description
This interface provides methods for computing the eigenvalues of a real symmetric or complex Hermitian matrix.
The function computes the eigenvalues of the matrix \f$A\f$, and returns them in an array.
The user can specify whether to use the upper or lower half of the matrix for computation.
- The function solves the eigenvalue problem:
\f[
A v = \lambda v
\f]
where \f$v\f$ is the eigenvector corresponding to eigenvalue \f$\lambda\f$.
- The computation supports both real and complex matrices. Regardless, due to symmetry the eigenvalues are returned as an array of
real values.
The user can specify whether to use the upper or lower half of the matrix \f$A\f$ for the computation (default: lower half).
Note: The solution is based on LAPACK’s [SYEV](@ref la_lapack::syev) and [HEEV](@ref la_lapack::heev) routines.
Arguments
a: A real or complex matrix of size \f$[n,n]\f$, representing the real symmetric or complex Hermitian matrix to be decomposed.
upper_a (optional): A logical flag indicating whether to use the upper half (.true.) or the lower half (.false.) of \f$A\f$ for the computation. The default is lower.
err (optional): A [type(la_state)](@ref la_state_type::la_state) variable to handle errors. If not provided, execution will stop on errors.
Return value
lambda: A real array containing the computed eigenvalues of the matrix \f$A\f$.
Errors
- Raises [LINALG_VALUE_ERROR](@ref la_state_type::linalg_value_error) if the matrix has invalid size.
- Raises [LINALG_ERROR](@ref la_state_type::linalg_error) if the eigendecomposition fails.
- If
err is not provided, execution will stop on errors.
Notes
- The eigenvalues are returned in a
real array, with the same kind as the input matrix a.
- This routine is based on LAPACK’s [SYEV](@ref la_lapack::syev) and [HEEV](@ref la_lapack::heev) routines.
- The form without
err is pure, so it can be used inside pure procedures and do concurrent blocks.
[solve](@ref la_solve::solve) - Solve a linear matrix equation or a linear system of equations.
Syntax
x = solve(a, b)
x = solve(a, b [, overwrite_a], err)
Description
Solve linear systems - one (b(:)) or many (b(:,:)).
Without err the function is pure and takes a as intent(in); with err it returns errors instead of stopping, is not pure, and can overwrite a.
Arguments
a: A real or complex coefficient matrix. If overwrite_a=.true., it is destroyed by the call.
b: A rank-1 (one system) or rank-2 (many systems) array of the same kind as a, containing the right-hand-side vector(s).
overwrite_a (optional, default = .false., with err only): If .true., input matrix a will be used as temporary storage and overwritten, to avoid internal data allocation.
err (optional): A [type(la_state)](@ref la_state_type::la_state) variable that receives the error state.
Return value
For a full-rank matrix, returns an array value that represents the solution to the linear system of equations. With err, a failed solve returns an empty array.
Errors
- Raises [LINALG_ERROR](@ref la_state_type::linalg_error) if the matrix is singular to working precision.
- Raises [LINALG_VALUE_ERROR](@ref la_state_type::linalg_value_error) if the matrix and rhs vectors have invalid/incompatible sizes.
- If
err is not present, exceptions trigger an error stop.
[solve_lu](@ref la_solve::solve_lu) - Solve a linear system into a pre-allocated array.
Syntax
call solve_lu(a, b, x [, pivot] [, overwrite_a] [, err])
Description
Solve linear systems - one (b(:)) or many (b(:,:)) - writing the result into the caller’s array x instead of returning a new one. Storage for the pivot indices may be supplied as well: when x and pivot are both provided and overwrite_a=.true., the call performs no internal allocation, which makes it suited to a loop over many systems of the same size. The routine is pure.
Arguments
a: A real or complex coefficient matrix of size \f$ [n,n] \f$. If overwrite_a=.true., it is destroyed by the call.
b: A rank-1 (one system) or rank-2 (many systems) array of the same kind as a, containing the right-hand-side vector(s).
x: An array of the same shape and kind as b. On output it holds the solution.
pivot (optional): An integer(ilp) array of size n that receives the diagonal pivot indices of the LU factorization.
overwrite_a (optional, default = .false.): If .true., input matrix a will be used as temporary storage and overwritten, to avoid internal data allocation.
err (optional): A [type(la_state)](@ref la_state_type::la_state) variable.
Errors
- Raises [LINALG_ERROR](@ref la_state_type::linalg_error) if the matrix is singular to working precision.
- Raises [LINALG_VALUE_ERROR](@ref la_state_type::linalg_value_error) if
a, b, x or pivot have invalid/incompatible sizes.
- If
err is not present, exceptions trigger an error stop.
Notes
- This subroutine is based on LAPACK’s LU decomposition solvers [GESV](@ref la_lapack::gesv).
- [solve](@ref la_solve::solve) is the function form; it allocates and returns the solution instead of writing into
x.
[solve_chol](@ref la_solve::solve_chol) - Solve a Hermitian positive definite system.
Syntax
call solve_chol(a, b, x [, lower] [, overwrite_a] [, err])
Description
Factorize a real symmetric or complex Hermitian positive definite matrix and solve \f$ A x = b \f$ in one call, for one (b(:)) or many (b(:,:)) right-hand sides. Only the triangle lower selects is read. The result is written into the caller’s array x. The routine is pure.
Arguments
a: A real symmetric or complex Hermitian positive definite matrix of size \f$ [n,n] \f$. If overwrite_a=.true., it is overwritten with its Cholesky factor.
b: A rank-1 (one system) or rank-2 (many systems) array of the same kind as a, containing the right-hand-side vector(s).
x: An array of the same shape and kind as b. On output it holds the solution.
lower (optional, default = .true.): If .true., the lower triangle of a is read and the factorization is \f$ A = L L^H \f$; otherwise the upper triangle is read and the factorization is \f$ A = U^H U \f$.
overwrite_a (optional, default = .false.): If .true., input matrix a will be used as temporary storage and overwritten, to avoid internal data allocation.
err (optional): A [type(la_state)](@ref la_state_type::la_state) variable.
Errors
- Raises [LINALG_ERROR](@ref la_state_type::linalg_error) if
a is not positive definite.
- Raises [LINALG_VALUE_ERROR](@ref la_state_type::linalg_value_error) if
a, b or x have invalid/incompatible sizes.
- If
err is not present, exceptions trigger an error stop.
Notes
- This subroutine is based on LAPACK’s [POSV](@ref la_lapack::posv) drivers.
- To reuse a factorization across several right-hand sides, call [cholesky](@ref la_cholesky::cholesky) once and then [solve_lower_chol](@ref la_solve::solve_lower_chol) or [solve_upper_chol](@ref la_solve::solve_upper_chol).
[solve_lower_chol](@ref la_solve::solve_lower_chol), [solve_upper_chol](@ref la_solve::solve_upper_chol) - Solve from a Cholesky factor.
Syntax
call solve_lower_chol(l, b, x [, err])
call solve_upper_chol(u, b, x [, err])
Description
Solve \f$ A x = b \f$ for one or many right-hand sides from a Cholesky factor computed earlier, without factorizing again. Each call costs two triangular solves. Both routines are pure.
Arguments
l / u: The lower or upper Cholesky factor of size \f$ [n,n] \f$, as returned by [cholesky](@ref la_cholesky::cholesky) with lower=.true. or lower=.false..
b: A rank-1 (one system) or rank-2 (many systems) array of the same kind as the factor, containing the right-hand-side vector(s).
x: An array of the same shape and kind as b. On output it holds the solution.
err (optional): A [type(la_state)](@ref la_state_type::la_state) variable.
Errors
- Raises [LINALG_VALUE_ERROR](@ref la_state_type::linalg_value_error) if the factor,
b or x have invalid/incompatible sizes.
- If
err is not present, exceptions trigger an error stop.
Notes
- Both routines are based on LAPACK’s [POTRS](@ref la_lapack::potrs) routines.
- The factor is taken as given: a matrix that is not a Cholesky factor produces a wrong answer, not an error.
[lstsq](@ref la_least_squares::lstsq) - Compute a least squares solution to a system of linear equations.
Syntax
x = lstsq(a, b [, cond])
x = lstsq(a, b [, cond] [, overwrite_a] [, rank], err)
Description
Solves the least-squares problem for the system \f$ A \cdot x = b \f$, where \f$ A \f$ is a square matrix of size \f$ n \times n \f$ and \f$ b \f$ is either a vector of size \f$ n \f$ or a matrix of size \f$ n \times nrhs \f$. The function minimizes the 2-norm \f$ |b - A \cdot x| \f$ by solving for \f$ x \f$.
The result \f$ x \f$ is returned as an allocatable array, and it is either a vector (for a single right-hand side) or a matrix (for multiple right-hand sides).
Without err the function is pure and takes a as intent(in); with err it returns errors instead of stopping, is not pure, can overwrite a, and can return the rank.
Arguments
a: A real matrix of size \f$ n \times n \f$ representing the coefficient matrix. If overwrite_a = .true., the contents of a may be modified during the computation.
b: A real vector or matrix representing the right-hand side. The size should be \f$ n \f$ (for a single right-hand side) or \f$ n \times nrhs \f$ (for multiple right-hand sides).
cond (optional): A cutoff for rank evaluation. Singular values \f$ s(i) \f$ such that \f$ s(i) \leq \text{cond} \cdot \max(s) \f$ are considered zero.
overwrite_a (optional, default = .false., with err only): If .true., both a and b may be overwritten and destroyed during computation.
rank (optional, with err only): An integer variable that returns the rank of the matrix \f$ A \f$.
err (optional): A [type(la_state)](@ref la_state_type::la_state) variable that returns the error state. If err is not provided, the function will stop execution on error.
Return value
Returns the solution array \f$ x \f$ with size \f$ n \f$ (for a single right-hand side) or \f$ n \times nrhs \f$ (for multiple right-hand sides). With err, an invalid problem returns an empty array.
Errors
- Raises [LINALG_ERROR](@ref la_state_type::linalg_error) if the matrix \f$ A \f$ is singular to working precision.
- Raises [LINALG_VALUE_ERROR](@ref la_state_type::linalg_value_error) if the matrix
a and the right-hand side b have incompatible sizes.
- If
err is not provided, the function stops execution on error.
Notes
- This function relies on LAPACK’s least-squares solvers, such as [GELSS](@ref la_lapack::gelss).
- If
overwrite_a is enabled, the original contents of a and b may be lost.
[lstsq_space](@ref la_least_squares::lstsq_space) - Workspace size for least squares operations.
Syntax
call lstsq_space(a, b, lrwork, liwork) for real data
call lstsq_space(a, b, lrwork, liwork, lcwork) for complex data
Description
This subroutine returns the sizes of the working arrays that [solve_lstsq](@ref la_least_squares::solve_lstsq) needs for a problem of the shape of a and b, so that a repeated solve of problems of the same size performs no internal allocation.
Arguments
a: A real or complex matrix of size \f$ [m,n] \f$. Only its shape is read.
b: A right-hand-side vector of size \f$m\f$ or matrix of size \f$ [m,nrhs] \f$. Only its shape is read.
lrwork: An integer returning the size of the real working array.
liwork: An integer returning the size of the integer working array.
lcwork: An integer returning the size of the complex working array. Complex data only.
Return value
The three (two, for real data) workspace sizes are returned in the output arguments.
Errors
- This subroutine is
pure and cannot fail: it reads the shapes of its inputs and nothing else.
Notes
- The sizes are those of LAPACK’s [GELSD](@ref la_lapack::gelsd), with 25% headroom.
[solve_lstsq](@ref la_least_squares::solve_lstsq) - Least squares solution into a pre-allocated array.
Syntax
call solve_lstsq(a, b, x [, real_storage] [, int_storage] [, cmpl_storage] [, cond] [, singvals] [, overwrite_a] [, rank] [, err])
Description
This subroutine computes the least-squares solution of \f$ A \cdot x = b \f$ and writes it into the caller’s array. Given the optional working arrays, it allocates nothing, so a repeated solve of problems of the same size runs without any memory traffic.
Arguments
a: A real or complex matrix of size \f$ [m,n] \f$. It is an intent(inout) argument, and is destroyed if overwrite_a is true.
b: The right-hand-side vector of size \f$m\f$ or matrix of size \f$ [m,nrhs] \f$. It is an intent(in) argument.
x: The solution vector of size \f$ \ge n \f$ or matrix of size \f$ [\ge n,nrhs] \f$. It is an intent(inout) contiguous argument.
real_storage (optional): A real working array of size at least the lrwork returned by [lstsq_space](@ref la_least_squares::lstsq_space).
int_storage (optional): An integer working array of size at least liwork.
cmpl_storage (optional): A complex working array of size at least lcwork. Complex data only.
cond (optional): The cutoff for rank evaluation: singular values \f$ s_i \le \text{cond} \cdot \max(s) \f$ are treated as zero.
singvals (optional): A real array of size at least \f$ \min(m,n) \f$ returning the singular values in decreasing order.
overwrite_a (optional): If true, a may be overwritten and destroyed. Default is false.
rank (optional): An integer returning the rank of a.
err (optional): A state return flag of [type(la_state)](@ref la_state_type::la_state). If an error occurs and err is not provided, the subroutine will stop execution.
Return value
The solution is written into x.
Errors
- Raises [LINALG_VALUE_ERROR](@ref la_state_type::linalg_value_error) if the matrix and right-hand-side sizes are inconsistent, if
x is too small, or if a working array is too small.
- Raises [LINALG_ERROR](@ref la_state_type::linalg_error) if the singular value decomposition did not converge.
- If
err is not provided, the subroutine will stop execution on errors.
Notes
- [
lstsq](@ref la_least_squares::lstsq) is this subroutine with the solution allocated for the caller.
- This subroutine is
pure.
[weighted_lstsq](@ref la_least_squares::weighted_lstsq) - Weighted least squares solution (function).
Syntax
x = weighted_lstsq(w, a, b [, cond])
x = weighted_lstsq(w, a, b [, cond] [, overwrite_a] [, rank], err)
Description
This function minimizes \f$ |D (b - A \cdot x)| \f$ with \f$ D = \mathrm{diag}(\sqrt{w}) \f$: the \f$i\f$-th equation carries the weight \f$ w_i \f$. Both sides are scaled and the resulting ordinary least-squares problem is solved with [GELSD](@ref la_lapack::gelsd).
Without err the function is pure and takes a as intent(in); with err it returns errors instead of stopping, is not pure, can overwrite a, and can return the rank.
Arguments
w: A real vector of size \f$m\f$. The weights are always real and must all be positive.
a: A real or complex matrix of size \f$ [m,n] \f$. With err it is an intent(inout) argument, and is destroyed if overwrite_a is true; without err it is intent(in).
b: The right-hand-side vector of size \f$m\f$. It is an intent(in) argument.
cond (optional): The cutoff for rank evaluation.
overwrite_a (optional, with err only): If true, a may be overwritten and destroyed. Default is false.
rank (optional, with err only): An integer returning the rank of a.
err (optional): A state return flag of [type(la_state)](@ref la_state_type::la_state). If an error occurs and err is not provided, the function will stop execution.
Return value
The function returns the solution vector of size \f$n\f$.
Errors
- Raises [LINALG_VALUE_ERROR](@ref la_state_type::linalg_value_error) if the matrix is empty, if
w or b does not have one entry per row of a, or if any weight is not positive.
- If
err is not provided, the function will stop execution on errors.
Notes
- With uniform weights the result is that of [
lstsq](@ref la_least_squares::lstsq).
[solve_weighted_lstsq](@ref la_least_squares::solve_weighted_lstsq) - Weighted least squares solution into a pre-allocated array.
Syntax
call solve_weighted_lstsq(w, a, b, x [, cond] [, overwrite_a] [, rank] [, err])
Description
This subroutine is the subroutine form of [weighted_lstsq](@ref la_least_squares::weighted_lstsq): it writes the solution into the caller’s array instead of allocating it.
Arguments
w: A real vector of size \f$m\f$ of positive weights.
a: A real or complex matrix of size \f$ [m,n] \f$. It is an intent(inout) argument, and is destroyed if overwrite_a is true.
b: The right-hand-side vector of size \f$m\f$. It is an intent(in) argument.
x: The solution vector of size \f$n\f$. It is an intent(inout) contiguous argument.
cond (optional): The cutoff for rank evaluation.
overwrite_a (optional): If true, a may be overwritten and destroyed. Default is false.
rank (optional): An integer returning the rank of a.
err (optional): A state return flag of [type(la_state)](@ref la_state_type::la_state). If an error occurs and err is not provided, the subroutine will stop execution.
Return value
The solution is written into x.
Errors
- Raises [LINALG_VALUE_ERROR](@ref la_state_type::linalg_value_error) if the matrix is empty, if
w or b does not have one entry per row of a, or if any weight is not positive.
- If
err is not provided, the subroutine will stop execution on errors.
Notes
- The weights are applied to a copy of
a unless overwrite_a is set, so a is unchanged by default.
- This subroutine is
pure.
[constrained_lstsq](@ref la_least_squares::constrained_lstsq) - Equality-constrained least squares solution (function).
Syntax
x = constrained_lstsq(a, b, c, d)
x = constrained_lstsq(a, b, c, d [, overwrite_matrices], err)
Description
This function minimizes \f$ |b - A \cdot x| \f$ subject to \f$ C \cdot x = d \f$, with \f$ A \f$ of size \f$ [m,n] \f$ and \f$ C \f$ of size \f$ [p,n] \f$. The problem has a unique solution when \f$ p \le n \le m+p \f$, \f$ \mathrm{rank}© = p \f$ and the stacked matrix has rank \f$ n \f$.
Without err the function is pure and takes a, b, c and d as intent(in); with err it returns errors instead of stopping, is not pure, and can overwrite them.
Arguments
a: A real or complex matrix of size \f$ [m,n] \f$, the least-squares cost. With err it is an intent(inout) argument, and is destroyed if overwrite_matrices is true; without err, a, b, c and d are intent(in).
b: The least-squares right-hand-side vector of size \f$m\f$. It is an intent(inout) argument with err.
c: The constraint matrix of size \f$ [p,n] \f$. It is an intent(inout) argument with err.
d: The constraint right-hand-side vector of size \f$p\f$. It is an intent(inout) argument with err.
overwrite_matrices (optional, with err only): If true, a, b, c and d may be overwritten and destroyed. Default is false.
err (optional): A state return flag of [type(la_state)](@ref la_state_type::la_state). If an error occurs and err is not provided, the function will stop execution.
Return value
The function returns the solution vector of size \f$n\f$.
Errors
- Raises [LINALG_VALUE_ERROR](@ref la_state_type::linalg_value_error) if any matrix is empty or if the four shapes are inconsistent.
- Raises [LINALG_ERROR](@ref la_state_type::linalg_error) if
c is rank deficient or if the stacked matrix is.
- If
err is not provided, the function will stop execution on errors.
Notes
- This function uses LAPACK’s [GGLSE](@ref la_lapack::gglse) driver.
[solve_constrained_lstsq](@ref la_least_squares::solve_constrained_lstsq) - Equality-constrained least squares solution into a pre-allocated array.
Syntax
call solve_constrained_lstsq(a, b, c, d, x [, storage] [, overwrite_matrices] [, err])
Description
This subroutine is the subroutine form of [constrained_lstsq](@ref la_least_squares::constrained_lstsq): it writes the solution into the caller’s array and can reuse a caller-provided workspace.
Arguments
a: A real or complex matrix of size \f$ [m,n] \f$, the least-squares cost. It is an intent(inout) argument.
b: The least-squares right-hand-side vector of size \f$m\f$. It is an intent(inout) argument.
c: The constraint matrix of size \f$ [p,n] \f$. It is an intent(inout) argument.
d: The constraint right-hand-side vector of size \f$p\f$. It is an intent(inout) argument.
x: The solution vector of size \f$n\f$. It is an intent(out) argument.
storage (optional): A working array of size at least the lwork returned by [constrained_lstsq_space](@ref la_least_squares::constrained_lstsq_space).
overwrite_matrices (optional): If true, a, b, c and d may be overwritten and destroyed. Default is false.
err (optional): A state return flag of [type(la_state)](@ref la_state_type::la_state). If an error occurs and err is not provided, the subroutine will stop execution.
Return value
The solution is written into x.
Errors
- Raises [LINALG_VALUE_ERROR](@ref la_state_type::linalg_value_error) if the shapes are inconsistent or if
storage is too small.
- Raises [LINALG_ERROR](@ref la_state_type::linalg_error) if
c is rank deficient or if the stacked matrix is.
- If
err is not provided, the subroutine will stop execution on errors.
Notes
- Without
overwrite_matrices, all four inputs are copied, so none of them is changed.
- This subroutine is
pure.
[constrained_lstsq_space](@ref la_least_squares::constrained_lstsq_space) - Workspace size for the constrained least squares solver.
Syntax
call constrained_lstsq_space(a, c, lwork [, err])
Description
This subroutine asks LAPACK for the optimal size of the workspace array that [solve_constrained_lstsq](@ref la_least_squares::solve_constrained_lstsq) needs for a problem of the shape of a and c.
Arguments
a: A real or complex matrix of size \f$ [m,n] \f$. Only its shape is read.
c: The constraint matrix of size \f$ [p,n] \f$. Only its shape is read.
lwork: An integer returning the size of the workspace array.
err (optional): A state return flag of [type(la_state)](@ref la_state_type::la_state). If an error occurs and err is not provided, the subroutine will stop execution.
Errors
- Raises [LINALG_VALUE_ERROR](@ref la_state_type::linalg_value_error) if LAPACK rejects the problem dimensions.
- If
err is not provided, the subroutine will stop execution on errors.
Notes
- The size returned is the optimal one, not the minimum one.
- This subroutine is
pure.
[generalized_lstsq](@ref la_least_squares::generalized_lstsq) - Generalized least squares solution (function).
Syntax
x = generalized_lstsq(w, a, b [, prefactored_w])
x = generalized_lstsq(w, a, b [, prefactored_w], err)
Description
This function minimizes \f$ (b - A \cdot x)^H W^{-1} (b - A \cdot x) \f$, the least-squares cost of observations whose errors are correlated, with the symmetric (Hermitian) positive definite covariance matrix \f$ W \f$. Without err it is pure; with err it returns errors instead of stopping, and is not pure.
Arguments
w: The covariance matrix of size \f$ [m,m] \f$, of the same type and kind as a. If prefactored_w is true, it is instead a square root \f$ B \f$ of the covariance matrix, \f$ W = B B^H \f$, which need not be triangular. It is not modified.
a: A real or complex matrix of size \f$ [m,n] \f$, with \f$ m \ge n \f$. It is not modified.
b: The right-hand-side vector of size \f$m\f$. It is not modified.
prefactored_w (optional): If true, w is a square root of the covariance matrix, for example its Cholesky factor. Default is false.
err (optional): A [type(la_state)](@ref la_state_type::la_state) variable that receives the error state.
Return value
The function returns the solution vector of size \f$n\f$. With err, a failed solve returns an empty vector.
Errors
- Raises [LINALG_VALUE_ERROR](@ref la_state_type::linalg_value_error) if
a is empty or has fewer rows than columns, or if w or b does not match the rows of a.
- Raises [LINALG_ERROR](@ref la_state_type::linalg_error) if
w is not positive definite, if its square root is singular, or if a does not have full column rank.
- If
err is not present, exceptions trigger an error stop.
Notes
- This function uses LAPACK’s [GGGLM](@ref la_lapack::ggglm) driver: with \f$ W = B B^H \f$, it minimizes \f$ |y| \f$ subject to \f$ b = A x + B y \f$. Without
prefactored_w, \f$ B \f$ is the Cholesky factor of w.
- With an identity covariance matrix, the solution is the one of [
lstsq](@ref la_least_squares::lstsq).
- [solve_generalized_lstsq](@ref la_least_squares::solve_generalized_lstsq) is the subroutine form.
[solve_generalized_lstsq](@ref la_least_squares::solve_generalized_lstsq) - Generalized least squares solution into a pre-allocated array.
Syntax
call solve_generalized_lstsq(w, a, b, x [, storage] [, prefactored_w] [, overwrite_a] [, overwrite_w] [, err])
Description
This subroutine is the subroutine form of [generalized_lstsq](@ref la_least_squares::generalized_lstsq): it writes the solution into the caller’s array, can reuse a caller-provided workspace, and can work on w and a in place.
Arguments
w: The covariance matrix of size \f$ [m,m] \f$, or a square root of it if prefactored_w is true. It is an intent(inout) argument, and is destroyed if overwrite_w is true.
a: A real or complex matrix of size \f$ [m,n] \f$, with \f$ m \ge n \f$. It is an intent(inout) argument, and is destroyed if overwrite_a is true.
b: The right-hand-side vector of size \f$m\f$. It is an intent(in) argument.
x: The solution vector of size \f$n\f$. It is an intent(out) argument.
storage (optional): A working array of size at least the lwork returned by [generalized_lstsq_space](@ref la_least_squares::generalized_lstsq_space).
prefactored_w (optional): If true, w is a square root of the covariance matrix. Default is false.
overwrite_a (optional): If true, a may be overwritten and destroyed. Default is false.
overwrite_w (optional): If true, w may be overwritten and destroyed. Default is false.
err (optional): A state return flag of [type(la_state)](@ref la_state_type::la_state). If an error occurs and err is not provided, the subroutine will stop execution.
Return value
The solution is written into x.
Errors
- Raises [LINALG_VALUE_ERROR](@ref la_state_type::linalg_value_error) if the shapes are inconsistent or if
storage is too small.
- Raises [LINALG_ERROR](@ref la_state_type::linalg_error) if
w is not positive definite, if its square root is singular, or if a does not have full column rank.
- If
err is not provided, the subroutine will stop execution on errors.
Notes
- Without
overwrite_a and overwrite_w, both matrices are copied, so neither is changed. With both, and with storage, the subroutine allocates nothing.
- This subroutine is
pure.
[generalized_lstsq_space](@ref la_least_squares::generalized_lstsq_space) - Workspace size for the generalized least squares solver.
Syntax
call generalized_lstsq_space(a, lwork [, err])
Description
This subroutine returns the size of the workspace array that [solve_generalized_lstsq](@ref la_least_squares::solve_generalized_lstsq) needs for a problem of the shape of a: the optimal workspace of LAPACK’s GGGLM, plus two vectors of size \f$m\f$.
Arguments
a: A real or complex matrix of size \f$ [m,n] \f$. Only its shape is read.
lwork: An integer returning the size of the workspace array.
err (optional): A state return flag of [type(la_state)](@ref la_state_type::la_state). If an error occurs and err is not provided, the subroutine will stop execution.
Errors
- Raises [LINALG_VALUE_ERROR](@ref la_state_type::linalg_value_error) if LAPACK rejects the problem dimensions, for example if
a has fewer rows than columns.
- If
err is not provided, the subroutine will stop execution on errors.
Notes
- The size returned is the optimal one, not the minimum one.
- This subroutine is
pure.
[det](@ref la_determinant::det) - Determinant of a scalar or rectangular matrix.
Syntax
d = det(a)
d = det(a [, overwrite_a], err)
Description
This function computes the determinant of a square matrix \f$ A \f$. The matrix must be a real matrix of size \f$ [m, n] \f$, and the determinant is computed using an efficient factorization method (e.g., LU decomposition).
Without err the function is pure, takes a as intent(in), and is the function behind [operator(.det.)](@ref la_determinant::operator(.det.)); with err it returns errors instead of stopping, is not pure, and can overwrite a.
Arguments
a: A real matrix of size \f$ [m, n] \f$, representing the rectangular matrix for which the determinant is calculated. With err, it is an inout argument and may be modified during computation if overwrite_a is set.
overwrite_a (optional, default = .false., with err only): A logical flag that determines whether the input matrix a can be overwritten. If .true., the matrix a may be destroyed and modified in place to save memory.
err (optional): A state return flag of [type(la_state)](@ref la_state_type::la_state). If an error occurs and err is not provided, the function will stop execution.
Return value
The function returns a real scalar value representing the determinant of the input matrix \f$ A \f$, with the same kind as \f$ A \f$.
Errors
- Raises [LINALG_VALUE_ERROR](@ref la_state_type::linalg_value_error) if the matrix
a is not square.
- If
err is not provided, the function will stop execution on errors.
Notes
- A singular matrix is not an error: its determinant is 0.
- The determinant of the matrix is computed using the LAPACK [getrf](@ref la_lapack::getrf) backend.
- If
overwrite_a is enabled, the input matrix a will be destroyed during the computation process.
[operator(.det.)](@ref la_determinant::operator(.det.)) - Determinant of a square matrix.
Syntax
d = .det. A
Description
This operator computes the determinant of a square real or complex matrix \f$ A \f$ from its LU factorization: it is det(A) without err. It is pure, so it can be used inside pure procedures and do concurrent blocks; it takes no overwrite_a flag and never modifies its operand, which is copied internally.
Arguments
A: A real or complex square matrix of size \f$ [n,n] \f$.
Return value
A scalar of the same type and kind as A, holding its determinant.
Errors
- Unlike [det](@ref la_determinant::det), this operator does not provide explicit error handling: it has no
err argument, so a non-square matrix triggers an error stop. A singular matrix is not an error: it returns 0.
Notes
- The determinant is computed through the LAPACK [getrf](@ref la_lapack::getrf) backend.
- If error handling is required, use [det](@ref la_determinant::det) with its
err argument instead.
[slogdet](@ref la_determinant::slogdet) - Sign and logarithm of the determinant of a square matrix.
Syntax
call slogdet(a, sign, logabsdet [, overwrite_a] [, err])
Description
This subroutine computes the determinant of a real or complex square matrix as \f$ \det(A) = sign \cdot e^{logabsdet} \f$, from its LU factorization. The logarithm stays representable where the determinant itself would overflow or underflow, as it does for large matrices or for products of many small pivots.
Arguments
a: A real or complex square matrix of size \f$ [n,n] \f$. It is an intent(inout) argument, and is overwritten by its LU factorization if overwrite_a is true.
sign: A scalar of the same type and kind as a, returning the sign of the determinant: \f$ \pm 1 \f$ for a real matrix, a complex number of unit modulus for a complex one, and 0 for a singular matrix. It is an intent(out) argument.
logabsdet: A real scalar of the kind of a, returning the natural logarithm of \f$ |\det(A)| \f$, and \f$ -\infty \f$ for a singular matrix. It is an intent(out) argument.
overwrite_a (optional, default = .false.): If .true., a may be overwritten and destroyed.
err (optional): A [type(la_state)](@ref la_state_type::la_state) variable that returns the error state. If not provided, the subroutine will stop execution on error.
Errors
- Raises [LINALG_VALUE_ERROR](@ref la_state_type::linalg_value_error) if
a is not square.
- A singular matrix is not an error: it returns
sign = 0 and logabsdet \f$ = -\infty \f$.
- If
err is not provided, exceptions trigger an error stop.
Notes
- The LU factorization comes from LAPACK’s [getrf](@ref la_lapack::getrf); the determinant of an empty matrix is 1.
- This subroutine is
pure.
[inv](@ref la_inverse::inv) - Inverse of a square matrix.
Syntax
inv_a = inv(a)
inv_a = inv(a, err)
Description
This function computes the inverse \f$ A^{-1} \f$ of a real or complex square matrix \f$ A \f$, provided that \f$ A \f$ is non-singular.
Without err it is pure, and is the function behind [operator(.inv.)](@ref la_inverse::operator(.inv.)); with err it returns errors instead of stopping, and is not pure.
The inverse of a matrix is defined as:
\f[
A A^{-1} = A^{-1} A = I
\f]
where \f$ I \f$ is the identity matrix of the same size as \f$ A \f$. The inverse exists only if \f$ A \f$ is square and has full rank (i.e., all its singular values are nonzero).
The computation is performed using LU decomposition.
Arguments
a: A real or complex square matrix of size \f$ [n,n] \f$, representing the matrix to be inverted.
err (optional): A [type(la_state)](@ref la_state_type::la_state) variable that returns the error state. If not provided, the function will stop execution on error.
Return value
inv_a: A real or complex square matrix of size \f$ [n,n] \f$, representing the inverse of a. With err, an error returns an empty \f$ [0,0] \f$ matrix.
Errors
- Raises [LINALG_ERROR](@ref la_state_type::linalg_error) if
a is singular.
- Raises [LINALG_VALUE_ERROR](@ref la_state_type::linalg_value_error) if
a has invalid size.
- If
err is not provided, exceptions will trigger an error stop.
Notes
- This function computes the inverse using LAPACK’s LU decomposition routine [GETRF](@ref la_lapack::getrf) followed by [GETRI](@ref la_lapack::getri).
- The inverse should be used with caution in numerical computations. For solving linear systems, using [solve](@ref la_solve::solve) is usually more stable and efficient than explicitly computing the inverse.
[invert](@ref la_inverse::invert) - Matrix inversion (subroutine).
Syntax
call invert(a [, pivot] [, err])
call invert(a, inva [, pivot] [, err])
Description
This subroutine computes the inverse \( A^{-1} \) of a real or complex square matrix \( A \). The first form works in-place, modifying a directly; the second writes the inverse into a second matrix inva of the same shape and leaves a untouched. Both use the LU decomposition method via LAPACK’s [GETRF](@ref la_lapack::getrf) and [GETRI](@ref la_lapack::getri) routines.
Given a square matrix \( A \), the LU decomposition factorizes it as:
\f[
A = P L U
\f]
where:
- \( P \) is a permutation matrix,
- \( L \) is a lower triangular matrix with unit diagonal,
- \( U \) is an upper triangular matrix.
The inverse is then obtained by solving \( A X = I \) using the LU factors.
Arguments
a: A real or complex square matrix of size \( [n,n] \). In the in-place form it is replaced with its inverse \( A^{-1} \) on output; in the split form it is read only.
inva (split form only): A matrix of the same shape and kind as a, which receives the inverse \( A^{-1} \).
pivot (optional): An integer(ilp) array of size at least n that receives the diagonal pivot indices of the LU factorization. Supplying it avoids the internal allocation of the pivot array.
err (optional): A [type(la_state)](@ref la_state_type::la_state) variable that returns the error state. If not provided, the function will stop execution on error.
Errors
- Raises [LINALG_ERROR](@ref la_state_type::linalg_error) if the matrix is singular.
- Raises [LINALG_VALUE_ERROR](@ref la_state_type::linalg_value_error) if
a has invalid size, if inva does not match the shape of a, or if pivot is shorter than n.
- If
err is not provided, exceptions will trigger an error stop.
Notes
- The in-place form modifies
a. If the original matrix needs to be preserved, use the split form or [inv](@ref la_inverse::inv) instead.
- The determinant of
a can be computed before inversion using [det](@ref la_determinant::det) to check for singularity.
- The computational complexity is \( O(n^3) \), making it expensive for large matrices.
- It is recommended to use matrix factorizations (e.g., LU or QR) for solving linear systems instead of computing the inverse explicitly, as it is numerically more stable and efficient.
- Both forms are
pure.
[operator(.inv.)](@ref la_inverse::operator(.inv.)) - Compute the inverse of a square matrix.
Syntax
invA = .inv. A
Description
This operator computes the inverse \f$ A^{-1} \f$ of a square, non-singular real or complex matrix \f$ A \f$ using an LU decomposition. The inversion satisfies:
\f[
A A^{-1} = I
\f]
where \f$ I \f$ is the identity matrix of appropriate size.
This operator is inv(A) without err, with a more convenient syntax. It supports operator chaining, allowing multiple inversions within expressions.
Arguments
A: A real or complex square matrix of size \f$ [n,n] \f$, representing the input matrix to be inverted.
Return value
invA: A real or complex square matrix of size \f$ [n,n] \f$, and same kind as A representing its inverse.
Errors
- Unlike [inv](@ref la_inverse::inv) with
err, this operator does not provide explicit error handling.
- If
A is singular or an error occurs during inversion, execution stops.
Notes
- This operator internally calls LAPACK’s LU decomposition routine [GETRF](@ref la_lapack::getrf) followed by [GETRI](@ref la_lapack::getri).
- If error handling is required, use [inv](@ref la_inverse::inv) with its
err argument instead.
- The operator is
pure, so it can be used inside pure procedures and do concurrent blocks.
[pinv](@ref la_pseudoinverse::pinv) - Moore-Penrose pseudo-inverse of a matrix (function).
Syntax
pinva = pinv(a [, rtol])
pinva = pinv(a [, rtol], err)
Description
This function computes the Moore-Penrose pseudo-inverse \f$ A^+ \f$ of a real or complex matrix \f$ A \f$ using Singular Value Decomposition (SVD). The pseudo-inverse provides a generalization of the inverse for non-square and singular matrices, making it useful for solving least-squares problems and underdetermined systems.
Without err it is pure; with err it returns errors instead of stopping, and is not pure.
The computation is based on the singular value decomposition (SVD):
\f[
A = U \Sigma V^T
\f]
where \f$ U \f$ and \f$ V \f$ are orthogonal matrices, and \f$ \Sigma \f$ is a diagonal matrix containing the singular values. The pseudo-inverse is computed as:
\f[
A^+ = V \Sigma^+ U^T
\f]
where \f$ \Sigma^+ \f$ is obtained by inverting the nonzero singular values.
Arguments
a: A real or complex matrix of size \f$ [m,n] \f$, representing the input matrix to be inverted.
rtol (optional): A real scalar specifying the relative tolerance for singular value truncation. Singular values smaller than rtol * max(singular_values(A)) are set to zero. If not provided, a default machine-precision-based tolerance is used.
err (optional): A [type(la_state)](@ref la_state_type::la_state) variable that returns the error state. If not provided, the function will stop execution on error.
Return value
pinva: A real or complex matrix of size \f$ [n,m] \f$, representing the pseudo-inverse of a.
Errors
- Raises [LINALG_VALUE_ERROR](@ref la_state_type::linalg_value_error) if the SVD decomposition fails or the input matrix has invalid dimensions.
- Raises [LINALG_ERROR](@ref la_state_type::linalg_error) if numerical instability prevents inversion.
- If
err is not provided, exceptions will trigger an error stop.
Notes
- This function computes the pseudo-inverse using LAPACK’s SVD decomposition routine [
*GESVD](@ref la_lapack::gesvd).
- The choice of
rtol affects numerical stability and rank estimation: setting it too high may result in an inaccurate inverse, while setting it too low may amplify numerical noise.
- This function returns a newly allocated matrix. For an in-place version, use [
pseudoinvert](@ref la_pseudoinverse::pseudoinvert).
[operator(.pinv.)](@ref la_pseudoinverse::operator(.pinv.)) - Compute the Moore-Penrose pseudo-inverse of a matrix.
Syntax
pinva = .pinv. a
Description
This operator computes the Moore-Penrose pseudo-inverse \f$ A^+ \f$ of a real or complex matrix \f$ A \f$ using Singular Value Decomposition (SVD). The pseudo-inverse is useful for solving least-squares problems and handling singular or underdetermined systems.
Given the singular value decomposition (SVD):
\f[
A = U \Sigma V^T
\f]
the pseudo-inverse is computed as:
\f[
A^+ = V \Sigma^+ U^T
\f]
where \f$ \Sigma^+ \f$ is the inverse of the nonzero singular values in \f$ \Sigma \f$.
Arguments
a: A real or complex matrix of size \f$ [m,n] \f$, representing the input matrix to be inverted.
Return value
pinva: A real or complex matrix of size \f$ [n,m] \f$, representing the pseudo-inverse of a.
Errors
- Raises [LINALG_VALUE_ERROR](@ref la_state_type::linalg_value_error) if the SVD decomposition fails or the input matrix has invalid dimensions.
- Raises [LINALG_ERROR](@ref la_state_type::linalg_error) if numerical instability prevents inversion.
- If an error occurs, execution will stop.
Notes
- This operator behaves as [pinv](@ref la_pseudoinverse::pinv) without
rtol and err.
- The pseudo-inverse is computed using LAPACK’s SVD decomposition routine [GESVD](@ref la_lapack::gesvd).
- This operator is a convenient shorthand for calling the functional interface
pinv(a).
- The operator is
pure, so it can be used inside pure procedures and do concurrent blocks.
[svd](@ref la_svd::svd) - Singular Value Decomposition (SVD) of a matrix
Syntax
call svd(a, s [, u] [, vt] [, overwrite_a] [, full_matrices] [, err])
Description
This subroutine computes the Singular Value Decomposition (SVD) of a matrix \f$ A \f$:
\f[
A = U \cdot S \cdot V^T
\f]
where:
- \f$ A \f$ is the input matrix of size \f$ [m,n] \f$.
- \f$ U \f$ is an orthogonal matrix of size \f$ [m,m] \f$ (or \f$ [m,k] \f$ for the reduced problem), containing the left singular vectors of \f$ A \f$.
- \f$ S \f$ is a diagonal matrix containing the singular values of size \f$ [k,k] \f$ with \f$ k = \min(m,n) \f$.
- \f$ V^T \f$ is an orthogonal matrix of size \f$ [n,n] \f$ (or \f$ [k,n] \f$ for the reduced problem), containing the right singular vectors of \f$ A^T \f$.
The singular values are returned in the array \f$ S \f$, and optionally, the matrices \f$ U \f$ and \f$ V^T \f$ are computed and returned.
Arguments
a: A real matrix of size \f$ [m,n] \f$ representing the input matrix \f$ A \f$. If overwrite_a = .true., this matrix may be modified during computation. This is an inout argument.
s: A real array of size \f$ k = \min(m,n) \f$, containing the singular values of \f$ A \f$. This is an output argument.
u: An optional real matrix of the same type and kind as a, representing the left singular vectors of \f$ A \f$. This has shape \f$ [m,m] \f$ for the full problem or \f$ [m,k] \f$ for the reduced problem. This is an output argument.
vt: An optional real matrix of the same type and kind as a, representing the right singular vectors of \f$ A^T \f$. This has shape \f$ [n,n] \f$ for the full problem or \f$ [k,n] \f$ for the reduced problem. This is an output argument.
overwrite_a: (Optional, default = .false.) A logical flag indicating whether the input matrix a may be overwritten during computation. If .true., a is overwritten to avoid additional memory allocation.
full_matrices: (Optional, default = .true.) A logical flag that determines whether to compute full-sized matrices \f$ U \f$ and \f$ V^T \f$ (shape \f$ [m,m] \f$ and \f$ [n,n] \f$). If .false., computes reduced matrices of shape \f$ [m,k] \f$ and \f$ [k,n] \f$.
err: (Optional) A [type(la_state)](@ref la_state_type::la_state) variable to capture the error state. If not provided, the function will stop execution on error.
Return value
The SVD of matrix \f$ A \f$ is returned in the corresponding output arguments, with the singular values in s, and optionally, the matrices \f$ U \f$ and \f$ V^T \f$.
Errors
- Raises [LINALG_VALUE_ERROR](@ref la_state_type::linalg_value_error) if the sizes of the matrices are incompatible with the full/reduced problem.
- Raises [LINALG_ERROR](@ref la_state_type::linalg_error) if there is insufficient storage space.
- If
err is not provided, exceptions will trigger an error stop.
Notes
- This subroutine computes the Singular Value Decomposition using LAPACK’s [GESDD](@ref la_lapack::gesdd) algorithm.
- If
overwrite_a is enabled, the input matrix a may be overwritten during computation.
- This subroutine is
pure.
[svdvals](@ref la_svd::svdvals) - Singular Values Computation (function).
Syntax
s = svdvals(a)
s = svdvals(a, err)
Description
This function computes the singular values of a real or complex matrix \f$ A \f$ and returns them in a vector \f$ s \f$, where \f$ s \f$ is an array of size \f$ k = \min(m, n) \f$.
Without err it is pure; with err it returns errors instead of stopping, and is not pure.
Singular values are non-negative values that provide important insights into the properties of the matrix, such as its rank and conditioning.
This function does not compute the full Singular Value Decomposition ([SVD](@ref la_svd::svd)); instead, it directly calculates and returns only the singular values of matrix \f$ A \f$.
The Singular Value Decomposition of a matrix \f$ A \f$ is expressed as:
\f[
A = U \cdot S \cdot V^T
\f]
where:
- \f$ A \f$ is the input matrix of size \f$ [m,n] \f$,
- \f$ U \f$ and \f$ V \f$ are orthogonal matrices,
- \f$ S \f$ is a diagonal matrix with the singular values of \f$ A \f$.
Arguments
a: A real or complex matrix of size \f$ [m,n] \f$, representing the input matrix whose singular values are to be computed.
err (optional): A [type(la_state)](@ref la_state_type::la_state) variable that returns the error state. If not provided, the function will stop execution on error.
Return value
s: A real array containing the singular values of the matrix \f$ A \f$, with the same type and kind as the input matrix. The size of the array is \f$ k = \min(m, n) \f$.
Errors
- Raises [LINALG_VALUE_ERROR](@ref la_state_type::linalg_value_error) if the input matrix has invalid dimensions or if the SVD computation fails.
- If
err is not provided, exceptions will trigger an error stop.
Notes
- This function only computes the singular values and does not compute the full SVD (i.e., matrices \f$ U \f$ and \f$ V \f$ are not computed).
- The singular values are returned as a vector, sorted in decreasing order.
- The function uses LAPACK’s [GESDD](@ref la_lapack::gesdd) routine for singular value computation.
[matrix_rank](@ref la_svd::matrix_rank) - Rank of a matrix.
Syntax
r = matrix_rank(a [, rtol] [, atol])
r = matrix_rank(a [, rtol] [, atol], err)
Description
This function returns the rank of a real or complex matrix: the number of its singular values larger than \f$ \max(atol, rtol \cdot \sigma_{max}) \f$, where \f$ \sigma_{max} \f$ is the largest one. Without err it is pure; with err it returns errors instead of stopping, and is not pure.
Arguments
a: A real or complex matrix of size \f$ [m,n] \f$. It is not modified.
rtol (optional, default = \f$ \max(m,n) \cdot \epsilon \f$): The relative tolerance, a real of the kind of a, as NumPy’s default.
atol (optional, default = 0): The absolute tolerance, a real of the kind of a.
err (optional): A [type(la_state)](@ref la_state_type::la_state) variable that receives the error state.
Return value
The rank, an integer(ilp). An empty matrix has rank 0; with err, a failed computation returns 0.
Errors
- Raises [LINALG_VALUE_ERROR](@ref la_state_type::linalg_value_error) if
rtol or atol is negative.
- Raises [LINALG_ERROR](@ref la_state_type::linalg_error) if the SVD does not converge.
- If
err is not present, exceptions trigger an error stop.
Notes
- The singular values come from LAPACK’s [GESDD](@ref la_lapack::gesdd), on a copy of
a.
[cond](@ref la_norms::cond) - Condition number of a matrix.
Syntax
c = cond(a [, order])
c = cond(a [, order], err)
Description
Thi
README 内容过长,已截取前 64 KB。完整内容请前往 GitHub 仓库查看。
评论