golib
0.5
|
#include <gomatrix.h>
Public Types | |
typedef T | value_type |
typedef VectorIterator< T > | vector_iterator |
typedef ConstVectorIterator< T > | const_vector_iterator |
Public Member Functions | |
Matrix (goSize_t rows=4, goSize_t cols=4) | |
Matrix (const Matrix< T > &other) | |
Copy constructor. More... | |
Matrix (T *data, goSize_t r, goSize_t c, goSize_t leadingDim=0) | |
Constructor for using external data. More... | |
vector_iterator | rowBegin () |
Get an iterator to the first row. More... | |
vector_iterator | rowEnd () |
Get an iterator pointing after the last row. More... | |
const_vector_iterator | rowBegin () const |
Get an iterator to the first row. More... | |
const_vector_iterator | rowEnd () const |
Get an iterator pointing after the last row. More... | |
vector_iterator | colBegin () |
Get an iterator to the first column. More... | |
vector_iterator | colEnd () |
Get an iterator pointing after the last column. More... | |
const_vector_iterator | colBegin () const |
Get an iterator to the first column. More... | |
const_vector_iterator | colEnd () const |
Get an iterator pointing after the last column. More... | |
bool | setData (T *data, goSize_t r, goSize_t c, goSize_t leadingDim=0) |
Set external data. More... | |
bool | setData (const T *data, goSize_t r, goSize_t c, goSize_t leadingDim=0) const |
bool | resize (goSize_t rows, goSize_t cols) |
Resize (re-allocate) the matrix. More... | |
bool | reshape (goSize_t rows, goSize_t cols) |
Reshape this matrix as long as the new shape results in the same total number of elements. More... | |
template<class To > | |
bool | resize (const Matrix< To > &o) |
bool | transpose () |
Transpose the data. This is very slow and should be used scarcely. For Multiplication with transposition, use goMath::MatrixMult(). | |
bool | getTranspose (Matrix< T > &trans) const |
Get a transposed copy of this matrix. More... | |
Matrix< T > | getTranspose () const |
bool | invert () |
void | power (T scalar) |
void | flip (goSize_t dim=0) |
Flip in row or column direction. More... | |
void | shiftRows (goIndex_t offset, Matrix< T > &ret) const |
Shift (cyclically permute) rows. More... | |
void | shiftColumns (goIndex_t offset, Matrix< T > &ret) const |
Shift (cyclically permute) columns. More... | |
Matrix< T > & | operator= (const Matrix< T > &other) |
Deep copy operator. More... | |
bool | operator== (const Matrix< T > &other) const |
bool | operator!= (const Matrix< T > &other) const |
T * | getData () |
Get data pointer. More... | |
const T * | getData () const |
Get data pointer. More... | |
T * | getPtr () |
Get data pointer. Same as getData(). | |
const T * | getPtr () const |
Get data pointer. Same as getData(). | |
goSize_t | getColumns () const |
Get number of columns. More... | |
goSize_t | getRows () const |
Get number of rows. More... | |
goSize_t | getLeadingDimension () const |
Get the leading dimension. In row major order, this is the number of columns. This can be directly used as LDX parameter in CBLAS routines. From http://www.inf.bv.tum.de/~heisserer/softwarelab04/index.html "Note that for cblas-functions the leading dimension (for 2D arrays in C-fashion, i.e. row major order) is the number of columns of the matrix (not the rows as in Fortran). The leading dimension is the number of entries in memory that separate the e.g. first elements of rows in c-fashion storage (row major order, i.e. elements of one row are contiguous in memory). As Fortran stores in column major order the leading dimension is the number of rows.". More... | |
T | sum () const |
Sum over all elements. More... | |
void | sum (int dimension, Matrix< T > &ret) const |
Sum over all columns / all rows. More... | |
void | sum (int dimension, goMath::Vector< T > &ret) const |
int | dim1 () const |
int | dim2 () const |
const Matrix< T > & | copy () const |
void | ref (goSize_t startRow, goSize_t startColumn, goSize_t num_rows, goSize_t num_cols, Matrix< T > &refMatrix) |
Make a reference to sub-matrix. More... | |
bool | copy (goSize_t startRow, goSize_t startCol, goSize_t endRow, goSize_t endCol, Matrix< T > &target, bool trans=false) const |
Copies sub-matrix from this matrix to target matrix. More... | |
bool | copy (goSize_t startRow, goSize_t startCol, goSize_t endRow, goSize_t endCol, goSize_t target_row, goSize_t target_col, Matrix< T > &target, bool trans=false) const |
Copy rectangular sub-matrix to another matrix. More... | |
bool | copy (Matrix< T > &target) const |
Copies this matrix to target matrix. More... | |
void | ref (goSize_t startRow, goSize_t startColumn, goSize_t num_rows, goSize_t num_cols, const Matrix< T > &refMatrix) const |
Make a reference to sub-matrix. More... | |
void | refRow (goSize_t row, goMath::Vector< T > &v) |
Makes a vector reference a row from this matrix. More... | |
void | refRow (goSize_t row, goSize_t column, goSize_t length, goMath::Vector< T > &v) |
Reference to a sub row. More... | |
void | setRow (goSize_t row, const goMath::Vector< T > &v) |
void | setColumn (goSize_t col, const goMath::Vector< T > &v) |
void | refRow (goSize_t row, const goMath::Vector< T > &v) const |
Const reference to row. More... | |
void | refRow (goSize_t row, goSize_t column, goSize_t length, const goMath::Vector< T > &v) const |
Reference to a sub row. More... | |
void | refColumn (goSize_t column, goMath::Vector< T > &v) |
Makes a vector reference a column from this matrix. More... | |
void | refColumn (goSize_t row, goSize_t column, goSize_t length, goMath::Vector< T > &v) |
Reference to a sub column. More... | |
void | refColumn (goSize_t column, const goMath::Vector< T > &v) const |
Const reference to column. More... | |
void | refColumn (goSize_t row, goSize_t column, goSize_t length, const goMath::Vector< T > &v) const |
Reference to a sub column. More... | |
template<class To > | |
void | copyRow (goSize_t row, goMath::Vector< To > &vRet) const |
Copies a row to vector vRet. More... | |
template<class To > | |
void | copyColumn (goSize_t col, goMath::Vector< To > &vRet) const |
Copied a column to vector vRet. More... | |
void | swapRows (goIndex_t i1, goIndex_t i2) |
void | swapColumns (goIndex_t i1, goIndex_t i2) |
T & | operator() (goIndex_t i, goIndex_t j) |
const T & | operator() (goIndex_t i, goIndex_t j) const |
void | operator() (goIndex_t i1, goIndex_t j1, goIndex_t i2, goIndex_t j2, Matrix< T > &target) const |
References sub-matrix from i1,j1 to i2,j2 into target. More... | |
void | operator() (const Matrix< T > &source, goIndex_t i1, goIndex_t j1, goIndex_t i2, goIndex_t j2) |
Copy source to sub-matrix from i1,j1 to i2,j2 in this matrix. More... | |
T & | operator[] (goSize_t index) |
const T & | operator[] (goSize_t index) const |
Matrix< T > | operator* (const Matrix< T > &other) const |
Matrix multiplication. More... | |
Matrix< T > | operator- (const Matrix< T > &other) const |
Matrix< T > | operator+ (const Matrix< T > &other) const |
Matrix< T > & | operator*= (const Matrix< T > &other) |
this = this * other More... | |
Matrix< T > & | operator+= (const Matrix< T > &other) |
Element-wise addition. More... | |
Matrix< T > & | operator+= (T scalar) |
Matrix< T > & | operator-= (const Matrix< T > &other) |
Element-wise subtraction. More... | |
Matrix< T > & | operator-= (T scalar) |
goMath::Vector< T > | operator* (const goMath::Vector< T > &v) const |
Matrix vector multiplication. More... | |
Matrix< T > & | operator*= (T scalar) |
Multiplication by a scalar. More... | |
Matrix< T > | operator* (T scalar) |
Matrix< T > & | operator/= (T scalar) |
Division by a scalar. More... | |
bool | multiplyElements (const Matrix< T > &other) |
Element-wise multiplication. More... | |
T | norm () const |
Calculate the Frobenius norm ![]() ![]() ![]() | |
T | trace () const |
Calculates the trace ![]() | |
void | setUnity () |
Load identity matrix. | |
void | setIdentity () |
Load identity matrix. | |
void | fill (T v) |
Fill matrix with a value v. More... | |
void | print () const |
bool | writeASCII (FILE *f) const |
bool | writeASCII (const char *fname) const |
bool | readASCII (FILE *f) |
bool | readASCIISimple (FILE *f) |
bool | readASCII (const char *f) |
template<> | |
goMath::Matrix< goDouble > | operator* (const goMath::Matrix< goDouble > &other) const |
template<> | |
goMath::Matrix< goFloat > | operator* (const goMath::Matrix< goFloat > &other) const |
template<> | |
Matrix< goFloat > & | operator*= (const Matrix< goFloat > &other) |
template<> | |
Matrix< goDouble > & | operator*= (const Matrix< goDouble > &other) |
template<> | |
goMath::Vector< goFloat > | operator* (const goMath::Vector< goFloat > &v) const |
template<> | |
goMath::Vector< goDouble > | operator* (const goMath::Vector< goDouble > &v) const |
template<> | |
goComplexf | trace () const |
template<> | |
void | power (goFloat scalar) |
template<> | |
void | power (goDouble scalar) |
template<> | |
goComplexf | norm () const |
template<> | |
bool | writeASCII (FILE *f) const |
template<> | |
bool | readASCII (FILE *f) |
template<> | |
bool | invert () |
template<> | |
bool | invert () |
Static Public Attributes | |
static const bool | rowMajor = goMath::rowMajor |
Protected Attributes | |
bool | externalData |
T * | matrix |
goSize_t | rows |
goSize_t | columns |
goSize_t | leadingDimension |
Matrix class.
This class uses linear data storage, row major, i.e. rows are contigious in memory. It is designed to be compatible with CBLAS operations. Leading dimension in CBLAS is here the number of columns – get it via getLeadingDimension().
There is no support for [i][j] indexing anymore. Use (i,j) instead.
goMath::Matrix< T >::Matrix | ( | goSize_t | r = 4 , |
goSize_t | c = 4 |
||
) |
y | Number of rows. |
x | Number of columns. |
goMath::Matrix< T >::Matrix | ( | const Matrix< T > & | other | ) |
Copy constructor.
other | Other matrix. Will be deep copied. |
goMath::Matrix< T >::Matrix | ( | T * | data, |
goSize_t | r, | ||
goSize_t | c, | ||
goSize_t | leadingDim = 0 |
||
) |
Constructor for using external data.
data | Pointer to the external data. |
r | Rows |
c | Columns |
leadingDim | leading dimension – if 0, is set to c. |
data | Pointer to the external data. |
r | Rows |
c | Columns |
goMath::Matrix< T >::vector_iterator goMath::Matrix< T >::colBegin | ( | ) |
Get an iterator to the first column.
goMath::Matrix< T >::const_vector_iterator goMath::Matrix< T >::colBegin | ( | ) | const |
Get an iterator to the first column.
goMath::Matrix< T >::vector_iterator goMath::Matrix< T >::colEnd | ( | ) |
Get an iterator pointing after the last column.
goMath::Matrix< T >::const_vector_iterator goMath::Matrix< T >::colEnd | ( | ) | const |
Get an iterator pointing after the last column.
bool goMath::Matrix< T >::copy | ( | goSize_t | startRow, |
goSize_t | startCol, | ||
goSize_t | endRow, | ||
goSize_t | endCol, | ||
goSize_t | target_row, | ||
goSize_t | target_col, | ||
goMath::Matrix< T > & | target, | ||
bool | trans = false |
||
) | const |
Copy rectangular sub-matrix to another matrix.
Copies from this matrix the sub-matrix (startRow...endRow , startCol...endCol) to the sub-matrix in target starting at target_row,target_col.
Does not do resizing. If the target is too small, nothing is copied.
startRow | Starting row in this matrix. |
startCol | Starting column in this matrix. |
endRow | End row in this matrix (inclusive) |
endCol | End column in this matrix (inclusive) |
target_row | Starting row in target |
target_col | Starting column in target |
target | Target matrix |
trans | Transpose while copying (target = transpose(this)) |
bool goMath::Matrix< T >::copy | ( | goSize_t | startRow, |
goSize_t | startCol, | ||
goSize_t | endRow, | ||
goSize_t | endCol, | ||
goMath::Matrix< T > & | target, | ||
bool | trans = false |
||
) | const |
Copies sub-matrix from this matrix to target matrix.
Copies the given sub-matrix from this to target starting in target at 0,0.
startRow | |
startCol | |
endRow | |
endCol | |
target | |
trans | Transpose while copying (target = transpose(this)) |
bool goMath::Matrix< T >::copy | ( | goMath::Matrix< T > & | target | ) | const |
Copies this matrix to target matrix.
Target is not resized, this matrix is copied to target starting at 0,0. If target is too small, false is returned.
target |
|
inline |
|
inline |
Copies a row to vector vRet.
vRet is resized if it vRet.getSize() != this->getColumns().
row | Row to copy. |
vRet | Vector. |
void goMath::Matrix< T >::fill | ( | T | v | ) |
Fill matrix with a value v.
v | Value to fill with. |
void goMath::Matrix< T >::flip | ( | goSize_t | dim = 0 | ) |
Flip in row or column direction.
dim | If 0, flips rows, otherwise flips columns. |
|
inline |
Get number of columns.
|
inline |
Get data pointer.
|
inline |
Get data pointer.
|
inline |
Get the leading dimension. In row major order, this is the number of columns. This can be directly used as LDX parameter in CBLAS routines.
From http://www.inf.bv.tum.de/~heisserer/softwarelab04/index.html
"Note that for cblas-functions the leading dimension (for 2D arrays in C-fashion, i.e. row major order) is the number of columns of the matrix (not the rows as in Fortran). The leading dimension is the number of entries in memory that separate the e.g. first elements of rows in c-fashion storage (row major order, i.e. elements of one row are contiguous in memory). As Fortran stores in column major order the leading dimension is the number of rows.".
|
inline |
Get number of rows.
bool goMath::Matrix< T >::getTranspose | ( | goMath::Matrix< T > & | trans | ) | const |
Get a transposed copy of this matrix.
trans | Contains the transpose after the method returned. |
bool goMath::Matrix< T >::multiplyElements | ( | const Matrix< T > & | other | ) |
Element-wise multiplication.
this . other
other | Other matrix. |
T goMath::Matrix< T >::norm | ( | ) | const |
Calculate the Frobenius norm where
is the
'th singular value.
void goMath::Matrix< T >::operator() | ( | const Matrix< T > & | source, |
goIndex_t | i1, | ||
goIndex_t | j1, | ||
goIndex_t | i2, | ||
goIndex_t | j2 | ||
) |
Copy source to sub-matrix from i1,j1 to i2,j2 in this matrix.
void goMath::Matrix< T >::operator() | ( | goIndex_t | i1, |
goIndex_t | j1, | ||
goIndex_t | i2, | ||
goIndex_t | j2, | ||
goMath::Matrix< T > & | target | ||
) | const |
References sub-matrix from i1,j1 to i2,j2 into target.
To make clear it is referencing, use the ref() method.
goMath::Vector< T > goMath::Matrix< T >::operator* | ( | const goMath::Vector< T > & | v | ) | const |
Matrix vector multiplication.
v | A vector. |
goMath::Matrix< T > goMath::Matrix< T >::operator* | ( | const Matrix< T > & | other | ) | const |
Matrix multiplication.
other | A matrix. |
goMath::Matrix< T > & goMath::Matrix< T >::operator*= | ( | const Matrix< T > & | other | ) |
this = this * other
other | A Matrix. |
|
inline |
Multiplication by a scalar.
scalar | A scalar. |
goMath::Matrix< T > & goMath::Matrix< T >::operator+= | ( | const Matrix< T > & | other | ) |
Element-wise addition.
other |
goMath::Matrix< T > & goMath::Matrix< T >::operator-= | ( | const Matrix< T > & | other | ) |
Element-wise subtraction.
other |
|
inline |
Division by a scalar.
scalar |
goMath::Matrix< T > & goMath::Matrix< T >::operator= | ( | const Matrix< T > & | other | ) |
Deep copy operator.
If the size of this matrix does not match, it will be resized (ref()'ed matrices will no longer be references!). If the size matches, nothing is changed and data are just copied.
other | Other matrix. |
|
inline |
Make a reference to sub-matrix.
refMatrix will be initialised to refer to the sub-matrix starting at (startRow,startColumn) and extending for num_rows and num_cols rows and columns, respectively.
startRow | Start element. |
startColumn | Start element. |
num_rows | Number of rows. |
num_cols | Number of columns. |
refMatrix | Refers to the defined sub-matrix on return. |
|
inline |
Make a reference to sub-matrix.
refMatrix will be initialised to refer to the sub-matrix starting at (startRow,startColumn) and extending for num_rows and num_cols rows and columns, respectively.
startRow | Start element. |
startColumn | Start element. |
num_rows | Number of rows. |
num_cols | Number of columns. |
refMatrix | Refers to the defined sub-matrix on return. |
|
inline |
Const reference to column.
column | Column index. |
v | WILL BE CHANGED and contains the reference. |
|
inline |
Makes a vector reference a column from this matrix.
column | Column to reference |
v | Vector that holds the reference after the method returns. |
|
inline |
Reference to a sub column.
row | Start element row |
column | Start element column |
length | length of vector |
v | Vector that contains the reference after the method call. |
|
inline |
Reference to a sub column.
row | Start element row |
column | Start element column |
length | length of vector |
v | Vector that contains the reference after the method call. |
|
inline |
Const reference to row.
row | Row index. |
v | WILL BE CHANGED and contains the reference. |
|
inline |
Makes a vector reference a row from this matrix.
row | Row to reference |
v | Vector that holds the reference after the method returns. |
|
inline |
Reference to a sub row.
row | Start element row |
column | Start element column |
length | length of vector |
v | Vector that contains the reference after the method call. |
|
inline |
Reference to a sub row.
row | Start element row |
column | Start element column |
length | length of vector |
v | Vector that contains the reference after the method call. |
bool goMath::Matrix< T >::reshape | ( | goSize_t | rows, |
goSize_t | cols | ||
) |
Reshape this matrix as long as the new shape results in the same total number of elements.
If the number rows*cols is the same as this->getRows()*this->getColumns(), the matrix is reshaped; the data are not touched in any way. Nothing is reallocated.
rows | Number of rows of the new shape |
cols | Number of columns of the new shape |
bool goMath::Matrix< T >::resize | ( | goSize_t | rows, |
goSize_t | cols | ||
) |
Resize (re-allocate) the matrix.
Memory is newly allocated, so all former data is lost.
rows | Rows. |
cols | Columns. |
goMath::Matrix< T >::vector_iterator goMath::Matrix< T >::rowBegin | ( | ) |
Get an iterator to the first row.
goMath::Matrix< T >::const_vector_iterator goMath::Matrix< T >::rowBegin | ( | ) | const |
Get an iterator to the first row.
goMath::Matrix< T >::vector_iterator goMath::Matrix< T >::rowEnd | ( | ) |
Get an iterator pointing after the last row.
goMath::Matrix< T >::const_vector_iterator goMath::Matrix< T >::rowEnd | ( | ) | const |
Get an iterator pointing after the last row.
bool goMath::Matrix< T >::setData | ( | T * | data, |
goSize_t | r, | ||
goSize_t | c, | ||
goSize_t | leadingDim = 0 |
||
) |
Set external data.
data | Pointer to the external data. |
r | Rows |
c | Columns |
void goMath::Matrix< T >::shiftColumns | ( | goIndex_t | offset, |
goMath::Matrix< T > & | ret | ||
) | const |
Shift (cyclically permute) columns.
Shifts all columns "down" a given offset. The offset can be positive or negative
offset | Offset |
ret | On return, contains the shifted matrix. |
void goMath::Matrix< T >::shiftRows | ( | goIndex_t | offset, |
goMath::Matrix< T > & | ret | ||
) | const |
Shift (cyclically permute) rows.
Shifts all rows "down" a given offset. The offset can be positive or negative
offset | Offset |
ret | On return, contains the shifted matrix. |
T goMath::Matrix< T >::sum | ( | ) | const |
Sum over all elements.
void goMath::Matrix< T >::sum | ( | int | dimension, |
goMath::Matrix< T > & | ret | ||
) | const |
Sum over all columns / all rows.
dimension | If 0, sums over all columns and the result is a row vector. Else, sums over all rows and the result is a column vector. |
ret | Result. |
T goMath::Matrix< T >::trace | ( | ) | const |
Calculates the trace .
Works also for non-square matrices; in that case, the loop runs to the smaller of the two dimensions.