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 
00068     // Memory management.
00069     void Reallocate(int i, int j);
00070     void SetData(int i, int j, pointer data);
00071     void Nullify();
00072     void Resize(int i, int j);
00073 
00074     // Element access and affectation.
00075     reference operator() (int i, int j);
00076 #ifndef SWIG
00077     const_reference operator() (int i, int j) const;
00078 #endif
00079     reference Val(int i, int j);
00080 #ifndef SWIG
00081     const_reference Val(int i, int j) const;
00082     reference operator[] (int i);
00083     const_reference operator[] (int i) const;
00084     Matrix_Pointers<T, Prop, Storage, Allocator>&
00085     operator= (const Matrix_Pointers<T, Prop, Storage, Allocator>& A);
00086 #endif
00087     void Copy(const Matrix_Pointers<T, Prop, Storage, Allocator>& A);
00088 
00089     // Convenient functions.
00090     int GetLD() const;
00091     void Zero();
00092     void SetIdentity();
00093     void Fill();
00094     template <class T0>
00095     void Fill(const T0& x);
00096 #ifndef SWIG
00097     template <class T0>
00098     Matrix_Pointers<T, Prop, Storage, Allocator>& operator= (const T0& x);
00099 #endif
00100     void FillRand();
00101     void Print() const;
00102     void Print(int a, int b, int m, int n) const;
00103     void Print(int l) const;
00104 
00105     // Input/output functions.
00106     void Write(string FileName, bool with_size = true) const;
00107     void Write(ostream& FileStream, bool with_size = true) const;
00108     void WriteText(string FileName) const;
00109     void WriteText(ostream& FileStream) const;
00110     void Read(string FileName, bool with_size = true);
00111     void Read(istream& FileStream, bool with_size = true);
00112     void ReadText(string FileName);
00113     void ReadText(istream& FileStream);
00114   };
00115 
00116 
00118   template <class T, class Prop, class Allocator>
00119   class Matrix<T, Prop, ColMajor, Allocator>:
00120     public Matrix_Pointers<T, Prop, ColMajor, Allocator>
00121   {
00122     // typedef declaration.
00123   public:
00124     typedef typename Allocator::value_type value_type;
00125     typedef Prop property;
00126     typedef ColMajor storage;
00127     typedef Allocator allocator;
00128 
00129   public:
00130     Matrix()  throw();
00131     Matrix(int i, int j);
00132     Matrix(const Matrix<T, Prop, ColMajor, Allocator>& A);
00133 
00134 #ifndef SWIG
00135     template <class T0>
00136     Matrix<T, Prop, ColMajor, Allocator>& operator= (const T0& x);
00137     Matrix<T, Prop, ColMajor, Allocator>& operator=(const Matrix<T, Prop,
00138                                                     ColMajor, Allocator>& A);
00139 #endif
00140     template<class T0>
00141     Matrix<T, Prop, ColMajor, Allocator>& operator*= (const T0& x);
00142   };
00143 
00144 
00146   template <class T, class Prop, class Allocator>
00147   class Matrix<T, Prop, RowMajor, Allocator>:
00148     public Matrix_Pointers<T, Prop, RowMajor, Allocator>
00149   {
00150     // typedef declaration.
00151   public:
00152     typedef typename Allocator::value_type value_type;
00153     typedef Prop property;
00154     typedef RowMajor storage;
00155     typedef Allocator allocator;
00156 
00157   public:
00158     Matrix()  throw();
00159     Matrix(int i, int j);
00160     Matrix(const Matrix<T, Prop, RowMajor, Allocator>& A);
00161 
00162 #ifndef SWIG
00163 
00164     template <class T0>
00165     Matrix<T, Prop, RowMajor, Allocator>& operator= (const T0& x);
00166     Matrix<T, Prop, RowMajor, Allocator>& operator=(const Matrix<T, Prop,
00167                                                     RowMajor, Allocator>& A);
00168 #endif
00169     template<class T0>
00170     Matrix<T, Prop, RowMajor, Allocator>& operator*= (const T0& x);
00171   };
00172 
00173 
00174 } // namespace Seldon.
00175 
00176 #define SELDON_FILE_MATRIX_POINTERS_HXX
00177 #endif