CPPLapack
 All Classes Files Functions Variables Friends
dgematrix-misc.hpp
Go to the documentation of this file.
00001 //=============================================================================
00002 /*! clear all the matrix data and set the sizes 0 */
00003 inline void dgematrix::clear()
00004 {VERBOSE_REPORT;
00005   m =0;
00006   n =0;
00007   delete [] array;
00008   delete [] darray;
00009   array =NULL;
00010   darray =NULL;
00011 }
00012 
00013 //=============================================================================
00014 /*! change the matrix into a zero matrix */
00015 inline dgematrix& dgematrix::zero()
00016 {VERBOSE_REPORT;
00017   for(long i=0; i<m*n; i++){ array[i] =0.0; }
00018   return *this;
00019 }
00020 
00021 //=============================================================================
00022 /*! change the matrix into an identity matrix */
00023 inline dgematrix& dgematrix::identity()
00024 {VERBOSE_REPORT;
00025 #ifdef  CPPL_DEBUG
00026   if(m!=n){
00027     ERROR_REPORT;
00028     std::cerr << "Only square matrix can be a identity matrix." << std::endl
00029               << "The matrix size was " << m << "x" << n << "." << std::endl;
00030     exit(1);
00031   }
00032 #endif//CPPL_DEBUG
00033   
00034   for(long i=0; i<m*n; i++){ array[i] =0.0; }
00035   for(long i=0; i<m; i++){ operator()(i,i) =1.0; }
00036   return *this;
00037 }
00038 
00039 //=============================================================================
00040 /*! change sign(+/-) of the matrix */
00041 inline void dgematrix::chsign()
00042 {VERBOSE_REPORT;
00043   for(long i=0; i<m*n; i++){ array[i] =-array[i]; }
00044 }
00045 
00046 //=============================================================================
00047 /*! make a deep copy of the matrix */
00048 inline void dgematrix::copy(const dgematrix& mat)
00049 {VERBOSE_REPORT;
00050   m =mat.m;
00051   n =mat.n;
00052   delete [] array;
00053   array =new double[mat.m*mat.n];
00054   delete [] darray;
00055   darray =new double*[n];
00056   for(int i=0; i<n; i++){ darray[i] =&array[i*m]; }
00057   
00058   dcopy_(mat.m*mat.n, mat.array, 1, array, 1);
00059 }
00060 
00061 //=============================================================================
00062 /*! make a shallow copy of the matrix\n
00063   This function is not designed to be used in project codes. */
00064 inline void dgematrix::shallow_copy(const _dgematrix& mat)
00065 {VERBOSE_REPORT;
00066   m =mat.m;
00067   n =mat.n;
00068   delete [] array;
00069   array =mat.array;
00070   delete [] darray;
00071   darray =mat.darray;
00072   
00073   mat.nullify();
00074 }
00075 
00076 //=============================================================================
00077 /*! resize the matrix */
00078 inline dgematrix& dgematrix::resize(const long& _m, const long& _n)
00079 {VERBOSE_REPORT;
00080 #ifdef  CPPL_DEBUG
00081   if( _m<0 || _n<0 ){
00082     ERROR_REPORT;
00083     std::cerr << "Matrix sizes must be positive integers." << std::endl
00084               << "Your input was (" << _m << "," << _n << ")." << std::endl;
00085     exit(1);
00086   }
00087 #endif//CPPL_DEBUG
00088   
00089   m =_m;
00090   n =_n;
00091   delete [] array;
00092   array =new double[m*n];
00093   delete [] darray;
00094   darray =new double*[n];
00095   for(int i=0; i<n; i++){ darray[i] =&array[i*m]; }
00096   
00097   return *this;
00098 }
00099 
00100 ///////////////////////////////////////////////////////////////////////////////
00101 ///////////////////////////////////////////////////////////////////////////////
00102 ///////////////////////////////////////////////////////////////////////////////
00103 
00104 //=============================================================================
00105 /*! get row of the matrix */
00106 inline _drovector dgematrix::row(const long& _m) const
00107 {VERBOSE_REPORT;
00108 #ifdef  CPPL_DEBUG
00109   if( _m<0 || _m>m ){
00110     ERROR_REPORT;
00111     std::cerr << "Input row number must be between 0 and " << m << "." << std::endl
00112               << "Your input was " << _m << "." << std::endl;
00113     exit(1);
00114   }
00115 #endif//CPPL_DEBUG
00116   
00117   drovector v(n);
00118   for(long j=0; j<n; j++){ v(j)=(*this)(_m,j); }
00119   return _(v);
00120 }
00121 
00122 //=============================================================================
00123 /*! get column of the matrix */
00124 inline _dcovector dgematrix::col(const long& _n) const
00125 {VERBOSE_REPORT;
00126 #ifdef  CPPL_DEBUG
00127   if( _n<0 || _n>n ){
00128     ERROR_REPORT;
00129     std::cerr << "Input row number must be between 0 and " << n << "." << std::endl
00130               << "Your input was " << _n << "." << std::endl;
00131     exit(1);
00132   }
00133 #endif//CPPL_DEBUG
00134   
00135   dcovector v(m);
00136   for(long i=0; i<m; i++){ v(i)=(*this)(i,_n); }
00137   return _(v);
00138 }
00139 
00140 ///////////////////////////////////////////////////////////////////////////////
00141 ///////////////////////////////////////////////////////////////////////////////
00142 ///////////////////////////////////////////////////////////////////////////////
00143 
00144 //=============================================================================
00145 /*! swap two matrices */
00146 inline void swap(dgematrix& A, dgematrix& B)
00147 {VERBOSE_REPORT;
00148   long A_m =A.m, A_n =A.n;
00149   double* A_array =A.array;
00150   double** A_darray=A.darray;
00151   A.m=B.m; A.n=B.n; A.array=B.array; A.darray=B.darray;
00152   B.m=A_m; B.n=A_n; B.array=A_array; B.darray=A_darray;
00153 }
00154 
00155 //=============================================================================
00156 /*! convert user object to smart-temporary object */
00157 inline _dgematrix _(dgematrix& mat)
00158 {VERBOSE_REPORT;
00159   _dgematrix newmat;
00160   
00161   //////// shallow copy ////////
00162   newmat.m =mat.m;
00163   newmat.n =mat.n;
00164   newmat.array =mat.array;
00165   newmat.darray =mat.darray;
00166   
00167   //////// nullify ////////
00168   mat.m =0;
00169   mat.n =0;
00170   mat.array =NULL;
00171   mat.darray =NULL;
00172   
00173   return newmat;
00174 }
 All Classes Files Functions Variables Friends