33 #include <itpp/itexports.h>
39 template <
class T>
class Vec;
41 template <
class T>
class Mat;
43 template <
class T>
class Sparse_Vec;
45 template <
class T>
class Sparse_Mat;
51 Sparse_Mat<T>
operator+(
const Sparse_Mat<T> &m1,
const Sparse_Mat<T> &m2);
55 Sparse_Mat<T>
operator*(
const T &c,
const Sparse_Mat<T> &m);
59 Sparse_Mat<T>
operator*(
const Sparse_Mat<T> &m1,
const Sparse_Mat<T> &m2);
63 Sparse_Vec<T>
operator*(
const Sparse_Mat<T> &m,
const Sparse_Vec<T> &v);
67 Vec<T>
operator*(
const Sparse_Mat<T> &m,
const Vec<T> &v);
71 Vec<T>
operator*(
const Vec<T> &v,
const Sparse_Mat<T> &m);
83 Sparse_Mat<T>
trans_mult(
const Sparse_Mat<T> &m1,
const Sparse_Mat<T> &m2);
87 Vec<T>
trans_mult(
const Sparse_Mat<T> &m,
const Vec<T> &v);
91 Sparse_Mat<T>
mult_trans(
const Sparse_Mat<T> &m1,
const Sparse_Mat<T> &m2);
152 void set_size(
int rows,
int cols,
int row_data_init = -1);
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);
315 typedef Sparse_Mat<int> sparse_imat;
321 typedef Sparse_Mat<double> sparse_mat;
327 typedef Sparse_Mat<std::complex<double> > sparse_cmat;
332 void Sparse_Mat<T>::init()
340 void Sparse_Mat<T>::alloc_empty()
345 col =
new Sparse_Vec<T>[n_cols];
349 void 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);
360 void Sparse_Mat<T>::free()
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++)
1020 ITPP_EXPORT_TEMPLATE
template class ITPP_EXPORT Sparse_Mat<int>;
1021 ITPP_EXPORT_TEMPLATE
template class ITPP_EXPORT Sparse_Mat<double>;
1022 ITPP_EXPORT_TEMPLATE
template class ITPP_EXPORT Sparse_Mat<std::complex<double> >;
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