Matrix_Sparse.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_SPARSE_HXX
24 
25 #include "../share/Common.hxx"
26 #include "../share/Properties.hxx"
27 #include "../share/Storage.hxx"
28 #include "../share/Errors.hxx"
29 #include "../share/Allocator.hxx"
30 
31 namespace Seldon
32 {
33 
34 
36 
43  template <class T, class Prop, class Storage, class Allocator
44  = typename SeldonDefaultAllocator<Storage, T>::allocator>
45  class Matrix_Sparse: public Matrix_Base<T, Allocator>
46  {
47  // typedef declaration.
48  public:
49  typedef typename Allocator::value_type value_type;
50  typedef typename Allocator::pointer pointer;
51  typedef typename Allocator::const_pointer const_pointer;
52  typedef typename Allocator::reference reference;
53  typedef typename Allocator::const_reference const_reference;
54  typedef typename SeldonDefaultAllocator<VectFull, int>::allocator AllocatorInt;
55  typedef typename SeldonDefaultAllocator<VectFull, long>::allocator AllocatorLong;
56  typedef value_type entry_type;
57  typedef value_type access_type;
58  typedef value_type const_access_type;
59 
60  // Attributes.
61  protected:
62  // Number of non-zero elements.
63  long nz_;
64  // Index (in data_) of first element stored for each row or column.
65  long* ptr_;
66  // Column or row index (in the matrix) each element.
67  int* ind_;
68 
69  // Methods.
70  public:
71  // Constructors.
72  Matrix_Sparse();
73  explicit Matrix_Sparse(int i, int j);
74  explicit Matrix_Sparse(int i, int j, long nz);
75  template <class Storage0, class Allocator0,
76  class Storage1, class Allocator1,
77  class Storage2, class Allocator2>
78  Matrix_Sparse(int i, int j, Vector<T, Storage0, Allocator0>& values,
82 
83  // Destructor.
85  void Clear();
86 
87  // Memory management.
88  template <class Storage0, class Allocator0,
89  class Storage1, class Allocator1,
90  class Storage2, class Allocator2>
91  void SetData(int i, int j,
95  void SetData(int i, int j, long nz, pointer values, long* ptr, int* ind);
96  void Nullify();
97  void Reallocate(int i, int j);
98  void Reallocate(int i, int j, long nz);
99  void Resize(int i, int j);
100  void Resize(int i, int j, long nz);
102 
103  // Basic methods.
104  long GetNonZeros() const;
105  long GetDataSize() const;
106  size_t GetMemorySize() const;
107  long* GetPtr() const;
108  int* GetInd() const;
109  int GetPtrSize() const;
110  long GetIndSize() const;
111  void FillPtrInt(Vector<int>&) const;
112 
113  // Element acess and affectation.
114  const value_type operator() (int i, int j) const;
115  value_type& Val(int i, int j);
116  value_type& Get(int i, int j);
117 #ifndef SWIG
118  const value_type& Val(int i, int j) const;
119  const value_type& Get(int i, int j) const;
120 #endif
121  void AddInteraction(int i, int j, const T& val);
122 
123  void AddInteractionRow(int i, int nb, const Vector<int>& col,
124  const Vector<T>& val, bool sorted = false);
125 
126  void Set(int i, int j, const T& x);
127  void SetEntry(int i, int j, const T& x);
128 
129 #ifndef SWIG
132 #endif
133 
134  // Convenient functions.
135  void Zero();
136  void SetIdentity();
137  void Fill();
138  template <class T0>
139  void Fill(const T0& x);
140  void FillRand();
141  void FillRand(long Nelement);
142  void FillRand(long Nelement, const T& x);
143 
144  void Print() const;
145  void Write(string FileName) const;
146  void Write(ostream& FileStream) const;
147  void WriteText(string FileName, bool cplx = false) const;
148  void WriteText(ostream& FileStream, bool cplx = false) const;
149  void Read(string FileName);
150  void Read(istream& FileStream);
151  void ReadText(string FileName, bool cplx = false);
152  void ReadText(istream& FileStream, bool cplx = false);
153 
154 #ifdef SELDON_WITH_VIRTUAL
155  typedef typename ClassComplexType<T>::Treal Treal;
156  typedef typename ClassComplexType<T>::Tcplx Tcplx;
157 
158  virtual void ApplySor(const SeldonTranspose&, Vector<Treal>& x, const Vector<Treal>& r,
159  const typename ClassComplexType<T>::Treal& omega,
160  int nb_iter, int stage_ssor) const;
161 
162  virtual void ApplySor(const SeldonTranspose&, Vector<Tcplx>& x, const Vector<Tcplx>& r,
163  const typename ClassComplexType<T>::Treal& omega,
164  int nb_iter, int stage_ssor) const;
165 
166  virtual void MltAddVector(const Treal& alpha, const Vector<Treal>& x,
167  const Treal& beta, Vector<Treal>& y) const;
168 
169  virtual void MltAddVector(const Tcplx& alpha, const Vector<Tcplx>& x,
170  const Tcplx& beta, Vector<Tcplx>& y) const;
171 
172  virtual void MltAddVector(const Treal& alpha, const SeldonTranspose&,
173  const Vector<Treal>& x,
174  const Treal& beta, Vector<Treal>& y) const;
175 
176  virtual void MltAddVector(const Tcplx& alpha, const SeldonTranspose&,
177  const Vector<Tcplx>& x,
178  const Tcplx& beta, Vector<Tcplx>& y) const;
179 
180  virtual void MltVector(const Vector<Treal>& x, Vector<Treal>& y) const;
181  virtual void MltVector(const Vector<Tcplx>& x, Vector<Tcplx>& y) const;
182 
183  virtual void MltVector(const SeldonTranspose&,
184  const Vector<Treal>& x, Vector<Treal>& y) const;
185 
186  virtual void MltVector(const SeldonTranspose&,
187  const Vector<Tcplx>& x, Vector<Tcplx>& y) const;
188 
189  virtual bool IsSymmetric() const;
190 #endif
191 
192  };
193 
194 
196  template <class T, class Prop, class Allocator>
197  class Matrix<T, Prop, ColSparse, Allocator>:
198  public Matrix_Sparse<T, Prop, ColSparse, Allocator>
199  {
200  // typedef declaration.
201  public:
202  typedef typename Allocator::value_type value_type;
203  typedef Prop property;
204  typedef ColSparse storage;
205  typedef Allocator allocator;
206 
207  public:
208  Matrix();
209  explicit Matrix(int i, int j);
210  explicit Matrix(int i, int j, long nz);
211  template <class Storage0, class Allocator0,
212  class Storage1, class Allocator1,
213  class Storage2, class Allocator2>
214  Matrix(int i, int j,
218  };
219 
220 
222  template <class T, class Prop, class Allocator>
223  class Matrix<T, Prop, RowSparse, Allocator>:
224  public Matrix_Sparse<T, Prop, RowSparse, Allocator>
225  {
226  // typedef declaration.
227  public:
228  typedef typename Allocator::value_type value_type;
229  typedef Prop property;
230  typedef RowSparse storage;
231  typedef Allocator allocator;
232 
233  public:
234  Matrix();
235  explicit Matrix(int i, int j);
236  explicit Matrix(int i, int j, long nz);
237  template <class Storage0, class Allocator0,
238  class Storage1, class Allocator1,
239  class Storage2, class Allocator2>
240  Matrix(int i, int j,
244 
245  };
246 
247 
248 } // namespace Seldon.
249 
250 #define SELDON_FILE_MATRIX_SPARSE_HXX
251 #endif
Seldon::Matrix_Sparse::operator=
Matrix_Sparse< T, Prop, Storage, Allocator > & operator=(const Matrix_Sparse< T, Prop, Storage, Allocator > &A)
Duplicates a matrix (assignment operator).
Definition: Matrix_SparseInline.cxx:275
Seldon::Matrix_Sparse::Fill
void Fill()
Fills the non-zero entries with 0, 1, 2, ...
Definition: Matrix_Sparse.cxx:1196
Seldon::SeldonTranspose
Definition: MatrixFlag.hxx:32
Seldon::Matrix_Sparse::Read
void Read(string FileName)
Reads the matrix from a file.
Definition: Matrix_Sparse.cxx:1465
Seldon::Matrix_Sparse::Write
void Write(string FileName) const
Writes the matrix in a file.
Definition: Matrix_Sparse.cxx:1344
Seldon::Matrix_Base
Base class for all matrices.
Definition: Matrix_Base.hxx:143
Seldon::Matrix_Sparse::FillPtrInt
void FillPtrInt(Vector< int > &) const
Fills vector ptr with integers instead of longs.
Definition: Matrix_Sparse.cxx:918
Seldon::Matrix_Sparse::Matrix_Sparse
Matrix_Sparse()
Default constructor.
Definition: Matrix_SparseInline.cxx:53
Seldon::ColSparse
Definition: Storage.hxx:79
Seldon::Matrix_Sparse::GetPtrSize
int GetPtrSize() const
Returns the length of the array of start indices.
Definition: Matrix_SparseInline.cxx:176
Seldon::Vector
Definition: SeldonHeader.hxx:207
Seldon::Matrix_Sparse::GetDataSize
long GetDataSize() const
Returns the number of elements stored in memory.
Definition: Matrix_SparseInline.cxx:138
Seldon::Matrix_Sparse::GetIndSize
long GetIndSize() const
Returns the length of the array of (column or row) indices.
Definition: Matrix_SparseInline.cxx:192
Seldon::Matrix_Sparse::~Matrix_Sparse
~Matrix_Sparse()
Destructor.
Definition: Matrix_SparseInline.cxx:109
Seldon::Matrix
Definition: SeldonHeader.hxx:226
Seldon::Matrix_Sparse::SetIdentity
void SetIdentity()
Sets the matrix to identity.
Definition: Matrix_Sparse.cxx:1162
Seldon::Matrix_Sparse::Val
value_type & Val(int i, int j)
Access method.
Definition: Matrix_Sparse.cxx:987
Seldon::Matrix_Sparse::Copy
void Copy(const Matrix_Sparse< T, Prop, Storage, Allocator > &A)
Copies a matrix.
Definition: Matrix_Sparse.cxx:750
Seldon::Matrix_Sparse::SetData
void SetData(int i, int j, Vector< T, Storage0, Allocator0 > &values, Vector< long, Storage1, Allocator1 > &ptr, Vector< int, Storage2, Allocator2 > &ind)
Redefines the matrix.
Definition: Matrix_Sparse.cxx:241
Seldon::Matrix_Sparse::GetNonZeros
long GetNonZeros() const
Returns the number of non-zero elements.
Definition: Matrix_SparseInline.cxx:125
Seldon::Matrix_Sparse::GetPtr
long * GetPtr() const
Returns (row or column) start indices.
Definition: Matrix_SparseInline.cxx:150
Seldon::Matrix_Sparse::Zero
void Zero()
Resets all non-zero entries to 0-value.
Definition: Matrix_Sparse.cxx:1150
Seldon::Matrix_Sparse::Print
void Print() const
Displays the matrix on the standard output.
Definition: Matrix_Sparse.cxx:1326
Seldon::Matrix_Sparse::FillRand
void FillRand()
Fills the non-zero entries randomly.
Definition: Matrix_Sparse.cxx:1223
Seldon::Matrix_Sparse::ReadText
void ReadText(string FileName, bool cplx=false)
Reads the matrix from a file.
Definition: Matrix_Sparse.cxx:1534
Seldon::Matrix_Sparse::Nullify
void Nullify()
Clears the matrix without releasing memory.
Definition: Matrix_Sparse.cxx:356
Seldon::Matrix_Sparse::Reallocate
void Reallocate(int i, int j)
Initialization of an empty sparse matrix with i rows and j columns.
Definition: Matrix_Sparse.cxx:373
Seldon::Matrix_Sparse::AddInteraction
void AddInteraction(int i, int j, const T &val)
Add a value to a non-zero entry.
Definition: Matrix_SparseInline.cxx:222
Seldon::Matrix_Sparse
Sparse-matrix class.
Definition: Matrix_Sparse.hxx:45
Seldon::Matrix_Sparse::SetEntry
void SetEntry(int i, int j, const T &x)
Sets an element (i, j) to a value.
Definition: Matrix_SparseInline.cxx:260
Seldon::Matrix_Sparse::AddInteractionRow
void AddInteractionRow(int i, int nb, const Vector< int > &col, const Vector< T > &val, bool sorted=false)
Adds values to several non-zero entries on a given row.
Definition: Matrix_SparseInline.cxx:231
Seldon::Matrix_Sparse::Clear
void Clear()
Clears the matrix.
Definition: Matrix_Sparse.cxx:159
Seldon::Matrix_Sparse::WriteText
void WriteText(string FileName, bool cplx=false) const
Writes the matrix in a file.
Definition: Matrix_Sparse.cxx:1406
Seldon::RowSparse
Definition: Storage.hxx:91
Seldon::Matrix_Sparse::Set
void Set(int i, int j, const T &x)
Sets an element (i, j) to a value.
Definition: Matrix_SparseInline.cxx:246
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::Matrix_Sparse::GetMemorySize
size_t GetMemorySize() const
returns size of A in bytes used to store the matrix
Definition: Matrix_Sparse.cxx:907
Seldon::Matrix_Sparse::operator()
const value_type operator()(int i, int j) const
Access operator.
Definition: Matrix_Sparse.cxx:940
Seldon::Matrix_Sparse::GetInd
int * GetInd() const
Returns (row or column) indices of non-zero entries.
Definition: Matrix_SparseInline.cxx:165
Seldon::Matrix_Sparse::Resize
void Resize(int i, int j)
Reallocates memory to resize the matrix and keeps previous entries.
Definition: Matrix_Sparse.cxx:596
Seldon::Matrix_Sparse::Get
value_type & Get(int i, int j)
Access method.
Definition: Matrix_Sparse.cxx:1088