Imagine++
Public Member Functions | Static Public Member Functions | Protected Attributes | Friends | List of all members
Imagine::FMatrix< T, M, N > Class Template Reference

Matrix of fixed dimension. More...

#include "Imagine/Common.h"

Public Member Functions

 FMatrix ()
 Empty constructor. More...
 
 FMatrix (const T &v)
 Constructor with constant value. More...
 
 FMatrix (const T t[M *N])
 Constructor from C array. More...
 
 FMatrix (const T t[M][N])
 Constructor from bidim C array. More...
 
template<typename T2 >
 FMatrix (const FMatrix< T2, M, N > &A)
 Copy constructor Constructs from another FMatrix (with a possibly different type) More...
 
const T * data () const
 Data pointer (read). More...
 
T * data ()
 Data pointer (write). More...
 
FMatrixfill (const T &v)
 Filling. More...
 
FVector< T, M > getCol (int j) const
 Get column. More...
 
FVector< T, N > getRow (int i) const
 Get row. More...
 
int ncol () const
 Number of columns. More...
 
int nrow () const
 Number of rows. More...
 
bool operator!= (const FMatrix &B) const
 Inequality test. More...
 
const T & operator() (int i, int j) const
 Read access. More...
 
T & operator() (int i, int j)
 Write access. More...
 
FMatrix operator* (T s) const
 Scalar multiplication. More...
 
template<int O>
FMatrix< T, M, O > operator* (const FMatrix< T, N, O > &B) const
 Product. More...
 
FVector< T, M > operator* (const FVector< T, N > &v) const
 Product with vector. More...
 
FMatrixoperator*= (T s)
 Scalar in place multiplication. More...
 
FMatrix operator+ (const FMatrix &B) const
 Addition. More...
 
FMatrix operator+ (T s) const
 Scalar Addition. More...
 
FMatrixoperator+= (const FMatrix &B)
 In place Addition. More...
 
FMatrixoperator+= (T s)
 Scalar in place Addition. More...
 
FMatrix operator- (const FMatrix &B) const
 Substraction. More...
 
FMatrix operator- (T s) const
 Scalar Substraction. More...
 
FMatrix operator- () const
 Opposite. More...
 
FMatrixoperator-= (const FMatrix &B)
 In place Substraction. More...
 
FMatrixoperator-= (T s)
 Scalar in place substractrion. More...
 
FMatrix operator/ (T s) const
 Scalar division. More...
 
FMatrixoperator/= (T s)
 Scalar in place division. More...
 
template<typename T2 >
FMatrixoperator= (const FMatrix< T2, M, N > &B)
 Assignment. More...
 
bool operator== (const FMatrix &B) const
 Equality test. More...
 
const T & operator[] (int k) const
 1D read access. More...
 
T & operator[] (int k)
 1D write access. More...
 
FMatrixsetCol (int j, const FVector< T, M > &v)
 Set column. More...
 
FMatrixsetRow (int i, const FVector< T, N > &v)
 Set row. More...
 
int size () const
 Size. More...
 

Static Public Member Functions

static FMatrix CrossProd (const FVector< T, 3 > &v)
 Cross product matrix. More...
 
static FMatrix Identity ()
 Identity. More...
 
static FMatrix Zero ()
 Zero matrix Matrix with constant 0 value. More...
 

Protected Attributes

_data [M *N]
 internal storage.
 

Friends

norm (const FMatrix &A)
 Frobenius norm. More...
 
norm2 (const FMatrix &A)
 Squared Frobenius norm. More...
 
std::ostream & operator<< (std::ostream &out, const FMatrix &A)
 ASCII write. More...
 
std::istream & operator>> (std::istream &in, FMatrix &A)
 ASCII read. More...
 
void read (std::istream &in, FMatrix &A)
 Binary read. More...
 
void write (std::ostream &out, const FMatrix &A)
 Binary write. More...
 

Detailed Description

template<typename T, int M, int N>
class Imagine::FMatrix< T, M, N >

Fixed size vectors.

Parameters
Tvalue type
M,Ndimensions (M=rows N=columns)
Examples:
Common/test/test.cpp, Graphics/test/test.cpp, and LinAlg/test/test.cpp.

Constructor & Destructor Documentation

