IT++ Logo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fix_functions.h
Go to the documentation of this file.
1 
30 #ifndef FIX_FUNCTIONS_H
31 #define FIX_FUNCTIONS_H
32 
33 #include <itpp/fixed/cfix.h>
34 #include <itpp/base/vec.h>
35 #include <itpp/base/mat.h>
36 #include <itpp/base/array.h>
37 #include <itpp/base/converters.h>
38 
39 
40 namespace itpp
41 {
42 
45 
47 template<class T> inline bool is_fix(const T &) {return false;}
49 template<> inline bool is_fix(const Fix &) {return true;}
51 template<> inline bool is_fix(const fixvec &) {return true;}
53 template<> inline bool is_fix(const fixmat &) {return true;}
55 template<> inline bool is_fix(const CFix &) {return true;}
57 template<> inline bool is_fix(const cfixvec &) {return true;}
59 template<> inline bool is_fix(const cfixmat &) {return true;}
61 template<class T> inline bool is_fix(const Array<T> &) {return is_fix(T());}
62 
64 inline void set_fix(Fix &y, double x, int n) {y.set(x, n);}
66 inline void set_fix(Fix &y, double x, int n, q_mode q) {y.set(x, n, q);}
68 inline void set_fix(fixvec &y, const vec &x, int n)
69 {
70  y.set_size(x.length());
71  for (int i = 0; i < y.size(); i++) y(i).set(x(i), n);
72 }
74 inline void set_fix(fixvec &y, const vec &x, int n, q_mode q)
75 {
76  y.set_size(x.length());
77  for (int i = 0; i < y.size(); i++) y(i).set(x(i), n, q);
78 }
80 inline void set_fix(fixmat &y, const mat &x, int n)
81 {
82  y.set_size(x.rows(), x.cols());
83  for (int i = 0; i < y.size(); i++) y(i).set(x(i), n);
84 }
86 inline void set_fix(fixmat &y, const mat &x, int n, q_mode q)
87 {
88  y.set_size(x.rows(), x.cols());
89  for (int i = 0; i < y.size(); i++) y(i).set(x(i), n, q);
90 }
92 inline void set_fix(double &y, double x, int) {y = x;}
94 inline void set_fix(double &y, double x, int, q_mode) {y = x;}
96 inline void set_fix(vec &y, const vec &x, int) {y = x;}
98 inline void set_fix(vec &y, const vec &x, int, q_mode) {y = x;}
100 inline void set_fix(mat &y, const mat &x, int) {y = x;}
102 inline void set_fix(mat &y, const mat &x, int, q_mode) {y = x;}
103 
105 inline void set_fix(CFix &y, std::complex<double> x, int n) {y.set(x, n);}
107 inline void set_fix(CFix &y, double real, double imag, int n) {y.set(real, imag, n);}
109 inline void set_fix(CFix &y, std::complex<double> x, int n, q_mode q) {y.set(x, n, q);}
111 inline void set_fix(CFix &y, double real, double imag, int n, q_mode q) {y.set(real, imag, n, q);}
113 inline void set_fix(cfixvec &y, const cvec &x, int n)
114 {
115  y.set_size(x.length());
116  for (int i = 0; i < y.size(); i++) y(i).set(x(i), n);
117 }
119 inline void set_fix(cfixvec &y, const vec &real, const vec &imag, int n)
120 {
121  it_assert_debug(real.length() == imag.length(), "set_fix: real and imag should have the same size");
122  y.set_size(real.length());
123  for (int i = 0; i < y.size(); i++) y(i).set(real(i), imag(i), n);
124 }
126 inline void set_fix(cfixvec &y, const cvec &x, int n, q_mode q)
127 {
128  y.set_size(x.length());
129  for (int i = 0; i < y.size(); i++) y(i).set(x(i), n, q);
130 }
132 inline void set_fix(cfixvec &y, const vec &real, const vec &imag, int n, q_mode q)
133 {
134  it_assert_debug(real.length() == imag.length(), "set_fix: real and imag should have the same size");
135  y.set_size(real.length());
136  for (int i = 0; i < y.size(); i++) y(i).set(real(i), imag(i), n, q);
137 }
139 inline void set_fix(cfixmat &y, const cmat &x, int n)
140 {
141  y.set_size(x.rows(), x.cols());
142  for (int i = 0; i < y.size(); i++) y(i).set(x(i), n);
143 }
145 inline void set_fix(cfixmat &y, const mat &real, const mat &imag, int n)
146 {
147  it_assert_debug(real.rows() == imag.rows() && real.cols() == imag.cols(), "set_fix: real and imag should have the same size");
148  y.set_size(real.rows(), real.cols());
149  for (int i = 0; i < y.size(); i++) y(i).set(real(i), imag(i), n);
150 }
152 inline void set_fix(cfixmat &y, const cmat &x, int n, q_mode q)
153 {
154  y.set_size(x.rows(), x.cols());
155  for (int i = 0; i < y.size(); i++) y(i).set(x(i), n, q);
156 }
158 inline void set_fix(cfixmat &y, const mat &real, const mat &imag, int n, q_mode q)
159 {
160  it_assert_debug(real.rows() == imag.rows() && real.cols() == imag.cols(), "set_fix: real and imag should have the same size");
161  y.set_size(real.rows(), real.cols());
162  for (int i = 0; i < y.size(); i++) y(i).set(real(i), imag(i), n, q);
163 }
165 inline void set_fix(std::complex<double> &y, const std::complex<double> &x, int) {y = x;}
167 inline void set_fix(std::complex<double> &y, double real, double imag, int) {y = std::complex<double>(real, imag);}
169 inline void set_fix(std::complex<double> &y, const std::complex<double> &x, int, q_mode) {y = x;}
171 inline void set_fix(std::complex<double> &y, double real, double imag, int, q_mode) {y = std::complex<double>(real, imag);}
173 inline void set_fix(cvec &y, const cvec &x, int) {y = x;}
175 inline void set_fix(cvec &y, const vec &real, const vec &imag, int) {y = to_cvec(real, imag);}
177 inline void set_fix(cvec &y, const cvec &x, int, q_mode) {y = x;}
179 inline void set_fix(cvec &y, const vec &real, const vec &imag, int, q_mode) {y = to_cvec(real, imag);}
181 inline void set_fix(cmat &y, const cmat &x, int) {y = x;}
183 inline void set_fix(cmat &y, const mat &real, const mat &imag, int) {y = to_cmat(real, imag);}
185 inline void set_fix(cmat &y, const cmat &x, int, q_mode) {y = x;}
187 inline void set_fix(cmat &y, const mat &real, const mat &imag, int, q_mode) {y = to_cmat(real, imag);}
188 
190 template<class T1, class T2> inline void set_fix(Array<T1> &y, const Array<T2> &x, int n)
191 {
192  y.set_size(x.size());
193  for (int i = 0; i < y.size(); i++) set_fix(y(i), x(i), n);
194 }
196 template<class T1, class T2> inline void set_fix(Array<T1> &y, const Array<T2> &real, const Array<T2> &imag, int n)
197 {
198  it_assert_debug(real.size() == imag.size(), "set_fix: real and imag should have the same size");
199  y.set_size(real.size());
200  for (int i = 0; i < y.size(); i++) set_fix(y(i), real(i), imag(i), n);
201 }
203 template<class T1, class T2> inline void set_fix(Array<T1> &y, const Array<T2> &x, int n, q_mode q)
204 {
205  y.set_size(x.size());
206  for (int i = 0; i < y.size(); i++) set_fix(y(i), x(i), n, q);
207 }
209 template<class T1, class T2> inline void set_fix(Array<T1> &y, const Array<T2> &real, const Array<T2> &imag, int n, q_mode q)
210 {
211  it_assert_debug(real.size() == imag.size(), "set_fix: real and imag should have the same size");
212  y.set_size(real.size());
213  for (int i = 0; i < y.size(); i++) set_fix(y(i), real(i), imag(i), n, q);
214 }
215 
217 inline void lshift_fix(Fix &y, int n) {y.lshift(n);}
219 inline void rshift_fix(Fix &y, int n) {y.rshift(n);}
221 inline void rshift_fix(Fix &y, int n, q_mode q) {y.rshift(n, q);}
223 inline void lshift_fix(fixvec &y, int n)
224 {for(int i = 0; i < y.size(); i++) y(i).lshift(n);}
226 inline void rshift_fix(fixvec &y, int n)
227 {for(int i = 0; i < y.size(); i++) y(i).rshift(n);}
229 inline void rshift_fix(fixvec &y, int n, q_mode q)
230 {for(int i = 0; i < y.size(); i++) y(i).rshift(n, q);}
232 inline void lshift_fix(fixmat &y, int n)
233 {for(int i = 0; i < y.size(); i++) y(i).lshift(n);}
235 inline void rshift_fix(fixmat &y, int n)
236 {for(int i = 0; i < y.size(); i++) y(i).rshift(n);}
238 inline void rshift_fix(fixmat &y, int n, q_mode q)
239 {for(int i = 0; i < y.size(); i++) y(i).rshift(n, q);}
241 inline void lshift_fix(double &, int) {}
243 inline void rshift_fix(double &, int) {}
245 inline void rshift_fix(double &, int, q_mode) {}
247 inline void lshift_fix(vec &, int) {}
249 inline void rshift_fix(vec &, int) {}
251 inline void rshift_fix(vec &, int, q_mode) {}
253 inline void lshift_fix(mat &, int) {}
255 inline void rshift_fix(mat &, int) {}
257 inline void rshift_fix(mat &, int, q_mode) {}
259 inline void lshift_fix(CFix &y, int n) {y.lshift(n);}
261 inline void rshift_fix(CFix &y, int n) {y.rshift(n);}
263 inline void rshift_fix(CFix &y, int n, q_mode q) {y.rshift(n, q);}
265 inline void lshift_fix(cfixvec &y, int n)
266 {for(int i = 0; i < y.size(); i++) y(i).lshift(n);}
268 inline void rshift_fix(cfixvec &y, int n)
269 {for(int i = 0; i < y.size(); i++) y(i).rshift(n);}
271 inline void rshift_fix(cfixvec &y, int n, q_mode q)
272 {for(int i = 0; i < y.size(); i++) y(i).rshift(n, q);}
274 inline void lshift_fix(cfixmat &y, int n)
275 {for(int i = 0; i < y.size(); i++) y(i).lshift(n);}
277 inline void rshift_fix(cfixmat &y, int n)
278 {for(int i = 0; i < y.size(); i++) y(i).rshift(n);}
280 inline void rshift_fix(cfixmat &y, int n, q_mode q)
281 {for(int i = 0; i < y.size(); i++) y(i).rshift(n, q);}
283 inline void lshift_fix(std::complex<double> &, int) {}
285 inline void rshift_fix(std::complex<double> &, int) {}
287 inline void rshift_fix(std::complex<double> &, int, q_mode) {}
289 inline void lshift_fix(cvec &, int) {}
291 inline void rshift_fix(cvec &, int) {}
293 inline void rshift_fix(cvec &, int, q_mode) {}
295 inline void lshift_fix(cmat &, int) {}
297 inline void rshift_fix(cmat &, int) {}
299 inline void rshift_fix(cmat &, int, q_mode) {}
301 template<class T> inline void lshift_fix(Array<T> &y, int n)
302 {for(int i = 0; i < y.size(); i++) lshift_fix(y(i), n);}
304 template<class T> inline void rshift_fix(Array<T> &y, int n)
305 {for(int i = 0; i < y.size(); i++) rshift_fix(y(i), n);}
307 template<class T> inline void rshift_fix(Array<T> &y, int n, q_mode q)
308 {for(int i = 0; i < y.size(); i++) rshift_fix(y(i), n, q);}
309 
311 inline void assert_fixshift(double, int) {}
313 inline void assert_fixshift(const std::complex<double> &, int) {}
315 inline void assert_fixshift(const Fix &x, int shift)
316 {it_assert_debug(x.get_shift() == shift, "Shift should be " + to_str(shift) + " but it is " + to_str(x.get_shift()) + ".");}
318 inline void assert_fixshift(const CFix &x, int shift)
319 {it_assert_debug(x.get_shift() == shift, "Shift should be " + to_str(shift) + " but it is " + to_str(x.get_shift()) + ".");}
320 
322 vec to_vec(const fixvec &v);
324 cvec to_cvec(const cfixvec &v);
326 mat to_mat(const fixmat &m);
328 cmat to_cmat(const cfixmat &m);
329 
331 
333 template<class T, class U>
334 class ConvertU2T
335 {
336 public:
337  typedef T result;
338 };
340 template<class T, class U>
341 class ConvertU2T<T, Array<U> >
342 {
343 public:
344  typedef Array<typename ConvertU2T<T, U>::result> result; // Recursive
345 };
347 template<class T, class U>
348 class ConvertU2T<T, Vec<U> >
349 {
350 public:
351  typedef Vec<T> result;
352 };
354 template<class T, class U>
355 class ConvertU2T<T, Mat<U> >
356 {
357 public:
358  typedef Mat<T> result;
359 };
360 
362 
364 template<class T> inline T to(double x) {return T(x);}
366 template<class T> inline T to(const Fix &x) {return T(x);}
368 template<class T> inline T to(const std::complex<double> &x) {return T(x);}
370 template<class T> inline T to(const CFix &x) {return T(x);}
372 template<class T> inline T to(double real, double imag) {return T(real, imag);}
374 template<class T> inline T to(const Fix &real, const Fix &imag) {return T(real, imag);}
375 
377 template<class T, class U> Vec<T> to(const Vec<U> &x)
378 {
379  Vec<T> y(x.length());
380  for (int i = 0; i < x.length(); i++) {
381  y(i) = T(x(i));
382  }
383  return y;
384 }
386 template<> inline vec to<double>(const vec &x) {return x;}
388 template<> inline cvec to<std::complex<double> >(const cvec &x) {return x;}
390 template<> inline fixvec to<Fix>(const fixvec &x) {return x;}
392 template<> inline cfixvec to<CFix>(const cfixvec &x) {return x;}
393 
395 template<class T, class U> Vec<T> to(const Vec<U> &real, const Vec<U> &imag)
396 {
397  it_assert_debug(real.length() == imag.length(), "to: real and imag should have the same size");
398  Vec<T> y(real.length());
399  for (int i = 0; i < real.length(); i++) {
400  y(i) = T(real(i), imag(i));
401  }
402  return y;
403 }
404 
406 template<class T, class U> Mat<T> to(const Mat<U> &x)
407 {
408  Mat<T> y(x.rows(), x.cols());
409  for (int i = 0; i < x.rows(); i++) {
410  for (int j = 0; j < x.cols(); j++) {
411  y(i, j) = T(x(i, j));
412  }
413  }
414  return y;
415 }
417 template<> inline mat to<double>(const mat &x) {return x;}
419 template<> inline cmat to<std::complex<double> >(const cmat &x) {return x;}
421 template<> inline fixmat to<Fix>(const fixmat &x) {return x;}
423 template<> inline cfixmat to<CFix>(const cfixmat &x) {return x;}
424 
426 template<class T, class U> Mat<T> to(const Mat<U> &real, const Mat<U> &imag)
427 {
428  it_assert_debug(real.rows() == imag.rows() && real.cols() == imag.cols(), "to: real and imag should have the same size");
429  Mat<T> y(real.rows(), real.cols());
430  for (int i = 0; i < real.rows(); i++) {
431  for (int j = 0; j < real.cols(); j++) {
432  y(i, j) = T(real(i, j), imag(i, j));
433  }
434  }
435  return y;
436 }
437 
439 template<class T, class U>
441 {
443  for (int i = 0; i < x.size(); i++) {
444  y(i) = to<T>(x(i));
445  }
446  return y;
447 }
448 
450 template<class T, class U>
452 {
453  it_assert_debug(real.size() == imag.size(), "to: real and imag should have the same size");
455  for (int i = 0; i < real.size(); i++) {
456  y(i) = to<T>(real(i), imag(i));
457  }
458  return y;
459 }
460 
462 inline double unfix(const Fix &x) {return x.unfix();}
464 inline std::complex<double> unfix(const CFix &x) {return x.unfix();}
466 inline vec unfix(const fixvec &x) {return to_vec(x);}
468 inline cvec unfix(const cfixvec &x) {return to_cvec(x);}
470 inline mat unfix(const fixmat &x) {return to_mat(x);}
472 inline cmat unfix(const cfixmat &x) {return to_cmat(x);}
473 
475 inline double unfix(double x) {return x;}
477 inline std::complex<double> unfix(const std::complex<double> &x) {return x;}
479 inline vec unfix(const vec &x) {return x;}
481 inline cvec unfix(const cvec &x) {return x;}
483 inline mat unfix(const mat &x) {return x;}
485 inline cmat unfix(const cmat &x) {return x;}
486 
488 
490 template<class T>
491 class Convert
492 {
493 public:
494  typedef double to_double;
495 };
497 template<>
498 class Convert<CFix>
499 {
500 public:
501  typedef std::complex<double> to_double;
502 };
504 template<class T>
505 class Convert<std::complex<T> >
506 {
507 public:
508  typedef std::complex<double> to_double;
509 };
511 template<class T>
512 class Convert<Array<T> >
513 {
514 public:
515  typedef Array<typename Convert<T>::to_double> to_double; // Recursive
516 };
518 template<class T>
519 class Convert<Vec<T> >
520 {
521 public:
522  typedef Vec<typename Convert<T>::to_double> to_double; // Recursive
523 };
525 template<class T>
526 class Convert<Mat<T> >
527 {
528 public:
529  typedef Mat<typename Convert<T>::to_double> to_double; // Recursive
530 };
531 
533 
535 template<class T>
537 {
539  for (int i = 0; i < x.size(); i++) {
540  y(i) = unfix(x(i));
541  }
542  return y;
543 }
544 
546 Fix abs(const Fix &x);
548 Fix real(const CFix &x);
550 Fix imag(const CFix &x);
552 CFix conj(const CFix &x);
553 
555 
556 } // namespace itpp
557 
558 #endif // #ifndef FIX_FUNCTIONS_H
SourceForge Logo

Generated on Fri Mar 21 2014 17:14:13 for IT++ by Doxygen 1.8.1.2