smat.h
Go to the documentation of this file.
#ifndef SMAT_H
#define SMAT_H
#include <itpp/base/svec.h>
#include <itpp/itexports.h>
namespace itpp
{
// Declaration of class Vec
template <class T> class Vec;
// Declaration of class Mat
template <class T> class Mat;
// Declaration of class Sparse_Vec
template <class T> class Sparse_Vec;
// Declaration of class Sparse_Mat
template <class T> class Sparse_Mat;
// ------------------------ Sparse_Mat Friends -------------------------------------
template <class T>
Sparse_Mat<T> operator+(const Sparse_Mat<T> &m1, const Sparse_Mat<T> &m2);
template <class T>
Sparse_Mat<T> operator*(const T &c, const Sparse_Mat<T> &m);
template <class T>
Sparse_Mat<T> operator*(const Sparse_Mat<T> &m1, const Sparse_Mat<T> &m2);
template <class T>
Sparse_Vec<T> operator*(const Sparse_Mat<T> &m, const Sparse_Vec<T> &v);
template <class T>
Vec<T> operator*(const Sparse_Mat<T> &m, const Vec<T> &v);
template <class T>
Vec<T> operator*(const Vec<T> &v, const Sparse_Mat<T> &m);
template <class T>
Mat<T> trans_mult(const Sparse_Mat<T> &m);
template <class T>
Sparse_Mat<T> trans_mult_s(const Sparse_Mat<T> &m);
template <class T>
Sparse_Mat<T> trans_mult(const Sparse_Mat<T> &m1, const Sparse_Mat<T> &m2);
template <class T>
Vec<T> trans_mult(const Sparse_Mat<T> &m, const Vec<T> &v);
template <class T>
Sparse_Mat<T> mult_trans(const Sparse_Mat<T> &m1, const Sparse_Mat<T> &m2);
template <class T>
class Sparse_Mat
{
public:
Sparse_Mat();
Sparse_Mat(int rows, int cols, int row_data_init = 200);
Sparse_Mat(const Sparse_Mat<T> &m);
Sparse_Mat(const Mat<T> &m);
Sparse_Mat(const Mat<T> &m, T epsilon);
~Sparse_Mat();
void set_size(int rows, int cols, int row_data_init = -1);
int rows() const { return n_rows; }
int cols() const { return n_cols; }
int nnz();
double density();
void compact();
void full(Mat<T> &m) const;
Mat<T> full() const;
T operator()(int r, int c) const;
void set(int r, int c, T v);
void set_new(int r, int c, T v);
void add_elem(const int r, const int c, const T v);
void zeros();
void zero_elem(const int r, const int c);
void clear();
void clear_elem(const int r, const int c);
void set_submatrix(int r1, int r2, int c1, int c2, const Mat<T> &m);
void set_submatrix(int r, int c, const Mat<T>& m);
Sparse_Mat<T> get_submatrix(int r1, int r2, int c1, int c2) const;
Sparse_Mat<T> get_submatrix_cols(int c1, int c2) const;
void get_col(int c, Sparse_Vec<T> &v) const;
Sparse_Vec<T> get_col(int c) const;
void set_col(int c, const Sparse_Vec<T> &v);
void transpose(Sparse_Mat<T> &m) const;
Sparse_Mat<T> transpose() const;
// Sparse_Mat<T> T() const { return this->transpose(); };
void operator=(const Sparse_Mat<T> &m);
void operator=(const Mat<T> &m);
Sparse_Mat<T> operator-() const;
bool operator==(const Sparse_Mat<T> &m) const;
void operator+=(const Sparse_Mat<T> &v);
void operator+=(const Mat<T> &v);
void operator-=(const Sparse_Mat<T> &v);
void operator-=(const Mat<T> &v);
void operator*=(const T &v);
void operator/=(const T &v);
friend Sparse_Mat<T> operator+<>(const Sparse_Mat<T> &m1, const Sparse_Mat<T> &m2);
friend Sparse_Mat<T> operator*<>(const T &c, const Sparse_Mat<T> &m);
friend Sparse_Mat<T> operator*<>(const Sparse_Mat<T> &m1, const Sparse_Mat<T> &m2);
friend Sparse_Vec<T> operator*<>(const Sparse_Mat<T> &m, const Sparse_Vec<T> &v);
friend Vec<T> operator*<>(const Sparse_Mat<T> &m, const Vec<T> &v);
friend Vec<T> operator*<>(const Vec<T> &v, const Sparse_Mat<T> &m);
friend Mat<T> trans_mult <>(const Sparse_Mat<T> &m);
friend Sparse_Mat<T> trans_mult_s <>(const Sparse_Mat<T> &m);
friend Sparse_Mat<T> trans_mult <>(const Sparse_Mat<T> &m1, const Sparse_Mat<T> &m2);
friend Vec<T> trans_mult <>(const Sparse_Mat<T> &m, const Vec<T> &v);
friend Sparse_Mat<T> mult_trans <>(const Sparse_Mat<T> &m1, const Sparse_Mat<T> &m2);
private:
void init();
void alloc_empty();
void alloc(int row_data_size = 200);
void free();
int n_rows, n_cols;
Sparse_Vec<T> *col;
};
typedef Sparse_Mat<int> sparse_imat;
typedef Sparse_Mat<double> sparse_mat;
typedef Sparse_Mat<std::complex<double> > sparse_cmat;
//---------------------- Implementation starts here --------------------------------
template <class T>
void Sparse_Mat<T>::init()
{
n_rows = 0;
n_cols = 0;
col = 0;
}
template <class T>
void Sparse_Mat<T>::alloc_empty()
{
if (n_cols == 0)
col = 0;
else
col = new Sparse_Vec<T>[n_cols];
}
template <class T>
void Sparse_Mat<T>::alloc(int row_data_init)
{
if (n_cols == 0)
col = 0;
else
col = new Sparse_Vec<T>[n_cols];
for (int c = 0; c < n_cols; c++)
col[c].set_size(n_rows, row_data_init);
}
template <class T>
void Sparse_Mat<T>::free()
{
delete [] col;
col = 0;
}
template <class T>
Sparse_Mat<T>::Sparse_Mat()
{
init();
}
template <class T>
Sparse_Mat<T>::Sparse_Mat(int rows, int cols, int row_data_init)
{
init();
n_rows = rows;
n_cols = cols;
alloc(row_data_init);
}
template <class T>
Sparse_Mat<T>::Sparse_Mat(const Sparse_Mat<T> &m)
{
init();
n_rows = m.n_rows;
n_cols = m.n_cols;
alloc_empty();
for (int c = 0; c < n_cols; c++)
col[c] = m.col[c];
}
template <class T>
Sparse_Mat<T>::Sparse_Mat(const Mat<T> &m)
{
init();
n_rows = m.rows();
n_cols = m.cols();
alloc();
for (int c = 0; c < n_cols; c++) {
for (int r = 0; r < n_rows; r++) {
//if (abs(m(r,c)) != T(0))
if (m(r, c) != T(0))
col[c].set_new(r, m(r, c));
}
col[c].compact();
}
}
template <class T>
Sparse_Mat<T>::Sparse_Mat(const Mat<T> &m, T epsilon)
{
init();
n_rows = m.rows();
n_cols = m.cols();
alloc();
for (int c = 0; c < n_cols; c++) {
for (int r = 0; r < n_rows; r++) {
if (std::abs(m(r, c)) > std::abs(epsilon))
col[c].set_new(r, m(r, c));
}
col[c].compact();
}
}
template <class T>
Sparse_Mat<T>::~Sparse_Mat()
{
free();
}
template <class T>
void Sparse_Mat<T>::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 <class T>
int Sparse_Mat<T>::nnz()
{
int n = 0;
for (int c = 0; c < n_cols; c++)
n += col[c].nnz();
return n;
}
template <class T>
double Sparse_Mat<T>::density()
{
//return static_cast<double>(nnz())/(n_rows*n_cols);
return double(nnz()) / (n_rows*n_cols);
}
template <class T>
void Sparse_Mat<T>::compact()
{
for (int c = 0; c < n_cols; c++)
col[c].compact();
}
template <class T>
void Sparse_Mat<T>::full(Mat<T> &m) const
{
m.set_size(n_rows, n_cols);
m = T(0);
for (int c = 0; c < n_cols; c++) {
for (int p = 0; p < col[c].nnz(); p++)
m(col[c].get_nz_index(p), c) = col[c].get_nz_data(p);
}
}
template <class T>
Mat<T> Sparse_Mat<T>::full() const
{
Mat<T> r(n_rows, n_cols);
full(r);
return r;
}
template <class T>
T Sparse_Mat<T>::operator()(int r, int c) const
{
it_assert_debug(r >= 0 && r<n_rows && c >= 0 && c < n_cols, "Incorrect input indexes given");
return col[c](r);
}
template <class T>
void Sparse_Mat<T>::set(int r, int c, T v)
{
it_assert_debug(r >= 0 && r<n_rows && c >= 0 && c < n_cols, "Incorrect input indexes given");
col[c].set(r, v);
}
template <class T>
void Sparse_Mat<T>::set_new(int r, int c, T v)
{
it_assert_debug(r >= 0 && r<n_rows && c >= 0 && c < n_cols, "Incorrect input indexes given");
col[c].set_new(r, v);
}
template <class T>
void Sparse_Mat<T>::add_elem(int r, int c, T v)
{
it_assert_debug(r >= 0 && r<n_rows && c >= 0 && c < n_cols, "Incorrect input indexes given");
col[c].add_elem(r, v);
}
template <class T>
void Sparse_Mat<T>::zeros()
{
for (int c = 0; c < n_cols; c++)
col[c].zeros();
}
template <class T>
void Sparse_Mat<T>::zero_elem(const int r, const int c)
{
it_assert_debug(r >= 0 && r<n_rows && c >= 0 && c < n_cols, "Incorrect input indexes given");
col[c].zero_elem(r);
}
template <class T>
void Sparse_Mat<T>::clear()
{
for (int c = 0; c < n_cols; c++)
col[c].clear();
}
template <class T>
void Sparse_Mat<T>::clear_elem(const int r, const int c)
{
it_assert_debug(r >= 0 && r<n_rows && c >= 0 && c < n_cols, "Incorrect input indexes given");
col[c].clear_elem(r);
}
template <class T>
void Sparse_Mat<T>::set_submatrix(int r1, int r2, int c1, int c2, const Mat<T>& 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 < n_rows && r2 < n_rows &&
c1 >= 0 && c2 >= 0 && c1 < n_cols && c2 < n_cols, "Sparse_Mat<Num_T>::set_submatrix(): index out of range");
it_assert_debug(r2 >= r1 && c2 >= c1, "Sparse_Mat<Num_T>::set_submatrix: r2<r1 or c2<c1");
it_assert_debug(m.rows() == r2 - r1 + 1 && m.cols() == c2 - c1 + 1, "Mat<Num_T>::set_submatrix(): sizes don't match");
for (int i = 0 ; i < m.rows() ; i++) {
for (int j = 0 ; j < m.cols() ; j++) {
set(r1 + i, c1 + j, m(i, j));
}
}
}
template <class T>
void Sparse_Mat<T>::set_submatrix(int r, int c, const Mat<T>& m)
{
it_assert_debug(r >= 0 && r + m.rows() <= n_rows &&
c >= 0 && c + m.cols() <= n_cols, "Sparse_Mat<Num_T>::set_submatrix(): index out of range");
for (int i = 0 ; i < m.rows() ; i++) {
for (int j = 0 ; j < m.cols() ; j++) {
set(r + i, c + j, m(i, j));
}
}
}
template <class T>
Sparse_Mat<T> Sparse_Mat<T>::get_submatrix(int r1, int r2, int c1, int c2) const
{
it_assert_debug(r1 <= r2 && r1 >= 0 && r1 < n_rows && c1 <= c2 && c1 >= 0 && c1 < n_cols,
"Sparse_Mat<T>::get_submatrix(): illegal input variables");
Sparse_Mat<T> 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 <class T>
Sparse_Mat<T> Sparse_Mat<T>::get_submatrix_cols(int c1, int c2) const
{
it_assert_debug(c1 <= c2 && c1 >= 0 && c1 < n_cols, "Sparse_Mat<T>::get_submatrix_cols()");
Sparse_Mat<T> 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 <class T>
void Sparse_Mat<T>::get_col(int c, Sparse_Vec<T> &v) const
{
it_assert(c >= 0 && c < n_cols, "Sparse_Mat<T>::get_col()");
v = col[c];
}
template <class T>
Sparse_Vec<T> Sparse_Mat<T>::get_col(int c) const
{
it_assert(c >= 0 && c < n_cols, "Sparse_Mat<T>::get_col()");
return col[c];
}
template <class T>
void Sparse_Mat<T>::set_col(int c, const Sparse_Vec<T> &v)
{
it_assert(c >= 0 && c < n_cols, "Sparse_Mat<T>::set_col()");
col[c] = v;
}
template <class T>
void Sparse_Mat<T>::transpose(Sparse_Mat<T> &m) const
{
m.set_size(n_cols, n_rows);
for (int c = 0; c < n_cols; c++) {
for (int p = 0; p < col[c].nnz(); p++)
m.col[col[c].get_nz_index(p)].set_new(c, col[c].get_nz_data(p));
}
}
template <class T>
Sparse_Mat<T> Sparse_Mat<T>::transpose() const
{
Sparse_Mat<T> m;
transpose(m);
return m;
}
template <class T>
void Sparse_Mat<T>::operator=(const Sparse_Mat<T> &m)
{
free();
n_rows = m.n_rows;
n_cols = m.n_cols;
alloc_empty();
for (int c = 0; c < n_cols; c++)
col[c] = m.col[c];
}
template <class T>
void Sparse_Mat<T>::operator=(const Mat<T> &m)
{
free();
n_rows = m.rows();
n_cols = m.cols();
alloc();
for (int c = 0; c < n_cols; c++) {
for (int r = 0; r < n_rows; r++) {
if (m(r, c) != T(0))
col[c].set_new(r, m(r, c));
}
col[c].compact();
}
}
template <class T>
Sparse_Mat<T> Sparse_Mat<T>::operator-() const
{
Sparse_Mat r(n_rows, n_cols, 0);
for (int c = 0; c < n_cols; c++) {
r.col[c].resize_data(col[c].nnz());
for (int p = 0; p < col[c].nnz(); p++)
r.col[c].set_new(col[c].get_nz_index(p), -col[c].get_nz_data(p));
}
return r;
}
template <class T>
bool Sparse_Mat<T>::operator==(const Sparse_Mat<T> &m) const
{
if (n_rows != m.n_rows || n_cols != m.n_cols)
return false;
for (int c = 0; c < n_cols; c++) {
if (!(col[c] == m.col[c]))
return false;
}
// If they passed all tests, they must be equal
return true;
}
template <class T>
void Sparse_Mat<T>::operator+=(const Sparse_Mat<T> &m)
{
it_assert_debug(m.rows() == n_rows && m.cols() == n_cols, "Addition of unequal sized matrices is not allowed");
Sparse_Vec<T> v;
for (int c = 0; c < n_cols; c++) {
m.get_col(c, v);
col[c] += v;
}
}
template <class T>
void Sparse_Mat<T>::operator+=(const Mat<T> &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 < n_cols; c++)
col[c] += (m.get_col(c));
}
template <class T>
void Sparse_Mat<T>::operator-=(const Sparse_Mat<T> &m)
{
it_assert_debug(m.rows() == n_rows && m.cols() == n_cols, "Subtraction of unequal sized matrices is not allowed");
Sparse_Vec<T> v;
for (int c = 0; c < n_cols; c++) {
m.get_col(c, v);
col[c] -= v;
}
}
template <class T>
void Sparse_Mat<T>::operator-=(const Mat<T> &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 < n_cols; c++)
col[c] -= (m.get_col(c));
}
template <class T>
void Sparse_Mat<T>::operator*=(const T &m)
{
for (int c = 0; c < n_cols; c++)
col[c] *= m;
}
template <class T>
void Sparse_Mat<T>::operator/=(const T &m)
{
for (int c = 0; c < n_cols; c++)
col[c] /= m;
}
template <class T>
Sparse_Mat<T> operator+(const Sparse_Mat<T> &m1, const Sparse_Mat<T> &m2)
{
it_assert_debug(m1.n_cols == m2.n_cols && m1.n_rows == m2.n_rows , "Sparse_Mat<T> + Sparse_Mat<T>");
Sparse_Mat<T> m(m1.n_rows, m1.n_cols, 0);
for (int c = 0; c < m.n_cols; c++)
m.col[c] = m1.col[c] + m2.col[c];
return m;
}
// This function added by EGL, May'05
template <class T>
Sparse_Mat<T> operator*(const T &c, const Sparse_Mat<T> &m)
{
int i, j;
Sparse_Mat<T> ret(m.n_rows, m.n_cols);
for (j = 0; j < m.n_cols; j++) {
for (i = 0; i < m.col[j].nnz(); i++) {
T x = c * m.col[j].get_nz_data(i);
int k = m.col[j].get_nz_index(i);
ret.set_new(k, j, x);
}
}
return ret;
}
template <class T>
Sparse_Mat<T> operator*(const Sparse_Mat<T> &m1, const Sparse_Mat<T> &m2)
{
it_assert_debug(m1.n_cols == m2.n_rows, "Sparse_Mat<T> * Sparse_Mat<T>");
Sparse_Mat<T> ret(m1.n_rows, m2.n_cols);
for (int c = 0; c < m2.n_cols; c++) {
Sparse_Vec<T> &m2colc = m2.col[c];
for (int p2 = 0; p2 < m2colc.nnz(); p2++) {
Sparse_Vec<T> &mcol = m1.col[m2colc.get_nz_index(p2)];
T x = m2colc.get_nz_data(p2);
for (int p1 = 0; p1 < mcol.nnz(); p1++) {
int r = mcol.get_nz_index(p1);
T inc = mcol.get_nz_data(p1) * x;
ret.col[c].add_elem(r, inc);
}
}
}
// old code
/* for (int c=0; c<m2.n_cols; c++) { */
/* for (int p2=0; p2<m2.col[c].nnz(); p2++) { */
/* Sparse_Vec<T> &mcol = m1.col[m2.col[c].get_nz_index(p2)]; */
/* for (int p1=0; p1<mcol.nnz(); p1++) { */
/* int r = mcol.get_nz_index(p1); */
/* T inc = mcol.get_nz_data(p1) * m2.col[c].get_nz_data(p2); */
/* ret.col[c].add_elem(r,inc); */
/* } */
/* } */
/* } */
ret.compact();
return ret;
}
// This is apparently buggy.
/* template <class T> */
/* Sparse_Mat<T> operator*(const Sparse_Mat<T> &m1, const Sparse_Mat<T> &m2) */
/* { */
/* it_assert_debug(m1.n_cols == m2.n_rows, "Sparse_Mat<T> * Sparse_Mat<T>"); */
/* Sparse_Mat<T> ret(m1.n_rows, m2.n_cols); */
/* ivec occupied_by(ret.n_rows), pos(ret.n_rows); */
/* for (int rp=0; rp<m1.n_rows; rp++) */
/* occupied_by[rp] = -1; */
/* for (int c=0; c<ret.n_cols; c++) { */
/* Sparse_Vec<T> &m2col = m2.col[c]; */
/* for (int p2=0; p2<m2col.nnz(); p2++) { */
/* Sparse_Vec<T> &m1col = m1.col[m2col.get_nz_index(p2)]; */
/* for (int p1=0; p1<m1col.nnz(); p1++) { */
/* int r = m1col.get_nz_index(p1); */
/* T inc = m1col.get_nz_data(p1) * m2col.get_nz_data(p2); */
/* if (occupied_by[r] == c) { */
/* int index=ret.col[c].get_nz_index(pos[r]); */
/* ret.col[c].add_elem(index,inc); */
/* } */
/* else { */
/* occupied_by[r] = c; */
/* pos[r] = ret.col[c].nnz(); */
/* ret.col[c].set_new(r, inc); */
/* } */
/* } */
/* } */
/* } */
/* ret.compact(); */
/* return ret; */
/* } */
// This function added by EGL, May'05
template <class T>
Sparse_Vec<T> operator*(const Sparse_Mat<T> &m, const Sparse_Vec<T> &v)
{
it_assert_debug(m.n_cols == v.size(), "Sparse_Mat<T> * Sparse_Vec<T>");
Sparse_Vec<T> 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<T> vv = v;
for (int p2 = 0; p2 < vv.nnz(); p2++) {
Sparse_Vec<T> &mcol = m.col[vv.get_nz_index(p2)];
T x = vv.get_nz_data(p2);
for (int p1 = 0; p1 < mcol.nnz(); p1++) {
int r = mcol.get_nz_index(p1);
T inc = mcol.get_nz_data(p1) * x;
ret.add_elem(r, inc);
}
}
ret.compact();
return ret;
}
template <class T>
Vec<T> operator*(const Sparse_Mat<T> &m, const Vec<T> &v)
{
it_assert_debug(m.n_cols == v.size(), "Sparse_Mat<T> * Vec<T>");
Vec<T> r(m.n_rows);
r.clear();
for (int c = 0; c < m.n_cols; c++) {
for (int p = 0; p < m.col[c].nnz(); p++)
r(m.col[c].get_nz_index(p)) += m.col[c].get_nz_data(p) * v(c);
}
return r;
}
template <class T>
Vec<T> operator*(const Vec<T> &v, const Sparse_Mat<T> &m)
{
it_assert_debug(v.size() == m.n_rows, "Vec<T> * Sparse_Mat<T>");
Vec<T> r(m.n_cols);
r.clear();
for (int c = 0; c < m.n_cols; c++)
r[c] = v * m.col[c];
return r;
}
template <class T>
Mat<T> trans_mult(const Sparse_Mat<T> &m)
{
Mat<T> ret(m.n_cols, m.n_cols);
Vec<T> col;
for (int c = 0; c < ret.cols(); c++) {
m.col[c].full(col);
for (int r = 0; r < c; r++) {
T tmp = m.col[r] * col;
ret(r, c) = tmp;
ret(c, r) = tmp;
}
ret(c, c) = m.col[c].sqr();
}
return ret;
}
template <class T>
Sparse_Mat<T> trans_mult_s(const Sparse_Mat<T> &m)
{
Sparse_Mat<T> ret(m.n_cols, m.n_cols);
Vec<T> col;
T tmp;
for (int c = 0; c < ret.n_cols; c++) {
m.col[c].full(col);
for (int r = 0; r < c; r++) {
tmp = m.col[r] * col;
if (tmp != T(0)) {
ret.col[c].set_new(r, tmp);
ret.col[r].set_new(c, tmp);
}
}
tmp = m.col[c].sqr();
if (tmp != T(0))
ret.col[c].set_new(c, tmp);
}
return ret;
}
template <class T>
Sparse_Mat<T> trans_mult(const Sparse_Mat<T> &m1, const Sparse_Mat<T> &m2)
{
it_assert_debug(m1.n_rows == m2.n_rows, "trans_mult()");
Sparse_Mat<T> ret(m1.n_cols, m2.n_cols);
Vec<T> col;
for (int c = 0; c < ret.n_cols; c++) {
m2.col[c].full(col);
for (int r = 0; r < ret.n_rows; r++)
ret.col[c].set_new(r, m1.col[r] * col);
}
return ret;
}
template <class T>
Vec<T> trans_mult(const Sparse_Mat<T> &m, const Vec<T> &v)
{
Vec<T> r(m.n_cols);
for (int c = 0; c < m.n_cols; c++)
r(c) = m.col[c] * v;
return r;
}
template <class T>
Sparse_Mat<T> mult_trans(const Sparse_Mat<T> &m1, const Sparse_Mat<T> &m2)
{
return trans_mult(m1.transpose(), m2.transpose());
}
template <class T>
inline Sparse_Mat<T> sparse(const Mat<T> &m, T epsilon)
{
Sparse_Mat<T> s(m, epsilon);
return s;
}
template <class T>
inline Mat<T> full(const Sparse_Mat<T> &s)
{
Mat<T> m;
s.full(m);
return m;
}
template <class T>
inline Sparse_Mat<T> transpose(const Sparse_Mat<T> &s)
{
Sparse_Mat<T> m;
s.transpose(m);
return m;
}
// ---------------------------------------------------------------------
// Instantiations
// ---------------------------------------------------------------------
ITPP_EXPORT_TEMPLATE template class ITPP_EXPORT Sparse_Mat<int>;
ITPP_EXPORT_TEMPLATE template class ITPP_EXPORT Sparse_Mat<double>;
ITPP_EXPORT_TEMPLATE template class ITPP_EXPORT Sparse_Mat<std::complex<double> >;
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT sparse_imat operator+(const sparse_imat &, const sparse_imat &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT sparse_mat operator+(const sparse_mat &, const sparse_mat &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT sparse_cmat operator+(const sparse_cmat &, const sparse_cmat &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT sparse_imat operator*(const sparse_imat &, const sparse_imat &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT sparse_mat operator*(const sparse_mat &, const sparse_mat &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT sparse_cmat operator*(const sparse_cmat &, const sparse_cmat &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec operator*(const ivec &, const sparse_imat &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec operator*(const vec &, const sparse_mat &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec operator*(const cvec &, const sparse_cmat &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec operator*(const sparse_imat &, const ivec &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec operator*(const sparse_mat &, const vec &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec operator*(const sparse_cmat &, const cvec &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat trans_mult(const sparse_imat &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat trans_mult(const sparse_mat &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat trans_mult(const sparse_cmat &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT sparse_imat trans_mult_s(const sparse_imat &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT sparse_mat trans_mult_s(const sparse_mat &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT sparse_cmat trans_mult_s(const sparse_cmat &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT sparse_imat trans_mult(const sparse_imat &, const sparse_imat &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT sparse_mat trans_mult(const sparse_mat &, const sparse_mat &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT sparse_cmat trans_mult(const sparse_cmat &, const sparse_cmat &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec trans_mult(const sparse_imat &, const ivec &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec trans_mult(const sparse_mat &, const vec &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec trans_mult(const sparse_cmat &, const cvec &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT sparse_imat mult_trans(const sparse_imat &, const sparse_imat &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT sparse_mat mult_trans(const sparse_mat &, const sparse_mat &);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT sparse_cmat mult_trans(const sparse_cmat &, const sparse_cmat &);
} // namespace itpp
#endif // #ifndef SMAT_H
itpp::Mat::set_size
void set_size(int rows, int cols, bool copy=false)
Set size of matrix. If copy = true then keep the data before resizing.
Definition: mat.h:647
itpp::Mat::cols
int cols() const
The number of columns.
Definition: mat.h:235
itpp::Sparse_Mat::zero_elem
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...
Definition: smat.h:526
itpp::Sparse_Mat::set_col
void set_col(int c, const Sparse_Vec< T > &v)
Set column c of the Sparse_Mat.
Definition: smat.h:623
itpp::Mat::rows
int rows() const
The number of rows.
Definition: mat.h:237
itpp::Sparse_Vec::get_nz_data
void get_nz_data(int p, T &data_out)
Extract the reference to the p-th non-zero data element.
Definition: svec.h:230
itpp::Sparse_Mat::operator==
bool operator==(const Sparse_Mat< T > &m) const
Compare two sparse matricies. False if wrong sizes or different values.
Definition: smat.h:691
itpp::Sparse_Vec::size
int size() const
Returns the size of the sparse vector.
Definition: svec.h:155
itpp::Mat< T >
itpp::Sparse_Mat::get_submatrix
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: smat.h:581
itpp::mult_trans
GF2mat mult_trans(const GF2mat &X, const GF2mat &Y)
Multiplication X*Y' where X and Y are GF(2) matrices.
Definition: gf2mat.cpp:903
itpp::Sparse_Mat::sparse_imat
Sparse_Mat< int > sparse_imat
Sparse integer matrix.
Definition: smat.h:315
itpp::Sparse_Mat::set
void set(int r, int c, T v)
Set element (r, c ) equal to v.
Definition: smat.h:498
itpp::trans_mult
Mat< T > trans_mult(const Sparse_Mat< T > &m)
m'*m where m is a sparse matrix
Definition: smat.h:916
itpp::Sparse_Mat::full
Mat< T > full() const
Returns a full, dense matrix.
Definition: smat.h:483
itpp::Sparse_Mat::get_submatrix_cols
Sparse_Mat< T > get_submatrix_cols(int c1, int c2) const
Returns the sub-matrix from columns c1 to c2 (all rows)
Definition: smat.h:596
itpp::Sparse_Mat::~Sparse_Mat
~Sparse_Mat()
Destructor.
Definition: smat.h:429
it_assert
#define it_assert(t, s)
Abort if t is not true.
Definition: itassert.h:94
it_assert_debug
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
Definition: itassert.h:107
itpp::Sparse_Vec
Templated sparse vector class.
Definition: smat.h:43
itpp::Sparse_Mat::cols
int cols() const
Returns the number of columns of the sparse matrix.
Definition: smat.h:158
itpp::zeros
ITPP_EXPORT vec zeros(int size)
A Double vector of zeros.
itpp::Sparse_Mat::density
double density()
Returns the density of the sparse matrix: (number of non-zero elements)/(total number of elements) ...
Definition: smat.h:458
itpp::Sparse_Mat::sparse_mat
Sparse_Mat< double > sparse_mat
Sparse double matrix.
Definition: smat.h:321
itpp::Sparse_Mat::transpose
Sparse_Mat< T > transpose() const
Definition: smat.h:640
itpp::Sparse_Vec::get_nz_index
int get_nz_index(int p)
Returns the vector index of the p-th non-zero element.
Definition: svec.h:246
itpp::Sparse_Mat::full
void full(Mat< T > &m) const
Returns a full, dense matrix in m.
Definition: smat.h:472
itpp::Sparse_Mat::operator()
T operator()(int r, int c) const
Returns element of row r and column c.
Definition: smat.h:491
itpp::operator+
GF2mat operator+(const GF2mat &X, const GF2mat &Y)
GF(2) matrix addition.
Definition: gf2mat.cpp:948
itpp::Sparse_Mat::set_new
void set_new(int r, int c, T v)
Set a new element with index (r, c ) equal to v.
Definition: smat.h:505
itpp::trans_mult_s
Sparse_Mat< T > trans_mult_s(const Sparse_Mat< T > &m)
m'*m where m is a sparse matrix
Definition: smat.h:934
itpp::full
Mat< T > full(const Sparse_Mat< T > &s)
Convert a sparse matrix s into its dense representation.
Definition: smat.h:998
itpp::Sparse_Mat::add_elem
void add_elem(const int r, const int c, const T v)
Add the element in row r and column c with v.
Definition: smat.h:512
itpp::Sparse_Mat::get_col
void get_col(int c, Sparse_Vec< T > &v) const
Returns column c of the Sparse_Mat in the Sparse_Vec v.
Definition: smat.h:609
itpp::operator*
GF2mat operator*(const GF2mat &X, const GF2mat &Y)
GF(2) matrix multiplication.
Definition: gf2mat.cpp:847
itpp::Sparse_Vec::nnz
int nnz()
Number of non-zero elements in the sparse vector.
Definition: svec.h:158
itpp::Sparse_Mat::set_submatrix
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.
Definition: smat.h:547
itpp::Vec::size
int size() const
The size of the vector.
Definition: vec.h:271
itpp::Sparse_Mat::nnz
int nnz()
The number of non-zero elements in the sparse matrix.
Definition: smat.h:448
itpp::Sparse_Mat::clear
void clear()
Clear all non-zero elements of the sparse matrix.
Definition: smat.h:533
itpp::Sparse_Mat::operator-=
void operator-=(const Sparse_Mat< T > &v)
Subtract sparse matrix v from all non-zero elements of the sparse matrix.
Definition: smat.h:725
itpp::Sparse_Mat::operator=
void operator=(const Sparse_Mat< T > &m)
Assign sparse matrix the value and dimensions of the sparse matrix m.
Definition: smat.h:648
itpp::Sparse_Mat::compact
void compact()
Set the maximum number of non-zero elements in each column equal to the actual number of non-zero ele...
Definition: smat.h:465
itpp::transpose
void transpose(const Mat< T > &m, Mat< T > &out)
Transposition of the matrix m returning the transposed matrix in out.
Definition: matfunc.h:308
itpp::sparse
Sparse_Mat< T > sparse(const Mat< T > &m, T epsilon)
Convert a dense matrix m into its sparse representation.
Definition: smat.h:990
itpp::Sparse_Mat::operator+=
void operator+=(const Sparse_Mat< T > &v)
Add sparse matrix v to all non-zero elements of the sparse matrix.
Definition: smat.h:704
itpp::Vec< T >
itpp::Sparse_Mat::rows
int rows() const
Returns the number of rows of the sparse matrix.
Definition: smat.h:155
itpp::Sparse_Mat::sparse_cmat
Sparse_Mat< std::complex< double > > sparse_cmat
Sparse complex<double> matrix.
Definition: smat.h:327
itpp::abs
bin abs(const bin &inbin)
absolute value of bin
Definition: binary.h:174
itpp::Sparse_Mat::set_size
void set_size(int rows, int cols, int row_data_init=-1)
Set the size of the sparse matrix.
Definition: smat.h:435
itpp::Mat::get_col
Vec< Num_T > get_col(int c) const
Get column c.
Definition: mat.h:889
itpp::Sparse_Mat::Sparse_Mat
Sparse_Mat()
Default constructor.
Definition: smat.h:367
itpp::Sparse_Mat::operator-
Sparse_Mat< T > operator-() const
Returns the sign inverse of all elements in the sparse matrix.
Definition: smat.h:677
svec.h
Sparse Vector Class definitions.
itpp::Sparse_Mat::zeros
void zeros()
Set the sparse matrix to the all zero matrix (removes all non-zero elements)
Definition: smat.h:519
itpp::Sparse_Mat::operator/=
void operator/=(const T &v)
Divide all non-zero elements of the sparse matrix with the scalar v.
Definition: smat.h:753
itpp::Sparse_Mat::clear_elem
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)
Definition: smat.h:540
itpp::Sparse_Mat::transpose
void transpose(Sparse_Mat< T > &m) const
Definition: smat.h:630
itpp::Sparse_Mat
Templated Sparse Matrix Class.
Definition: smat.h:45
itpp::Sparse_Mat::operator*=
void operator*=(const T &v)
Multiply all non-zero elements of the sparse matrix with the scalar v.
Definition: smat.h:746

Generated on Thu Mar 20 2014 12:26:42 for IT++ by Doxygen 1.8.6