Matrix Class Reference

A class for storing and performing operations on general 2-dimensional matrices. More...

#include <matrix.h>

Inheritance diagram for Matrix:
Inheritance graph
[legend]

List of all members.

Public Types

enum  Type { DENSE, SPARSE }

Public Member Functions

double absMax () const
 The largest absolute value of any element in this matrix.
int cols () const
 The number of columns of this matrix.
void copyMatrix (const Matrix &m)
 Copies the entire matrix m to this matrix; sizes must be identical.
virtual void copySubBlock (int startRow, int startCol, int rows, int cols, const Matrix &m, int startMRow, int startMCol)
 Copies a block of the matrix m into a block of this matrix.
void copySubMatrix (int startRow, int startCol, const Matrix &m)
 Copies the entire matrix m into a sub-block of this matrix.
virtual const double & elem (int m, int n) const
 Not inlined as it is overloaded in SparseMatrix.
virtual double & elem (int m, int n)
 Not inlined as it is overloaded in SparseMatrix.
double elementSum () const
 The sum of all the elements in the matrix.
void eye ()
 Sets this matrix to identity. Not really used, use the static EYE instead.
double fnorm () const
 Computes the Frobenius norm of the matrix: sqrt(sum_of_all_squared_elems).
Matrix getColumn (int c) const
virtual void getData (std::vector< double > *data) const
 Returns a copy of the data as a column major vector of doubles.
virtual std::auto_ptr< double > getDataCopy () const
 Returns an auto_ptr to a copy of the data.
virtual double * getDataPointer ()
 Returns the actual data pointer for this matrix.
virtual double getDefault () const
 Returns the default value for the elements of this matrix.
Matrix getRow (int r) const
Matrix getSubMatrix (int startRow, int startCol, int rows, int cols) const
virtual Type getType () const
 Matrix (const double *M, int m, int n, bool colMajor)
 A matrix of the given size, with contents initialized from an array in memory.
 Matrix (const Matrix &M)
 Copy constructor.
 Matrix (int m, int n)
 A matrix of the given size with unspecified contents.
void multiply (double s)
 Multiples the matrix by the scalar s.
virtual bool nextSequentialElement (int &i, int &j, double &val) const
 Returns the next non-zero element, in sequential access.
virtual int numElements () const
 Returns the number of elements set.
void print (FILE *fp=stderr) const
int rank () const
 Computes the rank of the matrix using SVD.
virtual void resize (int m, int n)
 Resizes the matrix. All current data is lost.
int rows () const
 The number of rows of this matrix.
virtual void sequentialReset () const
 Resets the sequential access to the matrix.
virtual void setAllElements (double val)
void swapCols (int c1, int c2)
void swapRows (int r1, int r2)
virtual void transpose ()
 Transposes this matrix in place.
Matrix transposed () const
 Returns the transpose of this matrix; the original is unaffected.
virtual ~Matrix ()

Static Public Member Functions

template<class MatrixType >
static MatrixType BLOCKCOLUMN (std::list< Matrix * > *blocks)
 Builds a block column matrix from the column matrices in the list.
template<class MatrixType >
static MatrixType BLOCKCOLUMN (const Matrix &M1, const Matrix &M2)
 Builds a block column matrix from the two given matrices.
template<class MatrixType >
static MatrixType BLOCKDIAG (std::list< Matrix * > *blocks)
 Builds a block diagonal matrix from the matrices in the list.
template<class MatrixType >
static MatrixType BLOCKDIAG (const Matrix &M1, const Matrix &M2)
 Builds a block diagonal matrix from the two given matrices.
template<class MatrixType >
static MatrixType BLOCKROW (std::list< Matrix * > *blocks)
 Builds a block row matrix from the row matrices in the list.
template<class MatrixType >
static MatrixType BLOCKROW (const Matrix &M1, const Matrix &M2)
 Builds a block row matrix from the two given matrices.
static Matrix EYE (int m, int n)
 An identity matrix of the given size.
static Matrix MAX_VECTOR (int rows)
 Returns a vector filled with the max value that can be expressed as a double.
static Matrix MIN_VECTOR (int rows)
 Returns a vector filled with the min value that can be expressed as a double.
static Matrix NEGEYE (int m, int n)
 A negated identity matrix of the given size.
static Matrix PERMUTATION (int n, int *jpvt)
 A permutation matrix initialized from a permutation vector in memory.
