Jafar
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
matlab.hpp
00001 
00014 #ifndef MATLAB_HPP_
00015 #define MATLAB_HPP_
00016 
00017 #include <iostream>
00018 #include <boost/numeric/ublas/blas.hpp>
00019 #include <boost/numeric/ublas/symmetric.hpp>
00020 #include "jmath/indirectArray.hpp"
00021 
00022 namespace ublas = boost::numeric::ublas;
00023 
00024 namespace jafar {
00025   namespace jmath {
00026 
00027 
00029     //   CLASS MATLAB
00031 
00124     class MATLAB {
00125 
00126       public:
00127         static bool fullPrec;
00128         static const size_t prec;
00129         std::string str;
00130         friend std::ostream & operator <<(std::ostream & os, const MATLAB & m) {
00131           return os << m.str;
00132         }
00133 
00134         template<typename bubTemplateVector>
00135         void initFromBubVector(const bubTemplateVector& v1) {
00136           std::ostringstream os;
00137           os.precision(prec);
00138           //          os.setf(std::ios::fixed,std::ios::floatfield);
00139           os.setf(std::ios::fixed,std::ios::adjustfield);
00140           os << "[ ";
00141           for (size_t i = 0; i < v1.size(); ++i) {
00142             {
00143               os << " ";
00144               double a = v1(i);
00145               os << a;
00146             }
00147             if (v1.size() != i + 1) {
00148               os << ", ";
00149             }
00150           }
00151           os << " ]';";
00152           str = os.str();
00153         }
00154 
00155         template<typename bubTemplateMatrix>
00156         void initFromBubMatrix(const bubTemplateMatrix& m1) {
00157           fullPrec = false;
00158           std::ostringstream os;
00159           os << "...\n[ ";
00160           std::ostringstream ostmp;
00161           ostmp.precision(prec);
00162 //          ostmp.setf(std::ios::fixed,std::ios::floatfield);
00163           ostmp.setf(std::ios::fixed,std::ios::adjustfield);
00164           for (size_t i = 0; i < m1.size1(); ++i) {
00165             for (size_t j = 0; j < m1.size2(); ++j) {
00166               if (m1(i, j) < 0)
00167                 ostmp << "-";
00168               else
00169                 ostmp << " ";
00170               if (fullPrec || fabs(m1(i, j)) > 1e-6)
00171                 ostmp << fabs(m1(i, j));
00172               else {
00173                 ostmp << "0";
00174               }
00175               if (m1.size2() != j + 1) {
00176                 ostmp << ",";
00177                 const int size = ostmp.str().length();
00178                 for (size_t i = size; i < prec+5; ++i)
00179                   ostmp << " ";
00180               }
00181               os << ostmp.str();
00182               ostmp.str("");
00183             }
00184             if (m1.size1() != i + 1) {
00185               os << " ;" << std::endl << "  ";
00186             }
00187             else {
00188               os << " ];";
00189             }
00190           }
00191           str = os.str();
00192         }
00193 
00194         template<typename bubTemplateIndex>
00195         void initFromBubIndex(const bubTemplateIndex & i1) {
00196           std::ostringstream os;
00197           os << "[ ";
00198           for (size_t i = 0; i < i1.size(); ++i) {
00199             os << (i1(i) + 1);
00200             if (i1.size() != i + 1) { // Matlab indexes from 1 to size()
00201               os << ", ";
00202             }
00203           }
00204           os << " ];";
00205           str = os.str();
00206         }
00207 
00208 
00209         // Specialized templates for vectors
00210 
00211         template<typename T>
00212         MATLAB(const ublas::vector<T>& v1) {
00213           initFromBubVector(v1);
00214         }
00215 
00216         template<typename T, std::size_t N>
00217         MATLAB(const ublas::bounded_vector<T, N>& v1) {
00218           initFromBubVector(v1);
00219         }
00220 
00221 
00222         // Specialized templates for indirect arrays, ranges and slices
00223 
00224         MATLAB(const ublas::indirect_array<>& i1) {
00225           initFromBubIndex(i1);
00226         }
00227 
00228         MATLAB(const ublas::range& r1) {
00229           initFromBubIndex(jafar::jmath::ublasExtra::ia_set(r1));
00230         }
00231 
00232         MATLAB(const ublas::slice& s1) {
00233           initFromBubIndex(jafar::jmath::ublasExtra::ia_set(s1));
00234         }
00235 
00236 
00237         // Specialized templates for vector proxies
00238 
00239         template<typename T>
00240         MATLAB(const ublas::vector_indirect<T>& v1) {
00241           initFromBubVector(v1);
00242         }
00243 
00244         template<typename T>
00245         MATLAB(const ublas::vector_range<T>& v1) {
00246           initFromBubVector(v1);
00247         }
00248 
00249         template<typename T>
00250         MATLAB(const ublas::vector_slice<T>& v1) {
00251           initFromBubVector(v1);
00252         }
00253 
00254 
00255         // Non-specialized templates will be interpreted as matrices.
00256         // These include all kinds of 2-dimensional arrays:
00257 
00258         template<class M>
00259         MATLAB(const M& m1) {
00260           initFromBubMatrix(m1);
00261         }
00262 
00263     };
00264 
00265   }
00266 }
00267 
00268 #endif /* MATLAB_HPP_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Wed Oct 15 2014 00:37:21 for Jafar by doxygen 1.7.6.1
LAAS-CNRS