00001
00029 #ifndef CONVERTERS_H
00030 #define CONVERTERS_H
00031
00032 #include <itpp/base/itcompat.h>
00033 #include <itpp/base/help_functions.h>
00034 #include <itpp/base/math/misc.h>
00035
00036
00037 namespace itpp
00038 {
00039
00041
00042
00043
00044
00045
00046
00051 template <class T>
00052 bvec to_bvec(const Vec<T> &v)
00053 {
00054 bvec temp(v.length());
00055 for (int i = 0; i < v.length(); ++i) {
00056 temp(i) = static_cast<bin>(v(i));
00057 }
00058 return temp;
00059 }
00060
00065 template <class T>
00066 svec to_svec(const Vec<T> &v)
00067 {
00068 svec temp(v.length());
00069 for (int i = 0; i < v.length(); ++i) {
00070 temp(i) = static_cast<short>(v(i));
00071 }
00072 return temp;
00073 }
00074
00079 template <class T>
00080 ivec to_ivec(const Vec<T> &v)
00081 {
00082 ivec temp(v.length());
00083 for (int i = 0; i < v.length(); ++i) {
00084 temp(i) = static_cast<int>(v(i));
00085 }
00086 return temp;
00087 }
00088
00093 template <class T>
00094 vec to_vec(const Vec<T> &v)
00095 {
00096 vec temp(v.length());
00097 for (int i = 0; i < v.length(); ++i) {
00098 temp(i) = static_cast<double>(v(i));
00099 }
00100 return temp;
00101 }
00102
00107 template <class T>
00108 cvec to_cvec(const Vec<T> &v)
00109 {
00110 cvec temp(v.length());
00111 for (int i = 0; i < v.length(); ++i) {
00112 temp(i) = std::complex<double>(static_cast<double>(v(i)), 0.0);
00113 }
00114 return temp;
00115 }
00116
00118 template<> inline
00119 cvec to_cvec(const cvec& v)
00120 {
00121 return v;
00122 }
00124
00129 template <class T>
00130 cvec to_cvec(const Vec<T> &real, const Vec<T> &imag)
00131 {
00132 it_assert(real.length() == imag.length(),
00133 "to_cvec(): real and imaginary parts must have the same length");
00134 cvec temp(real.length());
00135 for (int i = 0; i < real.length(); ++i) {
00136 temp(i) = std::complex<double>(static_cast<double>(real(i)),
00137 static_cast<double>(imag(i)));
00138 }
00139 return temp;
00140 }
00141
00146 ivec to_ivec(int s);
00147
00152 vec to_vec(double s);
00153
00158 cvec to_cvec(double real, double imag);
00159
00160
00161
00162
00163
00168 template <class T>
00169 bmat to_bmat(const Mat<T> &m)
00170 {
00171 bmat temp(m.rows(), m.cols());
00172 for (int i = 0; i < temp.rows(); ++i) {
00173 for (int j = 0; j < temp.cols(); ++j) {
00174 temp(i, j) = static_cast<bin>(m(i, j));
00175 }
00176 }
00177 return temp;
00178 }
00179
00184 template <class T>
00185 smat to_smat(const Mat<T> &m)
00186 {
00187 smat temp(m.rows(), m.cols());
00188 for (int i = 0; i < temp.rows(); ++i) {
00189 for (int j = 0; j < temp.cols(); ++j) {
00190 temp(i, j) = static_cast<short>(m(i, j));
00191 }
00192 }
00193 return temp;
00194 }
00195
00200 template <class T>
00201 imat to_imat(const Mat<T> &m)
00202 {
00203 imat temp(m.rows(), m.cols());
00204 for (int i = 0; i < temp.rows(); ++i) {
00205 for (int j = 0; j < temp.cols(); ++j) {
00206 temp(i, j) = static_cast<int>(m(i, j));
00207 }
00208 }
00209 return temp;
00210 }
00211
00216 template <class T>
00217 mat to_mat(const Mat<T> &m)
00218 {
00219 mat temp(m.rows(), m.cols());
00220 for (int i = 0; i < temp.rows(); ++i) {
00221 for (int j = 0; j < temp.cols(); ++j) {
00222 temp(i, j) = static_cast<double>(m(i, j));
00223 }
00224 }
00225 return temp;
00226 }
00227
00232 template <class T>
00233 cmat to_cmat(const Mat<T> &m)
00234 {
00235 cmat temp(m.rows(), m.cols());
00236 for (int i = 0; i < temp.rows(); ++i) {
00237 for (int j = 0; j < temp.cols(); ++j) {
00238 temp(i, j) = std::complex<double>(static_cast<double>(m(i, j)), 0.0);
00239 }
00240 }
00241 return temp;
00242 }
00243
00245 template<> inline
00246 cmat to_cmat(const cmat& m)
00247 {
00248 return m;
00249 }
00251
00256 template <class T>
00257 cmat to_cmat(const Mat<T> &real, const Mat<T> &imag)
00258 {
00259 it_assert_debug((real.rows() == imag.rows())
00260 && (real.cols() == imag.cols()),
00261 "to_cmat(): real and imag part sizes does not match");
00262 cmat temp(real.rows(), real.cols());
00263 for (int i = 0; i < temp.rows(); ++i) {
00264 for (int j = 0; j < temp.cols(); ++j) {
00265 temp(i, j) = std::complex<double>(static_cast<double>(real(i, j)),
00266 static_cast<double>(imag(i, j)));
00267 }
00268 }
00269 return temp;
00270 }
00271
00272
00276 bvec dec2bin(int length, int index);
00277
00281 void dec2bin(int index, bvec &v);
00282
00286 bvec dec2bin(int index, bool msb_first = true);
00287
00291 int bin2dec(const bvec &inbvec, bool msb_first = true);
00292
00300 bvec oct2bin(const ivec &octalindex, short keepzeros = 0);
00301
00309 ivec bin2oct(const bvec &inbits);
00310
00312 ivec bin2pol(const bvec &inbvec);
00313
00315 bvec pol2bin(const ivec &inpol);
00316
00318 inline double rad_to_deg(double x) { return (180.0 / itpp::pi * x); }
00320 inline double deg_to_rad(double x) { return (itpp::pi / 180.0 * x); }
00321
00323 inline double round(double x) { return ::rint(x); }
00325 inline vec round(const vec &x) { return apply_function<double>(::rint, x); }
00327 inline mat round(const mat &x) { return apply_function<double>(::rint, x); }
00329 inline int round_i(double x) { return static_cast<int>(::rint(x)); }
00331 ivec round_i(const vec &x);
00333 imat round_i(const mat &x);
00334
00336 inline vec ceil(const vec &x) { return apply_function<double>(std::ceil, x); }
00338 inline mat ceil(const mat &x) { return apply_function<double>(std::ceil, x); }
00340 inline int ceil_i(double x) { return static_cast<int>(std::ceil(x)); }
00342 ivec ceil_i(const vec &x);
00344 imat ceil_i(const mat &x);
00345
00347 inline vec floor(const vec &x) { return apply_function<double>(std::floor, x); }
00349 inline mat floor(const mat &x) { return apply_function<double>(std::floor, x); }
00351 inline int floor_i(double x) { return static_cast<int>(std::floor(x)); }
00353 ivec floor_i(const vec &x);
00355 imat floor_i(const mat &x);
00356
00357
00359 inline double round_to_zero(double x, double threshold = 1e-14)
00360 {
00361 return ((std::fabs(x) < threshold) ? 0.0 : x);
00362 }
00363
00365 inline std::complex<double> round_to_zero(const std::complex<double>& x,
00366 double threshold = 1e-14)
00367 {
00368 return std::complex<double>(round_to_zero(x.real(), threshold),
00369 round_to_zero(x.imag(), threshold));
00370 }
00371
00373 inline vec round_to_zero(const vec &x, double threshold = 1e-14)
00374 {
00375 return apply_function<double>(round_to_zero, x, threshold);
00376 }
00377
00379 inline mat round_to_zero(const mat &x, double threshold = 1e-14)
00380 {
00381 return apply_function<double>(round_to_zero, x, threshold);
00382 }
00383
00385 cvec round_to_zero(const cvec &x, double threshold = 1e-14);
00386
00388 cmat round_to_zero(const cmat &x, double threshold = 1e-14);
00389
00390
00392 inline int gray_code(int x) { return x ^(x >> 1); }
00393
00394
00400 template <typename T>
00401 std::string to_str(const T &i);
00402
00410 std::string to_str(const double &i, const int precision);
00411
00413
00414 template <typename T>
00415 std::string to_str(const T &i)
00416 {
00417 std::ostringstream ss;
00418 ss.precision(8);
00419 ss.setf(std::ostringstream::scientific, std::ostringstream::floatfield);
00420 ss << i;
00421 return ss.str();
00422 }
00423
00425
00426
00427
00428
00429
00430 #ifdef HAVE_EXTERN_TEMPLATE
00431
00432 extern template bvec to_bvec(const svec &v);
00433 extern template bvec to_bvec(const ivec &v);
00434
00435 extern template svec to_svec(const bvec &v);
00436 extern template svec to_svec(const ivec &v);
00437 extern template svec to_svec(const vec &v);
00438
00439
00440 #if (GCC_VERSION >= 30400)
00441 extern template ivec to_ivec(const bvec &v);
00442 #endif
00443 extern template ivec to_ivec(const svec &v);
00444 extern template ivec to_ivec(const vec &v);
00445
00446 extern template vec to_vec(const bvec &v);
00447 extern template vec to_vec(const svec &v);
00448 extern template vec to_vec(const ivec &v);
00449
00450 extern template cvec to_cvec(const bvec &v);
00451 extern template cvec to_cvec(const svec &v);
00452 extern template cvec to_cvec(const ivec &v);
00453 extern template cvec to_cvec(const vec &v);
00454
00455 extern template cvec to_cvec(const bvec &real, const bvec &imag);
00456 extern template cvec to_cvec(const svec &real, const svec &imag);
00457 extern template cvec to_cvec(const ivec &real, const ivec &imag);
00458 extern template cvec to_cvec(const vec &real, const vec &imag);
00459
00460 extern template bmat to_bmat(const smat &m);
00461 extern template bmat to_bmat(const imat &m);
00462
00463 extern template smat to_smat(const bmat &m);
00464 extern template smat to_smat(const imat &m);
00465 extern template smat to_smat(const mat &m);
00466
00467 extern template imat to_imat(const bmat &m);
00468 extern template imat to_imat(const smat &m);
00469 extern template imat to_imat(const mat &m);
00470
00471 extern template mat to_mat(const bmat &m);
00472 #if (GCC_VERSION >= 30400)
00473 extern template mat to_mat(const smat &m);
00474 extern template mat to_mat(const imat &m);
00475 #endif
00476
00477 extern template cmat to_cmat(const bmat &m);
00478 extern template cmat to_cmat(const smat &m);
00479 extern template cmat to_cmat(const imat &m);
00480 extern template cmat to_cmat(const mat &m);
00481
00482 extern template cmat to_cmat(const bmat &real, const bmat &imag);
00483 extern template cmat to_cmat(const smat &real, const smat &imag);
00484 extern template cmat to_cmat(const imat &real, const imat &imag);
00485 extern template cmat to_cmat(const mat &real, const mat &imag);
00486
00487 #endif // HAVE_EXTERN_TEMPLATE
00488
00490
00491 }
00492
00493 #endif // CONVERTERS_H