Matrix_SparseInline.cxx
1 // Copyright (C) 2001-2011 Vivien Mallet
2 // Copyright (C) 2003-2011 Marc DuruflĂ©
3 //
4 // This file is part of the linear-algebra library Seldon,
5 // http://seldon.sourceforge.net/.
6 //
7 // Seldon is free software; you can redistribute it and/or modify it under the
8 // terms of the GNU Lesser General Public License as published by the Free
9 // Software Foundation; either version 2.1 of the License, or (at your option)
10 // any later version.
11 //
12 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15 // more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with Seldon. If not, see http://www.gnu.org/licenses/.
19 
20 
21 #ifndef SELDON_FILE_MATRIX_SPARSE_INLINE_CXX
22 
23 #include "Matrix_Sparse.hxx"
24 
25 #include <set>
26 
27 /*
28  Functions defined in this file:
29 
30  reads coordinate form (i, j, val) from an input stream
31  ReadCoordinateMatrix(FileStream, row_index, col_index, values, cplx)
32 
33  writes coordinate form (i, j, val) in an input stream
34  WriteCoordinateMatrix(FileStream, row_index, col_index, values, cplx)
35 
36  These functions are used by ReadText/WriteText
37 */
38 
39 namespace Seldon
40 {
41 
42 
43  /****************
44  * CONSTRUCTORS *
45  ****************/
46 
47 
49 
52  template <class T, class Prop, class Storage, class Allocator>
54  Matrix_Base<T, Allocator>()
55  {
56  nz_ = 0;
57  ptr_ = NULL;
58  ind_ = NULL;
59  }
60 
61 
63 
68  template <class T, class Prop, class Storage, class Allocator>
70  int j):
71  Matrix_Base<T, Allocator>()
72  {
73  nz_ = 0;
74  ptr_ = NULL;
75  ind_ = NULL;
76 
77  Reallocate(i, j);
78  }
79 
80 
82 
89  template <class T, class Prop, class Storage, class Allocator>
91  Matrix_Sparse(int i, int j, long nz):
92  Matrix_Base<T, Allocator>()
93  {
94  this->nz_ = 0;
95  ind_ = NULL;
96  ptr_ = NULL;
97 
98  Reallocate(i, j, nz);
99  }
100 
101 
102  /**************
103  * DESTRUCTOR *
104  **************/
105 
106 
108  template <class T, class Prop, class Storage, class Allocator>
110  {
111  this->Clear();
112  }
113 
114 
115  /*******************
116  * BASIC FUNCTIONS *
117  *******************/
118 
119 
121 
124  template <class T, class Prop, class Storage, class Allocator>
126  {
127  return nz_;
128  }
129 
130 
132 
137  template <class T, class Prop, class Storage, class Allocator>
139  {
140  return nz_;
141  }
142 
143 
145 
149  template <class T, class Prop, class Storage, class Allocator>
151  {
152  return ptr_;
153  }
154 
155 
157 
164  template <class T, class Prop, class Storage, class Allocator>
166  {
167  return ind_;
168  }
169 
170 
172 
175  template <class T, class Prop, class Storage, class Allocator>
177  {
178  return (Storage::GetFirst(this->m_, this->n_) + 1);
179  }
180 
181 
183 
191  template <class T, class Prop, class Storage, class Allocator>
193  {
194  return nz_;
195  }
196 
197 
199 
204  template <class T, class Prop, class Storage, class Allocator>
205  inline const typename Matrix_Sparse<T, Prop, Storage, Allocator>::value_type&
207  {
208  return Val(i, j);
209  }
210 
211 
213 
220  template <class T, class Prop, class Storage, class Allocator>
222  ::AddInteraction(int i, int j, const T& val)
223  {
224  Get(i, j) += val;
225  }
226 
227 
229  template <class T, class Prop, class Storage, class Allocator>
231  ::AddInteractionRow(int i, int nb, const Vector<int>& col,
232  const Vector<T>& val, bool sorted)
233  {
234  throw Undefined("AddInteractionRow", "Not implemented");
235  }
236 
237 
239 
244  template <class T, class Prop, class Storage, class Allocator>
246  ::Set(int i, int j, const T& val)
247  {
248  Get(i, j) = val;
249  }
250 
251 
253 
258  template <class T, class Prop, class Storage, class Allocator>
260  ::SetEntry(int i, int j, const T& val)
261  {
262  Get(i, j) = val;
263  }
264 
265 
267 
272  template <class T, class Prop, class Storage, class Allocator>
276  {
277  this->Copy(A);
278 
279  return *this;
280  }
281 
282 
283 #ifdef SELDON_WITH_VIRTUAL
284  template <class T, class Prop, class Storage, class Allocator>
286  ::ApplySor(const SeldonTranspose& trans, Vector<Treal>& x, const Vector<Treal>& r,
287  const typename ClassComplexType<T>::Treal& omega,
288  int nb_iter, int stage_ssor) const
289  {
290  SOR(trans, static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this),
291  x, r, omega, nb_iter, stage_ssor);
292  }
293 
294  template <class T, class Prop, class Storage, class Allocator>
295  inline void Matrix_Sparse<T, Prop, Storage, Allocator>
296  ::ApplySor(const SeldonTranspose& trans, Vector<Tcplx>& x, const Vector<Tcplx>& r,
297  const typename ClassComplexType<T>::Treal& omega,
298  int nb_iter, int stage_ssor) const
299  {
300  SOR(trans,
301  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this),
302  x, r, omega, nb_iter, stage_ssor);
303  }
304 
305  template <class T, class Prop, class Storage, class Allocator>
306  inline void Matrix_Sparse<T, Prop, Storage, Allocator>
307  ::MltAddVector(const Treal& alpha, const Vector<Treal>& x,
308  const Treal& beta, Vector<Treal>& y) const
309  {
310  MltAdd(alpha,
311  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this),
312  x, beta, y);
313  }
314 
315  template <class T, class Prop, class Storage, class Allocator>
316  inline void Matrix_Sparse<T, Prop, Storage, Allocator>
317  ::MltAddVector(const Tcplx& alpha, const Vector<Tcplx>& x,
318  const Tcplx& beta, Vector<Tcplx>& y) const
319  {
320  MltAdd(alpha,
321  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this),
322  x, beta, y);
323  }
324 
325  template <class T, class Prop, class Storage, class Allocator>
326  inline void Matrix_Sparse<T, Prop, Storage, Allocator>
327  ::MltAddVector(const Treal& alpha, const SeldonTranspose& trans,
328  const Vector<Treal>& x,
329  const Treal& beta, Vector<Treal>& y) const
330  {
331  MltAdd(alpha, trans,
332  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this),
333  x, beta, y);
334  }
335 
336  template <class T, class Prop, class Storage, class Allocator>
337  inline void Matrix_Sparse<T, Prop, Storage, Allocator>
338  ::MltAddVector(const Tcplx& alpha, const SeldonTranspose& trans,
339  const Vector<Tcplx>& x,
340  const Tcplx& beta, Vector<Tcplx>& y) const
341  {
342  MltAdd(alpha, trans,
343  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this),
344  x, beta, y);
345  }
346 
347  template <class T, class Prop, class Storage, class Allocator>
348  inline void Matrix_Sparse<T, Prop, Storage, Allocator>
349  ::MltVector(const Vector<Treal>& x, Vector<Treal>& y) const
350  {
351  Mlt(static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this), x, y);
352  }
353 
354  template <class T, class Prop, class Storage, class Allocator>
355  inline void Matrix_Sparse<T, Prop, Storage, Allocator>
356  ::MltVector(const Vector<Tcplx>& x, Vector<Tcplx>& y) const
357  {
358  Mlt(static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this), x, y);
359  }
360 
361  template <class T, class Prop, class Storage, class Allocator>
362  inline void Matrix_Sparse<T, Prop, Storage, Allocator>
363  ::MltVector(const SeldonTranspose& trans,
364  const Vector<Treal>& x, Vector<Treal>& y) const
365  {
366  Mlt(trans,
367  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this), x, y);
368  }
369 
370  template <class T, class Prop, class Storage, class Allocator>
371  inline void Matrix_Sparse<T, Prop, Storage, Allocator>
372  ::MltVector(const SeldonTranspose& trans,
373  const Vector<Tcplx>& x, Vector<Tcplx>& y) const
374  {
375  Mlt(trans,
376  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this), x, y);
377  }
378 
379  template <class T, class Prop, class Storage, class Allocator>
380  inline bool Matrix_Sparse<T, Prop, Storage, Allocator>
381  ::IsSymmetric() const
382  {
383  return false;
384  }
385 #endif
386 
387 
389  // MATRIX<COLSPARSE> //
391 
392 
393  /****************
394  * CONSTRUCTORS *
395  ****************/
396 
398 
401  template <class T, class Prop, class Allocator>
403  Matrix_Sparse<T, Prop, ColSparse, Allocator>()
404  {
405  }
406 
407 
409 
413  template <class T, class Prop, class Allocator>
415  Matrix_Sparse<T, Prop, ColSparse, Allocator>(i, j, 0)
416  {
417  }
418 
419 
421 
427  template <class T, class Prop, class Allocator>
428  inline Matrix<T, Prop, ColSparse, Allocator>::Matrix(int i, int j, long nz):
429  Matrix_Sparse<T, Prop, ColSparse, Allocator>(i, j, nz)
430  {
431  }
432 
433 
435 
446  template <class T, class Prop, class Allocator>
447  template <class Storage0, class Allocator0,
448  class Storage1, class Allocator1,
449  class Storage2, class Allocator2>
451  Matrix(int i, int j,
455  Matrix_Sparse<T, Prop, ColSparse, Allocator>(i, j, values, ptr, ind)
456  {
457  }
458 
459 
460 
462  // MATRIX<ROWSPARSE> //
464 
465 
466  /****************
467  * CONSTRUCTORS *
468  ****************/
469 
471 
474  template <class T, class Prop, class Allocator>
476  Matrix_Sparse<T, Prop, RowSparse, Allocator>()
477  {
478  }
479 
480 
482 
486  template <class T, class Prop, class Allocator>
488  Matrix_Sparse<T, Prop, RowSparse, Allocator>(i, j, 0)
489  {
490  }
491 
492 
494 
500  template <class T, class Prop, class Allocator>
501  inline Matrix<T, Prop, RowSparse, Allocator>::Matrix(int i, int j, long nz):
502  Matrix_Sparse<T, Prop, RowSparse, Allocator>(i, j, nz)
503  {
504  }
505 
506 
508 
519  template <class T, class Prop, class Allocator>
520  template <class Storage0, class Allocator0,
521  class Storage1, class Allocator1,
522  class Storage2, class Allocator2>
524  Matrix(int i, int j,
528  Matrix_Sparse<T, Prop, RowSparse, Allocator>(i, j, values, ptr, ind)
529  {
530  }
531 
532 
533 } // namespace Seldon.
534 
535 #define SELDON_FILE_MATRIX_SPARSE_INLINE_CXX
536 #endif
Seldon::SeldonTranspose
Definition: MatrixFlag.hxx:32
Seldon::Matrix_Base
Base class for all matrices.
Definition: Matrix_Base.hxx:143
Seldon::Matrix_Sparse::Matrix_Sparse
Matrix_Sparse()
Default constructor.
Definition: Matrix_SparseInline.cxx:53
Seldon::ColSparse
Definition: Storage.hxx:79
Seldon::Vector< int >
Seldon::Matrix
Definition: SeldonHeader.hxx:226
Seldon::Undefined
Definition: Errors.hxx:62
Seldon::Matrix_Sparse
Sparse-matrix class.
Definition: Matrix_Sparse.hxx:45
Seldon::RowSparse
Definition: Storage.hxx:91
Seldon
Seldon namespace.
Definition: Array.cxx:24