IT++ Logo Newcom Logo

itpp::Sparse_Mat< T > Class Template Reference

Templated Sparse Matrix Class. More...

#include <itpp/base/smat.h>

List of all members.

Public Member Functions

 Sparse_Mat ()
 Default constructor.
 Sparse_Mat (int rows, int cols, int row_data_init=200)
 Initiate an empty sparse matrix.
 Sparse_Mat (const Sparse_Mat< T > &m)
 Initiate a new sparse matrix. The elements of m are copied into the new sparse matrix.
 Sparse_Mat (const Mat< T > &m)
 Initiate a new sparse matrix from a dense matrix. The elements of m are copied into the new sparse matrix.
 Sparse_Mat (const Mat< T > &m, T epsilon)
 Initiate a new sparse matrix from a dense matrix. Elements of m larger than epsilon are copied into the new sparse matrix.
 ~Sparse_Mat ()
 Destructor.
void set_size (int rows, int cols, int row_data_init=-1)
 Set the size of the sparse matrix.
int rows () const
 Returns the number of rows of the sparse matrix.
int cols () const
 Returns the number of columns of the sparse matrix.
int nnz ()
 The number of non-zero elements in the sparse matrix.
double density ()
 Returns the density of the sparse matrix: (number of non-zero elements)/(total number of elements).
void compact ()
 Set the maximum number of non-zero elements in each column equal to the actual number of non-zero elements in each column.
void full (Mat< T > &m) const
 Returns a full, dense matrix in m.
Mat< T > full () const
 Returns a full, dense matrix.
operator() (int r, int c) const
 Returns element of row r and column c.
void set (int r, int c, T v)
 Set element (r, c ) equal to v.
void set_new (int r, int c, T v)
 Set a new element with index (r, c ) equal to v.
void add_elem (const int r, const int c, const T v)
 Add the element in row r and column c with v.
void zeros ()
 Set the sparse matrix to the all zero matrix (removes all non-zero elements).
void zero_elem (const int r, const int c)
 Set the element in row r and column c to zero (i.e. clear that element if it contains a non-zero value).
void clear ()
 Clear all non-zero elements of the sparse matrix.
void clear_elem (const int r, const int c)
 Clear the element in row r and column c (if it contains a non-zero value).
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 r, int c, const Mat< T > &m)
 Set submatrix defined by upper-left element (r,c) and the size of matrix m to m.
Sparse_Mat< T > get_submatrix (int r1, int r2, int c1, int c2) const
 Returns the sub-matrix from rows r1 to r2 and columns c1 to c2.
Sparse_Mat< T > get_submatrix_cols (int c1, int c2) const
 Returns the sub-matrix from columns c1 to c2 (all rows).
void get_col (int c, Sparse_Vec< T > &v) const
 Returns column c of the Sparse_Mat in the Sparse_Vec v.
Sparse_Vec< T > get_col (int c) const
 Returns column c of the Sparse_Mat.
void set_col (int c, const Sparse_Vec< T > &v)
 Set column c of the Sparse_Mat.
void transpose (Sparse_Mat< T > &m) const
 Transpose the sparse matrix, return the result in m.
Sparse_Mat< T > transpose () const
 Returns the transpose of the sparse matrix.
void operator= (const Sparse_Mat< T > &m)
 Assign sparse matrix the value and dimensions of the sparse matrix m.
void operator= (const Mat< T > &m)
 Assign sparse matrix the value and dimensions of the dense matrix m.
Sparse_Mat< T > operator- () const
 Returns the sign inverse of all elements in the sparse matrix.
bool operator== (const Sparse_Mat< T > &m) const
 Compare two sparse matricies. False if wrong sizes or different values.
void operator+= (const Sparse_Mat< T > &v)
 Add sparse matrix v to all non-zero elements of the sparse matrix.
void operator+= (const Mat< T > &v)
 Add matrix v to all non-zero elements of the sparse matrix.
void operator-= (const Sparse_Mat< T > &v)
 Subtract sparse matrix v from all non-zero elements of the sparse matrix.
void operator-= (const Mat< T > &v)
 Subtract matrix v from all non-zero elements of the sparse matrix.
void operator *= (const T &v)
 Multiply all non-zero elements of the sparse matrix with the scalar v.
void operator/= (const T &v)
 Divide all non-zero elements of the sparse matrix with the scalar v.

Friends

Sparse_Mat< T > operator+ (const Sparse_Mat< T > &m1, const Sparse_Mat< T > &m2)
 Addition m1+m2 where m1 and m2 are sparse matrices.
