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

Matrix of variable size. More...

#include "Imagine/LinAlg.h"

Inheritance diagram for Imagine::Matrix< T >:
Imagine::MultiArray< T, 2 > 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::MultiArray< T, 2 >
typedef const T * const_iterator
 Const iterator type.
 
typedef T * 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

 Matrix ()
 Empty constructor. More...
 
 Matrix (const Base &A)
 Copy constructor. More...
 
 Matrix (const SymMatrix< T > &A)
 Conversion from SymMatrix. More...
 
 Matrix (int M, int N)
 Constructor (known size). More...
 
 Matrix (T *t, int M, int N, bool handleDelete=false)
 Constructor (pre-allocated). More...
 
 ~Matrix ()
 Destructor. More...
 
Matrix clone () const
 Cloning. More...
 
Matrixfill (T x)
 Filling. More...
 
Vector< T > getCol (int j) const
 Get column. More...
 
Vector< T > getColRef (int j)
 Get column by reference. More...
 
Matrix getColsRef (int j0, int n)
 Get part of columns by reference. More...
 
Vector< T > getDiagonal () const
 Get diagonal. More...
 
Vector< T > getRow (int i) const
 Get row. More...
 
Vector< T > getSubColRef (int j, int i0, int m)
 Get sub column by reference. More...
 
Matrix 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...
 
Matrix operator* (const Matrix &B) const
 Product. More...
 
Matrix operator* (const SymMatrix< T > &B) const
 Product. More...
 
Vector< T > operator* (const Vector< T > &v) const
 Product with vector. More...
 
Matrix operator* (T x) const
 Scalar multiplication. More...
 
Matrixoperator*= (T x)
 Scalar in place multiplication. More...
 
Matrix operator+ (const Matrix &B) const
 Addition. More...
 
Matrixoperator+= (const Matrix &B)
 In place Addition. More...
 
Matrix operator- () const
 Opposite. More...
 
Matrix operator- (const Matrix &B) const
 Substraction. More...
 
Matrixoperator-= (const Matrix &B)
 In place Substraction. More...
 
Matrix operator/ (T x) const
 Scalar division. More...
 
Matrixoperator/= (T x)
 Scalar in place division. More...
 
Matrixoperator= (const Matrix &A)
 Assignment. More...
 
MatrixsetCol (int j, const Vector< T > &v)
 Set column. More...
 
MatrixsetDiagonal (const Vector< T > &v)
 Set diagonal. More...
 
MatrixsetRow (int i, const Vector< T > &v)
 Set row. More...
 
- Public Member Functions inherited from Imagine::MultiArray< T, 2 >
 MultiArray ()
 Empty constructor. More...
 
 MultiArray (const Coords< dim > &sz)
 Constructor (known size). More...
 
 MultiArray (const MultiArray &A)
 Copy constructor. More...
 
 MultiArray (const MultiArray< T2, dim > &A)
 Constructor (different type). More...
 
 MultiArray (int s0, int s1)
 Constructor (2D shorcut). More...
 
 MultiArray (int s0, int s1, int s2)
 Constructor (3D shorcut). More...
 
 MultiArray (T *ptr, const Coords< dim > &sz, bool handleDelete=false)
 Constructor (pre-allocated). More...
 
 MultiArray (T *ptr, int s0, int s1, bool handleDelete=false)
 Constructor (pre-allocated) 2D alias. More...
 
 MultiArray (T *ptr, int s0, int s1, int s2, bool handleDelete=false)
 Constructor (pre-allocated) 3D alias. More...
 
virtual ~MultiArray ()
 Destructor. More...
 
MultiArray clone () const
 Cloning. More...
 
CoordsIterator< dim > coordsBegin () const
 Begin coords iterator. More...
 
CoordsIterator< dim > coordsEnd () const
 End coords iterator. More...
 
int depth () const
 Size alias 2. More...
 
MultiArrayfill (T x)
 Filling. More...
 
MultiArray getSubArray (const Coords< dim > &offset, const Coords< dim > &sz) const
 Sub array. More...
 
int height () const
 Size alias 1. More...
 
size_t offset (const Coords< dim > &c) const
 Offset. More...
 
size_t offset (int x, int y) const
 Offset (2D alias). More...
 
size_t offset (int x, int y, int z) const
 Offset (3D alias). More...
 
T & operator() (const Coords< dim > &c)
 Write access. More...
 
