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 Copy(const Matrix_SymSparse<T, Prop, Storage, Allocator>& A);
00096 
00097     // Basic methods.
00098     int GetNonZeros() const;
00099     int GetDataSize() const;
00100     int* GetPtr() const;
00101     int* GetInd() const;
00102     int GetPtrSize() const;
00103     int GetIndSize() const;
00104 
00105     // Element acess and affectation.
00106     value_type operator() (int i, int j) const;
00107     value_type& Val(int i, int j);
00108     const value_type& Val(int i, int j) const;
00109     Matrix_SymSparse<T, Prop, Storage, Allocator>&
00110     operator= (const Matrix_SymSparse<T, Prop, Storage, Allocator>& A);
00111 
00112     // Convenient functions.
00113     void Print() const;
00114     void WriteText(string FileName) const;
00115     void WriteText(ostream& FileStream) const;
00116   };
00117 
00118 
00120   template <class T, class Prop, class Allocator>
00121   class Matrix<T, Prop, ColSymSparse, Allocator>:
00122     public Matrix_SymSparse<T, Prop, ColSymSparse, Allocator>
00123   {
00124     // typedef declaration.
00125   public:
00126     typedef typename Allocator::value_type value_type;
00127     typedef Prop property;
00128     typedef ColSymSparse storage;
00129     typedef Allocator allocator;
00130 
00131   public:
00132     Matrix()  throw();
00133     Matrix(int i, int j);
00134     Matrix(int i, int j, int nz);
00135     template <class Storage0, class Allocator0,
00136               class Storage1, class Allocator1,
00137               class Storage2, class Allocator2>
00138     Matrix(int i, int j,
00139            Vector<T, Storage0, Allocator0>& values,
00140            Vector<int, Storage1, Allocator1>& ptr,
00141            Vector<int, Storage2, Allocator2>& ind);
00142   };
00143 
00144 
00146   template <class T, class Prop, class Allocator>
00147   class Matrix<T, Prop, RowSymSparse, Allocator>:
00148     public Matrix_SymSparse<T, Prop, RowSymSparse, Allocator>
00149   {
00150     // typedef declaration.
00151   public:
00152     typedef typename Allocator::value_type value_type;
00153     typedef Prop property;
00154     typedef RowSymSparse storage;
00155     typedef Allocator allocator;
00156 
00157   public:
00158     Matrix()  throw();
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 
00171 } // namespace Seldon.
00172 
00173 #define SELDON_FILE_MATRIX_SYMSPARSE_HXX
00174 #endif