Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

SPUC::Mat< T > Class Template Reference

Templated Matrix Class. More...

#include <matrix.h>

Collaboration diagram for SPUC::Mat< T >:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 Mat ()
 Class constructor.
 Mat (int inrow, int incol)
 Create a matrix of size (inrow, incol).
 Mat (const Mat< T > &m)
 Create a copy of the matrix m.
 Mat (const Vec< T > &invector)
 Create a copy of the vector invector treated as a column vector.
 Mat (const char *str)
 Set matrix equal to values in string.
 Mat (T *c_array, int rows, int cols, bool RowMajor=true)
 Constructor taking a C-array as input. Copies all data.
 ~Mat ()
 Class destructor.
int cols () const
 The number of columns.
int rows () const
 The number of rows.
void set_size (int inrow, int incol, bool copy=false)
 Set size of matrix. If copy = true then keep the data before resizing.
void zeros ()
 Set matrix equal to the all zero matrix.
void clear ()
 Set matrix equal to the all zero matrix.
void ones ()
 Set matrix equal to the all one matrix.
bool set (const char *str)
 Set matrix equal to values in the string.
const T & operator() (int R, int C) const
 Get element (R,C) from matrix.
T & operator() (int R, int C)
 Get element (R,C) from matrix.
T & operator() (int index)
 Get element index using linear addressing (by rows).
const T & operator() (int index) const
 Get element index using linear addressing (by rows).
const Mat< T > operator() (int r1, int r2, int c1, int c2) const
 Sub-matrix from row r1 to row r2 and columns c1 to c2.
Vec< T > get_row (int Index) const
 Get row Index.
Mat< T > get_rows (int r1, int r2) const
 Get rows r1 through r2.
Mat< T > get_rows (const Vec< int > &indexlist) const
 Get the rows specified by indexlist.
Vec< T > get_col (int Index) const
 Get column Index.
Mat< T > get_cols (int c1, int c2) const
 Get columns c1 through c2.
Mat< T > get_cols (const Vec< int > &indexlist) const
 Get the columns specified by indexlist.
void set_row (int Index, const Vec< T > &invector)
 Set row Index to invector.
void set_col (int Index, const Vec< T > &invector)
 Set column Index to invector.
void copy_row (int to, int from)
 Copy row from onto row to.
void copy_col (int to, int from)
 Copy column from onto column to.
void swap_rows (int r1, int r2)
 Swap the rows r1 and r2.
void swap_cols (int c1, int c2)
 Swap the columns c1 and c2.
void set_submatrix (int r1, int r2, int c1, int c2, const Mat< T > &m)
 Set submatrix defined by rows r1,r2 and columns c1,c2 to matrix m.
void set_submatrix (int r1, int r2, int c1, int c2, const T t)
 Set all elements of submatrix defined by rows r1,r2 and columns c1,c2 to value t.
void operator= (T t)
 Set all elements of the matrix equal to t.
void operator= (const Mat< T > &m)
 Set matrix equal to m.
void operator= (const Vec< T > &v)
 Set matrix equal to the vector v, assuming column vector.
void operator= (const char *values)
 Set matrix equal to values in the string.
void operator+= (const Mat< T > &m)
 Addition of matrices.
void operator+= (T t)
 Addition of scalar to matrix.
void operator-= (const Mat< T > &m)
 Subtraction of matrix.
void operator-= (T t)
 Subtraction of scalar from matrix.
void operator *= (const Mat< T > &m)
 Matrix multiplication.
void operator *= (T t)
 Multiplication by a scalar.
void operator/= (T t)
 Division by a scalar.
void operator/= (const Mat< T > &m)
 Elementwise division with the current matrix.
bool operator== (const Mat< T > &m) const
 Compare two matrices. False if wrong sizes or different values.
bool operator!= (const Mat< T > &m) const
 Compare two matrices. True if different.
