OpenMEEG
sparse_matrix.h
Go to the documentation of this file.
1 // Project Name: OpenMEEG (http://openmeeg.github.io)
2 // © INRIA and ENPC under the French open source license CeCILL-B.
3 // See full copyright notice in the file LICENSE.txt
4 // If you make a copy of this file, you must either:
5 // - provide also LICENSE.txt and modify this header to refer to it.
6 // - replace this header by the LICENSE.txt content.
7 
8 #pragma once
9 
10 #include <OMassert.H>
11 #include <map>
12 #include <utility>
13 
14 #include <linop.h>
15 #include <vector.h>
16 #include <matrix.h>
17 
18 #ifdef WIN32
19  template class OPENMEEGMATHS_EXPORT std::map<std::pair<size_t,size_t>,double>;
20 #endif
21 
22 namespace OpenMEEG {
23 
24  class SymMatrix;
25 
26  class OPENMEEGMATHS_EXPORT SparseMatrix : public LinOp {
27  public:
28 
29  typedef std::map<std::pair<size_t,size_t>,double> Tank;
30  typedef Tank::const_iterator const_iterator;
31  typedef Tank::iterator iterator;
32 
33  SparseMatrix(): LinOp(0,0,SPARSE,2) {};
34  SparseMatrix(const char* fname): LinOp(0,0,SPARSE,2) { this->load(fname); }
35  SparseMatrix(const size_t N,const size_t M): LinOp(N,M,SPARSE,2) {};
37 
38  inline double operator()(const size_t i,const size_t j) const {
39  om_assert(i<nlin());
40  om_assert(j<ncol());
41  const_iterator it = m_tank.find(std::make_pair(i,j));
42  return (it!=m_tank.end()) ? it->second : 0.0;
43  }
44 
45  inline double& operator()( size_t i, size_t j ) {
46  om_assert(i<nlin());
47  om_assert(j<ncol());
48  return m_tank[std::make_pair(i,j)];
49  }
50 
51  size_t size() const {
52  return m_tank.size();
53  }
54 
55  const_iterator begin() const { return m_tank.begin(); }
56  const_iterator end() const { return m_tank.end(); }
57 
59 
60  const Tank& tank() const { return m_tank; }
61 
62  void set(const double d) {
63  for(auto& tkelmt : m_tank)
64  tkelmt.second = d;
65  }
66 
67  Vector getlin(const size_t i) const {
68  om_assert(i<nlin());
69  Vector v(ncol());
70  for (size_t j=0; j<ncol(); ++j) {
71  const_iterator it = m_tank.find(std::make_pair(i,j));
72  v(j) = (it!=m_tank.end()) ? it->second : 0.0;
73  }
74  return v;
75  }
76 
77  void setlin(const Vector& v,const size_t i) {
78  om_assert(i<nlin());
79  for (size_t j=0; j<v.nlin(); ++j)
80  (*this)(i,j) = v(j);
81  }
82 
83  void save(const char* filename) const {
84  maths::ofstream ofs(filename);
85  try {
86  ofs << maths::format(filename,maths::format::FromSuffix) << *this;
87  } catch (maths::Exception&) {
88  ofs << *this;
89  }
90  }
91 
92  void load(const char* filename) {
93  maths::ifstream ifs(filename);
94  try {
95  ifs >> maths::format(filename,maths::format::FromSuffix) >> *this;
96  } catch (maths::Exception&) {
97  ifs >> *this;
98  }
99  }
100 
101  void save(const std::string& s) const { save(s.c_str()); }
102  void load(const std::string& s) { load(s.c_str()); }
103 
104  void info() const;
105  double frobenius_norm() const;
106 
107  Vector operator*(const Vector& x) const;
108  Matrix operator*(const SymMatrix& m) const;
109  Matrix operator*(const Matrix& m) const;
112 
113  private:
114 
115  Tank m_tank;
116  };
117 }
Dimension nlin() const
Definition: linop.h:48
Matrix class Matrix class.
Definition: matrix.h:28
void set(const double d)
Definition: sparse_matrix.h:62
SparseMatrix(const char *fname)
Definition: sparse_matrix.h:34
SparseMatrix transpose() const
double operator()(const size_t i, const size_t j) const
Definition: sparse_matrix.h:38
Tank::iterator iterator
Definition: sparse_matrix.h:31
Vector operator*(const Vector &x) const
SparseMatrix(const size_t N, const size_t M)
Definition: sparse_matrix.h:35
void save(const char *filename) const
Definition: sparse_matrix.h:83
const_iterator end() const
Definition: sparse_matrix.h:56
double & operator()(size_t i, size_t j)
Definition: sparse_matrix.h:45
size_t size() const
Definition: sparse_matrix.h:51
void save(const std::string &s) const
const_iterator begin() const
Definition: sparse_matrix.h:55
SparseMatrix operator+(const SparseMatrix &m) const
const Tank & tank() const
Definition: sparse_matrix.h:60
SparseMatrix operator*(const SparseMatrix &m) const
void load(const std::string &s)
void load(const char *filename)
Definition: sparse_matrix.h:92
std::map< std::pair< size_t, size_t >, double > Tank
Definition: sparse_matrix.h:29
Vector getlin(const size_t i) const
Definition: sparse_matrix.h:67
Tank::const_iterator const_iterator
Definition: sparse_matrix.h:30
void setlin(const Vector &v, const size_t i)
Definition: sparse_matrix.h:77
double frobenius_norm() const
Matrix operator*(const Matrix &m) const
Matrix operator*(const SymMatrix &m) const