IT++ Logo
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
itpp::Mat< Num_T > Class Template Reference

Matrix Class (Templated) More...

#include <itpp/base/mat.h>

Public Types

typedef Num_T value_type
 The type of the matrix values.
 

Public Member Functions

 Mat (const Factory &f=DEFAULT_FACTORY)
 Default constructor. An element factory f can be specified.
 
 Mat (int rows, int cols, const Factory &f=DEFAULT_FACTORY)
 Create a matrix of size (rows, cols). An element factory f can be specified.
 
 Mat (const Mat< Num_T > &m)
 Copy constructor.
 
 Mat (const Mat< Num_T > &m, const Factory &f)
 Constructor, similar to the copy constructor, but also takes an element factory f as argument.
 
 Mat (const Vec< Num_T > &v, const Factory &f=DEFAULT_FACTORY)
 Construct a matrix from a column vector v. An element factory f can be specified.
 
 Mat (const std::string &str, const Factory &f=DEFAULT_FACTORY)
 Set matrix equal to values in string str. An element factory f can be specified.
 
 Mat (const char *str, const Factory &f=DEFAULT_FACTORY)
 Set matrix equal to values in string str. An element factory f can be specified.
 
 Mat (const Num_T *c_array, int rows, int cols, bool row_major=true, const Factory &f=DEFAULT_FACTORY)
 Constructor taking a C-array as input. An element factory f can be specified.
 
 ~Mat ()
 Destructor.
 
int cols () const
 The number of columns.
 
int rows () const
 The number of rows.
 
int size () const
 The number of elements.
 
void set_size (int rows, int cols, 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.
 
void set (const std::string &str)
 Set matrix equal to values in the string str.
 
void set (const char *str)
 Set matrix equal to values in the string str.
 
const Num_T & operator() (int r, int c) const
 Get element (r,c) from matrix.
 
Num_T & operator() (int r, int c)
 Get element (r,c) from matrix.
 
const Num_T & operator() (int i) const
 Get element i using linear addressing (by rows)
 
Num_T & operator() (int i)
 Get element i using linear addressing (by rows)
 
const Num_T & get (int r, int c) const
 Get element (r,c) from matrix.
 
const Num_T & get (int i) const
 Get element i using linear addressing (by rows)
 
void set (int r, int c, Num_T t)
 Set element (r,c) of matrix.
 
Mat< Num_T > operator() (int r1, int r2, int c1, int c2) const
 Sub-matrix from row r1 to row r2 and columns c1 to c2.
 
Mat< Num_T > get (int r1, int r2, int c1, int c2) const
 Sub-matrix from row r1 to row r2 and columns c1 to c2.
 
Vec< Num_T > get_row (int r) const
 Get row r.
 
Mat< Num_T > get_rows (int r1, int r2) const
 Get rows r1 through r2.
 
Mat< Num_T > get_rows (const Vec< int > &indexlist) const
 Get the rows specified by indexlist.
 
Vec< Num_T > get_col (int c) const
 Get column c.
 
Mat< Num_T > get_cols (int c1, int c2) const
 Get columns c1 through c2.
 
Mat< Num_T > get_cols (const Vec< int > &indexlist) const
 Get the columns specified by indexlist.
 
void set_row (int r, const Vec< Num_T > &v)
 Set row r to vector v.
 
void set_col (int c, const Vec< Num_T > &v)
 Set column c to vector v.
 
void set_rows (int r, const Mat< Num_T > &m)
 Set rows to matrix m, staring from row r.
 
void set_cols (int c, const Mat< Num_T > &m)
 Set columns to matrix m, starting from column c.
 
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< Num_T > &m)
 This function is deprecated. Please use set_submatrix(int r, int c, const Mat<> &m) instead.
 
void set_submatrix (int r, int c, const Mat< Num_T > &m)
 Set submatrix defined by upper-left element (r,c) and the size of matrix m to m.
 
void set_submatrix (int r1, int r2, int c1, int c2, Num_T t)
 Set all elements of submatrix defined by rows r1,r2 and columns c1,c2 to value t.
 
void del_row (int r)
 Delete row number r.
 
void del_rows (int r1, int r2)
 Delete rows from r1 to r2.
 
void del_col (int c)
 Delete column number c.
 
void del_cols (int c1, int c2)
 Delete columns from c1 to c2.
 
void ins_row (int r, const Vec< Num_T > &v)
 Insert vector v at row number r. The matrix can be empty.
 
