| 1. | Introduction |
| 2. | Binomial model |
| 3. | Options |
| 4. | American puts |
| 5. | Systems of equations |
| 6. | Matrix operations |
| 7. | Matrix inverse |
Systems of Equations with C++ Tables
1. Matrix form of the system
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*}
2. Vectors and matrices in C++ tables
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> >.
2.1. Reading the vector from the input
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=LALG::columnToVector(A,0,5,3);
The arguments 0 and 5 specify the top left corner of the input matrix from where the copying will start. The last argument, 3, is the desired length of the vector.
2.2. 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.3. Solving the system
The system is solved with the following code:
std::vector<double> x=LALG::solve(M,b);
2.4. Storing the result in the output table B
The result can be stored in the output table \(B\) using the code below:
if(x.size()<1){
// multiple solutions or no solutions
B[0][1]=-1;
}
else{
LALG::copyVectorToColumn(B,x,0,0);
}
3. Final code
std::vector<double> b=LALG::columnToVector(A,0,5,3);
std::vector<std::vector<double> > M=LALG::subMatrix(A,0,0,3,3);
std::vector<double> x=LALG::solve(M,b);
if(x.size()<1){
// multiple solutions or no solutions
B[0][1]=-1;
}
else{
LALG::copyVectorToColumn(B,x,0,0);
}
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.