Imagine++
Public Types | Public Member Functions | Static Public Member Functions | Friends | List of all members
Imagine::SymMatrix< T > Class Template Reference

Symmetric Matrix of variable size. More...

#include "Imagine/LinAlg.h"

Inheritance diagram for Imagine::SymMatrix< T >:
Imagine::Array< T >

Public Types

typedef Base::const_iterator const_iterator
 Const iterator type.
 
typedef Base::iterator iterator
 Iterator type.
 
- Public Types inherited from Imagine::Array< T >
typedef const T * const_iterator
 Const iterator type.
 
typedef T * iterator
 Iterator type.
 

Public Member Functions

 SymMatrix ()
 Empty constructor. More...
 
 SymMatrix (int N)
 Constructor (known size). More...
 
 SymMatrix (const SymMatrix &A)
 Copy constructor. More...
 
 SymMatrix (T *t, int N, bool handleDelete=false)
 Constructor (pre-allocated). More...
 
 SymMatrix (const Matrix< T > &A)
 Conversion from Matrix. More...
 
 ~SymMatrix ()
 Destructor. More...
 
SymMatrix clone () const
 Cloning. More...
 
SymMatrixfill (T x)
 Filling. More...
 
Vector< T > getDiagonal () const
 Get diagonal. More...
 
Matrix< T > getSubMat (int i0, int m, int j0, int n) const
 Get sub matrix. More...
 
int ncol () const
 Number of columns. More...
 
int nrow () const
 Number of rows. More...
 
const T & operator() (int i, int j) const
 Read access. More...
 
T & operator() (int i, int j)
 Write access. More...
 
Vector< T > operator* (const Vector< T > &v) const
 Product with vector. More...
 
Matrix< T > operator* (const SymMatrix &B) const
 Product. More...
 
Matrix< T > operator* (const Matrix< T > &B) const
 Product. More...
 
SymMatrix operator* (T x) const
 Scalar multiplication. More...
 
SymMatrixoperator*= (T x)
 Scalar in place multiplication. More...
 
SymMatrix operator+ (const SymMatrix &B) const
 Addition. More...
 
SymMatrixoperator+= (const SymMatrix &B)
 In place Addition. More...
 
SymMatrix operator- (const SymMatrix &B) const
 Substraction. More...
 
SymMatrix operator- () const
 Opposite. More...
 
SymMatrixoperator-= (const SymMatrix &B)
 In place Substraction. More...
 
SymMatrix operator/ (T x) const
 Scalar division. More...
 
SymMatrixoperator/= (T x)
 Scalar in place division. More...
 
SymMatrixoperator= (const SymMatrix &A)
 Assignment. More...
 
void read (std::istream &in)
 Binary read. More...
 
void setSize (int n)
 Change sizes. More...
 
void write (std::ostream &out) const
 Binary write. More...
 
- Public Member Functions inherited from Imagine::Array< T >
 Array ()
 Empty constructor. More...
 
 Array (size_t size)
 Constructor (known size). More...
 
 Array (T *ptr, size_t size, bool handleDelete=false)
 Constructor (pre-allocated). More...
 
 Array (const Array &A)
 Copy constructor. More...
 
template<typename T2 >
 Array (const Array< T2 > &A)
 Constructor (different type). More...
 
 Array (const std::list< T > &L)
 Constructor (from list). More...
 
virtual ~Array ()
 Destructor. More...
 
iterator begin ()
 Begin iterator. More...
 
const_iterator begin () const
 Begin const iterator. More...
 
Array clone () const
 Cloning. More...
 
T * data ()
 Data pointer (read/write). More...
 
const T * data () const
 Data pointer (read). More...
 
bool empty () const
 Is empty. More...
 
iterator end ()
 End iterator. More...
 
const_iterator end () const
 End const iterator. More...
 
Arrayfill (const T &x)
 Filling. More...
 
Array getSubArray (size_t offset, size_t size) const
 Sub array. More...
 
bool operator!= (const Array &A) const
 Inequality test. More...
 
Arrayoperator= (const Array &A)
 Assignment. More...
 
template<typename T2 >
Arrayoperator= (const Array< T2 > &A)
 Assignment (different type). More...
 
bool operator== (const Array &A) const
 Equality test. More...
 
const T & operator[] (size_t i) const
 Read access. More...
 
T & operator[] (size_t i)
 Write access. More...
 
void setSize (size_t size)
 Change size. More...
 
size_t size () const
 Size. More...
 

Static Public Member Functions

static SymMatrix Identity (int n)
 Identity. More...
 
static SymMatrix Zero (int n)
 Zero matrix. More...
 

