Armadillo provides various functions to solve linear systems of equations, including the Moore-Penrose pseudoinverse and LU decomposition.
1. Moore-Penrose Pseudoinverse: The
pinv()
function computes the pseudoinverse of a matrix, which can be used to solve overdetermined or underdetermined systems. Example:
#include <armadillo>
using namespace arma;
mat A = randu<mat>(4, 3);
vec b = randu<vec>(4);
mat A_pinv = pinv(A);
vec x = A_pinv * b;
2. LU Decomposition: The
solve()
function uses LU decomposition by default for square matrices. It factors the input matrix into lower (L) and upper (U) triangular matrices. Example:
#include <armadillo>
using namespace arma;
mat A = randu<mat>(3, 3);
vec b = randu<vec>(3);
vec x = solve(A, b);
3. Cholesky Decomposition: For positive definite matrices, use the
chol()
function to perform Cholesky decomposition. Example:
#include <armadillo>
using namespace arma;
mat A = randu<mat>(3, 3);
A = A.t() * A; // Make it positive definite
vec b = randu<vec>(3);
mat L = chol(A);
vec y = solve(trimatl(L), b);
vec x = solve(trimatu(L.t()), y);
4. QR Decomposition: Use the
qr()
function for least squares problems. Example:
#include <armadillo>
using namespace arma;
mat A = randu<mat>(4, 3);
vec b = randu<vec>(4);
mat Q, R;
qr(Q, R, A);
vec x = solve(R, Q.t() * b);