BandMatrixInline.cxx
1 // Copyright (C) 2014 INRIA
2 // Author(s): 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 #ifndef SELDON_FILE_BAND_MATRIX_INLINE_CXX
21 
22 #include "BandMatrix.hxx"
23 
24 namespace Seldon
25 {
26 
27  /***************
28  * Matrix_Band *
29  ***************/
30 
31 
33  template <class T, class Prop, class Storage, class Allocator>
35  {
36  return this->m_;
37  }
38 
39 
41  template <class T, class Prop, class Storage, class Allocator>
43  {
44  return kl_;
45  }
46 
47 
49  template <class T, class Prop, class Storage, class Allocator>
51  {
52  return ku_;
53  }
54 
55 
57  template <class T, class Prop, class Storage, class Allocator>
59  {
60  return this->n_;
61  }
62 
63 
65  template <class T, class Prop, class Storage, class Allocator>
67  {
68  return data_.GetDataSize();
69  }
70 
71 
73  template <class T, class Prop, class Storage, class Allocator>
75  {
76  return sizeof(*this) + data_.GetMemorySize() - sizeof(data_);
77  }
78 
79 
81  template <class T, class Prop, class Storage, class Allocator>
83  {
84  data_.Zero();
85  }
86 
87 
89  template <class T, class Prop, class Storage, class Allocator>
91  {
92  }
93 
94 
96  template <class T, class Prop, class Storage, class Allocator>
98  ::Reallocate(int m, int n)
99  {
100  Reallocate(m, n, 0, 0);
101  }
102 
103 
105  template <class T, class Prop, class Storage, class Allocator>
107  {
108  return data_.GetData();
109  }
110 
111 
113  template <class T, class Prop, class Storage, class Allocator>
116  {
117  data_ *= alpha;
118  return static_cast<Matrix<T, Prop, Storage, Allocator>& >(*this);
119  }
120 
121 
123  template <class T, class Prop, class Storage, class Allocator>
125  {
126  return Get(i, j);
127  }
128 
129 
131  template <class T, class Prop, class Storage, class Allocator>
132  inline const T& Matrix_Band<T, Prop, Storage, Allocator>::Val(int i, int j) const
133  {
134  return Get(i, j);
135  }
136 
137 
139  template <class T, class Prop, class Storage, class Allocator>
141  ::Set(int i, int j, const T& val)
142  {
143  Get(i, j) = val;
144  }
145 
146 
148  template <class T, class Prop, class Storage, class Allocator>
150  ::SetEntry(int i, int j, const T& val)
151  {
152  Get(i, j) = val;
153  }
154 
155 
156 #ifdef SELDON_WITH_VIRTUAL
157  template <class T, class Prop, class Storage, class Allocator>
159  ::ApplySor(const SeldonTranspose& trans, Vector<Treal>& x, const Vector<Treal>& r,
160  const typename ClassComplexType<T>::Treal& omega,
161  int nb_iter, int stage_ssor) const
162  {
163  SOR(trans, static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this),
164  x, r, omega, nb_iter, stage_ssor);
165  }
166 
167  template <class T, class Prop, class Storage, class Allocator>
168  inline void Matrix_Band<T, Prop, Storage, Allocator>
169  ::ApplySor(const SeldonTranspose& trans, Vector<Tcplx>& x, const Vector<Tcplx>& r,
170  const typename ClassComplexType<T>::Treal& omega,
171  int nb_iter, int stage_ssor) const
172  {
173  SOR(trans,
174  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this),
175  x, r, omega, nb_iter, stage_ssor);
176  }
177 
178  template <class T, class Prop, class Storage, class Allocator>
179  inline void Matrix_Band<T, Prop, Storage, Allocator>
180  ::MltAddVector(const Treal& alpha, const Vector<Treal>& x,
181  const Treal& beta, Vector<Treal>& y) const
182  {
183  Seldon::
184  MltAdd(alpha,
185  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this),
186  x, beta, y);
187  }
188 
189  template <class T, class Prop, class Storage, class Allocator>
190  inline void Matrix_Band<T, Prop, Storage, Allocator>
191  ::MltAddVector(const Tcplx& alpha, const Vector<Tcplx>& x,
192  const Tcplx& beta, Vector<Tcplx>& y) const
193  {
194  Seldon::
195  MltAdd(alpha,
196  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this),
197  x, beta, y);
198  }
199 
200  template <class T, class Prop, class Storage, class Allocator>
201  inline void Matrix_Band<T, Prop, Storage, Allocator>
202  ::MltAddVector(const Treal& alpha, const SeldonTranspose& trans,
203  const Vector<Treal>& x,
204  const Treal& beta, Vector<Treal>& y) const
205  {
206  Seldon::
207  MltAdd(alpha, trans,
208  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this),
209  x, beta, y);
210  }
211 
212  template <class T, class Prop, class Storage, class Allocator>
213  inline void Matrix_Band<T, Prop, Storage, Allocator>
214  ::MltAddVector(const Tcplx& alpha, const SeldonTranspose& trans,
215  const Vector<Tcplx>& x,
216  const Tcplx& beta, Vector<Tcplx>& y) const
217  {
218  Seldon::
219  MltAdd(alpha, trans,
220  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this),
221  x, beta, y);
222  }
223 
224  template <class T, class Prop, class Storage, class Allocator>
225  inline void Matrix_Band<T, Prop, Storage, Allocator>
226  ::MltVector(const Vector<Treal>& x, Vector<Treal>& y) const
227  {
228  Mlt(static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this), x, y);
229  }
230 
231  template <class T, class Prop, class Storage, class Allocator>
232  inline void Matrix_Band<T, Prop, Storage, Allocator>
233  ::MltVector(const Vector<Tcplx>& x, Vector<Tcplx>& y) const
234  {
235  Mlt(static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this), x, y);
236  }
237 
238  template <class T, class Prop, class Storage, class Allocator>
239  inline void Matrix_Band<T, Prop, Storage, Allocator>
240  ::MltVector(const SeldonTranspose& trans,
241  const Vector<Treal>& x, Vector<Treal>& y) const
242  {
243  Mlt(trans,
244  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this), x, y);
245  }
246 
247  template <class T, class Prop, class Storage, class Allocator>
248  inline void Matrix_Band<T, Prop, Storage, Allocator>
249  ::MltVector(const SeldonTranspose& trans,
250  const Vector<Tcplx>& x, Vector<Tcplx>& y) const
251  {
252  Mlt(trans,
253  static_cast<const Matrix<T, Prop, Storage, Allocator>& >(*this), x, y);
254  }
255 
256  template <class T, class Prop, class Storage, class Allocator>
257  inline bool Matrix_Band<T, Prop, Storage, Allocator>
258  ::IsSymmetric() const
259  {
260  return false;
261  }
262 #endif
263 
264 
265  /****************
266  * Matrix_Arrow *
267  ****************/
268 
269 
271  template <class T, class Prop, class Storage, class Allocator>
273  : Matrix_Band<T, Prop, Storage, Allocator>()
274  {
275  }
276 
277 
279  template <class T, class Prop, class Storage, class Allocator>
281  {
282  return this->m_ + last_rows_.GetM();
283  }
284 
285 
287  template <class T, class Prop, class Storage, class Allocator>
289  {
290  return this->n_ + last_columns_.GetN();
291  }
292 
293 
295  template <class T, class Prop, class Storage, class Allocator>
297  {
298  return last_rows_.GetM();
299  }
300 
301 
303  template <class T, class Prop, class Storage, class Allocator>
305  {
306  return last_columns_.GetN();
307  }
308 
309 
311  template <class T, class Prop, class Storage, class Allocator>
313  {
314  return this->data_.GetDataSize() + this->last_rows_.GetDataSize()
315  + this->last_columns_.GetDataSize() + this->last_block_.GetDataSize();
316  }
317 
318 
320  template <class T, class Prop, class Storage, class Allocator>
322  {
324  + last_rows_.GetMemorySize() + last_columns_.GetMemorySize()
325  + last_block_.GetMemorySize();
326  }
327 
328 
330  template <class T, class Prop, class Storage, class Allocator>
332  {
333  }
334 
335 
337  template <class T, class Prop, class Storage, class Allocator>
339  {
340  Reallocate(m, n, 0, 0);
341  }
342 
343 
345  template <class T, class Prop, class Storage, class Allocator>
347  {
348  return Get(i, j);
349  }
350 
351 
353  template <class T, class Prop, class Storage, class Allocator>
354  inline const T& Matrix_Arrow<T, Prop, Storage, Allocator>::Val(int i, int j) const
355  {
356  return Get(i, j);
357  }
358 
359 
361  template <class T, class Prop, class Storage, class Allocator>
363  ::Set(int i, int j, const T& val)
364  {
365  Get(i, j) = val;
366  }
367 
368 
370  template <class T, class Prop, class Storage, class Allocator>
372  ::SetEntry(int i, int j, const T& val)
373  {
374  Get(i, j) = val;
375  }
376 
377 
378  /**************
379  * Functions *
380  *************/
381 
382 
384  template<class T0, class T1, class Prop1, class Allocator1,
385  class T2, class Prop2, class Allocator2>
386  inline void AddMatrix(const T0& alpha,
389  {
390  B.Add_(alpha, A);
391  }
392 
393 
395  template<class T, class Allocator>
398  {
399  B.Copy(A);
400  }
401 
402 
404  template<class T, class Allocator>
405  inline void SolveLU(const Matrix<T, General, BandedCol, Allocator>& mat_lu,
406  Vector<T>& x)
407  {
408  mat_lu.Solve(x);
409  }
410 
411 
413  template<class T, class Allocator>
414  inline void SolveLU(const Matrix<T, General, BandedCol, Allocator>& mat_lu,
415  Vector<complex<T> >& x)
416  {
417  mat_lu.Solve(x);
418  }
419 
420 
422  template<class T0, class T1, class Allocator>
423  inline void AddVector(const T0& alpha,
426  {
427  B.Add_(alpha, A);
428  }
429 
430 
432  template<class T0, class T1, class T, class T2, class Allocator>
433  inline void MltAddVector(const T0& alpha,
435  const Vector<T2>& x, const T1& beta, Vector<T2>& y)
436  {
437  Mlt(beta, y);
438  A.MltAdd(alpha, SeldonNoTrans, x, y);
439  }
440 
441 
443  template<class T0, class T1, class T, class T2, class Allocator>
444  inline void MltAddVector(const T0& alpha, const SeldonTranspose& trans,
446  const Vector<T2>& x, const T1& beta, Vector<T2>& y)
447  {
448  Mlt(beta, y);
449  A.MltAdd(alpha, trans, x, y);
450  }
451 
452 
454  template<class T, class Allocator, class T1>
455  inline void MltVector(const Matrix<T, General, BandedCol, Allocator>& A,
456  const Vector<T1>& x, Vector<T1>& y)
457  {
458  T1 zero, one;
459  SetComplexZero(zero); SetComplexOne(one);
460  y.Fill(zero);
461  A.MltAdd(one, SeldonNoTrans, x, y);
462  }
463 
464 
466  template<class T, class Allocator, class T1>
467  inline void MltVector(const SeldonTranspose& trans,
469  const Vector<T1>& x, Vector<T1>& y)
470  {
471  T1 zero, one;
472  SetComplexZero(zero); SetComplexOne(one);
473  y.Fill(zero);
474  A.MltAdd(one, trans, x, y);
475  }
476 
477 
479  template<class T0, class T1, class Allocator>
480  inline void MltScalar(const T0& alpha,
482  {
483  A *= alpha;
484  }
485 
486 
488  template<class T, class Allocator>
489  inline ostream& operator<<(ostream& out,
491  {
492  A.WriteText(out);
493  return out;
494  }
495 
496 
498  template<class T, class Allocator>
501  bool keep_matrix)
502  {
503  mat_lu = A;
504  if (!keep_matrix)
505  A.Clear();
506 
507  mat_lu.Factorize();
508  }
509 
510 
512  template<class T, class Allocator>
514  {
515  A.Factorize();
516  }
517 
518 
520  template<class T, class Allocator>
521  inline void SolveLU(const Matrix<T, General, ArrowCol, Allocator>& mat_lu,
522  Vector<T>& x)
523  {
524  mat_lu.Solve(x);
525  }
526 
527 
529  template<class T, class Allocator>
530  inline void SolveLU(const Matrix<T, General, ArrowCol, Allocator>& mat_lu,
531  Vector<complex<T> >& x)
532  {
533  mat_lu.Solve(x);
534  }
535 
536 
538  template<class T0, class T1, class Prop1, class Allocator1,
539  class T2, class Prop2, class Allocator2>
540  inline void AddMatrix(const T0& alpha,
543  {
544  B.Add_(alpha, A);
545  }
546 
547 
549  template<class T0, class T1, class T, class T2, class Allocator>
550  inline void MltAddVector(const T0& alpha,
552  const Vector<T2>& x, const T1& beta, Vector<T2>& y)
553  {
554  Mlt(beta, y);
555  A.MltAdd(alpha, SeldonNoTrans, x, y);
556  }
557 
558 
560  template<class T0, class T1, class T, class T2, class Allocator>
561  inline void MltAddVector(const T0& alpha, const SeldonTranspose& trans,
563  const Vector<T2>& x, const T1& beta, Vector<T2>& y)
564  {
565  Mlt(beta, y);
566  A.MltAdd(alpha, trans, x, y);
567  }
568 
569 
571  template<class T, class Allocator, class T1>
572  inline void MltVector(const Matrix<T, General, ArrowCol, Allocator>& A,
573  const Vector<T1>& x, Vector<T1>& y)
574  {
575  T1 zero, one;
576  SetComplexZero(zero); SetComplexOne(one);
577  y.Fill(zero);
578  A.MltAdd(one, SeldonNoTrans, x, y);
579  }
580 
581 
583  template<class T, class Allocator, class T1>
584  inline void MltVector(const SeldonTranspose& trans,
586  const Vector<T1>& x, Vector<T1>& y)
587  {
588  T1 zero, one;
589  SetComplexZero(zero); SetComplexOne(one);
590  y.Fill(zero);
591  A.MltAdd(one, trans, x, y);
592  }
593 
594 
596  template<class T0, class T1, class Allocator>
597  inline void MltScalar(const T0& alpha,
599  {
600  A *= alpha;
601  }
602 
603 
605  template<class T, class Allocator>
606  inline ostream& operator<<(ostream& out,
608  {
609  A.WriteText(out);
610  return out;
611  }
612 
613 }
614 
615 #define SELDON_FILE_BAND_MATRIX_INLINE_CXX
616 #endif
Seldon::Matrix_Band::GetMemorySize
size_t GetMemorySize() const
returns the memory used by the object in bytes
Definition: BandMatrixInline.cxx:74
Seldon::Matrix_Band::Set
void Set(int, int, const T &)
sets A(i, j)
Definition: BandMatrixInline.cxx:141
Seldon::Matrix_Band
base class for a banded-matrix
Definition: BandMatrix.hxx:36
Seldon::SeldonTranspose
Definition: MatrixFlag.hxx:32
Seldon::Matrix_Arrow
base class for arrow matrix (banded matrix + dense columns/rows)
Definition: BandMatrix.hxx:170
Seldon::MltScalar
void MltScalar(const T0 &alpha, Array3D< T, Allocator > &A)
Multiplication of all elements of a 3D array by a scalar.
Definition: Array3D.cxx:539
Seldon::Matrix_Band::MltAdd
void MltAdd(const T0 &alpha, const SeldonTranspose &trans, const Vector< T1 > &x, Vector< T1 > &y) const
performs matrix-vector product y = y + alpha A x
Definition: BandMatrix.cxx:328
Seldon::Matrix_Band::GetN
int GetN() const
returns the number of rows
Definition: BandMatrixInline.cxx:58
Seldon::Matrix_Band::GetData
T * GetData() const
returns a pointer to the array containing values
Definition: BandMatrixInline.cxx:106
Seldon::Matrix_Band::Solve
void Solve(Vector< T1 > &x) const
solves A x = b, assuming that Factorize has been previously called
Definition: BandMatrix.cxx:384
Seldon::Matrix_Band::Zero
void Zero()
fills non-zero entries with 0
Definition: BandMatrixInline.cxx:82
Seldon::Matrix< T, General, ArrowCol, Allocator >
arrow matrix stored by columns
Definition: BandMatrix.hxx:253
Seldon::Vector< Treal >
Seldon::Matrix_Band::Copy
void Copy(const Matrix< T, General, ArrayRowSparse > &A)
conversion from a CSR matrix
Definition: BandMatrix.cxx:205
Seldon::Matrix
Definition: SeldonHeader.hxx:226
Seldon::Matrix_Band::GetM
int GetM() const
returns the number of rows
Definition: BandMatrixInline.cxx:34
Seldon::Matrix_Arrow::Solve
void Solve(Vector< T1 > &x) const
solves A x = b, assuming that Factorize has been previously called
Definition: BandMatrix.cxx:983
Seldon::Matrix< T, General, BandedCol, Allocator >
banded matrix stored by columns (Lapack format)
Definition: BandMatrix.hxx:161
Seldon::Matrix_Arrow::GetN
int GetN() const
returns the number of columns
Definition: BandMatrixInline.cxx:288
Seldon::Matrix_Band::SetEntry
void SetEntry(int, int, const T &)
sets A(i, j)
Definition: BandMatrixInline.cxx:150
Seldon::Matrix_Arrow::Clear
void Clear()
clears the matrix
Definition: BandMatrix.cxx:524
Seldon::Matrix_Band::Val
T & Val(int i, int j)
returns a reference to A(i, j)
Definition: BandMatrixInline.cxx:124
Seldon::Matrix_Arrow::Matrix_Arrow
Matrix_Arrow()
default constructor
Definition: BandMatrixInline.cxx:272
Seldon::Matrix_Band::GetDataSize
long GetDataSize() const
returns the number of elements stored in the matrix
Definition: BandMatrixInline.cxx:66
Seldon::GetLU
void GetLU(Matrix< T0, Prop0, Storage0, Allocator0 > &A)
Returns the LU factorization of a matrix.
Definition: Functions_Matrix.cxx:2073
Seldon::Matrix_Arrow::GetDataSize
long GetDataSize() const
returns the number of non-zero entries
Definition: BandMatrixInline.cxx:312
Seldon::Matrix_Band::GetKU
int GetKU() const
returns the number of extra-diagonals in upper part of the matrix
Definition: BandMatrixInline.cxx:50
Seldon::Matrix_Band::operator*=
Matrix< T, Prop, Storage, Allocator > & operator*=(const T &alpha)
multiplication by a scalar
Definition: BandMatrixInline.cxx:115
Seldon::Matrix_Band::HideMessages
void HideMessages()
present for compatibility
Definition: BandMatrixInline.cxx:90
Seldon::Matrix_Arrow::GetM
int GetM() const
returns the number of rows
Definition: BandMatrixInline.cxx:280
Seldon::Matrix_Band::Reallocate
void Reallocate(int m, int n)
changes the size of the matrix, previous entries are lost
Definition: BandMatrixInline.cxx:98
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::operator<<
ostream & operator<<(ostream &out, const Array< T, N, Allocator > &A)
operator<< overloaded for a 3D array.
Definition: Array.cxx:1617
Seldon::Matrix_Arrow::WriteText
void WriteText(string FileName) const
writes the matrix in a file in text format
Definition: BandMatrix.cxx:1094
Seldon::Matrix_Arrow::Factorize
void Factorize()
performs LU factorisation without pivoting
Definition: BandMatrix.cxx:744
Seldon::Matrix_Band::WriteText
void WriteText(string FileName) const
writes the matrix in a file in text format
Definition: BandMatrix.cxx:476
Seldon::AddMatrix
void AddMatrix(const T0 &alpha, const Matrix< T1, Prop1, Storage1, Allocator1 > &A, Matrix< T2, Prop2, Storage2, Allocator2 > &B)
Adds two matrices.
Definition: Functions_Matrix.cxx:1619
Seldon::Matrix_Band::GetKL
int GetKL() const
returns the number of extra-diagonals in lower part of the matrix
Definition: BandMatrixInline.cxx:42
Seldon::Matrix_Arrow::MltAdd
void MltAdd(const T0 &alpha, const SeldonTranspose &trans, const Vector< T1 > &x, Vector< T1 > &y) const
performs matrix-vector product y = y + alpha A x
Definition: BandMatrix.cxx:884
Seldon::AddVector
void AddVector(const T0 &alpha, const Vector< T1, Storage1, Allocator1 > &X, Vector< T2, Storage2, Allocator2 > &Y)
Adds two vectors Y = Y + alpha X.
Definition: Functions_Vector.cxx:94