IT++ Logo
converters.h
Go to the documentation of this file.
1 
29 #ifndef CONVERTERS_H
30 #define CONVERTERS_H
31 
33 #include <itpp/base/math/misc.h>
34 #include <itpp/itexports.h>
35 
36 namespace itpp
37 {
38 
40 
41 
42 // ----------------------------------------------------------------------
43 // Converters for vectors
44 // ----------------------------------------------------------------------
45 
50 template <class T>
51 bvec to_bvec(const Vec<T> &v)
52 {
53  bvec temp(v.length());
54  for (int i = 0; i < v.length(); ++i) {
55  temp(i) = static_cast<bin>(v(i));
56  }
57  return temp;
58 }
59 
64 template <class T>
65 svec to_svec(const Vec<T> &v)
66 {
67  svec temp(v.length());
68  for (int i = 0; i < v.length(); ++i) {
69  temp(i) = static_cast<short>(v(i));
70  }
71  return temp;
72 }
73 
78 template <class T>
79 ivec to_ivec(const Vec<T> &v)
80 {
81  ivec temp(v.length());
82  for (int i = 0; i < v.length(); ++i) {
83  temp(i) = static_cast<int>(v(i));
84  }
85  return temp;
86 }
87 
92 template <class T>
93 vec to_vec(const Vec<T> &v)
94 {
95  vec temp(v.length());
96  for (int i = 0; i < v.length(); ++i) {
97  temp(i) = static_cast<double>(v(i));
98  }
99  return temp;
100 }
101 
106 template <class T>
107 cvec to_cvec(const Vec<T> &v)
108 {
109  cvec temp(v.length());
110  for (int i = 0; i < v.length(); ++i) {
111  temp(i) = std::complex<double>(static_cast<double>(v(i)), 0.0);
112  }
113  return temp;
114 }
115 
117 template<> inline
118 cvec to_cvec(const cvec& v)
119 {
120  return v;
121 }
123 
128 template <class T>
129 cvec to_cvec(const Vec<T> &real, const Vec<T> &imag)
130 {
131  it_assert(real.length() == imag.length(),
132  "to_cvec(): real and imaginary parts must have the same length");
133  cvec temp(real.length());
134  for (int i = 0; i < real.length(); ++i) {
135  temp(i) = std::complex<double>(static_cast<double>(real(i)),
136  static_cast<double>(imag(i)));
137  }
138  return temp;
139 }
140 
145 ivec to_ivec(int s);
146 
151 vec to_vec(double s);
152 
157 cvec to_cvec(double real, double imag);
158 
159 // ----------------------------------------------------------------------
160 // Converters for matrices
161 // ----------------------------------------------------------------------
162 
167 template <class T>
168 bmat to_bmat(const Mat<T> &m)
169 {
170  bmat temp(m.rows(), m.cols());
171  for (int i = 0; i < temp.rows(); ++i) {
172  for (int j = 0; j < temp.cols(); ++j) {
173  temp(i, j) = static_cast<bin>(m(i, j));
174  }
175  }
176  return temp;
177 }
178 
183 template <class T>
184 smat to_smat(const Mat<T> &m)
185 {
186  smat temp(m.rows(), m.cols());
187  for (int i = 0; i < temp.rows(); ++i) {
188  for (int j = 0; j < temp.cols(); ++j) {
189  temp(i, j) = static_cast<short>(m(i, j));
190  }
191  }
192  return temp;
193 }
194 
199 template <class T>
200 imat to_imat(const Mat<T> &m)
201 {
202  imat temp(m.rows(), m.cols());
203  for (int i = 0; i < temp.rows(); ++i) {
204  for (int j = 0; j < temp.cols(); ++j) {
205  temp(i, j) = static_cast<int>(m(i, j));
206  }
207  }
208  return temp;
209 }
210 
215 template <class T>
216 mat to_mat(const Mat<T> &m)
217 {
218  mat temp(m.rows(), m.cols());
219  for (int i = 0; i < temp.rows(); ++i) {
220  for (int j = 0; j < temp.cols(); ++j) {
221  temp(i, j) = static_cast<double>(m(i, j));
222  }
223  }
224  return temp;
225 }
226 
231 template <class T>
232 cmat to_cmat(const Mat<T> &m)
233 {
234  cmat temp(m.rows(), m.cols());
235  for (int i = 0; i < temp.rows(); ++i) {
236  for (int j = 0; j < temp.cols(); ++j) {
237  temp(i, j) = std::complex<double>(static_cast<double>(m(i, j)), 0.0);
238  }
239  }
240  return temp;
241 }
242 
244 template<> inline
245 cmat to_cmat(const cmat& m)
246 {
247  return m;
248 }
250 
255 template <class T>
256 cmat to_cmat(const Mat<T> &real, const Mat<T> &imag)
257 {
258  it_assert_debug((real.rows() == imag.rows())
259  && (real.cols() == imag.cols()),
260  "to_cmat(): real and imag part sizes does not match");
261  cmat temp(real.rows(), real.cols());
262  for (int i = 0; i < temp.rows(); ++i) {
263  for (int j = 0; j < temp.cols(); ++j) {
264  temp(i, j) = std::complex<double>(static_cast<double>(real(i, j)),
265  static_cast<double>(imag(i, j)));
266  }
267  }
268  return temp;
269 }
270 
271 
275 ITPP_EXPORT bvec dec2bin(int length, int index);
276 
280 ITPP_EXPORT void dec2bin(int index, bvec &v);
281 
285 ITPP_EXPORT bvec dec2bin(int index, bool msb_first = true);
286 
290 ITPP_EXPORT int bin2dec(const bvec &inbvec, bool msb_first = true);
291 
299 ITPP_EXPORT bvec oct2bin(const ivec &octalindex, short keepzeros = 0);
300 
308 ITPP_EXPORT ivec bin2oct(const bvec &inbits);
309 
311 ITPP_EXPORT ivec bin2pol(const bvec &inbvec);
312 
314 ITPP_EXPORT bvec pol2bin(const ivec &inpol);
315 
317 inline double rad_to_deg(double x) { return (180.0 / itpp::pi * x); }
319 inline double deg_to_rad(double x) { return (itpp::pi / 180.0 * x); }
320 
322 ITPP_EXPORT double round(double x);
324 ITPP_EXPORT vec round(const vec &x);
326 ITPP_EXPORT mat round(const mat &x);
328 ITPP_EXPORT int round_i(double x);
330 ITPP_EXPORT ivec round_i(const vec &x);
332 ITPP_EXPORT imat round_i(const mat &x);
333 
335 inline vec ceil(const vec &x) { return apply_function<double>(std::ceil, x); }
337 inline mat ceil(const mat &x) { return apply_function<double>(std::ceil, x); }
339 inline int ceil_i(double x) { return static_cast<int>(std::ceil(x)); }
341 ITPP_EXPORT ivec ceil_i(const vec &x);
343 ITPP_EXPORT imat ceil_i(const mat &x);
344 
346 inline vec floor(const vec &x) { return apply_function<double>(std::floor, x); }
348 inline mat floor(const mat &x) { return apply_function<double>(std::floor, x); }
350 inline int floor_i(double x) { return static_cast<int>(std::floor(x)); }
352 ITPP_EXPORT ivec floor_i(const vec &x);
354 ITPP_EXPORT imat floor_i(const mat &x);
355 
356 
358 inline double round_to_zero(double x, double threshold = 1e-14)
359 {
360  return ((std::fabs(x) < threshold) ? 0.0 : x);
361 }
362 
364 inline std::complex<double> round_to_zero(const std::complex<double>& x,
365  double threshold = 1e-14)
366 {
367  return std::complex<double>(round_to_zero(x.real(), threshold),
368  round_to_zero(x.imag(), threshold));
369 }
370 
372 inline vec round_to_zero(const vec &x, double threshold = 1e-14)
373 {
374  return apply_function<double>(round_to_zero, x, threshold);
375 }
376 
378 inline mat round_to_zero(const mat &x, double threshold = 1e-14)
379 {
380  return apply_function<double>(round_to_zero, x, threshold);
381 }
382 
384 ITPP_EXPORT cvec round_to_zero(const cvec &x, double threshold = 1e-14);
385 
387 ITPP_EXPORT cmat round_to_zero(const cmat &x, double threshold = 1e-14);
388 
390 inline double round_to_infty(const double in, const double threshold = 1e9)
391 {
392  return (std::fabs(in)>threshold)?itpp::round(in):in;
393 }
394 
396 inline std::complex<double> round_to_infty(const std::complex<double> &in, const double threshold = 1e9)
397 {
398  return std::complex<double>(round_to_infty(in.real(), threshold),
399  round_to_infty(in.imag(), threshold));
400 }
401 
403 inline vec round_to_infty(const vec &in, const double threshold = 1e9)
404 {
405  return apply_function<double>(round_to_infty, in, threshold);
406 }
407 
409 inline mat round_to_infty(const mat &in, const double threshold = 1e9)
410 {
411  return apply_function<double>(round_to_infty, in, threshold);
412 }
413 
415 ITPP_EXPORT cvec round_to_infty(const cvec &in, const double threshold = 1e9);
416 
418 ITPP_EXPORT cmat round_to_infty(const cmat &in, const double threshold = 1e9);
419 
421 inline int gray_code(int x) { return x ^(x >> 1); }
422 
423 
429 template <typename T>
430 std::string to_str(const T &i);
431 
439 ITPP_EXPORT std::string to_str(const double &i, const int precision);
440 
442 
443 template <typename T>
444 std::string to_str(const T &i)
445 {
446  std::ostringstream ss;
447  ss.precision(8);
448  ss.setf(std::ostringstream::scientific, std::ostringstream::floatfield);
449  ss << i;
450  return ss.str();
451 }
452 
454 
455 // ---------------------------------------------------------------------
456 // Instantiations
457 // ---------------------------------------------------------------------
458 
459 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec to_bvec(const svec &v);
460 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec to_bvec(const ivec &v);
461 
462 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec to_svec(const bvec &v);
463 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec to_svec(const ivec &v);
464 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec to_svec(const vec &v);
465 
466 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec to_ivec(const bvec &v);
467 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec to_ivec(const svec &v);
468 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec to_ivec(const vec &v);
469 
470 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec to_vec(const bvec &v);
471 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec to_vec(const svec &v);
472 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec to_vec(const ivec &v);
473 
474 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec to_cvec(const bvec &v);
475 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec to_cvec(const svec &v);
476 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec to_cvec(const ivec &v);
477 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec to_cvec(const vec &v);
478 
479 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec to_cvec(const bvec &real, const bvec &imag);
480 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec to_cvec(const svec &real, const svec &imag);
481 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec to_cvec(const ivec &real, const ivec &imag);
482 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec to_cvec(const vec &real, const vec &imag);
483 
484 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat to_bmat(const smat &m);
485 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat to_bmat(const imat &m);
486 
487 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat to_smat(const bmat &m);
488 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat to_smat(const imat &m);
489 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat to_smat(const mat &m);
490 
491 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat to_imat(const bmat &m);
492 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat to_imat(const smat &m);
493 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat to_imat(const mat &m);
494 
495 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat to_mat(const bmat &m);
496 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat to_mat(const smat &m);
497 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat to_mat(const imat &m);
498 
499 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat to_cmat(const bmat &m);
500 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat to_cmat(const smat &m);
501 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat to_cmat(const imat &m);
502 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat to_cmat(const mat &m);
503 
504 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat to_cmat(const bmat &real, const bmat &imag);
505 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat to_cmat(const smat &real, const smat &imag);
506 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat to_cmat(const imat &real, const imat &imag);
507 ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat to_cmat(const mat &real, const mat &imag);
508 
510 
511 } // namespace itpp
512 
513 #endif // CONVERTERS_H
SourceForge Logo

Generated on Sat May 25 2013 16:32:18 for IT++ by Doxygen 1.8.2