CAPD DynSys Library  5.2.0
Matrices

This file provides a short description on how to use class Matrix from CAPD package - for more details see header file "capd/vectalg/Matrix.h"

Content of this file:

Template arguments

The class Matrix is defined in the namespace capd::vectalg. The template class Matrix has three parameters - type of elements stored in a matrix, number of rows and number of columns

template class Matrix<typename ScalarType, int rows, int cols>

If both arguments rows and cols are greater than zero, the matrix is represented as an internal one-dimensional array with suitable indexing. If rows or cols is equal to zero, the matrix has a pointer to the allocated array

The following lines define new names for four dimensional vectors

The following lines define new names for vectors of arbitrary length

The following lines define new names for square matrices 4x4 both for doubles and intervals

The following lines define new names for matrices of arbitrary size

How to create a matrix

The following line creates a 4x4 matrix filled with zeros

DMatrix4D M;

The matrix N will be a 4x5 dimensional interval matrix filled with zeros

IMatrix N(4,5);

If one wishes to initialize the matrix when creating an object, he or she can define a table which contains the rows of a matrix and send it to constructor. The following line creates a matrix from a given table of numbers. The number of elements in a table should be greater or equal to the number of coefficients in created matrix. The table should contain rows of the matrix.

double data[] = {1.,2.,3.,4.,4.,3.,2.,1.,1.,2.,3.,4.};
const DMatrix P(3,4,data);
DMatrix Q(6,2,data);

How to create an array of matrices

When one needs to create an array of matrices which have undefined size at compilation time, the following solution is available.

DMatrix *tab = new (2,4) DMatrix[10];

which means that tab contains an adress of a table of 10 matrices, each of size 2x4. When the same method is applied to the matrices of fixed dimensions, there will be no effect

DMatrix4D *tab2 = new (5,6) DMatrix4D[10];

The pointer tab2 contains the address of a table of 10 matrices each of size 4x4 (the parameters 5,6 are ingorred because type DMatrix4D has fixed size).

Indexing

One can change or access a coefficient in a matrix by using operator() or iterators. The operator() has two arguments - number of row and number of column of the coefficient that is to be accessed. Rows and columns are numbered from 1 to the number of rows and the number of columns, respectively. See an example below.

for(int i=1;i<=P.numberOfRows();++i)
{
for(int j=1;j<=P.numberOfColumns();++j)
{
std::cout << "P(" << i << "," << j << ")=" << P(i,j) << std::endl;
//one can change a coefficient in nonconstant matrix
P(i,j) = i*j;
std::cout << "new value of P(" << i << "," << j << ")=" << P(i,j) << std::endl;
}
}

Rows and columns of matrices as vectors

The rows and columns of a matrix can be seen as vectors. The vectalg module provides two classes: RowVector and ColumnVector that can be used as references to rows and columns of matrices. Objects of these classes don't have their own allocated memory but only a pointer to a proper coefficient in a matrix.

These classes have almost the same properties as class Vector (indexing, iterators, normalization), hence they can be used as vectors in generic algorithms. Objects of these classes are created by methods of class Matrix

std::cout << "Reference to first row of matrix Q: " << Q.row(0) << std::endl;
std::cout << "Reference to first column of matrix Q: " << Q.column(0) << std::endl;
Q.row(0).normalize();
std::cout << "After normalization of first row of matrix Q:" << std::endl;
std::cout << "Q=" << Q << std::endl;

Rows and columns of a matrix are indexed from zero to number of rows minus 1 and number of columns minus 1, respectively. Class Martix defines two special types for references to rows and columns

which are useful when one needs to perform many operations on fixed row or column.

Low level iterators

The class Matrix provides low level iterators to a container which stores all the coefficients. They are useful when one needs to perform some operation on each element of a matrix, as in the Hadamard product of two matrices. The following code implements operation Q = Q*P, where symbol '*' denotes the Hadamard product.

// we assume matrices P and Q have the same dimensions
DMatrix::iterator b = Q.begin(), e = Q.end(), i=P.begin();
while(b!=e)
{
(*b) *= (*i);
++b;
++i;
}

Functions begin and end return low level iterators for the container of a matrix.

Const iterators for constant objects are defined in a similar way. An example below computes sum of all numbers in a matrix P

while(p!=k)
{
sum += (*p);
++p;
}
std::cout << sum;

Matrix iterators

One can use in generic algorithms types MatrixIterator and const_MatrixIterator for manipulating on coefficients in a matrix. These iterators are returned by functions beginMatrix, endMatrix, beginOfRow, endOfRow, beginOfColumn, endOfColumn