T & _elem (int R, int C)
 Get element (R,C) from matrix without boundary check (Not recommended to use).
const T & _elem (int R, int C) const
 Get element (R,C) from matrix without boundary check (Not recommended to use).
T & _elem (int index)
 Get element index using linear addressing (by rows) without boundary check (Not recommended to use).
const T & _elem (int index) const
 Get element index using linear addressing (by rows) without boundary check (Not recommended to use).
T * _data ()
 Access of the internal data structure. Don't use. May be changed!
const T * _data () const
 Access to the internal data structure. Don't use. May be changed!
int _datasize () const
 Access to the internal data structure. Don't use. May be changed!

Protected Member Functions

void alloc (int rows, int cols)
 Allocate memory for the matrix.
void free ()
 Free the memory space of the matrix.

Protected Attributes

int datasize
 Protected integer variables.
int no_rows
int no_cols
T * data
 Protected data pointer.

Friends

Mat< T > concat_horizontal TEMPLATE_FUN (const Mat< T > &m1, const Mat< T > &m2)
 Concatenate the matrices m1 and m2 horizontally.
Mat< T > concat_vertical TEMPLATE_FUN (const Mat< T > &m1, const Mat< T > &m2)
 Concatenate the matrices m1 and m2 vertically.
Mat< T > operator+TEMPLATE_FUN (const Mat< T > &m1, const Mat< T > &m2)
 Addition of two matrices.
Mat< T > operator+TEMPLATE_FUN (const Mat< T > &m, T t)
 Addition of matrix and scalar.
Mat< T > operator+TEMPLATE_FUN (T t, const Mat< T > &m)
 Addition of scalar and matrix.
Mat< T > operator-TEMPLATE_FUN (const Mat< T > &m1, const Mat< T > &m2)
 Subtraction of m2 from m1.
Mat< T > operator-TEMPLATE_FUN (const Mat< T > &m, T t)
 Subraction of scalar from matrix.
Mat< T > operator-TEMPLATE_FUN (T t, const Mat< T > &m)
 Subtract matrix from scalar.
Mat< T > operator-TEMPLATE_FUN (const Mat< T > &m)
 Subraction of matrix.
Mat< T > operator *TEMPLATE_FUN (const Mat< T > &m1, const Mat< T > &m2)
 Multiplication of two matrices.
Vec< T > operator *TEMPLATE_FUN (const Mat< T > &m, const Vec< T > &v)
 Multiplication of matrix m and vector v (column vector).
Vec< T > operator *TEMPLATE_FUN (const Vec< T > &v, const Mat< T > &m)
 Multiplication of transposed vector v and matrix m.
Mat< T > operator *TEMPLATE_FUN (const Mat< T > &m, T t)
 Multiplication of matrix and scalar.
Mat< T > operator *TEMPLATE_FUN (T t, const Mat< T > &m)
 Multiplication of scalar and matrix.
Mat< T > elem_mult TEMPLATE_FUN (const Mat< T > &m1, const Mat< T > &m2)
 Elementwise multiplication of two matrices.
Mat< T > operator/TEMPLATE_FUN (const Mat< T > &m, T t)
 Division of matrix with scalar.
Mat< T > elem_div TEMPLATE_FUN (const Mat< T > &m1, const Mat< T > &m2)
 Elementwise division of matrix m1 with matrix m2.

Related Functions

(Note that these are not member functions.)

typedef Mat< double > mat
 Output stream for matrices Default Matrix Type.
typedef Mat< complex< double > > cmat
 Default Complex Matrix Type.
typedef Mat< int > imat
 Integer matrix.
typedef Mat< long_long > llmat
 long_long matrix
typedef Mat< short int > smat
 short int matrix
typedef Mat< binbmat
 bin matrix
void sub_v_vT_m (mat &m, const vec &v)
 Calculates m=m-v*v'*m.
void sub_m_v_vT (mat &m, const vec &v)
 Calculates m=m-m*v*v'.
