Matrix_Symmetric.hxx
1 // Copyright (C) 2001-2011 Vivien Mallet
2 // Copyright (C) 2003-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_SYMMETRIC_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  template <class T, class Prop, class Storage, class Allocator
37  = typename SeldonDefaultAllocator<Storage, T>::allocator>
38  class Matrix_Symmetric: public Matrix_Base<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 typename Allocator::value_type entry_type;
48  typedef typename Allocator::value_type access_type;
49  typedef typename Allocator::value_type const_access_type;
50 
51  // Attributes.
52  protected:
53  pointer* me_;
54 
55  // Methods.
56  public:
57  // Constructor.
59  explicit Matrix_Symmetric(int i, int j);
61 
62  // Destructor.
64  void Clear();
65 
66  // Basic methods.
67  long GetDataSize() const;
68  size_t GetMemorySize() const;
69 
70  // Memory management.
71  void Reallocate(int i, int j);
72  void SetData(int i, int j, pointer data);
73  void Nullify();
74  void Resize(int i, int j);
75 
76  // Element access and affectation.
77  reference operator() (int i, int j);
78  const_reference operator() (int i, int j) const;
79  const_reference Val(int i, int j) const;
80  reference Val(int i, int j);
81  reference Get(int i, int j);
82  const_reference Get(int i, int j) const;
83  reference operator[] (int i);
84  const_reference operator[] (int i) const;
87  void Set(int i, int j, const T& x);
89 
90  // Convenient functions.
91  void Zero();
92  void SetIdentity();
93  void Fill();
94  template <class T0>
95  void Fill(const T0& x);
96  template <class T0>
98  void FillRand();
99  void Print() const;
100  void Print(int a, int b, int m, int n) const;
101  void Print(int l) const;
102 
103  // Input/output functions.
104  void Write(string FileName) const;
105  void Write(ostream& FileStream) const;
106  void WriteText(string FileName) const;
107  void WriteText(ostream& FileStream) const;
108  void Read(string FileName);
109  void Read(istream& FileStream);
110  void ReadText(string FileName);
111  void ReadText(istream& FileStream);
112 
113 #ifdef SELDON_WITH_VIRTUAL
114  typedef typename ClassComplexType<T>::Treal Treal;
115  typedef typename ClassComplexType<T>::Tcplx Tcplx;
116 
117  virtual void AddInteraction(int, int, const T&);
118  virtual void AddInteractionRow(int, int, const Vector<int>&,
119  const Vector<T>& val, bool s = false);
120 
121  virtual void AddInteractionColumn(int, int, const Vector<int>&,
122  const Vector<T>& val, bool s = false);
123 
124  virtual void ClearRow(int i);
125 
126  virtual void ApplySor(const SeldonTranspose&, Vector<Treal>& x, const Vector<Treal>& r,
127  const typename ClassComplexType<T>::Treal& omega,
128  int nb_iter, int stage_ssor) const;
129 
130  virtual void ApplySor(const SeldonTranspose&, Vector<Tcplx>& x, const Vector<Tcplx>& r,
131  const typename ClassComplexType<T>::Treal& omega,
132  int nb_iter, int stage_ssor) const;
133 
134  virtual void MltAddVector(const Treal& alpha, const Vector<Treal>& x,
135  const Treal& beta, Vector<Treal>& y) const;
136 
137  virtual void MltAddVector(const Tcplx& alpha, const Vector<Tcplx>& x,
138  const Tcplx& beta, Vector<Tcplx>& y) const;
139 
140  virtual void MltAddVector(const Treal& alpha, const SeldonTranspose&,
141  const Vector<Treal>& x,
142  const Treal& beta, Vector<Treal>& y) const;
143 
144  virtual void MltAddVector(const Tcplx& alpha, const SeldonTranspose&,
145  const Vector<Tcplx>& x,
146  const Tcplx& beta, Vector<Tcplx>& y) const;
147 
148  virtual void MltVector(const Vector<Treal>& x, Vector<Treal>& y) const;
149  virtual void MltVector(const Vector<Tcplx>& x, Vector<Tcplx>& y) const;
150 
151  virtual void MltVector(const SeldonTranspose&,
152  const Vector<Treal>& x, Vector<Treal>& y) const;
153 
154  virtual void MltVector(const SeldonTranspose&,
155  const Vector<Tcplx>& x, Vector<Tcplx>& y) const;
156 
157  virtual bool IsSymmetric() const;
158 #endif
159 
160  };
161 
162 
164  template <class T, class Prop, class Allocator>
165  class Matrix<T, Prop, ColSym, Allocator>:
166  public Matrix_Symmetric<T, Prop, ColSym, Allocator>
167  {
168  // typedef declaration.
169  public:
170  typedef typename Allocator::value_type value_type;
171  typedef Prop property;
172  typedef ColSym storage;
173  typedef Allocator allocator;
174 
175  public:
176  Matrix();
177  explicit Matrix(int i, int j);
178 
179  template <class T0>
180  Matrix<T, Prop, ColSym, Allocator>& operator= (const T0& x);
181  Matrix<T, Prop, ColSym, Allocator>& operator= (const Matrix<T, Prop,
182  ColSym,
183  Allocator>& A);
184  template<class T0>
185  Matrix<T, Prop, ColSym, Allocator>& operator*= (const T0& x);
186 
187  };
188 
189 
191  template <class T, class Prop, class Allocator>
192  class Matrix<T, Prop, RowSym, Allocator>:
193  public Matrix_Symmetric<T, Prop, RowSym, Allocator>
194  {
195  // typedef declaration.
196  public:
197  typedef typename Allocator::value_type value_type;
198  typedef Prop property;
199  typedef RowSym storage;
200  typedef Allocator allocator;
201 
202  public:
203  Matrix();
204  explicit Matrix(int i, int j);
205 
206  template <class T0>
207  Matrix<T, Prop, RowSym, Allocator>& operator= (const T0& x);
208  Matrix<T, Prop, RowSym, Allocator>& operator= (const Matrix<T, Prop,
209  RowSym,
210  Allocator>& A);
211  template<class T0>
212  Matrix<T, Prop, RowSym, Allocator>& operator*= (const T0& x);
213 
214  };
215 
216 
217 } // namespace Seldon.
218 
219 #define SELDON_FILE_MATRIX_SYMMETRIC_HXX
220 #endif
Seldon::Matrix_Symmetric::Resize
void Resize(int i, int j)
Reallocates memory to resize the matrix and keeps previous entries.
Definition: Matrix_Symmetric.cxx:397
Seldon::Matrix_Symmetric::Read
void Read(string FileName)
Reads the matrix from a file.
Definition: Matrix_Symmetric.cxx:722
Seldon::SeldonTranspose
Definition: MatrixFlag.hxx:32
Seldon::Matrix_Symmetric::ReadText
void ReadText(string FileName)
Reads the matrix from a file.
Definition: Matrix_Symmetric.cxx:786
Seldon::Matrix_Base
Base class for all matrices.
Definition: Matrix_Base.hxx:143
Seldon::Matrix_Symmetric::operator=
Matrix_Symmetric< T, Prop, Storage, Allocator > & operator=(const Matrix_Symmetric< T, Prop, Storage, Allocator > &A)
Duplicates a matrix (assignment operator).
Definition: Matrix_SymmetricInline.cxx:278
Seldon::Matrix_Symmetric::Val
const_reference Val(int i, int j) const
Access operator.
Definition: Matrix_SymmetricInline.cxx:151
Seldon::Matrix_Symmetric::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_Symmetric.cxx:300
Seldon::Matrix_Symmetric::Print
void Print() const
Displays the matrix on the standard output.
Definition: Matrix_Symmetric.cxx:518
Seldon::Matrix_Symmetric::Copy
void Copy(const Matrix_Symmetric< T, Prop, Storage, Allocator > &A)
Duplicates a matrix.
Definition: Matrix_SymmetricInline.cxx:308
Seldon::Matrix_Symmetric::Get
reference Get(int i, int j)
Returns the element (i, j)
Definition: Matrix_SymmetricInline.cxx:191
Seldon::Matrix_Symmetric::Reallocate
void Reallocate(int i, int j)
Reallocates memory to resize the matrix.
Definition: Matrix_Symmetric.cxx:195
Seldon::Vector< int >
Seldon::Matrix_Symmetric::operator[]
reference operator[](int i)
Access to elements of the data array.
Definition: Matrix_SymmetricInline.cxx:238
Seldon::Matrix
Definition: SeldonHeader.hxx:226
Seldon::Matrix_Symmetric::Set
void Set(int i, int j, const T &x)
Sets an element of the matrix.
Definition: Matrix_SymmetricInline.cxx:294
Seldon::Matrix< T, Prop, ColSym, Allocator >
Column-major symmetric full-matrix class.
Definition: Matrix_Symmetric.hxx:165
Seldon::Matrix< T, Prop, RowSym, Allocator >
Row-major symmetric full-matrix class.
Definition: Matrix_Symmetric.hxx:192
Seldon::Matrix_Symmetric::Nullify
void Nullify()
Clears the matrix without releasing memory.
Definition: Matrix_Symmetric.cxx:352
Seldon::Matrix_Symmetric::Fill
void Fill()
Fills the matrix with 0, 1, 2, ...
Definition: Matrix_Symmetric.cxx:459
Seldon::Matrix_Symmetric::operator()
reference operator()(int i, int j)
Access operator.
Definition: Matrix_SymmetricInline.cxx:102
Seldon::Matrix_Symmetric::GetDataSize
long GetDataSize() const
Returns the number of elements stored in memory.
Definition: Matrix_SymmetricInline.cxx:72
Seldon::Matrix_Symmetric::GetMemorySize
size_t GetMemorySize() const
Returns size of A in bytes used to store the matrix.
Definition: Matrix_SymmetricInline.cxx:80
Seldon::Matrix_Symmetric::Write
void Write(string FileName) const
Writes the matrix in a file.
Definition: Matrix_Symmetric.cxx:585
Seldon::Matrix_Symmetric::Matrix_Symmetric
Matrix_Symmetric()
Default constructor.
Definition: Matrix_SymmetricInline.cxx:39
Seldon::Matrix_Symmetric::FillRand
void FillRand()
Fills a matrix randomly.
Definition: Matrix_Symmetric.cxx:501
Seldon::Matrix_Symmetric
Symmetric matrix stored in a full matrix.
Definition: Matrix_Symmetric.hxx:38
Seldon::Matrix_Symmetric::Zero
void Zero()
Sets all elements to zero.
Definition: Matrix_Symmetric.cxx:431
Seldon::Matrix_Symmetric::Clear
void Clear()
Clears the matrix.
Definition: Matrix_Symmetric.cxx:133
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::ColSym
Definition: Storage.hxx:168
Seldon::Matrix_Symmetric::WriteText
void WriteText(string FileName) const
Writes the matrix in a file.
Definition: Matrix_Symmetric.cxx:653
Seldon::Matrix_Symmetric::~Matrix_Symmetric
~Matrix_Symmetric()
Destructor.
Definition: Matrix_SymmetricInline.cxx:53
Seldon::Matrix_Symmetric::SetIdentity
void SetIdentity()
Sets the matrix to the identity.
Definition: Matrix_Symmetric.cxx:440
Seldon::RowSym
Definition: Storage.hxx:179