Friends

det (const SymMatrix &A)
 Determinant. More...
 
void EVD (const SymMatrix &A, Matrix< T > &Q, Vector< T > &Lambda)
 Eigen values. More...
 
SymMatrix inverse (const SymMatrix &A)
 Inverse. More...
 
Vector< T > linSolve (const SymMatrix &A, const Vector< T > &b)
 Linear system. More...
 
SymMatrix operator* (T x, const SymMatrix &B)
 Scalar multiplication. More...
 
std::ostream & operator<< (std::ostream &out, const SymMatrix &A)
 ASCII write. More...
 
std::istream & operator>> (std::istream &in, SymMatrix &A)
 ASCII read. More...
 
SymMatrix posDefInverse (const SymMatrix &A)
 Inverse (variant). More...
 

Detailed Description

template<typename T>
class Imagine::SymMatrix< T >

Symmetric Matrix of variable size. Memory is reference counted, i.e.:

Parameters
Tvalue type
Examples:
LinAlg/test/test.cpp.

Constructor & Destructor Documentation

◆ SymMatrix() [1/5]

template<typename T>
Imagine::SymMatrix< T >::SymMatrix ( )
inline

Constructs an unallocated symmetric matrix of variables of type T

SymMatrix<T> A; // non allocated

◆ SymMatrix() [2/5]

template<typename T>
Imagine::SymMatrix< T >::SymMatrix ( int  N)
inlineexplicit

Constructs an allocated symmetric matrix of variables of type T

Parameters
Nmatrix size
SymMatrix<T> B(10); // allocated with specified size

◆ SymMatrix() [3/5]

template<typename T>
Imagine::SymMatrix< T >::SymMatrix ( const SymMatrix< T > &  A)
inline

Constructs a symmetric matrix from another one (sharing memory!)

Parameters
Amatrix to copy
SymMatrix<T> C(B); // copy constructor

◆ SymMatrix() [4/5]

template<typename T>
Imagine::SymMatrix< T >::SymMatrix ( T *  t,
int  N,
bool  handleDelete = false 
)
inline

Constructs an symmetric matrix from variables type T stored at an already allocated memory. t contains partial rows (0,0), (1,0), (1,1), (2,0), ... Does not allocate fresh memory. Does not free given memory at object destruction unless handleDelete=true. This memory must indeed stay available during object life.

Parameters
taddress of memory
Nmatrix size
handleDeletedelete memory at destruction? (default=false)
T t[]={1,2,3,4,5}; // pre-allocated
SymMatrix<T> D(t,3);
T *t2=new T[5];
SymMatrix<T> D2(t2,3,true); // ...

◆ SymMatrix() [5/5]

template<typename T>
Imagine::SymMatrix< T >::SymMatrix ( const Matrix< T > &  A)
inline

Constructs a symmetric matrix from a plain one, using upper part.

Parameters
Amatrix to copy
SymMatrix<T> F(E); // conversion Matrix -> SymMatrix

◆ ~SymMatrix()

template<typename T>
Imagine::SymMatrix< T >::~SymMatrix ( )
inline

Reference counted desctructor: frees memory if the object is the last one to use it.

Member Function Documentation

◆ clone()

template<typename T>
SymMatrix Imagine::SymMatrix< T >::clone ( ) const
inline

Clones: creates a new symmetric matrix, with fresh memory, copying values to it

Returns
cloned symmetric matrix
F=B.clone(); // cloning (fresh memory)

◆ fill()

template<typename T>
SymMatrix& Imagine::SymMatrix< T >::fill ( x)
inline

Fills with constant value

Parameters
xvalue to be copied to each element
Returns
self reference
F.fill(2); // filling with constant value

◆ getDiagonal()

template<typename T>
Vector<T> Imagine::SymMatrix< T >::getDiagonal ( ) const
inline

Returns diagonal vector

Returns
diagonal vector
Vector<T> d=F.getDiagonal(); // Get diagonal

◆ getSubMat()

template<typename T>
Matrix<T> Imagine::SymMatrix< T >::getSubMat ( int  i0,
int  m,
int  j0,
int  n 
) const
inline

Constructs a new matrix a copies a part of *this into it.

Parameters
i0starting row
mnumber of rows
j0starting column
nnumber of columns
Matrix<T> G=F.getSubMat(1,3,0,4); // Get sub matrix

◆ Identity()

template<typename T>
static SymMatrix Imagine::SymMatrix< T >::Identity ( int  n)
inlinestatic

Identity

Parameters
nsize
Returns
matrix
F=SymMatrix<T>::Identity(5); // Identity

