QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
Related Functions | List of all members
Matrix Class Reference

Matrix used in linear algebra. More...

#include <matrix.hpp>

+ Collaboration diagram for Matrix:

Public Member Functions

Constructors, destructor, and assignment
 Matrix ()
 creates a null matrix More...
 
 Matrix (Size rows, Size columns)
 creates a matrix with the given dimensions More...
 
 Matrix (Size rows, Size columns, Real value)
 creates the matrix and fills it with value More...
 
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 (Matrix &&) noexcept
 
 Matrix (std::initializer_list< std::initializer_list< Real > >)
 
 ~Matrix ()=default
 
Matrixoperator= (const Matrix &)
 
Matrixoperator= (Matrix &&) noexcept
 
bool operator== (const Matrix &) const
 
bool operator!= (const Matrix &) const
 
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)
 
Array diagonal () const
 
Realoperator() (Size i, Size j) const
 
Inspectors
Size rows () const
 
Size columns () const
 
bool empty () const
 
Size size1 () const
 
Size size2 () const
 

Related Functions

(Note that these are not member functions.)

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

Algebraic operators

typedef Realiterator
 
typedef const Realconst_iterator
 
typedef std::reverse_iterator< iteratorreverse_iterator
 
typedef std::reverse_iterator< const_iteratorconst_reverse_iterator
 
typedef Realrow_iterator
 
typedef const Realconst_row_iterator
 
typedef std::reverse_iterator< row_iteratorreverse_row_iterator
 
typedef std::reverse_iterator< const_row_iteratorconst_reverse_row_iterator
 
typedef step_iterator< iteratorcolumn_iterator
 
typedef step_iterator< const_iteratorconst_column_iterator
 
typedef std::reverse_iterator< column_iteratorreverse_column_iterator
 
typedef std::reverse_iterator< const_column_iteratorconst_reverse_column_iterator
 
const Matrixoperator+= (const Matrix &)
 
const Matrixoperator-= (const Matrix &)
 
const Matrixoperator*= (Real)
 
const Matrixoperator/= (Real)
 

Utilities

std::unique_ptr< Real[]> data_
 
Size rows_ = 0
 
Size columns_ = 0
 
void swap (Matrix &) noexcept
 

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.

Definition at line 41 of file matrix.hpp.

Member Typedef Documentation

◆ iterator

typedef Real* iterator

Definition at line 81 of file matrix.hpp.

◆ const_iterator

typedef const Real* const_iterator

Definition at line 82 of file matrix.hpp.

◆ reverse_iterator

typedef std::reverse_iterator<iterator> reverse_iterator

Definition at line 83 of file matrix.hpp.

◆ const_reverse_iterator

typedef std::reverse_iterator<const_iterator> const_reverse_iterator

Definition at line 84 of file matrix.hpp.

◆ row_iterator

typedef Real* row_iterator

Definition at line 85 of file matrix.hpp.

◆ const_row_iterator

typedef const Real* const_row_iterator

Definition at line 86 of file matrix.hpp.

◆ reverse_row_iterator

typedef std::reverse_iterator<row_iterator> reverse_row_iterator

Definition at line 87 of file matrix.hpp.

◆ const_reverse_row_iterator

typedef std::reverse_iterator<const_row_iterator> const_reverse_row_iterator

Definition at line 89 of file matrix.hpp.

◆ column_iterator

Definition at line 90 of file matrix.hpp.

◆ const_column_iterator

Definition at line 91 of file matrix.hpp.

◆ reverse_column_iterator

typedef std::reverse_iterator<column_iterator> reverse_column_iterator

Definition at line 93 of file matrix.hpp.

◆ const_reverse_column_iterator

typedef std::reverse_iterator<const_column_iterator> const_reverse_column_iterator

Definition at line 95 of file matrix.hpp.

Constructor & Destructor Documentation

◆ Matrix() [1/7]

Matrix ( )

creates a null matrix

Definition at line 222 of file matrix.hpp.

◆ Matrix() [2/7]

Matrix ( Size  rows,
Size  columns 
)

creates a matrix with the given dimensions

Definition at line 224 of file matrix.hpp.

◆ Matrix() [3/7]

