#include <TooN/toon.h>
Public Member Functions | |
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<class T, class Op> | |
Matrix (const T &arg, int rows, int cols, const Operator< Op > &) | |
template<class LHS, class RHS, class Op> | |
Matrix (const LHS &lhs, const RHS &rhs, int rows, int cols, const Operator< Op > &) | |
template<int Rows2, int Cols2, typename Precision2, typename Base2> | |
Matrix (const Matrix< Rows2, Cols2, Precision2, Base2 > &from) | |
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) |
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) |
template<int Rows2, int Cols2, typename Precision2, typename Base2> | |
bool | operator!= (const Matrix< Rows2, Cols2, Precision2, Base2 > &rhs) |
Matrix & | ref () |
Constructors | |
initialise the vector to any particular values | Matrix () |
Matrix (double darray[Rows *Cols]) | |
Matrix (double darray[Rows][Cols]) | |
Matrix (const Matrix< Rows, Cols > &from) | |
Reading and writing elements | |
are mismatched Matrix< Rows, Cols > & | operator= (const Matrix< Rows, Cols > &from) |
resize (int rows, int cols) | |
const double & | operator() (int r, int c) const |
double & | operator() (int r, int c) |
const Vector & | operator[] (int r) const |
Vector & | operator[] (int r) |
int | num_rows () const |
int | num_cols () const |
Transpose and sub-matrices | |
What is the memory layout of this matrix? | |
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) |
Related Functions | |
(Note that these are not member functions.) | |
Arithmetic operations | |
template<int Rows, int Cols> | |
Matrix< Rows, Cols > | operator+ (const Matrix< Rows, Cols > &lhs, const Matrix< Rows, Cols > &rhs) |
template<int Rows, int Cols> | |
Matrix< Rows, Cols > & | operator+= (Matrix< Rows, Cols > &lhs, const Matrix< Rows, Cols > &rhs) |
template<int Rows, int Cols> | |
Matrix< Rows, Cols > | operator- (const Matrix< Rows, Cols > &lhs, const Matrix< Rows, Cols > &rhs) |
template<int Rows, int Cols> | |
Matrix< Rows, Cols > & | operator-= (Matrix< Rows, Cols > &lhs, const Matrix< Rows, Cols > &rhs) |
template<int Rows, int Cols> | |
Matrix< Rows, Cols > | operator * (const Matrix< Rows, Cols > &lhs, double rhs) |
template<int rows, int Cols> | |
Matrix< Rows, Cols > | operator * (double lhs, const Matrix< Rows, Cols > &rhs) |
template<int Rows, int Cols> | |
Matrix< Rows, Cols > & | operator *= (const Matrix< Rows, Cols > &lhs, const double &rhs) |
template<int Rows, int Cols> | |
Matrix< Rows, Cols > | operator/ (const Matrix< Rows, Cols > &lhs, double rhs) |
template<int Rows, int Cols> | |
Matrix< Rows, Cols > & | operator/= (const Matrix< Rows, Cols > &lhs, const double &rhs) |
template<int Rows, int Cols> | |
Vector< Rows > | operator * (const Matrix< Rows, Cols > &lhs, const Vector< Cols > &rhs) |
template<int Rows, int Cols> | |
Vector< Cols > | operator * (const Vector< Rows > &lhs, const Matrix< Rows, Cols > &rhs) |
template<int Rows, int Inter, int Cols> | |
Matrix< Cols > | operator * (const Matrix< Inter, Rows > &lhs, const Matrix< Inter, Cols > &rhs) |
template<int Rows, Cols > | |
Matrix< Rows, Cols > | diagmult (const Vector< Rows > &lhs, const Matrix< Rows, Cols > &rhs) |
template<int Rows, Cols > | |
Matrix< Rows, Cols > | diagmult (const Matrix< Rows, Cols > &lhs, const Vector< Cols > &rhs) |
Input/output | |
template<int Rows, Cols > | |
std::ostream & | operator<< (std::ostream &os, const Matrix< Rows, Cols > &v) |
template<int Rows, Cols > | |
std::istream & | operator<< (std::istream &is, Matrix< Rows, Cols > &v) |
Support is provided for all the usual matrix operations:
See individual member function documentation for examples of usage.
Matrix<3,4> M;
Matrix<3> M;
Matrix<3,3>
. Matrices can also be constructed from pointers or static 1D or 2D arrays of doubles: void foo(double* dptr) { Matrix<3> M1 (dptr); double dvals1[9]={1,2,3,4,5,6}; Matrix<2,3> M2 (dvals1); double dvals2[2][3]={{1,2,3},{4,5,6}}; Matrix<2,3> M3 (dvals2); // ... }
void bar(int r, int c, double* dptr) { Matrix<> M (r,c,dptr); // ... }
Matrix<>
is a synonym for Matrix<Dynamic, Dynamic>
which is Matrix<-1,-1>
You can override the default for a specific matrix by specifying the layout when you construct it:
Matrix<3,3,double,ColMajor> M1; Matrix<-1,-1,double,RowMajor> M2(nrows, ncols);
initialise the vector to any particular values TooN::Matrix< Rows, Cols >::Matrix | ( | ) |
Default constructor.
For fixed sized matrices, this does nothing, i.e. does not guarantee to For dynamically sized matrices, this sets up a 0x0 matrix.
TooN::Matrix< Rows, Cols >::Matrix | ( | double | darray[Rows *Cols] | ) |
Construct a (fixed-size) matrix from a 1D array of doubles.
By default the layout ordering is row major (along the rows first i.e. raster scan order), unless ColMajor
is specified in the template arguments (see detailed description). No errors are generated if the array is the wrong length.
double d[4] = {1, 2, 3, 4}; Matrix<2> m(d); // now m = [1 2] // [3 4]
TooN::Matrix< Rows, Cols >::Matrix | ( | double | darray[Rows][Cols] | ) |
Construct a (fixed-size) matrix from a 2D array of doubles.
No errors are generated if the array is the wrong length.
double d[2][3] = {{1, 2, 3}, {4, 5, 6}}; Matrix<2,3> m(d); // now m = [1 2 3] // [4 5 6]
TooN::Matrix< Rows, Cols >::Matrix | ( | const Matrix< Rows, Cols > & | from | ) |
Copy constructor.
are mismatched Matrix<Rows, Cols>& TooN::Matrix< Rows, Cols >::operator= | ( | const Matrix< Rows, Cols > & | from | ) |
Assignment operator.
For dynamically sized matrices this will not cause a resize if the sizes
TooN::Matrix< Rows, Cols >::resize | ( | int | rows, | |
int | cols | |||
) |
Resize a dynamically sized matrix.
const double& TooN::Matrix< Rows, Cols >::operator() | ( | int | r, | |
int | c | |||
) | const |
Access an element from the matrix.
The index starts at zero, i.e. the top-left element is m(0, 0).
double d[2][3] = {{1, 2, 3}, {4, 5, 6}}; Matrix<2,3> m(d); double e = m(1,2); // now e = 6.0;
double& TooN::Matrix< Rows, Cols >::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).
double d[2][3] = {{1, 2, 3}, {4, 5, 6}}; Matrix<2,3> m(d); m(1,2) = 8; // now d = [1 2 3] // [4 5 8]
const Vector& TooN::Matrix< Rows, Cols >::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].
double d[2][3] = {{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];
Vector& TooN::Matrix< Rows, Cols >::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].
double d[2][3] = {{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]
int TooN::Matrix< Rows, Cols >::num_rows | ( | ) | const |
How many rows does this matrix have?
int TooN::Matrix< Rows, Cols >::num_cols | ( | ) | const |
How many columns does this matrix have?
const Matrix<Cols, Rows>& TooN::Matrix< Rows, Cols >::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.
double d[2][3] = {{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]
Matrix<Cols, Rows>& TooN::Matrix< Rows, Cols >::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.
double d[2][3] = {{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]
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
.
const Matrix<Rsize, Csize>& TooN::Matrix< Rows, Cols >::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.
double d[3][3] = {{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]
Matrix<Rsize, Csize>& TooN::Matrix< Rows, Cols >::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.
double d[2][3] = {{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]
const Matrix& TooN::Matrix< Rows, Cols >::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);
Matrix& TooN::Matrix< Rows, Cols >::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));
Matrix< Rows, Cols > operator+ | ( | const Matrix< Rows, Cols > & | lhs, | |
const Matrix< Rows, Cols > & | rhs | |||
) | [related] |
Add two matrices.
Returns the sum of the two matrices.
Matrix< Rows, Cols > & operator+= | ( | Matrix< Rows, Cols > & | lhs, | |
const Matrix< Rows, Cols > & | rhs | |||
) | [related] |
Add a matrix to another one.
Returns the modified matrix.
Matrix< Rows, Cols > operator- | ( | const Matrix< Rows, Cols > & | lhs, | |
const Matrix< Rows, Cols > & | rhs | |||
) | [related] |
Subtract two matrices.
Returns the dfference of the two matrices.
Matrix< Rows, Cols > & operator-= | ( | Matrix< Rows, Cols > & | lhs, | |
const Matrix< Rows, Cols > & | rhs | |||
) | [related] |
Subtract one matrix from another one.
Returns the modified matrix.
Matrix< Rows, Cols > operator * | ( | const Matrix< Rows, Cols > & | lhs, | |
double | rhs | |||
) | [related] |
Multiply a matrix by a scalar.
Both left- and right- multiply are allowed.
Matrix< Rows, Cols > operator * | ( | double | lhs, | |
const Matrix< Rows, Cols > & | rhs | |||
) | [related] |
Multiply a matrix by a scalar.
Both left- and right- multiply are allowed.
Matrix< Rows, Cols > & operator *= | ( | const Matrix< Rows, Cols > & | lhs, | |
const double & | rhs | |||
) | [related] |
Multiply a matrix by a scalar.
Returns the modified matrix.
Matrix< Rows, Cols > operator/ | ( | const Matrix< Rows, Cols > & | lhs, | |
double | rhs | |||
) | [related] |
Divide a matrix by a scalar.
Matrix< Rows, Cols > & operator/= | ( | const Matrix< Rows, Cols > & | lhs, | |
const double & | rhs | |||
) | [related] |
Divide matrix by a scalar.
Returns the modified matrix.
Vector< Rows > operator * | ( | const Matrix< Rows, Cols > & | lhs, | |
const Vector< Cols > & | rhs | |||
) | [related] |
Multiply a matrix by a vector (with the vector on the right).
This interprets the vector as a column vector.
Vector< Cols > operator * | ( | const Vector< Rows > & | lhs, | |
const Matrix< Rows, Cols > & | rhs | |||
) | [related] |
Multiply a vector by a matrix (with the vector on the left).
This interprets the vector as a row vector.
Matrix< Cols > operator * | ( | const Matrix< Inter, Rows > & | lhs, | |
const Matrix< Inter, Cols > & | rhs | |||
) | [related] |
Multiply a matrix by a matrix.
Matrix< Rows, Cols > diagmult | ( | const Vector< Rows > & | lhs, | |
const Matrix< Rows, Cols > & | rhs | |||
) | [related] |
Multiplication treating the vector as representing a diagonal matrix on the left of the matrix.
Matrix< Rows, Cols > diagmult | ( | const Matrix< Rows, Cols > & | lhs, | |
const Vector< Cols > & | rhs | |||
) | [related] |
Multiplication treating the vector as representing a diagonal matrix on the right of the matrix.
std::ostream & operator<< | ( | std::ostream & | os, | |
const Matrix< Rows, Cols > & | v | |||
) | [related] |
Write the matrix to a stream.
The elements are space-separated with rows being separated by new lines. The numbers are printed with a minimum field width of 12 characters.
std::istream & operator<< | ( | std::istream & | is, | |
Matrix< Rows, Cols > & | v | |||
) | [related] |
Read a matrix from a stream.
The numbers can be comma or white-space separated, and are assumed to be in row-major layout (i.e. scanline order) unless the matrix is defined as ColMajor