Matrix Inverse with C++ Tables

1. Reading the matrix from the input

The following code reads the matrix M of format \(3\times 3\) from the top-left corner of the input matrix \(A\).

std::vector<std::vector<double> > M;
M=LALG::subMatrix(A,0,0,3,3);

The function LALG::subMatrix has five arguments. The first argument is the big matrix from which the sub-matrix is extracted. In this case, the big matrix is the input matrix \(A\). The next two arguments are the coordinates of the top-left corner from which the submatrix will be extracted. The third argument is the number of rows, and the fourth argument is the number of columns of the submatrix.

2. Calculating the inverse

The matrix inverse is obtained by calling the function LALG::inverse.

std::vector<std::vector<double> > M_inv;
M_inv=LALG::inverse(M);

3. Storing the result in the output table B

The entries of the matrix M_inv need to be placed in the output table \(B\). Otherwise, we won't see the results of our calculation.

LALG::copySubMatrix(B,M_inv,0,0);

The last two arguments are the coordinates of the top-left corner in the destination B where we wish to put the matrix M_inv.

4. Final code

std::vector<std::vector<double> > M;
M=LALG::subMatrix(A,0,0,3,3);
std::vector<std::vector<double> > M_inv;
M_inv=LALG::inverse(M);
LALG::copySubMatrix(B,M_inv,0,0);

Here is one example of how to fill out the input table:

0 1 2 3 4 5 6
0 73-4 0000
1 586 0000
2 -490 0000
3 000 0000
4 000 0000

If we now press the button Calculate/Display, we will obtain \begin{eqnarray*}\left[\begin{array}{ccc}7&3&-4\\5&8&6\\-4&9&0\end{array}\right]^{-1}&=& \left[\begin{array}{rrr}0.07124011&0.04749340&-0.06596306\\ 0.03166227&0.02110818&0.08179420\\ -0.10158311&0.09894459&-0.05408971\end{array}\right] .\end{eqnarray*} The inverse will be placed in the top-left \(3\times 3\) submatrix of the output table \(B\).