IT++ Logo

converters.h

Go to the documentation of this file.
00001 
00030 #ifndef CONVERTERS_H
00031 #define CONVERTERS_H
00032 
00033 #ifndef _MSC_VER
00034 #  include <itpp/config.h>
00035 #else
00036 #  include <itpp/config_msvc.h>
00037 #endif
00038 
00039 #include <itpp/base/help_functions.h>
00040 #include <itpp/base/math/misc.h>
00041 
00042 
00043 #ifndef HAVE_RINT
00045 double rint(double x);
00046 #endif
00047 
00048 namespace itpp {
00049 
00051 
00052 
00053   // ----------------------------------------------------------------------
00054   // Converters for vectors
00055   // ----------------------------------------------------------------------
00056 
00061   template <class T>
00062   bvec to_bvec(const Vec<T> &v)
00063   {
00064     bvec temp(v.length());
00065     for (int i = 0; i < v.length(); ++i) {
00066       temp(i) = static_cast<bin>(v(i));
00067     }
00068     return temp;
00069   }
00070 
00075   template <class T>
00076   svec to_svec(const Vec<T> &v)
00077   {
00078     svec temp(v.length());
00079     for (int i = 0; i < v.length(); ++i) {
00080       temp(i) = static_cast<short>(v(i));
00081     }
00082     return temp;
00083   }
00084 
00089   template <class T>
00090   ivec to_ivec(const Vec<T> &v)
00091   {
00092     ivec temp(v.length());
00093     for (int i = 0; i < v.length(); ++i) {
00094       temp(i) = static_cast<int>(v(i));
00095     }
00096     return temp;
00097   }
00098 
00103   template <class T>
00104   vec to_vec(const Vec<T> &v)
00105   {
00106     vec temp(v.length());
00107     for (int i = 0; i < v.length(); ++i) {
00108       temp(i) = static_cast<double>(v(i));
00109     }
00110     return temp;
00111   }
00112 
00117   template <class T>
00118   cvec to_cvec(const Vec<T> &v)
00119   {
00120     cvec temp(v.length());
00121     for (int i = 0; i < v.length(); ++i) {
00122       temp(i) = std::complex<double>(static_cast<double>(v(i)), 0.0);
00123     }
00124     return temp;
00125   }
00126 
00128   template<> inline
00129   cvec to_cvec(const cvec& v)
00130   {
00131     return v;
00132   }
00134 
00139   template <class T>
00140   cvec to_cvec(const Vec<T> &real, const Vec<T> &imag)
00141   {
00142     it_assert(real.length() == imag.length(),
00143               "to_cvec(): real and imaginary parts must have the same length");
00144     cvec temp(real.length());
00145     for(int i = 0; i < real.length(); ++i) {
00146       temp(i) = std::complex<double>(static_cast<double>(real(i)),
00147                                      static_cast<double>(imag(i)));
00148     }
00149     return temp;
00150   }
00151 
00156   ivec to_ivec(int s);
00157 
00162   vec to_vec(double s);
00163 
00168   cvec to_cvec(double real, double imag);
00169 
00170   // ----------------------------------------------------------------------
00171   // Converters for matrices
00172   // ----------------------------------------------------------------------
00173 
00178   template <class T>
00179   bmat to_bmat(const Mat<T> &m)
00180   {
00181     bmat temp(m.rows(), m.cols());
00182     for (int i = 0; i < temp.rows(); ++i) {
00183       for (int j = 0; j < temp.cols(); ++j) {
00184         temp(i, j) = static_cast<bin>(m(i, j));
00185       }
00186     }
00187     return temp;
00188   }
00189 
00194   template <class T>
00195   smat to_smat(const Mat<T> &m)
00196   {
00197     smat temp(m.rows(), m.cols());
00198     for (int i = 0; i < temp.rows(); ++i) {
00199       for (int j = 0; j < temp.cols(); ++j) {
00200         temp(i, j) = static_cast<short>(m(i, j));
00201       }
00202     }
00203     return temp;
00204   }
00205 
00210   template <class T>
00211   imat to_imat(const Mat<T> &m)
00212   {
00213     imat temp(m.rows(), m.cols());
00214     for (int i = 0; i < temp.rows(); ++i) {
00215       for (int j = 0; j < temp.cols(); ++j) {
00216         temp(i, j) = static_cast<int>(m(i, j));
00217       }
00218     }
00219     return temp;
00220   }
00221 
00226   template <class T>
00227   mat to_mat(const Mat<T> &m)
00228   {
00229     mat temp(m.rows(), m.cols());
00230     for (int i = 0; i < temp.rows(); ++i) {
00231       for (int j = 0; j < temp.cols(); ++j) {
00232         temp(i, j) = static_cast<double>(m(i, j));
00233       }
00234     }
00235     return temp;
00236   }
00237 
00242   template <class T>
00243   cmat to_cmat(const Mat<T> &m)
00244   {
00245     cmat temp(m.rows(), m.cols());
00246     for (int i = 0; i < temp.rows(); ++i) {
00247       for (int j = 0; j < temp.cols(); ++j) {
00248         temp(i, j) = std::complex<double>(static_cast<double>(m(i, j)), 0.0);
00249       }
00250     }
00251     return temp;
00252   }
00253 
00255   template<> inline
00256   cmat to_cmat(const cmat& m)
00257   {
00258     return m;
00259   }
00261 
00266   template <class T>
00267   cmat to_cmat(const Mat<T> &real, const Mat<T> &imag)
00268   {
00269     it_assert_debug((real.rows() == imag.rows())
00270                     && (real.cols() == imag.cols()),
00271                     "to_cmat(): real and imag part sizes does not match");
00272     cmat temp(real.rows(), real.cols());
00273     for (int i = 0; i < temp.rows(); ++i) {
00274       for (int j = 0; j < temp.cols(); ++j) {
00275         temp(i, j) = std::complex<double>(static_cast<double>(real(i, j)),
00276                                           static_cast<double>(imag(i, j)));
00277       }
00278     }
00279     return temp;
00280   }
00281 
00282 
00286   bvec dec2bin(int length, int index);
00287 
00291   void dec2bin(int index, bvec &v);
00292 
00296   bvec dec2bin(int index, bool msb_first = true);
00297 
00301   int bin2dec(const bvec &inbvec, bool msb_first = true);
00302 
00310   bvec oct2bin(const ivec &octalindex, short keepzeros = 0);
00311 
00319   ivec bin2oct(const bvec &inbits);
00320 
00322   ivec bin2pol(const bvec &inbvec);
00323 
00325   bvec pol2bin(const ivec &inpol);
00326 
00328   inline double rad_to_deg(double x) { return (180.0 / itpp::pi * x); }
00330   inline double deg_to_rad(double x) { return (itpp::pi / 180.0 * x); }
00331 
00333   inline double round(double x) { return ::rint(x); }
00335   inline vec round(const vec &x) { return apply_function<double>(::rint, x); }
00337   inline mat round(const mat &x) { return apply_function<double>(::rint, x); }
00339   inline int round_i(double x) { return static_cast<int>(::rint(x)); }
00341   ivec round_i(const vec &x);
00343   imat round_i(const mat &x);
00344 
00346   inline vec ceil(const vec &x) { return apply_function<double>(std::ceil, x); }
00348   inline mat ceil(const mat &x) { return apply_function<double>(std::ceil, x); }
00350   inline int ceil_i(double x) { return static_cast<int>(std::ceil(x)); }
00352   ivec ceil_i(const vec &x);
00354   imat ceil_i(const mat &x);
00355 
00357   inline vec floor(const vec &x) { return apply_function<double>(std::floor, x); }
00359   inline mat floor(const mat &x) { return apply_function<double>(std::floor, x); }
00361   inline int floor_i(double x) { return static_cast<int>(std::floor(x)); }
00363   ivec floor_i(const vec &x);
00365   imat floor_i(const mat &x);
00366 
00367 
00369   inline double round_to_zero(double x, double threshold = 1e-14)
00370   {
00371     return ((std::fabs(x) < threshold) ? 0.0 : x);
00372   }
00373 
00375   inline std::complex<double> round_to_zero(const std::complex<double>& x,
00376                                             double threshold = 1e-14)
00377   {
00378     return std::complex<double>(round_to_zero(x.real(), threshold),
00379                                 round_to_zero(x.imag(), threshold));
00380   }
00381 
00383   inline vec round_to_zero(const vec &x, double threshold = 1e-14)
00384   {
00385     return apply_function<double>(round_to_zero, x, threshold);
00386   }
00387 
00389   inline mat round_to_zero(const mat &x, double threshold = 1e-14)
00390   {
00391     return apply_function<double>(round_to_zero, x, threshold);
00392   }
00393 
00395   cvec round_to_zero(const cvec &x, double threshold = 1e-14);
00396 
00398   cmat round_to_zero(const cmat &x, double threshold = 1e-14);
00399 
00400 
00402   inline int gray_code(int x) { return x^(x >> 1); }
00403 
00404 
00410   template <typename T>
00411   std::string to_str(const T &i);
00412 
00420   std::string to_str(const double &i, const int precision);
00421 
00423 
00424   template <typename T>
00425   std::string to_str(const T &i)
00426   {
00427     std::ostringstream ss;
00428     ss.precision(8);
00429     ss.setf(std::ostringstream::scientific,std::ostringstream::floatfield);
00430     ss << i;
00431     return ss.str();
00432   }
00433 
00435 
00436   // ---------------------------------------------------------------------
00437   // Instantiations
00438   // ---------------------------------------------------------------------
00439 
00440 #ifdef HAVE_EXTERN_TEMPLATE
00441 
00442   extern template bvec to_bvec(const svec &v);
00443   extern template bvec to_bvec(const ivec &v);
00444 
00445   extern template svec to_svec(const bvec &v);
00446   extern template svec to_svec(const ivec &v);
00447   extern template svec to_svec(const vec &v);
00448 
00449   // Workaround for GCC 3.3.x error when using -finine-functions or -O3 flag
00450 #if (GCC_VERSION >= 30400)
00451   extern template ivec to_ivec(const bvec &v);
00452 #endif
00453   extern template ivec to_ivec(const svec &v);
00454   extern template ivec to_ivec(const vec &v);
00455 
00456   extern template vec to_vec(const bvec &v);
00457   extern template vec to_vec(const svec &v);
00458   extern template vec to_vec(const ivec &v);
00459 
00460   extern template cvec to_cvec(const bvec &v);
00461   extern template cvec to_cvec(const svec &v);
00462   extern template cvec to_cvec(const ivec &v);
00463   extern template cvec to_cvec(const vec &v);
00464 
00465   extern template cvec to_cvec(const bvec &real, const bvec &imag);
00466   extern template cvec to_cvec(const svec &real, const svec &imag);
00467   extern template cvec to_cvec(const ivec &real, const ivec &imag);
00468   extern template cvec to_cvec(const vec &real, const vec &imag);
00469 
00470   extern template bmat to_bmat(const smat &m);
00471   extern template bmat to_bmat(const imat &m);
00472 
00473   extern template smat to_smat(const bmat &m);
00474   extern template smat to_smat(const imat &m);
00475   extern template smat to_smat(const mat &m);
00476 
00477   extern template imat to_imat(const bmat &m);
00478   extern template imat to_imat(const smat &m);
00479   extern template imat to_imat(const mat &m);
00480 
00481   extern template mat to_mat(const bmat &m);
00482 #if (GCC_VERSION >= 30400)
00483   extern template mat to_mat(const smat &m);
00484   extern template mat to_mat(const imat &m);
00485 #endif
00486 
00487   extern template cmat to_cmat(const bmat &m);
00488   extern template cmat to_cmat(const smat &m);
00489   extern template cmat to_cmat(const imat &m);
00490   extern template cmat to_cmat(const mat &m);
00491 
00492   extern template cmat to_cmat(const bmat &real, const bmat &imag);
00493   extern template cmat to_cmat(const smat &real, const smat &imag);
00494   extern template cmat to_cmat(const imat &real, const imat &imag);
00495   extern template cmat to_cmat(const mat &real, const mat &imag);
00496 
00497 #endif // HAVE_EXTERN_TEMPLATE
00498 
00500 
00501 } // namespace itpp
00502 
00503 #endif // CONVERTERS_H
SourceForge Logo

Generated on Sun Apr 20 12:40:05 2008 for IT++ by Doxygen 1.5.5