◆ FMatrix() [1/5]

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

Constructs an uninitialized MxN matrix of elements of type T

FMatrix<double,2,3> A; // uninitialized

◆ FMatrix() [2/5]

template<typename T, int M, int N>
Imagine::FMatrix< T, M, N >::FMatrix ( const T &  v)
inlineexplicit

Constructs an MxN matrix of elements of type T, each initialized to v

Parameters
vvalue used for initialization
FMatrix<double,2,3> B(1.5); // filled with constant value

◆ FMatrix() [3/5]

template<typename T, int M, int N>
Imagine::FMatrix< T, M, N >::FMatrix ( const T  t[M *N])
inline

Constructs an MxN matrix of elements of type T, and copies values to it. t contains elements (0,0), (1,0), ...

Parameters
tC array used for initialization
double t1[6]={1,2,3,4,5,6}; // filled from C array
FMatrix<double,2,3> C(t1); // (copying array into fresh memory)

◆ FMatrix() [4/5]

template<typename T, int M, int N>
Imagine::FMatrix< T, M, N >::FMatrix ( const T  t[M][N])
inline

Constructs an MxN matrix of elements of type T, and copies values to it

Parameters
tC bidimensional array used for initialization
double t2[2][3]={{1,2,3},{4,5,6}}; // filled from bidim C array
FMatrix<double,2,3> D(t2); // (copying array into fresh memory)

◆ FMatrix() [5/5]

template<typename T, int M, int N>
template<typename T2 >
Imagine::FMatrix< T, M, N >::FMatrix ( const FMatrix< T2, M, N > &  A)
inline
Parameters
AFMatrix to be copied (has to be of the same dimension)
Template Parameters
T2value type of A
FMatrix<double,2,3> Ed(D); // copy constructor
FMatrix<int,2,3> Ei(D); // copy from different type

Member Function Documentation

◆ CrossProd()

template<typename T, int M, int N>
static FMatrix Imagine::FMatrix< T, M, N >::CrossProd ( const FVector< T, 3 > &  v)
inlinestatic

Matrix corresponding to a 3D cross product, i.e. such that for all vector w, P*w gives v^w

Parameters
vThe vector.
Returns
matrix
FVector<double,3> v(1,2,3); // cross product matrix
FMatrix<double,3,3> P; // i.e. such that for all w, P*w = v^w

◆ data() [1/2]

template<typename T, int M, int N>
const T* Imagine::FMatrix< T, M, N >::data ( ) const
inline

Pointer to data for reading

Returns
const pointer to FMatrix elements. Element (i,j) is in position i+M*j.
x=A.data()[4]; // data pointer (read)

◆ data() [2/2]

template<typename T, int M, int N>
T* Imagine::FMatrix< T, M, N >::data ( )
inline

Pointer to data for writing

Returns
Pointer to FMatrix elements. Element (i,j) is in position i+M*j.
A.data()[4]=x; // data pointer (write)

◆ fill()

template<typename T, int M, int N>
FMatrix& Imagine::FMatrix< T, M, N >::fill ( const T &  v)
inline

Fills with constant value

Parameters
vvalue to be copied to each element
Returns
self reference
A.fill(1.3); // Fills with constant value

◆ getCol()

template<typename T, int M, int N>
FVector<T,M> Imagine::FMatrix< T, M, N >::getCol ( int  j) const
inline

Returns column of index j

Parameters
jcolumn index
Returns
column vector
v2=A23.getCol(0); // Get column

◆ getRow()

template<typename T, int M, int N>
FVector<T,N> Imagine::FMatrix< T, M, N >::getRow ( int  i) const
inline

Returns row of index i

Parameters
irow index
Returns
row vector
v3=A23.getRow(0); // Get row

◆ Identity()

template<typename T, int M, int N>
static FMatrix Imagine::FMatrix< T, M, N >::Identity ( )
inlinestatic

Identify Matrix

Returns
identity
FMatrix<double,2,2> I; // identity

◆ ncol()

template<typename T, int M, int N>
int Imagine::FMatrix< T, M, N >::ncol ( ) const
inline

Number of columns

Returns
N
/*int n=*/A.ncol(); // number of columns

◆ nrow()

template<typename T, int M, int N>
int Imagine::FMatrix< T, M, N >::nrow ( ) const
inline

