33 #include <itpp/itexports.h> 39 template <
class T>
class Vec;
41 template <
class T>
class Mat;
155 int rows()
const {
return n_rows; }
158 int cols()
const {
return n_cols; }
179 void set(
int r,
int c, T v);
182 void set_new(
int r,
int c, T v);
185 void add_elem(
const int r,
const int c,
const T v);
191 void zero_elem(
const int r,
const int c);
304 void alloc(
int row_data_size = 200);
355 for (
int c = 0; c < n_cols; c++)
356 col[c].
set_size(n_rows, row_data_init);
378 alloc(row_data_init);
389 for (
int c = 0; c < n_cols; c++)
401 for (
int c = 0; c < n_cols; c++) {
402 for (
int r = 0; r < n_rows; r++) {
405 col[c].set_new(r, m(r, c));
419 for (
int c = 0; c < n_cols; c++) {
420 for (
int r = 0; r < n_rows; r++) {
422 col[c].set_new(r, m(r, c));
440 if (cols != n_cols || row_data_init != -1) {
443 alloc(row_data_init);
451 for (
int c = 0; c < n_cols; c++)
461 return double(
nnz()) / (n_rows*n_cols);
467 for (
int c = 0; c < n_cols; c++)
476 for (
int c = 0; c < n_cols; c++) {
477 for (
int p = 0; p < col[c].nnz(); p++)
478 m(col[c].get_nz_index(p), c) = col[c].get_nz_data(p);
493 it_assert_debug(r >= 0 && r<n_rows && c >= 0 && c < n_cols,
"Incorrect input indexes given");
500 it_assert_debug(r >= 0 && r<n_rows && c >= 0 && c < n_cols,
"Incorrect input indexes given");
507 it_assert_debug(r >= 0 && r<n_rows && c >= 0 && c < n_cols,
"Incorrect input indexes given");
508 col[c].set_new(r, v);
514 it_assert_debug(r >= 0 && r<n_rows && c >= 0 && c < n_cols,
"Incorrect input indexes given");
515 col[c].add_elem(r, v);
521 for (
int c = 0; c < n_cols; c++)
528 it_assert_debug(r >= 0 && r<n_rows && c >= 0 && c < n_cols,
"Incorrect input indexes given");
535 for (
int c = 0; c < n_cols; c++)
542 it_assert_debug(r >= 0 && r<n_rows && c >= 0 && c < n_cols,
"Incorrect input indexes given");
543 col[c].clear_elem(r);
549 if (r1 == -1) r1 = n_rows - 1;
550 if (r2 == -1) r2 = n_rows - 1;
551 if (c1 == -1) c1 = n_cols - 1;
552 if (c2 == -1) c2 = n_cols - 1;
555 c1 >= 0 && c2 >= 0 && c1 < n_cols && c2 < n_cols,
"Sparse_Mat<Num_T>::set_submatrix(): index out of range");
557 it_assert_debug(r2 >= r1 && c2 >= c1,
"Sparse_Mat<Num_T>::set_submatrix: r2<r1 or c2<c1");
558 it_assert_debug(m.
rows() == r2 - r1 + 1 && m.
cols() == c2 - c1 + 1,
"Mat<Num_T>::set_submatrix(): sizes don't match");
560 for (
int i = 0 ; i < m.
rows() ; i++) {
561 for (
int j = 0 ; j < m.
cols() ; j++) {
562 set(r1 + i, c1 + j, m(i, j));
571 c >= 0 && c + m.
cols() <= n_cols,
"Sparse_Mat<Num_T>::set_submatrix(): index out of range");
573 for (
int i = 0 ; i < m.
rows() ; i++) {
574 for (
int j = 0 ; j < m.
cols() ; j++) {
575 set(r + i, c + j, m(i, j));
583 it_assert_debug(r1 <= r2 && r1 >= 0 && r1 < n_rows && c1 <= c2 && c1 >= 0 && c1 < n_cols,
584 "Sparse_Mat<T>::get_submatrix(): illegal input variables");
588 for (
int c = c1; c <= c2; c++)
589 r.col[c-c1] = col[c].get_subvector(r1, r2);
598 it_assert_debug(c1 <= c2 && c1 >= 0 && c1 < n_cols,
"Sparse_Mat<T>::get_submatrix_cols()");
601 for (
int c = c1; c <= c2; c++)
602 r.col[c-c1] = col[c];
611 it_assert(c >= 0 && c < n_cols,
"Sparse_Mat<T>::get_col()");
618 it_assert(c >= 0 && c < n_cols,
"Sparse_Mat<T>::get_col()");
625 it_assert(c >= 0 && c < n_cols,
"Sparse_Mat<T>::set_col()");
633 for (
int c = 0; c < n_cols; c++) {
634 for (
int p = 0; p < col[c].nnz(); p++)
635 m.col[col[c].get_nz_index(p)].set_new(c, col[c].get_nz_data(p));
655 for (
int c = 0; c < n_cols; c++)
667 for (
int c = 0; c < n_cols; c++) {
668 for (
int r = 0; r < n_rows; r++) {
670 col[c].set_new(r, m(r, c));
681 for (
int c = 0; c < n_cols; c++) {
682 r.col[c].resize_data(col[c].
nnz());
683 for (
int p = 0; p < col[c].nnz(); p++)
684 r.col[c].set_new(col[c].get_nz_index(p), -col[c].get_nz_data(p));
693 if (n_rows != m.n_rows || n_cols != m.n_cols)
695 for (
int c = 0; c < n_cols; c++) {
696 if (!(col[c] == m.col[c]))
706 it_assert_debug(m.
rows() == n_rows && m.
cols() == n_cols,
"Addition of unequal sized matrices is not allowed");
709 for (
int c = 0; c < n_cols; c++) {
718 it_assert_debug(m.
rows() == n_rows && m.
cols() == n_cols,
"Addition of unequal sized matrices is not allowed");
720 for (
int c = 0; c < n_cols; c++)
727 it_assert_debug(m.
rows() == n_rows && m.
cols() == n_cols,
"Subtraction of unequal sized matrices is not allowed");
730 for (
int c = 0; c < n_cols; c++) {
739 it_assert_debug(m.
rows() == n_rows && m.
cols() == n_cols,
"Subtraction of unequal sized matrices is not allowed");
741 for (
int c = 0; c < n_cols; c++)
748 for (
int c = 0; c < n_cols; c++)
755 for (
int c = 0; c < n_cols; c++)
762 it_assert_debug(m1.n_cols == m2.n_cols && m1.n_rows == m2.n_rows ,
"Sparse_Mat<T> + Sparse_Mat<T>");
766 for (
int c = 0; c < m.n_cols; c++)
767 m.col[c] = m1.col[c] + m2.col[c];
778 for (j = 0; j < m.n_cols; j++) {
779 for (i = 0; i < m.col[j].nnz(); i++) {
780 T x = c * m.col[j].get_nz_data(i);
781 int k = m.col[j].get_nz_index(i);
791 it_assert_debug(m1.n_cols == m2.n_rows,
"Sparse_Mat<T> * Sparse_Mat<T>");
795 for (
int c = 0; c < m2.n_cols; c++) {
797 for (
int p2 = 0; p2 < m2colc.
nnz(); p2++) {
800 for (
int p1 = 0; p1 < mcol.
nnz(); p1++) {
803 ret.col[c].add_elem(r, inc);
871 for (
int p2 = 0; p2 < vv.
nnz(); p2++) {
874 for (
int p1 = 0; p1 < mcol.
nnz(); p1++) {
877 ret.add_elem(r, inc);
893 for (
int c = 0; c < m.n_cols; c++) {
894 for (
int p = 0; p < m.col[c].nnz(); p++)
895 r(m.col[c].get_nz_index(p)) += m.col[c].get_nz_data(p) * v(c);
909 for (
int c = 0; c < m.n_cols; c++)
918 Mat<T> ret(m.n_cols, m.n_cols);
920 for (
int c = 0; c < ret.
cols(); c++) {
922 for (
int r = 0; r < c; r++) {
923 T tmp = m.col[r] * col;
927 ret(c, c) = m.col[c].sqr();
939 for (
int c = 0; c < ret.n_cols; c++) {
941 for (
int r = 0; r < c; r++) {
942 tmp = m.col[r] * col;
944 ret.col[c].set_new(r, tmp);
945 ret.col[r].set_new(c, tmp);
948 tmp = m.col[c].sqr();
950 ret.col[c].set_new(c, tmp);
963 for (
int c = 0; c < ret.n_cols; c++) {
965 for (
int r = 0; r < ret.n_rows; r++)
966 ret.col[c].set_new(r, m1.col[r] * col);
976 for (
int c = 0; c < m.n_cols; c++)
1024 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_imat
operator+(
const sparse_imat &,
const sparse_imat &);
1025 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_mat
operator+(
const sparse_mat &,
const sparse_mat &);
1026 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_cmat
operator+(
const sparse_cmat &,
const sparse_cmat &);
1028 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_imat
operator*(
const sparse_imat &,
const sparse_imat &);
1029 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_mat
operator*(
const sparse_mat &,
const sparse_mat &);
1030 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_cmat
operator*(
const sparse_cmat &,
const sparse_cmat &);
1032 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT ivec
operator*(
const ivec &,
const sparse_imat &);
1033 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT vec
operator*(
const vec &,
const sparse_mat &);
1034 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT cvec
operator*(
const cvec &,
const sparse_cmat &);
1036 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT ivec
operator*(
const sparse_imat &,
const ivec &);
1037 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT vec
operator*(
const sparse_mat &,
const vec &);
1038 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT cvec
operator*(
const sparse_cmat &,
const cvec &);
1040 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT imat
trans_mult(
const sparse_imat &);
1041 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT mat
trans_mult(
const sparse_mat &);
1042 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT cmat
trans_mult(
const sparse_cmat &);
1044 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_imat
trans_mult_s(
const sparse_imat &);
1045 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_mat
trans_mult_s(
const sparse_mat &);
1046 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_cmat
trans_mult_s(
const sparse_cmat &);
1048 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_imat
trans_mult(
const sparse_imat &,
const sparse_imat &);
1049 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_mat
trans_mult(
const sparse_mat &,
const sparse_mat &);
1050 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_cmat
trans_mult(
const sparse_cmat &,
const sparse_cmat &);
1052 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT ivec
trans_mult(
const sparse_imat &,
const ivec &);
1053 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT vec
trans_mult(
const sparse_mat &,
const vec &);
1054 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT cvec
trans_mult(
const sparse_cmat &,
const cvec &);
1056 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_imat
mult_trans(
const sparse_imat &,
const sparse_imat &);
1057 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_mat
mult_trans(
const sparse_mat &,
const sparse_mat &);
1058 ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_cmat
mult_trans(
const sparse_cmat &,
const sparse_cmat &);
1064 #endif // #ifndef SMAT_H friend Sparse_Mat< T > operator+(const Sparse_Mat< T > &m1, const Sparse_Mat< T > &m2)
Addition m1+m2 where m1 and m2 are sparse matrices.
void set_size(int rows, int cols, bool copy=false)
Set size of matrix. If copy = true then keep the data before resizing.
int cols() const
The number of columns.
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 valu...
void set_col(int c, const Sparse_Vec< T > &v)
Set column c of the Sparse_Mat.
int rows() const
The number of rows.
void get_nz_data(int p, T &data_out)
Extract the reference to the p-th non-zero data element.
bool operator==(const Sparse_Mat< T > &m) const
Compare two sparse matricies. False if wrong sizes or different values.
int size() const
Returns the size of the sparse vector.
friend 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 > 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.
GF2mat mult_trans(const GF2mat &X, const GF2mat &Y)
Multiplication X*Y' where X and Y are GF(2) matrices.
Sparse_Mat< int > sparse_imat
Sparse integer matrix.
void set(int r, int c, T v)
Set element (r, c ) equal to v.
Mat< T > trans_mult(const Sparse_Mat< T > &m)
m'*m where m is a sparse matrix
Mat< T > full() const
Returns a full, dense matrix.
Sparse_Mat< T > get_submatrix_cols(int c1, int c2) const
Returns the sub-matrix from columns c1 to c2 (all rows)
#define it_assert(t, s)
Abort if t is not true.
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
Templated sparse vector class.
int cols() const
Returns the number of columns of the sparse matrix.
double density()
Returns the density of the sparse matrix: (number of non-zero elements)/(total number of elements) ...
Sparse_Mat< double > sparse_mat
Sparse double matrix.
Sparse_Mat< T > transpose() const
int get_nz_index(int p)
Returns the vector index of the p-th non-zero element.
void full(Mat< T > &m) const
Returns a full, dense matrix in m.
T operator()(int r, int c) const
Returns element of row r and column c.
GF2mat operator+(const GF2mat &X, const GF2mat &Y)
GF(2) matrix addition.
void set_new(int r, int c, T v)
Set a new element with index (r, c ) equal to v.
Sparse_Mat< T > trans_mult_s(const Sparse_Mat< T > &m)
m'*m where m is a sparse matrix
friend Mat< T > trans_mult(const Sparse_Mat< T > &m)
Multiplication m'*m where m is a sparse matrix. Returns a full, dense matrix.
void add_elem(const int r, const int c, const T v)
Add the element in row r and column c with v.
void get_col(int c, Sparse_Vec< T > &v) const
Returns column c of the Sparse_Mat in the Sparse_Vec v.
GF2mat operator*(const GF2mat &X, const GF2mat &Y)
GF(2) matrix multiplication.
int nnz()
Number of non-zero elements in the sparse vector.
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.
int size() const
The size of the vector.
int nnz()
The number of non-zero elements in the sparse matrix.
friend 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.
void clear()
Clear 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 Sparse_Mat< T > &m)
Assign sparse matrix the value and dimensions of the sparse matrix m.
void compact()
Set the maximum number of non-zero elements in each column equal to the actual number of non-zero ele...
Sparse_Mat< T > sparse(const Mat< T > &m, T epsilon)
Convert a dense matrix m into its sparse representation.
void operator+=(const Sparse_Mat< T > &v)
Add sparse matrix v to all non-zero elements of the sparse matrix.
int rows() const
Returns the number of rows of the sparse matrix.
Sparse_Mat< std::complex< double > > sparse_cmat
Sparse complex<double> matrix.
friend 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.
bin abs(const bin &inbin)
absolute value of bin
void set_size(int rows, int cols, int row_data_init=-1)
Set the size of the sparse matrix.
Vec< Num_T > get_col(int c) const
Get column c.
Sparse_Mat()
Default constructor.
Sparse_Mat< T > operator-() const
Returns the sign inverse of all elements in the sparse matrix.
Sparse Vector Class definitions.
void zeros()
Set the sparse matrix to the all zero matrix (removes all non-zero elements)
void operator/=(const T &v)
Divide all non-zero elements of the sparse matrix with the scalar v.
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 transpose(Sparse_Mat< T > &m) const
Templated Sparse Matrix Class.
void operator*=(const T &v)
Multiply all non-zero elements of the sparse matrix with the scalar v.