![]() |
GURLS++
2.0.00
C++ Implementation of GURLS Matlab Toolbox
|
00001 00002 #ifdef _WIN32 00003 #pragma warning(push) 00004 #pragma warning(disable : 4244) 00005 #endif 00006 00007 #include <boost/archive/binary_iarchive.hpp> 00008 #include <boost/archive/binary_oarchive.hpp> 00009 #include <boost/archive/text_iarchive.hpp> 00010 #include <boost/archive/text_oarchive.hpp> 00011 00012 #ifdef _WIN32 00013 #pragma warning(pop) 00014 #endif 00015 00016 #include <boost/serialization/base_object.hpp> 00017 00018 00019 #ifdef USE_BINARY_ARCHIVES 00020 typedef boost::archive::binary_iarchive iarchive; 00021 typedef boost::archive::binary_oarchive oarchive; 00022 #else 00023 typedef boost::archive::text_iarchive iarchive; 00024 typedef boost::archive::text_oarchive oarchive; 00025 #endif 00026 00027 #include <boost/lexical_cast.hpp> 00028 #include <boost/tokenizer.hpp> 00029 00030 #include <fstream> 00031 #include <string> 00032 00033 namespace gurls { 00034 00035 template < typename T> 00036 const std::string gMat2D<T>::Less = "<"; 00037 template < typename T> 00038 const std::string gMat2D<T>::Greater = ">"; 00039 template < typename T> 00040 const std::string gMat2D<T>::LessEq = "<="; 00041 template < typename T> 00042 const std::string gMat2D<T>::GreaterEq = ">="; 00043 template < typename T> 00044 const std::string gMat2D<T>::Equal = "=="; 00045 00046 00047 // IMPLEMENTATION OF TEMPLATE FUNCTIONS 00048 00049 template <typename T> 00050 gMat2D<T>::gMat2D(unsigned long r, unsigned long c) : numcols(c), numrows(r) 00051 { 00052 this->alloc(r*c); 00053 } 00054 00055 // WARNING - HERE WE MAY BE USING A TROUBLESOME VERSION OF 'SET' 00056 template <typename T> 00057 gMat2D<T>::gMat2D(T* buf, unsigned long r, unsigned long c, bool owner) : numcols(c), numrows(r) 00058 { 00059 if(owner) 00060 { 00061 this->alloc(r*c); 00062 this->set(buf, r*c); 00063 } 00064 else 00065 { 00066 this->isowner = owner; 00067 this->data = buf; 00068 } 00069 } 00070 00071 template <typename T> 00072 gMat2D<T>::gMat2D(const gMat2D<T>& other) : numcols(other.numcols), numrows(other.numrows) 00073 { 00074 this->alloc(other.numrows*other.numcols); 00075 *this = other; 00076 } 00077 00078 // WARNING: TO BE DISCUSSED 00079 template <typename T> 00080 gMat2D<T>& gMat2D<T>::operator=(const gMat2D<T>& other) 00081 { 00082 if (this != &other) 00083 this->set(other.data, other.numrows*other.numcols); 00084 00085 return *this; 00086 } 00087 00088 template <typename T> 00089 void gMat2D<T>::submatrix(const gMat2D<T>& other, unsigned long r, unsigned long c) 00090 { 00091 const unsigned long cols = std::min(other.numcols, this->numcols-c); 00092 const unsigned long rows = std::min(other.numrows, this->numrows-r); 00093 00094 for(unsigned long i=0; i< cols; ++i) 00095 copy(this.data + r +((c+i)*this->numrows), other.data + (i*other.numrows), rows); 00096 } 00097 00098 template <typename T> 00099 void gMat2D<T>::transpose(gurls::gMat2D<T>& transposed) const 00100 { 00101 if(this->numrows != transposed.numcols || this->numcols != transposed.numrows) 00102 throw gException(gurls::Exception_Inconsistent_Size); 00103 00104 gurls::transpose(this->data, this->numrows, this->numcols, transposed.data); 00105 } 00106 00107 template <typename T> 00108 gMat2D<T> gMat2D<T>::zeros(unsigned long r, unsigned long c) 00109 { 00110 gMat2D<T> m(r, c); 00111 00112 gurls::set(m.data, (T)0.0, m.getSize()); 00113 00114 return m; 00115 } 00116 00117 template <typename T> 00118 gMat2D<T> gMat2D<T>::rand(unsigned long r, unsigned long c) 00119 { 00120 gMat2D<T> m(r, c); 00121 T* d = m.data; 00122 00123 for (unsigned long i = 0; i < r*c; ++i) 00124 *d++ = (T)std::rand()/RAND_MAX; 00125 00126 return m; 00127 } 00128 00129 template <typename T> 00130 gMat2D<T> gMat2D<T>::eye(unsigned long n) 00131 { 00132 gMat2D<T> m = gMat2D<T>::zeros(n,n); 00133 00134 gurls::set(m.data, (T)1.0, n, n+1); 00135 00136 return m; 00137 } 00138 00139 template <typename T> 00140 gMat2D<T> gMat2D<T>::diag(gVec<T>& diagonal) 00141 { 00142 unsigned long s = diagonal.getSize(); 00143 gMat2D<T> m = gMat2D<T>::zeros(s,s); 00144 00145 gurls::copy(m.data, diagonal.getData(), s, m.numrows+1, 1); 00146 00147 return m; 00148 } 00149 00150 00151 template <typename T> 00152 void gMat2D<T>::resize(unsigned long r, unsigned long c ) 00153 { 00154 BaseArray<T>::resize(r*c); 00155 this->numrows = r; 00156 this->numcols = c; 00157 } 00158 00159 template <typename T> 00160 void gMat2D<T>::reshape(unsigned long r, unsigned long c){ 00161 00162 if (r*c != this->size) 00163 throw gException(gurls::Exception_Invalid_Reshape_Arguments); 00164 00165 this->numcols = c; 00166 this->numrows = r; 00167 } 00168 00169 template <typename T> 00170 gMat2D<T> gMat2D<T>::operator+(T val) const { 00171 gMat2D<T> w(*this); 00172 w += val; 00173 return w; 00174 } 00175 00179 template <typename T> 00180 gMat2D<T> operator+(T val, const gMat2D<T>& v) { 00181 gMat2D<T> w(v); 00182 w += val; 00183 return w; 00184 } 00185 00189 template <typename T> 00190 gMat2D<T> operator-(T val, const gMat2D<T>& v) { 00191 gMat2D<T> w(-v); 00192 w += val; 00193 return w; 00194 } 00195 00196 // ------------ MULT SCALAR ------------------------------------------- 00197 00198 template <typename T> 00199 gMat2D<T> gMat2D<T>::operator*(T val) const { 00200 gMat2D<T> w(*this); 00201 w *= val; 00202 return w; 00203 } 00204 00208 template <typename T> 00209 gMat2D<T> operator*(T val, const gMat2D<T>& v) { 00210 gMat2D<T> w(v); 00211 w *= val; 00212 return w; 00213 } 00214 00218 template <typename T> 00219 gMat2D<T> operator/(T val, const gMat2D<T>& v) { 00220 gMat2D<T> w(v); 00221 w *= (static_cast<T>(1)/val); 00222 return w; 00223 } 00224 00225 // ----------------- SUM OF VECTORS -------------------------- 00226 00227 template <typename T> 00228 gMat2D<T>& gMat2D<T>::operator+=(const gMat2D<T>& v) { 00229 return static_cast<gMat2D<T>&>( BaseArray<T>::add(v) ); 00230 } 00231 00232 template <typename T> 00233 gMat2D<T> gMat2D<T>::operator+(const gMat2D<T>& v) const { 00234 gMat2D<T> w(v); 00235 w += *this; 00236 return w; 00237 } 00238 00239 00240 // ----------------- SUBTRACT OF VECTORS -------------------------- 00241 00242 template <typename T> 00243 gMat2D<T>& gMat2D<T>::operator-=(const gMat2D<T>& v) { 00244 return static_cast<gMat2D<T>&>( BaseArray<T>::subtract(v) ); 00245 } 00246 00247 template <typename T> 00248 gMat2D<T> gMat2D<T>::operator-(const gMat2D<T>& v) const { 00249 gMat2D<T> w(v); 00250 w -= *this; 00251 return w; 00252 } 00253 00254 // ------------------- MULT TWO VECTORS ------------------------------------ 00255 00256 template <typename T> 00257 gMat2D<T>& gMat2D<T>::operator*=(const gMat2D<T>& v) { 00258 return static_cast<gMat2D<T>&>( BaseArray<T>::multiply(v) ); 00259 } 00260 00261 template <typename T> 00262 gMat2D<T> gMat2D<T>::operator*(const gMat2D<T>& v) const { 00263 gMat2D<T> w(v); 00264 w *= *this; 00265 return w; 00266 } 00267 00268 00269 // ------------------- DIVIDE TWO VECTORS ------------------------------------ 00270 00271 template <typename T> 00272 gMat2D<T>& gMat2D<T>::operator/=(const gMat2D<T>& v) { 00273 return static_cast<gMat2D<T>&>( BaseArray<T>::divide(v) ); 00274 } 00275 00276 template <typename T> 00277 gMat2D<T> gMat2D<T>::operator/(const gMat2D<T>& v) const { 00278 gMat2D<T> w(v); 00279 w /= *this; 00280 return w; 00281 } 00282 00283 00284 template <typename T> 00285 gMat2D<bool>& gMat2D<T>::operator ==(T threshold) const { 00286 gMat2D<bool> *w(this->rows(), this->cols()); 00287 const T* ptr = this->data; 00288 bool* bptr = w->getData(); 00289 00290 for(unsigned long int i=0;i<this->size; ++i, ++bptr, ++ptr){ 00291 *bptr = (*ptr == threshold); 00292 } 00293 return *w; 00294 00295 } 00296 00297 00298 template <typename T> 00299 gMat2D<bool>& gMat2D<T>::operator <(T threshold) const { 00300 gMat2D<bool> *w = new gMat2D<bool>(this->rows(), this->cols()); 00301 const T* ptr = this->data; 00302 bool* bptr = w->getData(); 00303 00304 for(unsigned long int i=0;i<this->size;++i, ++bptr, ++ptr){ 00305 *bptr = (*ptr < threshold); 00306 } 00307 return *w; 00308 } 00309 00310 template <typename T> 00311 gMat2D<bool>& gMat2D<T>::operator >(T threshold) const { 00312 gMat2D<bool> *w = new gMat2D<bool>(this->rows(), this->cols()); 00313 const T* ptr = this->data; 00314 bool* bptr = w->getData(); 00315 00316 for(unsigned long int i=0;i<this->size;++i, ++bptr, ++ptr){ 00317 *bptr = (*ptr > threshold); 00318 } 00319 return *w; 00320 } 00321 00322 template <typename T> 00323 gMat2D<bool>& gMat2D<T>::operator <=(T threshold) const { 00324 gMat2D<bool> *w = new gMat2D<bool>(this->rows(), this->cols()); 00325 const T* ptr = this->data; 00326 bool* bptr = w->getData(); 00327 00328 for(unsigned long int i=0;i<this->size;++i, ++bptr, ++ptr){ 00329 *bptr = (*ptr <= threshold); 00330 } 00331 return *w; 00332 } 00333 00334 template <typename T> 00335 gMat2D<bool>& gMat2D<T>::operator >=(T threshold) const { 00336 gMat2D<bool> *w = new gMat2D<bool>(this->rows(), this->cols()); 00337 const T* ptr = this->data; 00338 bool* bptr = w->getData(); 00339 00340 for(unsigned long int i=0;i<this->size;++i, ++bptr, ++ptr){ 00341 *bptr = (*ptr >= threshold); 00342 } 00343 return *w; 00344 } 00345 00346 00347 template <typename T> 00348 gMat2D<bool>& gMat2D<T>::compare(T& threshold, std::string logical_operator) const { 00349 gMat2D<bool>* w = new gMat2D<bool>(this->rows(), this->cols()); 00350 *w = 0; 00351 const T* ptr = this->data; 00352 bool* bptr = w->getData(); 00353 00354 if ( !logical_operator.compare(GreaterEq)){ 00355 for(unsigned long int i=0;i<this->size;++i, ++bptr, ++ptr){ 00356 *bptr = *ptr >= threshold; 00357 } 00358 } else if (!logical_operator.compare(Greater)){ 00359 for(unsigned long int i=0;i<this->size;++i, ++bptr, ++ptr){ 00360 *bptr = *ptr > threshold; 00361 } 00362 } else if (!logical_operator.compare(LessEq)){ 00363 for(unsigned long int i=0;i<this->size;++i, ++bptr, ++ptr){ 00364 *bptr = *ptr <= threshold; 00365 } 00366 } else if (!logical_operator.compare(Less)){ 00367 for(unsigned long int i=0;i<this->size;++i, ++bptr, ++ptr){ 00368 *bptr = *ptr < threshold; 00369 } 00370 } else if (!logical_operator.compare(Equal)){ 00371 for(unsigned long int i=0;i<this->size;++i, ++bptr, ++ptr){ 00372 *bptr = *ptr == threshold; 00373 } 00374 }else { 00375 throw gException(gurls::Exception_Logical_Operator); 00376 } 00377 return *w; 00378 } 00379 00380 00381 template <typename T> 00382 gVec<T>& gMat2D<T>::where(const gMat2D<bool>& comparison) const { 00383 00384 // WARNING: add a check on the size of the input boolean matrix 00385 00386 unsigned long n0 = static_cast<unsigned long>(comparison.sum()), n1 = 0; 00387 //T* buf = new T[this->size]; 00388 T* buf = new T[n0]; 00389 const T* ptr = this->data; 00390 const bool* bptr = comparison.getData(); 00391 for(unsigned long int i=0;i<this->size;++i, ++ptr, ++bptr){ 00392 if (*bptr){ 00393 //*(buf+i) = *ptr; 00394 *(buf+n1) = *ptr; 00395 n1++; 00396 } 00397 } 00398 gVec<T> *v = new gVec<T>(buf, n1, true); 00399 return *v; 00400 } 00401 00402 00403 00404 // ------ TEST IF ELEMENTS ARE ALL EQUAL TO A CONSTANT VALUE ------- 00405 00406 template <typename T> 00407 bool gMat2D<T>::allEqualsTo(const T& val) const { 00408 const T *ptr = this->data; 00409 const unsigned long size = this->numrows* this->numcols; 00410 for (unsigned long i = 0; i < size; ++i, ++ptr){ 00411 if (*ptr != val) 00412 return false; 00413 } 00414 return true; 00415 } 00416 00417 // ----------------------- PRINTOUT -------------------------------- 00418 00422 template <typename T> 00423 std::ostream& operator<<(std::ostream& os, const gMat2D<T>& v) 00424 { 00425 // if (v.rows() >= (unsigned long)gurls::MAX_PRINTABLE_SIZE || v.cols() >= (unsigned long) gurls::MAX_PRINTABLE_SIZE) 00426 // { 00427 // os << v.what() << std::endl; 00428 // return os; 00429 // } 00430 00431 os << "["; 00432 for (unsigned long i = 0; i < v.numrows; ++i) 00433 { 00434 for (unsigned long j = 0; j < v.numcols; ++j) 00435 os << " " << v.data[i+v.numrows*j]; 00436 00437 if( i != (v.numrows-1) ) 00438 os << std::endl << " "; 00439 } 00440 00441 os << " ]"; 00442 os << std::endl; 00443 return os; 00444 } 00445 00446 00447 template <typename T> 00448 void gMat2D<T>::uppertriangular(gMat2D<T>& up) const 00449 { 00450 if(up.numrows != this->numrows || up.numcols != this->numcols) 00451 throw gException(Exception_Inconsistent_Size); 00452 00453 T* upptr; 00454 const T* ptr; 00455 00456 for (unsigned long int i = 0; i < up.numcols; ++i) 00457 { 00458 upptr = up.data + (up.numrows*(up.numcols-i-1)); 00459 ptr = this->data + this->numrows*(this->numcols-i-1); 00460 00461 gurls::set(upptr, (T)0.0, i); 00462 00463 gurls::copy(upptr+i, ptr+i, up.numrows-i); 00464 } 00465 } 00466 00467 template <typename T> 00468 void gMat2D<T>::lowertriangular(gMat2D<T>& lo) const 00469 { 00470 if(lo.numrows != this->numrows || lo.numcols != this->numcols) 00471 throw gException(Exception_Inconsistent_Size); 00472 00473 T* loptr; 00474 const T* ptr; 00475 00476 for (unsigned long int i = 0; i < lo.numcols; ++i) 00477 { 00478 loptr = lo.data + (lo.numrows*(lo.numcols-i-1)); 00479 ptr = this->data + this->numrows*(this->numcols-i-1); 00480 00481 gurls::copy(loptr, ptr, lo.numrows-i); 00482 00483 gurls::set(loptr+lo.numrows-i, (T)0.0, i); 00484 } 00485 } 00486 00487 00488 template <typename T> 00489 gMat2D<T>& gMat2D<T>::reciprocal() const { 00490 gMat2D<T> *w = new gMat2D(*this); 00491 w->setReciprocal(); 00492 return *w; 00493 } 00494 00495 template <typename T> 00496 gVec<T>* gMat2D<T>::sum(int order) const 00497 { 00498 gVec<T> *v; 00499 unsigned long n; 00500 00501 switch(order) 00502 { 00503 case gurls::COLUMNWISE: 00504 00505 n = this->numcols; 00506 v = new gVec<T>(n); 00507 00508 for (unsigned long i = 0; i < n; ++i) 00509 v->at(i) = (*this)(i).sum(); 00510 00511 break; 00512 case gurls::ROWWISE: 00513 00514 n = this->numrows; 00515 v = new gVec<T>(n); 00516 00517 for (unsigned long i = 0; i < n; ++i) 00518 v->at(i) = (*this)[i].sum(); 00519 00520 break; 00521 default: 00522 throw gException(gurls::Exception_Illegal_Argument_Value+" -> order must be either gurls::COLUMNWISE or gurls::ROWWISE."); 00523 } 00524 00525 return v; 00526 } 00527 00528 template <typename T> 00529 gVec<T>* gMat2D<T>::max(int order) 00530 { 00531 gVec<T> *v; 00532 unsigned long n; 00533 00534 switch(order) 00535 { 00536 case gurls::COLUMNWISE: 00537 00538 n = this->numcols; 00539 v = new gVec<T>(n); 00540 00541 for (unsigned long i = 0; i < n; ++i) 00542 v->at(i) = (*this)(i).max(); 00543 00544 break; 00545 case gurls::ROWWISE: 00546 00547 n = this->numrows; 00548 v = new gVec<T>(n); 00549 00550 for (unsigned long i = 0; i < n; ++i) 00551 v->at(i) = (*this)[i].max(); 00552 00553 break; 00554 default: 00555 throw gException(gurls::Exception_Illegal_Argument_Value+" -> order must be either gurls::COLUMNWISE or gurls::ROWWISE."); 00556 } 00557 00558 return v; 00559 } 00560 00561 template <typename T> 00562 gVec<T>* gMat2D<T>::min(int order) 00563 { 00564 gVec<T> *v; 00565 unsigned long n; 00566 00567 switch(order) 00568 { 00569 case gurls::COLUMNWISE: 00570 00571 n = this->numcols; 00572 v = new gVec<T>(n); 00573 00574 for (unsigned long i = 0; i < n; ++i) 00575 v->at(i) = (*this)(i).min(); 00576 00577 break; 00578 case gurls::ROWWISE: 00579 00580 n = this->numrows; 00581 v = new gVec<T>(n); 00582 00583 for (unsigned long i = 0; i < n; ++i) 00584 v->at(i) = (*this)[i].min(); 00585 00586 break; 00587 default: 00588 throw gException(gurls::Exception_Illegal_Argument_Value+" -> order must be either gurls::COLUMNWISE or gurls::ROWWISE."); 00589 } 00590 00591 return v; 00592 } 00593 00594 template <typename T> 00595 gVec<T>* gMat2D<T>::argmax(int order) 00596 { 00597 gVec<T> *v; 00598 unsigned long n; 00599 00600 switch(order) 00601 { 00602 case gurls::COLUMNWISE: 00603 00604 n = this->numcols; 00605 v = new gVec<T>(n); 00606 00607 for (unsigned long i = 0; i < n; ++i) 00608 v->at(i) = (*this)(i).argmax(); 00609 00610 break; 00611 case gurls::ROWWISE: 00612 00613 n = this->numrows; 00614 v = new gVec<T>(n); 00615 00616 for (unsigned long i = 0; i < n; ++i) 00617 v->at(i) = (*this)[i].argmax(); 00618 00619 break; 00620 default: 00621 throw gException(gurls::Exception_Illegal_Argument_Value+" -> order must be either gurls::COLUMNWISE or gurls::ROWWISE."); 00622 } 00623 00624 return v; 00625 } 00626 00627 template <typename T> 00628 gVec<T>* gMat2D<T>::argmin(int order) 00629 { 00630 gVec<T> *v; 00631 unsigned long n; 00632 00633 switch(order) 00634 { 00635 case gurls::COLUMNWISE: 00636 00637 n = this->numcols; 00638 v = new gVec<T>(n); 00639 00640 for (unsigned long i = 0; i < n; ++i) 00641 v->at(i) = (*this)(i).argmin(); 00642 00643 break; 00644 case gurls::ROWWISE: 00645 00646 n = this->numrows; 00647 v = new gVec<T>(n); 00648 00649 for (unsigned long i = 0; i < n; ++i) 00650 v->at(i) = (*this)[i].argmin(); 00651 00652 break; 00653 default: 00654 throw gException(gurls::Exception_Illegal_Argument_Value+" -> order must be either gurls::COLUMNWISE or gurls::ROWWISE."); 00655 } 00656 00657 return v; 00658 } 00659 00660 template <typename T> 00661 template<class Archive> 00662 void gMat2D<T>::save(Archive & ar, const unsigned int /* file_version */) const 00663 { 00664 ar & this->numrows & this->numcols & this->isowner; 00665 00666 T* ptr = this->data; 00667 T* ptr_end = this->data+(this->numrows*this->numcols); 00668 00669 while (ptr!=ptr_end) 00670 ar & *ptr++; 00671 } 00672 00673 template <typename T> 00674 template<class Archive> 00675 void gMat2D<T>::load(Archive & ar, const unsigned int /* file_version */) 00676 { 00677 ar & this->numrows; 00678 ar & this->numcols; 00679 ar & this->isowner; 00680 00681 this->size = this->numrows*this->numcols; 00682 this->data = new T[this->size]; 00683 T* ptr = this->data; 00684 T* ptr_end = this->data+this->size; 00685 00686 while (ptr!=ptr_end) 00687 ar & *ptr++; 00688 } 00689 00690 template <typename T> 00691 void gMat2D<T>::load(const std::string& fileName) 00692 { 00693 #ifndef USE_BINARY_ARCHIVES 00694 std::ifstream instream(fileName.c_str()); 00695 #else 00696 std::ifstream instream(fileName.c_str(), std::ios_base::binary); 00697 #endif 00698 00699 if(!instream.is_open()) 00700 throw gException("Could not open file " + fileName); 00701 00702 try 00703 { 00704 iarchive inar(instream); 00705 inar >> *this; 00706 } 00707 catch(boost::archive::archive_exception&) 00708 { 00709 instream.close(); 00710 throw gException("Invalid file format for " + fileName); 00711 } 00712 00713 instream.close(); 00714 } 00715 00716 template <typename T> 00717 void gMat2D<T>::save(const std::string& fileName) const 00718 { 00719 #ifndef USE_BINARY_ARCHIVES 00720 std::ofstream outstream(fileName.c_str()); 00721 #else 00722 std::ofstream outstream(fileName.c_str(), std::ios_base::binary); 00723 #endif 00724 00725 if(!outstream.is_open()) 00726 throw gException("Could not open file " + fileName); 00727 00728 oarchive outar(outstream); 00729 outar << *this; 00730 00731 outstream.close(); 00732 } 00733 00734 template <typename T> 00735 void gMat2D<T>::readCSV(const std::string& fileName, bool colMajor) 00736 { 00737 std::vector<std::vector< T > > matrix; 00738 std::ifstream in(fileName.c_str()); 00739 00740 if(!in.is_open()) 00741 throw gurls::gException("Cannot open file " + fileName); 00742 00743 unsigned long rows = 0; 00744 unsigned long cols = 0; 00745 00746 std::string line; 00747 typedef boost::tokenizer<boost::char_separator<char> > tokenizer; 00748 boost::char_separator<char> sep(" ;|,"); 00749 00750 while (std::getline(in, line)) 00751 { 00752 if(!line.empty()) 00753 { 00754 std::vector<T> tf; 00755 00756 tokenizer tokens(line, sep); 00757 for (tokenizer::iterator it = tokens.begin(); it != tokens.end(); ++it) 00758 tf.push_back(boost::lexical_cast<T>(*it)); 00759 00760 matrix.push_back(tf); 00761 ++rows; 00762 } 00763 } 00764 00765 in.close(); 00766 00767 if(!matrix.empty()) 00768 cols = matrix[0].size(); 00769 00770 this->resize(rows, cols); 00771 00772 if(colMajor) 00773 { 00774 for(unsigned long i=0; i<rows; ++i) 00775 gurls::copy(this->data + i, &(*(matrix[i].begin())), cols, rows, 1); 00776 } 00777 else 00778 { 00779 for(unsigned long i=0; i<rows; ++i) 00780 gurls::copy(this->data + i*cols, &(*(matrix[i].begin())), rows); 00781 } 00782 } 00783 00784 template <typename T> 00785 void gMat2D<T>::saveCSV(const std::string& fileName) const 00786 { 00787 std::ofstream out(fileName.c_str()); 00788 00789 if(!out.is_open()) 00790 throw gurls::gException("Cannot open file " + fileName); 00791 00792 for (unsigned long i = 0; i < numrows; ++i) 00793 { 00794 for (unsigned long j = 0; j < numcols; ++j) 00795 out << " " << this->data[i+numrows*j]; 00796 00797 out << std::endl; 00798 } 00799 00800 out.close(); 00801 } 00802 00803 }