Number of rows

Returns
M
/*int m=*/A.nrow(); // number of rows

◆ operator!=()

template<typename T, int M, int N>
bool Imagine::FMatrix< T, M, N >::operator!= ( const FMatrix< T, M, N > &  B) const
inline

Inequality test (component wise)

Parameters
BFMatrix for comparison (should be of same dims)
Returns
true if *this does not equal B
if (A!=B) cout << "A is different from B" << endl; // !=

◆ operator()() [1/2]

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

Reads element (i,j)

Parameters
irow index
jcolumn index
Returns
const reference to element (i,j)
x=A(1,2); // read access

◆ operator()() [2/2]

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

Writes element (i,j)

Parameters
irow index
jcolumn index
Returns
Reference to element (i,j)
A(1,2)=x; // write access

◆ operator*() [1/3]

template<typename T, int M, int N>
FMatrix Imagine::FMatrix< T, M, N >::operator* ( s) const
inline

Multiplies each element by a scalar

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

◆ operator*() [2/3]

template<typename T, int M, int N>
template<int O>
FMatrix<T,M,O> Imagine::FMatrix< T, M, N >::operator* ( const FMatrix< T, N, O > &  B) const
inline

Matrix matrix product

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

◆ operator*() [3/3]

template<typename T, int M, int N>
FVector<T,M> Imagine::FMatrix< T, M, N >::operator* ( const FVector< T, N > &  v) const
inline

Matrix vector product

Parameters
vright operand
Returns
(this) v
v3=A32*v2; // matrix * vector

◆ operator*=()

template<typename T, int M, int N>
FMatrix& Imagine::FMatrix< T, M, N >::operator*= ( s)
inline

Multiplies each element by a scalar

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

◆ operator+() [1/2]

template<typename T, int M, int N>
FMatrix Imagine::FMatrix< T, M, N >::operator+ ( const FMatrix< T, M, N > &  B) const
inline

Addition of two FMatrix

Parameters
BFMatrix to be added to myself
Returns
sum
C=A+B; // +

◆ operator+() [2/2]

template<typename T, int M, int N>
FMatrix Imagine::FMatrix< T, M, N >::operator+ ( s) const
inline

Adds a scalar to each element

Parameters
sScalar to be added to myself
Returns
sum
C=A+3.; // + scalar

◆ operator+=() [1/2]

template<typename T, int M, int N>
FMatrix& Imagine::FMatrix< T, M, N >::operator+= ( const FMatrix< T, M, N > &  B)
inline

In place Addition of FMatrix

Parameters
BFMatrix to be added to myself
Returns
self reference
C+=A; // +=

◆ operator+=() [2/2]

template<typename T, int M, int N>
FMatrix& Imagine::FMatrix< T, M, N >::operator+= ( s)
inline

Adds a scalar to each own element

Parameters
sScalar to be added to myself
Returns
self reference
C+=3.; // += scalar

◆ operator-() [1/3]

template<typename T, int M, int N>
FMatrix Imagine::FMatrix< T, M, N >::operator- ( const FMatrix< T, M, N > &  B) const
inline

Substraction of two FMatrix

Parameters
BFMatrix to be substracted from myself
Returns
difference
C=A-B; // -

◆ operator-() [2/3]

template<typename T, int M, int N>
FMatrix Imagine::FMatrix< T, M, N >::operator- ( s) const
inline

Substracts a scalar to each element

Parameters
sScalar to be substracted from myself
Returns
difference
C=A-3.; // - scalar

◆ operator-() [3/3]

template<typename T, int M, int N>
FMatrix Imagine::FMatrix< T, M, N >::operator- ( ) const
inline

Opposite of a FMatrix

Returns
Opposite
B=-A; // unary -

◆ operator-=() [1/2]

template<typename T, int M, int N>
FMatrix& Imagine::FMatrix< T, M, N >::operator-= ( const FMatrix< T, M, N > &  B)
inline

In place Substraction of FMatrix

Parameters
BFMatrix to be substracted from myself
Returns
self reference
C-=A; // -=

◆ operator-=() [2/2]

template<typename T, int M, int N>
FMatrix& Imagine::FMatrix< T, M, N >::operator-= ( s)
inline