void ins_col (int c, const Vec< Num_T > &v)
 Insert vector v at column number c. The matrix can be empty.
 
void append_row (const Vec< Num_T > &v)
 Append vector v to the bottom of the matrix. The matrix can be empty.
 
void append_col (const Vec< Num_T > &v)
 Append vector v to the right side of the matrix. The matrix can be empty.
 
Mat< Num_T > transpose () const
 Matrix transpose.
 
Mat< Num_T > T () const
 Matrix transpose.
 
Mat< Num_T > hermitian_transpose () const
 Hermitian matrix transpose (conjugate transpose)
 
Mat< Num_T > H () const
 Hermitian matrix transpose (conjugate transpose)
 
Mat< Num_T > & operator= (Num_T t)
 Set all elements of the matrix equal to t.
 
Mat< Num_T > & operator= (const Mat< Num_T > &m)
 Set matrix equal to m.
 
Mat< Num_T > & operator= (const Vec< Num_T > &v)
 Set matrix equal to the vector v, assuming column vector.
 
Mat< Num_T > & operator= (const std::string &str)
 Set matrix equal to values in the string str.
 
Mat< Num_T > & operator= (const char *str)
 Set matrix equal to values in the string str.
 
Mat< Num_T > & operator+= (const Mat< Num_T > &m)
 Addition of matrices.
 
Mat< Num_T > & operator+= (Num_T t)
 Addition of scalar to matrix.
 
Mat< Num_T > & operator-= (const Mat< Num_T > &m)
 Subtraction of matrix.
 
Mat< Num_T > & operator-= (Num_T t)
 Subtraction of scalar from matrix.
 
Mat< Num_T > & operator*= (const Mat< Num_T > &m)
 Matrix multiplication.
 
Mat< Num_T > & operator*= (Num_T t)
 Multiplication by a scalar.
 
Mat< Num_T > & operator/= (Num_T t)
 Division by a scalar.
 
Mat< Num_T > & operator/= (const Mat< Num_T > &m)
 Element-wise division with the current matrix.
 
bool operator== (const Mat< Num_T > &m) const
 Compare two matrices. False if wrong sizes or different values.
 
bool operator!= (const Mat< Num_T > &m) const
 Compare two matrices. True if different.
 
Num_T & _elem (int r, int c)
 Get element (r,c) from matrix without boundary check (not recommended to use)
 
const Num_T & _elem (int r, int c) const
 Get element (r,c) from matrix without boundary check (not recommended to use)
 
Num_T & _elem (int i)
 Get element i using linear addressing (by rows) without boundary check (not recommended to use)
 
const Num_T & _elem (int i) const
 Get element i using linear addressing (by rows) without boundary check (not recommended to use)
 
Num_T * _data ()
 Access of the internal data structure (not recommended to use)
 
const Num_T * _data () const
 Access to the internal data structure (not recommended to use)
 
int _datasize () const
 Access to the internal data structure (not recommended to use)
 

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

Num_T * data
 Protected data pointer.
 
const Factoryfactory
 Element factory (set to DEFAULT_FACTORY to use Num_T default constructors only)
 
int datasize
 
int no_rows
 
int no_cols
 

Friends

Mat< Num_T > concat_horizontal (const Mat< Num_T > &m1, const Mat< Num_T > &m2)
 Concatenate the matrices m1 and m2 horizontally.
 
Mat< Num_T > concat_vertical (const Mat< Num_T > &m1, const Mat< Num_T > &m2)
 Concatenate the matrices m1 and m2 vertically.
 
Mat< Num_T > operator+ (const Mat< Num_T > &m1, const Mat< Num_T > &m2)
 Addition of two matrices.
 
Mat< Num_T > operator+ (const Mat< Num_T > &m, Num_T t)
 Addition of matrix and scalar.
 
Mat< Num_T > operator+ (Num_T t, const Mat< Num_T > &m)
 Addition of scalar and matrix.
 
Mat< Num_T > operator- (const Mat< Num_T > &m1, const Mat< Num_T > &m2)
 Subtraction of m2 from m1.
 
Mat< Num_T > operator- (const Mat< Num_T > &m, Num_T t)
 Subtraction of scalar from matrix.
 
Mat< Num_T > operator- (Num_T t, const Mat< Num_T > &m)
 Subtract matrix from scalar.
 
Mat< Num_T > operator- (const Mat< Num_T > &m)
 Subtraction of matrix.
 
Mat< Num_T > elem_mult (const Mat< Num_T > &m1, const Mat< Num_T > &m2)
 Element wise multiplication of two matrices.
 
