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 {
00050 
00052 
00053 
00054 // ----------------------------------------------------------------------
00055 // Converters for vectors
00056 // ----------------------------------------------------------------------
00057 
00062 template <class T>
00063 bvec to_bvec(const Vec<T> &v)
00064 {
00065   bvec temp(v.length());
00066   for (int i = 0; i < v.length(); ++i) {
00067     temp(i) = static_cast<bin>(v(i));
00068   }
00069   return temp;
00070 }
00071 
00076 template <class T>
00077 svec to_svec(const Vec<T> &v)
00078 {
00079   svec temp(v.length());
00080   for (int i = 0; i < v.length(); ++i) {
00081     temp(i) = static_cast<short>(v(i));
00082   }
00083   return temp;
00084 }
00085 
00090 template <class T>
00091 ivec to_ivec(const Vec<T> &v)
00092 {
00093   ivec temp(v.length());
00094   for (int i = 0; i < v.length(); ++i) {
00095     temp(i) = static_cast<int>(v(i));
00096   }
00097   return temp;
00098 }
00099 
00104 template <class T>
00105 vec to_vec(const Vec<T> &v)
00106 {
00107   vec temp(v.length());
00108   for (int i = 0; i < v.length(); ++i) {
00109     temp(i) = static_cast<double>(v(i));
00110   }
00111   return temp;
00112 }
00113 
00118 template <class T>
00119 cvec to_cvec(const Vec<T> &v)
00120 {
00121   cvec temp(v.length());
00122   for (int i = 0; i < v.length(); ++i) {
00123     temp(i) = std::complex<double>(static_cast<double>(v(i)), 0.0);
00124   }
00125   return temp;
00126 }
00127 
00129 template<> inline
00130 cvec to_cvec(const cvec& v)
00131 {
00132   return v;
00133 }
00135 
00140 template <class T>
00141 cvec to_cvec(const Vec<T> &real, const Vec<T> &imag)
00142 {
00143   it_assert(real.length() == imag.length(),
00144             "to_cvec(): real and imaginary parts must have the same length");
00145   cvec temp(real.length());
00146   for (int i = 0; i < real.length(); ++i) {
00147     temp(i) = std::complex<double>(static_cast<double>(real(i)),
00148                                    static_cast<double>(imag(i)));
00149   }
00150   return temp;
00151 }
00152 
00157 ivec to_ivec(int s);
00158 
00163 vec to_vec(double s);
00164 
00169 cvec to_cvec(double real, double imag);
00170 
00171 // ----------------------------------------------------------------------
00172 // Converters for matrices
00173 // ----------------------------------------------------------------------
00174 
00179 template <class T>
00180 bmat to_bmat(const Mat<T> &m)
00181 {
00182   bmat temp(m.rows(), m.cols());
00183   for (int i = 0; i < temp.rows(); ++i) {
00184     for (int j = 0; j < temp.cols(); ++j) {
00185       temp(i, j) = static_cast<bin>(m(i, j));
00186     }
00187   }
00188   return temp;
00189 }
00190 
00195 template <class T>
00196 smat to_smat(const Mat<T> &m)
00197 {
00198   smat temp(m.rows(), m.cols());
00199   for (int i = 0; i < temp.rows(); ++i) {
00200     for (int j = 0; j < temp.cols(); ++j) {
00201       temp(i, j) = static_cast<short>(m(i, j));
00202     }
00203   }
00204   return temp;
00205 }
00206 
00211 template <class T>
00212 imat to_imat(const Mat<T> &m)
00213 {
00214   imat temp(m.rows(), m.cols());
00215   for (int i = 0; i < temp.rows(); ++i) {
00216     for (int j = 0; j < temp.cols(); ++j) {
00217       temp(i, j) = static_cast<int>(m(i, j));
00218     }
00219   }
00220   return temp;
00221 }
00222 
00227 template <class T>
00228 mat to_mat(const Mat<T> &m)
00229 {
00230   mat temp(m.rows(), m.cols());
00231   for (int i = 0; i < temp.rows(); ++i) {
00232     for (int j = 0; j < temp.cols(); ++j) {
00233       temp(i, j) = static_cast<double>(m(i, j));
00234     }
00235   }
00236   return temp;
00237 }
00238 
00243 template <class T>
00244 cmat to_cmat(const Mat<T> &m)
00245 {
00246   cmat temp(m.rows(), m.cols());
00247   for (int i = 0; i < temp.rows(); ++i) {
00248     for (int j = 0; j < temp.cols(); ++j) {
00249       temp(i, j) = std::complex<double>(static_cast<double>(m(i, j)), 0.0);
00250     }
00251   }
00252   return temp;
00253 }
00254 
00256 template<> inline
00257 cmat to_cmat(const cmat& m)
00258 {
00259   return m;
00260 }
00262 
00267 template <class T>
00268 cmat to_cmat(const Mat<T> &real, const Mat<T> &imag)
00269 {
00270   it_assert_debug((real.rows() == imag.rows())
00271                   && (real.cols() == imag.cols()),
00272                   "to_cmat(): real and imag part sizes does not match");
00273   cmat temp(real.rows(), real.cols());
00274   for (int i = 0; i < temp.rows(); ++i) {
00275     for (int j = 0; j < temp.cols(); ++j) {
00276       temp(i, j) = std::complex<double>(static_cast<double>(real(i, j)),
00277                                         static_cast<double>(imag(i, j)));
00278     }
00279   }
00280   return temp;
00281 }
00282 
00283 
00287 bvec dec2bin(int length, int index);
00288 
00292 void dec2bin(int index, bvec &v);
00293 
00297 bvec dec2bin(int index, bool msb_first = true);
00298 
00302 int bin2dec(const bvec &inbvec, bool msb_first = true);
00303 
00311 bvec oct2bin(const ivec &octalindex, short keepzeros = 0);
00312 
00320 ivec bin2oct(const bvec &inbits);
00321 
00323 ivec bin2pol(const bvec &inbvec);
00324 
00326 bvec pol2bin(const ivec &inpol);
00327 
00329 inline double rad_to_deg(double x) { return (180.0 / itpp::pi * x); }
00331 inline double deg_to_rad(double x) { return (itpp::pi / 180.0 * x); }
00332 
00334 inline double round(double x) { return ::rint(x); }
00336 inline vec round(const vec &x) { return apply_function<double>(::rint, x); }
00338 inline mat round(const mat &x) { return apply_function<double>(::rint, x); }
00340 inline int round_i(double x) { return static_cast<int>(::rint(x)); }
00342 ivec round_i(const vec &x);
00344 imat round_i(const mat &x);
00345 
00347 inline vec ceil(const vec &x) { return apply_function<double>(std::ceil, x); }
00349 inline mat ceil(const mat &x) { return apply_function<double>(std::ceil, x); }
00351 inline int ceil_i(double x) { return static_cast<int>(std::ceil(x)); }
00353 ivec ceil_i(const vec &x);
00355 imat ceil_i(const mat &x);
00356 
00358 inline vec floor(const vec &x) { return apply_function<double>(std::floor, x); }
00360 inline mat floor(const mat &x) { return apply_function<double>(std::floor, x); }
00362 inline int floor_i(double x) { return static_cast<int>(std::floor(x)); }
00364 ivec floor_i(const vec &x);
00366 imat floor_i(const mat &x);
00367 
00368 
00370 inline double round_to_zero(double x, double threshold = 1e-14)
00371 {
00372   return ((std::fabs(x) < threshold) ? 0.0 : x);
00373 }
00374 
00376 inline std::complex<double> round_to_zero(const std::complex<double>& x,
00377     double threshold = 1e-14)
00378 {
00379   return std::complex<double>(round_to_zero(x.real(), threshold),
00380                               round_to_zero(x.imag(), threshold));
00381 }
00382 
00384 inline vec round_to_zero(const vec &x, double threshold = 1e-14)
00385 {
00386   return apply_function<double>(round_to_zero, x, threshold);
00387 }
00388 
00390 inline mat round_to_zero(const mat &x, double threshold = 1e-14)
00391 {
00392   return apply_function<double>(round_to_zero, x, threshold);
00393 }
00394 
00396 cvec round_to_zero(const cvec &x, double threshold = 1e-14);
00397 
00399 cmat round_to_zero(const cmat &x, double threshold = 1e-14);
00400 
00401 
00403 inline int gray_code(int x) { return x ^(x >> 1); }
00404 
00405 
00411 template <typename T>
00412 std::string to_str(const T &i);
00413 
00421 std::string to_str(const double &i, const int precision);
00422 
00424 
00425 template <typename T>
00426 std::string to_str(const T &i)
00427 {
00428   std::ostringstream ss;
00429   ss.precision(8);
00430   ss.setf(std::ostringstream::scientific, std::ostringstream::floatfield);
00431   ss << i;
00432   return ss.str();
00433 }
00434 
00436 
00437 // ---------------------------------------------------------------------
00438 // Instantiations
00439 // ---------------------------------------------------------------------
00440 
00441 #ifdef HAVE_EXTERN_TEMPLATE
00442 
00443 extern template bvec to_bvec(const svec &v);
00444 extern template bvec to_bvec(const ivec &v);
00445 
00446 extern template svec to_svec(const bvec &v);
00447 extern template svec to_svec(const ivec &v);
00448 extern template svec to_svec(const vec &v);
00449 
00450 // Workaround for GCC 3.3.x error when using -finine-functions or -O3 flag
00451 #if (GCC_VERSION >= 30400)
00452 extern template ivec to_ivec(const bvec &v);
00453 #endif
00454 extern template ivec to_ivec(const svec &v);
00455 extern template ivec to_ivec(const vec &v);
00456 
00457 extern template vec to_vec(const bvec &v);
00458 extern template vec to_vec(const svec &v);
00459 extern template vec to_vec(const ivec &v);
00460 
00461 extern template cvec to_cvec(const bvec &v);
00462 extern template cvec to_cvec(const svec &v);
00463 extern template cvec to_cvec(const ivec &v);
00464 extern template cvec to_cvec(const vec &v);
00465 
00466 extern template cvec to_cvec(const bvec &real, const bvec &imag);
00467 extern template cvec to_cvec(const svec &real, const svec &imag);
00468 extern template cvec to_cvec(const ivec &real, const ivec &imag);
00469 extern template cvec to_cvec(const vec &real, const vec &imag);
00470 
00471 extern template bmat to_bmat(const smat &m);
00472 extern template bmat to_bmat(const imat &m);
00473 
00474 extern template smat to_smat(const bmat &m);
00475 extern template smat to_smat(const imat &m);
00476 extern template smat to_smat(const mat &m);
00477 
00478 extern template imat to_imat(const bmat &m);
00479 extern template imat to_imat(const smat &m);
00480 extern template imat to_imat(const mat &m);
00481 
00482 extern template mat to_mat(const bmat &m);
00483 #if (GCC_VERSION >= 30400)
00484 extern template mat to_mat(const smat &m);
00485 extern template mat to_mat(const imat &m);
00486 #endif
00487 
00488 extern template cmat to_cmat(const bmat &m);
00489 extern template cmat to_cmat(const smat &m);
00490 extern template cmat to_cmat(const imat &m);
00491 extern template cmat to_cmat(const mat &m);
00492 
00493 extern template cmat to_cmat(const bmat &real, const bmat &imag);
00494 extern template cmat to_cmat(const smat &real, const smat &imag);
00495 extern template cmat to_cmat(const imat &real, const imat &imag);
00496 extern template cmat to_cmat(const mat &real, const mat &imag);
00497 
00498 #endif // HAVE_EXTERN_TEMPLATE
00499 
00501 
00502 } // namespace itpp
00503 
00504 #endif // CONVERTERS_H
SourceForge Logo

Generated on Sun Jul 26 08:36:47 2009 for IT++ by Doxygen 1.5.9