Matrix_Pointers.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_POINTERS_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_Pointers: 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  pointer* me_;
53 
54  // Methods.
55  public:
56  // Constructor.
58  explicit Matrix_Pointers(int i, int j);
60 
61  // Destructor.
63  void Clear();
64 
65  // Basic methods.
66  long GetDataSize() const;
67  size_t GetMemorySize() const;
68  pointer* GetMe() 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  pointer GetDataPointer(int i, int j) const;
78  reference operator() (int i, int j);
79 #ifndef SWIG
80  const_reference operator() (int i, int j) const;
81 #endif
82  reference Val(int i, int j);
83  reference Get(int i, int j);
84 #ifndef SWIG
85  const_reference Val(int i, int j) const;
86  const_reference Get(int i, int j) const;
87  reference operator[] (int i);
88  const_reference operator[] (int i) const;
89 
92 #endif
93 
94  void Set(int i, int j, const T& val);
96 
97  // Convenient functions.
98  int GetLD() const;
99  void Zero();
100  void SetIdentity();
101  void Fill();
102  template <class T0>
103  void Fill(const T0& x);
104 #ifndef SWIG
105  template <class T0>
107 #endif
108  void FillRand();
109  void Print() const;
110  void Print(int a, int b, int m, int n) const;
111  void Print(int l) const;
112 
113  // Input/output functions.
114  void Append(string FileName) const;
115  void Write(string FileName, bool with_size = true) const;
116 #ifdef SELDON_WITH_HDF5
117  void WriteHDF5(string FileName, string group_name, string dataset_name)
118  const;
119 #endif
120  void Write(ostream& FileStream, bool with_size = true) const;
121  void WriteText(string FileName) const;
122  void WriteText(ostream& FileStream) const;
123  void Read(string FileName, bool with_size = true);
124  void Read(istream& FileStream, bool with_size = true);
125  void ReadText(string FileName);
126  void ReadText(istream& FileStream);
127 
128 #ifdef SELDON_WITH_VIRTUAL
129  // ApplySor, MltVector and MltAddVector are not defined for this matrix
130  // since this class is used with differents types T (not only real
131  // or complex numbers)
132  virtual bool IsSymmetric() const;
133 
134  virtual void AddInteraction(int, int, const T&);
135  virtual void AddInteractionRow(int, int, const Vector<int>&,
136  const Vector<T>& val, bool sorted = false);
137 
138  virtual void AddInteractionColumn(int, int, const Vector<int>&,
139  const Vector<T>& val, bool sorted = false);
140 
141  virtual void ClearRow(int i);
142 
143  typedef typename ClassComplexType<T>::Treal Treal;
144  typedef typename ClassComplexType<T>::Tcplx Tcplx;
145 
146  virtual void MltAddVector(const Treal& alpha, const Vector<Treal>& x,
147  const Treal& beta, Vector<Treal>& y) const;
148 
149  virtual void MltAddVector(const Tcplx& alpha, const Vector<Tcplx>& x,
150  const Tcplx& beta, Vector<Tcplx>& y) const;
151 
152  virtual void MltAddVector(const Treal& alpha, const SeldonTranspose&,
153  const Vector<Treal>& x,
154  const Treal& beta, Vector<Treal>& y) const;
155 
156  virtual void MltAddVector(const Tcplx& alpha, const SeldonTranspose&,
157  const Vector<Tcplx>& x,
158  const Tcplx& beta, Vector<Tcplx>& y) const;
159 
160  virtual void MltVector(const Vector<Treal>& x, Vector<Treal>& y) const;
161  virtual void MltVector(const Vector<Tcplx>& x, Vector<Tcplx>& y) const;
162 
163  virtual void MltVector(const SeldonTranspose&,
164  const Vector<Treal>& x, Vector<Treal>& y) const;
165 
166  virtual void MltVector(const SeldonTranspose&,
167  const Vector<Tcplx>& x, Vector<Tcplx>& y) const;
168 
169 #endif
170 
171  };
172 
173 
175  template <class T, class Prop, class Allocator>
176  class Matrix<T, Prop, ColMajor, Allocator>:
177  public Matrix_Pointers<T, Prop, ColMajor, Allocator>
178  {
179  // typedef declaration.
180  public:
181  typedef typename Allocator::value_type value_type;
182  typedef Prop property;
183  typedef ColMajor storage;
184  typedef Allocator allocator;
185 
186  public:
187  Matrix();
188  explicit Matrix(int i, int j);
190 
191  void WriteColumn(string FileName, int col) const;
192  void WriteColumn(ostream& FileStream, int col) const;
193 
194 #ifndef SWIG
195  template <class T0>
196  Matrix<T, Prop, ColMajor, Allocator>& operator= (const T0& x);
197  Matrix<T, Prop, ColMajor, Allocator>& operator=(const Matrix<T, Prop,
198  ColMajor, Allocator>& A);
199 #endif
200  template<class T0>
201  Matrix<T, Prop, ColMajor, Allocator>& operator*= (const T0& x);
202  };
203 
204 
206  template <class T, class Prop, class Allocator>
207  class Matrix<T, Prop, RowMajor, Allocator>:
208  public Matrix_Pointers<T, Prop, RowMajor, Allocator>
209  {
210  // typedef declaration.
211  public:
212  typedef typename Allocator::value_type value_type;
213  typedef Prop property;
214  typedef RowMajor storage;
215  typedef Allocator allocator;
216 
217  public:
218  Matrix();
219  explicit Matrix(int i, int j);
221 
222  void WriteRow(string FileName, int row) const;
223  void WriteRow(ostream& FileStream, int row) const;
224 
225 #ifndef SWIG
226 
227  template <class T0>
228  Matrix<T, Prop, RowMajor, Allocator>& operator= (const T0& x);
229  Matrix<T, Prop, RowMajor, Allocator>& operator=(const Matrix<T, Prop,
230  RowMajor, Allocator>& A);
231 #endif
232  template<class T0>
233  Matrix<T, Prop, RowMajor, Allocator>& operator*= (const T0& x);
234  };
235 
236 
237 } // namespace Seldon.
238 
239 #define SELDON_FILE_MATRIX_POINTERS_HXX
240 #endif
Seldon::RowMajor
Definition: Storage.hxx:45
Seldon::SeldonTranspose
Definition: MatrixFlag.hxx:32
Seldon::Matrix_Pointers::GetMemorySize
size_t GetMemorySize() const
Returns size of A in bytes used to store the matrix.
Definition: Matrix_PointersInline.cxx:80
Seldon::Matrix_Base
Base class for all matrices.
Definition: Matrix_Base.hxx:143
Seldon::Matrix_Pointers::Nullify
void Nullify()
Clears the matrix without releasing memory.
Definition: Matrix_Pointers.cxx:329
Seldon::Matrix_Pointers::Append
void Append(string FileName) const
Appends the matrix in a file.
Definition: Matrix_Pointers.cxx:574
Seldon::Matrix_Pointers::GetMe
pointer * GetMe() const
Returns the pointer 'me_'.
Definition: Matrix_PointersInline.cxx:97
Seldon::Matrix_Pointers
Full matrix class.
Definition: Matrix_Pointers.hxx:37
Seldon::Vector< int >
Seldon::Matrix_Pointers::Reallocate
void Reallocate(int i, int j)
Reallocates memory to resize the matrix.
Definition: Matrix_Pointers.cxx:183
Seldon::Matrix_Pointers::WriteText
void WriteText(string FileName) const
Writes the matrix in a file.
Definition: Matrix_Pointers.cxx:675
Seldon::Matrix_Pointers::~Matrix_Pointers
~Matrix_Pointers()
Destructor.
Definition: Matrix_PointersInline.cxx:53
Seldon::Matrix
Definition: SeldonHeader.hxx:226
Seldon::Matrix_Pointers::FillRand
void FillRand()
Fills the matrix randomly.
Definition: Matrix_Pointers.cxx:474
Seldon::Matrix_Pointers::Set
void Set(int i, int j, const T &val)
Sets an element of the matrix.
Definition: Matrix_PointersInline.cxx:296
Seldon::Matrix_Pointers::GetDataSize
long GetDataSize() const
Returns the number of elements stored in memory.
Definition: Matrix_PointersInline.cxx:72
Seldon::Matrix_Pointers::Clear
void Clear()
Clears the matrix.
Definition: Matrix_Pointers.cxx:122
Seldon::Matrix_Pointers::Val
reference Val(int i, int j)
Access operator.
Definition: Matrix_PointersInline.cxx:186
Seldon::Matrix_Pointers::Zero
void Zero()
Sets all elements to zero.
Definition: Matrix_Pointers.cxx:406
Seldon::Matrix_Pointers::Read
void Read(string FileName, bool with_size=true)
Reads the matrix from a file.
Definition: Matrix_Pointers.cxx:746
Seldon::Matrix_Pointers::operator()
reference operator()(int i, int j)
Access operator.
Definition: Matrix_PointersInline.cxx:145
Seldon::Matrix< T, Prop, RowMajor, Allocator >
Row-major full-matrix class.
Definition: Matrix_Pointers.hxx:207
Seldon::Matrix_Pointers::operator=
Matrix_Pointers< T, Prop, Storage, Allocator > & operator=(const Matrix_Pointers< T, Prop, Storage, Allocator > &A)
Duplicates a matrix (assignment operator).
Definition: Matrix_PointersInline.cxx:311
Seldon::Matrix_Pointers::Fill
void Fill()
Fills the matrix the matrix with 0, 1, 2, ...
Definition: Matrix_Pointers.cxx:434
Seldon::Matrix_Pointers::SetIdentity
void SetIdentity()
Sets the current matrix to the identity.
Definition: Matrix_Pointers.cxx:415
Seldon::Matrix_Pointers::Print
void Print() const
Displays the matrix on the standard output.
Definition: Matrix_Pointers.cxx:491
Seldon::Matrix_Pointers::ReadText
void ReadText(string FileName)
Reads the matrix from a file.
Definition: Matrix_Pointers.cxx:815
Seldon::Matrix_Pointers::GetLD
int GetLD() const
Returns the leading dimension.
Definition: Matrix_PointersInline.cxx:108
Seldon::Matrix_Pointers::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_Pointers.cxx:277
Seldon::Matrix_Pointers::Copy
void Copy(const Matrix_Pointers< T, Prop, Storage, Allocator > &A)
Duplicates a matrix.
Definition: Matrix_PointersInline.cxx:327
Seldon::Matrix_Pointers::Write
void Write(string FileName, bool with_size=true) const
Writes the matrix in a file.
Definition: Matrix_Pointers.cxx:604
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::Matrix_Pointers::Resize
void Resize(int i, int j)
Reallocates memory to resize the matrix and keeps previous entries.
Definition: Matrix_Pointers.cxx:369
Seldon::Matrix_Pointers::GetDataPointer
pointer GetDataPointer(int i, int j) const
Returns a pointer to a data element.
Definition: Matrix_PointersInline.cxx:127
Seldon::Matrix_Pointers::Get
reference Get(int i, int j)
Access operator.
Definition: Matrix_PointersInline.cxx:206
Seldon::Matrix_Pointers::Matrix_Pointers
Matrix_Pointers()
Default constructor.
Definition: Matrix_PointersInline.cxx:39
Seldon::Matrix< T, Prop, ColMajor, Allocator >
Column-major full-matrix class.
Definition: Matrix_Pointers.hxx:176
Seldon::ColMajor
Definition: Storage.hxx:33
Seldon::Matrix_Pointers::operator[]
reference operator[](int i)
Access to elements of the data array.
Definition: Matrix_PointersInline.cxx:257