00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 #ifndef KMATRIX_IMPL_HPP
00028 #define KMATRIX_IMPL_HPP
00029 
00032 
00033 #include KSSTREAM_HEADER
00034 
00035 namespace Kalman {
00036 
00038 
00045   class KMatrixContextImpl {
00046   public:
00047 
00049 
00057     KMatrixContextImpl(std::string elemDelim = " ",
00058                        std::string rowDelim = "\n",
00059                        std::string startDelim = std::string(),
00060                        std::string endDelim = std::string(),
00061                        unsigned prec = 4) 
00062       : elemDelim_(elemDelim), rowDelim_(rowDelim), startDelim_(startDelim), 
00063         endDelim_(endDelim), precision_(prec), width_(8+prec) {
00064       
00065       std::string ws(" \t\n");
00066       skipElemDelim_  = 
00067         ( elemDelim_.find_first_not_of(ws) != std::string::npos);
00068       skipRowDelim_   = 
00069         (  rowDelim_.find_first_not_of(ws) != std::string::npos);
00070       skipStartDelim_ = 
00071         (startDelim_.find_first_not_of(ws) != std::string::npos);
00072       skipEndDelim_   = 
00073         (  endDelim_.find_first_not_of(ws) != std::string::npos);
00074     }
00075 
00076     std::string elemDelim_;  
00077     std::string rowDelim_;   
00078     std::string startDelim_; 
00079     std::string endDelim_;   
00080     unsigned precision_;     
00081     unsigned width_;         
00082     bool skipElemDelim_;     
00083     bool skipRowDelim_;      
00084     bool skipStartDelim_;    
00085     bool skipEndDelim_;      
00086   };
00087 
00089 
00092   extern KMatrixContextImpl* currentMatrixContext;
00093 
00103   template<typename T, K_UINT_32 BEG, bool DBG>
00104   inline void KMatrix<T, BEG, DBG>::init(K_UINT_32 m, K_UINT_32 n) {
00105 
00106     if (m != 0 && n != 0) {
00107 
00108       
00109       vimpl_.resize(m);
00110       T* ptr = &Mimpl_[0] - BEG;
00111       T** M = &vimpl_[0];
00112       T** end = M + m;
00113       
00114       while (M != end) {
00115         *M++ = ptr;
00116         ptr += n;
00117       }
00118 
00119       M_ = &vimpl_[0] - BEG;
00120       m_ = m;
00121       n_ = n;
00122 
00123     } else {
00124 
00125       
00126       M_ = 0;
00127       m_ = 0;
00128       n_ = 0;
00129 
00130     }
00131 
00132   }
00133 
00134   template<typename T, K_UINT_32 BEG, bool DBG>
00135   inline KMatrix<T, BEG, DBG>::KMatrix() {
00136     init(0, 0);
00137   }
00138 
00143   template<typename T, K_UINT_32 BEG, bool DBG>
00144   inline KMatrix<T, BEG, DBG>::KMatrix(K_UINT_32 m, K_UINT_32 n)
00145     : Mimpl_(m*n) { 
00146     init(m, n);
00147   }
00148 
00154   template<typename T, K_UINT_32 BEG, bool DBG>
00155   inline KMatrix<T, BEG, DBG>::KMatrix(K_UINT_32 m, K_UINT_32 n, const T& a)
00156     : Mimpl_(m*n, a) {
00157     init(m, n);
00158   }
00159 
00171   template<typename T, K_UINT_32 BEG, bool DBG>
00172   inline KMatrix<T, BEG, DBG>::KMatrix(K_UINT_32 m, K_UINT_32 n, const T* v)
00173     : Mimpl_(v, v + m*n) {
00174     init(m, n);
00175   }
00176 
00179   template<typename T, K_UINT_32 BEG, bool DBG>
00180   inline KMatrix<T, BEG, DBG>::KMatrix(const KMatrix& M) 
00181     : Mimpl_(M.Mimpl_) {
00182     init(M.m_, M.n_);
00183   }
00184 
00185   template<typename T, K_UINT_32 BEG, bool DBG>
00186   inline KMatrix<T, BEG, DBG>::~KMatrix() {}
00187 
00193   template<typename T, K_UINT_32 BEG, bool DBG>
00194   inline T& KMatrix<T, BEG, DBG>::operator()(K_UINT_32 i, 
00195                                              K_UINT_32 j) {
00196     if (DBG) {
00197                 
00198                 if(BEG!=0)
00199                 {
00200 
00201 
00202 
00203 
00204 
00205 
00206 
00207 
00208                 }else{
00209                         if (i >= m_ + BEG || j >= n_ + BEG)
00210                         {
00211                                 KOSTRINGSTREAM oss;
00212                                 oss << "Trying to access element (" << i << ", " << j 
00213                                         << ") not included in [" << BEG << ", " << m_ + BEG - 1 << "][" 
00214                                         << BEG << ", " << n_ + BEG - 1 << "]." KEND_OF_STREAM;
00215                                 throw OutOfBoundError(oss.str());
00216                         }
00217                 }
00218     }
00219     return M_[i][j];
00220   }
00221 
00227   template<typename T, K_UINT_32 BEG, bool DBG>
00228   inline const T& KMatrix<T, BEG, DBG>::operator()(K_UINT_32 i, 
00229                                                    K_UINT_32 j) const {
00230     if (DBG) {
00231                 if(BEG!=0)
00232                 {
00233 
00234 
00235 
00236 
00237 
00238 
00239 
00240                 }else
00241                 {
00242                         if (i >= m_ + BEG || j >= n_ + BEG) {
00243                                 KOSTRINGSTREAM oss;
00244                                 oss << "Trying to access element (" << i << ", " << j 
00245                                         << ") not included in [" << BEG << ", " << m_ + BEG - 1 << "][" 
00246                                         << BEG << ", " << n_ + BEG - 1 << "]." KEND_OF_STREAM;
00247                                 throw OutOfBoundError(oss.str());
00248                         }       
00249                 }
00250     }
00251     return M_[i][j];
00252   }
00253 
00256   template<typename T, K_UINT_32 BEG, bool DBG>
00257   inline K_UINT_32 KMatrix<T, BEG, DBG>::nrow() const {
00258     return m_;
00259   }
00260 
00263   template<typename T, K_UINT_32 BEG, bool DBG>
00264   inline K_UINT_32 KMatrix<T, BEG, DBG>::ncol() const {
00265     return n_;
00266   }
00267 
00276   template<typename T, K_UINT_32 BEG, bool DBG>
00277   inline void KMatrix<T, BEG, DBG>::resize(K_UINT_32 m, K_UINT_32 n) {
00278 
00279     if (m == m_ && n == n_) {
00280       return;
00281     }
00282 
00283     Mimpl_.resize(m*n);
00284     init(m, n);
00285   }
00286 
00289   template<typename T, K_UINT_32 BEG, bool DBG>
00290   inline KMatrix<T, BEG, DBG>& KMatrix<T, BEG, DBG>::operator=(const T& a) {
00291     
00292     T* ptr = &Mimpl_[0];
00293     const T* end = ptr + Mimpl_.size();
00294 
00295     while (ptr != end) {
00296       *ptr++ = a;
00297     }
00298     return *this;
00299   }
00300 
00304   template<typename T, K_UINT_32 BEG, bool DBG>
00305   inline KMatrix<T, BEG, DBG>& 
00306   KMatrix<T, BEG, DBG>::operator=(const KMatrix& M) {
00307     KMatrix temp(M);
00308     swap(temp);    
00309     return *this;
00310   }
00311 
00319   template<typename T, K_UINT_32 BEG, bool DBG>
00320   inline void KMatrix<T, BEG, DBG>::assign(K_UINT_32 m, K_UINT_32 n, 
00321                                            const T* v) {
00322     KMatrix temp(m, n, v);
00323     swap(temp);
00324   }
00325 
00331   template<typename T, K_UINT_32 BEG, bool DBG>
00332   inline void KMatrix<T, BEG, DBG>::swap(KMatrix& M) {
00333     vimpl_.swap(M.vimpl_);
00334     Mimpl_.swap(M.Mimpl_);
00335     Util::swap(M_, M.M_);
00336     Util::swap(m_, M.m_);
00337     Util::swap(n_, M.n_);
00338   }
00339 
00346   template<typename T, K_UINT_32 BEG, bool DBG>
00347   inline void KMatrix<T, BEG, DBG>::get(std::istream& is) {
00348 
00349     T* ptr = &Mimpl_[0];
00350     std::string tmp;
00351     K_UINT_32 i, j;
00352 
00353     if (currentMatrixContext->skipStartDelim_) {
00354       is >> tmp;
00355     }
00356 
00357     if (currentMatrixContext->skipRowDelim_) {
00358 
00359       if (currentMatrixContext->skipElemDelim_) {
00360 
00361         for (i = 0; i < m_-1; ++i) {
00362           for (j = 0; j < n_; ++j) {
00363             is >> *ptr++ >> tmp;
00364           }
00365         }
00366 
00367         for (j = 0; j < n_-1; ++j) {
00368           is >> *ptr++ >> tmp;
00369         }
00370 
00371         is >> *ptr;
00372 
00373       } else {
00374 
00375         for (i = 0; i < m_-1; ++i) {
00376           for (j = 0; j < n_; ++j) {
00377             is >> *ptr++;
00378           }
00379           is >> tmp;
00380         }
00381 
00382         for (j = 0; j < n_; ++j) {
00383           is >> *ptr++;
00384         }
00385 
00386       }
00387 
00388     } else {
00389 
00390       if (currentMatrixContext->skipElemDelim_) {
00391 
00392         for (i = 0; i < m_; ++i) {
00393           for (j = 0; j < n_-1; ++j) {
00394             is >> *ptr++ >> tmp;
00395           }
00396           is >> *ptr++;
00397         }
00398 
00399       } else {
00400 
00401         for (i = 0; i < m_; ++i) {
00402           for (j = 0; j < n_; ++j) {
00403             is >> *ptr++;
00404           }
00405         }
00406 
00407       }
00408 
00409     }
00410 
00411     if (currentMatrixContext->skipEndDelim_) {
00412       is >> tmp;
00413     }
00414   }
00415 
00422   template<typename T, K_UINT_32 BEG, bool DBG>
00423   inline void KMatrix<T, BEG, DBG>::put(std::ostream& os) const {
00424     if (m_ == 0 || n_ == 0) {
00425       return;
00426     }
00427 
00428     const T* ptr = &Mimpl_[0];
00429     K_UINT_32 i, j;
00430 
00431     std::ios::fmtflags f = os.setf(std::ios::scientific, std::ios::floatfield);
00432     os.setf(std::ios::showpoint);
00433     std::streamsize p = os.precision(currentMatrixContext->precision_);
00434 
00435     os << currentMatrixContext->startDelim_;
00436 
00437     for (i = 0; i < m_-1; ++i) {
00438       for (j = 0; j < n_-1; ++j) {
00439         os.width(currentMatrixContext->width_);
00440         os << *ptr++ << currentMatrixContext->elemDelim_;
00441       }
00442       os.width(currentMatrixContext->width_);
00443       os << *ptr++ << currentMatrixContext->rowDelim_;
00444     }
00445 
00446     for (j = 0; j < n_-1; ++j) {
00447       os.width(currentMatrixContext->width_);
00448       os << *ptr++ << currentMatrixContext->elemDelim_;
00449     }
00450 
00451     os.width(currentMatrixContext->width_);
00452     os << *ptr++ << currentMatrixContext->endDelim_;
00453 
00454     os.precision(p);
00455     os.flags(f);
00456   }
00457 
00462   template<typename T, K_UINT_32 BEG, bool DBG>
00463   inline std::istream& operator>>(std::istream& is, 
00464                                   KMatrix<T, BEG, DBG>& M) {
00465     M.get(is);
00466     return is;
00467   }
00468 
00473   template<typename T, K_UINT_32 BEG, bool DBG>
00474   inline std::ostream& operator<<(std::ostream& os, 
00475                                   const KMatrix<T, BEG, DBG>& M) {
00476     M.put(os);
00477     return os;
00478   }
00479 
00480 }
00481 
00482 #endif