Matrix_SymPacked.hxx
1 // Copyright (C) 2001-2009 Vivien Mallet
2 //
3 // This file is part of the linear-algebra library Seldon,
4 // http://seldon.sourceforge.net/.
5 //
6 // Seldon is free software; you can redistribute it and/or modify it under the
7 // terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 2.1 of the License, or (at your option)
9 // any later version.
10 //
11 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
12 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
14 // more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with Seldon. If not, see http://www.gnu.org/licenses/.
18 
19 
20 // To be included by Seldon.hxx
21 
22 #ifndef SELDON_FILE_MATRIX_SYMPACKED_HXX
23 
24 #include "../share/Common.hxx"
25 #include "../share/Properties.hxx"
26 #include "../share/Storage.hxx"
27 #include "../share/Errors.hxx"
28 #include "../share/Allocator.hxx"
29 
30 namespace Seldon
31 {
32 
33 
35  template <class T, class Prop, class Storage, class Allocator
36  = typename SeldonDefaultAllocator<Storage, T>::allocator>
37  class Matrix_SymPacked: public Matrix_Base<T, Allocator>
38  {
39  // typedef declaration.
40  public:
41  typedef typename Allocator::value_type value_type;
42  typedef typename Allocator::pointer pointer;
43  typedef typename Allocator::const_pointer const_pointer;
44  typedef typename Allocator::reference reference;
45  typedef typename Allocator::const_reference const_reference;
46  typedef typename Allocator::value_type entry_type;
47  typedef typename Allocator::reference access_type;
48  typedef typename Allocator::const_reference const_access_type;
49 
50  // Attributes.
51  protected:
52 
53  // Methods.
54  public:
55  // Constructor.
57  explicit Matrix_SymPacked(int i, int j = 0);
59 
60  // Destructor.
62  void Clear();
63 
64  // Basic methods.
65  long GetDataSize() const;
66  size_t GetMemorySize() const;
67 
68  // Memory management.
69  void Reallocate(int i, int j);
70  void SetData(int i, int j, pointer data);
71  void Nullify();
72 
73  // Element access and affectation.
74  reference operator() (int i, int j);
75  const_reference operator() (int i, int j) const;
76  reference Val(int i, int j);
77  const_reference Val(int i, int j) const;
78  reference Get(int i, int j);
79  const_reference Get(int i, int j) const;
80  reference operator[] (int i);
81  const_reference operator[] (int i) const;
84  void Set(int i, int j, const T& x);
86 
87  // Convenient functions.
88  void Zero();
89  void SetIdentity();
90  void Fill();
91  template <class T0>
92  void Fill(const T0& x);
93  template <class T0>
95  void FillRand();
96  void Print() const;
97  void Print(int a, int b, int m, int n) const;
98  void Print(int l) const;
99 
100  // Input/output functions.
101  void Write(string FileName) const;
102  void Write(ostream& FileStream) const;
103  void WriteText(string FileName) const;
104  void WriteText(ostream& FileStream) const;
105  void Read(string FileName);
106  void Read(istream& FileStream);
107  void ReadText(string FileName);
108  void ReadText(istream& FileStream);
109 
110 #ifdef SELDON_WITH_VIRTUAL
111  typedef typename ClassComplexType<T>::Treal Treal;
112  typedef typename ClassComplexType<T>::Tcplx Tcplx;
113 
114  virtual void AddInteraction(int, int, const T&);
115  virtual void AddInteractionRow(int, int, const Vector<int>&,
116  const Vector<T>& val, bool s = false);
117 
118  virtual void AddInteractionColumn(int, int, const Vector<int>&,
119  const Vector<T>& val, bool s = false);
120 
121  virtual void ClearRow(int i);
122 
123  virtual void ApplySor(const SeldonTranspose&, Vector<Treal>& x, const Vector<Treal>& r,
124  const typename ClassComplexType<T>::Treal& omega,
125  int nb_iter, int stage_ssor) const;
126 
127  virtual void ApplySor(const SeldonTranspose&, Vector<Tcplx>& x, const Vector<Tcplx>& r,
128  const typename ClassComplexType<T>::Treal& omega,
129  int nb_iter, int stage_ssor) const;
130 
131  virtual void MltAddVector(const Treal& alpha, const Vector<Treal>& x,
132  const Treal& beta, Vector<Treal>& y) const;
133 
134  virtual void MltAddVector(const Tcplx& alpha, const Vector<Tcplx>& x,
135  const Tcplx& beta, Vector<Tcplx>& y) const;
136 
137  virtual void MltAddVector(const Treal& alpha, const SeldonTranspose&,
138  const Vector<Treal>& x,
139  const Treal& beta, Vector<Treal>& y) const;
140 
141  virtual void MltAddVector(const Tcplx& alpha, const SeldonTranspose&,
142  const Vector<Tcplx>& x,
143  const Tcplx& beta, Vector<Tcplx>& y) const;
144 
145  virtual void MltVector(const Vector<Treal>& x, Vector<Treal>& y) const;
146  virtual void MltVector(const Vector<Tcplx>& x, Vector<Tcplx>& y) const;
147 
148  virtual void MltVector(const SeldonTranspose&,
149  const Vector<Treal>& x, Vector<Treal>& y) const;
150 
151  virtual void MltVector(const SeldonTranspose&,
152  const Vector<Tcplx>& x, Vector<Tcplx>& y) const;
153 
154  virtual bool IsSymmetric() const;
155 #endif
156 
157  };
158 
159 
161  template <class T, class Prop, class Allocator>
162  class Matrix<T, Prop, ColSymPacked, Allocator>:
163  public Matrix_SymPacked<T, Prop, ColSymPacked, Allocator>
164  {
165  // typedef declaration.
166  public:
167  typedef typename Allocator::value_type value_type;
168  typedef Prop property;
169  typedef ColSymPacked storage;
170  typedef Allocator allocator;
171 
172  public:
173  Matrix();
174  explicit Matrix(int i, int j = 0);
176 
177  template <class T0>
178  Matrix<T, Prop, ColSymPacked, Allocator>& operator= (const T0& x);
179  Matrix<T, Prop, ColSymPacked, Allocator>& operator= (const Matrix<T, Prop,
180  ColSymPacked,
181  Allocator>& A);
182  template<class T0>
183  Matrix<T, Prop, ColSymPacked, Allocator>& operator*= (const T0& x);
184  void Resize(int i, int j);
185  };
186 
187 
189  template <class T, class Prop, class Allocator>
190  class Matrix<T, Prop, RowSymPacked, Allocator>:
191  public Matrix_SymPacked<T, Prop, RowSymPacked, Allocator>
192  {
193  // typedef declaration.
194  public:
195  typedef typename Allocator::value_type value_type;
196  typedef Prop property;
197  typedef RowSymPacked storage;
198  typedef Allocator allocator;
199 
200  public:
201  Matrix();
202  explicit Matrix(int i, int j = 0);
204 
205  template <class T0>
206  Matrix<T, Prop, RowSymPacked, Allocator>& operator= (const T0& x);
207  Matrix<T, Prop, RowSymPacked, Allocator>& operator= (const Matrix<T, Prop,
208  RowSymPacked,
209  Allocator>& A);
210  template<class T0>
211  Matrix<T, Prop, RowSymPacked, Allocator>& operator*= (const T0& x);
212  void Resize(int i, int j);
213  };
214 
215 
216 } // namespace Seldon.
217 
218 #define SELDON_FILE_MATRIX_SYMPACKED_HXX
219 #endif
Seldon::Matrix_SymPacked::~Matrix_SymPacked
~Matrix_SymPacked()
Destructor.
Definition: Matrix_SymPackedInline.cxx:52
Seldon::Matrix_SymPacked::Clear
void Clear()
Clears the matrix.
Definition: Matrix_SymPacked.cxx:87
Seldon::Matrix_SymPacked::operator()
reference operator()(int i, int j)
Access operator.
Definition: Matrix_SymPackedInline.cxx:97
Seldon::SeldonTranspose
Definition: MatrixFlag.hxx:32
Seldon::Matrix_Base
Base class for all matrices.
Definition: Matrix_Base.hxx:143
Seldon::Matrix_SymPacked::Nullify
void Nullify()
Clears the matrix without releasing memory.
Definition: Matrix_SymPacked.cxx:208
Seldon::Matrix_SymPacked::SetData
void SetData(int i, int j, pointer data)
Changes the size of the matrix and sets its data array (low level method).
Definition: Matrix_SymPacked.cxx:189
Seldon::Matrix_SymPacked::Get
reference Get(int i, int j)
Returns the element (i, j)
Definition: Matrix_SymPackedInline.cxx:222
Seldon::Vector< int >
Seldon::Matrix_SymPacked::GetMemorySize
size_t GetMemorySize() const
Returns size of A in bytes used to store the matrix.
Definition: Matrix_SymPackedInline.cxx:76
Seldon::Matrix_SymPacked
Symmetric packed matrix class.
Definition: Matrix_SymPacked.hxx:37
Seldon::Matrix_SymPacked::WriteText
void WriteText(string FileName) const
Writes the matrix in a file.
Definition: Matrix_SymPacked.cxx:447
Seldon::Matrix
Definition: SeldonHeader.hxx:226
Seldon::Matrix_SymPacked::Val
reference Val(int i, int j)
Direct access method.
Definition: Matrix_SymPackedInline.cxx:152
Seldon::Matrix_SymPacked::ReadText
void ReadText(string FileName)
Reads the matrix from a file.
Definition: Matrix_SymPacked.cxx:577
Seldon::Matrix_SymPacked::Set
void Set(int i, int j, const T &x)
Sets an element of the matrix.
Definition: Matrix_SymPackedInline.cxx:292
Seldon::Matrix_SymPacked::FillRand
void FillRand()
Fills the matrix randomly.
Definition: Matrix_SymPacked.cxx:297
Seldon::RowSymPacked
Definition: Storage.hxx:157
Seldon::ColSymPacked
Definition: Storage.hxx:146
Seldon::Matrix_SymPacked::Copy
void Copy(const Matrix_SymPacked< T, Prop, Storage, Allocator > &A)
Duplicates a matrix.
Definition: Matrix_SymPackedInline.cxx:306
Seldon::Matrix_SymPacked::Matrix_SymPacked
Matrix_SymPacked()
Default constructor.
Definition: Matrix_SymPackedInline.cxx:39
Seldon::Matrix_SymPacked::Fill
void Fill()
Fills the matrix with 0, 1, 2, ...
Definition: Matrix_SymPacked.cxx:255
Seldon::Matrix_SymPacked::Print
void Print() const
Displays the matrix on the standard output.
Definition: Matrix_SymPacked.cxx:314
Seldon::Matrix_SymPacked::operator=
Matrix_SymPacked< T, Prop, Storage, Allocator > & operator=(const Matrix_SymPacked< T, Prop, Storage, Allocator > &A)
Duplicates a matrix (assignment operator).
Definition: Matrix_SymPackedInline.cxx:276
Seldon::Matrix_SymPacked::operator[]
reference operator[](int i)
Access to elements of the data array.
Definition: Matrix_SymPackedInline.cxx:236
Seldon::Matrix_SymPacked::SetIdentity
void SetIdentity()
Sets the matrix to the identity.
Definition: Matrix_SymPacked.cxx:236
Seldon::Matrix_SymPacked::Zero
void Zero()
Sets all elements to zero.
Definition: Matrix_SymPacked.cxx:227
Seldon::Matrix< T, Prop, ColSymPacked, Allocator >
Column-major symmetric packed matrix class.
Definition: Matrix_SymPacked.hxx:162
Seldon::Matrix_SymPacked::Read
void Read(string FileName)
Reads the matrix from a file.
Definition: Matrix_SymPacked.cxx:514
Seldon::Matrix_SymPacked::Write
void Write(string FileName) const
Writes the matrix in a file.
Definition: Matrix_SymPacked.cxx:381
Seldon::Matrix< T, Prop, RowSymPacked, Allocator >
Row-major symmetric packed matrix class.
Definition: Matrix_SymPacked.hxx:190
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::Matrix_SymPacked::GetDataSize
long GetDataSize() const
Returns the number of elements stored in memory.
Definition: Matrix_SymPackedInline.cxx:68
Seldon::Matrix_SymPacked::Reallocate
void Reallocate(int i, int j)
Reallocates memory to resize the matrix.
Definition: Matrix_SymPacked.cxx:129