In this post, we shall see some basic operations of matrix algebra, namely addition and multiplication operations, matrix transposition, and finding the inverse matrix. The calculation results we obtain here can be checked by using Excel.
Our pieces below of codes start by declaring the matrice sizes, Matrix M ( Size Rows, Size Columns). The matrices are then populated. As an output, we print out on the screen the actual matrices and the calculation results.
Example 1 : adding two matrices
Do recall that only matrices of the same dimension can be added (subtracted) element by element.
#include<ql\quantlib.hpp> using namespace QuantLib; int main(int, char*[]){ // Matrix declaration and population Matrix A(2,2); A[0][0] = 1; A[0][1] = 2; A[1][0] = 3; A[1][1] = 4; Matrix B(2,2); B[0][0] = 5; B[0][1] = 6; B[1][0] = 7; B[1][1] = 8; // Outputting std::cout << "Matrix A :" << std::endl << A << std::endl; std::cout << "Matrix B :" << std::endl << B << std::endl; std::cout << "A + B :" << std::endl << A + B << std::endl; return 0; }
Example 2 : matrix multiplication
#include<ql\quantlib.hpp> using namespace QuantLib; int main(int, char*[]){ // Matrix declaration and population Matrix A(2,3); A[0][0] = 0; A[0][1] = 1; A[0][2] = -1; A[1][0] = 1; A[1][1] = 2; A[1][2] = 0; Matrix B(3,4); B[0][0] = 1; B[0][1] = 0; B[0][2] = 2; B[0][3] = 1; B[1][0] = 0; B[1][1] = 0; B[1][2] = 1; B[1][3] = -1; B[2][0] = 2; B[2][1] = -1; B[2][2] = 0; B[2][3] = 2; // Outputting std::cout << "Matrix A :" << std::endl << A << std::endl; std::cout << "Matrix B :" << std::endl << B << std::endl; std::cout << "A x B product :" << std::endl << A * B << std::endl; return 0; }
Example 3 : matrix transposition, inversion, and determinant
Let a standard matrix A be. We wish to get the transposition matrix A’, the inverted matrix A-1, and its determinant det (A).
#include<ql\quantlib.hpp> using namespace QuantLib; int main(int, char*[]){ // Matrix declaration and population Matrix A(3,3); A[0][0] = 1; A[0][1] = 2; A[0][2] = 3; A[1][0] = 4; A[1][1] = 5; A[1][2] = 6; A[2][0] = 7; A[2][1] = 8; A[2][2] = 9; // Outputting std::cout << "Matrix A :" << std::endl << A << std::endl; std::cout << "Matrix transpose :" << std::endl << transpose(A) << std::endl; std::cout << "Matrix determinant :" << std::endl << determinant(A) << std::endl; std::cout << "Matrix inverse :" << std::endl << inverse(A) << std::endl; return 0; }
Matrix = A(n,n)
how to get hold of the 1st column of A? I know that A[1] gives the first row.
Hello F.T. Thank you for visiting my blog site. Please, have a look at the Matrix Class Reference, here : http://quantlib.org/reference/class_quant_lib_1_1_matrix.html .
Thank you Edouard,
I have already looked at this site, but cannot quite understand. my question is actually very simple. In Matlab for instance:
A = rand(3); gives you a random matrix of size 3*3
A(:,1) gives you the first column of this matrix as a column vector and
A(1,:) gives you the first row of this matrix as a row vector.
That is what I am looking for in a Quantlibmatrix. I don’t quite understand the business with iterators.
if you define A = Matrix(3,3) ( filled with some elements later)
A.column_begin (1) does that give you the first column? but as what? as a matrix, array or iterator. How should I declare it?
Mat(3,1) firstcolumn = A.column_begin (1) ; ??
Please include some sample code!
thanks,
FT
FT
Hello FT. In case you don’t feel at ease with the Matrix class in QuantLib, you can get any row/columns by creating a standard C++ for loop.
Example : to get the 3nd column of a C(3,3) matrix (indexes of rows and columns start at 0)
Hello,
Any suggestion about how to do matrix-array multiplication in QuantLib? Shall I just define the array to be a row-1 or column-1 matrix?
Best Regards
Hello lesleyedwardsud. Thank you for visiting this blog.
Did you try with the array class of QuantLib (http://quantlib.org/reference/class_quant_lib_1_1_array.html)?
I get it, thanks so much!
I thought it was «vector»…
You’re welcome! The QL Array class is different from the std::vector. QuantLib::Array is purposefully made for algebra.
I cut and pasted example 3 verbatim, but when I executed it, it threw an exception on the matrix inversion:
Unhandled exception at 0x773f15de in test.exe: Microsoft C++ exception: boost::numeric::ublas::internal_logic at memory location 0x0032f2b0..
Hi wsevena.
Thank you for visiting this page.
The code read here actually are the piece of codes I compiled.
Did you get similar errors with the 2 other example code above? Which compiler do you use?
Best
Édouard
Hi Edouard,
The other pieces of code work fine for me. I am using Microsoft Visual C++ 2010 Express with Quantlib 1.51.0. I think the problem may be an ill-conditioned matrix. After I changed a couple of the numbers in the matrix, the example then worked.
Oops, I meant quantlib 1.2.1 and Boost 1.51.
Hi.
I now understand what’s wrong with the 3rd example code.
As you suggest, this is a problem with the properties of the matrix. In fact the square matrix A in this code has a determinant equal to 0. That means it is a singular matrix i.e. it is not invertible. An error is then sent as the program is asked to compute the inverse of A.
That’s strange I am pretty sure that the code examples in these pages are the original ones. …
Anyway, thank you very much for your comments.
I really appreciate. Feel free to post your own pieces of code, here.
Best,
Édouard.