IT++ Logo
Public Member Functions | Friends | List of all members
itpp::Sparse_Mat< T > Class Template Reference

Templated Sparse Matrix Class. More...

#include <itpp/base/smat.h>

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
 
Sparse_Mat< T > transpose () const
 
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.
 

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.

The implementation is based on representing all columns as sparse vectors. Thus, column access generally is much faster than row access. The elements in each vector are stored in random order, i.e. they are not sorted.

Definition at line 107 of file smat.h.

Constructor & Destructor Documentation

template<class T >
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
rowsNumber of rows in the matrix
colsNumber of columns in the matrix
row_data_initThe maximum number of non-zero elements in each column (default value is 200)

Definition at line 373 of file smat.h.

template<class T>
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 412 of file smat.h.

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

Member Function Documentation

template<class T >
void 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
rowsNumber of rows in the matrix
colsNumber of columns in the matrix
row_data_initThe maximum number of non-zero elements in each column (default value -1 => allocated size for the data is not changed)

Definition at line 435 of file smat.h.

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

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

Transpose the sparse matrix, return the result in m

Note: this function can be slow for large matrices.

Definition at line 630 of file smat.h.

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

Referenced by itpp::GF2mat_sparse_alist::from_sparse(), itpp::mult_trans(), itpp::GF2mat_sparse_alist::to_sparse(), and itpp::transpose().

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

Returns the transpose of the sparse matrix

Note: this function can be slow for large matrices.

Definition at line 640 of file smat.h.

References itpp::transpose().

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

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

Returns the transpose of the sparse matrix

Note: this function can be slow for large matrices.

Definition at line 648 of file smat.h.


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

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