QuantLib: Matrix Class Reference

A free/open-source library for quantitative finance Reference manual - version 1.12

Public Types | Related Functions | List of all members
Matrix Class Reference

Matrix used in linear algebra. More...

`#include <ql/math/matrix.hpp>`

Inheritance diagram for Matrix:
[legend]

## Public Types
typedef Real * | iterator
typedef const Real * | const_iterator
typedef boost::reverse_iterator< iterator > | reverse_iterator
typedef boost::reverse_iterator< const_iterator > | const_reverse_iterator
typedef Real * | row_iterator
typedef const Real * | const_row_iterator
typedef boost::reverse_iterator< row_iterator > | reverse_row_iterator
typedef boost::reverse_iterator< const_row_iterator > | const_reverse_row_iterator
typedef step_iterator< iterator > | column_iterator
typedef step_iterator< const_iterator > | const_column_iterator
typedef boost::reverse_iterator< column_iterator > | reverse_column_iterator
typedef boost::reverse_iterator< const_column_iterator > | const_reverse_column_iterator

## Public Member Functions
Constructors, destructor, and assignment
| Matrix ()
| creates a null matrix
| Matrix (Size rows, Size columns)
| creates a matrix with the given dimensions
| Matrix (Size rows, Size columns, Real value)
| creates the matrix and fills it with `value`
template<class Iterator >
| Matrix (Size rows, Size columns, Iterator begin, Iterator end)
| creates the matrix and fills it with data from a range. More...
| Matrix (const Matrix &)
| Matrix (const Disposable< Matrix > &)
Matrix & | operator= (const Matrix &)
Matrix & | operator= (const Disposable< Matrix > &)
Algebraic operators
const Matrix & | operator+= (const Matrix &)
const Matrix & | operator-= (const Matrix &)
const Matrix & | operator*= (Real)
const Matrix & | operator/= (Real)
Iterator access
const_iterator | begin () const
iterator | begin ()
const_iterator | end () const
iterator | end ()
const_reverse_iterator | rbegin () const
reverse_iterator | rbegin ()
const_reverse_iterator | rend () const
reverse_iterator | rend ()
const_row_iterator | row_begin (Size i) const
row_iterator | row_begin (Size i)
const_row_iterator | row_end (Size i) const
row_iterator | row_end (Size i)
const_reverse_row_iterator | row_rbegin (Size i) const
reverse_row_iterator | row_rbegin (Size i)
const_reverse_row_iterator | row_rend (Size i) const
reverse_row_iterator | row_rend (Size i)
const_column_iterator | column_begin (Size i) const
column_iterator | column_begin (Size i)
const_column_iterator | column_end (Size i) const
column_iterator | column_end (Size i)
const_reverse_column_iterator | column_rbegin (Size i) const
reverse_column_iterator | column_rbegin (Size i)
const_reverse_column_iterator | column_rend (Size i) const
reverse_column_iterator | column_rend (Size i)
Element access
const_row_iterator | operator[] (Size) const
const_row_iterator | at (Size) const
row_iterator | operator[] (Size)
row_iterator | at (Size)
Disposable< Array > | diagonal (void) const
Real & | operator() (Size i, Size j) const
Inspectors
Size | rows () const
Size | columns () const
bool | empty () const
Size | size1 () const
Size | size2 () const
Utilities
void | swap (Matrix &)

## Related Functions
(Note that these are not member functions.)
const Disposable< Matrix > | operator+ (const Matrix &, const Matrix &)
const Disposable< Matrix > | operator- (const Matrix &, const Matrix &)
const Disposable< Matrix > | operator* (const Matrix &, Real)
const Disposable< Matrix > | operator* (Real, const Matrix &)
const Disposable< Matrix > | operator/ (const Matrix &, Real)
const Disposable< Array > | operator* (const Array &, const Matrix &)
const Disposable< Array > | operator* (const Matrix &, const Array &)
const Disposable< Matrix > | operator* (const Matrix &, const Matrix &)
const Disposable< Matrix > | transpose (const Matrix &)
const Disposable< Matrix > | outerProduct (const Array &v1, const Array &v2)
template<class Iterator1 , class Iterator2 >
const Disposable< Matrix > | outerProduct (Iterator1 v1begin, Iterator1 v1end, Iterator2 v2begin, Iterator2 v2end)
void | swap (Matrix &, Matrix &)
std::ostream & | operator<< (std::ostream &, const Matrix &)
Disposable< Matrix > | inverse (const Matrix &m)
Real | determinant (const Matrix &m)
const Disposable< Matrix > | CholeskyDecomposition (const Matrix &m, bool flexible=false)
const Disposable< Matrix > | pseudoSqrt (const Matrix &, SalvagingAlgorithm::Type=SalvagingAlgorithm::None)
| Returns the pseudo square root of a real symmetric matrix. More...
const Disposable< Matrix > | rankReducedSqrt (const Matrix &, Size maxRank, Real componentRetainedPercentage, SalvagingAlgorithm::Type)
| Returns the rank-reduced pseudo square root of a real symmetric matrix. More...

