Matrix_ArraySparse.hxx
1 // Copyright (C) 2003-2011 Marc DuruflĂ©
2 // Copyright (C) 2001-2011 Vivien Mallet
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_ARRAY_SPARSE_HXX
24 
25 namespace Seldon
26 {
27 
29 
36  template <class T, class Prop, class Storage, class Allocator
37  = typename SeldonDefaultAllocator<Storage, T>::allocator>
39  {
40  // typedef declaration.
41  public:
42  typedef typename Allocator::value_type value_type;
43  typedef typename Allocator::pointer pointer;
44  typedef typename Allocator::const_pointer const_pointer;
45  typedef typename Allocator::reference reference;
46  typedef typename Allocator::const_reference const_reference;
47  typedef T entry_type;
48  typedef T& access_type;
49  typedef T const_access_type;
50 
51  // Attributes.
52  protected:
56 
57  public:
58  // Constructors. (inline)
60  explicit Matrix_ArraySparse(int i, int j);
61 
62  // Destructor. (inline)
64  void Clear();
65 
66  // Memory management.
67  void Reallocate(int i, int j);
68  void Resize(int i, int j);
69 
70  // Basic methods.
71  long GetNonZeros() const;
72  size_t GetMemorySize() const;
73 
74  // Inline methods.
75  long GetDataSize() const;
76  int* GetIndex(int i) const;
77  T* GetData(int i) const;
78 
80 
81  // Element acess and affectation. (inline)
82 #ifdef SELDON_WITH_MODIFIABLE_PARENTHESIS_OPERATOR
83  T& operator() (int i, int j);
84 #endif
85  const T operator() (int i, int j) const;
86  T& Get(int i, int j);
87  const T& Get(int i, int j) const;
88  T& Val(int i, int j);
89  const T& Val(int i, int j) const;
90  void Set(int i, int j, const T& x);
91  void SetEntry(int i, int j, const T& x);
92 
93  const T& Value(int num_row, int i) const;
94  T& Value(int num_row, int i);
95  int Index(int num_row, int i) const;
96  int& Index(int num_row, int i);
97 
99  void SetData(int, int, T*, int*);
100  void Nullify(int i);
101  void Nullify();
102 
103  // Convenient functions.
104  void Print() const;
105  void Assemble();
106  template<class T0>
107  void RemoveSmallEntry(const T0& epsilon);
108 
109  void SetIdentity();
110  void Zero();
111  void Fill();
112  template <class T0>
113  void Fill(const T0& x);
114  template <class T0>
116  void FillRand();
117 
118  // Input/output functions.
119  void Write(string FileName) const;
120  void Write(ostream& FileStream) const;
121  void WriteText(string FileName, bool cplx = false) const;
122  void WriteText(ostream& FileStream, bool cplx = false) const;
123  void Read(string FileName);
124  void Read(istream& FileStream);
125  void ReadText(string FileName, bool cplx = false);
126  void ReadText(istream& FileStream, bool cplx = false);
127 
128 #ifdef SELDON_WITH_VIRTUAL
129  // Inline virtual methods
130  typedef typename ClassComplexType<T>::Treal Treal;
131  typedef typename ClassComplexType<T>::Tcplx Tcplx;
132 
133  virtual void ApplySor(const SeldonTranspose&, Vector<Treal>& x, const Vector<Treal>& r,
134  const typename ClassComplexType<T>::Treal& omega,
135  int nb_iter, int stage_ssor) const;
136 
137  virtual void ApplySor(const SeldonTranspose&, Vector<Tcplx>& x, const Vector<Tcplx>& r,
138  const typename ClassComplexType<T>::Treal& omega,
139  int nb_iter, int stage_ssor) const;
140 
141  virtual void MltAddVector(const Treal& alpha, const Vector<Treal>& x,
142  const Treal& beta, Vector<Treal>& y) const;
143 
144  virtual void MltAddVector(const Tcplx& alpha, const Vector<Tcplx>& x,
145  const Tcplx& beta, Vector<Tcplx>& y) const;
146 
147  virtual void MltAddVector(const Treal& alpha, const SeldonTranspose&,
148  const Vector<Treal>& x,
149  const Treal& beta, Vector<Treal>& y) const;
150 
151  virtual void MltAddVector(const Tcplx& alpha, const SeldonTranspose&,
152  const Vector<Tcplx>& x,
153  const Tcplx& beta, Vector<Tcplx>& y) const;
154 
155  virtual void MltVector(const Vector<Treal>& x, Vector<Treal>& y) const;
156  virtual void MltVector(const Vector<Tcplx>& x, Vector<Tcplx>& y) const;
157 
158  virtual void MltVector(const SeldonTranspose&,
159  const Vector<Treal>& x, Vector<Treal>& y) const;
160 
161  virtual void MltVector(const SeldonTranspose&,
162  const Vector<Tcplx>& x, Vector<Tcplx>& y) const;
163 
164  virtual bool IsSymmetric() const;
165 #endif
166 
167  };
168 
169 
171  template <class T, class Prop, class Allocator>
172  class Matrix<T, Prop, ArrayColSparse, Allocator> :
173  public Matrix_ArraySparse<T, Prop, ArrayColSparse, Allocator>
174  {
175  // typedef declaration.
176  public:
177  typedef typename Allocator::value_type value_type;
178  typedef Prop property;
179  typedef ArrayColSparse storage;
180  typedef Allocator allocator;
181 
182  public:
183  Matrix();
184  explicit Matrix(int i, int j);
185 
186  // Memory management.
187  void ClearColumn(int i);
188  void ReallocateColumn(int i, int j);
189  void ResizeColumn(int i, int j);
190  void SwapColumn(int i, int i_);
191  void ReplaceIndexColumn(int i, IVect& new_index);
192 
193  int GetColumnSize(int i) const;
194  void PrintColumn(int i) const;
195  void AssembleColumn(int i);
196 
197  void AddInteraction(int i, int j, const T& val);
198 
199  void AddInteractionRow(int, int, int*, T*, bool already_sorted = false);
200  void AddInteractionColumn(int, int, int*, T*, bool already_sorted = false);
201 
202  void AddInteractionRow(int i, int nb, const Vector<int>& col,
203  const Vector<T>& val, bool already_sorted = false);
204 
205  void AddInteractionColumn(int i, int nb, const Vector<int>& row,
206  const Vector<T>& val,
207  bool already_sorted = false);
208 
209  };
210 
211 
213  template <class T, class Prop, class Allocator>
214  class Matrix<T, Prop, ArrayRowSparse, Allocator> :
215  public Matrix_ArraySparse<T, Prop, ArrayRowSparse, Allocator>
216  {
217  // typedef declaration.
218  public:
219  typedef typename Allocator::value_type value_type;
220  typedef Prop property;
221  typedef ArrayRowSparse storage;
222  typedef Allocator allocator;
223 
224  public:
225  Matrix();
226  explicit Matrix(int i, int j);
227 
228  // Memory management.
229  void ClearRow(int i);
230  void ReallocateRow(int i, int j);
231  void ResizeRow(int i, int j);
232  void SwapRow(int i, int i_);
233  void ReplaceIndexRow(int i, IVect& new_index);
234 
235  int GetRowSize(int i) const;
236  void PrintRow(int i) const;
237  void AssembleRow(int i);
238 
239  void AddInteraction(int i, int j, const T& val);
240 
241  void AddInteractionRow(int, int, int*, T*, bool already_sorted = false);
242  void AddInteractionColumn(int, int, int*, T*, bool already_sorted = false);
243 
244  void AddInteractionRow(int i, int nb, const Vector<int>& col,
245  const Vector<T>& val, bool already_sorted = false);
246 
247  void AddInteractionColumn(int i, int nb, const Vector<int>& row,
248  const Vector<T>& val, bool already_sorted = false);
249 
250  };
251 
253  template <class T, class Prop, class Allocator>
254  class Matrix<T, Prop, ArrayColSymSparse, Allocator>:
255  public Matrix_ArraySparse<T, Prop, ArrayColSymSparse, Allocator>
256  {
257  // typedef declaration.
258  public:
259  typedef typename Allocator::value_type value_type;
260  typedef Prop property;
261  typedef ArrayColSymSparse storage;
262  typedef Allocator allocator;
263 
264  public:
265  Matrix();
266  explicit Matrix(int i, int j);
267 
268  // access operator
269 #ifdef SELDON_WITH_MODIFIABLE_PARENTHESIS_OPERATOR
270  T& operator() (int i, int j);
271 #endif
272  const T operator() (int i, int j) const;
273 
274  T& Get(int i, int j);
275  const T& Get(int i, int j) const;
276  T& Val(int i, int j);
277  const T& Val(int i, int j) const;
278  void Set(int i, int j, const T& x);
279  void SetEntry(int i, int j, const T& x);
280 
281  // Memory management.
282  void ClearColumn(int i);
283  void ReallocateColumn(int i, int j);
284  void ResizeColumn(int i, int j);
285  void SwapColumn(int i, int i_);
286  void ReplaceIndexColumn(int i, IVect& new_index);
287 
288  int GetColumnSize(int i) const;
289  void PrintColumn(int i) const;
290  void AssembleColumn(int i);
291 
292  void AddInteraction(int i, int j, const T& val);
293 
294  void AddInteractionRow(int, int, int*, T*, bool already_sorted = false);
295  void AddInteractionColumn(int, int, int*, T*,
296  bool already_sorted = false);
297 
298  void AddInteractionRow(int i, int nb, const Vector<int>& col,
299  const Vector<T>& val,
300  bool already_sorted = false);
301 
302  void AddInteractionColumn(int i, int nb, const Vector<int>& row,
303  const Vector<T>& val,
304  bool already_sorted = false);
305 
306  };
307 
308 
310  template <class T, class Prop, class Allocator>
311  class Matrix<T, Prop, ArrayRowSymSparse, Allocator>:
312  public Matrix_ArraySparse<T, Prop, ArrayRowSymSparse, Allocator>
313  {
314  // typedef declaration.
315  public:
316  typedef typename Allocator::value_type value_type;
317  typedef Prop property;
318  typedef ArrayRowSymSparse storage;
319  typedef Allocator allocator;
320 
321  public:
322  Matrix();
323  explicit Matrix(int i, int j);
324 
325  // access operator
326 #ifdef SELDON_WITH_MODIFIABLE_PARENTHESIS_OPERATOR
327  T& operator() (int i, int j);
328 #endif
329  const T operator() (int i, int j) const;
330 
331  T& Get(int i, int j);
332  const T& Get(int i, int j) const;
333  T& Val(int i, int j);
334  const T& Val(int i, int j) const;
335  void Set(int i, int j, const T& x);
336  void SetEntry(int i, int j, const T& x);
337 
338  // Memory management.
339  void ClearRow(int i);
340  void ReallocateRow(int i, int j);
341  void ResizeRow(int i, int j);
342  void SwapRow(int i, int i_);
343  void ReplaceIndexRow(int i, IVect& new_index);
344 
345  int GetRowSize(int i) const;
346  void PrintRow(int i) const;
347  void AssembleRow(int i);
348 
349  void AddInteraction(int i, int j, const T& val);
350 
351  void AddInteractionRow(int, int, int*, T*, bool already_sorted = false);
352  void AddInteractionColumn(int, int, int*, T*, bool sorted = false);
353 
354  void AddInteractionRow(int i, int nb, const Vector<int>& col,
355  const Vector<T>& val, bool already_sorted = false);
356 
357  void AddInteractionColumn(int i, int nb, const Vector<int>& row,
358  const Vector<T>& val, bool already_sorted = false);
359 
360  };
361 
362 
363  template <class T, class Prop, class Allocator>
364  ostream& operator <<(ostream& out,
366 
367  template <class T, class Prop, class Allocator>
368  ostream& operator <<(ostream& out,
370 
371  template <class T, class Prop, class Allocator>
372  ostream& operator <<(ostream& out,
374 
375  template <class T, class Prop, class Allocator>
376  ostream& operator <<(ostream& out,
378 
379 } // namespace Seldon
380 
381 #define SELDON_FILE_MATRIX_ARRAY_SPARSE_HXX
382 #endif
Seldon::ArrayColSparse
Definition: Storage.hxx:128
Seldon::SeldonTranspose
Definition: MatrixFlag.hxx:32
Seldon::Matrix_ArraySparse::GetIndex
int * GetIndex(int i) const
Returns (row or column) indices of non-zero entries in row.
Definition: Matrix_ArraySparseInline.cxx:100
Seldon::Matrix_ArraySparse::Read
void Read(string FileName)
Reads the matrix from a file.
Definition: Matrix_ArraySparse.cxx:371
Seldon::Matrix_ArraySparse::Print
void Print() const
Displays the matrix on the standard output.
Definition: Matrix_ArraySparse.cxx:132
Seldon::Matrix_ArraySparse::SetEntry
void SetEntry(int i, int j, const T &x)
Sets an element of the matrix.
Definition: Matrix_ArraySparseInline.cxx:272
Seldon::Vector
Definition: SeldonHeader.hxx:207
Seldon::Matrix_ArraySparse::Clear
void Clear()
Clears the matrix.
Definition: Matrix_ArraySparseInline.cxx:66
Seldon::Matrix_ArraySparse::Val
T & Val(int i, int j)
Access method.
Definition: Matrix_ArraySparseInline.cxx:217
Seldon::Matrix
Definition: SeldonHeader.hxx:226
Seldon::Matrix_ArraySparse::Get
T & Get(int i, int j)
Access to element (i, j).
Definition: Matrix_ArraySparseInline.cxx:178
Seldon::Matrix_ArraySparse::SetIdentity
void SetIdentity()
Matrix is initialized to the identity matrix.
Definition: Matrix_ArraySparse.cxx:182
Seldon::Matrix_ArraySparse::SetData
void SetData(int, int, Vector< T, VectSparse, Allocator > *)
Redefines the matrix.
Definition: Matrix_ArraySparseInline.cxx:392
Seldon::ArrayRowSymSparse
Definition: Storage.hxx:132
Seldon::NewAlloc
Definition: Allocator.hxx:91
Seldon::Matrix_ArraySparse::~Matrix_ArraySparse
~Matrix_ArraySparse()
Destructor.
Definition: Matrix_ArraySparseInline.cxx:54
Seldon::Matrix_ArraySparse::GetMemorySize
size_t GetMemorySize() const
returns size of matrix in bytes
Definition: Matrix_ArraySparse.cxx:110
Seldon::VectFull
Definition: StorageInline.cxx:74
Seldon::Matrix_ArraySparse::RemoveSmallEntry
void RemoveSmallEntry(const T0 &epsilon)
Removes small coefficients from entries.
Definition: Matrix_ArraySparse.cxx:173
Seldon::Matrix_ArraySparse::Zero
void Zero()
Non-zero entries are set to 0 (but not removed).
Definition: Matrix_ArraySparse.cxx:197
Seldon::ArrayColSymSparse
Definition: Storage.hxx:136
Seldon::Matrix_ArraySparse::val_
Vector< Vector< T, VectSparse, Allocator >, VectFull, NewAlloc< Vector< T, VectSparse, Allocator > > > val_
rows or columns
Definition: Matrix_ArraySparse.hxx:55
Seldon::Matrix_ArraySparse::operator()
const T operator()(int i, int j) const
Access operator.
Definition: Matrix_ArraySparseInline.cxx:157
Seldon::Matrix_ArraySparse::Value
const T & Value(int num_row, int i) const
Returns j-th non-zero value of row/column i.
Definition: Matrix_ArraySparseInline.cxx:286
Seldon::Matrix_ArraySparse::WriteText
void WriteText(string FileName, bool cplx=false) const
Writes the matrix in a file.
Definition: Matrix_ArraySparse.cxx:314
Seldon::Matrix_ArraySparse::Fill
void Fill()
Non-zero entries are filled with values 0, 1, 2, 3 ...
Definition: Matrix_ArraySparse.cxx:206
Seldon::Matrix< T, Prop, ArrayRowSparse, Allocator >
Row-major sparse-matrix class.
Definition: Matrix_ArraySparse.hxx:214
Seldon::Matrix_ArraySparse::GetDataSize
long GetDataSize() const
Returns the number of elements stored in memory.
Definition: Matrix_ArraySparseInline.cxx:86
Seldon::Matrix_ArraySparse::Resize
void Resize(int i, int j)
Reallocates additional memory to resize the matrix.
Definition: Matrix_ArraySparse.cxx:63
Seldon::Matrix_ArraySparse::GetNonZeros
long GetNonZeros() const
Returns the number of non-zero entries.
Definition: Matrix_ArraySparse.cxx:97
Seldon::Matrix_ArraySparse::Index
int Index(int num_row, int i) const
Returns column/row number of j-th non-zero value of row/column i.
Definition: Matrix_ArraySparseInline.cxx:325
Seldon::Matrix< T, Prop, ArrayColSymSparse, Allocator >
Column-major symmetric sparse-matrix class.
Definition: Matrix_ArraySparse.hxx:254
Seldon::Matrix_ArraySparse::operator=
Matrix_ArraySparse< T, Prop, Storage, Allocator > & operator=(const T0 &x)
Non-zero entries are set to a given value x.
Definition: Matrix_ArraySparse.cxx:231
Seldon::Matrix_ArraySparse::Set
void Set(int i, int j, const T &x)
Sets an element of the matrix.
Definition: Matrix_ArraySparseInline.cxx:258
Seldon::Vector< T, VectSparse, Allocator >
Sparse vector class.
Definition: SparseVector.hxx:29
Seldon::Matrix_ArraySparse::FillRand
void FillRand()
Non-zero entries take a random value.
Definition: Matrix_ArraySparse.cxx:239
Seldon::Matrix_ArraySparse::ReadText
void ReadText(string FileName, bool cplx=false)
Reads the matrix from a file.
Definition: Matrix_ArraySparse.cxx:437
Seldon::Matrix_ArraySparse
Sparse Array-matrix class.
Definition: Matrix_ArraySparse.hxx:38
Seldon::Matrix_ArraySparse::Reallocate
void Reallocate(int i, int j)
Reallocates memory to resize the matrix.
Definition: Matrix_ArraySparse.cxx:42
Seldon::ArrayRowSparse
Definition: Storage.hxx:124
Seldon::Matrix_ArraySparse::GetData
Vector< T, VectSparse, Allocator > * GetData() const
Returns values of non-zero entries.
Definition: Matrix_ArraySparseInline.cxx:127
Seldon::Matrix_ArraySparse::Matrix_ArraySparse
Matrix_ArraySparse()
Default constructor.
Definition: Matrix_ArraySparseInline.cxx:32
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::Matrix< T, Prop, ArrayRowSymSparse, Allocator >
Row-major symmetric sparse-matrix class.
Definition: Matrix_ArraySparse.hxx:311
Seldon::operator<<
ostream & operator<<(ostream &out, const Array< T, N, Allocator > &A)
operator<< overloaded for a 3D array.
Definition: Array.cxx:1617
Seldon::Matrix_ArraySparse::Write
void Write(string FileName) const
Writes the matrix in a file.
Definition: Matrix_ArraySparse.cxx:259
Seldon::Matrix< T, Prop, ArrayColSparse, Allocator >
Column-major sparse-matrix class.
Definition: Matrix_ArraySparse.hxx:172
Seldon::Matrix_ArraySparse::Nullify
void Nullify()
Clears the matrix without releasing memory.
Definition: Matrix_ArraySparseInline.cxx:406
Seldon::VirtualMatrix
Abstract base class for all matrices.
Definition: Matrix_Base.hxx:42
Seldon::Matrix_ArraySparse::Assemble
void Assemble()
Assembles the matrix.
Definition: Matrix_ArraySparse.cxx:159