OpenMEEG
sparse_matrix.h
Go to the documentation of this file.
1 /*
2 Project Name : OpenMEEG
3 
4 © INRIA and ENPC (contributors: Geoffray ADDE, Maureen CLERC, Alexandre
5 GRAMFORT, Renaud KERIVEN, Jan KYBIC, Perrine LANDREAU, Théodore PAPADOPOULO,
6 Emmanuel OLIVI
7 Maureen.Clerc.AT.inria.fr, keriven.AT.certis.enpc.fr,
8 kybic.AT.fel.cvut.cz, papadop.AT.inria.fr)
9 
10 The OpenMEEG software is a C++ package for solving the forward/inverse
11 problems of electroencephalography and magnetoencephalography.
12 
13 This software is governed by the CeCILL-B license under French law and
14 abiding by the rules of distribution of free software. You can use,
15 modify and/ or redistribute the software under the terms of the CeCILL-B
16 license as circulated by CEA, CNRS and INRIA at the following URL
17 "http://www.cecill.info".
18 
19 As a counterpart to the access to the source code and rights to copy,
20 modify and redistribute granted by the license, users are provided only
21 with a limited warranty and the software's authors, the holders of the
22 economic rights, and the successive licensors have only limited
23 liability.
24 
25 In this respect, the user's attention is drawn to the risks associated
26 with loading, using, modifying and/or developing or reproducing the
27 software by the user in light of its specific status of free software,
28 that may mean that it is complicated to manipulate, and that also
29 therefore means that it is reserved for developers and experienced
30 professionals having in-depth computer knowledge. Users are therefore
31 encouraged to load and test the software's suitability as regards their
32 requirements in conditions enabling the security of their systems and/or
33 data to be ensured and, more generally, to use and operate it in the
34 same conditions as regards security.
35 
36 The fact that you are presently reading this means that you have had
37 knowledge of the CeCILL-B license and that you accept its terms.
38 */
39 
40 #pragma once
41 
42 #include <OMassert.H>
43 #include <map>
44 #include <utility>
45 
46 #include <linop.h>
47 #include <vector.h>
48 #include <matrix.h>
49 
50 // #ifdef WIN32
51 // #pragma warning( disable : 4251) //MSVC warning C4251 : DLL exports of STL templates
52 // #endif
53 
54 #ifdef WIN32
55  template class OPENMEEGMATHS_EXPORT std::map< std::pair< size_t, size_t >, double >;
56 #endif
57 
58 namespace OpenMEEG {
59 
60  class SymMatrix;
61 
62  class OPENMEEGMATHS_EXPORT SparseMatrix : public LinOp {
63 
64  public:
65 
66  typedef std::map< std::pair< size_t, size_t >, double > Tank;
67  typedef std::map< std::pair< size_t, size_t >, double >::const_iterator const_iterator;
68  typedef std::map< std::pair< size_t, size_t >, double >::iterator iterator;
69 
70  SparseMatrix() : LinOp(0,0,SPARSE,2) {};
71  SparseMatrix(const char* fname) : LinOp(0,0,SPARSE,2) { this->load(fname); }
72  SparseMatrix(size_t N,size_t M) : LinOp(N,M,SPARSE,2) {};
74 
75  inline double operator()( size_t i, size_t j ) const {
76  om_assert(i < nlin());
77  om_assert(j < ncol());
78  const_iterator it = m_tank.find(std::make_pair(i, j));
79  if (it != m_tank.end()) return it->second;
80  else return 0.0;
81  }
82 
83  inline double& operator()( size_t i, size_t j ) {
84  om_assert(i < nlin());
85  om_assert(j < ncol());
86  return m_tank[ std::make_pair( i, j ) ];
87  }
88 
89  size_t size() const {
90  return m_tank.size();
91  }
92 
93  const_iterator begin() const { return m_tank.begin(); }
94  const_iterator end() const { return m_tank.end(); }
95 
97 
98  const Tank& tank() const {return m_tank;}
99 
100  void set( double t);
101  Vector getlin(size_t i) const;
102  void setlin(Vector v, size_t i);
103 
104  void save(const char *filename) const;
105  void load(const char *filename);
106 
107  void save(const std::string& s) const { save(s.c_str()); }
108  void load(const std::string& s) { load(s.c_str()); }
109 
110  void info() const;
111  double frobenius_norm() const;
112 
113  Vector operator*( const Vector &x ) const;
114  Matrix operator*( const SymMatrix &m ) const;
115  Matrix operator*( const Matrix &m ) const;
118 
119  private:
120 
122  };
123 
124  inline Vector SparseMatrix::getlin(size_t i) const {
125  om_assert(i<nlin());
126  Vector v(ncol());
127  for (size_t j=0;j<ncol();j++){
128  const_iterator it = m_tank.find(std::make_pair(i, j));
129  if (it != m_tank.end()) v(j)=it->second;
130  else v(j)=0.0;
131  }
132  return v;
133  }
134 
135  inline void SparseMatrix::setlin(Vector v, size_t i) {
136  om_assert(i<nlin());
137  for (size_t j=0;j<v.nlin();j++){
138  (*this)(i,j) = v(j);
139  }
140  }
141 }
OpenMEEG::SparseMatrix::operator()
double operator()(size_t i, size_t j) const
Definition: sparse_matrix.h:75
OpenMEEG::SparseMatrix::getlin
Vector getlin(size_t i) const
Definition: sparse_matrix.h:124
OpenMEEG::SparseMatrix::SparseMatrix
SparseMatrix()
Definition: sparse_matrix.h:70
linop.h
OpenMEEG::SparseMatrix::SparseMatrix
SparseMatrix(size_t N, size_t M)
Definition: sparse_matrix.h:72
OpenMEEG::SparseMatrix::transpose
SparseMatrix transpose() const
OpenMEEG::LinOp
Definition: linop.h:100
OpenMEEG::SparseMatrix::size
size_t size() const
Definition: sparse_matrix.h:89
OpenMEEG::SparseMatrix::set
void set(double t)
OpenMEEG::SparseMatrix::load
void load(const char *filename)
OpenMEEG::LinOpInfo::nlin
size_t nlin() const
Definition: linop.h:77
OpenMEEG
Definition: analytics.h:45
matrix.h
OpenMEEG::SparseMatrix::operator*
Matrix operator*(const SymMatrix &m) const
OpenMEEG::SparseMatrix
Definition: sparse_matrix.h:62
OpenMEEG::SparseMatrix::~SparseMatrix
~SparseMatrix()
Definition: sparse_matrix.h:73
OpenMEEG::SparseMatrix::Tank
std::map< std::pair< size_t, size_t >, double > Tank
Definition: sparse_matrix.h:66
OpenMEEG::SparseMatrix::operator*
Matrix operator*(const Matrix &m) const
OpenMEEG::SparseMatrix::save
void save(const char *filename) const
OpenMEEG::SparseMatrix::const_iterator
std::map< std::pair< size_t, size_t >, double >::const_iterator const_iterator
Definition: sparse_matrix.h:67
OpenMEEG::SparseMatrix::setlin
void setlin(Vector v, size_t i)
Definition: sparse_matrix.h:135
OpenMEEG::SparseMatrix::SparseMatrix
SparseMatrix(const char *fname)
Definition: sparse_matrix.h:71
OpenMEEG::SparseMatrix::operator()
double & operator()(size_t i, size_t j)
Definition: sparse_matrix.h:83
OpenMEEG::SparseMatrix::m_tank
Tank m_tank
Definition: sparse_matrix.h:121
OpenMEEG::SparseMatrix::operator*
Vector operator*(const Vector &x) const
OpenMEEG::Vector
Definition: vector.h:56
OpenMEEG::LinOpInfo::ncol
virtual size_t ncol() const
Definition: linop.h:80
OpenMEEG::Matrix
Matrix class.
Definition: matrix.h:61
OpenMEEG::SparseMatrix::iterator
std::map< std::pair< size_t, size_t >, double >::iterator iterator
Definition: sparse_matrix.h:68
OpenMEEG::SparseMatrix::end
const_iterator end() const
Definition: sparse_matrix.h:94
vector.h
OpenMEEG::SparseMatrix::load
void load(const std::string &s)
Definition: sparse_matrix.h:108
OpenMEEG::SparseMatrix::begin
const_iterator begin() const
Definition: sparse_matrix.h:93
OpenMEEG::SparseMatrix::info
void info() const
OpenMEEG::SparseMatrix::tank
const Tank & tank() const
Definition: sparse_matrix.h:98
OpenMEEG::SparseMatrix::operator*
SparseMatrix operator*(const SparseMatrix &m) const
OpenMEEG::SparseMatrix::frobenius_norm
double frobenius_norm() const
OpenMEEG::SymMatrix
Definition: symmatrix.h:53
OpenMEEG::SparseMatrix::operator+
SparseMatrix operator+(const SparseMatrix &m) const
OpenMEEG::SparseMatrix::save
void save(const std::string &s) const
Definition: sparse_matrix.h:107