Examples:
LinAlg/test/test.cpp.

◆ ncol()

template<typename T>
int Imagine::SymMatrix< T >::ncol ( ) const
inline

Number of columns

Returns
Number of columns
/*int nr=*/F.nrow(),/*nc=*/F.ncol();// numbers of rows and columns (both equal to specified size!)

◆ nrow()

template<typename T>
int Imagine::SymMatrix< T >::nrow ( ) const
inline

Number of rows

Returns
Number of rows
/*int nr=*/F.nrow(),/*nc=*/F.ncol();// numbers of rows and columns (both equal to specified size!)

◆ operator()() [1/2]

template<typename T>
const T& Imagine::SymMatrix< T >::operator() ( int  i,
int  j 
) const
inline

Reads element (i,j)

Parameters
irow index
jcolumn index
Returns
const reference to element (i,j)
T a=F(2,2); // read access

◆ operator()() [2/2]

template<typename T>
T& Imagine::SymMatrix< T >::operator() ( int  i,
int  j 
)
inline

Writes element (i,j)

Parameters
irow index
jcolumn index
Returns
Reference to element (i,j)
F(2,3)=a; // write access

◆ operator*() [1/4]

template<typename T>
Vector<T> Imagine::SymMatrix< T >::operator* ( const Vector< T > &  v) const
inline

SymMatrix vector product

Parameters
vright operand
Returns
(this) v
Vector<T> v2=F*v1; // matrix * vector

◆ operator*() [2/4]

template<typename T>
Matrix<T> Imagine::SymMatrix< T >::operator* ( const SymMatrix< T > &  B) const
inline

sym matrix * sym matrix product. NB: result is not symmetric!

Parameters
Bright operand
Returns
*this * B
P=F*G; // symmatrix * symmatrix

◆ operator*() [3/4]

template<typename T>
Matrix<T> Imagine::SymMatrix< T >::operator* ( const Matrix< T > &  B) const
inline

sym matrix * matrix product.

Parameters
Bright operand
Returns
*this * B
P=F*P; // symmatrix * matrix

◆ operator*() [4/4]

template<typename T>
SymMatrix Imagine::SymMatrix< T >::operator* ( x) const
inline

Multiplies each element by a scalar

Parameters
xThe scalar
Returns
Result
F=F*2; // * scalar

◆ operator*=()

template<typename T>
SymMatrix& Imagine::SymMatrix< T >::operator*= ( x)
inline

Multiplies each element by a scalar

Parameters
xThe scalar
Returns
self reference
F*=2; // *= scalar

◆ operator+()

template<typename T>
SymMatrix Imagine::SymMatrix< T >::operator+ ( const SymMatrix< T > &  B) const
inline

Addition of two SymMatrix

Parameters
BMatrix to be added to myself
Returns
sum
F=F+G; // +

◆ operator+=()

template<typename T>
SymMatrix& Imagine::SymMatrix< T >::operator+= ( const SymMatrix< T > &  B)
inline

In place Addition of SymMatrix

Parameters
BSymMatrix to be added to myself
Returns
self reference
F+=G; // +=

◆ operator-() [1/2]

template<typename T>
SymMatrix Imagine::SymMatrix< T >::operator- ( const SymMatrix< T > &  B) const
inline

Substraction of two SymMatrix

Parameters
BSymMatrix to be substracted from myself
Returns
difference
T t[]={1,2,3,4,5}; // pre-allocated

◆ operator-() [2/2]

template<typename T>
SymMatrix Imagine::SymMatrix< T >::operator- ( ) const
inline

Opposite of a SymMatrix

Returns
Opposite
F=-F; // unary -

◆ operator-=()

template<typename T>
SymMatrix& Imagine::SymMatrix< T >::operator-= ( const SymMatrix< T > &  B)
inline

In place Substraction of SymMatrix

Parameters
BSymMatrix to be substracted from myself
Returns
self reference
F-=G; // -=

◆ operator/()

template<typename T>
SymMatrix Imagine::SymMatrix< T >::operator/ ( x) const
inline

Divides each element by a scalar

Parameters
xThe scalar
Returns
Result
F=2*F; // scalar * SymMatrix

◆ operator/=()

template<typename T>
SymMatrix& Imagine::SymMatrix< T >::operator/= ( x)
inline

Divides each element by a scalar

Parameters
xThe scalar
Returns
self reference
F/=2; // /= scalar

◆ operator=()

template<typename T>
SymMatrix& Imagine::SymMatrix< T >::operator= ( const SymMatrix< T > &  A)
inline