sum (const Mat< T > &m)
 Sum of all elements in the matrix.
sum_sqr (const Mat< T > &m)
 Sum of square of the elements in the matrix.
max (const Mat< T > &m)
 Maximum value of matrix.
min (const Mat< T > &m)
 Minimum value of matrix.
void max_index (const Mat< T > &in, int &row, int &col)
 The index of the element of maximum value.
void min_index (const Mat< T > &in, int &row, int &col)
 The index of the element of minimum value.
product (const Mat< T > &m)
 The product of all elements in the vector.
double mean (const mat &m)
 The mean value.
complex< double > mean (const cmat &m)
 The mean value.
double mean (const smat &m)
 The mean value.
double mean (const imat &m)
 The mean value.
Mat< T > diag (const Vec< T > &in)
 Returns a diagonal matrix whith the elements of the vector in on the diagonal and zeros elsewhere.
void diag (const Vec< T > &in, Mat< T > &m)
 Returns in the output wariable m a diagonal matrix whith the elements of the vector in on the diagonal and zeros elsewhere.
Vec< T > diag (const Mat< T > &in)
 Returns the diagonal elements of the input matrix in.
Mat< T > bidiag (const Vec< T > &main, const Vec< T > &sup)
 Returns a matrix with the elements of the input vector main on the diagonal and the elements of the input vector sup on the diagonal row above.
void bidiag (const Vec< T > &main, const Vec< T > &sup, Mat< T > &m)
 Returns in the output variable m a matrix with the elements of the input vector main on the diagonal and the elements of the input vector sup on the diagonal row above.
void bidiag (const Mat< T > &in, Vec< T > &main, Vec< T > &sup)
 Returns the main diagonal and the diagonal row above in the two output vectors main and sup.
Mat< T > tridiag (const Vec< T > &main, const Vec< T > &sup, const Vec< T > &sub)
 Returns a matrix with the elements of main on the diagonal, the elements of sup on the diagonal row above, and the elements of sub on the diagonal row below.
void tridiag (const Vec< T > &main, const Vec< T > &sup, const Vec< T > &sub, Mat< T > &m)
 Returns in the output matrix m a matrix with the elements of main on the diagonal, the elements of sup on the diagonal row above, and the elements of sub on the diagonal row below.
void tridiag (const Mat< T > &in, Vec< T > &main, Vec< T > &sup, Vec< T > &sub)
 Returns the main diagonal, the diagonal row above, and the diagonal row below int the output vectors main, sup, and sub.
trace (const Mat< T > &in)
 The trace of the matrix, i.e. the sum of the diagonal elements.
void transpose (const Mat< T > &m, Mat< T > &out)
 Transposition of the matrix returning the transposed matrix in out.
Mat< T > transpose (const Mat< T > &m)
 Transposition of the matrix.
Mat< T > repeat (const Mat< T > &m, int norepeats)
 Repeats each column norepeats times in sequence.
Mat< T > apply_function (fT(*f)(fT), const Mat< T > &data)
 Apply arbitrary functions to a matrix.
Vec< T > rvectorize (const Mat< T > &m)
 Row vectorize the matrix [(0,0) (0,1) ... (N-1,N-2) (N-1,N-1)].
Vec< T > cvectorize (const Mat< T > &m)
 Column vectorize the matrix [(0,0) (1,0) ... (N-2,N-1) (N-1,N-1)].
Mat< T > reshape (const Mat< T > &m, int rows, int cols)
 Reshape the matrix into an rows*cols matrix.
Mat< T > reshape (const Vec< T > &m, int rows, int cols)
 Reshape the vector into an rows*cols matrix.
void upsample (const Mat< T > &v, int usf, Mat< T > &u)
 Upsample each column by incerting (usf-1) zeros after each column.
Mat< T > upsample (const Mat< T > &v, int upsamplefactor)
 Upsample each column by incerting (usf-1) zeros after each column.
