Matrix_ComplexSparse.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_COMPLEXSPARSE_HXX
24 
25 namespace Seldon
26 {
27 
29  template<class T>
31  {
32  public :
33  typedef typename
35  ::allocator allocator;
36  };
37 
38  template<>
39  class SeldonDefaultAllocator<ColComplexSparse, complex<float> >
40  {
41  public:
42  typedef
43  SeldonDefaultAllocator<VectFull, float>::allocator allocator;
44  };
45 
46  template<>
47  class SeldonDefaultAllocator<ColComplexSparse, complex<double> >
48  {
49  public:
50  typedef
51  SeldonDefaultAllocator<VectFull, double>::allocator allocator;
52  };
53 
54  template<>
55  class SeldonDefaultAllocator<ColComplexSparse, 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<>
74  class SeldonDefaultAllocator<RowComplexSparse, complex<float> >
75  {
76  public:
77  typedef
78  SeldonDefaultAllocator<VectFull, float>::allocator allocator;
79  };
80 
81  template<>
82  class SeldonDefaultAllocator<RowComplexSparse, complex<double> >
83  {
84  public:
85  typedef
86  SeldonDefaultAllocator<VectFull, double>::allocator allocator;
87  };
88 
89  template<>
90  class SeldonDefaultAllocator<RowComplexSparse, complex<long double> >
91  {
92  public:
93  typedef
94  SeldonDefaultAllocator<VectFull, long double>::allocator allocator;
95  };
96 
97 
99 
110  template <class T, class Prop, class Storage, class Allocator
111  = typename SeldonDefaultAllocator<Storage, T>::allocator>
112  class Matrix_ComplexSparse: public Matrix_Base<T, Allocator>
113  {
114  // typedef declaration.
115  public:
116  typedef typename Allocator::value_type value_type;
117  typedef typename Allocator::pointer pointer;
118  typedef typename Allocator::const_pointer const_pointer;
119  typedef typename Allocator::reference reference;
120  typedef typename Allocator::const_reference const_reference;
121  typedef typename SeldonDefaultAllocator<VectFull, int>::allocator AllocatorInt;
122  typedef typename SeldonDefaultAllocator<VectFull, long>::allocator AllocatorLong;
123  typedef complex<value_type> entry_type;
124  typedef complex<value_type> access_type;
125  typedef complex<value_type> const_access_type;
126 
127  // Attributes.
128  protected:
129  // Number of non-zero elements.
130  long real_nz_;
131  long imag_nz_;
132  // Index (in data_) of first element stored for each row or column.
133  long* real_ptr_;
134  long* imag_ptr_;
135  // Column or row index (in the matrix) each element.
136  int* real_ind_;
137  int* imag_ind_;
138 
139  // Data.
140  value_type* real_data_;
141  value_type* imag_data_;
142 
143  // Methods.
144  public:
145  // Constructors.
147  explicit Matrix_ComplexSparse(int i, int j);
148  Matrix_ComplexSparse(int i, int j, long real_nz, long imag_nz);
149 
150  template <class Storage0, class Allocator0,
151  class Storage1, class Allocator1,
152  class Storage2, class Allocator2>
153  Matrix_ComplexSparse(int i, int j,
160 
162  Storage, Allocator>& A);
163 
164  // Destructor.
166  void Clear();
167 
168  // Memory management.
169  template <class Storage0, class Allocator0,
170  class Storage1, class Allocator1,
171  class Storage2, class Allocator2>
172  void SetData(int i, int j,
179 
180  void SetData(int i, int j,
181  long real_nz, pointer real_values, long* real_ptr,
182  int* real_ind,
183  long imag_nz, pointer imag_values, long* imag_ptr,
184  int* imag_ind);
185  void Nullify();
186  void Reallocate(int i, int j);
187  void Reallocate(int i, int j, long real_nz, long imag_nz);
188  void Resize(int i, int j);
189  void Resize(int i, int j, long real_nz, long imag_nz);
191 
192  // Basic methods.
193  long GetNonZeros() const;
194  long GetDataSize() const;
195  size_t GetMemorySize() const;
196  long* GetRealPtr() const;
197  long* GetImagPtr() const;
198  int* GetRealInd() const;
199  int* GetImagInd() const;
200  int GetRealPtrSize() const;
201  int GetImagPtrSize() const;
202  long GetRealIndSize() const;
203  long GetImagIndSize() const;
204  long GetRealDataSize() const;
205  long GetImagDataSize() const;
206  value_type* GetRealData() const;
207  value_type* GetImagData() const;
208 
209  // Element acess and affectation.
210  const entry_type operator() (int i, int j) const;
211  value_type& ValReal(int i, int j);
212  const value_type& ValReal(int i, int j) const;
213  value_type& ValImag(int i, int j);
214  const value_type& ValImag(int i, int j) const;
215  value_type& GetReal(int i, int j);
216  const value_type& GetReal(int i, int j) const;
217  value_type& GetImag(int i, int j);
218  const value_type& GetImag(int i, int j) const;
219  void Set(int i, int j, const entry_type& x);
220  void AddInteraction(int i, int j, const entry_type& x);
221 
222  void AddInteractionRow(int i, int nb, const IVect& col,
223  const Vector<entry_type>& val,
224  bool sorted = false);
225 
228 
229  // Convenient functions.
230  void Zero();
231  void SetIdentity();
232  void Fill();
233  void Fill(const entry_type& x);
234  void FillRand();
235 
236  void Print() const;
237  void Write(string FileName) const;
238  void Write(ostream& FileStream) const;
239  void WriteText(string FileName, bool cplx = false) const;
240  void WriteText(ostream& FileStream, bool cplx = false) const;
241  void Read(string FileName);
242  void Read(istream& FileStream);
243  void ReadText(string FileName, bool cplx = false);
244  void ReadText(istream& FileStream, bool cplx = false);
245 
246 #ifdef SELDON_WITH_VIRTUAL
247  typedef typename ClassComplexType<T>::Treal Treal;
248  typedef typename ClassComplexType<T>::Tcplx Tcplx;
249 
250  virtual void ApplySor(const SeldonTranspose&, Vector<Treal>& x, const Vector<Treal>& r,
251  const typename ClassComplexType<T>::Treal& omega,
252  int nb_iter, int stage_ssor) const;
253 
254  virtual void ApplySor(const SeldonTranspose&, Vector<Tcplx>& x, const Vector<Tcplx>& r,
255  const typename ClassComplexType<T>::Treal& omega,
256  int nb_iter, int stage_ssor) const;
257 
258  virtual void MltAddVector(const Treal& alpha, const Vector<Treal>& x,
259  const Treal& beta, Vector<Treal>& y) const;
260 
261  virtual void MltAddVector(const Tcplx& alpha, const Vector<Tcplx>& x,
262  const Tcplx& beta, Vector<Tcplx>& y) const;
263 
264  virtual void MltAddVector(const Treal& alpha, const SeldonTranspose&,
265  const Vector<Treal>& x,
266  const Treal& beta, Vector<Treal>& y) const;
267 
268  virtual void MltAddVector(const Tcplx& alpha, const SeldonTranspose&,
269  const Vector<Tcplx>& x,
270  const Tcplx& beta, Vector<Tcplx>& y) const;
271 
272  virtual void MltVector(const Vector<Treal>& x, Vector<Treal>& y) const;
273  virtual void MltVector(const Vector<Tcplx>& x, Vector<Tcplx>& y) const;
274 
275  virtual void MltVector(const SeldonTranspose&,
276  const Vector<Treal>& x, Vector<Treal>& y) const;
277 
278  virtual void MltVector(const SeldonTranspose&,
279  const Vector<Tcplx>& x, Vector<Tcplx>& y) const;
280 
281  virtual bool IsSymmetric() const;
282 #endif
283 
284  };
285 
286 
288  template <class T, class Prop, class Allocator>
289  class Matrix<T, Prop, ColComplexSparse, Allocator>:
290  public Matrix_ComplexSparse<T, Prop, ColComplexSparse, Allocator>
291  {
292  // typedef declaration.
293  public:
294  typedef typename Allocator::value_type value_type;
295  typedef Prop property;
296  typedef ColComplexSparse storage;
297  typedef Allocator allocator;
298 
299  public:
300  Matrix();
301  explicit Matrix(int i, int j);
302  Matrix(int i, int j, long real_nz, long imag_nz);
303 
304  template <class Storage0, class Allocator0,
305  class Storage1, class Allocator1,
306  class Storage2, class Allocator2>
307  Matrix(int i, int j,
314  };
315 
316 
318  template <class T, class Prop, class Allocator>
319  class Matrix<T, Prop, RowComplexSparse, Allocator>:
320  public Matrix_ComplexSparse<T, Prop, RowComplexSparse, Allocator>
321  {
322  // typedef declaration.
323  public:
324  typedef typename Allocator::value_type value_type;
325  typedef Prop property;
326  typedef RowComplexSparse storage;
327  typedef Allocator allocator;
328 
329  public:
330  Matrix();
331  explicit Matrix(int i, int j);
332  Matrix(int i, int j, long real_nz, long imag_nz);
333  template <class Storage0, class Allocator0,
334  class Storage1, class Allocator1,
335  class Storage2, class Allocator2>
336  Matrix(int i, int j,
343  };
344 
345 } // namespace Seldon.
346 
347 #define SELDON_FILE_MATRIX_COMPLEXSPARSE_HXX
348 #endif
Seldon::Matrix_ComplexSparse::FillRand
void FillRand()
Fills the non-zero entries randomly.
Definition: Matrix_ComplexSparse.cxx:2401
Seldon::Matrix_ComplexSparse::GetImag
value_type & GetImag(int i, int j)
Access method.
Definition: Matrix_ComplexSparse.cxx:2255
Seldon::Matrix_ComplexSparse::GetRealIndSize
long GetRealIndSize() const
Returns the length of the array of (column or row) indices for the real part.
Definition: Matrix_ComplexSparseInline.cxx:167
Seldon::SeldonDefaultAllocator
Selection of default allocator depending on storage and value type.
Definition: Allocator.hxx:174
Seldon::Matrix_ComplexSparse
Complex sparse-matrix class.
Definition: Matrix_ComplexSparse.hxx:112
Seldon::Matrix_ComplexSparse::Zero
void Zero()
Resets all non-zero entries to 0-value.
Definition: Matrix_ComplexSparse.cxx:2316
Seldon::SeldonTranspose
Definition: MatrixFlag.hxx:32
Seldon::Matrix_ComplexSparse::~Matrix_ComplexSparse
~Matrix_ComplexSparse()
Destructor.
Definition: Matrix_ComplexSparseInline.cxx:36
Seldon::RowComplexSparse
Definition: Storage.hxx:416
Seldon::Matrix_Base
Base class for all matrices.
Definition: Matrix_Base.hxx:143
Seldon::Matrix_ComplexSparse::GetRealPtrSize
int GetRealPtrSize() const
Returns the length of the array of start indices for the real part.
Definition: Matrix_ComplexSparseInline.cxx:136
Seldon::Matrix_ComplexSparse::SetIdentity
void SetIdentity()
Sets the matrix to identity.
Definition: Matrix_ComplexSparse.cxx:2331
Seldon::Matrix_ComplexSparse::Reallocate
void Reallocate(int i, int j)
Initialization of an empty matrix i x j.
Definition: Matrix_ComplexSparse.cxx:717
Seldon::Matrix_ComplexSparse::GetImagInd
int * GetImagInd() const
Returns (row or column) indices of non-zero entries for the imaginary part.
Definition: Matrix_ComplexSparseInline.cxx:124
Seldon::Vector
Definition: SeldonHeader.hxx:207
Seldon::Matrix_ComplexSparse::Nullify
void Nullify()
Clears the matrix without releasing memory.
Definition: Matrix_ComplexSparse.cxx:698
Seldon::Matrix_ComplexSparse::ValReal
value_type & ValReal(int i, int j)
Access method.
Definition: Matrix_ComplexSparse.cxx:1983
Seldon::Matrix_ComplexSparse::GetImagData
value_type * GetImagData() const
Returns the array of values of the imaginary part.
Definition: Matrix_ComplexSparseInline.cxx:248
Seldon::Matrix
Definition: SeldonHeader.hxx:226
Seldon::Matrix_ComplexSparse::GetMemorySize
size_t GetMemorySize() const
returns size of A in bytes used to store the matrix
Definition: Matrix_ComplexSparse.cxx:450
Seldon::Matrix_ComplexSparse::Set
void Set(int i, int j, const entry_type &x)
Sets an element (i, j) to a value.
Definition: Matrix_ComplexSparseInline.cxx:331
Seldon::Matrix_ComplexSparse::ReadText
void ReadText(string FileName, bool cplx=false)
Reads the matrix from a file.
Definition: Matrix_ComplexSparse.cxx:2637
Seldon::Matrix_ComplexSparse::WriteText
void WriteText(string FileName, bool cplx=false) const
Writes the matrix in a file.
Definition: Matrix_ComplexSparse.cxx:2507
Seldon::Matrix_ComplexSparse::GetImagPtr
long * GetImagPtr() const
Returns (row or column) start indices for the imaginary part.
Definition: Matrix_ComplexSparseInline.cxx:93
Seldon::Matrix_ComplexSparse::Fill
void Fill()
Fills the non-zero entries with 0, 1, 2, ...
Definition: Matrix_ComplexSparse.cxx:2370
Seldon::ColComplexSparse
Definition: Storage.hxx:406
Seldon::Matrix_ComplexSparse::GetDataSize
long GetDataSize() const
Returns the number of elements stored in memory.
Definition: Matrix_ComplexSparseInline.cxx:69
Seldon::Matrix_ComplexSparse::GetRealDataSize
long GetRealDataSize() const
Returns the length of the array of (column or row) indices for the real part.
Definition: Matrix_ComplexSparseInline.cxx:205
Seldon::Matrix_ComplexSparse::operator()
const entry_type operator()(int i, int j) const
Access operator.
Definition: Matrix_ComplexSparse.cxx:1896
Seldon::Matrix_ComplexSparse::GetNonZeros
long GetNonZeros() const
Returns the number of elements stored in memory.
Definition: Matrix_ComplexSparseInline.cxx:55
Seldon::Matrix_ComplexSparse::Resize
void Resize(int i, int j)
Changing the number of rows and columns.
Definition: Matrix_ComplexSparse.cxx:1174
Seldon::Matrix_ComplexSparse::ValImag
value_type & ValImag(int i, int j)
Access method.
Definition: Matrix_ComplexSparse.cxx:2087
Seldon::Matrix_ComplexSparse::Matrix_ComplexSparse
Matrix_ComplexSparse()
Default constructor.
Definition: Matrix_ComplexSparse.cxx:40
Seldon::Matrix_ComplexSparse::GetImagPtrSize
int GetImagPtrSize() const
Returns the length of the array of start indices for the imaginary part.
Definition: Matrix_ComplexSparseInline.cxx:148
Seldon::Matrix_ComplexSparse::Clear
void Clear()
Clears the matrix.
Definition: Matrix_ComplexSparse.cxx:316
Seldon::Matrix_ComplexSparse::AddInteraction
void AddInteraction(int i, int j, const entry_type &x)
Add a value to a non-zero entry.
Definition: Matrix_ComplexSparseInline.cxx:303
Seldon::Matrix_ComplexSparse::GetImagIndSize
long GetImagIndSize() const
Returns the length of the array of (column or row) indices for the imaginary part.
Definition: Matrix_ComplexSparseInline.cxx:186
Seldon::Matrix_ComplexSparse::Copy
void Copy(const Matrix_ComplexSparse< T, Prop, Storage, Allocator > &A)
Copies a matrix.
Definition: Matrix_ComplexSparse.cxx:1500
Seldon::Matrix_ComplexSparse::GetRealData
value_type * GetRealData() const
Returns the array of values of the real part.
Definition: Matrix_ComplexSparseInline.cxx:236
Seldon::Matrix_ComplexSparse::GetRealPtr
long * GetRealPtr() const
Returns (row or column) start indices for the real part.
Definition: Matrix_ComplexSparseInline.cxx:81
Seldon::Matrix_ComplexSparse::Write
void Write(string FileName) const
Writes the matrix in a file.
Definition: Matrix_ComplexSparse.cxx:2439
Seldon::Matrix_ComplexSparse::Print
void Print() const
Displays the matrix on the standard output.
Definition: Matrix_ComplexSparse.cxx:2421
Seldon::Matrix_ComplexSparse::GetReal
value_type & GetReal(int i, int j)
Access method.
Definition: Matrix_ComplexSparse.cxx:2190
Seldon::Matrix_ComplexSparse::Read
void Read(string FileName)
Reads the matrix from a file.
Definition: Matrix_ComplexSparse.cxx:2563
Seldon::Matrix_ComplexSparse::GetRealInd
int * GetRealInd() const
Returns (row or column) indices of non-zero entries for the real part.
Definition: Matrix_ComplexSparseInline.cxx:108
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::Matrix_ComplexSparse::operator=
Matrix_ComplexSparse< T, Prop, Storage, Allocator > & operator=(const Matrix_ComplexSparse< T, Prop, Storage, Allocator > &A)
Duplicates a matrix (assignment operator).
Definition: Matrix_ComplexSparseInline.cxx:347
Seldon::Matrix_ComplexSparse::GetImagDataSize
long GetImagDataSize() const
Returns the length of the array of (column or row) indices for the imaginary part.
Definition: Matrix_ComplexSparseInline.cxx:224
Seldon::Matrix_ComplexSparse::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_ComplexSparse.cxx:487
Seldon::Matrix_ComplexSparse::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_ComplexSparseInline.cxx:316