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 <om_utils.h>
47 #include <linop.h>
48 #include <vector.h>
49 #include <matrix.h>
50 
51 // #ifdef WIN32
52 // #pragma warning( disable : 4251) //MSVC warning C4251 : DLL exports of STL templates
53 // #endif
54 
55 #ifdef WIN32
56  template class OPENMEEGMATHS_EXPORT std::map< std::pair< size_t, size_t >, double >;
57 #endif
58 
59 namespace OpenMEEG {
60 
61  class SymMatrix;
62 
63  class OPENMEEGMATHS_EXPORT SparseMatrix : public LinOp {
64 
65  public:
66 
67  typedef std::map< std::pair< size_t, size_t >, double > Tank;
68  typedef std::map< std::pair< size_t, size_t >, double >::const_iterator const_iterator;
69  typedef std::map< std::pair< size_t, size_t >, double >::iterator iterator;
70 
71  SparseMatrix() : LinOp(0,0,SPARSE,2) {};
72  SparseMatrix(const char* fname) : LinOp(0,0,SPARSE,2) { this->load(fname); }
73  SparseMatrix(size_t N,size_t M) : LinOp(N,M,SPARSE,2) {};
75 
76  inline double operator()( size_t i, size_t j ) const {
77  om_assert(i < nlin());
78  om_assert(j < ncol());
79  const_iterator it = m_tank.find(std::make_pair(i, j));
80  if (it != m_tank.end()) return it->second;
81  else return 0.0;
82  }
83 
84  inline double& operator()( size_t i, size_t j ) {
85  om_assert(i < nlin());
86  om_assert(j < ncol());
87  return m_tank[ std::make_pair( i, j ) ];
88  }
89 
90  size_t size() const {
91  return m_tank.size();
92  }
93 
94  const_iterator begin() const { return m_tank.begin(); }
95  const_iterator end() const { return m_tank.end(); }
96 
97  SparseMatrix transpose() const;
98 
99  const Tank& tank() const {return m_tank;}
100 
101  void set( double t);
102  Vector getlin(size_t i) const;
103  void setlin(Vector v, size_t i);
104 
105  void save(const char *filename) const;
106  void load(const char *filename);
107 
108  void save(const std::string& s) const { save(s.c_str()); }
109  void load(const std::string& s) { load(s.c_str()); }
110 
111  void info() const;
112  double frobenius_norm() const;
113 
114  Vector operator*( const Vector &x ) const;
115  Matrix operator*( const SymMatrix &m ) const;
116  Matrix operator*( const Matrix &m ) const;
117  SparseMatrix operator*( const SparseMatrix &m ) const;
118  SparseMatrix operator+( const SparseMatrix &m ) const;
119 
120  private:
121 
123  };
124 
125  inline Vector SparseMatrix::getlin(size_t i) const {
126  om_assert(i<nlin());
127  Vector v(ncol());
128  for (size_t j=0;j<ncol();j++){
129  const_iterator it = m_tank.find(std::make_pair(i, j));
130  if (it != m_tank.end()) v(j)=it->second;
131  else v(j)=0.0;
132  }
133  return v;
134  }
135 
136  inline void SparseMatrix::setlin(Vector v, size_t i) {
137  om_assert(i<nlin());
138  for (size_t j=0;j<v.nlin();j++){
139  (*this)(i,j) = v(j);
140  }
141  }
142 }
void save(const std::string &s) const
SparseMatrix(const char *fname)
Definition: sparse_matrix.h:72
size_t nlin() const
Definition: linop.h:85
const_iterator end() const
Definition: sparse_matrix.h:95
void load(const std::string &s)
Vect3 operator*(const double &d, const Vect3 &v)
Definition: vect3.h:151
std::map< std::pair< size_t, size_t >, double > Tank
Definition: sparse_matrix.h:67
const Tank & tank() const
Definition: sparse_matrix.h:99
void setlin(Vector v, size_t i)
Vector getlin(size_t i) const
double & operator()(size_t i, size_t j)
Definition: sparse_matrix.h:84
double operator()(size_t i, size_t j) const
Definition: sparse_matrix.h:76
std::map< std::pair< size_t, size_t >, double >::const_iterator const_iterator
Definition: sparse_matrix.h:68
size_t size() const
Definition: sparse_matrix.h:90
SparseMatrix(size_t N, size_t M)
Definition: sparse_matrix.h:73
std::map< std::pair< size_t, size_t >, double >::iterator iterator
Definition: sparse_matrix.h:69
const_iterator begin() const
Definition: sparse_matrix.h:94
virtual size_t ncol() const
Definition: linop.h:88
Matrix class.
Definition: matrix.h:61