| 1. | Introduction |
| 2. | Binomial model |
| 3. | Options |
| 4. | American puts |
| 5. | Systems of equations |
| 6. | Matrix operations |
| 7. | Matrix inverse |
Basic Matrix Operations with C++ Tables
1. Input and output
1.1. Matrix input
All data can be received from the input matrix A, which is of format \(20\times 20\). The output is stored in the matrix B, also of format \(20\times 20\).
Assume that we want to read a \(3\times 4\) matrix M from the cells
A[0][0] A[0][1] A[0][2] A[0][3] A[1][0] A[1][1] A[1][2] A[0][3] A[2][0] A[2][1] A[2][2] A[0][3]
The input A was of format \(20\times 20\) and might have contained other data that should not be part of M. In order to make sure that M is of format \(3\times 4\) and that it is read from A starting at the top left corner, we give the following instruction:
std::vector<std::vector<double> > M=LALG::subMatrix(A,0,0,3,4);
The last two arguments, 3 and 4, represent the format of the matrix M. The matrix M has 3 rows and 4 columns. The numbers 0 and 0 represent the top-left corner in the input matrix from where the reading should start.
If we wanted a sub matrix of A whose top left corner has coordinates 5 and 0, then we can use the following instruction:
std::vector<std::vector<double> > N=LALG::subMatrix(A,5,0,3,4);
1.2. Vector input
Row vectors are matrices of format \(n\times 1\). Column vectors are matrices of format \(1\times n\). Matrices are more general objects than vectors.
In C++, vectors are more basic data types than matrices. Vectors are arrays, and matrices are vectors of vectors.
Since the C++ tables framework supports matrices, all operations on vectors can be achieved by working with matrices. However, the C++ tables provide the convenience of vectors.
The following code reads the vector x of dimension \(3\) from the cells A[0][5], A[1][5], and A[2][5].
std::vector<double> x=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.
1.3. Matrix output
Let us work with an example. Let us start with our matrix M from the subsection 1.1. The matrix was of format \(3\times 4\). We can create its transpose MT and place it in the output matrix B in such a way that its top-left corner is at the cell (0,1):
LALG::copySubMatrix(B,MT,0,1);
1.4. Vector output
The vector can be placed either in row or in a column of the output matrix B
LALG::copyVectorToRow(B,z,10,0); LALG::copyVectorToColumn(B,z,10,5);
The first of the above two commands will place the vector z in the cells B[10][0], B[10][1], B[10][2], ...
The second command will place the vector z in the cells B[10][5], B[11][5], B[12][5], ...
2. Linear operations
A matrix can be multiplied by a scalar. If alpha is a real number and M a matrix, then the following instruction will create a matrix Q of the same format as A that satisfies \(Q=\alpha A\):
std::vector<std::vector<double> > Q = alpha*M;
Note: Just as in linear algebra, the scalar must be placed to the left of the matrix. Hence, alpha*M a valid operation, while M*alpha is not.
Similarly, a vector can be multiplied by a scalar.
Vectors of the same length can be added with operation +. Matrices of the same format can be added with operation +. The following code provides an example of matrix additions and vector additions.
std::vector<std::vector<double> > M=LALG::subMatrix(A,0,0,3,4); double alpha=100.0; std::vector<std::vector<double> > Q = alpha*M; LALG::copySubMatrix(B,Q,0,0); std::vector<std::vector<double> > N=LALG::subMatrix(A,5,0,3,4); std::vector<std::vector<double> > R=(100.0*M)-N; LALG::copySubMatrix(B,R,5,2); std::vector<double> x=LALG::columnToVector(A,0,5,3); std::vector<double> y=LALG::rowToVector(A,4,5,3); std::vector<double> z=(100.0*x)+y; LALG::copyVectorToRow(B,z,10,0); LALG::copyVectorToColumn(B,z,10,5);
3. Matrix multiplication
If \(V\) is a matrix of the format \(p\times q\) and \(W\) a matrix of the format \(q\times r\), then their product \(VW\) is a matrix of the format \(p\times r\) whose component \((i,j)\) is defined as
\begin{align} (VW)_{ij}&=\sum_{c} V_{ic}W_{cj}.\nonumber \end{align}In C++ tables, the function LALG::mMult can be used to calculate the product of matrices. Alternatively, we can use the operator *. The following code shows how the product of matrices can be calculated in two different ways. The obtained matrices P1 and P2 will have the same entries.
std::vector<std::vector<double> > V=LALG::subMatrix(A,0,0,3,4); std::vector<std::vector<double> > W=LALG::subMatrix(A,0,5,4,2); std::vector<std::vector<double> > P1=LALG::mMult(V,W); LALG::copySubMatrix(B,P1,0,0); std::vector<std::vector<double> > P2=V*W; LALG::copySubMatrix(B,P2,5,0);