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 Copy(const Matrix_Sparse<T, Prop, Storage, Allocator>& A);
00095 
00096     // Basic methods.
00097     int GetNonZeros() const;
00098     int GetDataSize() const;
00099     int* GetPtr() const;
00100     int* GetInd() const;
00101     int GetPtrSize() const;
00102     int GetIndSize() const;
00103 
00104     // Element acess and affectation.
00105     value_type operator() (int i, int j) const;
00106     value_type& Val(int i, int j);
00107 #ifndef SWIG
00108     const value_type& Val(int i, int j) const;
00109 #endif
00110     void AddInteraction(int i, int j, const T& val);
00111 #ifndef SWIG
00112     Matrix_Sparse<T, Prop, Storage, Allocator>&
00113     operator= (const Matrix_Sparse<T, Prop, Storage, Allocator>& A);
00114 #endif
00115 
00116     // Convenient functions.
00117     void Zero();
00118     void SetIdentity();
00119     void Fill();
00120     template <class T0>
00121     void Fill(const T0& x);
00122     void FillRand();
00123     void Print() const;
00124     void WriteText(string FileName) const;
00125     void WriteText(ostream& FileStream) const;
00126   };
00127 
00128 
00130   template <class T, class Prop, class Allocator>
00131   class Matrix<T, Prop, ColSparse, Allocator>:
00132     public Matrix_Sparse<T, Prop, ColSparse, Allocator>
00133   {
00134     // typedef declaration.
00135   public:
00136     typedef typename Allocator::value_type value_type;
00137     typedef Prop property;
00138     typedef ColSparse storage;
00139     typedef Allocator allocator;
00140 
00141   public:
00142     Matrix()  throw();
00143     Matrix(int i, int j);
00144     Matrix(int i, int j, int nz);
00145     template <class Storage0, class Allocator0,
00146               class Storage1, class Allocator1,
00147               class Storage2, class Allocator2>
00148     Matrix(int i, int j,
00149            Vector<T, Storage0, Allocator0>& values,
00150            Vector<int, Storage1, Allocator1>& ptr,
00151            Vector<int, Storage2, Allocator2>& ind);
00152   };
00153 
00154 
00156   template <class T, class Prop, class Allocator>
00157   class Matrix<T, Prop, RowSparse, Allocator>:
00158     public Matrix_Sparse<T, Prop, RowSparse, Allocator>
00159   {
00160     // typedef declaration.
00161   public:
00162     typedef typename Allocator::value_type value_type;
00163     typedef Prop property;
00164     typedef RowSparse storage;
00165     typedef Allocator allocator;
00166 
00167   public:
00168     Matrix()  throw();
00169     Matrix(int i, int j);
00170     Matrix(int i, int j, int nz);
00171     template <class Storage0, class Allocator0,
00172               class Storage1, class Allocator1,
00173               class Storage2, class Allocator2>
00174     Matrix(int i, int j,
00175            Vector<T, Storage0, Allocator0>& values,
00176            Vector<int, Storage1, Allocator1>& ptr,
00177            Vector<int, Storage2, Allocator2>& ind);
00178 
00179     void FillRand(int Nelement);
00180     void FillRand(int Nelement, const T& x);
00181   };
00182 
00183 
00184 } // namespace Seldon.
00185 
00186 #define SELDON_FILE_MATRIX_SPARSE_HXX
00187 #endif