Minimalist matrix template class. More...
#include <kmatrix.hpp>
Public Types | |
enum | { beg = BEG } |
typedef T | type |
Type of objects contained in the matrix. | |
Public Member Functions | |
void | assign (K_UINT_32 m, K_UINT_32 n, const T *v) |
Copies a C-style array of instances of T in an m by n matrix. | |
KMatrix & | operator= (const KMatrix &M) |
Copy assignment operator. Performs a deep copy. | |
KMatrix & | operator= (const T &a) |
Assigns a copy of a to all elements of the matrix. | |
void | resize (K_UINT_32 m, K_UINT_32 n) |
Resizes the matrix. Resulting matrix contents are undefined. | |
void | swap (KMatrix &M) |
Constant-time swap function between two matrices. | |
Streaming functions | |
void | get (std::istream &is) |
Reads a matrix from a stream. | |
void | put (std::ostream &os) const |
Writes a matrix to a stream. | |
Constructors and destructor. | |
KMatrix (const KMatrix &M) | |
Copy constructor. Performs a deep copy. | |
KMatrix (K_UINT_32 m, K_UINT_32 n, const T *v) | |
Creates an m by n matrix from an array of instances of T . | |
KMatrix (K_UINT_32 m, K_UINT_32 n, const T &a) | |
Creates an m by n matrix of copies of a . | |
KMatrix (K_UINT_32 m, K_UINT_32 n) | |
Creates an m by n matrix of default instances of T . | |
KMatrix () | |
Default constructor. Creates an empty matrix. | |
~KMatrix () | |
Destructor. | |
Member access functions. | |
K_UINT_32 | ncol () const |
Returns n_, the number of columns of the matrix. | |
K_UINT_32 | nrow () const |
Returns m_, the number of rows of the matrix. | |
const T & | operator() (K_UINT_32 i, K_UINT_32 j) const |
Returns the element (i,j) , const version. | |
T & | operator() (K_UINT_32 i, K_UINT_32 j) |
Returns the element (i,j) . | |
Private Member Functions | |
void | init (K_UINT_32 m, K_UINT_32 n) |
Helper function to initialize matrix. | |
Private Attributes | |
K_UINT_32 | m_ |
Number of rows of matrix. | |
T ** | M_ |
Pointer to the start of vimpl_. | |
std::vector< T > | Mimpl_ |
Underlying vector implementation. | |
K_UINT_32 | n_ |
Number of columns of matrix. | |
std::vector< T * > | vimpl_ |
Array of pointers to rows of Mimpl_. |
Minimalist matrix template class.
This matrix class does not define any fancy linear algebra functions, nor even any operator between matrices. Its sole purpose is to well encapsulate an extensible array representing a matrix, and also to allow starting index to be 0 or 1.
T
: Type of elements contained in the matrix. Usually float
or double
.BEG
: Starting index of matrix. Can be either 0 or 1.DGB
: Debug flag. If true
, then bound-checking will be performed, and OutOfBoundError
exceptions can be thrown.T
must be default constructible.T
must be assignable.T
must be serializable.t1
, t2
are instances of T
, is
is of type istream
and os
is of type ostream
, the following expressions must be valid :T(); T t1;
T t1 = t2; T t1(t2); T(t1);
t1 = t2;
is >> t1;
operator>>()
os << t1;
operator<<()
operator>>()
and operator<<()
must be compatible. Also, operator&()
must not have been overloaded. Definition at line 72 of file kmatrix.hpp.
typedef T Kalman::KMatrix< T, BEG, DBG >::type |
Type of objects contained in the matrix.
Definition at line 75 of file kmatrix.hpp.
anonymous enum |
Definition at line 77 of file kmatrix.hpp.
Kalman::KMatrix< T, BEG, DBG >::KMatrix | ( | ) | [inline] |
Default constructor. Creates an empty matrix.
Definition at line 135 of file kmatrix_impl.hpp.
Kalman::KMatrix< T, BEG, DBG >::KMatrix | ( | K_UINT_32 | m, | |
K_UINT_32 | n | |||
) | [inline] |
Creates an m
by n
matrix of default instances of T
.
m | Number of rows in matrix. Can be 0. | |
n | Number of columns in matrix. Can be 0. |
m
or n
is 0, then both the number of rows and the number of columns will be set to 0. Definition at line 144 of file kmatrix_impl.hpp.
Kalman::KMatrix< T, BEG, DBG >::KMatrix | ( | K_UINT_32 | m, | |
K_UINT_32 | n, | |||
const T & | a | |||
) | [inline] |
Creates an m
by n
matrix of copies of a
.
m | Number of rows in matrix. Can be 0. | |
n | Number of columns in matrix. Can be 0. | |
a | Value to copy multiple times in the matrix. |
m
or n
is 0, then both the number of rows and the number of columns will be set to 0. Definition at line 155 of file kmatrix_impl.hpp.
Kalman::KMatrix< T, BEG, DBG >::KMatrix | ( | K_UINT_32 | m, | |
K_UINT_32 | n, | |||
const T * | v | |||
) | [inline] |
Creates an m
by n
matrix from an array of instances of T
.
This function allows to transform a C-style array of T
objects in a KMatrix<T, BEG, DBG>
equivalent array. Note that objects from the C-style array are copied into the matrix, which may slow down application if used extensively. The elements are copied row-wise, that is to say, the elements of the C-style array first fill the first row of the matrix, then the second, etc.
m | Number of rows in matrix. Can be 0. | |
n | Number of columns in matrix. Can be 0. | |
v | Pointer to an m*n array of T objects. |
m
or n
is 0, then both the number of rows and the number of columns will be set to 0. Definition at line 172 of file kmatrix_impl.hpp.
Kalman::KMatrix< T, BEG, DBG >::KMatrix | ( | const KMatrix< T, BEG, DBG > & | M | ) | [inline] |
Copy constructor. Performs a deep copy.
M | Matrix to copy. Can be an empty matrix. |
Definition at line 180 of file kmatrix_impl.hpp.
Kalman::KMatrix< T, BEG, DBG >::~KMatrix | ( | ) | [inline] |
Destructor.
Definition at line 186 of file kmatrix_impl.hpp.
void Kalman::KMatrix< T, BEG, DBG >::assign | ( | K_UINT_32 | m, | |
K_UINT_32 | n, | |||
const T * | v | |||
) | [inline] |
Copies a C-style array of instances of T
in an m
by n
matrix.
The elements are copied row-wise, that is to say, the elements of the C-style array first fill the first row of the matrix, then the second, etc.
m | Number of rows in matrix. Can be 0. | |
n | Number of columns in matrix. Can be 0. | |
v | Pointer to first element to copy from C-style array. |
Definition at line 320 of file kmatrix_impl.hpp.
void Kalman::KMatrix< T, BEG, DBG >::get | ( | std::istream & | is | ) | [inline] |
Reads a matrix from a stream.
This function will extract nrow()*ncol()
elements from a stream to fill the matrix, while considering the formatting constraints of the current matrix printing context.
is | A reference to the input stream. |
Definition at line 347 of file kmatrix_impl.hpp.
void Kalman::KMatrix< T, BEG, DBG >::init | ( | K_UINT_32 | m, | |
K_UINT_32 | n | |||
) | [inline, private] |
Helper function to initialize matrix.
This function is called by all the constructors and also by the resize()
function. If either m
or n
is 0, then both m_ and n_ will be set to 0.
m*n
elements. m
elements. m | Number of rows. Can be 0. | |
n | Number of cols. Can be 0. |
Definition at line 104 of file kmatrix_impl.hpp.
K_UINT_32 Kalman::KMatrix< T, BEG, DBG >::ncol | ( | ) | const [inline] |
Returns n_, the number of columns of the matrix.
Definition at line 264 of file kmatrix_impl.hpp.
K_UINT_32 Kalman::KMatrix< T, BEG, DBG >::nrow | ( | ) | const [inline] |
Returns m_, the number of rows of the matrix.
Definition at line 257 of file kmatrix_impl.hpp.
const T & Kalman::KMatrix< T, BEG, DBG >::operator() | ( | K_UINT_32 | i, | |
K_UINT_32 | j | |||
) | const [inline] |
Returns the element (i,j)
, const
version.
i | Row index of matrix element. | |
j | Column index of matrix element. |
const
reference to the element (i,j)
. OutOfBoundError | Thrown if i or j is out of matrix bounds and DBG == true . |
Definition at line 228 of file kmatrix_impl.hpp.
T & Kalman::KMatrix< T, BEG, DBG >::operator() | ( | K_UINT_32 | i, | |
K_UINT_32 | j | |||
) | [inline] |
Returns the element (i,j)
.
i | Row index of matrix element. | |
j | Column index of matrix element. |
(i,j)
. OutOfBoundError | Thrown if i or j is out of matrix bounds and DBG == true . |
Definition at line 194 of file kmatrix_impl.hpp.
KMatrix< T, BEG, DBG > & Kalman::KMatrix< T, BEG, DBG >::operator= | ( | const KMatrix< T, BEG, DBG > & | M | ) | [inline] |
Copy assignment operator. Performs a deep copy.
M | Matrix to copy. |
Definition at line 306 of file kmatrix_impl.hpp.
KMatrix< T, BEG, DBG > & Kalman::KMatrix< T, BEG, DBG >::operator= | ( | const T & | a | ) | [inline] |
Assigns a copy of a
to all elements of the matrix.
a | Instance to copy to each element of matrix. |
Definition at line 290 of file kmatrix_impl.hpp.
void Kalman::KMatrix< T, BEG, DBG >::put | ( | std::ostream & | os | ) | const [inline] |
Writes a matrix to a stream.
This function will send all the matrix elements to a stream, while considering the formatting constraints of the current matrix printing context.
os | A reference to the output stream. |
Definition at line 423 of file kmatrix_impl.hpp.
void Kalman::KMatrix< T, BEG, DBG >::resize | ( | K_UINT_32 | m, | |
K_UINT_32 | n | |||
) | [inline] |
Resizes the matrix. Resulting matrix contents are undefined.
m | Number of rows in matrix. Can be 0. | |
n | Number of columns in matrix. Can be 0. |
KMatrix<T, BEG, DBG>(M).swap(M)
. m
or n
is 0, then both the number of rows and the number of columns will be set to 0. Definition at line 277 of file kmatrix_impl.hpp.
void Kalman::KMatrix< T, BEG, DBG >::swap | ( | KMatrix< T, BEG, DBG > & | M | ) | [inline] |
Constant-time swap function between two matrices.
This function is fast, since it exchanges pointers to underlying implementation without copying any element.
M | Matrix to swap. |
Definition at line 332 of file kmatrix_impl.hpp.
K_UINT_32 Kalman::KMatrix< T, BEG, DBG >::m_ [private] |
Number of rows of matrix.
Definition at line 161 of file kmatrix.hpp.
T** Kalman::KMatrix< T, BEG, DBG >::M_ [private] |
Pointer to the start of vimpl_.
In fact, M_ is such that &M_[beg] == &vimpl_[0]
. This means also that &M_[beg][beg] == &Mimpl_[0][0]
.
M_[0]
is not defined for beg != 0
. Definition at line 160 of file kmatrix.hpp.
std::vector<T> Kalman::KMatrix< T, BEG, DBG >::Mimpl_ [private] |
Underlying vector implementation.
Definition at line 152 of file kmatrix.hpp.
K_UINT_32 Kalman::KMatrix< T, BEG, DBG >::n_ [private] |
Number of columns of matrix.
Definition at line 162 of file kmatrix.hpp.
std::vector<T*> Kalman::KMatrix< T, BEG, DBG >::vimpl_ [private] |
Array of pointers to rows of Mimpl_.
In fact, vimpl_ is such that &vimpl_[i][beg] == &Mimpl_[i*n_]
.
Definition at line 151 of file kmatrix.hpp.