void lininterp (const Mat< T > &v, int usf, Mat< T > &u)
 Upsample each column by a factor of (usf-1) by linear interpolation.
Mat< T > lininterp (const Mat< T > &v, int usf)
 Upsample each column by a factor of (usf-1) by linear interpolation.
void lininterp (const Vec< T > &v, int usf, Vec< T > &u)
 Upsample by a factor of (usf-1) by linear interpolation.
Vec< T > lininterp (const Vec< T > &v, int usf)
 Upsample by a factor of (usf-1) by linear interpolation.
Mat< T > mat_1x1 (T m00)
 Matrix of size 1 by 1.
Mat< T > mat_1x2 (T m00, T m01)
 Matrix of size 1 by 2.
Mat< T > mat_2x1 (T m00, T m10)
 Matrix of size 2 by 1.
Mat< T > mat_2x2 (T m00, T m01, T m10, T m11)
 Matrix of size 2 by 2.
Mat< T > mat_1x3 (T m00, T m01, T m02)
 Matrix of size 1 by 3.
Mat< T > mat_3x1 (T m00, T m10, T m20)
 Matrix of size 3 by 1.
Mat< T > mat_2x3 (T m00, T m01, T m02, T m10, T m11, T m12)
 Matrix of size 2 by 3.
Mat< T > mat_3x2 (T m00, T m01, T m10, T m11, T m20, T m21)
 Matrix of size 3 by 2.
Mat< T > mat_3x3 (T m00, T m01, T m02, T m10, T m11, T m12, T m20, T m21, T m22)
 Matrix of size 3 by 3.

Detailed Description

template<class T>
class SPUC::Mat< T >

Templated Matrix Class.

Author:
Tony Ottosson and Tobias Ringstrom
Matrices can be of arbitrarily types, but conversions and functions are prepared for bin, short, int, double, and double_complex vectors and these are predefined as: bmat, smat, imat, mat, and cmat. double and double_complex are usually double and complex<double> respectively. However, this can be changed when \ compiling the it++ (see installation notes for more details).

Examples:

Matrix Constructors: When constructing a matrix without a dimensions (memory) use

 mat temp; 
For construction of a matrix of a given size use
 mat temp(rows, cols); 
It is also possible to assign the constructed matrix the value and dimension of another matrix by
 vec temp(inmatrix); 
If you have explicit values you would like to assign to the matrix it is possible to do this using strings as:
  mat a("0 0.7;5 9.3"); // that is a = [0, 0.7; 5, 9.3]
  mat a="0 0.7;5 9.3";  // the constructor are called implicitly
It is also possible to change dimension by
 temp.set_size(new_rows, new_cols, false); 
where false is used to indicate that the old values in temp is not copied. If you like to preserve the values use true.

There are a number of methods to access parts of a matrix. Examples are

  a(5,3);     // Element number (5,3)
  a(5,9,3,5);  // Sub-matrix from rows 5, 6, 7, 8, 9 the columns 3, 4, and 5  
  a.get_row(10);  // Row 10
  a.get_col(10); // Column 10

It is also possible to modify parts of a vector as e.g. in

  a.set_row(5, invector);    // Set row 5 to \c invector
  a.set_col(3, invector); // Set column 3 to \c invector
  a.copy_col(1, 5); // Copy column 5 to column 1
  a.swap_cols(1, 5); // Swap the contents of columns 1 and 5

It is of course also possible to perform the common linear algebra methods such as addition, subtraction, and matrix multiplication. Observe though, that vectors are assumed to be column-vectors in operations with matrices.

Most elementary functions such as sin(), cosh(), log(), abs(), ..., are available as operations on the individual elements of the matrices. Please see the individual functions for more details.


Constructor & Destructor Documentation

template<class T>
SPUC::Mat< T >::Mat  )  [inline]
 

Class constructor.