Matrix ( Size  rows,
Size  columns,
Real  value 
)

creates the matrix and fills it with value

Definition at line 228 of file matrix.hpp.

+ Here is the call graph for this function:

◆ Matrix() [4/7]

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.

Definition at line 235 of file matrix.hpp.

+ Here is the call graph for this function:

◆ Matrix() [5/7]

Matrix ( const Matrix from)

Definition at line 241 of file matrix.hpp.

+ Here is the call graph for this function:

◆ Matrix() [6/7]

Matrix ( Matrix &&  from)
noexcept

Definition at line 250 of file matrix.hpp.

+ Here is the call graph for this function:

◆ Matrix() [7/7]

Matrix ( std::initializer_list< std::initializer_list< Real > >  data)

Definition at line 255 of file matrix.hpp.

+ Here is the call graph for this function:

◆ ~Matrix()

~Matrix ( )
default

Member Function Documentation

◆ operator=() [1/2]

Matrix & operator= ( const Matrix from)

Definition at line 270 of file matrix.hpp.

+ Here is the call graph for this function:

◆ operator=() [2/2]

Matrix & operator= ( Matrix &&  from)
noexcept

Definition at line 277 of file matrix.hpp.

+ Here is the call graph for this function:

◆ operator==()

bool operator== ( const Matrix to) const

Definition at line 282 of file matrix.hpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ operator!=()

bool operator!= ( const Matrix to) const

Definition at line 287 of file matrix.hpp.

+ Here is the call graph for this function:

◆ operator+=()

const Matrix & operator+= ( const Matrix m)
Precondition
all matrices involved in an algebraic expression must have the same size.

Definition at line 297 of file matrix.hpp.

+ Here is the call graph for this function:

◆ operator-=()

const Matrix & operator-= ( const Matrix m)

Definition at line 307 of file matrix.hpp.

+ Here is the call graph for this function:

◆ operator*=()

const Matrix & operator*= ( Real  x)

Definition at line 317 of file matrix.hpp.

+ Here is the call graph for this function:

◆ operator/=()

const Matrix & operator/= ( Real  x)

Definition at line 322 of file matrix.hpp.

+ Here is the call graph for this function:

◆ begin() [1/2]

Matrix::const_iterator begin ( ) const

Definition at line 327 of file matrix.hpp.

+ Here is the caller graph for this function:

◆ begin() [2/2]

Matrix::iterator begin ( )

Definition at line 331 of file matrix.hpp.

◆ end() [1/2]

Matrix::const_iterator end ( ) const

Definition at line 335 of file matrix.hpp.

+ Here is the caller graph for this function:

◆ end() [2/2]

Definition at line 339 of file matrix.hpp.

◆ rbegin() [1/2]

Definition at line 343 of file matrix.hpp.

+ Here is the call graph for this function:

◆ rbegin() [2/2]

Definition at line 347 of file matrix.hpp.

+ Here is the call graph for this function:

◆ rend() [1/2]

Definition at line 351 of file matrix.hpp.

+ Here is the call graph for this function:

◆ rend() [2/2]

Definition at line 355 of file matrix.hpp.

+ Here is the call graph for this function:

◆ row_begin() [1/2]

Matrix::const_row_iterator row_begin ( Size  i) const

Definition at line 360 of file matrix.hpp.

+ Here is the caller graph for this function:

◆ row_begin() [2/2]

Matrix::row_iterator row_begin ( Size  i)

Definition at line 369 of file matrix.hpp.

◆ row_end() [1/2]

Matrix::const_row_iterator row_end ( Size  i) const

Definition at line 378 of file matrix.hpp.

+ Here is the caller graph for this function:

◆ row_end() [2/2]

Matrix::row_iterator row_end ( Size  i)

Definition at line 387 of file matrix.hpp.

◆ row_rbegin() [1/2]

Matrix::const_reverse_row_iterator row_rbegin ( Size  i) const

Definition at line 397 of file matrix.hpp.

+ Here is the call graph for this function:

◆ row_rbegin() [2/2]

Definition at line 401 of file matrix.hpp.

+ Here is the call graph for this function:

◆ row_rend() [1/2]

Definition at line 406 of file matrix.hpp.

+ Here is the call graph for this function:

