numerics
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/// Partial pivoting (swap largest element to pivot position) ensures
33/// |L(i,j)| <= 1 for all i > j, which bounds round-off growth.
34LUResult lu(const Matrix& A);
35
36/// @brief Solve A*x = b using a precomputed LU factorization.
37///
38/// Three steps:
39/// 1. Apply permutation: y = P*b
40/// 2. Forward substitution: solve L*z = y
41/// 3. Backward substitution: solve U*x = z
42void lu_solve(const LUResult& f, const Vector& b, Vector& x);
43
44/// @brief Solve A*X = B for multiple right-hand sides.
45///
46/// Each column of B is solved independently using the same factorization.
47/// B and X are nxnrhs matrices (column-major access pattern).
48void lu_solve(const LUResult& f, const Matrix& B, Matrix& X);
49
50/// @brief Determinant of A from its LU factorization.
51///
52/// det(A) = det(P)^{-1} * prod(U[i,i])
53/// = (-1)^{swaps} * prod(U[i,i])
54///
55/// Overflow is possible for large n -- use log-determinant for stability.
56real lu_det(const LUResult& f);
57
58/// @brief Inverse of A from its LU factorization.
59///
60/// Solves A * X = I column by column. O(n^3) -- only use when the full
61/// inverse is genuinely needed (prefer lu_solve for specific right-hand sides).
62Matrix lu_inv(const LUResult& f);
63
64} // namespace num
Dense row-major matrix with optional GPU storage.
Definition matrix.hpp:12
Matrix operations.
double real
Definition types.hpp:10
LUResult lu(const Matrix &A)
LU factorization of a square matrix A with partial pivoting.
Definition lu.cpp:21
constexpr T ipow(T x) noexcept
Compute x^N at compile time via repeated squaring.
real lu_det(const LUResult &f)
Determinant of A from its LU factorization.
Definition lu.cpp:109
Matrix lu_inv(const LUResult &f)
Inverse of A from its LU factorization.
Definition lu.cpp:122
void lu_solve(const LUResult &f, const Vector &b, Vector &x)
Solve A*x = b using a precomputed LU factorization.
Definition lu.cpp:67
Backend enum for linear algebra operations.
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