Sparse_Mat< T > operator * (const T &c, const Sparse_Mat< T > &m)
 Multiplication c*m where c is a scalar and m is a sparse matrix.
Sparse_Mat< T > operator * (const Sparse_Mat< T > &m1, const Sparse_Mat< T > &m2)
 Multiplication m1*m2 where m1 and m2 are sparse matrices.
Sparse_Vec< T > operator * (const Sparse_Mat< T > &m, const Sparse_Vec< T > &v)
 Multiplication m*v where m is a sparse matrix and v is a sparse vector.
Vec< T > operator * (const Sparse_Mat< T > &m, const Vec< T > &v)
 Multiplication m*v where m is a sparse matrix and v is a full column vector.
Vec< T > operator * (const Vec< T > &v, const Sparse_Mat< T > &m)
 Multiplication v'*m where m is a sparse matrix and v is a full column vector.
Mat< T > trans_mult (const Sparse_Mat< T > &m)
 Multiplication m'*m where m is a sparse matrix. Returns a full, dense matrix.
Sparse_Mat< T > trans_mult_s (const Sparse_Mat< T > &m)
 Multiplication m'*m where m is a sparse matrix, Returns a sparse matrix.
Sparse_Mat< T > trans_mult (const Sparse_Mat< T > &m1, const Sparse_Mat< T > &m2)
 Multiplication m1'*m2 where m1 and m2 are sparse matrices.
Vec< T > trans_mult (const Sparse_Mat< T > &m, const Vec< T > &v)
 Multiplication m'*v where m is a sparse matrix and v is a full column vector.
Sparse_Mat< T > mult_trans (const Sparse_Mat< T > &m1, const Sparse_Mat< T > &m2)
 Multiplication m1*m2' where m1 and m2 are sparse matrices.

Related Functions

(Note that these are not member functions.)

typedef Sparse_Mat< int > sparse_imat
 Sparse integer matrix.
typedef Sparse_Mat< double > sparse_mat
 Sparse double matrix.
typedef Sparse_Mat< std::complex<
double > > 
sparse_cmat
 Sparse complex<double> matrix.


Detailed Description

template<class T>
class itpp::Sparse_Mat< T >

Templated Sparse Matrix Class.

Author:
Tony Ottosson and Tobias Ringstrom
A sparse matrix is a matrix where most elements are zero. The maximum number of non-zero elements in each column is a parameter to the constructor.

Definition at line 104 of file smat.h.


Constructor & Destructor Documentation

template<class T>
itpp::Sparse_Mat< T >::Sparse_Mat (  ) 

Default constructor.

Definition at line 354 of file smat.h.

template<class T>
itpp::Sparse_Mat< T >::Sparse_Mat ( int  rows,
int  cols,
int  row_data_init = 200 
)

Initiate an empty sparse matrix.

A Sparse_Mat consists of colums that have the type Sparse_Vec. The maximum number of non-zero elements is each column is denoted row_data_init.

Parameters:
rows Number of rows in the matrix
cols Number of columns in the matrix
row_data_init The maximum number of non-zero elements in each column (default value is 200)

Definition at line 360 of file smat.h.

template<class T>
itpp::Sparse_Mat< T >::Sparse_Mat ( const Sparse_Mat< T > &  m  ) 

Initiate a new sparse matrix. The elements of m are copied into the new sparse matrix.

Definition at line 369 of file smat.h.

References itpp::Sparse_Mat< T >::col, itpp::Sparse_Mat< T >::n_cols, and itpp::Sparse_Mat< T >::n_rows.

template<class T>
itpp::Sparse_Mat< T >::Sparse_Mat ( const Mat< T > &  m  ) 

Initiate a new sparse matrix from a dense matrix. The elements of m are copied into the new sparse matrix.

Definition at line 381 of file smat.h.

References itpp::Mat< Num_T >::cols(), itpp::Mat< Num_T >::rows(), and itpp::Sparse_Mat< T >::set_new().

template<class T>
itpp::Sparse_Mat< T >::Sparse_Mat ( const Mat< T > &  m,
epsilon 
)

Initiate a new sparse matrix from a dense matrix. Elements of m larger than epsilon are copied into the new sparse matrix.

Note:
If the type T is double complex, then the elements of m larger than abs(epsilon) are copied into the new sparse matrix.

Definition at line 399 of file smat.h.

References itpp::abs(), itpp::Mat< Num_T >::cols(), itpp::Mat< Num_T >::rows(), and itpp::Sparse_Mat< T >::set_new().

template<class T>
itpp::Sparse_Mat< T >::~Sparse_Mat (  ) 

Destructor.

Definition at line 416 of file smat.h.


