33#include <itpp/itexports.h>
39template <
class T>
class Vec;
41template <
class T>
class Mat;
43template <
class T>
class Sparse_Vec;
45template <
class T>
class Sparse_Mat;
51Sparse_Mat<T>
operator+(
const Sparse_Mat<T> &m1,
const Sparse_Mat<T> &m2);
55Sparse_Mat<T>
operator*(
const T &c,
const Sparse_Mat<T> &m);
59Sparse_Mat<T>
operator*(
const Sparse_Mat<T> &m1,
const Sparse_Mat<T> &m2);
63Sparse_Vec<T>
operator*(
const Sparse_Mat<T> &m,
const Sparse_Vec<T> &v);
67Vec<T>
operator*(
const Sparse_Mat<T> &m,
const Vec<T> &v);
71Vec<T>
operator*(
const Vec<T> &v,
const Sparse_Mat<T> &m);
83Sparse_Mat<T>
trans_mult(
const Sparse_Mat<T> &m1,
const Sparse_Mat<T> &m2);
87Vec<T>
trans_mult(
const Sparse_Mat<T> &m,
const Vec<T> &v);
91Sparse_Mat<T>
mult_trans(
const Sparse_Mat<T> &m1,
const Sparse_Mat<T> &m2);
155 int rows()
const {
return n_rows; }
158 int cols()
const {
return n_cols; }
349void Sparse_Mat<T>::alloc(
int row_data_init)
354 col =
new Sparse_Vec<T>[n_cols];
355 for (
int c = 0; c < n_cols; c++)
356 col[c].
set_size(n_rows, row_data_init);
360void Sparse_Mat<T>::free()
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++) {
421 if (std::abs(m(r,
c)) > std::abs(epsilon))
422 col[
c].set_new(r, m(r,
c));
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);
508 col[
c].set_new(r,
v);
515 col[
c].add_elem(r,
v);
521 for (
int c = 0;
c < n_cols;
c++)
535 for (
int c = 0;
c < n_cols;
c++)
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));
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);
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++)
721 col[
c] += (m.get_col(
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++)
742 col[
c] -= (m.get_col(
c));
748 for (
int c = 0;
c < n_cols;
c++)
755 for (
int c = 0;
c < n_cols;
c++)
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);
782 ret.set_new(k,
j, x);
795 for (
int c = 0;
c <
m2.n_cols;
c++) {
801 int r =
mcol.get_nz_index(
p1);
871 for (
int p2 = 0;
p2 <
vv.nnz();
p2++) {
873 T x =
vv.get_nz_data(
p2);
875 int r =
mcol.get_nz_index(
p1);
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++)
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;
948 tmp = m.col[
c].sqr();
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++)
1020ITPP_EXPORT_TEMPLATE
template class ITPP_EXPORT Sparse_Mat<int>;
1021ITPP_EXPORT_TEMPLATE
template class ITPP_EXPORT Sparse_Mat<double>;
1022ITPP_EXPORT_TEMPLATE
template class ITPP_EXPORT Sparse_Mat<std::complex<double> >;
1024ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_imat
operator+(
const sparse_imat &,
const sparse_imat &);
1025ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_mat
operator+(
const sparse_mat &,
const sparse_mat &);
1026ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_cmat
operator+(
const sparse_cmat &,
const sparse_cmat &);
1028ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_imat
operator*(
const sparse_imat &,
const sparse_imat &);
1029ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_mat
operator*(
const sparse_mat &,
const sparse_mat &);
1030ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_cmat
operator*(
const sparse_cmat &,
const sparse_cmat &);
1032ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT ivec
operator*(
const ivec &,
const sparse_imat &);
1033ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT vec
operator*(
const vec &,
const sparse_mat &);
1034ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT cvec
operator*(
const cvec &,
const sparse_cmat &);
1036ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT ivec
operator*(
const sparse_imat &,
const ivec &);
1037ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT vec
operator*(
const sparse_mat &,
const vec &);
1038ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT cvec
operator*(
const sparse_cmat &,
const cvec &);
1040ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT imat
trans_mult(
const sparse_imat &);
1041ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT mat
trans_mult(
const sparse_mat &);
1042ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT cmat
trans_mult(
const sparse_cmat &);
1044ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_imat
trans_mult_s(
const sparse_imat &);
1045ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_mat
trans_mult_s(
const sparse_mat &);
1046ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_cmat
trans_mult_s(
const sparse_cmat &);
1048ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_imat
trans_mult(
const sparse_imat &,
const sparse_imat &);
1049ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_mat
trans_mult(
const sparse_mat &,
const sparse_mat &);
1050ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_cmat
trans_mult(
const sparse_cmat &,
const sparse_cmat &);
1052ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT ivec
trans_mult(
const sparse_imat &,
const ivec &);
1053ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT vec
trans_mult(
const sparse_mat &,
const vec &);
1054ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT cvec
trans_mult(
const sparse_cmat &,
const cvec &);
1056ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_imat
mult_trans(
const sparse_imat &,
const sparse_imat &);
1057ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_mat
mult_trans(
const sparse_mat &,
const sparse_mat &);
1058ITPP_EXPORT_TEMPLATE
template ITPP_EXPORT sparse_cmat
mult_trans(
const sparse_cmat &,
const sparse_cmat &);
void alloc(int n)
Allocate storage for an array of length n.
void free()
Free the storage space allocated by the array.
int size() const
Returns the number of data elements in the array object.
void set_size(int n, bool copy=false)
Resizing an Array<T>.
Templated Sparse Matrix Class.
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.
void operator=(const Mat< T > &m)
Assign sparse matrix the value and dimensions of the dense matrix m.
void full(Mat< T > &m) const
Returns a full, dense matrix in m.
void clear()
Clear all non-zero elements of the sparse matrix.
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.
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 ma...
Sparse_Mat< std::complex< double > > sparse_cmat
Sparse complex<double> matrix.
int nnz()
The number of non-zero elements in the sparse matrix.
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 > 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.
void set(int r, int c, T v)
Set element (r, c ) equal to v.
double density()
Returns the density of the sparse matrix: (number of non-zero elements)/(total number of elements)
void zeros()
Set the sparse matrix to the all zero matrix (removes all non-zero elements)
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 operator/=(const T &v)
Divide all non-zero elements of the sparse matrix with the scalar v.
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...
int cols() const
Returns the number of columns 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 operator+=(const Mat< T > &v)
Add matrix v to all non-zero elements of the sparse matrix.
Sparse_Mat(int rows, int cols, int row_data_init=200)
Initiate an empty sparse matrix.
Sparse_Mat< int > sparse_imat
Sparse integer matrix.
Sparse_Mat< double > sparse_mat
Sparse double matrix.
void operator+=(const Sparse_Mat< T > &v)
Add sparse matrix v to all non-zero elements of the 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 t...
void operator-=(const Mat< T > &v)
Subtract matrix v from 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 set_new(int r, int c, T v)
Set a new element with index (r, c ) equal to v.
Mat< T > full() const
Returns a full, dense matrix.
Sparse_Mat< T > operator-() const
Returns the sign inverse of all elements in the 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< T > transpose() const
Sparse_Mat()
Default constructor.
void transpose(Sparse_Mat< T > &m) const
void set_size(int rows, int cols, int row_data_init=-1)
Set the size of 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 > &m)
Assign sparse matrix the value and dimensions of the sparse matrix m.
T operator()(int r, int c) const
Returns element of row r and column c.
void add_elem(const int r, const int c, const T v)
Add the element in row r and column c with v.
void set_col(int c, const Sparse_Vec< T > &v)
Set column c of the Sparse_Mat.
Sparse_Mat< T > get_submatrix_cols(int c1, int c2) const
Returns the sub-matrix from columns c1 to c2 (all rows)
int rows() const
Returns the number of rows of the sparse matrix.
void operator*=(const T &v)
Multiply all non-zero elements of the sparse matrix with the scalar v.
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
#define it_assert(t, s)
Abort if t is not true.
void transpose(const Mat< T > &m, Mat< T > &out)
Transposition of the matrix m returning the transposed matrix in out.
ITPP_EXPORT vec zeros(int size)
A Double vector of zeros.
GF2mat mult_trans(const GF2mat &X, const GF2mat &Y)
Multiplication X*Y' where X and Y are GF(2) matrices.
Sparse_Mat< T > sparse(const Mat< T > &m, T epsilon)
Convert a dense matrix m into its sparse representation.
Mat< T > full(const Sparse_Mat< T > &s)
Convert a sparse matrix s into its dense representation.
Sparse_Mat< T > trans_mult_s(const Sparse_Mat< T > &m)
m'*m where m is a sparse matrix
GF2mat operator*(const GF2mat &X, const GF2mat &Y)
GF(2) matrix multiplication.
Mat< T > trans_mult(const Sparse_Mat< T > &m)
m'*m where m is a sparse matrix
GF2mat operator+(const GF2mat &X, const GF2mat &Y)
GF(2) matrix addition.
Sparse Vector Class definitions.