Matrix_HermitianInline.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_HERMITIAN_INLINE_CXX
22 
23 #include "Matrix_Hermitian.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  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 += sizeof(pointer)*this->m_;
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 const typename
102  Matrix_Hermitian<T, Prop, Storage, Allocator>::value_type
104  ::operator() (int i, int j) const
105  {
106 
107 #ifdef SELDON_CHECK_BOUNDS
108  CheckBounds(i, j, this->m_, this->n_, "Matrix_Hermitian");
109 #endif
110 
111  if (i > j)
112  return conj(me_[Storage::GetSecond(i, j)][Storage::GetFirst(i, j)]);
113  else
114  return me_[Storage::GetFirst(i, j)][Storage::GetSecond(i, j)];
115 
116  }
117 
118 
120 
126  template <class T, class Prop, class Storage, class Allocator>
128  ::const_reference
130  {
131 
132 #ifdef SELDON_CHECK_BOUNDS
133  CheckBoundsSym(i, j, this->m_, this->n_, "Matrix_Hermitian");
134 #endif
135 
136  return me_[Storage::GetFirst(i, j)][Storage::GetSecond(i, j)];
137  }
138 
139 
141 
147  template <class T, class Prop, class Storage, class Allocator>
148  inline typename Matrix_Hermitian<T, Prop, Storage, Allocator>::reference
150  {
151 
152 #ifdef SELDON_CHECK_BOUNDS
153  CheckBoundsSym(i, j, this->m_, this->n_, "Matrix_Hermitian");
154 #endif
155 
156  return me_[Storage::GetFirst(i, j)][Storage::GetSecond(i, j)];
157  }
158 
159 
161 
167  template <class T, class Prop, class Storage, class Allocator>
169  ::const_reference
171  {
172 
173 #ifdef SELDON_CHECK_BOUNDS
174  CheckBoundsSym(i, j, this->m_, this->n_, "Matrix_Hermitian");
175 #endif
176 
177  return me_[Storage::GetFirst(i, j)][Storage::GetSecond(i, j)];
178  }
179 
180 
182 
188  template <class T, class Prop, class Storage, class Allocator>
189  inline typename Matrix_Hermitian<T, Prop, Storage, Allocator>::reference
191  {
192 
193 #ifdef SELDON_CHECK_BOUNDS
194  CheckBoundsSym(i, j, this->m_, this->n_, "Matrix_Hermitian");
195 #endif
196 
197  return me_[Storage::GetFirst(i, j)][Storage::GetSecond(i, j)];
198  }
199 
200 
202 
207  template <class T, class Prop, class Storage, class Allocator>
208  inline typename Matrix_Hermitian<T, Prop, Storage, Allocator>::reference
210  {
211 
212 #ifdef SELDON_CHECK_BOUNDS
213  CheckBounds(i, this->GetDataSize(), "Matrix_Hermitian");
214 #endif
215 
216  return this->data_[i];
217  }
218 
219 
221 
226  template <class T, class Prop, class Storage, class Allocator>
228  ::const_reference
230  {
231 
232 #ifdef SELDON_CHECK_BOUNDS
233  CheckBounds(i, this->GetDataSize(), "Matrix_Hermitian");
234 #endif
235 
236  return this->data_[i];
237  }
238 
239 
241 
246  template <class T, class Prop, class Storage, class Allocator>
248  ::Set(int i, int j, const T& x)
249  {
250  if (i > j)
251  this->Val(j, i) = conj(x);
252  else
253  this->Val(i, j) = x;
254  }
255 
256 
258 
263  template <class T, class Prop, class Storage, class Allocator>
267  {
268  this->Copy(A);
269 
270  return *this;
271  }
272 
273 
275 
280  template <class T, class Prop, class Storage, class Allocator>
283  {
284  this->Reallocate(A.GetM(), A.GetN());
285 
286  Allocator::memorycpy(this->data_, A.GetData(), this->GetDataSize());
287  }
288 
289 
290 #ifdef SELDON_WITH_VIRTUAL
291  template <class T, class Prop, class Storage, class Allocator>
294  ::AddInteraction(int i, int j, const T& val)
295  {
296  if (i <= j)
297  this->Val(i, j) += val;
298  }
299 
300 
302  template <class T, class Prop, class Storage, class Allocator>
303  inline void Matrix_Hermitian<T, Prop, Storage, Allocator>
304  ::AddInteractionRow(int i, int n, const Vector<int>& col,
305  const Vector<T>& val, bool sorted)
306  {
307  for (int k = 0; k < n; k++)
308  if (i <= col(k))
309  this->Val(i, col(k)) += val(k);
310  }
311 
312 
314  template <class T, class Prop, class Storage, class Allocator>
315  inline void Matrix_Hermitian<T, Prop, Storage, Allocator>
316  ::AddInteractionColumn(int i, int n, const Vector<int>& row,
317  const Vector<T>& val, bool sorted)
318  {
319  for (int k = 0; k < n; k++)
320  if (row(k) <= i)
321  this->Val(row(k), i) += val(k);
322  }
323 
324 
326  template <class T, class Prop, class Storage, class Allocator>
327  inline void Matrix_Hermitian<T, Prop, Storage, Allocator>
328  ::ClearRow(int i)
329  {
330  for (int k = 0; k < this->n_; k++)
331  SetComplexZero(this->Val(i, k));
332  }
333 
334 
335  template <class T, class Prop, class Storage, class Allocator>
336  inline void Matrix_Hermitian<T, Prop, Storage, Allocator>
337  ::ApplySor(const SeldonTranspose& trans, Vector<Treal>& x, const Vector<Treal>& r,
338  const typename ClassComplexType<T>::Treal& omega,
339  int nb_iter, int stage_ssor) const
340  {
341  SOR(trans, static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this),
342  x, r, omega, nb_iter, stage_ssor);
343  }
344 
345  template <class T, class Prop, class Storage, class Allocator>
346  inline void Matrix_Hermitian<T, Prop, Storage, Allocator>
347  ::ApplySor(const SeldonTranspose& trans, Vector<Tcplx>& x, const Vector<Tcplx>& r,
348  const typename ClassComplexType<T>::Treal& omega,
349  int nb_iter, int stage_ssor) const
350  {
351  SOR(trans,
352  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this),
353  x, r, omega, nb_iter, stage_ssor);
354  }
355 
356  template <class T, class Prop, class Storage, class Allocator>
357  inline void Matrix_Hermitian<T, Prop, Storage, Allocator>
358  ::MltAddVector(const Treal& alpha, const Vector<Treal>& x,
359  const Treal& beta, Vector<Treal>& y) const
360  {
361  cout << "Not defined for real numbers" << endl;
362  abort();
363  }
364 
365  template <class T, class Prop, class Storage, class Allocator>
366  inline void Matrix_Hermitian<T, Prop, Storage, Allocator>
367  ::MltAddVector(const Tcplx& alpha, const Vector<Tcplx>& x,
368  const Tcplx& beta, Vector<Tcplx>& y) const
369  {
370  MltAdd(alpha,
371  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this),
372  x, beta, y);
373  }
374 
375  template <class T, class Prop, class Storage, class Allocator>
376  inline void Matrix_Hermitian<T, Prop, Storage, Allocator>
377  ::MltAddVector(const Treal& alpha, const SeldonTranspose& trans,
378  const Vector<Treal>& x,
379  const Treal& beta, Vector<Treal>& y) const
380  {
381  cout << "Not defined for real numbers" << endl;
382  abort();
383  }
384 
385  template <class T, class Prop, class Storage, class Allocator>
386  inline void Matrix_Hermitian<T, Prop, Storage, Allocator>
387  ::MltAddVector(const Tcplx& alpha, const SeldonTranspose& trans,
388  const Vector<Tcplx>& x,
389  const Tcplx& beta, Vector<Tcplx>& y) const
390  {
391  MltAdd(alpha, trans,
392  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this),
393  x, beta, y);
394  }
395 
396  template <class T, class Prop, class Storage, class Allocator>
397  inline void Matrix_Hermitian<T, Prop, Storage, Allocator>
398  ::MltVector(const Vector<Treal>& x, Vector<Treal>& y) const
399  {
400  cout << "Not defined for real numbers" << endl;
401  abort();
402  }
403 
404  template <class T, class Prop, class Storage, class Allocator>
405  inline void Matrix_Hermitian<T, Prop, Storage, Allocator>
406  ::MltVector(const Vector<Tcplx>& x, Vector<Tcplx>& y) const
407  {
408  Mlt(static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this), x, y);
409  }
410 
411  template <class T, class Prop, class Storage, class Allocator>
412  inline void Matrix_Hermitian<T, Prop, Storage, Allocator>
413  ::MltVector(const SeldonTranspose& trans,
414  const Vector<Treal>& x, Vector<Treal>& y) const
415  {
416  cout << "Not defined for real numbers" << endl;
417  abort();
418  }
419 
420  template <class T, class Prop, class Storage, class Allocator>
421  inline void Matrix_Hermitian<T, Prop, Storage, Allocator>
422  ::MltVector(const SeldonTranspose& trans,
423  const Vector<Tcplx>& x, Vector<Tcplx>& y) const
424  {
425  Mlt(trans,
426  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 bool Matrix_Hermitian<T, Prop, Storage, Allocator>
431  ::IsSymmetric() const
432  {
433  return false;
434  }
435 #endif
436 
437 
439  // MATRIX<COLHERM> //
441 
442 
443  /****************
444  * CONSTRUCTORS *
445  ****************/
446 
447 
449 
452  template <class T, class Prop, class Allocator>
454  Matrix_Hermitian<T, Prop, ColHerm, Allocator>()
455  {
456  }
457 
458 
460 
464  template <class T, class Prop, class Allocator>
466  Matrix_Hermitian<T, Prop, ColHerm, Allocator>(i, j)
467  {
468  }
469 
470 
471  /*****************
472  * OTHER METHODS *
473  *****************/
474 
475 
477 
480  template <class T, class Prop, class Allocator>
481  template <class T0>
484  {
485  this->Fill(x);
486 
487  return *this;
488  }
489 
491 
496  template <class T, class Prop, class Allocator>
499  ColHerm,
500  Allocator>& A)
501  {
502  this->Copy(A);
503  return *this;
504  }
505 
506 
508 
513  template <class T, class Prop, class Allocator>
514  template <class T0>
517  {
518  long size = this->GetDataSize();
519  for (long i = 0; i < size;i++)
520  this->data_[i] *= x;
521 
522  return *this;
523  }
524 
525 
526 
528  // MATRIX<ROWHERM> //
530 
531 
532  /****************
533  * CONSTRUCTORS *
534  ****************/
535 
536 
538 
541  template <class T, class Prop, class Allocator>
543  Matrix_Hermitian<T, Prop, RowHerm, Allocator>()
544  {
545  }
546 
547 
549 
553  template <class T, class Prop, class Allocator>
555  Matrix_Hermitian<T, Prop, RowHerm, Allocator>(i, j)
556  {
557  }
558 
559 
560  /*****************
561  * OTHER METHODS *
562  *****************/
563 
564 
566 
569  template <class T, class Prop, class Allocator>
570  template <class T0>
573  {
574  this->Fill(x);
575 
576  return *this;
577  }
578 
580 
585  template <class T, class Prop, class Allocator>
588  RowHerm,
589  Allocator>& A)
590  {
591  this->Copy(A);
592  return *this;
593  }
594 
595 
597 
600  template <class T, class Prop, class Allocator>
601  template <class T0>
604  {
605  long size = this->GetDataSize();
606  for (long i = 0; i < size; i++)
607  this->data_[i] *= x;
608 
609  return *this;
610  }
611 
612 } // namespace Seldon.
613 
614 #define SELDON_FILE_MATRIX_HERMITIAN_INLINE_CXX
615 #endif
Seldon::ColHerm
Definition: Storage.hxx:196
Seldon::Matrix_Base
Base class for all matrices.
Definition: Matrix_Base.hxx:143
Seldon::Matrix
Definition: SeldonHeader.hxx:226
Seldon::Matrix< T, Prop, ColHerm, Allocator >
Column-major hermitian full-matrix class.
Definition: Matrix_Hermitian.hxx:167
Seldon::Matrix_Hermitian
Hermitian matrix stored in a full matrix.
Definition: Matrix_Hermitian.hxx:38
Seldon::Matrix< T, Prop, RowHerm, Allocator >
Row-major hermitian full-matrix class.
Definition: Matrix_Hermitian.hxx:194
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::RowHerm
Definition: Storage.hxx:207
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
Seldon namespace.
Definition: Array.cxx:24
Seldon::Matrix_Hermitian::Matrix_Hermitian
Matrix_Hermitian()
Default constructor.
Definition: Matrix_HermitianInline.cxx:39