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 namespace itpp { 00044 00046 00047 00048 // ---------------------------------------------------------------------- 00049 // Converters for vectors 00050 // ---------------------------------------------------------------------- 00051 00056 template <class T> 00057 bvec to_bvec(const Vec<T> &v) 00058 { 00059 bvec temp(v.length()); 00060 for (int i = 0; i < v.length(); ++i) { 00061 temp(i) = static_cast<bin>(v(i)); 00062 } 00063 return temp; 00064 } 00065 00070 template <class T> 00071 svec to_svec(const Vec<T> &v) 00072 { 00073 svec temp(v.length()); 00074 for (int i = 0; i < v.length(); ++i) { 00075 temp(i) = static_cast<short>(v(i)); 00076 } 00077 return temp; 00078 } 00079 00084 template <class T> 00085 ivec to_ivec(const Vec<T> &v) 00086 { 00087 ivec temp(v.length()); 00088 for (int i = 0; i < v.length(); ++i) { 00089 temp(i) = static_cast<int>(v(i)); 00090 } 00091 return temp; 00092 } 00093 00098 template <class T> 00099 vec to_vec(const Vec<T> &v) 00100 { 00101 vec temp(v.length()); 00102 for (int i = 0; i < v.length(); ++i) { 00103 temp(i) = static_cast<double>(v(i)); 00104 } 00105 return temp; 00106 } 00107 00112 template <class T> 00113 cvec to_cvec(const Vec<T> &v) 00114 { 00115 cvec temp(v.length()); 00116 for (int i = 0; i < v.length(); ++i) { 00117 temp(i) = std::complex<double>(static_cast<double>(v(i)), 0.0); 00118 } 00119 return temp; 00120 } 00121 00126 template <class T> 00127 cvec to_cvec(const Vec<T> &real, const Vec<T> &imag) 00128 { 00129 it_assert(real.length() == imag.length(), 00130 "to_cvec(): real and imaginary parts must have the same length"); 00131 cvec temp(real.length()); 00132 for(int i = 0; i < real.length(); ++i) { 00133 temp(i) = std::complex<double>(static_cast<double>(real(i)), 00134 static_cast<double>(imag(i))); 00135 } 00136 return temp; 00137 } 00138 00143 ivec to_ivec(int s); 00144 00149 vec to_vec(double s); 00150 00155 cvec to_cvec(double real, double imag); 00156 00157 // ---------------------------------------------------------------------- 00158 // Converters for matrices 00159 // ---------------------------------------------------------------------- 00160 00165 template <class T> 00166 bmat to_bmat(const Mat<T> &m) 00167 { 00168 bmat temp(m.rows(), m.cols()); 00169 for (int i = 0; i < temp.rows(); ++i) { 00170 for (int j = 0; j < temp.cols(); ++j) { 00171 temp(i, j) = static_cast<bin>(m(i, j)); 00172 } 00173 } 00174 return temp; 00175 } 00176 00181 template <class T> 00182 smat to_smat(const Mat<T> &m) 00183 { 00184 smat temp(m.rows(), m.cols()); 00185 for (int i = 0; i < temp.rows(); ++i) { 00186 for (int j = 0; j < temp.cols(); ++j) { 00187 temp(i, j) = static_cast<short>(m(i, j)); 00188 } 00189 } 00190 return temp; 00191 } 00192 00197 template <class T> 00198 imat to_imat(const Mat<T> &m) 00199 { 00200 imat temp(m.rows(), m.cols()); 00201 for (int i = 0; i < temp.rows(); ++i) { 00202 for (int j = 0; j < temp.cols(); ++j) { 00203 temp(i, j) = static_cast<int>(m(i, j)); 00204 } 00205 } 00206 return temp; 00207 } 00208 00213 template <class T> 00214 mat to_mat(const Mat<T> &m) 00215 { 00216 mat temp(m.rows(), m.cols()); 00217 for (int i = 0; i < temp.rows(); ++i) { 00218 for (int j = 0; j < temp.cols(); ++j) { 00219 temp(i, j) = static_cast<double>(m(i, j)); 00220 } 00221 } 00222 return temp; 00223 } 00224 00229 template <class T> 00230 cmat to_cmat(const Mat<T> &m) 00231 { 00232 cmat temp(m.rows(), m.cols()); 00233 for (int i = 0; i < temp.rows(); ++i) { 00234 for (int j = 0; j < temp.cols(); ++j) { 00235 temp(i, j) = std::complex<double>(static_cast<double>(m(i, j)), 0.0); 00236 } 00237 } 00238 return temp; 00239 } 00240 00245 template <class T> 00246 cmat to_cmat(const Mat<T> &real, const Mat<T> &imag) 00247 { 00248 it_assert_debug((real.rows() == imag.rows()) 00249 && (real.cols() == imag.cols()), 00250 "to_cmat(): real and imag part sizes does not match"); 00251 cmat temp(real.rows(), real.cols()); 00252 for (int i = 0; i < temp.rows(); ++i) { 00253 for (int j = 0; j < temp.cols(); ++j) { 00254 temp(i, j) = std::complex<double>(static_cast<double>(real(i, j)), 00255 static_cast<double>(imag(i, j))); 00256 } 00257 } 00258 return temp; 00259 } 00260 00261 00265 bvec dec2bin(int length, int index); 00266 00270 void dec2bin(int index, bvec &v); 00271 00275 bvec dec2bin(int index, bool msb_first = true); 00276 00280 int bin2dec(const bvec &inbvec, bool msb_first = true); 00281 00289 bvec oct2bin(const ivec &octalindex, short keepzeros = 0); 00290 00298 ivec bin2oct(const bvec &inbits); 00299 00301 ivec bin2pol(const bvec &inbvec); 00302 00304 bvec pol2bin(const ivec &inpol); 00305 00307 inline double rad_to_deg(double x) { return (180.0 / itpp::pi * x); } 00309 inline double deg_to_rad(double x) { return (itpp::pi / 180.0 * x); } 00310 00311 00312 #ifndef HAVE_RINT 00314 inline double rint(double x) { return floor(x + 0.5); } 00315 #endif 00317 inline double round(double x) { return rint(x); } 00319 inline vec round(const vec &x) { return apply_function<double>(round, x); } 00321 inline mat round(const mat &x) { return apply_function<double>(round, x); } 00323 inline int round_i(double x) { return static_cast<int>(rint(x)); } 00325 ivec round_i(const vec &x); 00327 imat round_i(const mat &x); 00328 00330 inline vec ceil(const vec &x) { return apply_function<double>(std::ceil, x); } 00332 inline mat ceil(const mat &x) { return apply_function<double>(std::ceil, x); } 00334 inline int ceil_i(double x) { return static_cast<int>(std::ceil(x)); } 00336 ivec ceil_i(const vec &x); 00338 imat ceil_i(const mat &x); 00339 00341 inline vec floor(const vec &x) { return apply_function<double>(std::floor, x); } 00343 inline mat floor(const mat &x) { return apply_function<double>(std::floor, x); } 00345 inline int floor_i(double x) { return static_cast<int>(std::floor(x)); } 00347 ivec floor_i(const vec &x); 00349 imat floor_i(const mat &x); 00350 00351 00353 inline double round_to_zero(double x, double threshold = 1e-14) 00354 { 00355 return ((std::fabs(x) < threshold) ? 0.0 : x); 00356 } 00357 00359 inline std::complex<double> round_to_zero(const std::complex<double>& x, 00360 double threshold = 1e-14) 00361 { 00362 return std::complex<double>(round_to_zero(x.real(), threshold), 00363 round_to_zero(x.imag(), threshold)); 00364 } 00365 00367 inline vec round_to_zero(const vec &x, double threshold = 1e-14) 00368 { 00369 return apply_function<double>(round_to_zero, x, threshold); 00370 } 00371 00373 inline mat round_to_zero(const mat &x, double threshold = 1e-14) 00374 { 00375 return apply_function<double>(round_to_zero, x, threshold); 00376 } 00377 00379 cvec round_to_zero(const cvec &x, double threshold = 1e-14); 00380 00382 cmat round_to_zero(const cmat &x, double threshold = 1e-14); 00383 00384 00386 inline int gray_code(int x) { return x^(x >> 1); } 00387 00388 00394 template <typename T> 00395 std::string to_str(const T &i); 00396 00404 std::string to_str(const double &i, const int precision); 00405 00407 00408 template <typename T> 00409 std::string to_str(const T &i) 00410 { 00411 std::ostringstream ss; 00412 ss.precision(8); 00413 ss.setf(std::ostringstream::scientific,std::ostringstream::floatfield); 00414 ss << i; 00415 return ss.str(); 00416 } 00417 00419 00420 // --------------------------------------------------------------------- 00421 // Instantiations 00422 // --------------------------------------------------------------------- 00423 00424 #ifdef HAVE_EXTERN_TEMPLATE 00425 00426 extern template bvec to_bvec(const svec &v); 00427 extern template bvec to_bvec(const ivec &v); 00428 00429 extern template svec to_svec(const bvec &v); 00430 extern template svec to_svec(const ivec &v); 00431 extern template svec to_svec(const vec &v); 00432 00433 extern template ivec to_ivec(const bvec &v); 00434 extern template ivec to_ivec(const svec &v); 00435 extern template ivec to_ivec(const vec &v); 00436 00437 extern template vec to_vec(const bvec &v); 00438 extern template vec to_vec(const svec &v); 00439 extern template vec to_vec(const ivec &v); 00440 00441 extern template cvec to_cvec(const bvec &v); 00442 extern template cvec to_cvec(const svec &v); 00443 extern template cvec to_cvec(const ivec &v); 00444 extern template cvec to_cvec(const vec &v); 00445 00446 extern template cvec to_cvec(const bvec &real, const bvec &imag); 00447 extern template cvec to_cvec(const svec &real, const svec &imag); 00448 extern template cvec to_cvec(const ivec &real, const ivec &imag); 00449 extern template cvec to_cvec(const vec &real, const vec &imag); 00450 00451 extern template bmat to_bmat(const smat &m); 00452 extern template bmat to_bmat(const imat &m); 00453 00454 extern template smat to_smat(const bmat &m); 00455 extern template smat to_smat(const imat &m); 00456 extern template smat to_smat(const mat &m); 00457 00458 extern template imat to_imat(const bmat &m); 00459 extern template imat to_imat(const smat &m); 00460 extern template imat to_imat(const mat &m); 00461 00462 extern template mat to_mat(const bmat &m); 00463 extern template mat to_mat(const smat &m); 00464 extern template mat to_mat(const imat &m); 00465 00466 extern template cmat to_cmat(const bmat &m); 00467 extern template cmat to_cmat(const smat &m); 00468 extern template cmat to_cmat(const imat &m); 00469 extern template cmat to_cmat(const mat &m); 00470 00471 extern template cmat to_cmat(const bmat &real, const bmat &imag); 00472 extern template cmat to_cmat(const smat &real, const smat &imag); 00473 extern template cmat to_cmat(const imat &real, const imat &imag); 00474 extern template cmat to_cmat(const mat &real, const mat &imag); 00475 00476 #endif // HAVE_EXTERN_TEMPLATE 00477 00479 00480 } // namespace itpp 00481 00482 #endif // CONVERTERS_H
Generated on Sun Dec 9 17:38:40 2007 for IT++ by Doxygen 1.5.4