OpenMEEG
Loading...
Searching...
No Matches
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
22namespace 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)
SparseMatrix(const char *fname)
SparseMatrix transpose() const
double operator()(const size_t i, const size_t j) const
const Tank & tank() const
Tank::const_iterator const_iterator
Vector operator*(const Vector &x) const
SparseMatrix(const size_t N, const size_t M)
void save(const char *filename) const
const_iterator end() const
Tank::iterator iterator
void save(const std::string &s) const
const_iterator begin() const
SparseMatrix operator+(const SparseMatrix &m) const
SparseMatrix operator*(const SparseMatrix &m) const
void load(const std::string &s)
std::map< std::pair< size_t, size_t >, double > Tank
void load(const char *filename)
Vector getlin(const size_t i) const
void setlin(const Vector &v, const size_t i)
double frobenius_norm() const
double & operator()(size_t i, size_t j)
Matrix operator*(const Matrix &m) const
Matrix operator*(const SymMatrix &m) const