matrix/Matrix_Pointers.hxx

00001 // Copyright (C) 2001-2009 Vivien Mallet
00002 //
00003 // This file is part of the linear-algebra library Seldon,
00004 // http://seldon.sourceforge.net/.
00005 //
00006 // Seldon is free software; you can redistribute it and/or modify it under the
00007 // terms of the GNU Lesser General Public License as published by the Free
00008 // Software Foundation; either version 2.1 of the License, or (at your option)
00009 // any later version.
00010 //
00011 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
00012 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
00014 // more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public License
00017 // along with Seldon. If not, see http://www.gnu.org/licenses/.
00018 
00019 
00020 // To be included by Seldon.hxx
00021 
00022 #ifndef SELDON_FILE_MATRIX_POINTERS_HXX
00023 
00024 #include "../share/Common.hxx"
00025 #include "../share/Properties.hxx"
00026 #include "../share/Storage.hxx"
00027 #include "../share/Errors.hxx"
00028 #include "../share/Allocator.hxx"
00029 
00030 namespace Seldon
00031 {
00032 
00033 
00035   template <class T, class Prop, class Storage,
00036             class Allocator = SELDON_DEFAULT_ALLOCATOR<T> >
00037   class Matrix_Pointers: public Matrix_Base<T, Allocator>
00038   {
00039     // typedef declaration.
00040   public:
00041     typedef typename Allocator::value_type value_type;
00042     typedef typename Allocator::pointer pointer;
00043     typedef typename Allocator::const_pointer const_pointer;
00044     typedef typename Allocator::reference reference;
00045     typedef typename Allocator::const_reference const_reference;
00046     typedef typename Allocator::value_type entry_type;
00047     typedef typename Allocator::reference access_type;
00048     typedef typename Allocator::const_reference const_access_type;
00049 
00050     // Attributes.
00051   protected:
00052     pointer* me_;
00053 
00054     // Methods.
00055   public:
00056     // Constructor.
00057     Matrix_Pointers();
00058     Matrix_Pointers(int i, int j);
00059     Matrix_Pointers(const Matrix_Pointers<T, Prop, Storage, Allocator>& A);
00060 
00061     // Destructor.
00062     ~Matrix_Pointers();
00063     void Clear();
00064 
00065     // Basic methods.
00066     int GetDataSize() const;
00067     pointer* GetMe() const;
00068 
00069     // Memory management.
00070     void Reallocate(int i, int j);
00071     void SetData(int i, int j, pointer data);
00072     void Nullify();
00073     void Resize(int i, int j);
00074 
00075     // Element access and affectation.
00076     reference operator() (int i, int j);
00077 #ifndef SWIG
00078     const_reference operator() (int i, int j) const;
00079 #endif
00080     reference Val(int i, int j);
00081     reference Get(int i, int j);
00082 #ifndef SWIG
00083     const_reference Val(int i, int j) const;
00084     const_reference Get(int i, int j) const;
00085     reference operator[] (int i);
00086     const_reference operator[] (int i) const;
00087 
00088     Matrix_Pointers<T, Prop, Storage, Allocator>&
00089     operator= (const Matrix_Pointers<T, Prop, Storage, Allocator>& A);
00090 #endif
00091 
00092     void Set(int i, int j, const T& val);
00093     void Copy(const Matrix_Pointers<T, Prop, Storage, Allocator>& A);
00094 
00095     // Convenient functions.
00096     int GetLD() const;
00097     void Zero();
00098     void SetIdentity();
00099     void Fill();
00100     template <class T0>
00101     void Fill(const T0& x);
00102 #ifndef SWIG
00103     template <class T0>
00104     Matrix_Pointers<T, Prop, Storage, Allocator>& operator= (const T0& x);
00105 #endif
00106     void FillRand();
00107     void Print() const;
00108     void Print(int a, int b, int m, int n) const;
00109     void Print(int l) const;
00110 
00111     // Input/output functions.
00112     void Write(string FileName, bool with_size = true) const;
00113 #ifdef SELDON_WITH_HDF5
00114     void WriteHDF5(string FileName, string group_name, string dataset_name)
00115       const;
00116 #endif
00117     void Write(ostream& FileStream, bool with_size = true) const;
00118     void WriteText(string FileName) const;
00119     void WriteText(ostream& FileStream) const;
00120     void Read(string FileName, bool with_size = true);
00121     void Read(istream& FileStream, bool with_size = true);
00122     void ReadText(string FileName);
00123     void ReadText(istream& FileStream);
00124   };
00125 
00126 
00128   template <class T, class Prop, class Allocator>
00129   class Matrix<T, Prop, ColMajor, Allocator>:
00130     public Matrix_Pointers<T, Prop, ColMajor, Allocator>
00131   {
00132     // typedef declaration.
00133   public:
00134     typedef typename Allocator::value_type value_type;
00135     typedef Prop property;
00136     typedef ColMajor storage;
00137     typedef Allocator allocator;
00138 
00139   public:
00140     Matrix();
00141     Matrix(int i, int j);
00142     Matrix(const Matrix<T, Prop, ColMajor, Allocator>& A);
00143 
00144 #ifndef SWIG
00145     template <class T0>
00146     Matrix<T, Prop, ColMajor, Allocator>& operator= (const T0& x);
00147     Matrix<T, Prop, ColMajor, Allocator>& operator=(const Matrix<T, Prop,
00148                                                     ColMajor, Allocator>& A);
00149 #endif
00150     template<class T0>
00151     Matrix<T, Prop, ColMajor, Allocator>& operator*= (const T0& x);
00152   };
00153 
00154 
00156   template <class T, class Prop, class Allocator>
00157   class Matrix<T, Prop, RowMajor, Allocator>:
00158     public Matrix_Pointers<T, Prop, RowMajor, Allocator>
00159   {
00160     // typedef declaration.
00161   public:
00162     typedef typename Allocator::value_type value_type;
00163     typedef Prop property;
00164     typedef RowMajor storage;
00165     typedef Allocator allocator;
00166 
00167   public:
00168     Matrix();
00169     Matrix(int i, int j);
00170     Matrix(const Matrix<T, Prop, RowMajor, Allocator>& A);
00171 
00172 #ifndef SWIG
00173 
00174     template <class T0>
00175     Matrix<T, Prop, RowMajor, Allocator>& operator= (const T0& x);
00176     Matrix<T, Prop, RowMajor, Allocator>& operator=(const Matrix<T, Prop,
00177                                                     RowMajor, Allocator>& A);
00178 #endif
00179     template<class T0>
00180     Matrix<T, Prop, RowMajor, Allocator>& operator*= (const T0& x);
00181   };
00182 
00183 
00184 } // namespace Seldon.
00185 
00186 #define SELDON_FILE_MATRIX_POINTERS_HXX
00187 #endif