OpenMEEG
Toggle main menu visibility
Loading...
Searching...
No Matches
OpenMEEGMaths
include
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) {};
36
~SparseMatrix
() {};
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
58
SparseMatrix
transpose
()
const
;
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
;
110
SparseMatrix
operator*
(
const
SparseMatrix
& m)
const
;
111
SparseMatrix
operator+
(
const
SparseMatrix
& m)
const
;
112
113
private
:
114
115
Tank
m_tank;
116
};
117
}
OpenMEEG::LinOpInfo::nlin
Dimension nlin() const
Definition
linop.h:48
OpenMEEG::LinOpInfo::ncol
virtual Dimension ncol() const
Definition
linop.h:51
OpenMEEG::LinOpInfo::SPARSE
@ SPARSE
Definition
linop.h:40
OpenMEEG::LinOp::LinOp
LinOp()
Definition
linop.h:77
OpenMEEG::Matrix
Matrix class Matrix class.
Definition
matrix.h:28
OpenMEEG::SparseMatrix::set
void set(const double d)
Definition
sparse_matrix.h:62
OpenMEEG::SparseMatrix::SparseMatrix
SparseMatrix(const char *fname)
Definition
sparse_matrix.h:34
OpenMEEG::SparseMatrix::transpose
SparseMatrix transpose() const
OpenMEEG::SparseMatrix::SparseMatrix
SparseMatrix()
Definition
sparse_matrix.h:33
OpenMEEG::SparseMatrix::operator()
double operator()(const size_t i, const size_t j) const
Definition
sparse_matrix.h:38
OpenMEEG::SparseMatrix::iterator
Tank::iterator iterator
Definition
sparse_matrix.h:31
OpenMEEG::SparseMatrix::tank
const Tank & tank() const
Definition
sparse_matrix.h:60
OpenMEEG::SparseMatrix::operator*
Vector operator*(const Vector &x) const
OpenMEEG::SparseMatrix::SparseMatrix
SparseMatrix(const size_t N, const size_t M)
Definition
sparse_matrix.h:35
OpenMEEG::SparseMatrix::save
void save(const char *filename) const
Definition
sparse_matrix.h:83
OpenMEEG::SparseMatrix::end
const_iterator end() const
Definition
sparse_matrix.h:56
OpenMEEG::SparseMatrix::size
size_t size() const
Definition
sparse_matrix.h:51
OpenMEEG::SparseMatrix::save
void save(const std::string &s) const
Definition
sparse_matrix.h:101
OpenMEEG::SparseMatrix::begin
const_iterator begin() const
Definition
sparse_matrix.h:55
OpenMEEG::SparseMatrix::operator+
SparseMatrix operator+(const SparseMatrix &m) const
OpenMEEG::SparseMatrix::operator*
SparseMatrix operator*(const SparseMatrix &m) const
OpenMEEG::SparseMatrix::load
void load(const std::string &s)
Definition
sparse_matrix.h:102
OpenMEEG::SparseMatrix::load
void load(const char *filename)
Definition
sparse_matrix.h:92
OpenMEEG::SparseMatrix::info
void info() const
OpenMEEG::SparseMatrix::Tank
std::map< std::pair< size_t, size_t >, double > Tank
Definition
sparse_matrix.h:29
OpenMEEG::SparseMatrix::getlin
Vector getlin(const size_t i) const
Definition
sparse_matrix.h:67
OpenMEEG::SparseMatrix::const_iterator
Tank::const_iterator const_iterator
Definition
sparse_matrix.h:30
OpenMEEG::SparseMatrix::setlin
void setlin(const Vector &v, const size_t i)
Definition
sparse_matrix.h:77
OpenMEEG::SparseMatrix::frobenius_norm
double frobenius_norm() const
OpenMEEG::SparseMatrix::operator()
double & operator()(size_t i, size_t j)
Definition
sparse_matrix.h:45
OpenMEEG::SparseMatrix::operator*
Matrix operator*(const Matrix &m) const
OpenMEEG::SparseMatrix::operator*
Matrix operator*(const SymMatrix &m) const
OpenMEEG::SparseMatrix::~SparseMatrix
~SparseMatrix()
Definition
sparse_matrix.h:36
OpenMEEG::SymMatrix
Definition
symmatrix.h:21
OpenMEEG::Vector
Definition
vector.h:23
linop.h
matrix.h
OpenMEEG
Definition
analytics.h:14
vector.h
Generated by
1.17.0