const T & operator() (const Coords< dim > &c) const
 Read access. More...
 
T & operator() (int x, int y)
 Write access 2D alias. More...
 
const T & operator() (int x, int y) const
 Read access 2D alias. More...
 
T & operator() (int x, int y, int z)
 Write access 3D alias. More...
 
const T & operator() (int x, int y, int z) const
 Read access 3D alias. More...
 
MultiArrayoperator= (const MultiArray &A)
 Assignment. More...
 
MultiArrayoperator= (const MultiArray< T2, dim > &A)
 Assignment (different type). More...
 
void setSize (const Coords< dim > &sz)
 Change sizes. More...
 
void setSize (int s0, int s1)
 Change size 2D alias. More...
 
void setSize (int s0, int s1, int s2)
 Change size 3D alias. More...
 
int size (int i) const
 ith size. More...
 
Coords< dim > sizes () const
 Sizes. More...
 
FArray< size_t, dim > stride () const
 Stride. More...
 
size_t stride (int i) const
 ith stride. More...
 
size_t totalSize () const
 Total Size. More...
 
int width () const
 Size alias 0. More...
 
- Public Member Functions inherited from Imagine::Array< T >
 Array ()
 Empty constructor. 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...
 
 Array (size_t size)
 Constructor (known size). More...
 
 Array (T *ptr, size_t size, bool handleDelete=false)
 Constructor (pre-allocated). 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...
 
T & operator[] (size_t i)
 Write access. More...
 
const T & operator[] (size_t i) const
 Read access. More...
 
void setSize (size_t size)
 Change size. More...
 
size_t size () const
 Size. More...
 

Static Public Member Functions

static Matrix Identity (int N)
 Identity. More...
 
static Matrix Zero (int M, int N)
 Zero matrix. More...
 

Friends

Matrix cholesky (const Matrix &A, bool low=true)
 Cholesky decomposition. More...
 
det (const Matrix &A)
 Determinant. More...
 
dot (const Matrix &a, const Matrix &b)
 Scalar product. More...
 
Matrix inverse (const Matrix &A)
 Inverse. More...
 
Vector< T > linSolve (const Matrix &A, const Vector< T > &b)
 Linear system. More...
 
Matrix multt (const Matrix &A, const Matrix &B)
 Product. More...
 
norm (const Matrix &A, char type='F')
 Norm. More...
 
Matrix operator* (T x, const Matrix &B)
 Scalar multiplication. More...
 
std::ostream & operator<< (std::ostream &out, const Matrix &A)
 ASCII write. More...
 
std::istream & operator>> (std::istream &in, Matrix &A)
 ASCII read. More...
 
Matrix pseudoInverse (const Matrix &A, T tolrel=0)
 Pseudo inverse. More...
 
bool QR (const Matrix &A, Matrix &Q, Matrix &R, bool all=false)
 QR decomposition. More...
 
rcond (const Matrix &A)
 Reciprocal condition number. More...
 
void svd (const Matrix &A, Matrix &U, Vector< T > &S, Matrix &Vt, bool all=false)
 SVD. More...
 
Matrix tmult (const Matrix &A, const Matrix &B)
 Product. More...
 
Vector< T > tmult (const Matrix &A, const Vector< T > &v)
 Product with vector. More...
 
Matrix tmultt (const Matrix &A, const Matrix &B)
 Product. More...
 
Matrix transpose (const Matrix &A)
 Transpose. More...
 

Detailed Description

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

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

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

Constructor & Destructor Documentation

◆ Matrix() [1/5]

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

Constructs an unallocated matrix of variables of type T

Matrix<T> A; // non allocated
Matrix of variable size.
Definition: Matrix.h:23

◆ Matrix() [2/5]

template<typename T >
Imagine::Matrix< T >::Matrix ( int  M,
int  N 
)
inline

Constructs an allocated matrix of variables of type T

Parameters
Mnumber of rows
Nnumber of columns
Matrix<T> B(3,4); // allocated with specified size

◆ Matrix() [3/5]

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