◆ row_rend() [2/2]

Definition at line 410 of file matrix.hpp.

+ Here is the call graph for this function:

◆ column_begin() [1/2]

Matrix::const_column_iterator column_begin ( Size  i) const

Definition at line 415 of file matrix.hpp.

+ Here is the caller graph for this function:

◆ column_begin() [2/2]

Matrix::column_iterator column_begin ( Size  i)

Definition at line 424 of file matrix.hpp.

◆ column_end() [1/2]

Matrix::const_column_iterator column_end ( Size  i) const

Definition at line 434 of file matrix.hpp.

+ Here is the caller graph for this function:

◆ column_end() [2/2]

Matrix::column_iterator column_end ( Size  i)

Definition at line 443 of file matrix.hpp.

◆ column_rbegin() [1/2]

Matrix::const_reverse_column_iterator column_rbegin ( Size  i) const

Definition at line 453 of file matrix.hpp.

+ Here is the call graph for this function:

◆ column_rbegin() [2/2]

Matrix::reverse_column_iterator column_rbegin ( Size  i)

Definition at line 458 of file matrix.hpp.

+ Here is the call graph for this function:

◆ column_rend() [1/2]

Matrix::const_reverse_column_iterator column_rend ( Size  i) const

Definition at line 463 of file matrix.hpp.

+ Here is the call graph for this function:

◆ column_rend() [2/2]

Definition at line 468 of file matrix.hpp.

+ Here is the call graph for this function:

◆ operator[]() [1/2]

Matrix::const_row_iterator operator[] ( Size  i) const

Definition at line 473 of file matrix.hpp.

+ Here is the call graph for this function:

◆ at() [1/2]

Definition at line 478 of file matrix.hpp.

+ Here is the call graph for this function:

◆ operator[]() [2/2]

Matrix::row_iterator operator[] ( Size  i)

Definition at line 483 of file matrix.hpp.

+ Here is the call graph for this function:

◆ at() [2/2]

Definition at line 487 of file matrix.hpp.

+ Here is the call graph for this function:

◆ diagonal()

Array diagonal ( ) const

Definition at line 492 of file matrix.hpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ operator()()

Real & operator() ( Size  i,
Size  j 
) const

Definition at line 500 of file matrix.hpp.

+ Here is the call graph for this function:

◆ rows()

Size rows ( ) const

Definition at line 504 of file matrix.hpp.

+ Here is the caller graph for this function:

◆ columns()

Size columns ( ) const

Definition at line 508 of file matrix.hpp.

+ Here is the caller graph for this function:

◆ empty()

bool empty ( ) const

Definition at line 520 of file matrix.hpp.

+ Here is the caller graph for this function:

◆ size1()

Size size1 ( ) const

Definition at line 512 of file matrix.hpp.

+ Here is the call graph for this function:

◆ size2()

Size size2 ( ) const

Definition at line 516 of file matrix.hpp.

+ Here is the call graph for this function:

◆ swap()

void swap ( Matrix from)
noexcept

Definition at line 291 of file matrix.hpp.

+ Here is the caller graph for this function:

Friends And Related Function Documentation

◆ operator+() [1/4]

Matrix operator+ ( const Matrix ,
const Matrix  
)
related

Definition at line 524 of file matrix.hpp.

+ Here is the call graph for this function:

◆ operator+() [2/4]

Matrix operator+ ( const Matrix ,
Matrix &&   
)
related

Definition at line 536 of file matrix.hpp.

+ Here is the call graph for this function:

◆ operator+() [3/4]

Matrix operator+ ( Matrix &&  ,
const Matrix  
)
related

Definition at line 547 of file matrix.hpp.

+ Here is the call graph for this function:

◆ operator+() [4/4]

Matrix operator+ ( Matrix &&  ,
Matrix &&   
)
related

Definition at line 558 of file matrix.hpp.

◆ operator-() [1/6]

Matrix operator- ( const Matrix )
related

Definition at line 569 of file matrix.hpp.

+ Here is the call graph for this function:

◆ operator-() [2/6]

Matrix operator- ( Matrix &&  )
related

Definition at line 575 of file matrix.hpp.

◆ operator-() [3/6]