void elem_mult_out (const Mat< Num_T > &m1, const Mat< Num_T > &m2, Mat< Num_T > &out)
 Element wise multiplication of two matrices, storing the result in matrix out.
 
void elem_mult_out (const Mat< Num_T > &m1, const Mat< Num_T > &m2, const Mat< Num_T > &m3, Mat< Num_T > &out)
 Element wise multiplication of three matrices, storing the result in matrix out.
 
void elem_mult_out (const Mat< Num_T > &m1, const Mat< Num_T > &m2, const Mat< Num_T > &m3, const Mat< Num_T > &m4, Mat< Num_T > &out)
 Element wise multiplication of four matrices, storing the result in matrix out.
 
void elem_mult_inplace (const Mat< Num_T > &m1, Mat< Num_T > &m2)
 In-place element wise multiplication of two matrices. Fast version of B = elem_mult(A, B).
 
Num_T elem_mult_sum (const Mat< Num_T > &m1, const Mat< Num_T > &m2)
 Element wise multiplication of two matrices, followed by summation of the resultant elements. Fast version of sumsum(elem_mult(A, B)).
 
Mat< Num_T > operator/ (const Mat< Num_T > &m, Num_T t)
 Element-wise division by a scalar.
 
Mat< Num_T > operator/ (Num_T t, const Mat< Num_T > &m)
 Element-wise division (t is the dividend, elements of m are divisors)
 
Mat< Num_T > elem_div (const Mat< Num_T > &m1, const Mat< Num_T > &m2)
 Element wise division of two matrices.
 
void elem_div_out (const Mat< Num_T > &m1, const Mat< Num_T > &m2, Mat< Num_T > &out)
 Element wise division of two matrices, storing the result in matrix out.
 
Num_T elem_div_sum (const Mat< Num_T > &m1, const Mat< Num_T > &m2)
 Element wise division of two matrices, followed by summation of the resultant elements. Fast version of sumsum(elem_div(A, B)).
 

Detailed Description

template<class Num_T>
class itpp::Mat< Num_T >

Matrix Class (Templated)

Author
Tony Ottosson, Tobias Ringstrom, Adam Piatyszek and Conrad Sanderson

Matrices can be of arbitrarily types, but conversions and functions are prepared for bin, short, int, double, and complex<double> vectors and these are predefined as: bmat, smat, imat, mat, and cmat. double and complex<double> are usually double and complex<double> respectively. However, this can be changed when compiling the it++ (see installation notes for more details). (Note: for binary matrices, an alternative to the bmat class is GF2mat and GF2mat_dense, which offer a more memory efficient representation and additional functions for linear algebra.)

Examples:

Matrix Constructors: When constructing a matrix without 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.

By default, the Mat elements are created using the default constructor for the element type. This can be changed by specifying a suitable Factory in the Mat constructor call; see Detailed Description for Factory.

Definition at line 201 of file mat.h.

Constructor & Destructor Documentation

template<class Num_T>
Mat< Num_T >::Mat ( const Num_T *  c_array,
int  rows,
int  cols,
bool  row_major = true,
const Factory f = DEFAULT_FACTORY 
)

Constructor taking a C-array as input. An element factory f can be specified.

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

Definition at line 626 of file mat.h.

References itpp::Mat< Num_T >::alloc(), itpp::Mat< Num_T >::cols(), itpp::Mat< Num_T >::data, itpp::Mat< Num_T >::datasize, itpp::Mat< Num_T >::no_cols, itpp::Mat< Num_T >::no_rows, and itpp::Mat< Num_T >::rows().

Member Function Documentation

template<class Num_T >
Mat< Num_T > Mat< Num_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.

Definition at line 826 of file mat.h.

References itpp::Mat< Num_T >::data, it_assert_debug, itpp::Mat< Num_T >::no_cols, and itpp::Mat< Num_T >::no_rows.

template<class Num_T >
Mat< Num_T > Mat< Num_T >::get ( 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.

Definition at line 846 of file mat.h.

Member Data Documentation

template<class Num_T>
int itpp::Mat< Num_T >::datasize
protected
template<class Num_T>
int itpp::Mat< Num_T >::no_rows
protected
template<class Num_T>
int itpp::Mat< Num_T >::no_cols
protected

The documentation for this class was generated from the following files:
SourceForge Logo

Generated on Sat Jul 6 2013 10:54:31 for IT++ by Doxygen 1.8.2