MatrixIterator<DMatrix> i = P.begin(); // iterator is set to the first coefficient in P
MatrixIterator<DMatrix> j = P.beginOfRow(1); // iterator is set to the first element in the first row (rows indexed from 1)
const_MatrixIterator<DMatrix> b = Q.beginOfColumn(2); // iterator is set to the first element in the second column (columns indexed from 1)
const_MatrixIterator<DMatrix> e = Q.endOfColumn(2);

The following member functions are available for moving those iterators

One can access a coefficient pointed by iterator by using operator*

std::cout << "value pointed by iterator i: " << (*i) << std::endl;

For more details about MatrixIterator and const_MatrixIterator see the header file "capd/vectalg/MatrixIterator.h".

Basic operations on matrices

The following operations on matrices and vectors are available

transposition of a matrix: DMatrix R = Transpose(Q);
sum: P+R
subtraction: P-R
multiplication of two matrices or matices and vectors: Q*R
multiplication by scalar: 2.*P, P*2.
multiplication by reference to column or row of a matrix: Q*R.column(0)

Moreover, the standard operations like +=, -= etc. whenever possible are available

Member functions

Transpose - if Q is a square matrix, it can be transposed by calling

Q.Transpose();

clear - this member function assigns zero to each coefficient in the matrix

Q.clear();

Identity - this static function returns the identity matrix of a given dimension

Operations for interval matrices only

The following operations are available for interval matrices only - compare similar methods for the class Vector

  • taking a center of a matrix. Function midMatrix returns an interval matrix in which each coefficient is the center of the corresponding coefficient (an interval) in argument.
    // create an interval matrix
    interval d1[] = {interval(-1.,1.),interval(2.,2.),interval(3.,3.1), interval(4.,4.1)};
    IMatrix m1(2,2,d1);
    std::cout << midMatrix(m1) << std::endl;
    // one should obtain on the screen {{[0,0],[2,2]},{[3.05,3.05],[4.05,4.05]}}
  • splitting. This operation is usefull in Lohner algorithm. The function split(m1,m2) has two arguments which are modified by this function in the following way (the actual implementation uses equivalent but optimized version):
    m2 = m1-midMatrix(m1);
    m1 = midMatrix(m1);
    After calling (m1 as in the previous example)
    split(m1,m2);
    std::cout << "m1=" << m1 << std::endl;
    std::cout << "m2=" << m2 << std::endl;
    one should obtain on the screen
    m1={{[0,0],[2,2]},{[3.05,3.05],[4.05,4.05]}}
    m2={{[-1,1],[0,0]},{[-0.05,0.05],[-0.05,0.05]}}
MatrixIterator::moveToNextColumn
MatrixIterator & moveToNextColumn()
Definition: MatrixIterator.h:40
double
capd::vectalg::RowVector
RowVector class realizes a vector without its own container. He is just a reference to a part of othe...
Definition: RowVector.h:37
capd::vectalg::Transpose
Matrix< Scalar, cols, rows > Transpose(const Matrix< Scalar, rows, cols > &)
Definition: Matrix.hpp:158
MatrixIterator::moveToNextRow
MatrixIterator & moveToNextRow()
Definition: MatrixIterator.h:48
MatrixIterator::moveToPrevRow
MatrixIterator & moveToPrevRow()
Definition: MatrixIterator.h:52
const_MatrixIterator
Definition: MatrixIterator.h:106
MatrixIterator
Definition: MatrixIterator.h:23
capd::vectalg::Vector
Definition: ColumnVector.h:177
capd::vectalg::Matrix::Identity
static Matrix Identity(size_type dim)
Definition: Matrix.hpp:109
capd::vectalg::Matrix::column
ColumnVector< Scalar, rows > column(size_type j) const
Definition: Matrix_inline.h:243
MatrixIterator::moveToPrevColumn
MatrixIterator & moveToPrevColumn()
Definition: MatrixIterator.h:44
capd::vectalg::midMatrix
IMatrixType midMatrix(const IMatrixType &v)
Definition: Matrix_Interval.hpp:26
capd::vectalg::Matrix::const_iterator
ContainerType::const_iterator const_iterator
Definition: Matrix.h:70
capd::vectalg::Matrix::iterator
ContainerType::iterator iterator
Definition: Matrix.h:69
capd::intervals::split
void split(Interval< T_Bound, T_Rnd > &A_iv, Interval< T_Bound, T_Rnd > &A_rMid, T_Bound &A_diam)
On output: .
Definition: Interval_Fun.hpp:192
capd::vectalg::Vector::begin
iterator begin()
Definition: Container.h:160
capd::vectalg::ColumnVector
This class realizes a vector without its own container, which is a reference to a subset of other obj...
Definition: ColumnVector.h:184
capd::vectalg::Vector::end
iterator end()
Definition: Container.h:166
capd::vectalg::Matrix
Definition: ColumnVector.h:174
capd::interval
intervals::DoubleInterval interval
Definition: DoubleInterval.h:36