/*! * \file * \brief Sparse Matrix Class Definitions * \author Tony Ottosson and Tobias Ringstrom * * ------------------------------------------------------------------------- * * IT++ - C++ library of mathematical, signal processing, speech processing, * and communications classes and functions * * Copyright (C) 1995-2007 (see AUTHORS file for a list of contributors) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * ------------------------------------------------------------------------- */ #ifndef SMAT_H #define SMAT_H #ifndef _MSC_VER # include #else # include #endif #include namespace itpp { // Declaration of class Vec template class Vec; // Declaration of class Mat template class Mat; // Declaration of class Sparse_Vec template class Sparse_Vec; // Declaration of class Sparse_Mat template class Sparse_Mat; // ------------------------ Sparse_Mat Friends ------------------------------------- //! m1+m2 where m1 and m2 are sparse matrices template Sparse_Mat operator+(const Sparse_Mat &m1, const Sparse_Mat &m2); //! c*m where c is a scalar and m is a sparse matrix template Sparse_Mat operator*(const T &c, const Sparse_Mat &m); //! m1*m2 where m1 and m2 are sparse matrices template Sparse_Mat operator*(const Sparse_Mat &m1, const Sparse_Mat &m2); //! m*v where m is a sparse matrix and v is a sparse vector template Sparse_Vec operator*(const Sparse_Mat &m, const Sparse_Vec &v); //! m*v where m is a sparse matrix and v is a full column vector template Vec operator*(const Sparse_Mat &m, const Vec &v); //! v'*m where m is a sparse matrix and v is a full column vector template Vec operator*(const Vec &v, const Sparse_Mat &m); //! m'*m where m is a sparse matrix template Mat trans_mult(const Sparse_Mat &m); //! m'*m where m is a sparse matrix template Sparse_Mat trans_mult_s(const Sparse_Mat &m); //! m1'*m2 where m1 and m2 are sparse matrices template Sparse_Mat trans_mult(const Sparse_Mat &m1, const Sparse_Mat &m2); //! m'*v where m is a sparse matrix and v is a full column vector template Vec trans_mult(const Sparse_Mat &m, const Vec &v); //! m1*m2' where m1 and m2 are sparse matrices template Sparse_Mat mult_trans(const Sparse_Mat &m1, const Sparse_Mat &m2); /*! \brief 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. */ template class Sparse_Mat { public: //! Default constructor Sparse_Mat(); /*! \brief 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 \c row_data_init. \param rows Number of rows in the matrix \param cols Number of columns in the matrix \param row_data_init The maximum number of non-zero elements in each column (default value is 200) */ Sparse_Mat(int rows, int cols, int row_data_init=200); //! Initiate a new sparse matrix. The elements of \c m are copied into the new sparse matrix Sparse_Mat(const Sparse_Mat &m); //! Initiate a new sparse matrix from a dense matrix. The elements of \c m are copied into the new sparse matrix Sparse_Mat(const Mat &m); /*! \brief Initiate a new sparse matrix from a dense matrix. Elements of \c m larger than \c epsilon are copied into the new sparse matrix. \note If the type T is double complex, then the elements of \c m larger than \c abs(epsilon) are copied into the new sparse matrix. */ Sparse_Mat(const Mat &m, T epsilon); //! Destructor ~Sparse_Mat(); /*! \brief 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 \c row_data_init, with default value =-1 indicating that the number of data elements is not changed. \param rows Number of rows in the matrix \param cols Number of columns in the matrix \param row_data_init The maximum number of non-zero elements in each column (default value -1 \c => allocated size for the data is not changed) */ void set_size(int rows, int cols, int row_data_init=-1); //! Returns the number of rows of the sparse matrix int rows() const { return n_rows; } //! Returns the number of columns of the sparse matrix int cols() const { return n_cols; } //! The number of non-zero elements in the sparse matrix int nnz(); //! Returns the density of the sparse matrix: (number of non-zero elements)/(total number of elements) double density(); //! Set the maximum number of non-zero elements in each column equal to the actual number of non-zero elements in each column void compact(); //! Returns a full, dense matrix in \c m void full(Mat &m) const; //! Returns a full, dense matrix Mat full() const; //! Returns element of row \c r and column \c c T operator()(int r, int c) const; //! Set element (\c r, \c c ) equal to \c v void set(int r, int c, T v); //! Set a new element with index (\c r, \c c ) equal to \c v void set_new(int r, int c, T v); //! Add the element in row \c r and column \c c with \c v void add_elem(const int r, const int c, const T v); //! Set the sparse matrix to the all zero matrix (removes all non-zero elements) void zeros(); //! Set the element in row \c r and column \c c to zero (i.e. clear that element if it contains a non-zero value) void zero_elem(const int r, const int c); //! Clear all non-zero elements of the sparse matrix void clear(); //! Clear the element in row \c r and column \c c (if it contains a non-zero value) void clear_elem(const int r, const int c); //! 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 Mat &m); //! Set submatrix defined by upper-left element (\c r,\c c) and the size of matrix \c m to \c m void set_submatrix(int r, int c, const Mat& m); //! Returns the sub-matrix from rows \c r1 to \c r2 and columns \c c1 to \c c2 Sparse_Mat get_submatrix(int r1, int r2, int c1, int c2) const; //! Returns the sub-matrix from columns \c c1 to \c c2 (all rows) Sparse_Mat get_submatrix_cols(int c1, int c2) const; //! Returns column \c c of the Sparse_Mat in the Sparse_Vec \c v void get_col(int c, Sparse_Vec &v) const; //! Returns column \c c of the Sparse_Mat Sparse_Vec get_col(int c) const; //! Set column \c c of the Sparse_Mat void set_col(int c, const Sparse_Vec &v); /*! Transpose the sparse matrix, return the result in \c m Note: this function can be slow for large matrices. */ void transpose(Sparse_Mat &m) const; /*! Returns the transpose of the sparse matrix Note: this function can be slow for large matrices. */ Sparse_Mat transpose() const; /*! Returns the transpose of the sparse matrix Note: this function can be slow for large matrices. */ // Sparse_Mat T() const { return this->transpose(); }; //! Assign sparse matrix the value and dimensions of the sparse matrix \c m void operator=(const Sparse_Mat &m); //! Assign sparse matrix the value and dimensions of the dense matrix \c m void operator=(const Mat &m); //! Returns the sign inverse of all elements in the sparse matrix Sparse_Mat operator-() const; //! Compare two sparse matricies. False if wrong sizes or different values bool operator==(const Sparse_Mat &m) const; //! Add sparse matrix \c v to all non-zero elements of the sparse matrix void operator+=(const Sparse_Mat &v); //! Add matrix \c v to all non-zero elements of the sparse matrix void operator+=(const Mat &v); //! Subtract sparse matrix \c v from all non-zero elements of the sparse matrix void operator-=(const Sparse_Mat &v); //! Subtract matrix \c v from all non-zero elements of the sparse matrix void operator-=(const Mat &v); //! Multiply all non-zero elements of the sparse matrix with the scalar \c v void operator*=(const T &v); //! Divide all non-zero elements of the sparse matrix with the scalar \c v void operator/=(const T &v); //! Addition m1+m2 where m1 and m2 are sparse matrices friend Sparse_Mat operator+<>(const Sparse_Mat &m1, const Sparse_Mat &m2); //! Multiplication c*m where c is a scalar and m is a sparse matrix friend Sparse_Mat operator*<>(const T &c, const Sparse_Mat &m); //! Multiplication m1*m2 where m1 and m2 are sparse matrices friend Sparse_Mat operator*<>(const Sparse_Mat &m1, const Sparse_Mat &m2); //! Multiplication m*v where m is a sparse matrix and v is a sparse vector friend Sparse_Vec operator*<>(const Sparse_Mat &m, const Sparse_Vec &v); //! Multiplication m*v where m is a sparse matrix and v is a full column vector friend Vec operator*<>(const Sparse_Mat &m, const Vec &v); //! Multiplication v'*m where m is a sparse matrix and v is a full column vector friend Vec operator*<>(const Vec &v, const Sparse_Mat &m); //! Multiplication m'*m where m is a sparse matrix. Returns a full, dense matrix friend Mat trans_mult <>(const Sparse_Mat &m); //! Multiplication m'*m where m is a sparse matrix, Returns a sparse matrix friend Sparse_Mat trans_mult_s <>(const Sparse_Mat &m); //! Multiplication m1'*m2 where m1 and m2 are sparse matrices friend Sparse_Mat trans_mult <>(const Sparse_Mat &m1, const Sparse_Mat &m2); //! Multiplication m'*v where m is a sparse matrix and v is a full column vector friend Vec trans_mult <>(const Sparse_Mat &m, const Vec &v); //! Multiplication m1*m2' where m1 and m2 are sparse matrices friend Sparse_Mat mult_trans <>(const Sparse_Mat &m1, const Sparse_Mat &m2); private: void init(); void alloc_empty(); void alloc(int row_data_size=200); void free(); int n_rows, n_cols; Sparse_Vec *col; }; /*! \relates Sparse_Mat \brief Sparse integer matrix */ typedef Sparse_Mat sparse_imat; /*! \relates Sparse_Mat \brief Sparse double matrix */ typedef Sparse_Mat sparse_mat; /*! \relates Sparse_Mat \brief Sparse complex matrix */ typedef Sparse_Mat > sparse_cmat; //---------------------- Implementation starts here -------------------------------- template void Sparse_Mat::init() { n_rows = 0; n_cols = 0; col = 0; } template void Sparse_Mat::alloc_empty() { if (n_cols == 0) col = 0; else col = new Sparse_Vec[n_cols]; } template void Sparse_Mat::alloc(int row_data_init) { if (n_cols == 0) col = 0; else col = new Sparse_Vec[n_cols]; for (int c=0; c void Sparse_Mat::free() { delete [] col; col = 0; } template Sparse_Mat::Sparse_Mat() { init(); } template Sparse_Mat::Sparse_Mat(int rows, int cols, int row_data_init) { init(); n_rows = rows; n_cols = cols; alloc(row_data_init); } template Sparse_Mat::Sparse_Mat(const Sparse_Mat &m) { init(); n_rows = m.n_rows; n_cols = m.n_cols; alloc_empty(); for (int c=0; c Sparse_Mat::Sparse_Mat(const Mat &m) { init(); n_rows = m.rows(); n_cols = m.cols(); alloc(); for (int c=0; c Sparse_Mat::Sparse_Mat(const Mat &m, T epsilon) { init(); n_rows = m.rows(); n_cols = m.cols(); alloc(); for (int c=0; c std::abs(epsilon)) col[c].set_new(r, m(r,c)); } col[c].compact(); } } template Sparse_Mat::~Sparse_Mat() { free(); } template void Sparse_Mat::set_size(int rows, int cols, int row_data_init) { n_rows = rows; //Allocate new memory for data if the number of columns has changed or if row_data_init != -1 if (cols!=n_cols||row_data_init!=-1) { n_cols = cols; free(); alloc(row_data_init); } } template int Sparse_Mat::nnz() { int n=0; for (int c=0; c double Sparse_Mat::density() { //return static_cast(nnz())/(n_rows*n_cols); return double(nnz())/(n_rows*n_cols); } template void Sparse_Mat::compact() { for (int c=0; c void Sparse_Mat::full(Mat &m) const { m.set_size(n_rows, n_cols); m = T(0); for (int c=0; c Mat Sparse_Mat::full() const { Mat r(n_rows, n_cols); full(r); return r; } template T Sparse_Mat::operator()(int r, int c) const { it_assert_debug(r>=0&&r=0&&c void Sparse_Mat::set(int r, int c, T v) { it_assert_debug(r>=0&&r=0&&c void Sparse_Mat::set_new(int r, int c, T v) { it_assert_debug(r>=0&&r=0&&c void Sparse_Mat::add_elem(int r, int c, T v) { it_assert_debug(r>=0&&r=0&&c void Sparse_Mat::zeros() { for (int c=0; c void Sparse_Mat::zero_elem(const int r, const int c) { it_assert_debug(r>=0&&r=0&&c void Sparse_Mat::clear() { for (int c=0; c void Sparse_Mat::clear_elem(const int r, const int c) { it_assert_debug(r>=0&&r=0&&c void Sparse_Mat::set_submatrix(int r1, int r2, int c1, int c2, const Mat& m) { if (r1 == -1) r1 = n_rows-1; if (r2 == -1) r2 = n_rows-1; if (c1 == -1) c1 = n_cols-1; if (c2 == -1) c2 = n_cols-1; it_assert_debug(r1>=0 && r2>=0 && r1=0 && c2>=0 && c1::set_submatrix(): index out of range"); it_assert_debug(r2>=r1 && c2>=c1, "Sparse_Mat::set_submatrix: r2::set_submatrix(): sizes don't match"); for (int i=0 ; i void Sparse_Mat::set_submatrix(int r, int c, const Mat& m) { it_assert_debug(r>=0 && r+m.rows()<=n_rows && c>=0 && c+m.cols()<=n_cols, "Sparse_Mat::set_submatrix(): index out of range"); for (int i=0 ; i Sparse_Mat Sparse_Mat::get_submatrix(int r1, int r2, int c1, int c2) const { it_assert_debug(r1<=r2 && r1>=0 && r1=0 && c1::get_submatrix(): illegal input variables"); Sparse_Mat r(r2-r1+1, c2-c1+1); for (int c=c1; c<=c2; c++) r.col[c-c1] = col[c].get_subvector(r1, r2); r.compact(); return r; } template Sparse_Mat Sparse_Mat::get_submatrix_cols(int c1, int c2) const { it_assert_debug(c1<=c2 && c1>=0 && c1::get_submatrix_cols()"); Sparse_Mat r(n_rows, c2-c1+1, 0); for (int c=c1; c<=c2; c++) r.col[c-c1] = col[c]; r.compact(); return r; } template void Sparse_Mat::get_col(int c, Sparse_Vec &v) const { it_assert(c>=0 && c::get_col()"); v = col[c]; } template Sparse_Vec Sparse_Mat::get_col(int c) const { it_assert(c>=0 && c::get_col()"); return col[c]; } template void Sparse_Mat::set_col(int c, const Sparse_Vec &v) { it_assert(c>=0 && c::set_col()"); col[c] = v; } template void Sparse_Mat::transpose(Sparse_Mat &m) const { m.set_size(n_cols, n_rows); for (int c=0; c Sparse_Mat Sparse_Mat::transpose() const { Sparse_Mat m; transpose(m); return m; } template void Sparse_Mat::operator=(const Sparse_Mat &m) { free(); n_rows = m.n_rows; n_cols = m.n_cols; alloc_empty(); for (int c=0; c void Sparse_Mat::operator=(const Mat &m) { free(); n_rows = m.rows(); n_cols = m.cols(); alloc(); for (int c=0; c Sparse_Mat Sparse_Mat::operator-() const { Sparse_Mat r(n_rows, n_cols, 0); for (int c=0; c bool Sparse_Mat::operator==(const Sparse_Mat &m) const { if (n_rows!=m.n_rows || n_cols!=m.n_cols) return false; for (int c=0; c void Sparse_Mat::operator+=(const Sparse_Mat &m) { it_assert_debug(m.rows()==n_rows&&m.cols()==n_cols, "Addition of unequal sized matrices is not allowed"); Sparse_Vec v; for (int c=0; c void Sparse_Mat::operator+=(const Mat &m) { it_assert_debug(m.rows()==n_rows&&m.cols()==n_cols, "Addition of unequal sized matrices is not allowed"); for (int c=0; c void Sparse_Mat::operator-=(const Sparse_Mat &m) { it_assert_debug(m.rows()==n_rows&&m.cols()==n_cols, "Subtraction of unequal sized matrices is not allowed"); Sparse_Vec v; for (int c=0; c void Sparse_Mat::operator-=(const Mat &m) { it_assert_debug(m.rows()==n_rows&&m.cols()==n_cols, "Subtraction of unequal sized matrices is not allowed"); for (int c=0; c void Sparse_Mat::operator*=(const T &m) { for (int c=0; c void Sparse_Mat::operator/=(const T &m) { for (int c=0; c Sparse_Mat operator+(const Sparse_Mat &m1, const Sparse_Mat &m2) { it_assert_debug(m1.n_cols == m2.n_cols && m1.n_rows == m2.n_rows , "Sparse_Mat + Sparse_Mat"); Sparse_Mat m(m1.n_rows, m1.n_cols, 0); for (int c=0; c Sparse_Mat operator*(const T &c, const Sparse_Mat &m) { int i,j; Sparse_Mat ret(m.n_rows,m.n_cols); for (j=0; j Sparse_Mat operator*(const Sparse_Mat &m1, const Sparse_Mat &m2) { it_assert_debug(m1.n_cols == m2.n_rows, "Sparse_Mat * Sparse_Mat"); Sparse_Mat ret(m1.n_rows, m2.n_cols); for (int c=0; c &m2colc=m2.col[c]; for (int p2=0; p2 &mcol = m1.col[m2colc.get_nz_index(p2)]; T x = m2colc.get_nz_data(p2); for (int p1=0; p1 &mcol = m1.col[m2.col[c].get_nz_index(p2)]; */ /* for (int p1=0; p1 */ /* Sparse_Mat operator*(const Sparse_Mat &m1, const Sparse_Mat &m2) */ /* { */ /* it_assert_debug(m1.n_cols == m2.n_rows, "Sparse_Mat * Sparse_Mat"); */ /* Sparse_Mat ret(m1.n_rows, m2.n_cols); */ /* ivec occupied_by(ret.n_rows), pos(ret.n_rows); */ /* for (int rp=0; rp &m2col = m2.col[c]; */ /* for (int p2=0; p2 &m1col = m1.col[m2col.get_nz_index(p2)]; */ /* for (int p1=0; p1 Sparse_Vec operator*(const Sparse_Mat &m, const Sparse_Vec &v) { it_assert_debug(m.n_cols == v.size(), "Sparse_Mat * Sparse_Vec"); Sparse_Vec ret(m.n_rows); /* The two lines below added because the input parameter "v" is declared const, but the some functions (e.g., nnz()) change the vector... Is there a better workaround? */ Sparse_Vec vv = v; for (int p2=0; p2 &mcol = m.col[vv.get_nz_index(p2)]; T x = vv.get_nz_data(p2); for (int p1=0; p1 Vec operator*(const Sparse_Mat &m, const Vec &v) { it_assert_debug(m.n_cols == v.size(), "Sparse_Mat * Vec"); Vec r(m.n_rows); r.clear(); for (int c=0; c Vec operator*(const Vec &v, const Sparse_Mat &m) { it_assert_debug(v.size() == m.n_rows, "Vec * Sparse_Mat"); Vec r(m.n_cols); r.clear(); for (int c=0; c Mat trans_mult(const Sparse_Mat &m) { Mat ret(m.n_cols, m.n_cols); Vec col; for (int c=0; c Sparse_Mat trans_mult_s(const Sparse_Mat &m) { Sparse_Mat ret(m.n_cols, m.n_cols); Vec col; T tmp; for (int c=0; c Sparse_Mat trans_mult(const Sparse_Mat &m1, const Sparse_Mat &m2) { it_assert_debug(m1.n_rows == m2.n_rows, "trans_mult()"); Sparse_Mat ret(m1.n_cols, m2.n_cols); Vec col; for (int c=0; c Vec trans_mult(const Sparse_Mat &m, const Vec &v) { Vec r(m.n_cols); for (int c=0; c Sparse_Mat mult_trans(const Sparse_Mat &m1, const Sparse_Mat &m2) { return trans_mult(m1.transpose(),m2.transpose()); } //! Convert a dense matrix \c m into its sparse representation template inline Sparse_Mat sparse(const Mat &m, T epsilon) { Sparse_Mat s(m, epsilon); return s; } //! Convert a sparse matrix \c s into its dense representation template inline Mat full(const Sparse_Mat &s) { Mat m; s.full(m); return m; } //! Transpose a sparse matrix \c s template inline Sparse_Mat transpose(const Sparse_Mat &s) { Sparse_Mat m; s.transpose(m); return m; } //! \cond // --------------------------------------------------------------------- // Instantiations // --------------------------------------------------------------------- #ifdef HAVE_EXTERN_TEMPLATE extern template class Sparse_Mat; extern template class Sparse_Mat; extern template class Sparse_Mat >; extern template sparse_imat operator+(const sparse_imat &, const sparse_imat &); extern template sparse_mat operator+(const sparse_mat &, const sparse_mat &); extern template sparse_cmat operator+(const sparse_cmat &, const sparse_cmat &); extern template sparse_imat operator*(const sparse_imat &, const sparse_imat &); extern template sparse_mat operator*(const sparse_mat &, const sparse_mat &); extern template sparse_cmat operator*(const sparse_cmat &, const sparse_cmat &); extern template ivec operator*(const ivec &, const sparse_imat &); extern template vec operator*(const vec &, const sparse_mat &); extern template cvec operator*(const cvec &, const sparse_cmat &); extern template ivec operator*(const sparse_imat &, const ivec &); extern template vec operator*(const sparse_mat &, const vec &); extern template cvec operator*(const sparse_cmat &, const cvec &); extern template imat trans_mult(const sparse_imat &); extern template mat trans_mult(const sparse_mat &); extern template cmat trans_mult(const sparse_cmat &); extern template sparse_imat trans_mult_s(const sparse_imat &); extern template sparse_mat trans_mult_s(const sparse_mat &); extern template sparse_cmat trans_mult_s(const sparse_cmat &); extern template sparse_imat trans_mult(const sparse_imat &, const sparse_imat &); extern template sparse_mat trans_mult(const sparse_mat &, const sparse_mat &); extern template sparse_cmat trans_mult(const sparse_cmat &, const sparse_cmat &); extern template ivec trans_mult(const sparse_imat &, const ivec &); extern template vec trans_mult(const sparse_mat &, const vec &); extern template cvec trans_mult(const sparse_cmat &, const cvec &); extern template sparse_imat mult_trans(const sparse_imat &, const sparse_imat &); extern template sparse_mat mult_trans(const sparse_mat &, const sparse_mat &); extern template sparse_cmat mult_trans(const sparse_cmat &, const sparse_cmat &); #endif // HAVE_EXTERN_TEMPLATE //! \endcond } // namespace itpp #endif // #ifndef SMAT_H