OpenMEEG
Toggle main menu visibility
Loading...
Searching...
No Matches
OpenMEEGMaths
include
fast_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 "
OpenMEEGMathsConfig.h
"
11
#include "
vector.h
"
12
#include "
sparse_matrix.h
"
13
14
namespace
OpenMEEG
{
15
16
class
OPENMEEGMATHS_EXPORT
FastSparseMatrix
17
{
18
public
:
19
20
inline
friend
std::ostream&
operator<<
(std::ostream& f,
const
FastSparseMatrix
&M);
21
22
protected
:
23
24
double
*
tank
;
25
size_t
*
js
;
26
size_t
*
rowindex
;
27
size_t
m_nlin
;
28
size_t
m_ncol
;
29
30
inline
void
alloc
(
size_t
nl,
size_t
nc,
size_t
nz);
31
inline
void
destroy
();
32
33
public
:
34
inline
FastSparseMatrix
();
35
inline
FastSparseMatrix
(
size_t
n,
size_t
p,
size_t
sp);
36
inline
FastSparseMatrix
(
const
SparseMatrix
&M);
37
inline
FastSparseMatrix
(
const
FastSparseMatrix
&M);
38
inline
~FastSparseMatrix
() {
destroy
();}
39
inline
size_t
nlin()
const
;
40
inline
size_t
ncol()
const
;
41
inline
void
write(std::ostream& f)
const
;
42
inline
void
read(std::istream& f);
43
44
inline
double
operator()(
size_t
i,
size_t
j)
const
;
45
inline
double
& operator()(
size_t
i,
size_t
j);
46
inline
Vector
operator * (
const
Vector
&v)
const
;
47
inline
void
operator =(
const
FastSparseMatrix
&M);
48
49
inline
double
&
operator[]
(
size_t
i) {
return
tank
[i];};
50
51
inline
void
info()
const
;
52
53
};
54
55
inline
std::ostream&
operator<<
(std::ostream& f,
const
FastSparseMatrix
&M)
56
{
57
size_t
nz = M.
rowindex
[M.
nlin
()];
58
f << M.
nlin
() <<
" "
<< M.
ncol
() << std::endl;
59
f << nz << std::endl;
60
for
(
size_t
i=0;i<M.
nlin
();i++)
61
{
62
for
(
size_t
j=M.
rowindex
[i];j<M.
rowindex
[i+1];j++)
63
{
64
f<<(
long
unsigned
int)i<<
"\t"
<<(
long
unsigned
int
)M.
js
[j]<<
"\t"
<<M.
tank
[j]<<std::endl;
65
}
66
}
67
return
f;
68
}
69
70
inline
void
FastSparseMatrix::info
()
const
{
71
if
((
nlin
() == 0) && (
ncol
() == 0)) {
72
std::cout <<
"Matrix Empty"
<< std::endl;
73
return
;
74
}
75
76
std::cout <<
"Dimensions : "
<<
nlin
() <<
" x "
<<
ncol
() << std::endl;
77
std::cout << *
this
;
78
}
79
80
inline
FastSparseMatrix::FastSparseMatrix
()
81
{
82
alloc
(1,1,1);
83
}
84
85
inline
FastSparseMatrix::FastSparseMatrix
(
size_t
n,
size_t
p,
size_t
sp=1)
86
{
87
alloc
(n,p,sp);
88
}
89
90
inline
void
FastSparseMatrix::operator =
(
const
FastSparseMatrix
&M)
91
{
92
destroy
();
93
alloc
(M.
m_nlin
,M.
m_ncol
,M.
rowindex
[M.
m_nlin
]);
94
memcpy(
tank
,M.
tank
,
sizeof
(
double
)*M.
rowindex
[M.
m_nlin
]);
95
memcpy(
js
,M.
js
,
sizeof
(
size_t
)*M.
rowindex
[M.
m_nlin
]);
96
memcpy(
rowindex
,M.
rowindex
,
sizeof
(
size_t
)*(M.
m_nlin
+1));
97
}
98
99
inline
FastSparseMatrix::FastSparseMatrix
(
const
SparseMatrix
&M)
100
{
101
tank
=
new
double
[M.
size
()];
102
js
=
new
size_t
[M.
size
()];
103
rowindex
=
new
size_t
[M.
nlin
()+1];
104
m_nlin
=(size_t)M.
nlin
();
105
m_ncol
=(size_t)M.
ncol
();
106
107
// we fill a data structure faster for computation
108
SparseMatrix::const_iterator
it;
109
int
cnt = 0;
110
size_t
current_line = (size_t)-1;
111
for
( it = M.
begin
(); it != M.
end
(); ++it) {
112
size_t
i = it->first.first;
113
size_t
j = it->first.second;
114
double
val = it->second;
115
tank
[cnt] = val;
116
js
[cnt] = j;
117
if
(i != current_line) {
118
for
(
size_t
k = current_line+1; k <= i; ++k) {
119
rowindex
[k]=cnt;
120
}
121
current_line = i;
122
}
123
cnt++;
124
}
125
126
for
(
size_t
k = current_line+1; k <= M.
nlin
(); ++k) {
127
rowindex
[k]=M.
size
();
128
}
129
130
}
131
132
inline
void
FastSparseMatrix::write
(std::ostream& f)
const
133
{
134
size_t
nz=
rowindex
[
m_nlin
];
135
f.write((
const
char
*)&
m_nlin
,(std::streamsize)
sizeof
(
size_t
));
136
f.write((
const
char
*)&
m_ncol
,(std::streamsize)
sizeof
(
size_t
));
137
f.write((
const
char
*)&nz,(std::streamsize)
sizeof
(
size_t
));
138
f.write((
const
char
*)
tank
,(std::streamsize)(
sizeof
(
double
)*nz));
139
f.write((
const
char
*)
js
,(std::streamsize)(
sizeof
(
size_t
)*nz));
140
f.write((
const
char
*)
rowindex
,(std::streamsize)(
sizeof
(
size_t
)*
m_nlin
));
141
}
142
143
inline
void
FastSparseMatrix::read
(std::istream& f)
144
{
145
destroy
();
146
size_t
nz;
147
f.read((
char
*)&
m_nlin
,(std::streamsize)
sizeof
(
size_t
));
148
f.read((
char
*)&
m_ncol
,(std::streamsize)
sizeof
(
size_t
));
149
f.read((
char
*)&nz,(std::streamsize)
sizeof
(
size_t
));
150
alloc
(
m_nlin
,
m_ncol
,nz);
151
f.read((
char
*)
tank
,(std::streamsize)(
sizeof
(
double
)*nz));
152
f.read((
char
*)
js
,(std::streamsize)(
sizeof
(
size_t
)*nz));
153
f.read((
char
*)
rowindex
,(std::streamsize)(
sizeof
(
size_t
)*
m_nlin
));
154
}
155
156
inline
void
FastSparseMatrix::alloc
(
size_t
nl,
size_t
nc,
size_t
nz)
157
{
158
m_nlin
=nl;
159
m_ncol
=nc;
160
tank
=
new
double
[nz];
161
js
=
new
size_t
[nz];
162
rowindex
=
new
size_t
[nl+1];
163
rowindex
[nl]=nz;
164
}
165
166
inline
void
FastSparseMatrix::destroy
()
167
{
168
delete
[]
tank
;
169
delete
[]
js
;
170
delete
[]
rowindex
;
171
}
172
173
inline
FastSparseMatrix::FastSparseMatrix
(
const
FastSparseMatrix
&m)
174
{
175
alloc
(m.
m_nlin
,m.
m_ncol
,m.
rowindex
[m.
m_nlin
]);
176
memcpy(
tank
,m.
tank
,
sizeof
(
double
)*m.
rowindex
[m.
m_nlin
]);
177
memcpy(
js
,m.
js
,
sizeof
(
size_t
)*m.
rowindex
[m.
m_nlin
]);
178
memcpy(
rowindex
,m.
rowindex
,
sizeof
(
size_t
)*(m.
m_nlin
+1));
179
}
180
181
inline
size_t
FastSparseMatrix::nlin
()
const
{
return
(
size_t
)
m_nlin
;}
182
183
inline
size_t
FastSparseMatrix::ncol
()
const
{
return
(
size_t
)
m_ncol
;}
184
185
inline
double
FastSparseMatrix::operator()
(
size_t
i,
size_t
j)
const
186
{
187
for
(
size_t
k=
rowindex
[i];k<
rowindex
[i+1];k++)
188
{
189
if
(
js
[k]<j)
continue
;
190
else
if
(
js
[k]==j)
return
tank
[k];
191
else
break
;
192
}
193
194
return
0;
195
}
196
197
inline
double
&
FastSparseMatrix::operator()
(
size_t
i,
size_t
j)
198
{
199
for
(
size_t
k=
rowindex
[i];k<
rowindex
[i+1];k++)
200
{
201
if
(
js
[k]<j)
continue
;
202
else
if
(
js
[k]==j)
return
tank
[k];
203
else
break
;
204
}
205
206
throw
OpenMEEG::maths::BadSparseOperation(
"FastSparseMatrix : double& operator()(size_t i,size_t j) can't add element"
);
207
}
208
209
inline
Vector
FastSparseMatrix::operator *
(
const
Vector
&v)
const
210
{
211
Vector
result(
m_nlin
); result.
set
(0);
212
double
*pt_result=&result(0);
213
Vector
*_v=(
Vector
*)&v;
214
double
*pt_vect=&(*_v)(0);
215
216
for
(
size_t
i=0;i<
m_nlin
;i++)
217
{
218
double
& total=pt_result[i];
219
for
(
size_t
j=
rowindex
[i];j<
rowindex
[i+1];j++) {
220
total+=
tank
[j]*pt_vect[
js
[j]];
221
}
222
}
223
return
result;
224
}
225
}
OpenMEEGMathsConfig.h
OpenMEEG::FastSparseMatrix
Definition
fast_sparse_matrix.h:17
OpenMEEG::FastSparseMatrix::m_ncol
size_t m_ncol
Definition
fast_sparse_matrix.h:28
OpenMEEG::FastSparseMatrix::destroy
void destroy()
Definition
fast_sparse_matrix.h:166
OpenMEEG::FastSparseMatrix::operator[]
double & operator[](size_t i)
Definition
fast_sparse_matrix.h:49
OpenMEEG::FastSparseMatrix::FastSparseMatrix
FastSparseMatrix()
Definition
fast_sparse_matrix.h:80
OpenMEEG::FastSparseMatrix::write
void write(std::ostream &f) const
Definition
fast_sparse_matrix.h:132
OpenMEEG::FastSparseMatrix::~FastSparseMatrix
~FastSparseMatrix()
Definition
fast_sparse_matrix.h:38
OpenMEEG::FastSparseMatrix::operator*
Vector operator*(const Vector &v) const
Definition
fast_sparse_matrix.h:209
OpenMEEG::FastSparseMatrix::operator<<
friend std::ostream & operator<<(std::ostream &f, const FastSparseMatrix &M)
Definition
fast_sparse_matrix.h:55
OpenMEEG::FastSparseMatrix::tank
double * tank
Definition
fast_sparse_matrix.h:24
OpenMEEG::FastSparseMatrix::info
void info() const
Definition
fast_sparse_matrix.h:70
OpenMEEG::FastSparseMatrix::ncol
size_t ncol() const
Definition
fast_sparse_matrix.h:183
OpenMEEG::FastSparseMatrix::operator()
double operator()(size_t i, size_t j) const
Definition
fast_sparse_matrix.h:185
OpenMEEG::FastSparseMatrix::js
size_t * js
Definition
fast_sparse_matrix.h:25
OpenMEEG::FastSparseMatrix::alloc
void alloc(size_t nl, size_t nc, size_t nz)
Definition
fast_sparse_matrix.h:156
OpenMEEG::FastSparseMatrix::operator=
void operator=(const FastSparseMatrix &M)
Definition
fast_sparse_matrix.h:90
OpenMEEG::FastSparseMatrix::read
void read(std::istream &f)
Definition
fast_sparse_matrix.h:143
OpenMEEG::FastSparseMatrix::rowindex
size_t * rowindex
Definition
fast_sparse_matrix.h:26
OpenMEEG::FastSparseMatrix::m_nlin
size_t m_nlin
Definition
fast_sparse_matrix.h:27
OpenMEEG::FastSparseMatrix::nlin
size_t nlin() const
Definition
fast_sparse_matrix.h:181
OpenMEEG::LinOpInfo::nlin
Dimension nlin() const
Definition
linop.h:48
OpenMEEG::LinOpInfo::ncol
virtual Dimension ncol() const
Definition
linop.h:51
OpenMEEG::SparseMatrix
Definition
sparse_matrix.h:26
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::begin
const_iterator begin() const
Definition
sparse_matrix.h:55
OpenMEEG::SparseMatrix::const_iterator
Tank::const_iterator const_iterator
Definition
sparse_matrix.h:30
OpenMEEG::Vector
Definition
vector.h:23
OpenMEEG::Vector::set
void set(const double x)
OpenMEEG
Definition
analytics.h:14
OpenMEEG::operator<<
std::ostream & operator<<(std::ostream &os, const Conductivity< REP > &m)
Definition
PropertiesSpecialized.h:31
sparse_matrix.h
vector.h
Generated by
1.17.0