Matrix_Triangular.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_TRIANGULAR_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_Triangular: 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_Triangular(int i, int j);
61  Storage, Allocator>& A);
62 
63  // Destructor.
65  void Clear();
66 
67  // Basic methods.
68  long GetDataSize() const;
69  size_t GetMemorySize() const;
70 
71  // Memory management.
72  void Reallocate(int i, int j);
73  void Resize(int i, int j);
74  void SetData(int i, int j, pointer data);
75  void Nullify();
76 
77  // Element access and affectation.
78  const value_type operator() (int i, int j) const;
79  const_reference Val(int i, int j) const;
80  reference Val(int i, int j);
81  const_reference Get(int i, int j) const;
82  reference Get(int i, int j);
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 
97  template <class T0>
99 
100  void FillRand();
101  void Print() const;
102  void Print(int a, int b, int m, int n) const;
103  void Print(int l) const;
104 
105  // Input/output functions.
106  void Write(string FileName) const;
107  void Write(ostream& FileStream) const;
108  void WriteText(string FileName) const;
109  void WriteText(ostream& FileStream) const;
110  void Read(string FileName);
111  void Read(istream& FileStream);
112  void ReadText(string FileName);
113  void ReadText(istream& FileStream);
114 
115 #ifdef SELDON_WITH_VIRTUAL
116  typedef typename ClassComplexType<T>::Treal Treal;
117  typedef typename ClassComplexType<T>::Tcplx Tcplx;
118 
119  virtual void MltAddVector(const Treal& alpha, const Vector<Treal>& x,
120  const Treal& beta, Vector<Treal>& y) const;
121 
122  virtual void MltAddVector(const Tcplx& alpha, const Vector<Tcplx>& x,
123  const Tcplx& beta, Vector<Tcplx>& y) const;
124 
125  virtual void MltAddVector(const Treal& alpha, const SeldonTranspose&,
126  const Vector<Treal>& x,
127  const Treal& beta, Vector<Treal>& y) const;
128 
129  virtual void MltAddVector(const Tcplx& alpha, const SeldonTranspose&,
130  const Vector<Tcplx>& x,
131  const Tcplx& beta, Vector<Tcplx>& y) const;
132 
133  virtual void MltVector(const Vector<Treal>& x, Vector<Treal>& y) const;
134  virtual void MltVector(const Vector<Tcplx>& x, Vector<Tcplx>& y) const;
135 
136  virtual void MltVector(const SeldonTranspose&,
137  const Vector<Treal>& x, Vector<Treal>& y) const;
138 
139  virtual void MltVector(const SeldonTranspose&,
140  const Vector<Tcplx>& x, Vector<Tcplx>& y) const;
141 
142  virtual bool IsSymmetric() const;
143 #endif
144 
145  };
146 
147 
149  template <class T, class Prop, class Allocator>
150  class Matrix<T, Prop, ColUpTriang, Allocator>:
151  public Matrix_Triangular<T, Prop, ColUpTriang, Allocator>
152  {
153  // typedef declaration.
154  public:
155  typedef typename Allocator::value_type value_type;
156  typedef Prop property;
157  typedef ColUpTriang storage;
158  typedef Allocator allocator;
159 
160  public:
161  Matrix();
162  explicit Matrix(int i, int j);
163 
164  template <class T0>
165  Matrix<T, Prop, ColUpTriang, Allocator>& operator= (const T0& x);
167  operator= (const Matrix<T, Prop, ColUpTriang, Allocator>& A);
168  template<class T0>
169  Matrix<T, Prop, ColUpTriang, Allocator>& operator*= (const T0& x);
170 
171  };
172 
173 
175  template <class T, class Prop, class Allocator>
176  class Matrix<T, Prop, ColLoTriang, Allocator>:
177  public Matrix_Triangular<T, Prop, ColLoTriang, Allocator>
178  {
179  // typedef declaration.
180  public:
181  typedef typename Allocator::value_type value_type;
182  typedef Prop property;
183  typedef ColLoTriang storage;
184  typedef Allocator allocator;
185 
186  public:
187  Matrix();
188  explicit Matrix(int i, int j);
189 
190  template <class T0>
191  Matrix<T, Prop, ColLoTriang, Allocator>& operator= (const T0& x);
193  operator= (const Matrix<T, Prop, ColLoTriang, Allocator>& A);
194  template<class T0>
195  Matrix<T, Prop, ColLoTriang, Allocator>& operator*= (const T0& x);
196 
197  };
198 
199 
201  template <class T, class Prop, class Allocator>
202  class Matrix<T, Prop, RowUpTriang, Allocator>:
203  public Matrix_Triangular<T, Prop, RowUpTriang, Allocator>
204  {
205  // typedef declaration.
206  public:
207  typedef typename Allocator::value_type value_type;
208  typedef Prop property;
209  typedef RowUpTriang storage;
210  typedef Allocator allocator;
211 
212  public:
213  Matrix();
214  explicit Matrix(int i, int j);
215 
216  template <class T0>
217  Matrix<T, Prop, RowUpTriang, Allocator>& operator= (const T0& x);
219  operator= (const Matrix<T, Prop, RowUpTriang, Allocator>& A);
220  template<class T0>
221  Matrix<T, Prop, RowUpTriang, Allocator>& operator*= (const T0& x);
222 
223  };
224 
225 
227  template <class T, class Prop, class Allocator>
228  class Matrix<T, Prop, RowLoTriang, Allocator>:
229  public Matrix_Triangular<T, Prop, RowLoTriang, Allocator>
230  {
231  // typedef declaration.
232  public:
233  typedef typename Allocator::value_type value_type;
234  typedef Prop property;
235  typedef RowLoTriang storage;
236  typedef Allocator allocator;
237 
238  public:
239  Matrix();
240  explicit Matrix(int i, int j);
241 
242  template <class T0>
243  Matrix<T, Prop, RowLoTriang, Allocator>& operator= (const T0& x);
245  operator= (const Matrix<T, Prop, RowLoTriang, Allocator>& A);
246  template<class T0>
247  Matrix<T, Prop, RowLoTriang, Allocator>& operator*= (const T0& x);
248  };
249 
250 
251 } // namespace Seldon.
252 
253 #define SELDON_FILE_MATRIX_TRIANGULAR_HXX
254 #endif
Seldon::Matrix_Triangular::Nullify
void Nullify()
Clears the matrix without releasing memory.
Definition: Matrix_Triangular.cxx:353
Seldon::SeldonTranspose
Definition: MatrixFlag.hxx:32
Seldon::Matrix_Triangular::operator[]
reference operator[](int i)
Access to elements of the data array.
Definition: Matrix_TriangularInline.cxx:219
Seldon::Matrix_Base
Base class for all matrices.
Definition: Matrix_Base.hxx:143
Seldon::Matrix_Triangular::Read
void Read(string FileName)
Reads the matrix from a file.
Definition: Matrix_Triangular.cxx:720
Seldon::Vector< Treal >
Seldon::ColLoTriang
Definition: Storage.hxx:258
Seldon::Matrix_Triangular::Val
const_reference Val(int i, int j) const
Access operator.
Definition: Matrix_TriangularInline.cxx:144
Seldon::Matrix
Definition: SeldonHeader.hxx:226
Seldon::Matrix< T, Prop, ColLoTriang, Allocator >
Column-major lower-triangular full-matrix class.
Definition: Matrix_Triangular.hxx:176
Seldon::Matrix_Triangular::Matrix_Triangular
Matrix_Triangular()
Default constructor.
Definition: Matrix_TriangularInline.cxx:39
Seldon::Matrix< T, Prop, RowLoTriang, Allocator >
Row-major lower-triangular full-matrix class.
Definition: Matrix_Triangular.hxx:228
Seldon::Matrix_Triangular::ReadText
void ReadText(string FileName)
Reads the matrix from a file.
Definition: Matrix_Triangular.cxx:784
Seldon::ColUpTriang
Definition: Storage.hxx:246
Seldon::Matrix_Triangular::Write
void Write(string FileName) const
Writes the matrix in a file.
Definition: Matrix_Triangular.cxx:583
Seldon::RowLoTriang
Definition: Storage.hxx:282
Seldon::Matrix_Triangular::WriteText
void WriteText(string FileName) const
Writes the matrix in a file.
Definition: Matrix_Triangular.cxx:651
Seldon::Matrix_Triangular::Clear
void Clear()
Clears the matrix.
Definition: Matrix_Triangular.cxx:135
Seldon::Matrix< T, Prop, RowUpTriang, Allocator >
Row-major upper-triangular full-matrix class.
Definition: Matrix_Triangular.hxx:202
Seldon::Matrix_Triangular::GetMemorySize
size_t GetMemorySize() const
Returns size of A in bytes used to store the matrix.
Definition: Matrix_TriangularInline.cxx:80
Seldon::Matrix_Triangular::Print
void Print() const
Displays the matrix on the standard output.
Definition: Matrix_Triangular.cxx:516
Seldon::Matrix_Triangular::Zero
void Zero()
Sets all elements to zero.
Definition: Matrix_Triangular.cxx:427
Seldon::Matrix_Triangular::Resize
void Resize(int i, int j)
Reallocates memory to resize the matrix and keeps previous entries.
Definition: Matrix_Triangular.cxx:393
Seldon::Matrix< T, Prop, ColUpTriang, Allocator >
Column-major upper-triangular full-matrix class.
Definition: Matrix_Triangular.hxx:150
Seldon::Matrix_Triangular::~Matrix_Triangular
~Matrix_Triangular()
Destructor.
Definition: Matrix_TriangularInline.cxx:53
Seldon::Matrix_Triangular::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_Triangular.cxx:301
Seldon::Matrix_Triangular::Fill
void Fill()
Fills the matrix with 0, 1, 2, ...
Definition: Matrix_Triangular.cxx:455
Seldon::Matrix_Triangular::GetDataSize
long GetDataSize() const
Returns the number of elements stored in memory.
Definition: Matrix_TriangularInline.cxx:72
Seldon::Matrix_Triangular::Get
const_reference Get(int i, int j) const
Returns the element (i, j)
Definition: Matrix_TriangularInline.cxx:190
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::Matrix_Triangular::operator=
Matrix_Triangular< T, Prop, Storage, Allocator > & operator=(const Matrix_Triangular< T, Prop, Storage, Allocator > &A)
Duplicates a matrix (assignment operator).
Definition: Matrix_TriangularInline.cxx:259
Seldon::Matrix_Triangular::SetIdentity
void SetIdentity()
Sets the matrix to the identity.
Definition: Matrix_Triangular.cxx:436
Seldon::RowUpTriang
Definition: Storage.hxx:270
Seldon::Matrix_Triangular::Set
void Set(int i, int j, const T &x)
Sets an element of the matrix.
Definition: Matrix_TriangularInline.cxx:275
Seldon::Matrix_Triangular::Copy
void Copy(const Matrix_Triangular< T, Prop, Storage, Allocator > &A)
Duplicates a matrix.
Definition: Matrix_TriangularInline.cxx:289
Seldon::Matrix_Triangular::operator()
const value_type operator()(int i, int j) const
Access operator.
Definition: Matrix_TriangularInline.cxx:104
Seldon::Matrix_Triangular::Reallocate
void Reallocate(int i, int j)
Reallocates memory to resize the matrix.
Definition: Matrix_Triangular.cxx:196
Seldon::Matrix_Triangular::FillRand
void FillRand()
Fills the matrix randomly.
Definition: Matrix_Triangular.cxx:499
Seldon::Matrix_Triangular
Triangular matrix stored in a full matrix.
Definition: Matrix_Triangular.hxx:38