numerics 0.1.0
Loading...
Searching...
No Matches
lu.hpp
Go to the documentation of this file.
1/// @file lu.hpp
2/// @brief LU factorization with partial pivoting
3#pragma once
4
5#include "core/matrix.hpp"
6#include "core/policy.hpp"
7#include <vector>
8
9namespace num {
10
11/// @brief Result of an LU factorization with partial pivoting (PA = LU)
12///
13/// L and U are stored packed in a single matrix:
14/// - U occupies the upper triangle (including the diagonal)
15/// - L occupies the strict lower triangle (diagonal of L is implicitly 1)
16///
17/// piv[k] = the row index that was swapped into position k at step k.
18/// singular is set if any diagonal of U is below the tolerance 1e-14.
19struct LUResult {
21 std::vector<idx> piv;
22 bool singular = false;
23};
24
25/// @brief LU factorization of a square matrix A with partial pivoting.
26///
27/// Computes P, L, U such that P*A = L*U where:
28/// P = permutation matrix encoded in piv
29/// L = unit lower triangular (diagonal = 1, stored below diag of LU)
30/// U = upper triangular (stored on and above diag of LU)
31///
32/// @param backend Backend::lapack uses LAPACKE_dgetrf (default when
33/// available).
34/// Backend::seq uses our Doolittle implementation.
35LUResult lu(const Matrix &A, Backend backend = lapack_backend);
36
37/// @brief Solve A*x = b using a precomputed LU factorization.
38///
39/// Three steps:
40/// 1. Apply permutation: y = P*b
41/// 2. Forward substitution: solve L*z = y
42/// 3. Backward substitution: solve U*x = z
43void lu_solve(const LUResult &f, const Vector &b, Vector &x);
44
45/// @brief Solve A*X = B for multiple right-hand sides.
46///
47/// Each column of B is solved independently using the same factorization.
48/// B and X are nxnrhs matrices (column-major access pattern).
49void lu_solve(const LUResult &f, const Matrix &B, Matrix &X);
50
51/// @brief Determinant of A from its LU factorization.
52///
53/// det(A) = det(P)^{-1} * prod(U[i,i])
54/// = (-1)^{swaps} * prod(U[i,i])
55///
56/// Overflow is possible for large n -- use log-determinant for stability.
57real lu_det(const LUResult &f);
58
59/// @brief Inverse of A from its LU factorization.
60///
61/// Solves A * X = I column by column. O(n^3) -- only use when the full
62/// inverse is genuinely needed (prefer lu_solve for specific right-hand sides).
63Matrix lu_inv(const LUResult &f);
64
65} // namespace num
Dense row-major matrix with optional GPU storage.
Definition matrix.hpp:12
Backend enum for linear algebra operations.
Matrix operations.
double real
Definition types.hpp:10
Backend
Selects which backend handles a linalg operation.
Definition policy.hpp:19
real lu_det(const LUResult &f)
Determinant of A from its LU factorization.
Definition lu.cpp:66
constexpr Backend lapack_backend
Definition policy.hpp:124
Matrix lu_inv(const LUResult &f)
Inverse of A from its LU factorization.
Definition lu.cpp:80
void lu_solve(const LUResult &f, const Vector &b, Vector &x)
Solve A*x = b using a precomputed LU factorization.
Definition lu.cpp:28
LUResult lu(const Matrix &A, Backend backend=lapack_backend)
LU factorization of a square matrix A with partial pivoting.
Definition lu.cpp:17
Result of an LU factorization with partial pivoting (PA = LU)
Definition lu.hpp:19
Matrix LU
Definition lu.hpp:20
std::vector< idx > piv
Definition lu.hpp:21
bool singular
Definition lu.hpp:22