template<class T>
SPUC::Mat< T >::Mat int  inrow,
int  incol
[inline]
 

Create a matrix of size (inrow, incol).

Here is the call graph for this function:

template<class T>
SPUC::Mat< T >::Mat const Mat< T > &  m  )  [inline]
 

Create a copy of the matrix m.

Here is the call graph for this function:

template<class T>
SPUC::Mat< T >::Mat const Vec< T > &  invector  )  [inline]
 

Create a copy of the vector invector treated as a column vector.

Here is the call graph for this function:

template<class T>
SPUC::Mat< T >::Mat const char *  str  )  [inline]
 

Set matrix equal to values in string.

Mat(const std::string &str) { init(); set(str); }

Here is the call graph for this function:

template<class T>
SPUC::Mat< T >::Mat T *  c_array,
int  rows,
int  cols,
bool  RowMajor = true
[inline]
 

Constructor taking a C-array as input. Copies all data.

By default the matrix is stored as a RowMajor matrix (i.e. listing elements in sequence beginning with the first column).

Here is the call graph for this function:

template<class T>
SPUC::Mat< T >::~Mat  )  [inline]
 

Class destructor.

Here is the call graph for this function:


Member Function Documentation

template<class T>
const T* SPUC::Mat< T >::_data  )  const [inline]
 

Access to the internal data structure. Don't use. May be changed!

template<class T>
T* SPUC::Mat< T >::_data  )  [inline]
 

Access of the internal data structure. Don't use. May be changed!

template<class T>
int SPUC::Mat< T >::_datasize  )  const [inline]
 

Access to the internal data structure. Don't use. May be changed!

template<class T>
const T& SPUC::Mat< T >::_elem int  index  )  const [inline]
 

Get element index using linear addressing (by rows) without boundary check (Not recommended to use).

template<class T>
T& SPUC::Mat< T >::_elem int  index  )  [inline]
 

Get element index using linear addressing (by rows) without boundary check (Not recommended to use).

template<class T>
const T& SPUC::Mat< T >::_elem int  R,
int  C
const [inline]
 

Get element (R,C) from matrix without boundary check (Not recommended to use).

template<class T>
T& SPUC::Mat< T >::_elem int  R,
int  C
[inline]
 

Get element (R,C) from matrix without boundary check (Not recommended to use).

template<class T>
void SPUC::Mat< T >::alloc int  rows,
int  cols
[inline, protected]
 

Allocate memory for the matrix.

Here is the call graph for this function:

template<class T>
void SPUC::Mat< T >::clear  )  [inline]
 

Set matrix equal to the all zero matrix.

Here is the call graph for this function:

template<class T>
int SPUC::Mat< T >::cols  )  const [inline]
 

The number of columns.

template<class T>
void SPUC::Mat< T >::copy_col int  to,
int  from
[inline]
 

Copy column from onto column to.

template<class T>
void SPUC::Mat< T >::copy_row int  to,
int  from
[inline]
 

Copy row from onto row to.

template<class T>
void SPUC::Mat< T >::free  )  [inline, protected]
 

Free the memory space of the matrix.

template<class T>
Vec< T > SPUC::Mat< T >::get_col int  Index  )  const [inline]
 

Get column Index.

Here is the call graph for this function:

template<class T>
Mat< T > SPUC::Mat< T >::get_cols const Vec< int > &  indexlist  )  const [inline]
 

Get the columns specified by indexlist.

Here is the call graph for this function:

template<class T>
Mat< T > SPUC::Mat< T >::get_cols int  c1,
int  c2
const [inline]
 

Get columns c1 through c2.

Here is the call graph for this function:

template<class T>
Vec< T > SPUC::Mat< T >::get_row int  Index  )  const [inline]
 

Get row Index.

Here is the call graph for this function:

template<class T>
Mat< T > SPUC::Mat< T >::get_rows const Vec< int > &  indexlist  )  const
 