static Matrix ROTATION (const mat3 &rot)
 A 3x3 rotation matrix that can be used to left-multiply a 3x1 vector.
static Matrix ROTATION2D (double theta)
 A 2D 2x2 rotation matrix that can be used to left-multiply a 2x1 vector.
static Matrix TRANSFORM (const transf &t)
 A 4x4 transform matrix that can be used to left-multiply a 4x1 homogeneous vector.
template<class MatrixType >
static MatrixType ZEROES (int m, int n)
 A mtrix of the given size filled with zeroes.

Static Public Attributes

static const double EPS = 1.0e-7
 Used for zero comparisons in all computations.

Protected Attributes

int mCols
int mRows
 The size of this matrix, set at construction time.

Private Member Functions

void initialize (int m, int n)
 All the work that is common between all constructors.
void setFromColMajor (const double *M)
 Copies the actual data from a column-major array in memory.
void setFromRowMajor (const double *M)
 Copies the actual data from a row-major array in memory.

Private Attributes

double * mData
 The actual data, in column-major format.
int mSequentialI
 Used to keep track of sequential access.
int mSequentialJ
 Used to keep track of sequential access.

Friends

std::ostream & operator<< (std::ostream &os, const Matrix &m)

Detailed Description

A class for storing and performing operations on general 2-dimensional matrices.

The Matrix is a general 2-dimensional matrix, along with a (growing) number of operations that can be performed on it. The data itself is stored as a column-major array of doubles. The important design decisions are:

A Matrix only holds doubles for now. At some point I hope to template the class to hold anything you might want.

Many of the computations are done using LAPACK. This should be really fast, but also has problems. One of them is that it prevents using const correctly for all the operations and arguments that should be const.

A Matrix's size is determined when it is constructed, and can never be changed afterwards. This is somewhat inflexible, but allows us to catch size mismatches at run time. In debug mode, all operations first assert(...) the correct matrix sizes.

All Matrix copies are deep copies. This avoids lots of problem with consistency, but can also eat up memory and slow down operations. Be particularly careful when using the copy constructor, or any functions that return instances of the Matrix class: all are done using deep copies.

No operators have been implemented. Most operations that involve two or more matrices are external to this class, and rather than returning the result, they take in the result as an argument. This makes it somewhat easier to catch errors, but also makes chaining of operations annoying.

Also provides what I call "sequential access" so that a sparse matrix (which inherits from this class) can give you its non-zero elements in linear time in the number of elements. This is somewhat hackish, for now. The way sequential access works, for both dense and sparse matrix, is using two functions: sequentialReset() and nextSequentialElement(...). See documentation of those functions for details.

Definition at line 74 of file matrix.h.


Member Enumeration Documentation

Enumerator:
DENSE 
SPARSE 

Definition at line 93 of file matrix.h.


Constructor & Destructor Documentation

Matrix::Matrix ( int  m,
int  n 
)

A matrix of the given size with unspecified contents.

Definition at line 75 of file matrix.cpp.

Matrix::Matrix ( const Matrix M  ) 

Copy constructor.

Definition at line 79 of file matrix.cpp.

Matrix::Matrix ( const double *  M,
int  m,
int  n,
bool  colMajor 
)

A matrix of the given size, with contents initialized from an array in memory.

Definition at line 87 of file matrix.cpp.

Matrix::~Matrix (  )  [virtual]

Definition at line 97 of file matrix.cpp.


Member Function Documentation

double Matrix::absMax (  )  const

The largest absolute value of any element in this matrix.

Definition at line 379 of file matrix.cpp.

template<class MatrixType >
MatrixType Matrix::BLOCKCOLUMN ( std::list< Matrix * > *  blocks  )  [inline, static]

Builds a block column matrix from the column matrices in the list.

Definition at line 424 of file matrix.h.

template<class MatrixType >
MatrixType Matrix::BLOCKCOLUMN ( const Matrix M1,
const Matrix M2 
) [inline, static]

Builds a block column matrix from the two given matrices.

Definition at line 451 of file matrix.h.

template<class MatrixType >
MatrixType Matrix::BLOCKDIAG ( std::list< Matrix * > *  blocks  )  [inline, static]

Builds a block diagonal matrix from the matrices in the list.

Definition at line 402 of file matrix.h.

template<class MatrixType >
MatrixType Matrix::BLOCKDIAG ( const Matrix M1,
const Matrix M2 
) [inline, static]