Member Function Documentation

template<class T>
void itpp::Sparse_Mat< T >::set_size ( int  rows,
int  cols,
int  row_data_init = -1 
)

Set the size of the sparse matrix.

A Sparse_Mat consists of colums that have the type Sparse_Vec. The maximum number of non-zero elements is each column is denoted row_data_init, with default value =-1 indicating that the number of data elements is not changed.

Parameters:
rows Number of rows in the matrix
cols Number of columns in the matrix
row_data_init The maximum number of non-zero elements in each column (default value -1 => allocated size for the data is not changed)

Definition at line 422 of file smat.h.

Referenced by itpp::Sparse_Mat< T >::transpose().

template<class T>
int itpp::Sparse_Mat< T >::rows (  )  const [inline]

Returns the number of rows of the sparse matrix.

Definition at line 151 of file smat.h.

Referenced by itpp::GF2mat::GF2mat(), itpp::Sparse_Mat< T >::operator+=(), and itpp::Sparse_Mat< T >::operator-=().

template<class T>
int itpp::Sparse_Mat< T >::cols (  )  const [inline]

Returns the number of columns of the sparse matrix.

Definition at line 154 of file smat.h.

Referenced by itpp::GF2mat::GF2mat(), itpp::Sparse_Mat< T >::operator+=(), and itpp::Sparse_Mat< T >::operator-=().

template<class T>
int itpp::Sparse_Mat< T >::nnz (  ) 

The number of non-zero elements in the sparse matrix.

Definition at line 435 of file smat.h.

Referenced by itpp::Sparse_Mat< T >::density().

template<class T>
double itpp::Sparse_Mat< T >::density (  ) 

Returns the density of the sparse matrix: (number of non-zero elements)/(total number of elements).

Definition at line 445 of file smat.h.

References itpp::Sparse_Mat< T >::nnz().

template<class T>
void itpp::Sparse_Mat< T >::compact (  ) 

Set the maximum number of non-zero elements in each column equal to the actual number of non-zero elements in each column.

Definition at line 452 of file smat.h.

Referenced by itpp::Sparse_Mat< T >::get_submatrix(), itpp::Sparse_Mat< T >::get_submatrix_cols(), and itpp::operator *().

template<class T>
void itpp::Sparse_Mat< T >::full ( Mat< T > &  m  )  const

Returns a full, dense matrix in m.

Definition at line 459 of file smat.h.

References itpp::Mat< Num_T >::set_size().

Referenced by itpp::full().

template<class T>
Mat< T > itpp::Sparse_Mat< T >::full (  )  const

Returns a full, dense matrix.

Definition at line 470 of file smat.h.

template<class T>
T itpp::Sparse_Mat< T >::operator() ( int  r,
int  c 
) const

Returns element of row r and column c.

Definition at line 478 of file smat.h.

References it_assert0.

template<class T>
void itpp::Sparse_Mat< T >::set ( int  r,
int  c,
v 
)

Set element (r, c ) equal to v.

Definition at line 485 of file smat.h.

References it_assert0.

Referenced by itpp::Sparse_Mat< T >::set_submatrix(), and itpp::GF2mat::sparsify().

template<class T>
void itpp::Sparse_Mat< T >::set_new ( int  r,
int  c,
v 
)

Set a new element with index (r, c ) equal to v.

Definition at line 492 of file smat.h.

References it_assert0.

Referenced by itpp::operator *(), itpp::Sparse_Mat< T >::operator=(), and itpp::Sparse_Mat< T >::Sparse_Mat().

template<class T>
void itpp::Sparse_Mat< T >::add_elem ( const int  r,
const int  c,
const T  v 
)

Add the element in row r and column c with v.

Definition at line 499 of file smat.h.

References it_assert0.

template<class T>
void itpp::Sparse_Mat< T >::zeros (  ) 

Set the sparse matrix to the all zero matrix (removes all non-zero elements).

Definition at line 506 of file smat.h.

template<class T>
void itpp::Sparse_Mat< T >::zero_elem ( const int  r,
const int  c 
)

Set the element in row r and column c to zero (i.e. clear that element if it contains a non-zero value).

Definition at line 513 of file smat.h.

References it_assert0.

template<class T>
void itpp::Sparse_Mat< T >::clear (  ) 

Clear all non-zero elements of the sparse matrix.

Definition at line 520 of file smat.h.

template<class T>
void itpp::Sparse_Mat< T >::clear_elem ( const int  r,
const int  c 
)

Clear the element in row r and column c (if it contains a non-zero value).

Definition at line 527 of file smat.h.

References it_assert0.