Matrix operator- ( const Matrix ,
const Matrix  
)
related

Definition at line 580 of file matrix.hpp.

+ Here is the call graph for this function:

◆ operator-() [4/6]

Matrix operator- ( const Matrix ,
Matrix &&   
)
related

Definition at line 592 of file matrix.hpp.

+ Here is the call graph for this function:

◆ operator-() [5/6]

Matrix operator- ( Matrix &&  ,
const Matrix  
)
related

Definition at line 603 of file matrix.hpp.

+ Here is the call graph for this function:

◆ operator-() [6/6]

Matrix operator- ( Matrix &&  ,
Matrix &&   
)
related

Definition at line 614 of file matrix.hpp.

◆ operator*() [1/7]

Matrix operator* ( const Matrix ,
Real   
)
related

Definition at line 625 of file matrix.hpp.

+ Here is the call graph for this function:

◆ operator*() [2/7]

Matrix operator* ( Matrix &&  ,
Real   
)
related

Definition at line 631 of file matrix.hpp.

◆ operator*() [3/7]

Matrix operator* ( Real  ,
const Matrix  
)
related

Definition at line 636 of file matrix.hpp.

+ Here is the call graph for this function:

◆ operator*() [4/7]

Matrix operator* ( Real  ,
Matrix &&   
)
related

Definition at line 642 of file matrix.hpp.

◆ operator/() [1/2]

Matrix operator/ ( const Matrix ,
Real   
)
related

Definition at line 647 of file matrix.hpp.

+ Here is the call graph for this function:

◆ operator/() [2/2]

Matrix operator/ ( Matrix &&  ,
Real   
)
related

Definition at line 653 of file matrix.hpp.

◆ operator*() [5/7]

Array operator* ( const Array ,
const Matrix  
)
related

Definition at line 658 of file matrix.hpp.

+ Here is the call graph for this function:

◆ operator*() [6/7]

Array operator* ( const Matrix ,
const Array  
)
related

Definition at line 671 of file matrix.hpp.

+ Here is the call graph for this function:

◆ operator*() [7/7]

Matrix operator* ( const Matrix ,
const Matrix  
)
related

Definition at line 683 of file matrix.hpp.

+ Here is the call graph for this function:

◆ transpose()

Matrix transpose ( const Matrix )
related

Definition at line 700 of file matrix.hpp.

+ Here is the call graph for this function:

◆ outerProduct() [1/2]

Matrix outerProduct ( const Array v1,
const Array v2 
)
related

Definition at line 710 of file matrix.hpp.

+ Here is the call graph for this function:

◆ outerProduct() [2/2]

Matrix outerProduct ( Iterator1  v1begin,
Iterator1  v1end,
Iterator2  v2begin,
Iterator2  v2end 
)
related

Definition at line 715 of file matrix.hpp.

+ Here is the call graph for this function:

◆ swap()

void swap ( Matrix ,
Matrix  
)
related

Definition at line 732 of file matrix.hpp.

◆ operator<<()

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

Definition at line 736 of file matrix.hpp.

+ Here is the call graph for this function:

◆ inverse()

Matrix inverse ( const Matrix m)
related

Definition at line 44 of file matrix.cpp.

+ Here is the call graph for this function:

◆ determinant()

Real determinant ( const Matrix m)
related

Definition at line 81 of file matrix.cpp.

+ Here is the call graph for this function:

◆ CholeskyDecomposition()

Matrix CholeskyDecomposition ( const Matrix m,
bool  flexible = false 
)
related

Definition at line 26 of file choleskydecomposition.cpp.

+ Here is the call graph for this function:

◆ pseudoSqrt()

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.

Definition at line 347 of file pseudosqrt.cpp.

+ Here is the call graph for this function:

◆ rankReducedSqrt()

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.

Definition at line 427 of file pseudosqrt.cpp.

+ Here is the call graph for this function:

Member Data Documentation

◆ data_

std::unique_ptr<Real[]> data_
private

Definition at line 148 of file matrix.hpp.

◆ rows_

Size rows_ = 0
private

Definition at line 149 of file matrix.hpp.

◆ columns_

Size columns_ = 0
private

Definition at line 149 of file matrix.hpp.