TooN 2.1
|
A matrix. More...
#include <matrix.hh>
Public Member Functions | |
const double & | operator() (int r, int c) const |
const double & | operator[] (const std::pair< int, int > &row_col) const |
double & | operator[] (const std::pair< int, int > &row_col) |
double & | operator() (int r, int c) |
const Vector & | operator[] (int r) const |
Vector & | operator[] (int r) |
int | num_rows () const |
int | num_cols () const |
Construction and destruction | |
Matrix () | |
Matrix (int rows, int cols) | |
Matrix (Precision *p) | |
Matrix (Precision *p, int r, int c) | |
Matrix (Precision *data, int rows, int cols, int rowstride, int colstride, Internal::Slicing) | |
template<class Op > | |
Matrix (const Operator< Op > &op) | |
template<int Rows2, int Cols2, typename Precision2 , typename Base2 > | |
Matrix (const Matrix< Rows2, Cols2, Precision2, Base2 > &from) | |
Assignment | |
operator = from copy | |
Matrix & | operator= (const Matrix &from) |
template<class Op > | |
Matrix & | operator= (const Operator< Op > &op) |
template<int Rows2, int Cols2, typename Precision2 , typename Base2 > | |
Matrix & | operator= (const Matrix< Rows2, Cols2, Precision2, Base2 > &from) |
operations on the matrix | |
Matrix & | operator*= (const Precision rhs) |
Matrix & | operator/= (const Precision rhs) |
template<int Rows2, int Cols2, typename Precision2 , typename Base2 > | |
Matrix & | operator+= (const Matrix< Rows2, Cols2, Precision2, Base2 > &from) |
template<class Op > | |
Matrix & | operator+= (const Operator< Op > &op) |
template<class Op > | |
Matrix & | operator-= (const Operator< Op > &op) |
template<int Rows2, int Cols2, typename Precision2 , typename Base2 > | |
Matrix & | operator-= (const Matrix< Rows2, Cols2, Precision2, Base2 > &from) |
template<int Rows2, int Cols2, typename Precision2 , typename Base2 > | |
bool | operator== (const Matrix< Rows2, Cols2, Precision2, Base2 > &rhs) const |
template<int Rows2, int Cols2, typename Precision2 , typename Base2 > | |
bool | operator!= (const Matrix< Rows2, Cols2, Precision2, Base2 > &rhs) const |
template<class Op > | |
bool | operator!= (const Operator< Op > &op) |
Misc | |
Matrix & | ref () |
Transpose and sub-matrices | |
const Matrix< Cols, Rows > & | T () const |
Matrix< Cols, Rows > & | T () |
template<Rstart , Cstart , Rsize , Csize > | |
const Matrix< Rsize, Csize > & | slice () const |
template<Rstart , Cstart , Rsize , Csize > | |
Matrix< Rsize, Csize > & | slice () |
const Matrix & | slice (int rstart, int cstart, int rsize, int csize) const |
Matrix & | slice (int rstart, int cstart, int rsize, int csize) |
A matrix.
Support is provided for all the usual matrix operations:
See individual member function documentation for examples of usage.
The library provides classes for statically and dynamically sized matrices. As with Vectors, statically sized matrices are more efficient, since their size is determined at compile-time, not run-time. To create a matrix, use:
Matrix<3,4> M;
or replace 3 and 4 with the dimensions of your choice. If the matrix is square, it can be declared as:
Matrix<3> M;
which just is a synonym for Matrix<3,3>
. Matrices can also be constructed from pointers or static 1D or 2D arrays of doubles:
Matrix<2,3, Reference::RowMajor> M2 = Data(1,2,3,4,5,6);
To create a dynamically sized matrix, use:
where num_rows and num_cols are integers which will be evaluated at run time.
Half-dynamic matriced can be constructed in either dimension:
Matrix<Dynamic, 2> M(num_rows, 2);
note that the static dimension must be provided, but it is ignored.
Matrix<>
is a synonym for Matrix<Dynamic, Dynamic>
.
The library supports both row major (the default - but you can change this if you prefer) and column major layout ordering. Row major implies that the matrix is laid out in memory in raster scan order:
You can override the default for a specific matrix by specifying the layout when you construct it:
Matrix<3,3,double,ColMajor> M1; Matrix<Dynamic,Dynamic,double,RowMajor> M2(nrows, ncols);
In this case the precision template argument must be given as it precedes the layout argument
Matrix | ( | Precision * | data, |
int | rows, | ||
int | cols, | ||
int | rowstride, | ||
int | colstride, | ||
Internal::Slicing | |||
) |
Advanced construction of dynamically sized slice matrices.
Internal constructor used by GenericMBase::slice(...).
const double& operator() | ( | int | r, |
int | c | ||
) | const |
const double& operator[] | ( | const std::pair< int, int > & | row_col | ) | const |
Access an element from the matrix.
row_col | row_col.first holds the row, row_col.second holds the column. |
This method is not defined by Matrix: it is inherited.
double& operator() | ( | int | r, |
int | c | ||
) |
Access an element from the matrix.
This can be used as either an r-value or an l-value. The index starts at zero, i.e. the top-left element is m(0, 0).
Matrix<2,3> m(Data( 1,2,3 4,5,6)); Matrix<2,3> m(d); m(1,2) = 8; // now d = [1 2 3] // [4 5 8]
This method is not defined by Matrix: it is inherited.
const Vector& operator[] | ( | int | r | ) | const |
Access a row from the matrix.
This can be used either as an r-value or an l-value. The index starts at zero, i.e. the first row is m[0]. To extract a column from a matrix, apply [] to the transpose of the matrix (see example). This can be used either as an r-value or an l-value. The index starts at zero, i.e. the first row (or column) is m[0].
Matrix<2,3> m(Data( 1,2,3 4,5,6)); Matrix<2,3> m(d); Vector<3> v = m[1]; // now v = [4 5 6]; Vector<2> v2 = m.T()[0]; // now v2 = [1 4];
This method is not defined by Matrix: it is inherited.
Vector& operator[] | ( | int | r | ) |
Access a row from the matrix.
This can be used either as an r-value or an l-value. The index starts at zero, i.e. the first row is m[0]. To extract a column from a matrix, apply [] to the transpose of the matrix (see example). This can be used either as an r-value or an l-value. The index starts at zero, i.e. the first row (or column) is m[0].
Matrix<2,3> m(Data( 1,2,3 4,5,6)); Matrix<2,3> m(d); Zero(m[0]); // set the first row to zero Vector<2> v = 8,9; m.T()[1] = v; // now m = [0 8 0] // [4 9 6]
This method is not defined by Matrix: it is inherited.
int num_rows | ( | ) | const |
How many rows does this matrix have?
This method is not defined by Matrix: it is inherited.
Referenced by WLS< Size, Precision >::add_mJ(), WLS< Size, Precision >::add_prior(), SIM3< Precision >::adjoint(), SE3< Precision >::adjoint(), LU< Size, Precision >::backsub(), Cholesky< Size, Precision >::backsub(), Cholesky< Size, Precision >::Cholesky(), WLS< Size, Precision >::compute(), SymEigen< Size, Precision >::compute(), ComputeSymEigen< Size >::compute(), LU< Size, Precision >::compute(), Cholesky< Size, Precision >::compute(), LU< Size, Precision >::determinant(), TooN::determinant(), Cholesky< Size, Precision >::determinant(), TooN::determinant_gaussian_elimination(), TooN::exp(), TooN::Internal::exp_taylor(), TooN::Fill(), DownhillSimplex< N, Precision >::find_next_point(), TooN::gauss_jordan(), TooN::gaussian_elimination(), LU< Size, Precision >::get_inverse(), Cholesky< Size, Precision >::get_inverse(), SVD< Size, Size, Precision >::get_U(), SVD< Size, Size, Precision >::get_VT(), TooN::Internal::log_taylor(), TooN::norm_1(), TooN::norm_fro(), TooN::norm_inf(), DownhillSimplex< N, Precision >::restart(), TooN::sqrt(), TooN::Symmetrize(), TooN::trace(), SIM3< Precision >::trinvadjoint(), and SE3< Precision >::trinvadjoint().
int num_cols | ( | ) | const |
How many columns does this matrix have?
This method is not defined by Matrix: it is inherited.
Referenced by WLS< Size, Precision >::add_sparse_mJ_rows(), SIM3< Precision >::adjoint(), SE3< Precision >::adjoint(), LU< Size, Precision >::backsub(), Cholesky< Size, Precision >::backsub(), Cholesky< Size, Precision >::Cholesky(), SymEigen< Size, Precision >::compute(), ComputeSymEigen< Size >::compute(), LU< Size, Precision >::compute(), Cholesky< Size, Precision >::compute(), TooN::determinant(), TooN::determinant_gaussian_elimination(), TooN::exp(), TooN::Internal::exp_taylor(), TooN::Fill(), DownhillSimplex< N, Precision >::find_next_point(), TooN::gauss_jordan(), TooN::gaussian_elimination(), SVD< Size, Size, Precision >::get_U(), SVD< Size, Size, Precision >::get_VT(), TooN::Internal::log_taylor(), TooN::norm_1(), TooN::norm_fro(), TooN::norm_inf(), TooN::project(), DownhillSimplex< N, Precision >::restart(), TooN::sqrt(), TooN::Symmetrize(), TooN::trace(), SIM3< Precision >::trinvadjoint(), and SE3< Precision >::trinvadjoint().
const Matrix<Cols, Rows>& T | ( | ) | const |
The transpose of the matrix.
This is a very fast operation--it simply reinterprets a row-major matrix as column-major or vice-versa. This can be used as an l-value.
Matrix<2,3> m(Data( 1,2,3 4,5,6)); Matrix<2,3> m(d); Zero(m[0]); // set the first row to zero Vector<2> v = 8,9; m.T()[1] = v; // now m = [0 8 0] // [4 9 6]
This method is not defined by Matrix: it is inherited.
Referenced by WLS< Size, Precision >::add_mJ(), WLS< Size, Precision >::add_mJ_rows(), WLS< Size, Precision >::add_sparse_mJ_rows(), SIM3< Precision >::adjoint(), SE3< Precision >::adjoint(), SymEigen< Size, Precision >::backsub(), SymEigen< Size, Precision >::get_isqrtm(), SymEigen< Size, Precision >::get_pinv(), GR_SVD< M, N, Precision, WANT_U, WANT_V >::get_pinv(), SymEigen< Size, Precision >::get_sqrtm(), GR_SVD< M, N, Precision, WANT_U, WANT_V >::reorder(), SO3< P >::SO3(), SIM3< Precision >::trinvadjoint(), and SE3< Precision >::trinvadjoint().
Matrix<Cols, Rows>& T | ( | ) |
The transpose of the matrix.
This is a very fast operation--it simply reinterprets a row-major matrix as column-major or vice-versa. The result can be used as an l-value.
Matrix<2,3> m(Data( 1,2,3 4,5,6)); Matrix<2,3> m(d); Vector<2> v = 8,9; // Set the first column to v m.T()[0] = v; // now m = [8 2 3] // [9 5 6]
This means that the semantics of M=M.T()
are broken. In general, it is not necessary to say M=M.T()
, since you can use M.T() for free whenever you need the transpose, but if you do need to, you have to use the Tranpose() function defined in helpers.h
.
This method is not defined by Matrix: it is inherited.
const Matrix<Rsize, Csize>& slice | ( | ) | const |
Extract a sub-matrix.
The matrix extracted will be begin at element (Rstart, Cstart) and will contain the next Rsize by Csize elements.
Matrix<2,3> m(Data( 1,2,3 4,5,6 7,8,9)); Matrix<3> m(d); Extract the top-left 2x2 matrix Matrix<2> b = m.slice<0,0,2,2>(); // b = [1 2] // [4 5]
This method is not defined by Matrix: it is inherited.
Referenced by WLS< Size, Precision >::add_sparse_mJ_rows(), and TooN::project().
Matrix<Rsize, Csize>& slice | ( | ) |
Extract a sub-matrix.
The matrix extracted will be begin at element (Rstart, Cstart) and will contain the next Rsize by Csize elements. This can be used as either an r-value or an l-value.
Matrix<2,3> m(Data( 1,2,3 4,5,6)); Matrix<2,3> m(d); Zero(m.slice<0,2,2,1>()); // b = [1 2 0] // [4 5 0]
This method is not defined by Matrix: it is inherited.
const Matrix& slice | ( | int | rstart, |
int | cstart, | ||
int | rsize, | ||
int | csize | ||
) | const |
Extract a sub-matrix with runtime location and size.
The matrix extracted will begin at element (rstart, cstart) and will contain the next rsize by csize elements.
Matrix<> m(3,3); Extract the top-left 2x2 matrix Matrix<2> b = m.slice(0,0,2,2);
This method is not defined by Matrix: it is inherited.
Matrix& slice | ( | int | rstart, |
int | cstart, | ||
int | rsize, | ||
int | csize | ||
) |
Extract a sub-matrix with runtime location and size, which can be used as an l-value.
The matrix extracted will be begin at element (rstart, cstart) and will contain the next rsize by csize elements.
Matrix<> m(3,3); Zero(m.slice(0,0,2,2));
This method is not defined by Matrix: it is inherited.