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