Builds a block diagonal matrix from the two given matrices.

Definition at line 473 of file matrix.h.

template<class MatrixType >
MatrixType Matrix::BLOCKROW ( std::list< Matrix * > *  blocks  )  [inline, static]

Builds a block row matrix from the row matrices in the list.

Definition at line 374 of file matrix.h.

template<class MatrixType >
MatrixType Matrix::BLOCKROW ( const Matrix M1,
const Matrix M2 
) [inline, static]

Builds a block row matrix from the two given matrices.

Definition at line 463 of file matrix.h.

int Matrix::cols (  )  const [inline]

The number of columns of this matrix.

Definition at line 121 of file matrix.h.

void Matrix::copyMatrix ( const Matrix m  )  [inline]

Copies the entire matrix m to this matrix; sizes must be identical.

Definition at line 134 of file matrix.h.

void Matrix::copySubBlock ( int  startRow,
int  startCol,
int  rows,
int  cols,
const Matrix m,
int  startMRow,
int  startMCol 
) [virtual]

Copies a block of the matrix m into a block of this matrix.

Reimplemented in SparseMatrix.

Definition at line 495 of file matrix.cpp.

void Matrix::copySubMatrix ( int  startRow,
int  startCol,
const Matrix m 
) [inline]

Copies the entire matrix m into a sub-block of this matrix.

Definition at line 131 of file matrix.h.

const double & Matrix::elem ( int  m,
int  n 
) const [virtual]

Not inlined as it is overloaded in SparseMatrix.

Reimplemented in SparseMatrix.

Definition at line 137 of file matrix.cpp.

double & Matrix::elem ( int  m,
int  n 
) [virtual]

Not inlined as it is overloaded in SparseMatrix.

Reimplemented in SparseMatrix.

Definition at line 129 of file matrix.cpp.

double Matrix::elementSum (  )  const

The sum of all the elements in the matrix.

Definition at line 393 of file matrix.cpp.

Matrix Matrix::EYE ( int  m,
int  n 
) [static]

An identity matrix of the given size.

Reimplemented in SparseMatrix.

Definition at line 203 of file matrix.cpp.

void Matrix::eye (  ) 

Sets this matrix to identity. Not really used, use the static EYE instead.

Definition at line 636 of file matrix.cpp.

double Matrix::fnorm (  )  const

Computes the Frobenius norm of the matrix: sqrt(sum_of_all_squared_elems).

Definition at line 367 of file matrix.cpp.

Matrix Matrix::getColumn ( int  c  )  const

Definition at line 455 of file matrix.cpp.

void Matrix::getData ( std::vector< double > *  data  )  const [virtual]

Returns a copy of the data as a column major vector of doubles.

Reimplemented in SparseMatrix.

Definition at line 331 of file matrix.cpp.

std::auto_ptr< double > Matrix::getDataCopy (  )  const [virtual]

Returns an auto_ptr to a copy of the data.

Reimplemented in SparseMatrix.

Definition at line 323 of file matrix.cpp.

double * Matrix::getDataPointer (  )  [virtual]

Returns the actual data pointer for this matrix.

Reimplemented in SparseMatrix.

Definition at line 337 of file matrix.cpp.

virtual double Matrix::getDefault (  )  const [inline, virtual]

Returns the default value for the elements of this matrix.

Really only makes sense for sparse matrices.

Reimplemented in SparseMatrix.

Definition at line 145 of file matrix.h.

Matrix Matrix::getRow ( int  r  )  const

Definition at line 466 of file matrix.cpp.

Matrix Matrix::getSubMatrix ( int  startRow,
int  startCol,
int  rows,
int  cols 
) const

Definition at line 477 of file matrix.cpp.

virtual Type Matrix::getType (  )  const [inline, virtual]

Reimplemented in SparseMatrix.

Definition at line 106 of file matrix.h.

void Matrix::initialize ( int  m,
int  n 
) [private]

All the work that is common between all constructors.

Definition at line 55 of file matrix.cpp.

Matrix Matrix::MAX_VECTOR ( int  rows  )  [static]

Returns a vector filled with the max value that can be expressed as a double.

Definition at line 276 of file matrix.cpp.

Matrix Matrix::MIN_VECTOR ( int  rows  )  [static]

Returns a vector filled with the min value that can be expressed as a double.

Definition at line 284 of file matrix.cpp.

void Matrix::multiply ( double  s  ) 

