Matrix_SymSparse.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_SYMSPARSE_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 
44  template <class T, class Prop, class Storage, class Allocator
45  = typename SeldonDefaultAllocator<Storage, T>::allocator>
46  class Matrix_SymSparse: public Matrix_Base<T, Allocator>
47  {
48  // typedef declaration.
49  public:
50  typedef typename Allocator::value_type value_type;
51  typedef typename Allocator::pointer pointer;
52  typedef typename Allocator::const_pointer const_pointer;
53  typedef typename Allocator::reference reference;
54  typedef typename Allocator::const_reference const_reference;
55  typedef typename Allocator::value_type entry_type;
56  typedef typename Allocator::value_type access_type;
57  typedef typename Allocator::value_type const_access_type;
58  typedef typename SeldonDefaultAllocator<VectFull, int>::allocator AllocatorInt;
59  typedef typename SeldonDefaultAllocator<VectFull, long>::allocator AllocatorLong;
60 
61  // Attributes.
62  protected:
63  // Number of non-zero (stored) elements.
64  long nz_;
65  // Index (in data_) of first element stored for each row or column.
66  long* ptr_;
67  // Column or row index (in the matrix) each element.
68  int* ind_;
69 
70  // Methods.
71  public:
72  // Constructors.
74  Matrix_SymSparse(int i, int j);
75  Matrix_SymSparse(int i, int j, long nz);
76  template <class Storage0, class Allocator0,
77  class Storage1, class Allocator1,
78  class Storage2, class Allocator2>
83 
84  // Destructor.
86  void Clear();
87 
88  // Memory management.
89  template <class Storage0, class Allocator0,
90  class Storage1, class Allocator1,
91  class Storage2, class Allocator2>
92  void SetData(int i, int j,
96  void SetData(int i, int j, long nz, pointer values, long* ptr, int* ind);
97  void Nullify();
98  void Reallocate(int i, int j);
99  void Reallocate(int i, int j, long nz);
100  void Resize(int i, int j);
101  void Resize(int i, int j, long nz);
103 
104  // Basic methods.
105  long GetNonZeros() const;
106  long GetDataSize() const;
107  size_t GetMemorySize() const;
108  long* GetPtr() const;
109  int* GetInd() const;
110  int GetPtrSize() const;
111  long GetIndSize() const;
112  void FillPtrInt(Vector<int>&) const;
113 
114  // Element acess and affectation.
115  const value_type operator() (int i, int j) const;
116  value_type& Val(int i, int j);
117  value_type& Get(int i, int j);
118  const value_type& Val(int i, int j) const;
119  const value_type& Get(int i, int j) const;
120  void Set(int i, int j, const T& x);
121  void SetEntry(int i, int j, const T& x);
122 
123  void AddInteraction(int i, int j, const T& x);
124 
125  void AddInteractionRow(int i, int nb, const Vector<int>& col,
126  const Vector<T>& val, bool sorted);
127 
130 
131  // Convenient functions.
132  void Zero();
133  void SetIdentity();
134  void Fill();
135  template <class T0>
136  void Fill(const T0& x);
137  void FillRand();
138 
139  void Print() const;
140  void Write(string FileName) const;
141  void Write(ostream& FileStream) const;
142  void WriteText(string FileName, bool cplx = false) const;
143  void WriteText(ostream& FileStream, bool cplx = false) const;
144  void Read(string FileName);
145  void Read(istream& FileStream);
146  void ReadText(string FileName, bool cplx = false);
147  void ReadText(istream& FileStream, bool cplx = false);
148 
149 #ifdef SELDON_WITH_VIRTUAL
150  typedef typename ClassComplexType<T>::Treal Treal;
151  typedef typename ClassComplexType<T>::Tcplx Tcplx;
152 
153  virtual void ApplySor(const SeldonTranspose&, Vector<Treal>& x, const Vector<Treal>& r,
154  const typename ClassComplexType<T>::Treal& omega,
155  int nb_iter, int stage_ssor) const;
156 
157  virtual void ApplySor(const SeldonTranspose&, Vector<Tcplx>& x, const Vector<Tcplx>& r,
158  const typename ClassComplexType<T>::Treal& omega,
159  int nb_iter, int stage_ssor) const;
160 
161  virtual void MltAddVector(const Treal& alpha, const Vector<Treal>& x,
162  const Treal& beta, Vector<Treal>& y) const;
163 
164  virtual void MltAddVector(const Tcplx& alpha, const Vector<Tcplx>& x,
165  const Tcplx& beta, Vector<Tcplx>& y) const;
166 
167  virtual void MltAddVector(const Treal& alpha, const SeldonTranspose&,
168  const Vector<Treal>& x,
169  const Treal& beta, Vector<Treal>& y) const;
170 
171  virtual void MltAddVector(const Tcplx& alpha, const SeldonTranspose&,
172  const Vector<Tcplx>& x,
173  const Tcplx& beta, Vector<Tcplx>& y) const;
174 
175  virtual void MltVector(const Vector<Treal>& x, Vector<Treal>& y) const;
176  virtual void MltVector(const Vector<Tcplx>& x, Vector<Tcplx>& y) const;
177 
178  virtual void MltVector(const SeldonTranspose&,
179  const Vector<Treal>& x, Vector<Treal>& y) const;
180 
181  virtual void MltVector(const SeldonTranspose&,
182  const Vector<Tcplx>& x, Vector<Tcplx>& y) const;
183 
184  virtual bool IsSymmetric() const;
185 #endif
186 
187  };
188 
189 
191  template <class T, class Prop, class Allocator>
192  class Matrix<T, Prop, ColSymSparse, Allocator>:
193  public Matrix_SymSparse<T, Prop, ColSymSparse, Allocator>
194  {
195  // typedef declaration.
196  public:
197  typedef typename Allocator::value_type value_type;
198  typedef Prop property;
199  typedef ColSymSparse storage;
200  typedef Allocator allocator;
201 
202  public:
203  Matrix();
204  explicit Matrix(int i, int j);
205  explicit Matrix(int i, int j, long nz);
206  template <class Storage0, class Allocator0,
207  class Storage1, class Allocator1,
208  class Storage2, class Allocator2>
209  Matrix(int i, int j,
213  };
214 
215 
217  template <class T, class Prop, class Allocator>
218  class Matrix<T, Prop, RowSymSparse, Allocator>:
219  public Matrix_SymSparse<T, Prop, RowSymSparse, Allocator>
220  {
221  // typedef declaration.
222  public:
223  typedef typename Allocator::value_type value_type;
224  typedef Prop property;
225  typedef RowSymSparse storage;
226  typedef Allocator allocator;
227 
228  public:
229  Matrix();
230  explicit Matrix(int i, int j);
231  explicit Matrix(int i, int j, long nz);
232  template <class Storage0, class Allocator0,
233  class Storage1, class Allocator1,
234  class Storage2, class Allocator2>
235  Matrix(int i, int j,
239  };
240 
241 } // namespace Seldon.
242 
243 #define SELDON_FILE_MATRIX_SYMSPARSE_HXX
244 #endif
Seldon::Matrix_SymSparse::operator()
const value_type operator()(int i, int j) const
Access operator.
Definition: Matrix_SymSparse.cxx:880
Seldon::SeldonTranspose
Definition: MatrixFlag.hxx:32
Seldon::Matrix_SymSparse::SetIdentity
void SetIdentity()
Sets the matrix to identity.
Definition: Matrix_SymSparse.cxx:1132
Seldon::Matrix_SymSparse::GetInd
int * GetInd() const
Returns (row or column) indices of non-zero entries.
Definition: Matrix_SymSparseInline.cxx:153
Seldon::Matrix_SymSparse::GetMemorySize
size_t GetMemorySize() const
returns size of matrix in bytes
Definition: Matrix_SymSparse.cxx:846
Seldon::Matrix_Base
Base class for all matrices.
Definition: Matrix_Base.hxx:143
Seldon::Matrix_SymSparse::operator=
Matrix_SymSparse< T, Prop, Storage, Allocator > & operator=(const Matrix_SymSparse< T, Prop, Storage, Allocator > &A)
Duplicates a matrix (assignment operator).
Definition: Matrix_SymSparseInline.cxx:270
Seldon::Matrix_SymSparse::Fill
void Fill()
Fills the non-zero entries with 0, 1, 2, ...
Definition: Matrix_SymSparse.cxx:1161
Seldon::Vector
Definition: SeldonHeader.hxx:207
Seldon::Matrix_SymSparse::GetPtrSize
int GetPtrSize() const
Returns the length of the array of start indices.
Definition: Matrix_SymSparseInline.cxx:164
Seldon::Matrix
Definition: SeldonHeader.hxx:226
Seldon::Matrix_SymSparse::SetEntry
void SetEntry(int i, int j, const T &x)
Sets an element (i, j) to a value.
Definition: Matrix_SymSparseInline.cxx:255
Seldon::Matrix_SymSparse
Symmetric sparse-matrix class.
Definition: Matrix_SymSparse.hxx:46
Seldon::Matrix_SymSparse::Write
void Write(string FileName) const
Writes the matrix in a file.
Definition: Matrix_SymSparse.cxx:1223
Seldon::Matrix_SymSparse::GetPtr
long * GetPtr() const
Returns (row or column) start indices.
Definition: Matrix_SymSparseInline.cxx:138
Seldon::Matrix_SymSparse::GetDataSize
long GetDataSize() const
Returns the number of elements stored in memory.
Definition: Matrix_SymSparseInline.cxx:126
Seldon::Matrix_SymSparse::Reallocate
void Reallocate(int i, int j)
Initialization of an empty sparse matrix with i rows and j columns.
Definition: Matrix_SymSparse.cxx:356
Seldon::Matrix_SymSparse::Nullify
void Nullify()
Clears the matrix without releasing memory.
Definition: Matrix_SymSparse.cxx:339
Seldon::Matrix_SymSparse::Zero
void Zero()
Resets all non-zero entries to 0-value.
Definition: Matrix_SymSparse.cxx:1121
Seldon::ColSymSparse
Definition: Storage.hxx:103
Seldon::Matrix_SymSparse::~Matrix_SymSparse
~Matrix_SymSparse()
Destructor.
Definition: Matrix_SymSparseInline.cxx:97
Seldon::Matrix_SymSparse::Print
void Print() const
Displays the matrix on the standard output.
Definition: Matrix_SymSparse.cxx:1205
Seldon::Matrix_SymSparse::FillRand
void FillRand()
Fills the non-zero entries randomly.
Definition: Matrix_SymSparse.cxx:1188
Seldon::Matrix_SymSparse::Copy
void Copy(const Matrix_SymSparse< T, Prop, Storage, Allocator > &A)
Copies a matrix.
Definition: Matrix_SymSparse.cxx:698
Seldon::Matrix_SymSparse::ReadText
void ReadText(string FileName, bool cplx=false)
Reads the matrix from a file.
Definition: Matrix_SymSparse.cxx:1411
Seldon::Matrix_SymSparse::Set
void Set(int i, int j, const T &x)
Sets an element (i, j) to a value.
Definition: Matrix_SymSparseInline.cxx:241
Seldon::Matrix_SymSparse::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_SymSparse.cxx:226
Seldon::Matrix_SymSparse::WriteText
void WriteText(string FileName, bool cplx=false) const
Writes the matrix in a file.
Definition: Matrix_SymSparse.cxx:1284
Seldon::Matrix_SymSparse::FillPtrInt
void FillPtrInt(Vector< int > &) const
Fills vector ptr with integers instead of longs.
Definition: Matrix_SymSparse.cxx:857
Seldon::Matrix_SymSparse::AddInteraction
void AddInteraction(int i, int j, const T &x)
Add a value to a non-zero entry.
Definition: Matrix_SymSparseInline.cxx:216
Seldon::Matrix_SymSparse::Val
value_type & Val(int i, int j)
Access method.
Definition: Matrix_SymSparse.cxx:936
Seldon::Matrix_SymSparse::GetNonZeros
long GetNonZeros() const
Returns the number of elements stored in memory.
Definition: Matrix_SymSparseInline.cxx:114
Seldon::Matrix_SymSparse::Matrix_SymSparse
Matrix_SymSparse()
Default constructor.
Definition: Matrix_SymSparseInline.cxx:39
Seldon::Matrix_SymSparse::Clear
void Clear()
Clears the matrix.
Definition: Matrix_SymSparse.cxx:143
Seldon::RowSymSparse
Definition: Storage.hxx:114
Seldon::Matrix_SymSparse::GetIndSize
long GetIndSize() const
Returns the length of the array of (column or row) indices.
Definition: Matrix_SymSparseInline.cxx:181
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::Matrix_SymSparse::Read
void Read(string FileName)
Reads the matrix from a file.
Definition: Matrix_SymSparse.cxx:1342
Seldon::Matrix_SymSparse::Get
value_type & Get(int i, int j)
Access method.
Definition: Matrix_SymSparse.cxx:1053
Seldon::Matrix_SymSparse::Resize
void Resize(int i, int j)
Changing the number of rows and columns.
Definition: Matrix_SymSparse.cxx:561
Seldon::Matrix_SymSparse::AddInteractionRow
void AddInteractionRow(int i, int nb, const Vector< int > &col, const Vector< T > &val, bool sorted)
Adds values to several non-zero entries on a given row.
Definition: Matrix_SymSparseInline.cxx:226