![]() |
RTXI 1.3
|
00001 // 00002 // File = matrix_T.cpp 00003 // 00004 00005 #include <error.h> 00006 #include "matrix_T.h" 00007 00008 #ifdef _DEBUG 00009 #include <fstream> 00010 extern std::ofstream DebugFile; 00011 //#define _MTX_DEBUG 1 00012 #endif 00013 00014 //--------------------------------------------- 00015 // constructor 00016 template< class T > 00017 matrix<T>::matrix( int row_orig, 00018 int nrows, 00019 int col_orig, 00020 int ncols) 00021 { 00022 if( (nrows <= 0) || (ncols <=0)) 00023 std::cout << "illegal matrix dimension" << std::endl; 00024 _p = new mrep; 00025 #ifdef _MTX_DEBUG 00026 DebugFile << "\nctor for matrix at " << this 00027 << " (mrep = " << (void*)_p << ")" << std::endl; 00028 #endif 00029 _p->length = nrows; 00030 _p->orig_indx = row_orig; 00031 _p->max_indx = row_orig + nrows - 1; 00032 _p->f = new rowvec<T>*[nrows]; 00033 #ifdef _MTX_DEBUG 00034 DebugFile << "v::v(i,i): ptr array " << nrows 00035 << " long alloc at " 00036 << (void*)(_p->f) << std::endl; 00037 #endif 00038 for(int i=0; i<nrows; i++) 00039 _p->f[i] = new rowvec<T>( col_orig, ncols); 00040 _p->refcnt = 1; 00041 Is_Temp = 0; 00042 } 00043 00044 //---------------------------------------------- 00045 template<class T> 00046 matrix<T>::~matrix(void) 00047 { 00048 //rowvec<T> *row_ptr; 00049 00050 #ifdef _MTX_DEBUG 00051 DebugFile << "\ndtor for matrix at " << (void*)this << std::endl; 00052 #endif 00053 if( --_p->refcnt == 0) 00054 { 00055 int nrows = _p->length; 00056 for(int i=0; i<nrows; i++) 00057 delete _p->f[i]; 00058 delete _p->f; 00059 #ifdef _MTX_DEBUG 00060 DebugFile << "\nm::~m(): deleting mrep at " 00061 << (void*)_p << std::endl; 00062 #endif 00063 delete _p; 00064 } 00065 } 00066 //---------------------------------------------- 00067 // row extraction 00068 template < class T > 00069 rowvec<T>& matrix<T>::operator[](int i) 00070 { 00071 return *(_p->f[ (((i>=(_p->orig_indx)) && (i<= _p->max_indx)) ? 00072 (i-(_p->orig_indx)) : 0)]); 00073 } 00074 00075 //-------------------------------------------------- 00076 // post-multiply matrix by a column vector 00077 template < class T > 00078 colvec<T>& matrix<T>::operator*( colvec<T> &v2) 00079 { 00080 // check dimensions 00081 int row_orig = _p->orig_indx; 00082 int nrows = _p->length; 00083 int col_orig = ((_p->f[_p->orig_indx])->p)->orig_indx; 00084 int ncols = ((_p->f[_p->orig_indx])->p)->length; 00085 int vec_orig = v2.p->orig_indx; 00086 int vec_len = v2.p->length; 00087 00088 if(ncols != vec_len) 00089 { 00090 std::cout << "error in matrix method" << std::endl; 00091 return( v2 ); 00092 } 00093 00094 // allocate new vector for result 00095 colvec<T> *v_res = new colvec<T>(row_orig, nrows); 00096 #ifdef _MTX_DEBUG 00097 DebugFile << "\nm::op*(cv): new colvec at " 00098 << (void*)v_res << std::endl; 00099 #endif 00100 v_res->Is_Temp = 1; 00101 00102 // perform multiplication and populate results vector 00103 T sum; 00104 for(int i=0; i<nrows; i++) 00105 { 00106 sum = 0.0; 00107 for(int j=0; j<vec_len; j++) 00108 { 00109 //sum += ((v2.p->f[j]) * (((_p->f[i-(_p->orig_indx)])->p)->f[j])); 00110 sum += ((v2.p->f[j]) * (((_p->f[i])->p)->f[j])); 00111 } 00112 (v_res->p)->f[i] = sum; 00113 } 00114 if(v2.Is_Temp) 00115 { 00116 #ifdef _MTX_DEBUG 00117 DebugFile << "\nm::op*(cv): deleting colvec at " 00118 << (void*)(&v2) << std::endl; 00119 #endif 00120 delete (&v2); 00121 } 00122 if(Is_Temp) 00123 { 00124 #ifdef _MTX_DEBUG 00125 DebugFile << "\nm::op*(cv): deleting matrix at " 00126 << (void*)this << std::endl; 00127 #endif 00128 delete this; 00129 } 00130 return (*v_res); 00131 } 00132 //-------------------------------------------------- 00133 // do element-by-element subtraction 00134 template< class T> 00135 matrix<T>& matrix<T>::operator-=(matrix<T> &m2) 00136 { 00137 int nrows = _p->length; 00138 int ncols = ((_p->f[_p->orig_indx])->p)->length; 00139 for(int i=0; i<nrows; i++) 00140 { 00141 for(int j=0; j<ncols; j++) 00142 { 00143 ((_p->f[i])->p)->f[j] -= (((m2._p)->f[i])->p)->f[j]; 00144 } 00145 } 00146 if(m2.Is_Temp) 00147 { 00148 #ifdef _MTX_DEBUG 00149 DebugFile << "\nm::op-=(m): deleting matrix at " 00150 << (void*)(&m2) << std::endl; 00151 #endif 00152 delete (&m2); 00153 } 00154 return(*this); 00155 } 00156 template matrix<double>; 00157 template matrix<complex>;