template<class T>
void itpp::Sparse_Mat< T >::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.

Definition at line 534 of file smat.h.

References itpp::Mat< Num_T >::cols(), it_assert1, itpp::Mat< Num_T >::rows(), and itpp::Sparse_Mat< T >::set().

template<class T>
void itpp::Sparse_Mat< T >::set_submatrix ( int  r,
int  c,
const Mat< T > &  m 
)

Set submatrix defined by upper-left element (r,c) and the size of matrix m to m.

Definition at line 555 of file smat.h.

References itpp::Mat< Num_T >::cols(), it_assert1, itpp::Mat< Num_T >::rows(), and itpp::Sparse_Mat< T >::set().

template<class T>
Sparse_Mat< T > itpp::Sparse_Mat< T >::get_submatrix ( int  r1,
int  r2,
int  c1,
int  c2 
) const

Returns the sub-matrix from rows r1 to r2 and columns c1 to c2.

Definition at line 568 of file smat.h.

References itpp::Sparse_Mat< T >::col, itpp::Sparse_Mat< T >::compact(), and it_assert0.

template<class T>
Sparse_Mat< T > itpp::Sparse_Mat< T >::get_submatrix_cols ( int  c1,
int  c2 
) const

Returns the sub-matrix from columns c1 to c2 (all rows).

Definition at line 583 of file smat.h.

References itpp::Sparse_Mat< T >::col, itpp::Sparse_Mat< T >::compact(), and it_assert0.

template<class T>
void itpp::Sparse_Mat< T >::get_col ( int  c,
Sparse_Vec< T > &  v 
) const

Returns column c of the Sparse_Mat in the Sparse_Vec v.

Definition at line 596 of file smat.h.

References it_assert.

Referenced by itpp::GF2mat::GF2mat(), itpp::Sparse_Mat< T >::operator+=(), and itpp::Sparse_Mat< T >::operator-=().

template<class T>
Sparse_Vec< T > itpp::Sparse_Mat< T >::get_col ( int  c  )  const

Returns column c of the Sparse_Mat.

Definition at line 603 of file smat.h.

References it_assert.

template<class T>
void itpp::Sparse_Mat< T >::set_col ( int  c,
const Sparse_Vec< T > &  v 
)

Set column c of the Sparse_Mat.

Definition at line 610 of file smat.h.

References it_assert.

template<class T>
void itpp::Sparse_Mat< T >::transpose ( Sparse_Mat< T > &  m  )  const

Transpose the sparse matrix, return the result in m.

Definition at line 617 of file smat.h.

References itpp::Sparse_Mat< T >::col, and itpp::Sparse_Mat< T >::set_size().

Referenced by itpp::mult_trans(), and itpp::transpose().

template<class T>
Sparse_Mat< T > itpp::Sparse_Mat< T >::transpose (  )  const

Returns the transpose of the sparse matrix.

Definition at line 627 of file smat.h.

template<class T>
void itpp::Sparse_Mat< T >::operator= ( const Sparse_Mat< T > &  m  ) 

Assign sparse matrix the value and dimensions of the sparse matrix m.

Definition at line 635 of file smat.h.

References itpp::Sparse_Mat< T >::col, itpp::Sparse_Mat< T >::n_cols, and itpp::Sparse_Mat< T >::n_rows.

template<class T>
void itpp::Sparse_Mat< T >::operator= ( const Mat< T > &  m  ) 

Assign sparse matrix the value and dimensions of the dense matrix m.

Definition at line 647 of file smat.h.

References itpp::Mat< Num_T >::cols(), itpp::Mat< Num_T >::rows(), and itpp::Sparse_Mat< T >::set_new().

template<class T>
Sparse_Mat< T > itpp::Sparse_Mat< T >::operator- (  )  const

Returns the sign inverse of all elements in the sparse matrix.

Definition at line 664 of file smat.h.

References itpp::Sparse_Mat< T >::col.

template<class T>
bool itpp::Sparse_Mat< T >::operator== ( const Sparse_Mat< T > &  m  )  const

Compare two sparse matricies. False if wrong sizes or different values.

Definition at line 678 of file smat.h.

References itpp::Sparse_Mat< T >::col, itpp::Sparse_Mat< T >::n_cols, and itpp::Sparse_Mat< T >::n_rows.

template<class T>
void itpp::Sparse_Mat< T >::operator+= ( const Sparse_Mat< T > &  v  ) 

Add sparse matrix v to all non-zero elements of the sparse matrix.

Definition at line 691 of file smat.h.

References itpp::Sparse_Mat< T >::cols(), itpp::Sparse_Mat< T >::get_col(), it_assert0, and itpp::Sparse_Mat< T >::rows().

