Program listing for file kernel/src/utils/SiconosAlgebra/SiconosMatrix.hpp#
Return to documentation for this file
1#ifndef __SiconosMatrix__
2#define __SiconosMatrix__
3
4#include <stddef.h>
5#include <iosfwd>
6#include "CSparseMatrix.h"
7#include "SiconosAlgebraTypeDef.hpp"
8#include "SiconosFwd.hpp"
9#include "SiconosSerialization.hpp"
10#include "SiconosVisitor.hpp"
11class BlockVector;
12
13
14
15union MATRIX_UBLAS_TYPE
16{
17 DenseMat *Dense;
18 TriangMat *Triang;
19 SymMat *Sym;
20 SparseMat *Sparse;
21 BandedMat *Banded;
22 ZeroMat *Zero;
23 IdentityMat *Identity;
24 SparseCoordinateMat *SparseCoordinate;
25};
26
27typedef std::vector<int> VInt;
28TYPEDEF_SPTR(VInt)
29
30
31
32class SiconosMatrix
33{
34protected:
35 ACCEPT_SERIALIZATION(SiconosMatrix);
36
37
38 siconos::UBLAS_TYPE _num;
39
40
41 bool _isSymmetric;
42
43
44 bool _isPositiveDefinite;
45
46
47 SiconosMatrix() {};
48
49
50 SiconosMatrix(siconos::UBLAS_TYPE type);
51
52
53 void private_prod(unsigned int startRow, const SiconosVector& x, SiconosVector& y, bool init) const;
54
55
56
57 void private_addprod(unsigned int startRow, unsigned int startCol, const SiconosVector& x, SiconosVector& res) const;
58
59public:
60
61
62 virtual ~SiconosMatrix() {};
63
64
65 inline bool isBlock(void) const
66 {
67 if (_num == siconos::BLOCK) return true ;
68 else return false;
69 }
70
71
72 inline virtual bool isPLUInversed() const
73 {
74 return false;
75 };
76
77
78 inline bool isSymmetric() const
79 {
80 return _isSymmetric;
81 }
82
83
84 inline void setIsSymmetric(bool b)
85 {
86 _isSymmetric= b;
87 }
88
89
90 inline bool isPositiveDefinite() const
91 {
92 return _isPositiveDefinite;
93 }
94
95
96 inline void setIsPositiveDefinite(bool b)
97 {
98 _isPositiveDefinite= b ;
99 }
100
101
102 virtual bool checkSymmetry(double tol) const =0;
103
104
105 inline virtual bool isPLUFactorized() const
106 {
107 return false;
108 };
109
110
111 inline virtual bool isPLUFactorizedInPlace() const
112 {
113 return false;
114 };
115
116
117 inline virtual bool isCholeskyFactorized() const
118 {
119 return false;
120 };
121
122
123 inline bool isQRFactorized() const
124 {
125 return false;
126 }
127
128 inline virtual SP::VInt ipiv() const
129 {
130 SP::VInt dummy;
131 return dummy;
132 }
133
134
135 inline virtual bool isFactorized() const
136 {
137 return (isPLUFactorized() || isPLUFactorizedInPlace() || isCholeskyFactorized() || isQRFactorized());
138 };
139
140
141
142 virtual unsigned int size(unsigned int index) const = 0;
143
144
145 inline siconos::UBLAS_TYPE num() const
146 {
147 return _num;
148 };
149
150
151 inline virtual unsigned int numberOfBlocks(unsigned int i) const
152 {
153 return 1;
154 };
155
156
157 virtual const SP::Index tabRow() const ;
158
159
160 virtual const SP::Index tabCol() const ;
161
162
163 virtual const DenseMat getDense(unsigned int row = 0, unsigned int col = 0) const = 0;
164
165
166 virtual const TriangMat getTriang(unsigned int row = 0, unsigned int col = 0) const = 0;
167
168
169 virtual const SymMat getSym(unsigned int row = 0, unsigned int col = 0) const = 0;
170
171
172 virtual const BandedMat getBanded(unsigned int row = 0, unsigned int col = 0) const = 0;
173
174
175 virtual const SparseMat getSparse(unsigned int row = 0, unsigned int col = 0) const = 0;
176
177
178 virtual const SparseCoordinateMat getSparseCoordinate(unsigned int row = 0, unsigned int col = 0) const = 0;
179
180
181 virtual const ZeroMat getZero(unsigned int row = 0, unsigned int col = 0) const = 0;
182
183
184 virtual const IdentityMat getIdentity(unsigned int row = 0, unsigned int col = 0) const = 0;
185
186
187 virtual DenseMat* dense(unsigned int row = 0, unsigned int col = 0) const = 0;
188
189
190 virtual TriangMat* triang(unsigned int row = 0, unsigned int col = 0) const = 0;
191
192
193 virtual SymMat* sym(unsigned int row = 0, unsigned int col = 0) const = 0;
194
195
196 virtual BandedMat* banded(unsigned int row = 0, unsigned int col = 0) const = 0;
197
198
199 virtual SparseMat* sparse(unsigned int row = 0, unsigned int col = 0) const = 0;
200
201
202 virtual SparseCoordinateMat* sparseCoordinate(unsigned int row = 0, unsigned int col = 0) const = 0;
203
204
205 virtual ZeroMat* zero_mat(unsigned int row = 0, unsigned int col = 0) const = 0;
206
207
208 virtual IdentityMat* identity(unsigned int row = 0, unsigned int col = 0) const = 0;
209
210
211 virtual double* getArray(unsigned int row = 0, unsigned int col = 0) const = 0;
212
213
214 virtual void zero() = 0;
215
216
217 virtual void randomize() = 0;
218
219
220 virtual void eye() = 0;
221
222
223 virtual void resize(unsigned int nbrow, unsigned int nbcol,
224 unsigned int lower = 0, unsigned int upper = 0, bool preserve = true) = 0;
225
226
227 virtual double normInf() const = 0;
228
229
230 virtual void display() const = 0;
231
232
233 virtual void displayExpert(bool brief = true ) const = 0;
234
235
236 virtual std::string toString() const = 0;
237
238
239
240
241
242
243 virtual double& operator()(unsigned int i, unsigned int j) = 0;
244
245
246 virtual double operator()(unsigned int i, unsigned int j) const = 0;
247
248
249 virtual double getValue(unsigned int i, unsigned int j) const = 0;
250
251
252 virtual void setValue(unsigned int i, unsigned int j, double value) = 0;
253
254
255 virtual SP::SiconosMatrix block(unsigned int row = 0, unsigned int col = 0)
256 {
257 THROW_EXCEPTION("must be implemented");
258 };
259
260
261
262 virtual SPC::SiconosMatrix block(unsigned int row = 0, unsigned int col = 0) const
263 {
264 THROW_EXCEPTION("must be implemented");
265 };
266
267
268 virtual void getRow(unsigned int index, SiconosVector& vOut) const = 0;
269
270
271 virtual void getCol(unsigned int index, SiconosVector& vOut) const = 0;
272
273
274 virtual void setRow(unsigned int index, const SiconosVector& vIn) = 0;
275
276
277 virtual void setCol(unsigned int index, const SiconosVector& vIn) = 0;
278
279
280 virtual void trans() = 0;
281
282
283 virtual void trans(const SiconosMatrix& m) = 0;
284
285
286 virtual SiconosMatrix& operator = (const SiconosMatrix& m) = 0;
287
288
289 virtual SiconosMatrix& operator = (const DenseMat& m) = 0;
290
291
292 virtual SiconosMatrix& operator +=(const SiconosMatrix& m) = 0;
293
294
295 virtual SiconosMatrix& operator -=(const SiconosMatrix& m) = 0;
296
297 virtual void updateNumericsMatrix() =0;
298
299 virtual NumericsMatrix * numericsMatrix() const
300 {
301 return nullptr;
302 };
303
304
305
306 virtual void PLUFactorizationInPlace() = 0;
307
308
309 virtual void Factorize() = 0;
310
311
312 virtual void PLUInverseInPlace() = 0;
313
314
315 virtual void PLUForwardBackwardInPlace(SiconosMatrix &B) = 0;
316
317
318 virtual void Solve(SiconosMatrix &B) = 0;
319
320
321 virtual void PLUForwardBackwardInPlace(SiconosVector &B) = 0;
322
323
324 virtual void Solve(SiconosVector &B) = 0;
325
326
327 virtual void resetLU()
328 {
329 THROW_EXCEPTION(" SiconosMatrix::resetLU not yet implemented for BlockMatrix.");
330 };
331
332
333 virtual void resetFactorizationFlags()
334 {
335 THROW_EXCEPTION(" SiconosMatrix::resetFactorizationFlags not yet implemented for BlockMatrix.");
336 };
337
338
339 virtual size_t nnz(double tol = 1e-14);
340
341
342 bool fillCSC(CSparseMatrix* csc, size_t row_off, size_t col_off, double tol = 1e-14);
343
344
345 bool fillCSC(CSparseMatrix* csc, double tol = 1e-14);
346
347 bool fromCSC(CSparseMatrix* csc);
348
349
350 bool fillTriplet(CSparseMatrix* csc, size_t row_off, size_t col_off, double tol = 1e-14);
351
352 VIRTUAL_ACCEPT_VISITORS(SiconosMatrix);
353
354
355
356
357 friend void prod(const SiconosMatrix& A, const SiconosVector& x, BlockVector& y, bool init);
358
359 friend void prod(const SiconosMatrix& A, const BlockVector& x, SiconosVector& y, bool init);
360
361
362 friend std::ostream& operator<<(std::ostream& os, const SiconosMatrix& sm);
363
364
365 friend SiconosMatrix& operator *=(SiconosMatrix& m, const double& s);
366
367
368 friend SiconosMatrix& operator /=(SiconosMatrix& m, const double& s);
369
370
371};
372
373#endif