Get the rows specified by indexlist.

Here is the call graph for this function:

template<class T>
Mat< T > SPUC::Mat< T >::get_rows int  r1,
int  r2
const
 

Get rows r1 through r2.

Here is the call graph for this function:

template<class T>
void SPUC::Mat< T >::ones  )  [inline]
 

Set matrix equal to the all one matrix.

template<class T>
void SPUC::Mat< T >::operator *= t  )  [inline]
 

Multiplication by a scalar.

template<class T>
void SPUC::Mat< T >::operator *= const Mat< T > &  m  )  [inline]
 

Matrix multiplication.

Here is the call graph for this function:

template<class T>
bool SPUC::Mat< T >::operator!= const Mat< T > &  m  )  const
 

Compare two matrices. True if different.

template<class T>
const Mat< T > SPUC::Mat< T >::operator() int  r1,
int  r2,
int  c1,
int  c2
const [inline]
 

Sub-matrix from row r1 to row r2 and columns c1 to c2.

Value -1 indicates the last row and column, respectively.

template<class T>
const T& SPUC::Mat< T >::operator() int  index  )  const [inline]
 

Get element index using linear addressing (by rows).

template<class T>
T& SPUC::Mat< T >::operator() int  index  )  [inline]
 

Get element index using linear addressing (by rows).

template<class T>
T& SPUC::Mat< T >::operator() int  R,
int  C
[inline]
 

Get element (R,C) from matrix.

template<class T>
const T& SPUC::Mat< T >::operator() int  R,
int  C
const [inline]
 

Get element (R,C) from matrix.

template<class T>
void SPUC::Mat< T >::operator+= t  )  [inline]
 

Addition of scalar to matrix.

template<class T>
void SPUC::Mat< T >::operator+= const Mat< T > &  m  )  [inline]
 

Addition of matrices.

Here is the call graph for this function:

template<class T>
void SPUC::Mat< T >::operator-= t  )  [inline]
 

Subtraction of scalar from matrix.

template<class T>
void SPUC::Mat< T >::operator-= const Mat< T > &  m  )  [inline]
 

Subtraction of matrix.

Here is the call graph for this function:

template<class T>
void SPUC::Mat< T >::operator/= const Mat< T > &  m  )  [inline]
 

Elementwise division with the current matrix.

template<class T>
void SPUC::Mat< T >::operator/= t  )  [inline]
 

Division by a scalar.

template<class T>
void SPUC::Mat< T >::operator= const char *  values  )  [inline]
 

Set matrix equal to values in the string.

Here is the call graph for this function:

template<class T>
void SPUC::Mat< T >::operator= const Vec< T > &  v  )  [inline]
 

Set matrix equal to the vector v, assuming column vector.

Here is the call graph for this function:

template<class T>
void SPUC::Mat< T >::operator= const Mat< T > &  m  )  [inline]
 

Set matrix equal to m.

Here is the call graph for this function:

template<class T>
void SPUC::Mat< T >::operator= t  )  [inline]
 

Set all elements of the matrix equal to t.

template<class T>
bool SPUC::Mat< T >::operator== const Mat< T > &  m  )  const
 

Compare two matrices. False if wrong sizes or different values.

template<class T>
int SPUC::Mat< T >::rows  )  const [inline]
 

The number of rows.

template<class T>
bool SPUC::Mat< T >::set const char *  str  ) 
 

Set matrix equal to values in the string.

Here is the call graph for this function:

template<class T>
void SPUC::Mat< T >::set_col int  Index,
const Vec< T > &  invector
[inline]
 

Set column Index to invector.

Here is the call graph for this function:

template<class T>
void SPUC::Mat< T >::set_row int  Index,
const Vec< T > &  invector
[inline]
 

Set row Index to invector.

Here is the call graph for this function:

template<class T>
void SPUC::Mat< T >::set_size int  inrow,
int  incol,
bool  copy = false