Matrix_SymPackedInline.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_SYMPACKED_INLINE_CXX
22 
23 #include "Matrix_SymPacked.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  }
43 
44 
45  /**************
46  * DESTRUCTOR *
47  **************/
48 
49 
51  template <class T, class Prop, class Storage, class Allocator>
53  {
54  this->Clear();
55  }
56 
57 
58  /*******************
59  * BASIC FUNCTIONS *
60  *******************/
61 
62 
64 
67  template <class T, class Prop, class Storage, class Allocator>
69  {
70  return (long(this->m_) * long(this->m_ + 1)) / 2;
71  }
72 
73 
75  template <class T, class Prop, class Storage, class Allocator>
77  {
78  size_t taille = sizeof(*this) + size_t(GetDataSize())*sizeof(T);
79  return taille;
80  }
81 
82 
83  /**********************************
84  * ELEMENT ACCESS AND AFFECTATION *
85  **********************************/
86 
87 
89 
95  template <class T, class Prop, class Storage, class Allocator>
96  inline typename Matrix_SymPacked<T, Prop, Storage, Allocator>::reference
98  {
99 
100 #ifdef SELDON_CHECK_BOUNDS
101  CheckBounds(i, j, this->m_, this->n_, "Matrix_SymPacked");
102 #endif
103 
104  return this->data_[j > i
105  ? Storage::GetFirst(i * long(this->n_)
106  - (i * long(i + 1)) / 2 + j,
107  (j*long(j+1)) / 2 + i)
108  : Storage::GetFirst(j * long(this->m_)
109  - (j * long(j + 1)) / 2 + i,
110  (i * long(i + 1)) / 2 + j)];
111  }
112 
113 
115 
121  template <class T, class Prop, class Storage, class Allocator>
123  ::const_reference
125  int j) const
126  {
127 
128 #ifdef SELDON_CHECK_BOUNDS
129  CheckBounds(i, j, this->m_, this->n_, "Matrix_SymPacked");
130 #endif
131 
132  return this->data_[j > i
133  ? Storage::GetFirst(i * long(this->n_)
134  - (i * long(i + 1)) / 2 + j,
135  (j * long(j + 1)) / 2 + i)
136  : Storage::GetFirst(j * long(this->m_)
137  - (j * long(j + 1)) / 2 + i,
138  (i * long(i + 1)) / 2 + j)];
139  }
140 
141 
143 
150  template <class T, class Prop, class Storage, class Allocator>
151  inline typename Matrix_SymPacked<T, Prop, Storage, Allocator>::reference
153  {
154 
155 #ifdef SELDON_CHECK_BOUNDS
156  CheckBounds(i, j, this->m_, this->n_, "Matrix_SymPacked");
157 #endif
158 
159  return this->data_[j > i
160  ? Storage::GetFirst(i * long(this->n_)
161  - (i * long(i + 1)) / 2 + j,
162  (j * long(j + 1)) / 2 + i)
163  : Storage::GetFirst(j * long(this->m_)
164  - (j * long(j + 1)) / 2 + i,
165  (i * long(i + 1)) / 2 + j)];
166  }
167 
168 
170 
177  template <class T, class Prop, class Storage, class Allocator>
179  ::const_reference
181  {
182 
183 #ifdef SELDON_CHECK_BOUNDS
184  CheckBounds(i, j, this->m_, this->n_, "Matrix_SymPacked");
185 #endif
186 
187  return this->data_[j > i
188  ? Storage::GetFirst(i * long(this->n_)
189  - (i * long(i + 1)) / 2 + j,
190  (j * long(j + 1)) / 2 + i)
191  : Storage::GetFirst(j * long(this->m_)
192  - (j * long(j + 1)) / 2 + i,
193  (i * long(i + 1)) / 2 + j)];
194  }
195 
196 
198 
204  template <class T, class Prop, class Storage, class Allocator>
206  ::const_reference
208  {
209  return this->Val(i, j);
210  }
211 
212 
214 
220  template <class T, class Prop, class Storage, class Allocator>
223  {
224  return this->Val(i, j);
225  }
226 
227 
229 
234  template <class T, class Prop, class Storage, class Allocator>
235  inline typename Matrix_SymPacked<T, Prop, Storage, Allocator>::reference
237  {
238 
239 #ifdef SELDON_CHECK_BOUNDS
240  CheckBounds(i, this->GetDataSize(), "Matrix_SymPacked");
241 #endif
242 
243  return this->data_[i];
244  }
245 
246 
248 
253  template <class T, class Prop, class Storage, class Allocator>
255  ::const_reference
257  {
258 
259 #ifdef SELDON_CHECK_BOUNDS
260  CheckBounds(i, this->GetDataSize(), "Matrix_SymPacked");
261 #endif
262 
263  return this->data_[i];
264  }
265 
266 
268 
273  template <class T, class Prop, class Storage, class Allocator>
277  {
278  this->Copy(A);
279 
280  return *this;
281  }
282 
283 
285 
290  template <class T, class Prop, class Storage, class Allocator>
292  ::Set(int i, int j, const T& x)
293  {
294  this->Val(i, j) = x;
295  }
296 
297 
299 
304  template <class T, class Prop, class Storage, class Allocator>
307  {
308  this->Reallocate(A.GetM(), A.GetN());
309 
310  Allocator::memorycpy(this->data_, A.GetData(), this->GetDataSize());
311  }
312 
313 
314 #ifdef SELDON_WITH_VIRTUAL
315  template <class T, class Prop, class Storage, class Allocator>
318  ::AddInteraction(int i, int j, const T& val)
319  {
320  if (i <= j)
321  this->Val(i, j) += val;
322  }
323 
324 
326  template <class T, class Prop, class Storage, class Allocator>
327  inline void Matrix_SymPacked<T, Prop, Storage, Allocator>
328  ::AddInteractionRow(int i, int n, const Vector<int>& col,
329  const Vector<T>& val, bool sorted)
330  {
331  for (int k = 0; k < n; k++)
332  if (i <= col(k))
333  this->Val(i, col(k)) += val(k);
334  }
335 
336 
338  template <class T, class Prop, class Storage, class Allocator>
339  inline void Matrix_SymPacked<T, Prop, Storage, Allocator>
340  ::AddInteractionColumn(int i, int n, const Vector<int>& row,
341  const Vector<T>& val, bool sorted)
342  {
343  for (int k = 0; k < n; k++)
344  if (row(k) <= i)
345  this->Val(row(k), i) += val(k);
346  }
347 
348 
350  template <class T, class Prop, class Storage, class Allocator>
351  inline void Matrix_SymPacked<T, Prop, Storage, Allocator>
352  ::ClearRow(int i)
353  {
354  for (int k = 0; k < this->n_; k++)
355  SetComplexZero(this->Val(i, k));
356  }
357 
358 
359  template <class T, class Prop, class Storage, class Allocator>
360  inline void Matrix_SymPacked<T, Prop, Storage, Allocator>
361  ::ApplySor(const SeldonTranspose& trans, Vector<Treal>& x, const Vector<Treal>& r,
362  const typename ClassComplexType<T>::Treal& omega,
363  int nb_iter, int stage_ssor) const
364  {
365  SOR(trans, static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this),
366  x, r, omega, nb_iter, stage_ssor);
367  }
368 
369  template <class T, class Prop, class Storage, class Allocator>
370  inline void Matrix_SymPacked<T, Prop, Storage, Allocator>
371  ::ApplySor(const SeldonTranspose& trans, Vector<Tcplx>& x, const Vector<Tcplx>& r,
372  const typename ClassComplexType<T>::Treal& omega,
373  int nb_iter, int stage_ssor) const
374  {
375  SOR(trans,
376  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this),
377  x, r, omega, nb_iter, stage_ssor);
378  }
379 
380  template <class T, class Prop, class Storage, class Allocator>
381  inline void Matrix_SymPacked<T, Prop, Storage, Allocator>
382  ::MltAddVector(const Treal& alpha, const Vector<Treal>& x,
383  const Treal& beta, Vector<Treal>& y) const
384  {
385  MltAdd(alpha,
386  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this),
387  x, beta, y);
388  }
389 
390  template <class T, class Prop, class Storage, class Allocator>
391  inline void Matrix_SymPacked<T, Prop, Storage, Allocator>
392  ::MltAddVector(const Tcplx& alpha, const Vector<Tcplx>& x,
393  const Tcplx& beta, Vector<Tcplx>& y) const
394  {
395  MltAdd(alpha,
396  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this),
397  x, beta, y);
398  }
399 
400  template <class T, class Prop, class Storage, class Allocator>
401  inline void Matrix_SymPacked<T, Prop, Storage, Allocator>
402  ::MltAddVector(const Treal& alpha, const SeldonTranspose& trans,
403  const Vector<Treal>& x,
404  const Treal& beta, Vector<Treal>& y) const
405  {
406  MltAdd(alpha, trans,
407  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this),
408  x, beta, y);
409  }
410 
411  template <class T, class Prop, class Storage, class Allocator>
412  inline void Matrix_SymPacked<T, Prop, Storage, Allocator>
413  ::MltAddVector(const Tcplx& alpha, const SeldonTranspose& trans,
414  const Vector<Tcplx>& x,
415  const Tcplx& beta, Vector<Tcplx>& y) const
416  {
417  MltAdd(alpha, trans,
418  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this),
419  x, beta, y);
420  }
421 
422  template <class T, class Prop, class Storage, class Allocator>
423  inline void Matrix_SymPacked<T, Prop, Storage, Allocator>
424  ::MltVector(const Vector<Treal>& x, Vector<Treal>& y) const
425  {
426  Mlt(static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this), x, y);
427  }
428 
429  template <class T, class Prop, class Storage, class Allocator>
430  inline void Matrix_SymPacked<T, Prop, Storage, Allocator>
431  ::MltVector(const Vector<Tcplx>& x, Vector<Tcplx>& y) const
432  {
433  Mlt(static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this), x, y);
434  }
435 
436  template <class T, class Prop, class Storage, class Allocator>
437  inline void Matrix_SymPacked<T, Prop, Storage, Allocator>
438  ::MltVector(const SeldonTranspose& trans,
439  const Vector<Treal>& x, Vector<Treal>& y) const
440  {
441  Mlt(trans,
442  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this), x, y);
443  }
444 
445  template <class T, class Prop, class Storage, class Allocator>
446  inline void Matrix_SymPacked<T, Prop, Storage, Allocator>
447  ::MltVector(const SeldonTranspose& trans,
448  const Vector<Tcplx>& x, Vector<Tcplx>& y) const
449  {
450  Mlt(trans,
451  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this), x, y);
452  }
453 
454  template <class T, class Prop, class Storage, class Allocator>
455  inline bool Matrix_SymPacked<T, Prop, Storage, Allocator>
456  ::IsSymmetric() const
457  {
458  return true;
459  }
460 #endif
461 
463  // MATRIX<COLSYMPACKED> //
465 
466 
467  /****************
468  * CONSTRUCTORS *
469  ****************/
470 
471 
473 
476  template <class T, class Prop, class Allocator>
478  Matrix_SymPacked<T, Prop, ColSymPacked, Allocator>()
479  {
480  }
481 
482 
484 
489  template <class T, class Prop, class Allocator>
491  Matrix_SymPacked<T, Prop, ColSymPacked, Allocator>(i, i)
492  {
493  }
494 
495 
496  /*****************
497  * OTHER METHODS *
498  *****************/
499 
500 
502 
505  template <class T, class Prop, class Allocator>
506  template <class T0>
509  {
510  this->Fill(x);
511 
512  return *this;
513  }
514 
516 
521  template <class T, class Prop, class Allocator>
524  ColSymPacked,
525  Allocator>& A)
526  {
527  this->Copy(A);
528  return *this;
529  }
530 
531 
533 
536  template <class T, class Prop, class Allocator>
537  template <class T0>
540  {
541  long size = this->GetDataSize();
542  for (long i = 0; i < size;i++)
543  this->data_[i] *= x;
544 
545  return *this;
546  }
547 
548 
550  // MATRIX<ROWSYMPACKED> //
552 
553 
554  /****************
555  * CONSTRUCTORS *
556  ****************/
557 
558 
560 
563  template <class T, class Prop, class Allocator>
565  Matrix_SymPacked<T, Prop, RowSymPacked, Allocator>()
566  {
567  }
568 
569 
571 
576  template <class T, class Prop, class Allocator>
578  Matrix_SymPacked<T, Prop, RowSymPacked, Allocator>(i, i)
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  return *this;
599  }
600 
601 
603 
608  template <class T, class Prop, class Allocator>
611  RowSymPacked,
612  Allocator>& A)
613  {
614  this->Copy(A);
615  return *this;
616  }
617 
618 
620 
623  template <class T, class Prop, class Allocator>
624  template <class T0>
627  {
628  long size = this->GetDataSize();
629  for (long i = 0; i < size;i++)
630  this->data_[i] *= x;
631 
632  return *this;
633  }
634 
635 
636 } // namespace Seldon.
637 
638 #define SELDON_FILE_MATRIX_SYMPACKED_INLINE_CXX
639 #endif
Seldon::Matrix_Base
Base class for all matrices.
Definition: Matrix_Base.hxx:143
Seldon::Matrix_SymPacked
Symmetric packed matrix class.
Definition: Matrix_SymPacked.hxx:37
Seldon::Matrix
Definition: SeldonHeader.hxx:226
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::RowSymPacked
Definition: Storage.hxx:157
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::ColSymPacked
Definition: Storage.hxx:146
Seldon::Matrix_SymPacked::Matrix_SymPacked
Matrix_SymPacked()
Default constructor.
Definition: Matrix_SymPackedInline.cxx:39
Seldon::Matrix< T, Prop, ColSymPacked, Allocator >
Column-major symmetric packed matrix class.
Definition: Matrix_SymPacked.hxx:162
Seldon::Matrix< T, Prop, RowSymPacked, Allocator >
Row-major symmetric packed matrix class.
Definition: Matrix_SymPacked.hxx:190
Seldon
Seldon namespace.
Definition: Array.cxx:24