00001 00029 #ifndef CONVERTERS_H 00030 #define CONVERTERS_H 00031 00032 #include <itpp/base/help_functions.h> 00033 #include <itpp/base/math/misc.h> 00034 00035 00036 namespace itpp 00037 { 00038 00040 00041 00042 // ---------------------------------------------------------------------- 00043 // Converters for vectors 00044 // ---------------------------------------------------------------------- 00045 00050 template <class T> 00051 bvec to_bvec(const Vec<T> &v) 00052 { 00053 bvec temp(v.length()); 00054 for (int i = 0; i < v.length(); ++i) { 00055 temp(i) = static_cast<bin>(v(i)); 00056 } 00057 return temp; 00058 } 00059 00064 template <class T> 00065 svec to_svec(const Vec<T> &v) 00066 { 00067 svec temp(v.length()); 00068 for (int i = 0; i < v.length(); ++i) { 00069 temp(i) = static_cast<short>(v(i)); 00070 } 00071 return temp; 00072 } 00073 00078 template <class T> 00079 ivec to_ivec(const Vec<T> &v) 00080 { 00081 ivec temp(v.length()); 00082 for (int i = 0; i < v.length(); ++i) { 00083 temp(i) = static_cast<int>(v(i)); 00084 } 00085 return temp; 00086 } 00087 00092 template <class T> 00093 vec to_vec(const Vec<T> &v) 00094 { 00095 vec temp(v.length()); 00096 for (int i = 0; i < v.length(); ++i) { 00097 temp(i) = static_cast<double>(v(i)); 00098 } 00099 return temp; 00100 } 00101 00106 template <class T> 00107 cvec to_cvec(const Vec<T> &v) 00108 { 00109 cvec temp(v.length()); 00110 for (int i = 0; i < v.length(); ++i) { 00111 temp(i) = std::complex<double>(static_cast<double>(v(i)), 0.0); 00112 } 00113 return temp; 00114 } 00115 00117 template<> inline 00118 cvec to_cvec(const cvec& v) 00119 { 00120 return v; 00121 } 00123 00128 template <class T> 00129 cvec to_cvec(const Vec<T> &real, const Vec<T> &imag) 00130 { 00131 it_assert(real.length() == imag.length(), 00132 "to_cvec(): real and imaginary parts must have the same length"); 00133 cvec temp(real.length()); 00134 for (int i = 0; i < real.length(); ++i) { 00135 temp(i) = std::complex<double>(static_cast<double>(real(i)), 00136 static_cast<double>(imag(i))); 00137 } 00138 return temp; 00139 } 00140 00145 ivec to_ivec(int s); 00146 00151 vec to_vec(double s); 00152 00157 cvec to_cvec(double real, double imag); 00158 00159 // ---------------------------------------------------------------------- 00160 // Converters for matrices 00161 // ---------------------------------------------------------------------- 00162 00167 template <class T> 00168 bmat to_bmat(const Mat<T> &m) 00169 { 00170 bmat temp(m.rows(), m.cols()); 00171 for (int i = 0; i < temp.rows(); ++i) { 00172 for (int j = 0; j < temp.cols(); ++j) { 00173 temp(i, j) = static_cast<bin>(m(i, j)); 00174 } 00175 } 00176 return temp; 00177 } 00178 00183 template <class T> 00184 smat to_smat(const Mat<T> &m) 00185 { 00186 smat temp(m.rows(), m.cols()); 00187 for (int i = 0; i < temp.rows(); ++i) { 00188 for (int j = 0; j < temp.cols(); ++j) { 00189 temp(i, j) = static_cast<short>(m(i, j)); 00190 } 00191 } 00192 return temp; 00193 } 00194 00199 template <class T> 00200 imat to_imat(const Mat<T> &m) 00201 { 00202 imat temp(m.rows(), m.cols()); 00203 for (int i = 0; i < temp.rows(); ++i) { 00204 for (int j = 0; j < temp.cols(); ++j) { 00205 temp(i, j) = static_cast<int>(m(i, j)); 00206 } 00207 } 00208 return temp; 00209 } 00210 00215 template <class T> 00216 mat to_mat(const Mat<T> &m) 00217 { 00218 mat temp(m.rows(), m.cols()); 00219 for (int i = 0; i < temp.rows(); ++i) { 00220 for (int j = 0; j < temp.cols(); ++j) { 00221 temp(i, j) = static_cast<double>(m(i, j)); 00222 } 00223 } 00224 return temp; 00225 } 00226 00231 template <class T> 00232 cmat to_cmat(const Mat<T> &m) 00233 { 00234 cmat temp(m.rows(), m.cols()); 00235 for (int i = 0; i < temp.rows(); ++i) { 00236 for (int j = 0; j < temp.cols(); ++j) { 00237 temp(i, j) = std::complex<double>(static_cast<double>(m(i, j)), 0.0); 00238 } 00239 } 00240 return temp; 00241 } 00242 00244 template<> inline 00245 cmat to_cmat(const cmat& m) 00246 { 00247 return m; 00248 } 00250 00255 template <class T> 00256 cmat to_cmat(const Mat<T> &real, const Mat<T> &imag) 00257 { 00258 it_assert_debug((real.rows() == imag.rows()) 00259 && (real.cols() == imag.cols()), 00260 "to_cmat(): real and imag part sizes does not match"); 00261 cmat temp(real.rows(), real.cols()); 00262 for (int i = 0; i < temp.rows(); ++i) { 00263 for (int j = 0; j < temp.cols(); ++j) { 00264 temp(i, j) = std::complex<double>(static_cast<double>(real(i, j)), 00265 static_cast<double>(imag(i, j))); 00266 } 00267 } 00268 return temp; 00269 } 00270 00271 00275 bvec dec2bin(int length, int index); 00276 00280 void dec2bin(int index, bvec &v); 00281 00285 bvec dec2bin(int index, bool msb_first = true); 00286 00290 int bin2dec(const bvec &inbvec, bool msb_first = true); 00291 00299 bvec oct2bin(const ivec &octalindex, short keepzeros = 0); 00300 00308 ivec bin2oct(const bvec &inbits); 00309 00311 ivec bin2pol(const bvec &inbvec); 00312 00314 bvec pol2bin(const ivec &inpol); 00315 00317 inline double rad_to_deg(double x) { return (180.0 / itpp::pi * x); } 00319 inline double deg_to_rad(double x) { return (itpp::pi / 180.0 * x); } 00320 00322 double round(double x); 00324 vec round(const vec &x); 00326 mat round(const mat &x); 00328 int round_i(double x); 00330 ivec round_i(const vec &x); 00332 imat round_i(const mat &x); 00333 00335 inline vec ceil(const vec &x) { return apply_function<double>(std::ceil, x); } 00337 inline mat ceil(const mat &x) { return apply_function<double>(std::ceil, x); } 00339 inline int ceil_i(double x) { return static_cast<int>(std::ceil(x)); } 00341 ivec ceil_i(const vec &x); 00343 imat ceil_i(const mat &x); 00344 00346 inline vec floor(const vec &x) { return apply_function<double>(std::floor, x); } 00348 inline mat floor(const mat &x) { return apply_function<double>(std::floor, x); } 00350 inline int floor_i(double x) { return static_cast<int>(std::floor(x)); } 00352 ivec floor_i(const vec &x); 00354 imat floor_i(const mat &x); 00355 00356 00358 inline double round_to_zero(double x, double threshold = 1e-14) 00359 { 00360 return ((std::fabs(x) < threshold) ? 0.0 : x); 00361 } 00362 00364 inline std::complex<double> round_to_zero(const std::complex<double>& x, 00365 double threshold = 1e-14) 00366 { 00367 return std::complex<double>(round_to_zero(x.real(), threshold), 00368 round_to_zero(x.imag(), threshold)); 00369 } 00370 00372 inline vec round_to_zero(const vec &x, double threshold = 1e-14) 00373 { 00374 return apply_function<double>(round_to_zero, x, threshold); 00375 } 00376 00378 inline mat round_to_zero(const mat &x, double threshold = 1e-14) 00379 { 00380 return apply_function<double>(round_to_zero, x, threshold); 00381 } 00382 00384 cvec round_to_zero(const cvec &x, double threshold = 1e-14); 00385 00387 cmat round_to_zero(const cmat &x, double threshold = 1e-14); 00388 00390 inline double round_to_infty(const double in, const double threshold = 1e9) 00391 { 00392 return (std::fabs(in)>threshold)?itpp::round(in):in; 00393 } 00394 00396 inline std::complex<double> round_to_infty(const std::complex<double> &in, const double threshold = 1e9) 00397 { 00398 return std::complex<double>(round_to_infty(in.real(), threshold), 00399 round_to_infty(in.imag(), threshold)); 00400 } 00401 00403 inline vec round_to_infty(const vec &in, const double threshold = 1e9) 00404 { 00405 return apply_function<double>(round_to_infty, in, threshold); 00406 } 00407 00409 inline mat round_to_infty(const mat &in, const double threshold = 1e9) 00410 { 00411 return apply_function<double>(round_to_infty, in, threshold); 00412 } 00413 00415 cvec round_to_infty(const cvec &in, const double threshold = 1e9); 00416 00418 cmat round_to_infty(const cmat &in, const double threshold = 1e9); 00419 00421 inline int gray_code(int x) { return x ^(x >> 1); } 00422 00423 00429 template <typename T> 00430 std::string to_str(const T &i); 00431 00439 std::string to_str(const double &i, const int precision); 00440 00442 00443 template <typename T> 00444 std::string to_str(const T &i) 00445 { 00446 std::ostringstream ss; 00447 ss.precision(8); 00448 ss.setf(std::ostringstream::scientific, std::ostringstream::floatfield); 00449 ss << i; 00450 return ss.str(); 00451 } 00452 00454 00455 // --------------------------------------------------------------------- 00456 // Instantiations 00457 // --------------------------------------------------------------------- 00458 00459 #ifndef _MSC_VER 00460 00461 extern template bvec to_bvec(const svec &v); 00462 extern template bvec to_bvec(const ivec &v); 00463 00464 extern template svec to_svec(const bvec &v); 00465 extern template svec to_svec(const ivec &v); 00466 extern template svec to_svec(const vec &v); 00467 00468 extern template ivec to_ivec(const bvec &v); 00469 extern template ivec to_ivec(const svec &v); 00470 extern template ivec to_ivec(const vec &v); 00471 00472 extern template vec to_vec(const bvec &v); 00473 extern template vec to_vec(const svec &v); 00474 extern template vec to_vec(const ivec &v); 00475 00476 extern template cvec to_cvec(const bvec &v); 00477 extern template cvec to_cvec(const svec &v); 00478 extern template cvec to_cvec(const ivec &v); 00479 extern template cvec to_cvec(const vec &v); 00480 00481 extern template cvec to_cvec(const bvec &real, const bvec &imag); 00482 extern template cvec to_cvec(const svec &real, const svec &imag); 00483 extern template cvec to_cvec(const ivec &real, const ivec &imag); 00484 extern template cvec to_cvec(const vec &real, const vec &imag); 00485 00486 extern template bmat to_bmat(const smat &m); 00487 extern template bmat to_bmat(const imat &m); 00488 00489 extern template smat to_smat(const bmat &m); 00490 extern template smat to_smat(const imat &m); 00491 extern template smat to_smat(const mat &m); 00492 00493 extern template imat to_imat(const bmat &m); 00494 extern template imat to_imat(const smat &m); 00495 extern template imat to_imat(const mat &m); 00496 00497 extern template mat to_mat(const bmat &m); 00498 extern template mat to_mat(const smat &m); 00499 extern template mat to_mat(const imat &m); 00500 00501 extern template cmat to_cmat(const bmat &m); 00502 extern template cmat to_cmat(const smat &m); 00503 extern template cmat to_cmat(const imat &m); 00504 extern template cmat to_cmat(const mat &m); 00505 00506 extern template cmat to_cmat(const bmat &real, const bmat &imag); 00507 extern template cmat to_cmat(const smat &real, const smat &imag); 00508 extern template cmat to_cmat(const imat &real, const imat &imag); 00509 extern template cmat to_cmat(const mat &real, const mat &imag); 00510 00511 #endif // _MSC_VER 00512 00514 00515 } // namespace itpp 00516 00517 #endif // CONVERTERS_H
Generated on Wed Jul 27 2011 16:27:04 for IT++ by Doxygen 1.7.4