## Detailed Description

Matrix used in linear algebra.

This class implements the concept of Matrix as used in linear algebra. As such, it is not meant to be used as a container.

## Constructor & Destructor Documentation

## ◆ Matrix()

Matrix | ( | Size | rows,
|  | Size | columns,
|  | Iterator | begin,
|  | Iterator | end
| ) |  |

creates the matrix and fills it with data from a range.

Warning:
  if the range defined by [begin, end) is larger than the size of the matrix, a memory access violation might occur. It is up to the user to avoid this.

## Member Function Documentation

## ◆ operator+=()

const Matrix & operator+= | ( | const Matrix & | m | ) |

Precondition
  all matrices involved in an algebraic expression must have the same size.

## Friends And Related Function Documentation

## ◆ operator+()

const Disposable< Matrix > operator+ | ( | const Matrix & | ,
|  | const Matrix & |
| ) |  |
| related

## ◆ operator-()

const Disposable< Matrix > operator- | ( | const Matrix & | ,
|  | const Matrix & |
| ) |  |
| related

## ◆ operator*() [1/5]

const Disposable< Matrix > operator* | ( | const Matrix & | ,
|  | Real |
| ) |  |
| related

## ◆ operator*() [2/5]

const Disposable< Matrix > operator* | ( | Real | ,
|  | const Matrix & |
| ) |  |
| related

## ◆ operator/()

const Disposable< Matrix > operator/ | ( | const Matrix & | ,
|  | Real |
| ) |  |
| related

## ◆ operator*() [3/5]

const Disposable< Array > operator* | ( | const Array & | ,
|  | const Matrix & |
| ) |  |
| related

## ◆ operator*() [4/5]

const Disposable< Array > operator* | ( | const Matrix & | ,
|  | const Array & |
| ) |  |
| related

## ◆ operator*() [5/5]

const Disposable< Matrix > operator* | ( | const Matrix & | ,
|  | const Matrix & |
| ) |  |
| related

## ◆ transpose()

const Disposable< Matrix > transpose | ( | const Matrix & |  | ) |
| related

## ◆ outerProduct() [1/2]

const Disposable< Matrix > outerProduct | ( | const Array & | v1,
|  | const Array & | v2
| ) |  |
| related

## ◆ outerProduct() [2/2]

const Disposable< Matrix > outerProduct | ( | Iterator1 | v1begin,
|  | Iterator1 | v1end,
|  | Iterator2 | v2begin,
|  | Iterator2 | v2end
| ) |  |
| related

## ◆ swap()

void swap | ( | Matrix & | ,
|  | Matrix & |
| ) |  |
| related

## ◆ operator<<()

std::ostream & operator<< | ( | std::ostream & | ,
|  | const Matrix & |
| ) |  |
| related

## ◆ inverse()

Disposable< Matrix > inverse | ( | const Matrix & | m | ) |
| related

## ◆ determinant()

Real determinant | ( | const Matrix & | m | ) |
| related

## ◆ CholeskyDecomposition()

const Disposable< Matrix > CholeskyDecomposition | ( | const Matrix & | m,
|  | bool | flexible = `false`
| ) |  |
| related

## ◆ pseudoSqrt()

const Disposable< Matrix > pseudoSqrt | ( | const Matrix & | ,
|  | SalvagingAlgorithm::Type | = `SalvagingAlgorithm::None`
| ) |  |
| related

Returns the pseudo square root of a real symmetric matrix.

Given a matrix \( M \), the result \( S \) is defined as the matrix such that \( S S^T = M. \) If the matrix is not positive semi definite, it can return an approximation of the pseudo square root using a (user selected) salvaging algorithm.

For more information see: R. Rebonato and P. Jäckel, The most general methodology to create a valid correlation matrix for risk management and option pricing purposes, The Journal of Risk, 2(2), Winter 1999/2000. http://www.rebonato.com/correlationmatrix.pdf

Revised and extended in "Monte Carlo Methods in Finance", by Peter Jäckel, Chapter 6.

Precondition
  the given matrix must be symmetric.

Warning:
  Higham algorithm only works for correlation matrices.

Tests:
  - the correctness of the results is tested by reproducing known good data.
  - the correctness of the results is tested by checking returned values against numerical calculations.

## ◆ rankReducedSqrt()

const Disposable< Matrix > rankReducedSqrt | ( | const Matrix & | ,
|  | Size | maxRank,
|  | Real | componentRetainedPercentage,
|  | SalvagingAlgorithm::Type |
| ) |  |
| related

Returns the rank-reduced pseudo square root of a real symmetric matrix.

The result matrix has rank<=maxRank. If maxRank>=size, then the specified percentage of eigenvalues out of the eigenvalues' sum is retained.

If the input matrix is not positive semi definite, it can return an approximation of the pseudo square root using a (user selected) salvaging algorithm.

Precondition
  the given matrix must be symmetric.