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