template<class T>
void itpp::Sparse_Mat< T >::operator+= ( const Mat< T > &  v  ) 

Add matrix v to all non-zero elements of the sparse matrix.

Definition at line 703 of file smat.h.

References itpp::Mat< Num_T >::cols(), itpp::Mat< Num_T >::get_col(), it_assert0, and itpp::Mat< Num_T >::rows().

template<class T>
void itpp::Sparse_Mat< T >::operator-= ( const Sparse_Mat< T > &  v  ) 

Subtract sparse matrix v from all non-zero elements of the sparse matrix.

Definition at line 712 of file smat.h.

References itpp::Sparse_Mat< T >::cols(), itpp::Sparse_Mat< T >::get_col(), it_assert0, and itpp::Sparse_Mat< T >::rows().

template<class T>
void itpp::Sparse_Mat< T >::operator-= ( const Mat< T > &  v  ) 

Subtract matrix v from all non-zero elements of the sparse matrix.

Definition at line 724 of file smat.h.

References itpp::Mat< Num_T >::cols(), itpp::Mat< Num_T >::get_col(), it_assert0, and itpp::Mat< Num_T >::rows().

template<class T>
void itpp::Sparse_Mat< T >::operator *= ( const T &  v  ) 

Multiply all non-zero elements of the sparse matrix with the scalar v.

Definition at line 733 of file smat.h.

template<class T>
void itpp::Sparse_Mat< T >::operator/= ( const T &  v  ) 

Divide all non-zero elements of the sparse matrix with the scalar v.

Definition at line 740 of file smat.h.


Friends And Related Function Documentation

template<class T>
typedef Sparse_Mat<int> sparse_imat [related]

Sparse integer matrix.

Definition at line 302 of file smat.h.

template<class T>
typedef Sparse_Mat<double> sparse_mat [related]

Sparse double matrix.

Definition at line 308 of file smat.h.

template<class T>
typedef Sparse_Mat<std::complex<double> > sparse_cmat [related]

Sparse complex<double> matrix.

Definition at line 314 of file smat.h.

template<class T>
Sparse_Mat<T> operator+ ( const Sparse_Mat< T > &  m1,
const Sparse_Mat< T > &  m2 
) [friend]

Addition m1+m2 where m1 and m2 are sparse matrices.

template<class T>
Sparse_Mat<T> operator * ( const T &  c,
const Sparse_Mat< T > &  m 
) [friend]

Multiplication c*m where c is a scalar and m is a sparse matrix.

template<class T>
Sparse_Mat<T> operator * ( const Sparse_Mat< T > &  m1,
const Sparse_Mat< T > &  m2 
) [friend]

Multiplication m1*m2 where m1 and m2 are sparse matrices.

template<class T>
Sparse_Vec<T> operator * ( const Sparse_Mat< T > &  m,
const Sparse_Vec< T > &  v 
) [friend]

Multiplication m*v where m is a sparse matrix and v is a sparse vector.

template<class T>
Vec<T> operator * ( const Sparse_Mat< T > &  m,
const Vec< T > &  v 
) [friend]

Multiplication m*v where m is a sparse matrix and v is a full column vector.

template<class T>
Vec<T> operator * ( const Vec< T > &  v,
const Sparse_Mat< T > &  m 
) [friend]

Multiplication v'*m where m is a sparse matrix and v is a full column vector.

template<class T>
Mat<T> trans_mult ( const Sparse_Mat< T > &  m  )  [friend]

Multiplication m'*m where m is a sparse matrix. Returns a full, dense matrix.

template<class T>
Sparse_Mat<T> trans_mult_s ( const Sparse_Mat< T > &  m  )  [friend]

Multiplication m'*m where m is a sparse matrix, Returns a sparse matrix.

template<class T>
Sparse_Mat<T> trans_mult ( const Sparse_Mat< T > &  m1,
const Sparse_Mat< T > &  m2 
) [friend]

Multiplication m1'*m2 where m1 and m2 are sparse matrices.

template<class T>
Vec<T> trans_mult ( const Sparse_Mat< T > &  m,
const Vec< T > &  v 
) [friend]

Multiplication m'*v where m is a sparse matrix and v is a full column vector.

template<class T>
Sparse_Mat<T> mult_trans ( const Sparse_Mat< T > &  m1,
const Sparse_Mat< T > &  m2 
) [friend]

Multiplication m1*m2' where m1 and m2 are sparse matrices.


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

Generated on Thu Apr 19 14:15:02 2007 for IT++ by Doxygen 1.5.1