00001 #ifndef SPARSEIWRAPPER_HH 00002 #define SPARSEIWRAPPER_HH 00003 00004 #include <complex> 00005 #include "toolbox.hh" 00006 00007 00008 #ifndef MATFILE_PROCESSED_BY_DOXYGEN 00009 using namespace matfile; 00010 namespace concepts { 00011 #endif 00012 00037 template<class Scalar> 00038 class SparseIWrapper 00039 { 00040 00041 // Part 1: mandatory public typedef 00042 public: 00043 typedef ArraySparseWrapper Type; 00044 00045 // Part 2: matrix typedef 00046 public: 00052 typedef 00053 concepts::SparseMatrix<Scalar> MatrixType; 00054 concepts::Array<Scalar> ValType; 00055 concepts::Array<miUINT32_t> CoordType; 00056 00057 00058 00059 // Part 3: internal scalar typedefs 00060 private: 00061 typedef typename NumTraits<Scalar>::Real RealScalar; 00062 typedef std::complex<RealScalar> ComplexScalar; 00063 00064 00065 // Part 4: matrix informations 00066 private: 00067 bool isComplex_; 00068 miUINT32_t nnz_; //miUINT32_t 00069 miUINT32_t cols_; 00070 miUINT32_t rows_; 00071 MATtype realType_, imagType_; 00072 00073 00074 // Part 5: other internal informations 00075 private: 00076 00077 00078 concepts::Array<Scalar>& CCSval_; 00079 concepts::Array<miUINT32_t>& CCScol_; 00080 concepts::Array<miUINT32_t>& CCSrow_; 00081 //concepts::Array<miUINT32_t>& dimX_; 00082 00083 bool matrixReady_; 00084 std::string prCopy_, piCopy_; 00085 00086 00087 // Part 6: public methods 00088 public: 00103 SparseIWrapper(concepts::Array<Scalar>& val, concepts::Array<miUINT32_t>& col,concepts::Array<miUINT32_t>& row)//, concepts::Array<miUINT32_t>& dimX) 00104 :CCSval_(val), CCScol_(col), CCSrow_(row) {}//, dimX_(dimX){ } 00105 00106 00107 00108 public: 00109 void isComplex(bool isComplex) { 00110 isComplex_ = isComplex; 00111 } 00112 00113 void nnz(const miUINT32_t& nnz) { 00114 nnz_ = nnz; 00115 CCSval_.resize(nnz); 00116 } 00117 00118 void size(const miUINT32_t& rows, const miUINT32_t& cols) { 00119 rows_ = rows; 00120 cols_ = cols; 00121 //dimX_.resize(1); 00122 //dimX_[0]=rows; 00123 CCScol_.resize(cols_+1); 00124 CCSrow_.resize(nnz_); 00125 } 00126 00127 void name(const std::string&) { } 00128 00129 miUINT32_t *ir() { // row 00130 00131 return &CCSrow_[0]; 00132 } 00133 00134 miUINT32_t *jc() { //column 00135 return &CCScol_[0]; 00136 } 00137 00138 00139 00140 void realType(const MATtype& realType) { 00141 realType_ = realType; 00142 } 00143 00144 byte *pr() { 00145 if (realType_ == NumTraits<Scalar>::MatType && NumTraits<Scalar>::isComplex == 0) { 00146 matrixReady_ = true; 00147 return reinterpret_cast<byte*>(&CCSval_[0]); //CCSvalR_ 00148 } 00149 matrixReady_ = false; 00150 00151 00152 prCopy_.reserve(nnz_*typeSize(realType_)); 00153 return &prCopy_[0]; 00154 } 00155 00156 void imagType(const MATtype& imagType) { 00157 imagType_ = imagType; 00158 } 00159 00160 byte *pi() { 00161 if (NumTraits<Scalar>::isComplex == 0) 00162 return NULL; 00163 00164 piCopy_.reserve(nnz_*typeSize(imagType_)); 00165 return &piCopy_[0]; 00166 } 00167 00168 // Part 8: private internal methods 00169 private: 00170 template<class Other> void copy(bool real = true) { 00171 if (NumTraits<Scalar>::isComplex != 0) { 00172 if (real) { 00173 Other *pr = reinterpret_cast<Other*>(&prCopy_[0]); 00174 ComplexScalar *v = reinterpret_cast<ComplexScalar*>(&(CCSval_[0])); //CCSvalR_ 00175 for (uint i = 0; i < nnz_; ++i) 00176 *v++ = *pr++; 00177 } else if (!real && isComplex_) { 00178 Other *pi = reinterpret_cast<Other*>(&piCopy_[0]); 00179 ComplexScalar *v = reinterpret_cast<ComplexScalar*>(&(CCSval_[0])); //CCSvalI_ 00180 for (uint i = 0; i < nnz_; ++i) 00181 *v++ += ComplexScalar(0, *pi++); 00182 } 00183 return; 00184 } 00185 00186 Other *pr = reinterpret_cast<Other*>(&prCopy_[0]); 00187 Scalar *v = reinterpret_cast<Scalar*>(&(CCSval_[0])); //&matrix_.value_data()[0] 00188 for (uint i = 0; i < nnz_; ++i) 00189 *v++ = *pr++; //*v++ 00190 } 00191 00192 00193 00194 00195 00196 // Part 9: other public methods 00197 public: 00210 void get_matrix() { 00211 00212 if (matrixReady_){ 00213 return; 00214 } 00215 //Real typ 00216 MATtype *nowCopy = &realType_; 00217 while (true) { 00218 bool real; 00219 if (nowCopy == &realType_) 00220 real = true; 00221 else 00222 real = false; 00223 00224 switch(*nowCopy) { 00225 case miINT8: 00226 copy<miINT8_t>(real); 00227 break; 00228 case miUINT8: 00229 copy<miUINT8_t>(real); 00230 break; 00231 case miINT16: 00232 copy<miINT16_t>(real); 00233 break; 00234 case miUINT16: 00235 copy<miUINT16_t>(real); 00236 break; 00237 case miINT32: 00238 copy<miINT32_t>(real); 00239 break; 00240 case miUINT32: 00241 copy<miUINT32_t>(real); 00242 break; 00243 case miINT64: 00244 copy<miINT64_t>(real); 00245 break; 00246 case miUINT64: 00247 copy<miUINT64_t>(real); 00248 break; 00249 case miSINGLE: 00250 copy<miSINGLE_t>(real); 00251 break; 00252 case miDOUBLE: 00253 copy<miDOUBLE_t>(real); 00254 break; 00255 } 00256 00257 if (real && isComplex_) 00258 nowCopy = &imagType_; 00259 else 00260 break; 00261 } 00262 00263 return; 00264 } 00265 00266 }; 00267 00268 } 00269 00270 00271 #endif // SPARSEIWRAPPER_HH