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& Get(int i, int j);
00089     const T& Get(int i, int j) const;
00090     T& Val(int i, int j);
00091     const T& Val(int i, int j) const;
00092     void Set(int i, int j, const T& x);
00093 
00094     const T& Value(int num_row, int i) const;
00095     T& Value(int num_row, int i);
00096     int Index(int num_row, int i) const;
00097     int& Index(int num_row, int i);
00098 
00099     void SetData(int, int, Vector<T, VectSparse, Allocator>*);
00100     void SetData(int, int, T*, int*);
00101     void Nullify(int i);
00102     void Nullify();
00103 
00104     // Convenient functions.
00105     void Print() const;
00106     void Assemble();
00107     template<class T0>
00108     void RemoveSmallEntry(const T0& epsilon);
00109 
00110     void SetIdentity();
00111     void Zero();
00112     void Fill();
00113     template <class T0>
00114     void Fill(const T0& x);
00115     template <class T0>
00116     Matrix_ArraySparse<T, Prop, Storage, Allocator>& operator= (const T0& x);
00117     void FillRand();
00118 
00119     // Input/output functions.
00120     void Write(string FileName) const;
00121     void Write(ostream& FileStream) const;
00122     void WriteText(string FileName) const;
00123     void WriteText(ostream& FileStream) const;
00124     void Read(string FileName);
00125     void Read(istream& FileStream);
00126     void ReadText(string FileName);
00127     void ReadText(istream& FileStream);
00128 
00129   };
00130 
00131 
00133   template <class T, class Prop, class Allocator>
00134   class Matrix<T, Prop, ArrayColSparse, Allocator> :
00135     public Matrix_ArraySparse<T, Prop, ArrayColSparse, Allocator>
00136   {
00137     // typedef declaration.
00138   public:
00139     typedef typename Allocator::value_type value_type;
00140     typedef Prop property;
00141     typedef ArrayColSparse storage;
00142     typedef Allocator allocator;
00143 
00144   public:
00145     Matrix();
00146     Matrix(int i, int j);
00147 
00148     // Memory management.
00149     void ClearColumn(int i);
00150     void ReallocateColumn(int i, int j);
00151     void ResizeColumn(int i, int j);
00152     void SwapColumn(int i, int i_);
00153     void ReplaceIndexColumn(int i, IVect& new_index);
00154 
00155     int GetColumnSize(int i) const;
00156     void PrintColumn(int i) const;
00157     void AssembleColumn(int i);
00158 
00159     void AddInteraction(int i, int j, const T& val);
00160 
00161     void AddInteractionRow(int, int, int*, T*);
00162     void AddInteractionColumn(int, int, int*, T*);
00163 
00164     template<class Alloc1>
00165     void AddInteractionRow(int i, int nb, const IVect& col,
00166                            const Vector<T, VectFull, Alloc1>& val);
00167     template<class Alloc1>
00168     void AddInteractionColumn(int i, int nb, const IVect& row,
00169                               const Vector<T, VectFull, Alloc1>& val);
00170   };
00171 
00172 
00174   template <class T, class Prop, class Allocator>
00175   class Matrix<T, Prop, ArrayRowSparse, Allocator> :
00176     public Matrix_ArraySparse<T, Prop, ArrayRowSparse, Allocator>
00177   {
00178     // typedef declaration.
00179   public:
00180     typedef typename Allocator::value_type value_type;
00181     typedef Prop property;
00182     typedef ArrayRowSparse storage;
00183     typedef Allocator allocator;
00184 
00185   public:
00186     Matrix();
00187     Matrix(int i, int j);
00188 
00189     // Memory management.
00190     void ClearRow(int i);
00191     void ReallocateRow(int i, int j);
00192     void ResizeRow(int i, int j);
00193     void SwapRow(int i, int i_);
00194     void ReplaceIndexRow(int i, IVect& new_index);
00195 
00196     int GetRowSize(int i) const;
00197     void PrintRow(int i) const;
00198     void AssembleRow(int i);
00199 
00200     void AddInteraction(int i, int j, const T& val);
00201 
00202     void AddInteractionRow(int, int, int*, T*);
00203     void AddInteractionColumn(int, int, int*, T*);
00204 
00205     template<class Alloc1>
00206     void AddInteractionRow(int i, int nb, const IVect& col,
00207                            const Vector<T, VectFull, Alloc1>& val);
00208     template<class Alloc1>
00209     void AddInteractionColumn(int i, int nb, const IVect& row,
00210                               const Vector<T, VectFull, Alloc1>& val);
00211   };
00212 
00214   template <class T, class Prop, class Allocator>
00215   class Matrix<T, Prop, ArrayColSymSparse, Allocator>:
00216     public Matrix_ArraySparse<T, Prop, ArrayColSymSparse, Allocator>
00217   {
00218     // typedef declaration.
00219   public:
00220     typedef typename Allocator::value_type value_type;
00221     typedef Prop property;
00222     typedef ArrayColSymSparse storage;
00223     typedef Allocator allocator;
00224 
00225   public:
00226     Matrix();
00227     Matrix(int i, int j);
00228 
00229     T operator() (int i, int j) const;
00230     T& Get(int i, int j);
00231     const T& Get(int i, int j) const;
00232     T& Val(int i, int j);
00233     const T& Val(int i, int j) const;
00234     void Set(int i, int j, const T& x);
00235 
00236     // Memory management.
00237     void ClearColumn(int i);
00238     void ReallocateColumn(int i, int j);
00239     void ResizeColumn(int i, int j);
00240     void SwapColumn(int i, int i_);
00241     void ReplaceIndexColumn(int i, IVect& new_index);
00242 
00243     int GetColumnSize(int i) const;
00244     void PrintColumn(int i) const;
00245     void AssembleColumn(int i);
00246 
00247     void AddInteraction(int i, int j, const T& val);
00248 
00249     void AddInteractionRow(int, int, int*, T*);
00250     void AddInteractionColumn(int, int, int*, T*);
00251 
00252     template<class Alloc1>
00253     void AddInteractionRow(int i, int nb, const IVect& col,
00254                            const Vector<T, VectFull, Alloc1>& val);
00255     template<class Alloc1>
00256     void AddInteractionColumn(int i, int nb, const IVect& row,
00257                               const Vector<T, VectFull, Alloc1>& val);
00258   };
00259 
00260 
00262   template <class T, class Prop, class Allocator>
00263   class Matrix<T, Prop, ArrayRowSymSparse, Allocator>:
00264     public Matrix_ArraySparse<T, Prop, ArrayRowSymSparse, Allocator>
00265   {
00266     // typedef declaration.
00267   public:
00268     typedef typename Allocator::value_type value_type;
00269     typedef Prop property;
00270     typedef ArrayRowSymSparse storage;
00271     typedef Allocator allocator;
00272 
00273   public:
00274     Matrix();
00275     Matrix(int i, int j);
00276 
00277     T operator() (int i, int j) const;
00278     T& Get(int i, int j);
00279     const T& Get(int i, int j) const;
00280     T& Val(int i, int j);
00281     const T& Val(int i, int j) const;
00282     void Set(int i, int j, const T& x);
00283 
00284     // Memory management.
00285     void ClearRow(int i);
00286     void ReallocateRow(int i, int j);
00287     void ResizeRow(int i, int j);
00288     void SwapRow(int i, int i_);
00289     void ReplaceIndexRow(int i, IVect& new_index);
00290 
00291     int GetRowSize(int i) const;
00292     void PrintRow(int i) const;
00293     void AssembleRow(int i);
00294 
00295     void AddInteraction(int i, int j, const T& val);
00296 
00297     void AddInteractionRow(int, int, int*, T*);
00298     void AddInteractionColumn(int, int, int*, T*);
00299 
00300     template<class Alloc1>
00301     void AddInteractionRow(int i, int nb, const IVect& col,
00302                            const Vector<T, VectFull, Alloc1>& val);
00303     template<class Alloc1>
00304     void AddInteractionColumn(int i, int nb, const IVect& row,
00305                               const Vector<T, VectFull, Alloc1>& val);
00306   };
00307 
00308 } // namespace Seldon
00309 
00310 #define SELDON_FILE_MATRIX_ARRAY_SPARSE_HXX
00311 #endif