Matrix_SymComplexSparse.hxx
1 // Copyright (C) 2001-2011 Vivien Mallet
2 // Copyright (C) 2001-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 // To be included by Seldon.hxx
22 
23 #ifndef SELDON_FILE_MATRIX_SYMCOMPLEXSPARSE_HXX
24 
25 namespace Seldon
26 {
27 
29  template<class T>
31  {
32  public :
33  typedef typename
35  ::allocator allocator;
36  };
37 
38  template<>
40  {
41  public:
42  typedef
43  SeldonDefaultAllocator<VectFull, float>::allocator allocator;
44  };
45 
46  template<>
48  {
49  public:
50  typedef
51  SeldonDefaultAllocator<VectFull, double>::allocator allocator;
52  };
53 
54  template<>
55  class SeldonDefaultAllocator<ColSymComplexSparse, complex<long double> >
56  {
57  public:
58  typedef
59  SeldonDefaultAllocator<VectFull, long double>::allocator allocator;
60  };
61 
62 
64  template<class T>
66  {
67  public :
68  typedef typename
70  ::allocator allocator;
71  };
72 
73  template<>
75  {
76  public:
77  typedef
78  SeldonDefaultAllocator<VectFull, float>::allocator allocator;
79  };
80 
81  template<>
83  {
84  public:
85  typedef
86  SeldonDefaultAllocator<VectFull, double>::allocator allocator;
87  };
88 
89  template<>
90  class SeldonDefaultAllocator<RowSymComplexSparse, complex<long double> >
91  {
92  public:
93  typedef
94  SeldonDefaultAllocator<VectFull, long double>::allocator allocator;
95  };
96 
97 
99 
111  template <class T, class Prop, class Storage, class Allocator
112  = typename SeldonDefaultAllocator<Storage, T>::allocator>
113  class Matrix_SymComplexSparse: public Matrix_Base<T, Allocator>
114  {
115  // typedef declaration.
116  public:
117  typedef typename Allocator::value_type value_type;
118  typedef typename Allocator::pointer pointer;
119  typedef typename Allocator::const_pointer const_pointer;
120  typedef typename Allocator::reference reference;
121  typedef typename Allocator::const_reference const_reference;
122  typedef typename SeldonDefaultAllocator<VectFull, int>::allocator AllocatorInt;
123  typedef typename SeldonDefaultAllocator<VectFull, long>::allocator AllocatorLong;
124  typedef complex<value_type> entry_type;
125  typedef complex<value_type> access_type;
126  typedef complex<value_type> const_access_type;
127 
128  // Attributes.
129  protected:
130  // Number of non-zero (stored) elements.
131  long real_nz_;
132  long imag_nz_;
133  // Index (in data_) of first element stored for each row or column.
134  long* real_ptr_;
135  long* imag_ptr_;
136  // Column or row index (in the matrix) each element.
137  int* real_ind_;
138  int* imag_ind_;
139 
140  // Data.
141  value_type* real_data_;
142  value_type* imag_data_;
143 
144  // Methods.
145  public:
146  // Constructors.
148  explicit Matrix_SymComplexSparse(int i, int j);
149  Matrix_SymComplexSparse(int i, int j, long real_nz, long imag_nz);
150  template <class Storage0, class Allocator0,
151  class Storage1, class Allocator1,
152  class Storage2, class Allocator2>
153  Matrix_SymComplexSparse(int i, int j,
160  Matrix_SymComplexSparse(const Matrix_SymComplexSparse<T, Prop, Storage,
161  Allocator>& A);
162 
163  // Destructor.
165  void Clear();
166 
167  // Memory management.
168  template <class Storage0, class Allocator0,
169  class Storage1, class Allocator1,
170  class Storage2, class Allocator2>
171  void SetData(int i, int j,
178  void SetData(int i, int j,
179  long real_nz, pointer real_values, long* real_ptr,
180  int* real_ind,
181  long imag_nz, pointer imag_values, long* imag_ptr,
182  int* imag_ind);
183  void Nullify();
184  void Reallocate(int i, int j);
185  void Reallocate(int i, int j, long real_nz, long imag_nz);
186  void Resize(int i, int j);
187  void Resize(int i, int j, long real_nz, long imag_nz);
189 
190  // Basic methods.
191  long GetNonZeros() const;
192  long GetDataSize() const;
193  size_t GetMemorySize() const;
194  long* GetRealPtr() const;
195  long* GetImagPtr() const;
196  int* GetRealInd() const;
197  int* GetImagInd() const;
198  int GetRealPtrSize() const;
199  int GetImagPtrSize() const;
200  long GetRealIndSize() const;
201  long GetImagIndSize() const;
202  long GetRealDataSize() const;
203  long GetImagDataSize() const;
204  value_type* GetRealData() const;
205  value_type* GetImagData() const;
206 
207  // Element acess and affectation.
208  const complex<value_type> operator() (int i, int j) const;
209  value_type& ValReal(int i, int j);
210  const value_type& ValReal(int i, int j) const;
211  value_type& ValImag(int i, int j);
212  const value_type& ValImag(int i, int j) const;
213  value_type& GetReal(int i, int j);
214  const value_type& GetReal(int i, int j) const;
215  value_type& GetImag(int i, int j);
216  const value_type& GetImag(int i, int j) const;
217  void Set(int i, int j, const entry_type& x);
218  void AddInteraction(int i, int j, const entry_type& x);
219 
220  void AddInteractionRow(int i, int nb, const IVect& col,
221  const Vector<entry_type>& val,
222  bool sorted = false);
223 
226 
227  // Convenient functions.
228  void Zero();
229  void SetIdentity();
230  void Fill();
231  void Fill(const entry_type& x);
232  void FillRand();
233 
234  void Print() const;
235  void Write(string FileName) const;
236  void Write(ostream& FileStream) const;
237  void WriteText(string FileName, bool cplx = false) const;
238  void WriteText(ostream& FileStream, bool cplx = false) const;
239  void Read(string FileName);
240  void Read(istream& FileStream);
241  void ReadText(string FileName, bool cplx = false);
242  void ReadText(istream& FileStream, bool cplx = false);
243 
244 #ifdef SELDON_WITH_VIRTUAL
245  typedef typename ClassComplexType<T>::Treal Treal;
246  typedef typename ClassComplexType<T>::Tcplx Tcplx;
247 
248  // methods used for iterative solvers
249  virtual void ApplySor(const SeldonTranspose&, Vector<Treal>& x, const Vector<Treal>& r,
250  const typename ClassComplexType<T>::Treal& omega,
251  int nb_iter, int stage_ssor) const;
252 
253  virtual void ApplySor(const SeldonTranspose&, Vector<Tcplx>& x, const Vector<Tcplx>& r,
254  const typename ClassComplexType<T>::Treal& omega,
255  int nb_iter, int stage_ssor) const;
256 
257  virtual void MltAddVector(const Treal& alpha, const Vector<Treal>& x,
258  const Treal& beta, Vector<Treal>& y) const;
259 
260  virtual void MltAddVector(const Tcplx& alpha, const Vector<Tcplx>& x,
261  const Tcplx& beta, Vector<Tcplx>& y) const;
262 
263  virtual void MltAddVector(const Treal& alpha, const SeldonTranspose&,
264  const Vector<Treal>& x,
265  const Treal& beta, Vector<Treal>& y) const;
266 
267  virtual void MltAddVector(const Tcplx& alpha, const SeldonTranspose&,
268  const Vector<Tcplx>& x,
269  const Tcplx& beta, Vector<Tcplx>& y) const;
270 
271  virtual void MltVector(const Vector<Treal>& x, Vector<Treal>& y) const;
272  virtual void MltVector(const Vector<Tcplx>& x, Vector<Tcplx>& y) const;
273 
274  virtual void MltVector(const SeldonTranspose&,
275  const Vector<Treal>& x, Vector<Treal>& y) const;
276 
277  virtual void MltVector(const SeldonTranspose&,
278  const Vector<Tcplx>& x, Vector<Tcplx>& y) const;
279 
280  virtual bool IsSymmetric() const;
281 #endif
282 
283  };
284 
285 
287  template <class T, class Prop, class Allocator>
288  class Matrix<T, Prop, ColSymComplexSparse, Allocator>:
289  public Matrix_SymComplexSparse<T, Prop, ColSymComplexSparse, Allocator>
290  {
291  // typedef declaration.
292  public:
293  typedef typename Allocator::value_type value_type;
294  typedef Prop property;
296  typedef Allocator allocator;
297 
298  public:
299  Matrix();
300  explicit Matrix(int i, int j);
301  Matrix(int i, int j, long real_nz, long imag_nz);
302  template <class Storage0, class Allocator0,
303  class Storage1, class Allocator1,
304  class Storage2, class Allocator2>
305  Matrix(int i, int j,
312  };
313 
314 
316  template <class T, class Prop, class Allocator>
317  class Matrix<T, Prop, RowSymComplexSparse, Allocator>:
318  public Matrix_SymComplexSparse<T, Prop, RowSymComplexSparse, Allocator>
319  {
320  // typedef declaration.
321  public:
322  typedef typename Allocator::value_type value_type;
323  typedef Prop property;
325  typedef Allocator allocator;
326 
327  public:
328  Matrix();
329  explicit Matrix(int i, int j);
330  Matrix(int i, int j, long real_nz, long imag_nz);
331  template <class Storage0, class Allocator0,
332  class Storage1, class Allocator1,
333  class Storage2, class Allocator2>
334  Matrix(int i, int j,
341  };
342 
343 } // namespace Seldon.
344 
345 #define SELDON_FILE_MATRIX_SYMCOMPLEXSPARSE_HXX
346 #endif
Seldon::SeldonDefaultAllocator
Selection of default allocator depending on storage and value type.
Definition: Allocator.hxx:174
Seldon::Matrix_SymComplexSparse::Zero
void Zero()
Resets all non-zero entries to 0-value.
Definition: Matrix_SymComplexSparse.cxx:2259
Seldon::SeldonTranspose
Definition: MatrixFlag.hxx:32
Seldon::Matrix_SymComplexSparse::WriteText
void WriteText(string FileName, bool cplx=false) const
Writes the matrix in a file.
Definition: Matrix_SymComplexSparse.cxx:2443
Seldon::Matrix_Base
Base class for all matrices.
Definition: Matrix_Base.hxx:143
Seldon::Matrix_SymComplexSparse::AddInteractionRow
void AddInteractionRow(int i, int nb, const IVect &col, const Vector< entry_type > &val, bool sorted=false)
Adds values to several non-zero entries on a given row.
Definition: Matrix_SymComplexSparseInline.cxx:329
Seldon::Matrix_SymComplexSparse::GetRealInd
int * GetRealInd() const
Returns (row or column) indices of non-zero entries for the real part.
Definition: Matrix_SymComplexSparseInline.cxx:113
Seldon::Vector
Definition: SeldonHeader.hxx:207
Seldon::Matrix_SymComplexSparse::ValImag
value_type & ValImag(int i, int j)
Access method.
Definition: Matrix_SymComplexSparse.cxx:1985
Seldon::Matrix_SymComplexSparse::GetRealPtr
long * GetRealPtr() const
Returns (row or column) start indices for the real part.
Definition: Matrix_SymComplexSparseInline.cxx:84
Seldon::Matrix
Definition: SeldonHeader.hxx:226
Seldon::Matrix_SymComplexSparse::GetNonZeros
long GetNonZeros() const
Returns the number of elements stored in memory.
Definition: Matrix_SymComplexSparseInline.cxx:56
Seldon::RowSymComplexSparse
Definition: Storage.hxx:436
Seldon::Matrix_SymComplexSparse::operator=
Matrix_SymComplexSparse< T, Prop, Storage, Allocator > & operator=(const Matrix_SymComplexSparse< T, Prop, Storage, Allocator > &A)
Duplicates a matrix (assignment operator).
Definition: Matrix_SymComplexSparseInline.cxx:360
Seldon::Matrix_SymComplexSparse::GetMemorySize
size_t GetMemorySize() const
returns size of A in bytes used to store the matrix
Definition: Matrix_SymComplexSparse.cxx:2247
Seldon::Matrix_SymComplexSparse::Fill
void Fill()
Fills the non-zero entries with 0, 1, 2, ...
Definition: Matrix_SymComplexSparse.cxx:2306
Seldon::Matrix_SymComplexSparse::GetDataSize
long GetDataSize() const
Returns the number of elements stored in memory.
Definition: Matrix_SymComplexSparseInline.cxx:71
Seldon::Matrix_SymComplexSparse
Symmetric complex sparse-matrix class.
Definition: Matrix_SymComplexSparse.hxx:113
Seldon::Matrix_SymComplexSparse::operator()
const complex< value_type > operator()(int i, int j) const
Access operator.
Definition: Matrix_SymComplexSparse.cxx:1770
Seldon::Matrix_SymComplexSparse::FillRand
void FillRand()
Fills the non-zero entries randomly.
Definition: Matrix_SymComplexSparse.cxx:2337
Seldon::Matrix_SymComplexSparse::ValReal
value_type & ValReal(int i, int j)
Access method.
Definition: Matrix_SymComplexSparse.cxx:1865
Seldon::Matrix_SymComplexSparse::GetReal
value_type & GetReal(int i, int j)
Access method.
Definition: Matrix_SymComplexSparse.cxx:2104
Seldon::Matrix_SymComplexSparse::GetImagIndSize
long GetImagIndSize() const
Returns the length of the array of (column or row) indices for the imaginary part.
Definition: Matrix_SymComplexSparseInline.cxx:194
Seldon::Matrix_SymComplexSparse::AddInteraction
void AddInteraction(int i, int j, const entry_type &x)
Add a value to a non-zero entry.
Definition: Matrix_SymComplexSparseInline.cxx:313
Seldon::Matrix_SymComplexSparse::GetRealIndSize
long GetRealIndSize() const
Returns the length of the array of (column or row) indices for the real part.
Definition: Matrix_SymComplexSparseInline.cxx:174
Seldon::Matrix_SymComplexSparse::GetImagPtr
long * GetImagPtr() const
Returns (row or column) start indices for the imaginary part.
Definition: Matrix_SymComplexSparseInline.cxx:97
Seldon::Matrix_SymComplexSparse::Copy
void Copy(const Matrix_SymComplexSparse< T, Prop, Storage, Allocator > &A)
Copies a matrix.
Definition: Matrix_SymComplexSparse.cxx:1408
Seldon::Matrix_SymComplexSparse::Read
void Read(string FileName)
Reads the matrix from a file.
Definition: Matrix_SymComplexSparse.cxx:2499
Seldon::Matrix_SymComplexSparse::Set
void Set(int i, int j, const entry_type &x)
Sets an element (i, j) to a value.
Definition: Matrix_SymComplexSparseInline.cxx:344
Seldon::Matrix_SymComplexSparse::Print
void Print() const
Displays the matrix on the standard output.
Definition: Matrix_SymComplexSparse.cxx:2357
Seldon::Matrix_SymComplexSparse::GetImagPtrSize
int GetImagPtrSize() const
Returns the length of the array of start indices for the imaginary part.
Definition: Matrix_SymComplexSparseInline.cxx:154
Seldon::Matrix_SymComplexSparse::GetRealPtrSize
int GetRealPtrSize() const
Returns the length of the array of start indices for the real part.
Definition: Matrix_SymComplexSparseInline.cxx:142
Seldon::Matrix_SymComplexSparse::SetData
void SetData(int i, int j, Vector< value_type, Storage0, Allocator0 > &real_values, Vector< long, Storage1, Allocator1 > &real_ptr, Vector< int, Storage2, Allocator2 > &real_ind, Vector< value_type, Storage0, Allocator0 > &imag_values, Vector< long, Storage1, Allocator1 > &imag_ptr, Vector< int, Storage2, Allocator2 > &imag_ind)
Redefines the matrix.
Definition: Matrix_SymComplexSparse.cxx:466
Seldon::Matrix_SymComplexSparse::Resize
void Resize(int i, int j)
Changing the number of rows and columns.
Definition: Matrix_SymComplexSparse.cxx:1097
Seldon::Matrix_SymComplexSparse::GetImagData
value_type * GetImagData() const
Returns the array of values of the imaginary part.
Definition: Matrix_SymComplexSparseInline.cxx:258
Seldon::Matrix_SymComplexSparse::GetImagInd
int * GetImagInd() const
Returns (row or column) indices of non-zero entries for the imaginary part.
Definition: Matrix_SymComplexSparseInline.cxx:130
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::Matrix_SymComplexSparse::GetImag
value_type & GetImag(int i, int j)
Access method.
Definition: Matrix_SymComplexSparse.cxx:2177
Seldon::Matrix_SymComplexSparse::Write
void Write(string FileName) const
Writes the matrix in a file.
Definition: Matrix_SymComplexSparse.cxx:2375
Seldon::Matrix_SymComplexSparse::SetIdentity
void SetIdentity()
Sets the matrix to identity.
Definition: Matrix_SymComplexSparse.cxx:2273
Seldon::Matrix_SymComplexSparse::GetRealDataSize
long GetRealDataSize() const
Returns the length of the array of (column or row) indices for the real part.
Definition: Matrix_SymComplexSparseInline.cxx:214
Seldon::Matrix_SymComplexSparse::Matrix_SymComplexSparse
Matrix_SymComplexSparse()
Default constructor.
Definition: Matrix_SymComplexSparse.cxx:39
Seldon::Matrix_SymComplexSparse::Clear
void Clear()
Clears the matrix.
Definition: Matrix_SymComplexSparse.cxx:317
Seldon::Matrix_SymComplexSparse::~Matrix_SymComplexSparse
~Matrix_SymComplexSparse()
Destructor.
Definition: Matrix_SymComplexSparseInline.cxx:36
Seldon::ColSymComplexSparse
Definition: Storage.hxx:426
Seldon::Matrix_SymComplexSparse::GetRealData
value_type * GetRealData() const
Returns the array of values of the real part.
Definition: Matrix_SymComplexSparseInline.cxx:246
Seldon::Matrix_SymComplexSparse::Nullify
void Nullify()
Clears the matrix without releasing memory.
Definition: Matrix_SymComplexSparse.cxx:671
Seldon::Matrix_SymComplexSparse::Reallocate
void Reallocate(int i, int j)
Initialization of an empty matrix i x j.
Definition: Matrix_SymComplexSparse.cxx:690
Seldon::Matrix_SymComplexSparse::ReadText
void ReadText(string FileName, bool cplx=false)
Reads the matrix from a file.
Definition: Matrix_SymComplexSparse.cxx:2573
Seldon::Matrix_SymComplexSparse::GetImagDataSize
long GetImagDataSize() const
Returns the length of the array of (column or row) indices for the imaginary part.
Definition: Matrix_SymComplexSparseInline.cxx:234