Assigns from another symmetric matrix (sharing its memory)

Parameters
Asymmetric to be assigned to
Returns
self reference
F=B; // assignment

◆ read()

template<typename T>
void Imagine::SymMatrix< T >::read ( std::istream &  in)
inline

Reads SymMatrix from binary stream

Parameters
ininput stream
ifstream in("tmp.bin",ios::binary); // binary read
F.read(in); // ...

◆ setSize()

template<typename T>
void Imagine::SymMatrix< T >::setSize ( int  n)
inline

Resize symmetric matrix:

  • reallocates fresh memory
  • data is not copied from old to new memory
  • concerns only this object, not the ones that shared the old memory with it (and continue to use this old memory)
  • if the new size is the same as the old one, does nothing
    Parameters
    nnew size
    F.setSize(5); // setSize

◆ write()

template<typename T>
void Imagine::SymMatrix< T >::write ( std::ostream &  out) const
inline

Writes SymMatrix to binary stream

Parameters
outoutput stream
ofstream out("tmp.bin",ios::binary); // binary write
F.write(out); // ...

◆ Zero()

template<typename T>
static SymMatrix Imagine::SymMatrix< T >::Zero ( int  n)
inlinestatic

Matrix with constant 0 value

Parameters
nsize
Returns
matrix
F=SymMatrix<T>::Zero(5); // Matrix with constant 0 value

Examples:
LinAlg/test/test.cpp.

Friends And Related Function Documentation

◆ det

template<typename T>
T det ( const SymMatrix< T > &  A)
friend

Determinant.

Parameters
Aargument
Returns
determinant

◆ EVD

template<typename T>
void EVD ( const SymMatrix< T > &  A,
Matrix< T > &  Q,
Vector< T > &  Lambda 
)
friend

Eigen values. Value Lambda[i] is associated with vector stored at ith column of Q. Thus Q is orthonormal and A=Q*diag(Lambda)*Q^T

Parameters
Aargument
QEigen vectors
LambdaEigen values in increasing order
Matrix<T> Q; // Eigen values
Vector<T> Lambda;
EVD(F,Q,Lambda);
cout << "Check EVD: "
<< norm(Q*transpose(Q)- Matrix<T>::Identity(5)) << " "
<< norm(Q*Diagonal(Lambda)*transpose(Q)-F) << endl; // ...

◆ inverse

template<typename T>
SymMatrix inverse ( const SymMatrix< T > &  A)
friend

Inverse matrix. If non invertible, ouptuts a message to cerr and returns a matrix with zeroed elements.

Parameters
Aargument
Returns
Inverse
SymMatrix<T> invF=inverse(F); // inverse
cout << "Inverse: " << norm(F*invF-Matrix<T>::Identity(5)) << endl;// ...

◆ linSolve

template<typename T>
Vector<T> linSolve ( const SymMatrix< T > &  A,
const Vector< T > &  b 
)
friend

Solves symmetric system. Returns x such that Ax=b

Parameters
Aargument
bright term
Returns
solution (zeroed vector if no solution)
Vector<T> b=Vector<T>(5).fill(1); // Linear solve
Vector<T> x=linSolve(F,b);
cout << norm(F*x-b) << endl; // ...

◆ operator*

template<typename T>
SymMatrix operator* ( x,
const SymMatrix< T > &  B 
)
friend

Multiplies each element by a scalar

Parameters
xThe scalar
BThe SymMatrix
Returns
The result
F=2*F; // scalar * SymMatrix

◆ operator<<

template<typename T>
std::ostream& operator<< ( std::ostream &  out,
const SymMatrix< T > &  A 
)
friend

Writes SymMatrix to stream

Parameters
outoutput stream
ASymMatrix to write
Returns
updated stream
out.open("tmp.txt"); // ASCII write
out << F << endl; // ...

◆ operator>>

template<typename T>
std::istream& operator>> ( std::istream &  in,
SymMatrix< T > &  A 
)
friend

Reads SymMatrix from stream

Parameters
ininput stream
ASymMatrix to read
Returns
updated stream
in.open("tmp.txt"); // ASCII read
in >> F; // ...

◆ posDefInverse

template<typename T>
SymMatrix posDefInverse ( const SymMatrix< T > &  A)
friend

Inverse matrix if positive-definite. If not the case invertible, ouptuts a message to cerr and returns a matrix with zeroed elements.

Parameters
Aargument
Returns
Inverse
invF=posDefInverse(F); // positive-definite inverse
cout << "Inverse: " << norm(F*invF-Matrix<T>::Identity(5)) << endl; // ...


The documentation for this class was generated from the following files: