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.
T | 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.

## Related Symbols

(Note that these are not member symbols.)
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.

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

## Sparse_Mat() [1/5]

template<class T >

itpp::Sparse_Mat< T >::Sparse_Mat | ( |  | ) |

Default constructor.

Definition at line 367 of file smat.h.

## Sparse_Mat() [2/5]

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 373 of file smat.h.

References itpp::Array< T >::alloc().

## Sparse_Mat() [3/5]

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 382 of file smat.h.

## Sparse_Mat() [4/5]

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 394 of file smat.h.

References itpp::Array< T >::alloc().

## Sparse_Mat() [5/5]

template<class T >

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

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::Array< T >::alloc().

## ~Sparse_Mat()

template<class T >

itpp::Sparse_Mat< T >::~Sparse_Mat | ( |  | ) |

Destructor.

Definition at line 429 of file smat.h.

References itpp::Array< T >::free().

## Member Function Documentation

## set_size()

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 435 of file smat.h.

References itpp::Array< T >::alloc(), and itpp::Array< T >::free().

## rows()

template<class T >

int itpp::Sparse_Mat< T >::rows | ( |  | ) | const

inline

Returns the number of rows of the sparse matrix.

Definition at line 155 of file smat.h.

## cols()

template<class T >

int itpp::Sparse_Mat< T >::cols | ( |  | ) | const

inline

Returns the number of columns of the sparse matrix.

Definition at line 158 of file smat.h.

## nnz()

template<class T >

int itpp::Sparse_Mat< T >::nnz | ( |  | ) |

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

Definition at line 448 of file smat.h.

## 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 458 of file smat.h.

## compact()

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 465 of file smat.h.

## full() [1/2]

template<class T >

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

Returns a full, dense matrix in `m`.

Definition at line 472 of file smat.h.

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

## full() [2/2]

template<class T >

Mat< T > itpp::Sparse_Mat< T >::full | ( |  | ) | const

Returns a full, dense matrix.

Definition at line 483 of file smat.h.

References itpp::full().

## operator()()

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 491 of file smat.h.

References it_assert_debug.

## set()

template<class T >

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

Set element (`r`, `c` ) equal to `v`.

Definition at line 498 of file smat.h.

References it_assert_debug.

Referenced by itpp::LDPC_Parity::set(), and itpp::GF2mat::sparsify().

## set_new()

template<class T >

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

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

Definition at line 505 of file smat.h.

References it_assert_debug.

## add_elem()

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 512 of file smat.h.

References it_assert_debug.

## zeros()

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 519 of file smat.h.

References itpp::zeros().

## zero_elem()

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 526 of file smat.h.

References it_assert_debug.

## clear()

template<class T >

void itpp::Sparse_Mat< T >::clear | ( |  | ) |

Clear all non-zero elements of the sparse matrix.

Definition at line 533 of file smat.h.

## clear_elem()

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 540 of file smat.h.

References it_assert_debug.

Referenced by itpp::LDPC_Parity::set().

## set_submatrix() [1/2]

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 547 of file smat.h.

References it_assert_debug.

## set_submatrix() [2/2]

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 568 of file smat.h.

References it_assert_debug.

## get_submatrix()

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 581 of file smat.h.

References it_assert_debug.

## get_submatrix_cols()

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 596 of file smat.h.

References it_assert_debug.

## get_col() [1/2]

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 609 of file smat.h.

References it_assert.

## get_col() [2/2]

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 616 of file smat.h.

References it_assert.

## set_col()

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 623 of file smat.h.

References it_assert.

## transpose() [1/2]

template<class T >

void itpp::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::Array< T >::set_size().

## transpose() [2/2]

template<class T >

Sparse_Mat< T > itpp::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().

## operator=() [1/2]

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`.

Returns the transpose of the sparse matrix

Note: this function can be slow for large matrices.

Definition at line 648 of file smat.h.

References itpp::Array< T >::free().

## operator=() [2/2]

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 660 of file smat.h.

References itpp::Array< T >::alloc(), and itpp::Array< T >::free().

## operator-()

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 677 of file smat.h.

## operator==()

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 691 of file smat.h.

## operator+=() [1/2]

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 704 of file smat.h.

References it_assert_debug.

## operator+=() [2/2]

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 716 of file smat.h.

References it_assert_debug.

## operator-=() [1/2]

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 725 of file smat.h.

References it_assert_debug.

## operator-=() [2/2]

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 737 of file smat.h.

References it_assert_debug.

## operator*=()

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 746 of file smat.h.

## operator/=()

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 753 of file smat.h.

## Friends And Related Symbol Documentation

## sparse_imat

template<class T >

typedef Sparse_Mat<int> sparse_imat

related

Sparse integer matrix.

Definition at line 315 of file smat.h.

## sparse_mat

template<class T >

typedef Sparse_Mat<double> sparse_mat

related

Sparse double matrix.

Definition at line 321 of file smat.h.

## sparse_cmat

template<class T >

typedef Sparse_Mat<std::complex<double> > sparse_cmat

related

Sparse complex<double> matrix.

Definition at line 327 of file smat.h.

## operator+

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.

Definition at line 760 of file smat.h.

## operator* [1/5]

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.

Definition at line 774 of file smat.h.

## operator* [2/5]

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.

Definition at line 789 of file smat.h.

## operator* [3/5]

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.

Definition at line 860 of file smat.h.

## operator* [4/5]

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.

Definition at line 886 of file smat.h.

## operator* [5/5]

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.

Definition at line 902 of file smat.h.

## trans_mult [1/3]

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.

Definition at line 916 of file smat.h.

## trans_mult_s

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.

Definition at line 934 of file smat.h.

## trans_mult [2/3]

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.

Definition at line 957 of file smat.h.

## trans_mult [3/3]

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.

Definition at line 973 of file smat.h.

## mult_trans

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.

Definition at line 983 of file smat.h.

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

- itpp/base/smat.h

Generated on Mon Jun 10 2024 11:49:40 for IT++ by Doxygen 1.9.8