The system of equations \begin{eqnarray*} a_{11}x_1 + a_{12}x_2+\cdots+a_{1n}x_n&=&b_1\\ a_{21}x_1+a_{22}x_2+\cdots+ a_{2n}x_n&=&b_2\\ &\vdots& \\ a_{n1}x_1+a_{n2}x_2+\cdots+a_{nn}x_n&=&b_n \end{eqnarray*} can be written in the matrix form \(M\overrightarrow x=\overrightarrow b\), where the matrix \(M\) and the vectors \(\overrightarrow x\) and \(\overrightarrow b\) are defined in the following way \begin{eqnarray*}M&=& \left[\begin{array}{cccc} a_{11}&a_{12}&\cdots&a_{1n}\\ a_{21}&a_{22}&\cdots&a_{2n}\\ \vdots&\vdots&\ddots&\vdots\\ a_{n1}&a_{n2}&\cdots&a_{nn}\end{array}\right],\quad \overrightarrow x=\left[\begin{array}{c}x_1\\x_2\\ \vdots\\x_n\end{array}\right],\quad \overrightarrow b = \left[\begin{array}{c}b_1\\b_2\\ \vdots\\b_n\end{array}\right]. \end{eqnarray*}
If we want to solve the system \(M\overrightarrow x=\overrightarrow b\) using C++ tables, we first need to get the vector \(\overrightarrow b\) and the matrix \(M\) from the input table \(A\).
The vector \(\overrightarrow b\) has to be of the type std::vector<double>. The matrix \(M\) has to be of type std::vector<std::vector<double> >.
The following code reads the vector b of dimension \(3\) from the cells A[0][5], A[1][5], and A[2][5].
std::vector<double> b;
b.resize(3);
for(long i=0;i<3;++i){
b[i]=A[i][5];
}
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.
Before we solve the system, we declare the vector x in which we will store the solution. Then, we call the function LARG::solve.
std::vector<double> x; LALG::solve(M,b,x);
The result can be stored in the output table \(B\) using the following code.
for(long i=0;i<3;++i){
B[i][0]=x[i];
}
std::vector<double> b;
b.resize(3);
for(long i=0;i<3;++i){
b[i]=A[i][5];
}
std::vector<std::vector<double> > M;
M=LALG::subMatrix(A,0,0,3,3);
std::vector<double> x;
LALG::solve(M,b,x);
for(long i=0;i<3;++i){
B[i][0]=x[i];
}
Here is one example of how to fill out the input table:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | |
| 0 | 7 | 3 | -4 | 0 | 0 | 4 | 0 |
| 1 | 5 | 8 | 6 | 0 | 0 | 53 | 0 |
| 2 | -4 | 9 | 0 | 0 | 0 | -3 | 0 |
| 3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
If we now press the button Calculate/Display we will obtain the solution to the system
\begin{eqnarray*}\left[\begin{array}{ccc}7&3&-4\\5&8&6\\-4&9&0\end{array}\right]\cdot\left[\begin{array}{c}x_1\\x_2\\x_3\end{array}\right]&=&
\left[\begin{array}{c}4\\53\\-3\end{array}\right].\end{eqnarray*}
The solution is \[\left[\begin{array}{c}x_1\\x_2\\x_3\end{array}\right]=\left[\begin{array}{c}3\\1\\5\end{array}\right]\] and it will be displayed in the cells \(B[0][0]\), \(B[1][0]\), and \(B[2][0]\) of the output table.