Warning: this documentation for the development version is under construction.

/home/vivien/public_html/.src_seldon/matrix_sparse/Matrix_Sparse.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_SPARSE_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 
00042   template <class T, class Prop, class Storage,
00043             class Allocator = SELDON_DEFAULT_ALLOCATOR<T> >
00044   class Matrix_Sparse: public Matrix_Base<T, Allocator>
00045   {
00046     // typedef declaration.
00047   public:
00048     typedef typename Allocator::value_type value_type;
00049     typedef typename Allocator::pointer pointer;
00050     typedef typename Allocator::const_pointer const_pointer;
00051     typedef typename Allocator::reference reference;
00052     typedef typename Allocator::const_reference const_reference;
00053     typedef value_type entry_type;
00054     typedef value_type access_type;
00055     typedef value_type const_access_type;
00056 
00057     // Attributes.
00058   protected:
00059     // Number of non-zero elements.
00060     int nz_;
00061     // Index (in data_) of first element stored for each row or column.
00062     int* ptr_;
00063     // Column or row index (in the matrix) each element.
00064     int* ind_;
00065 
00066     // Methods.
00067   public:
00068     // Constructors.
00069     Matrix_Sparse();
00070     Matrix_Sparse(int i, int j);
00071     Matrix_Sparse(int i, int j, int nz);
00072     template <class Storage0, class Allocator0,
00073               class Storage1, class Allocator1,
00074               class Storage2, class Allocator2>
00075     Matrix_Sparse(int i, int j, Vector<T, Storage0, Allocator0>& values,
00076                   Vector<int, Storage1, Allocator1>& ptr,
00077                   Vector<int, Storage2, Allocator2>& ind);
00078     Matrix_Sparse(const Matrix_Sparse<T, Prop, Storage, Allocator>& A);
00079 
00080     // Destructor.
00081     ~Matrix_Sparse();
00082     void Clear();
00083 
00084     // Memory management.
00085     template <class Storage0, class Allocator0,
00086               class Storage1, class Allocator1,
00087               class Storage2, class Allocator2>
00088     void SetData(int i, int j,
00089                  Vector<T, Storage0, Allocator0>& values,
00090                  Vector<int, Storage1, Allocator1>& ptr,
00091                  Vector<int, Storage2, Allocator2>& ind);
00092     void SetData(int i, int j, int nz, pointer values, int* ptr, int* ind);
00093     void Nullify();
00094     void Reallocate(int i, int j);
00095     void Reallocate(int i, int j, int nz);
00096     void Resize(int i, int j);
00097     void Resize(int i, int j, int nz);
00098     void Copy(const Matrix_Sparse<T, Prop, Storage, Allocator>& A);
00099 
00100     // Basic methods.
00101     int GetNonZeros() const;
00102     int GetDataSize() const;
00103     int* GetPtr() const;
00104     int* GetInd() const;
00105     int GetPtrSize() const;
00106     int GetIndSize() const;
00107 
00108     // Element acess and affectation.
00109     value_type operator() (int i, int j) const;
00110     value_type& Val(int i, int j);
00111     value_type& Get(int i, int j);
00112 #ifndef SWIG
00113     const value_type& Val(int i, int j) const;
00114     const value_type& Get(int i, int j) const;
00115 #endif
00116     void AddInteraction(int i, int j, const T& val);
00117     void Set(int i, int j, const T& x);
00118 #ifndef SWIG
00119     Matrix_Sparse<T, Prop, Storage, Allocator>&
00120     operator= (const Matrix_Sparse<T, Prop, Storage, Allocator>& A);
00121 #endif
00122 
00123     // Convenient functions.
00124     void Zero();
00125     void SetIdentity();
00126     void Fill();
00127     template <class T0>
00128     void Fill(const T0& x);
00129     void FillRand();
00130     void FillRand(int Nelement);
00131     void FillRand(int Nelement, const T& x);
00132 
00133     void Print() const;
00134     void Write(string FileName) const;
00135     void Write(ostream& FileStream) const;
00136     void WriteText(string FileName) const;
00137     void WriteText(ostream& FileStream) const;
00138     void Read(string FileName);
00139     void Read(istream& FileStream);
00140     void ReadText(string FileName);
00141     void ReadText(istream& FileStream);
00142   };
00143 
00144 
00146   template <class T, class Prop, class Allocator>
00147   class Matrix<T, Prop, ColSparse, Allocator>:
00148     public Matrix_Sparse<T, Prop, ColSparse, Allocator>
00149   {
00150     // typedef declaration.
00151   public:
00152     typedef typename Allocator::value_type value_type;
00153     typedef Prop property;
00154     typedef ColSparse storage;
00155     typedef Allocator allocator;
00156 
00157   public:
00158     Matrix();
00159     Matrix(int i, int j);
00160     Matrix(int i, int j, int nz);
00161     template <class Storage0, class Allocator0,
00162               class Storage1, class Allocator1,
00163               class Storage2, class Allocator2>
00164     Matrix(int i, int j,
00165            Vector<T, Storage0, Allocator0>& values,
00166            Vector<int, Storage1, Allocator1>& ptr,
00167            Vector<int, Storage2, Allocator2>& ind);
00168   };
00169 
00170 
00172   template <class T, class Prop, class Allocator>
00173   class Matrix<T, Prop, RowSparse, Allocator>:
00174     public Matrix_Sparse<T, Prop, RowSparse, Allocator>
00175   {
00176     // typedef declaration.
00177   public:
00178     typedef typename Allocator::value_type value_type;
00179     typedef Prop property;
00180     typedef RowSparse storage;
00181     typedef Allocator allocator;
00182 
00183   public:
00184     Matrix();
00185     Matrix(int i, int j);
00186     Matrix(int i, int j, int nz);
00187     template <class Storage0, class Allocator0,
00188               class Storage1, class Allocator1,
00189               class Storage2, class Allocator2>
00190     Matrix(int i, int j,
00191            Vector<T, Storage0, Allocator0>& values,
00192            Vector<int, Storage1, Allocator1>& ptr,
00193            Vector<int, Storage2, Allocator2>& ind);
00194 
00195   };
00196 
00197   template<class Tint, class AllocInt, class T, class Allocator>
00198   void ReadCoordinateMatrix(istream& FileStream,
00199                             Vector<Tint, VectFull, AllocInt>& row_numbers,
00200                             Vector<Tint, VectFull, AllocInt>& col_numbers,
00201                             Vector<T, VectFull, Allocator>& values);
00202 
00203   template<class Matrix1, class T>
00204   void ReadCoordinateMatrix(Matrix1& A, istream& FileStream, T& zero,
00205                             int index = 1, int nnz = -1);
00206 
00207 
00208 } // namespace Seldon.
00209 
00210 #define SELDON_FILE_MATRIX_SPARSE_HXX
00211 #endif