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   }
00080 
00081 
00082   /**************
00083    * DESTRUCTOR *
00084    **************/
00085 
00086 
00088 
00091   template <class T, class Allocator>
00092   inline Matrix_Base<T, Allocator>::~Matrix_Base()
00093   {
00094 
00095   }
00096 
00097 
00098   /*******************
00099    * BASIC FUNCTIONS *
00100    *******************/
00101 
00102 
00104 
00107   template <class T, class Allocator>
00108   int Matrix_Base<T, Allocator>::GetM() const
00109   {
00110     return m_;
00111   }
00112 
00113 
00115 
00118   template <class T, class Allocator>
00119   int Matrix_Base<T, Allocator>::GetN() const
00120   {
00121     return n_;
00122   }
00123 
00124 
00126 
00130   template <class T, class Allocator>
00131   int Matrix_Base<T, Allocator>::GetM(const SeldonTranspose& status) const
00132   {
00133     if (status.NoTrans())
00134       return m_;
00135     else
00136       return n_;
00137   }
00138 
00139 
00141 
00145   template <class T, class Allocator>
00146   int Matrix_Base<T, Allocator>::GetN(const SeldonTranspose& status) const
00147   {
00148     if (status.NoTrans())
00149       return n_;
00150     else
00151       return m_;
00152   }
00153 
00154 
00155 #ifdef SELDON_WITH_BLAS
00156 
00157 
00161   template <class T, class Allocator>
00162   int Matrix_Base<T, Allocator>::GetM(const CBLAS_TRANSPOSE& status) const
00163   {
00164     if (status == CblasNoTrans)
00165       return m_;
00166     else
00167       return n_;
00168   }
00169 #endif
00170 
00171 
00172 #ifdef SELDON_WITH_BLAS
00173 
00174 
00178   template <class T, class Allocator>
00179   int Matrix_Base<T, Allocator>::GetN(const CBLAS_TRANSPOSE& status) const
00180   {
00181     if (status == CblasNoTrans)
00182       return n_;
00183     else
00184       return m_;
00185   }
00186 #endif
00187 
00188 
00190 
00195   template <class T, class Allocator>
00196   int Matrix_Base<T, Allocator>::GetSize() const
00197   {
00198     return m_ * n_;
00199   }
00200 
00201 
00203 
00207   template <class T, class Allocator>
00208   typename Matrix_Base<T, Allocator>::pointer
00209   Matrix_Base<T, Allocator>::GetData() const
00210   {
00211     return data_;
00212   }
00213 
00214 
00216 
00220   template <class T, class Allocator>
00221   typename Matrix_Base<T, Allocator>::const_pointer
00222   Matrix_Base<T, Allocator>::GetDataConst() const
00223   {
00224     return reinterpret_cast<typename Matrix_Base<T,
00225       Allocator>::const_pointer>(data_);
00226   }
00227 
00228 
00230 
00234   template <class T, class Allocator>
00235   void* Matrix_Base<T, Allocator>::GetDataVoid() const
00236   {
00237     return reinterpret_cast<void*>(data_);
00238   }
00239 
00240 
00242 
00247   template <class T, class Allocator>
00248   const void* Matrix_Base<T, Allocator>::GetDataConstVoid() const
00249   {
00250     return reinterpret_cast<const void*>(data_);
00251   }
00252 
00253 
00255 
00258   template <class T, class Allocator>
00259   Allocator& Matrix_Base<T, Allocator>::GetAllocator()
00260   {
00261     return allocator_;
00262   }
00263 
00264 
00265   // operator<< overloaded for matrices.
00271   template <class T, class Prop, class Storage, class Allocator>
00272   ostream& operator << (ostream& out,
00273                         const Matrix<T, Prop, Storage, Allocator>& A)
00274   {
00275 
00276     for (int i = 0; i < A.GetM(); i++)
00277       {
00278         for (int j = 0; j < A.GetN(); j++)
00279           out << A(i, j) << '\t';
00280         out << endl;
00281       }
00282 
00283     return out;
00284 
00285   }
00286 
00287 
00288 } // namespace Seldon.
00289 
00290 #define SELDON_FILE_MATRIX_BASE_CXX
00291 #endif