Multiples the matrix by the scalar s.

Definition at line 650 of file matrix.cpp.

Matrix Matrix::NEGEYE ( int  m,
int  n 
) [static]

A negated identity matrix of the given size.

Reimplemented in SparseMatrix.

Definition at line 227 of file matrix.cpp.

bool Matrix::nextSequentialElement ( int &  i,
int &  j,
double &  val 
) const [virtual]

Returns the next non-zero element, in sequential access.

Reimplemented in SparseMatrix.

Definition at line 413 of file matrix.cpp.

virtual int Matrix::numElements (  )  const [inline, virtual]

Returns the number of elements set.

Reimplemented in SparseMatrix.

Definition at line 148 of file matrix.h.

Matrix Matrix::PERMUTATION ( int  n,
int *  jpvt 
) [static]

A permutation matrix initialized from a permutation vector in memory.

Definition at line 251 of file matrix.cpp.

void Matrix::print ( FILE *  fp = stderr  )  const

Definition at line 597 of file matrix.cpp.

int Matrix::rank (  )  const

Computes the rank of the matrix using SVD.

Definition at line 343 of file matrix.cpp.

void Matrix::resize ( int  m,
int  n 
) [virtual]

Resizes the matrix. All current data is lost.

Reimplemented in SparseMatrix.

Definition at line 104 of file matrix.cpp.

Matrix Matrix::ROTATION ( const mat3 rot  )  [static]

A 3x3 rotation matrix that can be used to left-multiply a 3x1 vector.

Definition at line 264 of file matrix.cpp.

Matrix Matrix::ROTATION2D ( double  theta  )  [static]

A 2D 2x2 rotation matrix that can be used to left-multiply a 2x1 vector.

| cos(theta) -sin(theta) | | sin(theta) cos(theta) |

Definition at line 296 of file matrix.cpp.

int Matrix::rows (  )  const [inline]

The number of rows of this matrix.

Definition at line 119 of file matrix.h.

void Matrix::sequentialReset (  )  const [virtual]

Resets the sequential access to the matrix.

Reimplemented in SparseMatrix.

Definition at line 406 of file matrix.cpp.

void Matrix::setAllElements ( double  val  )  [virtual]

Reimplemented in SparseMatrix.

Definition at line 486 of file matrix.cpp.

void Matrix::setFromColMajor ( const double *  M  )  [private]

Copies the actual data from a column-major array in memory.

Definition at line 305 of file matrix.cpp.

void Matrix::setFromRowMajor ( const double *  M  )  [private]

Copies the actual data from a row-major array in memory.

Definition at line 312 of file matrix.cpp.

void Matrix::swapCols ( int  c1,
int  c2 
)

Definition at line 604 of file matrix.cpp.

void Matrix::swapRows ( int  r1,
int  r2 
)

Definition at line 612 of file matrix.cpp.

static Matrix Matrix::TRANSFORM ( const transf t  )  [static]

A 4x4 transform matrix that can be used to left-multiply a 4x1 homogeneous vector.

void Matrix::transpose (  )  [virtual]

Transposes this matrix in place.

Reimplemented in SparseMatrix.

Definition at line 620 of file matrix.cpp.

Matrix Matrix::transposed (  )  const

Returns the transpose of this matrix; the original is unaffected.

Definition at line 659 of file matrix.cpp.

template<class MatrixType >
MatrixType Matrix::ZEROES ( int  m,
int  n 
) [inline, static]

A mtrix of the given size filled with zeroes.

Definition at line 365 of file matrix.h.


Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  os,
const Matrix m 
) [friend]

Definition at line 586 of file matrix.cpp.


Member Data Documentation

const double Matrix::EPS = 1.0e-7 [static]

Used for zero comparisons in all computations.

Definition at line 212 of file matrix.h.

int Matrix::mCols [protected]

Definition at line 91 of file matrix.h.

double* Matrix::mData [private]

The actual data, in column-major format.

Definition at line 77 of file matrix.h.

int Matrix::mRows [protected]

The size of this matrix, set at construction time.

Definition at line 91 of file matrix.h.

int Matrix::mSequentialI [mutable, private]

Used to keep track of sequential access.

Definition at line 86 of file matrix.h.

int Matrix::mSequentialJ [mutable, private]

Used to keep track of sequential access.

Definition at line 88 of file matrix.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


graspit
Author(s):
autogenerated on Wed Jan 25 11:00:22 2012