To solve the matrix equation Ay = b where A is a square matrix of equation coefficients, y is a column vector of values to be solved for, and b is a column vector, use the code
int n = something
Matrix A(n,n); ColumnVector b(n);
... put values in A and b
ColumnVector y = A.i() * b; // solves matrix equation
The following notes are for the case where you want to solve more than
one matrix equation with different values of b but the same
A. Or where
you want to solve a matrix equation and also find the determinant of
A.
In these cases you probably want to avoid repeating the LU decomposition of
A
for each solve or determinant calculation.
If A is a square or symmetric matrix use
CroutMatrix X = A; // carries out LU decomposition
Matrix AP = X.i()*P; Matrix AQ = X.i()*Q;
LogAndSign ld = X.LogDeterminant();
rather than
Matrix AP = A.i()*P; Matrix AQ = A.i()*Q;
LogAndSign ld = A.LogDeterminant();
since each operation will repeat the LU decomposition.
If A is a BandMatrix or a SymmetricBandMatrix begin with
BandLUMatrix X = A; // carries out LU decomposition
A CroutMatrix or a BandLUMatrix can't be manipulated or
copied. Use
references as an alternative to copying.
Alternatively use
LinearEquationSolver X = A;
This will choose the most appropriate decomposition of A. That is, the
band form if A is banded; the Crout decomposition if A is
square or symmetric and no decomposition if A is triangular or
diagonal. If you want to use the LinearEquationSolver #include
newmatap.h.