Matrix_SymmetricInline.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_SYMMETRIC_INLINE_CXX
22 
23 #include "Matrix_Symmetric.hxx"
24 
25 namespace Seldon
26 {
27 
28 
29  /****************
30  * CONSTRUCTORS *
31  ****************/
32 
33 
35 
38  template <class T, class Prop, class Storage, class Allocator>
40  Matrix_Base<T, Allocator>()
41  {
42  me_ = NULL;
43  }
44 
45 
46  /**************
47  * DESTRUCTOR *
48  **************/
49 
50 
52  template <class T, class Prop, class Storage, class Allocator>
54  {
55  this->Clear();
56  }
57 
58 
59  /*******************
60  * BASIC FUNCTIONS *
61  *******************/
62 
63 
65 
71  template <class T, class Prop, class Storage, class Allocator>
73  {
74  return long(this->m_) * long(this->n_);
75  }
76 
77 
79  template <class T, class Prop, class Storage, class Allocator>
81  {
82  size_t taille = sizeof(*this) + size_t(GetDataSize())*sizeof(T);
83  taille += this->m_*sizeof(pointer);
84  return taille;
85  }
86 
87 
88  /**********************************
89  * ELEMENT ACCESS AND AFFECTATION *
90  **********************************/
91 
92 
94 
100  template <class T, class Prop, class Storage, class Allocator>
101  inline typename Matrix_Symmetric<T, Prop, Storage, Allocator>::reference
103  {
104 
105 #ifdef SELDON_CHECK_BOUNDS
106  CheckBounds(i, j, this->m_, this->n_, "Matrix_Symmetric");
107 #endif
108 
109  if (i > j)
110  return me_[Storage::GetSecond(i, j)][Storage::GetFirst(i, j)];
111  else
112  return me_[Storage::GetFirst(i, j)][Storage::GetSecond(i, j)];
113  }
114 
115 
117 
123  template <class T, class Prop, class Storage, class Allocator>
125  ::const_reference
127  ::operator() (int i, int j) const
128  {
129 
130 #ifdef SELDON_CHECK_BOUNDS
131  CheckBounds(i, j, this->m_, this->n_, "Matrix_Symmetric");
132 #endif
133 
134  if (i > j)
135  return me_[Storage::GetSecond(i, j)][Storage::GetFirst(i, j)];
136  else
137  return me_[Storage::GetFirst(i, j)][Storage::GetSecond(i, j)];
138  }
139 
140 
142 
148  template <class T, class Prop, class Storage, class Allocator>
150  ::const_reference
152  {
153 
154 #ifdef SELDON_CHECK_BOUNDS
155  CheckBoundsSym(i, j, this->m_, this->n_, "Matrix_Symmetric");
156 #endif
157 
158  return me_[Storage::GetFirst(i, j)][Storage::GetSecond(i, j)];
159  }
160 
161 
163 
169  template <class T, class Prop, class Storage, class Allocator>
170  inline typename Matrix_Symmetric<T, Prop, Storage, Allocator>::reference
172  {
173 
174 #ifdef SELDON_CHECK_BOUNDS
175  CheckBoundsSym(i, j, this->m_, this->n_, "Matrix_Symmetric");
176 #endif
177 
178  return me_[Storage::GetFirst(i, j)][Storage::GetSecond(i, j)];
179  }
180 
181 
183 
189  template <class T, class Prop, class Storage, class Allocator>
190  inline typename Matrix_Symmetric<T, Prop, Storage, Allocator>::reference
192  {
193 
194 #ifdef SELDON_CHECK_BOUNDS
195  CheckBounds(i, j, this->m_, this->n_, "Matrix_Symmetric");
196 #endif
197 
198  if (i > j)
199  return me_[Storage::GetSecond(i, j)][Storage::GetFirst(i, j)];
200  else
201  return me_[Storage::GetFirst(i, j)][Storage::GetSecond(i, j)];
202  }
203 
204 
206 
212  template <class T, class Prop, class Storage, class Allocator>
214  ::const_reference
216  ::Get(int i, int j) const
217  {
218 
219 #ifdef SELDON_CHECK_BOUNDS
220  CheckBounds(i, j, this->m_, this->n_, "Matrix_Symmetric");
221 #endif
222 
223  if (i > j)
224  return me_[Storage::GetSecond(i, j)][Storage::GetFirst(i, j)];
225  else
226  return me_[Storage::GetFirst(i, j)][Storage::GetSecond(i, j)];
227  }
228 
229 
231 
236  template <class T, class Prop, class Storage, class Allocator>
237  inline typename Matrix_Symmetric<T, Prop, Storage, Allocator>::reference
239  {
240 
241 #ifdef SELDON_CHECK_BOUNDS
242  CheckBounds(i, this->GetDataSize(), "Matrix_Symmetric");
243 #endif
244 
245  return this->data_[i];
246  }
247 
248 
250 
255  template <class T, class Prop, class Storage, class Allocator>
257  ::const_reference
259  {
260 
261 #ifdef SELDON_CHECK_BOUNDS
262  CheckBounds(i, this->GetDataSize(), "Matrix_Symmetric");
263 #endif
264 
265  return this->data_[i];
266  }
267 
268 
270 
275  template <class T, class Prop, class Storage, class Allocator>
279  {
280  this->Copy(A);
281 
282  return *this;
283  }
284 
285 
287 
292  template <class T, class Prop, class Storage, class Allocator>
294  ::Set(int i, int j, const T& x)
295  {
296  this->Get(i, j) = x;
297  }
298 
299 
301 
306  template <class T, class Prop, class Storage, class Allocator>
309  {
310  this->Reallocate(A.GetM(), A.GetN());
311 
312  Allocator::memorycpy(this->data_, A.GetData(), this->GetDataSize());
313  }
314 
315 
316 #ifdef SELDON_WITH_VIRTUAL
317  template <class T, class Prop, class Storage, class Allocator>
320  ::AddInteraction(int i, int j, const T& val)
321  {
322  if (i <= j)
323  this->Val(i, j) += val;
324  }
325 
326 
328  template <class T, class Prop, class Storage, class Allocator>
329  inline void Matrix_Symmetric<T, Prop, Storage, Allocator>
330  ::AddInteractionRow(int i, int n, const Vector<int>& col,
331  const Vector<T>& val, bool sorted)
332  {
333  for (int k = 0; k < n; k++)
334  if (i <= col(k))
335  this->Val(i, col(k)) += val(k);
336  }
337 
338 
340  template <class T, class Prop, class Storage, class Allocator>
341  inline void Matrix_Symmetric<T, Prop, Storage, Allocator>
342  ::AddInteractionColumn(int i, int n, const Vector<int>& row,
343  const Vector<T>& val, bool sorted)
344  {
345  for (int k = 0; k < n; k++)
346  if (row(k) <= i)
347  this->Val(row(k), i) += val(k);
348  }
349 
350 
352  template <class T, class Prop, class Storage, class Allocator>
353  inline void Matrix_Symmetric<T, Prop, Storage, Allocator>
354  ::ClearRow(int i)
355  {
356  for (int k = 0; k < this->n_; k++)
357  SetComplexZero(this->Val(i, k));
358  }
359 
360 
361  template <class T, class Prop, class Storage, class Allocator>
362  inline void Matrix_Symmetric<T, Prop, Storage, Allocator>
363  ::ApplySor(const SeldonTranspose& trans, Vector<Treal>& x, const Vector<Treal>& r,
364  const typename ClassComplexType<T>::Treal& omega,
365  int nb_iter, int stage_ssor) const
366  {
367  SOR(trans, static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this),
368  x, r, omega, nb_iter, stage_ssor);
369  }
370 
371  template <class T, class Prop, class Storage, class Allocator>
372  inline void Matrix_Symmetric<T, Prop, Storage, Allocator>
373  ::ApplySor(const SeldonTranspose& trans, Vector<Tcplx>& x, const Vector<Tcplx>& r,
374  const typename ClassComplexType<T>::Treal& omega,
375  int nb_iter, int stage_ssor) const
376  {
377  SOR(trans,
378  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this),
379  x, r, omega, nb_iter, stage_ssor);
380  }
381 
382  template <class T, class Prop, class Storage, class Allocator>
383  inline void Matrix_Symmetric<T, Prop, Storage, Allocator>
384  ::MltAddVector(const Treal& alpha, const Vector<Treal>& x,
385  const Treal& beta, Vector<Treal>& y) const
386  {
387  MltAdd(alpha,
388  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this),
389  x, beta, y);
390  }
391 
392  template <class T, class Prop, class Storage, class Allocator>
393  inline void Matrix_Symmetric<T, Prop, Storage, Allocator>
394  ::MltAddVector(const Tcplx& alpha, const Vector<Tcplx>& x,
395  const Tcplx& beta, Vector<Tcplx>& y) const
396  {
397  MltAdd(alpha,
398  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this),
399  x, beta, y);
400  }
401 
402  template <class T, class Prop, class Storage, class Allocator>
403  inline void Matrix_Symmetric<T, Prop, Storage, Allocator>
404  ::MltAddVector(const Treal& alpha, const SeldonTranspose& trans,
405  const Vector<Treal>& x,
406  const Treal& beta, Vector<Treal>& y) const
407  {
408  MltAdd(alpha, trans,
409  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this),
410  x, beta, y);
411  }
412 
413  template <class T, class Prop, class Storage, class Allocator>
414  inline void Matrix_Symmetric<T, Prop, Storage, Allocator>
415  ::MltAddVector(const Tcplx& alpha, const SeldonTranspose& trans,
416  const Vector<Tcplx>& x,
417  const Tcplx& beta, Vector<Tcplx>& y) const
418  {
419  MltAdd(alpha, trans,
420  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this),
421  x, beta, y);
422  }
423 
424  template <class T, class Prop, class Storage, class Allocator>
425  inline void Matrix_Symmetric<T, Prop, Storage, Allocator>
426  ::MltVector(const Vector<Treal>& x, Vector<Treal>& y) const
427  {
428  Mlt(static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this), x, y);
429  }
430 
431  template <class T, class Prop, class Storage, class Allocator>
432  inline void Matrix_Symmetric<T, Prop, Storage, Allocator>
433  ::MltVector(const Vector<Tcplx>& x, Vector<Tcplx>& y) const
434  {
435  Mlt(static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this), x, y);
436  }
437 
438  template <class T, class Prop, class Storage, class Allocator>
439  inline void Matrix_Symmetric<T, Prop, Storage, Allocator>
440  ::MltVector(const SeldonTranspose& trans,
441  const Vector<Treal>& x, Vector<Treal>& y) const
442  {
443  Mlt(trans,
444  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this), x, y);
445  }
446 
447  template <class T, class Prop, class Storage, class Allocator>
448  inline void Matrix_Symmetric<T, Prop, Storage, Allocator>
449  ::MltVector(const SeldonTranspose& trans,
450  const Vector<Tcplx>& x, Vector<Tcplx>& y) const
451  {
452  Mlt(trans,
453  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this), x, y);
454  }
455 
456  template <class T, class Prop, class Storage, class Allocator>
457  inline bool Matrix_Symmetric<T, Prop, Storage, Allocator>
458  ::IsSymmetric() const
459  {
460  return true;
461  }
462 #endif
463 
465  // MATRIX<COLSYM> //
467 
468 
469  /****************
470  * CONSTRUCTORS *
471  ****************/
472 
473 
475 
478  template <class T, class Prop, class Allocator>
480  Matrix_Symmetric<T, Prop, ColSym, Allocator>()
481  {
482  }
483 
484 
486 
490  template <class T, class Prop, class Allocator>
492  Matrix_Symmetric<T, Prop, ColSym, Allocator>(i, j)
493  {
494  }
495 
496 
497  /*****************
498  * OTHER METHODS *
499  *****************/
500 
501 
503 
506  template <class T, class Prop, class Allocator>
507  template <class T0>
510  {
511  this->Fill(x);
512 
513  return *this;
514  }
515 
516 
518 
523  template <class T, class Prop, class Allocator>
526  ColSym,
527  Allocator>& A)
528  {
529  this->Copy(A);
530  return *this;
531  }
532 
533 
535 
538  template <class T, class Prop, class Allocator>
539  template <class T0>
542  {
543  for (long i = 0; i < this->GetDataSize();i++)
544  this->data_[i] *= x;
545 
546  return *this;
547  }
548 
549 
551  // MATRIX<ROWSYM> //
553 
554 
555  /****************
556  * CONSTRUCTORS *
557  ****************/
558 
559 
561 
564  template <class T, class Prop, class Allocator>
566  Matrix_Symmetric<T, Prop, RowSym, Allocator>()
567  {
568  }
569 
570 
572 
576  template <class T, class Prop, class Allocator>
578  Matrix_Symmetric<T, Prop, RowSym, Allocator>(i, j)
579  {
580  }
581 
582 
583  /*****************
584  * OTHER METHODS *
585  *****************/
586 
587 
589 
592  template <class T, class Prop, class Allocator>
593  template <class T0>
596  {
597  this->Fill(x);
598 
599  return *this;
600  }
601 
602 
604 
609  template <class T, class Prop, class Allocator>
612  RowSym,
613  Allocator>& A)
614  {
615  this->Copy(A);
616  return *this;
617  }
618 
619 
621 
624  template <class T, class Prop, class Allocator>
625  template <class T0>
628  {
629  for (long i = 0; i < this->GetDataSize();i++)
630  this->data_[i] *= x;
631 
632  return *this;
633  }
634 
635 } // namespace Seldon.
636 
637 #define SELDON_FILE_MATRIX_SYMMETRIC_INLINE_CXX
638 #endif
Seldon::Matrix_Base
Base class for all matrices.
Definition: Matrix_Base.hxx:143
Seldon::Matrix
Definition: SeldonHeader.hxx:226
Seldon::Matrix< T, Prop, ColSym, Allocator >
Column-major symmetric full-matrix class.
Definition: Matrix_Symmetric.hxx:165
Seldon::Matrix< T, Prop, RowSym, Allocator >
Row-major symmetric full-matrix class.
Definition: Matrix_Symmetric.hxx:192
Seldon::Matrix_Base< T, typename SeldonDefaultAllocator< Storage, T >::allocator >::GetData
pointer GetData() const
Returns a pointer to the data array.
Definition: Matrix_BaseInline.cxx:241
Seldon::VirtualMatrix::GetN
int GetN() const
Returns the number of columns.
Definition: Matrix_BaseInline.cxx:80
Seldon::VirtualMatrix::GetM
int GetM() const
Returns the number of rows.
Definition: Matrix_BaseInline.cxx:69
Seldon::Matrix_Symmetric::Matrix_Symmetric
Matrix_Symmetric()
Default constructor.
Definition: Matrix_SymmetricInline.cxx:39
Seldon::Matrix_Symmetric
Symmetric matrix stored in a full matrix.
Definition: Matrix_Symmetric.hxx:38
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::ColSym
Definition: Storage.hxx:168
Seldon::RowSym
Definition: Storage.hxx:179