matrix_sparse/Matrix_ArrayComplexSparse.hxx

00001 // Copyright (C) 2003-2009 Marc Duruflé
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_ARRAY_COMPLEX_SPARSE_HXX
00023 
00024 namespace Seldon
00025 {
00026 
00028 
00035   template <class T, class Prop, class Storage,
00036             class Allocator = SELDON_DEFAULT_ALLOCATOR<T> >
00037   class Matrix_ArrayComplexSparse
00038   {
00039     // typedef declaration.
00040   public:
00041     typedef typename Allocator::value_type value_type;
00042     typedef typename Allocator::pointer pointer;
00043     typedef typename Allocator::const_pointer const_pointer;
00044     typedef typename Allocator::reference reference;
00045     typedef typename Allocator::const_reference const_reference;
00046     typedef complex<T> entry_type;
00047     typedef complex<T> access_type;
00048     typedef complex<T> const_access_type;
00049 
00050     // Attributes.
00051   protected:
00053     int m_;
00055     int n_;
00057     Vector<Vector<T, VectSparse, Allocator>, VectFull,
00058            NewAlloc<Vector<T, VectSparse, Allocator> > > val_real_;
00060     Vector<Vector<T, VectSparse, Allocator>, VectFull,
00061            NewAlloc<Vector<T, VectSparse, Allocator> > > val_imag_;
00062 
00063     // Methods.
00064   public:
00065     // Constructors.
00066     Matrix_ArrayComplexSparse();
00067     Matrix_ArrayComplexSparse(int i, int j);
00068 
00069     // Destructor.
00070     ~Matrix_ArrayComplexSparse();
00071     void Clear();
00072 
00073     // Memory management.
00074     void Reallocate(int i, int j);
00075     void Resize(int i, int j);
00076 
00077     // Basic methods.
00078     int GetM() const;
00079     int GetN() const;
00080     int GetM(const SeldonTranspose& status) const;
00081     int GetN(const SeldonTranspose& status) const;
00082     int GetRealNonZeros() const;
00083     int GetImagNonZeros() const;
00084     int GetRealDataSize() const;
00085     int GetImagDataSize() const;
00086     int GetDataSize() const;
00087     int* GetRealInd(int i) const;
00088     int* GetImagInd(int i) const;
00089     T* GetRealData(int i) const;
00090     T* GetImagData(int i) const;
00091     Vector<T, VectSparse, Allocator>* GetRealData() const;
00092     Vector<T, VectSparse, Allocator>* GetImagData() const;
00093 
00094     // Element acess and affectation.
00095     complex<T> operator() (int i, int j) const;
00096     complex<T>& Val(int i, int j);
00097     const complex<T>& Val(int i, int j) const;
00098     complex<T>& Get(int i, int j);
00099     const complex<T>& Get(int i, int j) const;
00100 
00101     T& ValReal(int i, int j);
00102     const T& ValReal(int i, int j) const;
00103     T& ValImag(int i, int j);
00104     const T& ValImag(int i, int j) const;
00105     T& GetReal(int i, int j);
00106     const T& GetReal(int i, int j) const;
00107     T& GetImag(int i, int j);
00108     const T& GetImag(int i, int j) const;
00109 
00110     void Set(int i, int j, const complex<T>& x);
00111 
00112     const T& ValueReal(int num_row,int i) const;
00113     T& ValueReal(int num_row,int i);
00114     int IndexReal(int num_row,int i) const;
00115     int& IndexReal(int num_row,int i);
00116     const T& ValueImag(int num_row,int i) const;
00117     T& ValueImag(int num_row,int i);
00118     int IndexImag(int num_row,int i) const;
00119     int& IndexImag(int num_row,int i);
00120 
00121     void SetRealData(int, int, Vector<T, VectSparse, Allocator>*);
00122     void SetImagData(int, int, Vector<T, VectSparse, Allocator>*);
00123     void SetRealData(int, int, T*, int*);
00124     void SetImagData(int, int, T*, int*);
00125     void NullifyReal(int i);
00126     void NullifyImag(int i);
00127     void NullifyReal();
00128     void NullifyImag();
00129 
00130     // Convenient functions.
00131     void Print() const;
00132     void Write(string FileName) const;
00133     void Write(ostream& FileStream) const;
00134     void WriteText(string FileName) const;
00135     void WriteText(ostream& FileStream) const;
00136     void Read(string FileName);
00137     void Read(istream& FileStream);
00138     void ReadText(string FileName);
00139     void ReadText(istream& FileStream);
00140 
00141     void Assemble();
00142     template<class T0>
00143     void RemoveSmallEntry(const T0& epsilon);
00144 
00145     void SetIdentity();
00146     void Zero();
00147     void Fill();
00148     template <class T0>
00149     void Fill(const complex<T0>& x);
00150     template <class T0>
00151     Matrix_ArrayComplexSparse<T, Prop, Storage, Allocator>& operator=
00152     (const complex<T0>& x);
00153     void FillRand();
00154 
00155   };
00156 
00157 
00159   template <class T, class Prop, class Allocator>
00160   class Matrix<T, Prop, ArrayColComplexSparse, Allocator> :
00161     public Matrix_ArrayComplexSparse<T, Prop, ArrayColComplexSparse, Allocator>
00162   {
00163     // typedef declaration.
00164   public:
00165     typedef typename Allocator::value_type value_type;
00166     typedef Prop property;
00167     typedef ArrayColComplexSparse storage;
00168     typedef Allocator allocator;
00169 
00170   public:
00171     Matrix();
00172     Matrix(int i, int j);
00173 
00174     // Memory management.
00175     void ClearRealColumn(int i);
00176     void ClearImagColumn(int i);
00177     void ReallocateRealColumn(int i, int j);
00178     void ReallocateImagColumn(int i, int j);
00179     void ResizeRealColumn(int i, int j);
00180     void ResizeImagColumn(int i, int j);
00181     void SwapRealColumn(int i, int i_);
00182     void SwapImagColumn(int i, int i_);
00183     void ReplaceRealIndexColumn(int i, IVect& new_index);
00184     void ReplaceImagIndexColumn(int i, IVect& new_index);
00185 
00186     int GetRealColumnSize(int i) const;
00187     int GetImagColumnSize(int i) const;
00188     void PrintRealColumn(int i) const;
00189     void PrintImagColumn(int i) const;
00190     void AssembleRealColumn(int i);
00191     void AssembleImagColumn(int i);
00192 
00193     void AddInteraction(int i, int j, const complex<T>& val);
00194 
00195     template<class Alloc1>
00196     void AddInteractionRow(int i, int nb, const IVect& col,
00197                            const Vector<complex<T>, VectFull, Alloc1>& val);
00198     template<class Alloc1>
00199     void AddInteractionColumn(int i, int nb, const IVect& row,
00200                               const Vector<complex<T>, VectFull,
00201                               Alloc1>& val);
00202   };
00203 
00204 
00206   template <class T, class Prop, class Allocator>
00207   class Matrix<T, Prop, ArrayRowComplexSparse, Allocator> :
00208     public Matrix_ArrayComplexSparse<T, Prop, ArrayRowComplexSparse, Allocator>
00209   {
00210     // typedef declaration.
00211   public:
00212     typedef typename Allocator::value_type value_type;
00213     typedef Prop property;
00214     typedef ArrayRowComplexSparse storage;
00215     typedef Allocator allocator;
00216 
00217   public:
00218     Matrix();
00219     Matrix(int i, int j);
00220 
00221     // Memory management.
00222     void ClearRealRow(int i);
00223     void ClearImagRow(int i);
00224     void ReallocateRealRow(int i, int j);
00225     void ReallocateImagRow(int i, int j);
00226     void ResizeRealRow(int i, int j);
00227     void ResizeImagRow(int i, int j);
00228     void SwapRealRow(int i, int i_);
00229     void SwapImagRow(int i, int i_);
00230     void ReplaceRealIndexRow(int i, IVect& new_index);
00231     void ReplaceImagIndexRow(int i, IVect& new_index);
00232 
00233     int GetRealRowSize(int i) const;
00234     int GetImagRowSize(int i) const;
00235     void PrintRealRow(int i) const;
00236     void PrintImagRow(int i) const;
00237     void AssembleRealRow(int i);
00238     void AssembleImagRow(int i);
00239 
00240     void AddInteraction(int i, int j, const complex<T>& val);
00241 
00242     template<class Alloc1>
00243     void AddInteractionRow(int i, int nb, const IVect& col,
00244                            const Vector<complex<T>, VectFull, Alloc1>& val);
00245     template<class Alloc1>
00246     void AddInteractionColumn(int i, int nb, const IVect& row,
00247                               const Vector<complex<T>, VectFull,
00248                               Alloc1>& val);
00249   };
00250 
00251 
00253   template <class T, class Prop, class Allocator>
00254   class Matrix<T, Prop, ArrayColSymComplexSparse, Allocator>:
00255     public Matrix_ArrayComplexSparse<T, Prop, ArrayColSymComplexSparse, Allocator>
00256   {
00257     // typedef declaration.
00258   public:
00259     typedef typename Allocator::value_type value_type;
00260     typedef Prop property;
00261     typedef ArrayColSymComplexSparse storage;
00262     typedef Allocator allocator;
00263 
00264   public:
00265     Matrix();
00266     Matrix(int i, int j);
00267 
00268     complex<T> operator() (int i, int j) const;
00269 
00270     T& ValReal(int i, int j);
00271     const T& ValReal(int i, int j) const;
00272     T& ValImag(int i, int j);
00273     const T& ValImag(int i, int j) const;
00274     T& GetReal(int i, int j);
00275     const T& GetReal(int i, int j) const;
00276     T& GetImag(int i, int j);
00277     const T& GetImag(int i, int j) const;
00278 
00279     void Set(int i, int j, const complex<T>& x);
00280 
00281     // Memory management.
00282     void ClearRealColumn(int i);
00283     void ClearImagColumn(int i);
00284     void ReallocateRealColumn(int i, int j);
00285     void ReallocateImagColumn(int i, int j);
00286     void ResizeRealColumn(int i, int j);
00287     void ResizeImagColumn(int i, int j);
00288     void SwapRealColumn(int i, int i_);
00289     void SwapImagColumn(int i, int i_);
00290     void ReplaceRealIndexColumn(int i, IVect& new_index);
00291     void ReplaceImagIndexColumn(int i, IVect& new_index);
00292 
00293     int GetRealColumnSize(int i) const;
00294     int GetImagColumnSize(int i) const;
00295     void PrintRealColumn(int i) const;
00296     void PrintImagColumn(int i) const;
00297     void AssembleRealColumn(int i);
00298     void AssembleImagColumn(int i);
00299 
00300     void AddInteraction(int i, int j, const complex<T>& val);
00301 
00302     template<class Alloc1>
00303     void AddInteractionRow(int i, int nb, const IVect& col,
00304                            const Vector<complex<T>, VectFull, Alloc1>& val);
00305     template<class Alloc1>
00306     void AddInteractionColumn(int i, int nb, const IVect& row,
00307                               const Vector<complex<T>, VectFull,
00308                               Alloc1>& val);
00309   };
00310 
00311 
00313   template <class T, class Prop, class Allocator>
00314   class Matrix<T, Prop, ArrayRowSymComplexSparse, Allocator>:
00315     public Matrix_ArrayComplexSparse<T, Prop, ArrayRowSymComplexSparse, Allocator>
00316   {
00317     // typedef declaration.
00318   public:
00319     typedef typename Allocator::value_type value_type;
00320     typedef Prop property;
00321     typedef ArrayRowSymComplexSparse storage;
00322     typedef Allocator allocator;
00323 
00324   public:
00325     Matrix();
00326     Matrix(int i, int j);
00327 
00328     complex<T> operator() (int i, int j) const;
00329 
00330     T& ValReal(int i, int j);
00331     const T& ValReal(int i, int j) const;
00332     T& ValImag(int i, int j);
00333     const T& ValImag(int i, int j) const;
00334     T& GetReal(int i, int j);
00335     const T& GetReal(int i, int j) const;
00336     T& GetImag(int i, int j);
00337     const T& GetImag(int i, int j) const;
00338 
00339     void Set(int i, int j, const complex<T>& x);
00340 
00341     // Memory management.
00342     void ClearRealRow(int i);
00343     void ClearImagRow(int i);
00344     void ReallocateRealRow(int i, int j);
00345     void ReallocateImagRow(int i, int j);
00346     void ResizeRealRow(int i, int j);
00347     void ResizeImagRow(int i, int j);
00348     void SwapRealRow(int i, int i_);
00349     void SwapImagRow(int i, int i_);
00350     void ReplaceRealIndexRow(int i, IVect& new_index);
00351     void ReplaceImagIndexRow(int i, IVect& new_index);
00352 
00353     int GetRealRowSize(int i) const;
00354     int GetImagRowSize(int i) const;
00355     void PrintRealRow(int i) const;
00356     void PrintImagRow(int i) const;
00357     void AssembleRealRow(int i);
00358     void AssembleImagRow(int i);
00359 
00360     void AddInteraction(int i, int j, const complex<T>& val);
00361 
00362     template<class Alloc1>
00363     void AddInteractionRow(int i, int nb, const IVect& col,
00364                            const Vector<complex<T>, VectFull, Alloc1>& val);
00365     template<class Alloc1>
00366     void AddInteractionColumn(int i, int nb, const IVect& row,
00367                               const Vector<complex<T>, VectFull,
00368                               Alloc1>& val);
00369   };
00370 
00371 
00372 } // namespace Seldon
00373 
00374 #define SELDON_FILE_MATRIX_ARRAY_COMPLEX_SPARSE_HXX
00375 #endif