Subtracts a scalar to each own element

Parameters
sScalar to be substracted from myself
Returns
self reference
C-=3.; // -= scalar

◆ operator/()

template<typename T, int M, int N>
FMatrix Imagine::FMatrix< T, M, N >::operator/ ( s) const
inline

Divides each element by a scalar

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

◆ operator/=()

template<typename T, int M, int N>
FMatrix& Imagine::FMatrix< T, M, N >::operator/= ( s)
inline

Divides each element by a scalar

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

◆ operator=()

template<typename T, int M, int N>
template<typename T2 >
FMatrix& Imagine::FMatrix< T, M, N >::operator= ( const FMatrix< T2, M, N > &  B)
inline

Assigns from another FMatrix (of possibly different type but same dimension)

Parameters
BSource FVector
Template Parameters
T2value type of B
Returns
self reference
Ed=B; // assignment (copies values)
Ei=B; // assignment from different type

◆ operator==()

template<typename T, int M, int N>
bool Imagine::FMatrix< T, M, N >::operator== ( const FMatrix< T, M, N > &  B) const
inline

Equality test (component wise)

Parameters
BFMatrix for comparison (should be of same dims)
Returns
true if *this equals B
if (A==B) cout << "A equals B" << endl; // ==

◆ operator[]() [1/2]

template<typename T, int M, int N>
const T& Imagine::FMatrix< T, M, N >::operator[] ( int  k) const
inline

Reads element at position k. Element (i,j) is at position i+M*j

Parameters
kelement index
Returns
const ref to element at position k
x=A[4]; // 1D read access []

◆ operator[]() [2/2]

template<typename T, int M, int N>
T& Imagine::FMatrix< T, M, N >::operator[] ( int  k)
inline

Writes element at position k. Element (i,j) is at position i+M*j

Parameters
kelement index
Returns
Reference to element at position k
A[4]=x; // 1D write access []

◆ setCol()

template<typename T, int M, int N>
FMatrix& Imagine::FMatrix< T, M, N >::setCol ( int  j,
const FVector< T, M > &  v 
)
inline

Sets column of index j

Parameters
jcolumn index
vcolumn
Returns
self reference
A23.setCol(0,v2); // Set column

◆ setRow()

template<typename T, int M, int N>
FMatrix& Imagine::FMatrix< T, M, N >::setRow ( int  i,
const FVector< T, N > &  v 
)
inline

Sets row of index i

Parameters
irow index
vrow
Returns
self reference
A23.setRow(0,v3); // Set row

◆ size()

template<typename T, int M, int N>
int Imagine::FMatrix< T, M, N >::size ( ) const
inline

Total dimension

Returns
M*N
/*int s=*/A.size(); // size

◆ Zero()

template<typename T, int M, int N>
static FMatrix Imagine::FMatrix< T, M, N >::Zero ( )
inlinestatic
Returns
matrix
B=FMatrix<double,2,3>::Zero(); // Matrix with constant 0 value

Friends And Related Function Documentation

◆ norm

template<typename T, int M, int N>
T norm ( const FMatrix< T, M, N > &  A)
friend

Frobenius norm (provided sqrt is available for T)

Parameters
Aargument
Returns
norm
x=norm2(A); // Squared Frobenius norm

◆ norm2

template<typename T, int M, int N>
T norm2 ( const FMatrix< T, M, N > &  A)
friend

Squared Frobenius norm

Parameters
Aargument
Returns
squared norm
x=norm2(A); // Squared Frobenius norm

◆ operator<<

template<typename T, int M, int N>
std::ostream& operator<< ( std::ostream &  out,
const FMatrix< T, M, N > &  A 
)
friend

Writes FMatrix to stream

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

◆ operator>>

template<typename T, int M, int N>
std::istream& operator>> ( std::istream &  in,
FMatrix< T, M, N > &  A 
)
friend

Reads FMatrix from stream

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

◆ read

template<typename T, int M, int N>
void read ( std::istream &  in,
FMatrix< T, M, N > &  A 
)
friend

Reads FMatrix from binary stream

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

◆ write

template<typename T, int M, int N>
void write ( std::ostream &  out,
const FMatrix< T, M, N > &  A 
)
friend

Writes FMatrix to binary stream

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


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