matrix/Matrix_Base.cxx

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 #ifndef SELDON_FILE_MATRIX_BASE_CXX
00021 
00022 #include "Matrix_Base.hxx"
00023 
00024 namespace Seldon
00025 {
00026 
00027 
00028   /****************
00029    * CONSTRUCTORS *
00030    ****************/
00031 
00032 
00034 
00037   template <class T, class Allocator>
00038   inline Matrix_Base<T, Allocator>::Matrix_Base()
00039   {
00040     m_ = 0;
00041     n_ = 0;
00042     data_ = NULL;
00043   }
00044 
00045 
00047 
00052   template <class T, class Allocator>
00053   inline Matrix_Base<T, Allocator>::Matrix_Base(int i, int j)
00054   {
00055 #ifdef SELDON_CHECK_DIMENSIONS
00056     if (i < 0 || j < 0)
00057       throw WrongDim("Matrix_Base::Matrix_Base(int, int)",
00058                      "Unable to define a matrix with size "
00059                      + to_str(i) + " by " + to_str(j) + ".");
00060 #endif
00061 
00062     m_ = i;
00063     n_ = j;
00064     data_ = NULL;
00065   }
00066 
00067 
00069 
00073   template <class T, class Allocator>
00074   inline Matrix_Base<T, Allocator>::
00075   Matrix_Base(const Matrix_Base<T, Allocator>& A)
00076   {
00077     m_ = A.GetM();
00078     n_ = A.GetN();
00079     data_ = NULL;
00080   }
00081 
00082 
00083   /**************
00084    * DESTRUCTOR *
00085    **************/
00086 
00087 
00089 
00092   template <class T, class Allocator>
00093   inline Matrix_Base<T, Allocator>::~Matrix_Base()
00094   {
00095 
00096   }
00097 
00098 
00099   /*******************
00100    * BASIC FUNCTIONS *
00101    *******************/
00102 
00103 
00105 
00108   template <class T, class Allocator>
00109   int Matrix_Base<T, Allocator>::GetM() const
00110   {
00111     return m_;
00112   }
00113 
00114 
00116 
00119   template <class T, class Allocator>
00120   int Matrix_Base<T, Allocator>::GetN() const
00121   {
00122     return n_;
00123   }
00124 
00125 
00127 
00131   template <class T, class Allocator>
00132   int Matrix_Base<T, Allocator>::GetM(const SeldonTranspose& status) const
00133   {
00134     if (status.NoTrans())
00135       return m_;
00136     else
00137       return n_;
00138   }
00139 
00140 
00142 
00146   template <class T, class Allocator>
00147   int Matrix_Base<T, Allocator>::GetN(const SeldonTranspose& status) const
00148   {
00149     if (status.NoTrans())
00150       return n_;
00151     else
00152       return m_;
00153   }
00154 
00155 
00156 #ifdef SELDON_WITH_BLAS
00157 
00158 
00162   template <class T, class Allocator>
00163   int Matrix_Base<T, Allocator>::GetM(const CBLAS_TRANSPOSE& status) const
00164   {
00165     if (status == CblasNoTrans)
00166       return m_;
00167     else
00168       return n_;
00169   }
00170 #endif
00171 
00172 
00173 #ifdef SELDON_WITH_BLAS
00174 
00175 
00179   template <class T, class Allocator>
00180   int Matrix_Base<T, Allocator>::GetN(const CBLAS_TRANSPOSE& status) const
00181   {
00182     if (status == CblasNoTrans)
00183       return n_;
00184     else
00185       return m_;
00186   }
00187 #endif
00188 
00189 
00191 
00196   template <class T, class Allocator>
00197   int Matrix_Base<T, Allocator>::GetSize() const
00198   {
00199     return m_ * n_;
00200   }
00201 
00202 
00204 
00208   template <class T, class Allocator>
00209   typename Matrix_Base<T, Allocator>::pointer
00210   Matrix_Base<T, Allocator>::GetData() const
00211   {
00212     return data_;
00213   }
00214 
00215 
00217 
00221   template <class T, class Allocator>
00222   typename Matrix_Base<T, Allocator>::const_pointer
00223   Matrix_Base<T, Allocator>::GetDataConst() const
00224   {
00225     return reinterpret_cast<typename Matrix_Base<T,
00226       Allocator>::const_pointer>(data_);
00227   }
00228 
00229 
00231 
00235   template <class T, class Allocator>
00236   void* Matrix_Base<T, Allocator>::GetDataVoid() const
00237   {
00238     return reinterpret_cast<void*>(data_);
00239   }
00240 
00241 
00243 
00248   template <class T, class Allocator>
00249   const void* Matrix_Base<T, Allocator>::GetDataConstVoid() const
00250   {
00251     return reinterpret_cast<const void*>(data_);
00252   }
00253 
00254 
00256 
00259   template <class T, class Allocator>
00260   Allocator& Matrix_Base<T, Allocator>::GetAllocator()
00261   {
00262     return allocator_;
00263   }
00264 
00265 
00266   // operator<< overloaded for matrices.
00272   template <class T, class Prop, class Storage, class Allocator>
00273   ostream& operator << (ostream& out,
00274                         const Matrix<T, Prop, Storage, Allocator>& A)
00275   {
00276 
00277     for (int i = 0; i < A.GetM(); i++)
00278       {
00279         for (int j = 0; j < A.GetN(); j++)
00280           out << A(i, j) << '\t';
00281         out << endl;
00282       }
00283 
00284     return out;
00285 
00286   }
00287 
00288 
00289 } // namespace Seldon.
00290 
00291 #define SELDON_FILE_MATRIX_BASE_CXX
00292 #endif