matrix_sparse/Matrix_ArraySparse.hxx

00001 // Copyright (C) 2003-2009 Marc Duruflé
00002 // Copyright (C) 2001-2009 Vivien Mallet
00003 //
00004 // This file is part of the linear-algebra library Seldon,
00005 // http://seldon.sourceforge.net/.
00006 //
00007 // Seldon is free software; you can redistribute it and/or modify it under the
00008 // terms of the GNU Lesser General Public License as published by the Free
00009 // Software Foundation; either version 2.1 of the License, or (at your option)
00010 // any later version.
00011 //
00012 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
00013 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
00015 // more details.
00016 //
00017 // You should have received a copy of the GNU Lesser General Public License
00018 // along with Seldon. If not, see http://www.gnu.org/licenses/.
00019 
00020 
00021 // To be included by Seldon.hxx
00022 
00023 #ifndef SELDON_FILE_MATRIX_ARRAY_SPARSE_HXX
00024 
00025 namespace Seldon
00026 {
00027 
00029 
00036   template <class T, class Prop, class Storage,
00037             class Allocator = SELDON_DEFAULT_ALLOCATOR<T> >
00038   class Matrix_ArraySparse
00039   {
00040     // typedef declaration.
00041   public:
00042     typedef typename Allocator::value_type value_type;
00043     typedef typename Allocator::pointer pointer;
00044     typedef typename Allocator::const_pointer const_pointer;
00045     typedef typename Allocator::reference reference;
00046     typedef typename Allocator::const_reference const_reference;
00047     typedef T entry_type;
00048     typedef T& access_type;
00049     typedef T const_access_type;
00050 
00051     // Attributes.
00052   protected:
00054     int m_;
00056     int n_;
00058     Vector<Vector<T, VectSparse, Allocator>, VectFull,
00059            NewAlloc<Vector<T, VectSparse, Allocator> > > val_;
00060 
00061   public:
00062     // Constructors.
00063     Matrix_ArraySparse();
00064     Matrix_ArraySparse(int i, int j);
00065 
00066     // Destructor.
00067     ~Matrix_ArraySparse();
00068     void Clear();
00069 
00070     // Memory management.
00071     void Reallocate(int i,int j);
00072     void Resize(int i,int j);
00073 
00074     // Basic methods.
00075     int GetM() const;
00076     int GetN() const;
00077     int GetM(const SeldonTranspose& status) const;
00078     int GetN(const SeldonTranspose& status) const;
00079     int GetNonZeros() const;
00080     int GetDataSize() const;
00081     int* GetIndex(int i) const;
00082     T* GetData(int i) const;
00083 
00084     Vector<T, VectSparse, Allocator>* GetData() const;
00085 
00086     // Element acess and affectation.
00087     T operator() (int i, int j) const;
00088     T& operator() (int i, int j);
00089     T& Val(int i, int j);
00090     const T& Val(int i, int j) const;
00091 
00092     const T& Value(int num_row, int i) const;
00093     T& Value(int num_row, int i);
00094     int Index(int num_row, int i) const;
00095     int& Index(int num_row, int i);
00096 
00097     void SetData(int, int, Vector<T, VectSparse, Allocator>*);
00098     void SetData(int, int, T*, int*);
00099     void Nullify(int i);
00100     void Nullify();
00101 
00102     // Convenient functions.
00103     void Print() const;
00104     void Assemble();
00105     template<class T0>
00106     void RemoveSmallEntry(const T0& epsilon);
00107 
00108     void SetIdentity();
00109     void Zero();
00110     void Fill();
00111     template <class T0>
00112     void Fill(const T0& x);
00113     template <class T0>
00114     Matrix_ArraySparse<T, Prop, Storage, Allocator>& operator= (const T0& x);
00115     void FillRand();
00116 
00117     // Input/output functions.
00118     void Write(string FileName) const;
00119     void Write(ostream& FileStream) const;
00120     void WriteText(string FileName) const;
00121     void WriteText(ostream& FileStream) const;
00122     void Read(string FileName);
00123     void Read(istream& FileStream);
00124     void ReadText(string FileName);
00125     void ReadText(istream& FileStream);
00126 
00127   };
00128 
00129 
00131   template <class T, class Prop, class Allocator>
00132   class Matrix<T, Prop, ArrayColSparse, Allocator> :
00133     public Matrix_ArraySparse<T, Prop, ArrayColSparse, Allocator>
00134   {
00135     // typedef declaration.
00136   public:
00137     typedef typename Allocator::value_type value_type;
00138     typedef Prop property;
00139     typedef ArrayColSparse storage;
00140     typedef Allocator allocator;
00141 
00142   public:
00143     Matrix()  throw();
00144     Matrix(int i, int j);
00145 
00146     // Memory management.
00147     void ClearColumn(int i);
00148     void ReallocateColumn(int i, int j);
00149     void ResizeColumn(int i, int j);
00150     void SwapColumn(int i, int i_);
00151     void ReplaceIndexColumn(int i, IVect& new_index);
00152 
00153     int GetColumnSize(int i) const;
00154     void PrintColumn(int i) const;
00155     void AssembleColumn(int i);
00156 
00157     void AddInteraction(int i, int j, const T& val);
00158 
00159     void AddInteractionRow(int, int, int*, T*);
00160     void AddInteractionColumn(int, int, int*, T*);
00161 
00162     template<class Alloc1>
00163     void AddInteractionRow(int i, int nb, const IVect& col,
00164                            const Vector<T, VectFull, Alloc1>& val);
00165     template<class Alloc1>
00166     void AddInteractionColumn(int i, int nb, const IVect& row,
00167                               const Vector<T, VectFull, Alloc1>& val);
00168   };
00169 
00170 
00172   template <class T, class Prop, class Allocator>
00173   class Matrix<T, Prop, ArrayRowSparse, Allocator> :
00174     public Matrix_ArraySparse<T, Prop, ArrayRowSparse, Allocator>
00175   {
00176     // typedef declaration.
00177   public:
00178     typedef typename Allocator::value_type value_type;
00179     typedef Prop property;
00180     typedef ArrayRowSparse storage;
00181     typedef Allocator allocator;
00182 
00183   public:
00184     Matrix()  throw();
00185     Matrix(int i, int j);
00186 
00187     // Memory management.
00188     void ClearRow(int i);
00189     void ReallocateRow(int i, int j);
00190     void ResizeRow(int i, int j);
00191     void SwapRow(int i, int i_);
00192     void ReplaceIndexRow(int i, IVect& new_index);
00193 
00194     int GetRowSize(int i) const;
00195     void PrintRow(int i) const;
00196     void AssembleRow(int i);
00197 
00198     void AddInteraction(int i, int j, const T& val);
00199 
00200     void AddInteractionRow(int, int, int*, T*);
00201     void AddInteractionColumn(int, int, int*, T*);
00202 
00203     template<class Alloc1>
00204     void AddInteractionRow(int i, int nb, const IVect& col,
00205                            const Vector<T, VectFull, Alloc1>& val);
00206     template<class Alloc1>
00207     void AddInteractionColumn(int i, int nb, const IVect& row,
00208                               const Vector<T, VectFull, Alloc1>& val);
00209   };
00210 
00212   template <class T, class Prop, class Allocator>
00213   class Matrix<T, Prop, ArrayColSymSparse, Allocator>:
00214     public Matrix_ArraySparse<T, Prop, ArrayColSymSparse, Allocator>
00215   {
00216     // typedef declaration.
00217   public:
00218     typedef typename Allocator::value_type value_type;
00219     typedef Prop property;
00220     typedef ArrayColSymSparse storage;
00221     typedef Allocator allocator;
00222 
00223   public:
00224     Matrix()  throw();
00225     Matrix(int i, int j);
00226 
00227     T operator() (int i, int j) const;
00228     T& operator() (int i, int j);
00229 
00230     // Memory management.
00231     void ClearColumn(int i);
00232     void ReallocateColumn(int i, int j);
00233     void ResizeColumn(int i, int j);
00234     void SwapColumn(int i, int i_);
00235     void ReplaceIndexColumn(int i, IVect& new_index);
00236 
00237     int GetColumnSize(int i) const;
00238     void PrintColumn(int i) const;
00239     void AssembleColumn(int i);
00240 
00241     void AddInteraction(int i, int j, const T& val);
00242 
00243     void AddInteractionRow(int, int, int*, T*);
00244     void AddInteractionColumn(int, int, int*, T*);
00245 
00246     template<class Alloc1>
00247     void AddInteractionRow(int i, int nb, const IVect& col,
00248                            const Vector<T, VectFull, Alloc1>& val);
00249     template<class Alloc1>
00250     void AddInteractionColumn(int i, int nb, const IVect& row,
00251                               const Vector<T, VectFull, Alloc1>& val);
00252   };
00253 
00254 
00256   template <class T, class Prop, class Allocator>
00257   class Matrix<T, Prop, ArrayRowSymSparse, Allocator>:
00258     public Matrix_ArraySparse<T, Prop, ArrayRowSymSparse, Allocator>
00259   {
00260     // typedef declaration.
00261   public:
00262     typedef typename Allocator::value_type value_type;
00263     typedef Prop property;
00264     typedef ArrayRowSymSparse storage;
00265     typedef Allocator allocator;
00266 
00267   public:
00268     Matrix()  throw();
00269     Matrix(int i, int j);
00270 
00271     T operator() (int i, int j) const;
00272     T& operator() (int i, int j);
00273 
00274     // Memory management.
00275     void ClearRow(int i);
00276     void ReallocateRow(int i, int j);
00277     void ResizeRow(int i, int j);
00278     void SwapRow(int i, int i_);
00279     void ReplaceIndexRow(int i, IVect& new_index);
00280 
00281     int GetRowSize(int i) const;
00282     void PrintRow(int i) const;
00283     void AssembleRow(int i);
00284 
00285     void AddInteraction(int i, int j, const T& val);
00286 
00287     void AddInteractionRow(int, int, int*, T*);
00288     void AddInteractionColumn(int, int, int*, T*);
00289 
00290     template<class Alloc1>
00291     void AddInteractionRow(int i, int nb, const IVect& col,
00292                            const Vector<T, VectFull, Alloc1>& val);
00293     template<class Alloc1>
00294     void AddInteractionColumn(int i, int nb, const IVect& row,
00295                               const Vector<T, VectFull, Alloc1>& val);
00296   };
00297 
00298 } // namespace Seldon
00299 
00300 #define SELDON_FILE_MATRIX_ARRAY_SPARSE_HXX
00301 #endif