Constructs an MxN matrix from variables type T stored at an already allocated memory. t contains (0,0), (1,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
Mnumber of rows
Nnumber of columns
handleDeletedelete memory at destruction? (default=false)
T tt[]={1,2,3,4,5,6}; // pre-allocated
Matrix<T> D(tt,3,2);
T *tt2=new T[6];
for(int i=0; i<6; i++)
tt2[i]=rand()/(T)RAND_MAX;
Matrix<T> D2(tt2,3,2,true); // ...

◆ Matrix() [4/5]

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

Constructs a matrix from another one (sharing memory!)

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

◆ Matrix() [5/5]

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

Constructs a matrix from a symmetric one

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

◆ ~Matrix()

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

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

Member Function Documentation

◆ clone()

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

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

Returns
cloned matrix
A=B.clone(); // cloning (fresh memory)
Examples
LinAlg/test/test.cpp.

◆ fill()

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

Fills with constant value

Parameters
xvalue to be copied to each element
Returns
self reference
A.fill(1); // filling with constant value
Examples
LinAlg/test/test.cpp.

◆ getCol()

template<typename T >
Vector< T > Imagine::Matrix< T >::getCol ( int  j) const
inline

Returns column of index j

Parameters
jcolumn index
Returns
column vector
v=A.getCol(2); // Get column
Examples
LinAlg/test/test.cpp.

◆ getColRef()

template<typename T >
Vector< T > Imagine::Matrix< T >::getColRef ( int  j)
inline

Construct a vector without allocating new memory but pointing to a column of *this. Beware: *this (or its memory) must not be destroyed as long as the returned vector is used!

Parameters
jcolumn index
Returns
column
v=A.getColRef(2); // Vector pointing to column (beware of restrictions!)
Examples
LinAlg/test/test.cpp.

◆ getColsRef()

template<typename T >
Matrix Imagine::Matrix< T >::getColsRef ( int  j0,
int  n 
)
inline

Construct a matrix without allocating new memory but pointing to part of columns of *this. Beware: *this (or its memory) must not be destroyed as long as the returned vector is used!

Parameters
j0starting column index
nnumber of columns
Returns
columns
B=A.getColsRef(1,2); // Matrix pointing to part of columns (beware of restrictions!)
Examples
LinAlg/test/test.cpp.

◆ getDiagonal()

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

Returns diagonal vector of size=min(nrow,ncol)

Returns
diagonal vector
v=A.getDiagonal(); // Get diagonal
Examples
LinAlg/test/test.cpp.

◆ getRow()

template<typename T >
Vector< T > Imagine::Matrix< T >::getRow ( int  i) const
inline

Returns row of index i

Parameters
irow index
Returns
row vector
v=A.getRow(1); // Get row
Examples
LinAlg/test/test.cpp.

◆ getSubColRef()

template<typename T >
Vector< T > Imagine::Matrix< T >::getSubColRef ( int  j,
int  i0,
int  m 
)
inline

Construct a vector without allocating new memory but pointing to part of a column of *this. Beware: *this (or its memory) must not be destroyed as long as the returned vector is used!

Parameters
jcolumn index
i0starting row
mnumber of rows
Returns
column
v=A.getSubColRef(2,1,2); // Vector pointing to sub-column (beware of restrictions!)
Examples
LinAlg/test/test.cpp.

◆ getSubMat()

template<typename T >
Matrix Imagine::Matrix< 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
B=A.getSubMat(1,2,1,2); // Get sub matrix
Examples
LinAlg/test/test.cpp.

◆ Identity()

template<typename T >
static Matrix Imagine::Matrix< T >::Identity ( int  N)
inlinestatic

Identity

Parameters
Ndimension (NxN matrix)
Returns
matrix
Matrix<T> I=Matrix<T>::Identity(4); // Identity
static Matrix Identity(int N)
Identity.
Definition: Matrix.h:136

◆ ncol()

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

Number of columns

Returns
Number of columns
/*int m=*/A.nrow(),/*n=*/A.ncol(); // Dimensions
Examples
LinAlg/test/test.cpp.

◆ nrow()

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

Number of rows

Returns
Number of rows
/*int m=*/A.nrow(),/*n=*/A.ncol(); // Dimensions
Examples
LinAlg/test/test.cpp.

◆ operator*() [1/4]

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

Matrix matrix product

Parameters
Bright operand
Returns
*this * B
A34=A32*A24; // matrix * matrix

◆ operator*() [2/4]

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

Matrix symmatrix product

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

◆ operator*() [3/4]

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

Matrix vector product

Parameters
vright operand
Returns
(this) v
w=A*v; // matrix * vector

◆ operator*() [4/4]

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

Multiplies each element by a scalar

Parameters
xThe scalar
Returns
Result
C=A*2; // * scalar

◆ operator*=()

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

Multiplies each element by a scalar

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

◆ operator+()

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

Addition of two Matrix

Parameters
BMatrix to be added to myself
Returns
sum
for(int i=0; i<3; i++)

◆ operator+=()

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

In place Addition of Matrix

Parameters
BMatrix to be added to myself
Returns
self reference
C+=B; // +=

◆ operator-() [1/2]

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

Opposite of a matrix

Returns
Opposite
C=-A; // unary -

◆ operator-() [2/2]

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

Substraction of two Matrix

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

◆ operator-=()

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

In place Substraction of Matrix

Parameters
BMatrix to be substracted from myself
Returns
self reference
C-=B; // -=

◆ operator/()

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

Divides each element by a scalar

Parameters
xThe scalar
Returns
Result
C=A/2; // / scalar

◆ operator/=()

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

Divides each element by a scalar

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

◆ operator=()

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

Assigns from another matrix (sharing its memory)

Parameters
Amatrix to be assigned to
Returns
self reference

◆ setCol()

template<typename T >
Matrix & Imagine::Matrix< T >::setCol ( int  j,
const Vector< T > &  v 
)
inline

Sets column of index j

Parameters
jcolumn index
vcolumn
Returns
self reference
A.setCol(1,v); // Set column
Examples
LinAlg/test/test.cpp.

◆ setDiagonal()

template<typename T >
Matrix & Imagine::Matrix< T >::setDiagonal ( const Vector< T > &  v)
inline

Set diagonal from vector of size=min(nrow,ncol)

Parameters
vdiagonal
Returns
self reference
A.setDiagonal(v); // Set diagonal
Examples
LinAlg/test/test.cpp.

◆ setRow()

template<typename T >
Matrix & Imagine::Matrix< T >::setRow ( int  i,
const Vector< T > &  v 
)
inline

Sets row of index i

Parameters
irow index
vrow
Returns
self reference
A.setRow(2,v); // Set row
Examples
LinAlg/test/test.cpp.

◆ Zero()

template<typename T >
static Matrix Imagine::Matrix< T >::Zero ( int  M,
int  N 
)
inlinestatic

Matrix with constant 0 value

Parameters
M,Ndimensions
Returns
matrix
A=Matrix<T>::Zero(3,4); // Matrix with constant 0 value
static Matrix Zero(int M, int N)
Zero matrix.
Definition: Matrix.h:124

Friends And Related Function Documentation

◆ cholesky

template<typename T >
Matrix cholesky ( const Matrix< T > &  A,
bool  low = true 
)
friend

Cholesky decomposition of symmetric positive-definite matrix.

Parameters
Amatrix
lowChoose lower (default) or upper
  • true: A=L*L^T with L lower
  • false: A=U^T*U with U upper
Returns
L or U
Matrix<T> L=cholesky(B); // Cholesky decomposition
friend Matrix cholesky(const Matrix &A, bool low=true)
Cholesky decomposition.
Definition: Matrix.h:720
cout << "Lower Cholesky " << norm(B-L*transpose(L)) << endl; // Check lower
U=cholesky(B,false);
cout << "Upper Cholesky " << norm(B-transpose(U)*U) << endl; // Check upper...
friend T norm(const Matrix &A, char type='F')
Norm.
Definition: Matrix.h:546
friend Matrix transpose(const Matrix &A)
Transpose.
Definition: Matrix.h:312

◆ det

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

Determinant. Uses LU decomposition.

Parameters
Amatrix
Returns
determinant
cout << "Determinants: " << det(A)*det(inverse(A))-1 << endl;// Determinant
friend Matrix inverse(const Matrix &A)
Inverse.
Definition: Matrix.h:324
friend T det(const Matrix &A)
Determinant.
Definition: Matrix.h:752
cout << "Determinants: " << det(B)*det(inverse(B))-1 << endl;// Check sym pos-def acceleration...

◆ dot

template<typename T >
T dot ( const Matrix< T > &  a,
const Matrix< T > &  b 
)
friend

Scalar product (L^2)

Parameters
aleft term
bright term
Returns
scalar product
a=dot(A,C); // scalar product
friend T dot(const Matrix &a, const Matrix &b)
Scalar product.
Definition: Matrix.h:529

◆ inverse

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

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

Parameters
Amatrix to inverse
Returns
the inverse matrix
B=inverse(A); // inverse

◆ linSolve

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

Solve linear system. Returns x such that:

  • M=N, square system: Ax=b
  • M>N, over determined system: x=argmin_y|Ay-b|
  • M<N, under determined system: x=argmin_{y,Ay=b}|y|
Parameters
Amatrix
bright term
Returns
solution (zeroed vector if no solution)
initRandom(1); // Linear solve
void initRandom(unsigned int s)
Init with seed.
Definition: Random.h:18
A.setSize(5,5);
for (int i=0;i<5;i++)
for (int j=0;j<5;j++)
A(i,j)=T(doubleRandom());
Vector<T> b(5),x;
for (int j=0;j<5;j++)
b[j]=T(doubleRandom());
x=linSolve(A,b);
cout << "Square system: " << norm(A*x-b) << endl; // Check
C=A.getSubMat(0,5,0,4);
x=linSolve(C,b);
cout << "Over determined system: " << norm(C*x-b) << endl; // Check (min possible but not 0)
C=A.getSubMat(0,4,0,5);
x=linSolve(C,b.getSubVect(0,4));
cout << "Minimum norm under determined system: "
<< norm(C*x-b.getSubVect(0,4)) << " " << norm(x) << endl; // Check (min |x| possible but not 0)...
friend Vector< T > linSolve(const Matrix &A, const Vector< T > &b)
Linear system.
Definition: Matrix.h:615
Vector of variable size.
Definition: Vector.h:24
double doubleRandom()
Uniform double.
Definition: Random.h:38

◆ multt

template<typename T >
Matrix multt ( const Matrix< T > &  A,
const Matrix< T > &  B 
)
friend

Matrix matrix product (variant)

Parameters
Aleft operand
Bright operand
Returns
A * transpose(B)
A34=multt(A32,A42); // matrix * matrix^T
friend Matrix multt(const Matrix &A, const Matrix &B)
Product.
Definition: Matrix.h:379

◆ norm

template<typename T >
T norm ( const Matrix< T > &  A,
char  type = 'F' 
)
friend

Matrix norms.

Parameters
Aargument
typenorm choice:
  • 'F' (default): frobenius (sqrt of sum of squares)
  • '1': 1-norm (max column sum)
  • 'I': infinity-norm (max row sum)
  • 'M': max-norm (largest abs value)
Returns
norm
a=norm(A); // frobenius norm
a=norm(A,'1'); // 1-norm
a=norm(A,'I'); // infinity-norm
a=norm(A,'M'); // max norm...

◆ operator*

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

Multiplies each element by a scalar

Parameters
xThe scalar
BThe Matrix
Returns
The result
C=2*A; // scalar * Matrix

◆ operator<<

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

Writes to stream (size and values). Reimplemented from MultiArray: add newlines and iterates over j at fixed i (MultiArray would do the opposite).

Parameters
outoutput stream
Amatrix to write
Returns
updated stream
ofstream out("tmp.txt"); // ASCII write
out << A << endl; // ...

◆ operator>>

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

Reads from stream (size and values). Reimplemented from MultiArray: add newlines and iterates over j at fixed i (MultiArray would do the opposite).

Parameters
ininput stream
Amatrix to write
Returns
updated stream
ifstream in("tmp.txt"); // ASCII read
in >> A; // ...

◆ pseudoInverse

template<typename T >
Matrix pseudoInverse ( const Matrix< T > &  A,
tolrel = 0 
)
friend

Pseudo-inverse using SVD

Parameters
Amatrix
tolrelrelative tolerance (under which a sing. value is considered nul)
Returns
pseudo-inverse
x=pseudoInverse(A)*b; // pseudo-inverse
friend Matrix pseudoInverse(const Matrix &A, T tolrel=0)
Pseudo inverse.
Definition: Matrix.h:660
cout << "Pseudo inverse, square: " << norm(A*x-b) << endl; // Check it, square case
C=A.getSubMat(0,5,0,4);
x=pseudoInverse(C)*b;
cout << "Pseudo inverse, over determined: " << norm(C*x-b) << endl; // Check it, over determined case
C=A.getSubMat(0,4,0,5);
x=pseudoInverse(C)*b.getSubVect(0,4);
cout << "Pseudo inverse, under determined: "
<< norm(C*x-b.getSubVect(0,4)) << " " << norm(x) << endl; // Check it, under determined case...

◆ QR

template<typename T >
bool QR ( const Matrix< T > &  A,
Matrix< T > &  Q,
Matrix< T > &  R,
bool  all = false 
)
friend

QR decomposition. A is MxN with M>=N. A=QR where:

Parameters
Amatrix
Q,Routput
all
  • false: Q is MxN with orthogonal columns and R is NxN upper triangular
  • true: Q is MxM orthogonal and R is MxN with N first rows upper diagonal and M-N last rows nul.
Matrix<T> Q,R; // QR decomposition
QR(D,Q,R);
cout << "QR " << norm(D-Q*R) << endl; // check thin QR
QR(D,Q,R,true);
cout << "QR " << norm(D-Q*R) << endl; // check full QR...
friend bool QR(const Matrix &A, Matrix &Q, Matrix &R, bool all=false)
QR decomposition.
Definition: Matrix.h:688

◆ rcond

template<typename T >
T rcond ( const Matrix< T > &  A)
friend

Reciprocal condition number in the 1-norm

Parameters
Aargument
Returns
condition number
a=rcond(A); // reciprocal condition number
friend T rcond(const Matrix &A)
Reciprocal condition number.
Definition: Matrix.h:558

◆ svd

template<typename T >
void svd ( const Matrix< T > &  A,
Matrix< T > &  U,
Vector< T > &  S,
Matrix< T > &  Vt,
bool  all = false 
)
friend

Singular value decomposition. A is MxN. S is of size min(M,N) with decreasing singular values. A=U*diag(S)*Vt where U (resp. Vt) is MxM (resp NxN) orthonormal, diag(S) is MxN with S as diagonal. If M<N (resp. M>N) then Vt (resp. U) is incompletely filled, except if all is true.

Parameters
Amatrix
U,S,VtSVD output
allcompletely fill U and V (even columns multiplied by zero)
Vector<T> S; // Singular value decomposition
Matrix<T> U,Vt;
svd(A,U,S,Vt); // square matrix
cout << "SVD check 1: "
<< norm(A-U*Diagonal(S)*Vt) << endl; // Check result
svd(A34,U,S,Vt); // non square matrix
cout << "SVD check 2: "
<< norm(A34-U*Matrix<T>::Zero(3,4).setDiagonal(S)*Vt) << endl; // Check result
svd(A34,U,S,Vt,true); // non square, complete U and Vt
cout << "SVD check 3: "
<< norm(Vt*transpose(Vt)-Matrix<T>::Identity(4)) << endl; // Check Vt...
friend void svd(const Matrix &A, Matrix &U, Vector< T > &S, Matrix &Vt, bool all=false)
SVD.
Definition: Matrix.h:589
Matrix & setDiagonal(const Vector< T > &v)
Set diagonal.
Definition: Matrix.h:214
FMatrix< T, M, M > Diagonal(const FVector< T, M > &d)
Diagonal.
Definition: FMatrix.h:743

See equivalent function for FMatrix.

◆ tmult [1/2]

template<typename T >
Matrix tmult ( const Matrix< T > &  A,
const Matrix< T > &  B 
)
friend

Matrix matrix product (variant)

Parameters
Aleft operand
Bright operand
Returns
transpose(A) * B
A34=tmult(A23,A24); // matrix^T * matrix
friend Vector< T > tmult(const Matrix &A, const Vector< T > &v)
Product with vector.
Definition: Matrix.h:298

◆ tmult [2/2]

template<typename T >
Vector< T > tmult ( const Matrix< T > &  A,
const Vector< T > &  v 
)
friend

Matrix vector product (variant)

Parameters
Aleft operand
vright operand
Returns
transpose(A)* v
v=tmult(A,w); // matrix^T * vector

◆ tmultt

template<typename T >
Matrix tmultt ( const Matrix< T > &  A,
const Matrix< T > &  B 
)
friend

Matrix matrix product (variant)

Parameters
Aleft operand
Bright operand
Returns
transpose(A) * transpose(B)
A34=tmultt(A23,A42); // matrix^T * matrix^T
friend Matrix tmultt(const Matrix &A, const Matrix &B)
Product.
Definition: Matrix.h:395

◆ transpose

template<typename T >
Matrix transpose ( const Matrix< T > &  A)
friend

Transposed matrix

Parameters
Aargument
Returns
Transposed matrix
A=A*transpose(A); // transpose

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