Simple matrix algebra with QuantLib

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;
}


13 pensamientos en “Simple matrix algebra with QuantLib

      1. F.T.

        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

  1. édouard Autor de la entrada

    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)

    for (int x = 0; C.rows() - 1 ; x++){
    	std::cout << C[x][2] << std::endl;}
    Responder
      1. édouard Autor de la entrada

        You’re welcome! The QL Array class is different from the std::vector. QuantLib::Array is purposefully made for algebra.

  2. wstevena

    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..

    Responder
    1. édouard Autor de la entrada

      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

      Responder
  3. wstevena

    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.

    Responder
      1. édouard Autor de la entrada

        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.

Responder

Por favor, inicia sesión con uno de estos métodos para publicar tu comentario:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s