00001 00031 #ifndef FIX_FUNCTIONS_H 00032 #define FIX_FUNCTIONS_H 00033 00034 #include <itpp/fixed/cfix.h> 00035 #include <itpp/base/vec.h> 00036 #include <itpp/base/mat.h> 00037 #include <itpp/base/array.h> 00038 #include <itpp/base/converters.h> 00039 00040 00041 namespace itpp 00042 { 00043 00046 00048 template<class T> inline bool is_fix(const T &) {return false;} 00050 template<> inline bool is_fix(const Fix &) {return true;} 00052 template<> inline bool is_fix(const fixvec &) {return true;} 00054 template<> inline bool is_fix(const fixmat &) {return true;} 00056 template<> inline bool is_fix(const CFix &) {return true;} 00058 template<> inline bool is_fix(const cfixvec &) {return true;} 00060 template<> inline bool is_fix(const cfixmat &) {return true;} 00062 template<class T> inline bool is_fix(const Array<T> &) {return is_fix(T());} 00063 00065 inline void set_fix(Fix &y, double x, int n) {y.set(x, n);} 00067 inline void set_fix(Fix &y, double x, int n, q_mode q) {y.set(x, n, q);} 00069 inline void set_fix(fixvec &y, const vec &x, int n) 00070 { 00071 y.set_size(x.length()); 00072 for (int i = 0; i < y.size(); i++) y(i).set(x(i), n); 00073 } 00075 inline void set_fix(fixvec &y, const vec &x, int n, q_mode q) 00076 { 00077 y.set_size(x.length()); 00078 for (int i = 0; i < y.size(); i++) y(i).set(x(i), n, q); 00079 } 00081 inline void set_fix(fixmat &y, const mat &x, int n) 00082 { 00083 y.set_size(x.rows(), x.cols()); 00084 for (int i = 0; i < y.size(); i++) y(i).set(x(i), n); 00085 } 00087 inline void set_fix(fixmat &y, const mat &x, int n, q_mode q) 00088 { 00089 y.set_size(x.rows(), x.cols()); 00090 for (int i = 0; i < y.size(); i++) y(i).set(x(i), n, q); 00091 } 00093 inline void set_fix(double &y, double x, int) {y = x;} 00095 inline void set_fix(double &y, double x, int, q_mode) {y = x;} 00097 inline void set_fix(vec &y, const vec &x, int) {y = x;} 00099 inline void set_fix(vec &y, const vec &x, int, q_mode) {y = x;} 00101 inline void set_fix(mat &y, const mat &x, int) {y = x;} 00103 inline void set_fix(mat &y, const mat &x, int, q_mode) {y = x;} 00104 00106 inline void set_fix(CFix &y, std::complex<double> x, int n) {y.set(x, n);} 00108 inline void set_fix(CFix &y, double real, double imag, int n) {y.set(real, imag, n);} 00110 inline void set_fix(CFix &y, std::complex<double> x, int n, q_mode q) {y.set(x, n, q);} 00112 inline void set_fix(CFix &y, double real, double imag, int n, q_mode q) {y.set(real, imag, n, q);} 00114 inline void set_fix(cfixvec &y, const cvec &x, int n) 00115 { 00116 y.set_size(x.length()); 00117 for (int i = 0; i < y.size(); i++) y(i).set(x(i), n); 00118 } 00120 inline void set_fix(cfixvec &y, const vec &real, const vec &imag, int n) 00121 { 00122 it_assert_debug(real.length() == imag.length(), "set_fix: real and imag should have the same size"); 00123 y.set_size(real.length()); 00124 for (int i = 0; i < y.size(); i++) y(i).set(real(i), imag(i), n); 00125 } 00127 inline void set_fix(cfixvec &y, const cvec &x, int n, q_mode q) 00128 { 00129 y.set_size(x.length()); 00130 for (int i = 0; i < y.size(); i++) y(i).set(x(i), n, q); 00131 } 00133 inline void set_fix(cfixvec &y, const vec &real, const vec &imag, int n, q_mode q) 00134 { 00135 it_assert_debug(real.length() == imag.length(), "set_fix: real and imag should have the same size"); 00136 y.set_size(real.length()); 00137 for (int i = 0; i < y.size(); i++) y(i).set(real(i), imag(i), n, q); 00138 } 00140 inline void set_fix(cfixmat &y, const cmat &x, int n) 00141 { 00142 y.set_size(x.rows(), x.cols()); 00143 for (int i = 0; i < y.size(); i++) y(i).set(x(i), n); 00144 } 00146 inline void set_fix(cfixmat &y, const mat &real, const mat &imag, int n) 00147 { 00148 it_assert_debug(real.rows() == imag.rows() && real.cols() == imag.cols(), "set_fix: real and imag should have the same size"); 00149 y.set_size(real.rows(), real.cols()); 00150 for (int i = 0; i < y.size(); i++) y(i).set(real(i), imag(i), n); 00151 } 00153 inline void set_fix(cfixmat &y, const cmat &x, int n, q_mode q) 00154 { 00155 y.set_size(x.rows(), x.cols()); 00156 for (int i = 0; i < y.size(); i++) y(i).set(x(i), n, q); 00157 } 00159 inline void set_fix(cfixmat &y, const mat &real, const mat &imag, int n, q_mode q) 00160 { 00161 it_assert_debug(real.rows() == imag.rows() && real.cols() == imag.cols(), "set_fix: real and imag should have the same size"); 00162 y.set_size(real.rows(), real.cols()); 00163 for (int i = 0; i < y.size(); i++) y(i).set(real(i), imag(i), n, q); 00164 } 00166 inline void set_fix(std::complex<double> &y, const std::complex<double> &x, int) {y = x;} 00168 inline void set_fix(std::complex<double> &y, double real, double imag, int) {y = std::complex<double>(real, imag);} 00170 inline void set_fix(std::complex<double> &y, const std::complex<double> &x, int, q_mode) {y = x;} 00172 inline void set_fix(std::complex<double> &y, double real, double imag, int, q_mode) {y = std::complex<double>(real, imag);} 00174 inline void set_fix(cvec &y, const cvec &x, int) {y = x;} 00176 inline void set_fix(cvec &y, const vec &real, const vec &imag, int) {y = to_cvec(real, imag);} 00178 inline void set_fix(cvec &y, const cvec &x, int, q_mode) {y = x;} 00180 inline void set_fix(cvec &y, const vec &real, const vec &imag, int, q_mode) {y = to_cvec(real, imag);} 00182 inline void set_fix(cmat &y, const cmat &x, int) {y = x;} 00184 inline void set_fix(cmat &y, const mat &real, const mat &imag, int) {y = to_cmat(real, imag);} 00186 inline void set_fix(cmat &y, const cmat &x, int, q_mode) {y = x;} 00188 inline void set_fix(cmat &y, const mat &real, const mat &imag, int, q_mode) {y = to_cmat(real, imag);} 00189 00191 template<class T1, class T2> inline void set_fix(Array<T1> &y, const Array<T2> &x, int n) 00192 { 00193 y.set_size(x.size()); 00194 for (int i = 0; i < y.size(); i++) set_fix(y(i), x(i), n); 00195 } 00197 template<class T1, class T2> inline void set_fix(Array<T1> &y, const Array<T2> &real, const Array<T2> &imag, int n) 00198 { 00199 it_assert_debug(real.size() == imag.size(), "set_fix: real and imag should have the same size"); 00200 y.set_size(real.size()); 00201 for (int i = 0; i < y.size(); i++) set_fix(y(i), real(i), imag(i), n); 00202 } 00204 template<class T1, class T2> inline void set_fix(Array<T1> &y, const Array<T2> &x, int n, q_mode q) 00205 { 00206 y.set_size(x.size()); 00207 for (int i = 0; i < y.size(); i++) set_fix(y(i), x(i), n, q); 00208 } 00210 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) 00211 { 00212 it_assert_debug(real.size() == imag.size(), "set_fix: real and imag should have the same size"); 00213 y.set_size(real.size()); 00214 for (int i = 0; i < y.size(); i++) set_fix(y(i), real(i), imag(i), n, q); 00215 } 00216 00218 inline void lshift_fix(Fix &y, int n) {y.lshift(n);} 00220 inline void rshift_fix(Fix &y, int n) {y.rshift(n);} 00222 inline void rshift_fix(Fix &y, int n, q_mode q) {y.rshift(n, q);} 00224 inline void lshift_fix(fixvec &y, int n) 00225 {for(int i = 0; i < y.size(); i++) y(i).lshift(n);} 00227 inline void rshift_fix(fixvec &y, int n) 00228 {for(int i = 0; i < y.size(); i++) y(i).rshift(n);} 00230 inline void rshift_fix(fixvec &y, int n, q_mode q) 00231 {for(int i = 0; i < y.size(); i++) y(i).rshift(n, q);} 00233 inline void lshift_fix(fixmat &y, int n) 00234 {for(int i = 0; i < y.size(); i++) y(i).lshift(n);} 00236 inline void rshift_fix(fixmat &y, int n) 00237 {for(int i = 0; i < y.size(); i++) y(i).rshift(n);} 00239 inline void rshift_fix(fixmat &y, int n, q_mode q) 00240 {for(int i = 0; i < y.size(); i++) y(i).rshift(n, q);} 00242 inline void lshift_fix(double &, int) {} 00244 inline void rshift_fix(double &, int) {} 00246 inline void rshift_fix(double &, int, q_mode) {} 00248 inline void lshift_fix(vec &, int) {} 00250 inline void rshift_fix(vec &, int) {} 00252 inline void rshift_fix(vec &, int, q_mode) {} 00254 inline void lshift_fix(mat &, int) {} 00256 inline void rshift_fix(mat &, int) {} 00258 inline void rshift_fix(mat &, int, q_mode) {} 00260 inline void lshift_fix(CFix &y, int n) {y.lshift(n);} 00262 inline void rshift_fix(CFix &y, int n) {y.rshift(n);} 00264 inline void rshift_fix(CFix &y, int n, q_mode q) {y.rshift(n, q);} 00266 inline void lshift_fix(cfixvec &y, int n) 00267 {for(int i = 0; i < y.size(); i++) y(i).lshift(n);} 00269 inline void rshift_fix(cfixvec &y, int n) 00270 {for(int i = 0; i < y.size(); i++) y(i).rshift(n);} 00272 inline void rshift_fix(cfixvec &y, int n, q_mode q) 00273 {for(int i = 0; i < y.size(); i++) y(i).rshift(n, q);} 00275 inline void lshift_fix(cfixmat &y, int n) 00276 {for(int i = 0; i < y.size(); i++) y(i).lshift(n);} 00278 inline void rshift_fix(cfixmat &y, int n) 00279 {for(int i = 0; i < y.size(); i++) y(i).rshift(n);} 00281 inline void rshift_fix(cfixmat &y, int n, q_mode q) 00282 {for(int i = 0; i < y.size(); i++) y(i).rshift(n, q);} 00284 inline void lshift_fix(std::complex<double> &, int) {} 00286 inline void rshift_fix(std::complex<double> &, int) {} 00288 inline void rshift_fix(std::complex<double> &, int, q_mode) {} 00290 inline void lshift_fix(cvec &, int) {} 00292 inline void rshift_fix(cvec &, int) {} 00294 inline void rshift_fix(cvec &, int, q_mode) {} 00296 inline void lshift_fix(cmat &, int) {} 00298 inline void rshift_fix(cmat &, int) {} 00300 inline void rshift_fix(cmat &, int, q_mode) {} 00302 template<class T> inline void lshift_fix(Array<T> &y, int n) 00303 {for(int i = 0; i < y.size(); i++) lshift_fix(y(i), n);} 00305 template<class T> inline void rshift_fix(Array<T> &y, int n) 00306 {for(int i = 0; i < y.size(); i++) rshift_fix(y(i), n);} 00308 template<class T> inline void rshift_fix(Array<T> &y, int n, q_mode q) 00309 {for(int i = 0; i < y.size(); i++) rshift_fix(y(i), n, q);} 00310 00312 inline void assert_fixshift(double, int) {} 00314 inline void assert_fixshift(const std::complex<double> &, int) {} 00316 inline void assert_fixshift(const Fix &x, int shift) 00317 {it_assert_debug(x.get_shift() == shift, "Shift should be " + to_str(shift) + " but it is " + to_str(x.get_shift()) + ".");} 00319 inline void assert_fixshift(const CFix &x, int shift) 00320 {it_assert_debug(x.get_shift() == shift, "Shift should be " + to_str(shift) + " but it is " + to_str(x.get_shift()) + ".");} 00321 00323 vec to_vec(const fixvec &v); 00325 cvec to_cvec(const cfixvec &v); 00327 mat to_mat(const fixmat &m); 00329 cmat to_cmat(const cfixmat &m); 00330 00332 00334 template<class T, class U> 00335 class ConvertU2T 00336 { 00337 public: 00338 typedef T result; 00339 }; 00341 template<class T, class U> 00342 class ConvertU2T<T, Array<U> > 00343 { 00344 public: 00345 typedef Array<typename ConvertU2T<T, U>::result> result; // Recursive 00346 }; 00348 template<class T, class U> 00349 class ConvertU2T<T, Vec<U> > 00350 { 00351 public: 00352 typedef Vec<T> result; 00353 }; 00355 template<class T, class U> 00356 class ConvertU2T<T, Mat<U> > 00357 { 00358 public: 00359 typedef Mat<T> result; 00360 }; 00361 00363 00365 template<class T> inline T to(double x) {return T(x);} 00367 template<class T> inline T to(const Fix &x) {return T(x);} 00369 template<class T> inline T to(const std::complex<double> &x) {return T(x);} 00371 template<class T> inline T to(const CFix &x) {return T(x);} 00373 template<class T> inline T to(double real, double imag) {return T(real, imag);} 00375 template<class T> inline T to(const Fix &real, const Fix &imag) {return T(real, imag);} 00376 00378 template<class T, class U> Vec<T> to(const Vec<U> &x) 00379 { 00380 Vec<T> y(x.length()); 00381 for (int i = 0; i < x.length(); i++) { 00382 y(i) = T(x(i)); 00383 } 00384 return y; 00385 } 00387 template<> inline vec to<double>(const vec &x) {return x;} 00389 template<> inline cvec to<std::complex<double> >(const cvec &x) {return x;} 00391 template<> inline fixvec to<Fix>(const fixvec &x) {return x;} 00393 template<> inline cfixvec to<CFix>(const cfixvec &x) {return x;} 00394 00396 template<class T, class U> Vec<T> to(const Vec<U> &real, const Vec<U> &imag) 00397 { 00398 it_assert_debug(real.length() == imag.length(), "to: real and imag should have the same size"); 00399 Vec<T> y(real.length()); 00400 for (int i = 0; i < real.length(); i++) { 00401 y(i) = T(real(i), imag(i)); 00402 } 00403 return y; 00404 } 00405 00407 template<class T, class U> Mat<T> to(const Mat<U> &x) 00408 { 00409 Mat<T> y(x.rows(), x.cols()); 00410 for (int i = 0; i < x.rows(); i++) { 00411 for (int j = 0; j < x.cols(); j++) { 00412 y(i, j) = T(x(i, j)); 00413 } 00414 } 00415 return y; 00416 } 00418 template<> inline mat to<double>(const mat &x) {return x;} 00420 template<> inline cmat to<std::complex<double> >(const cmat &x) {return x;} 00422 template<> inline fixmat to<Fix>(const fixmat &x) {return x;} 00424 template<> inline cfixmat to<CFix>(const cfixmat &x) {return x;} 00425 00427 template<class T, class U> Mat<T> to(const Mat<U> &real, const Mat<U> &imag) 00428 { 00429 it_assert_debug(real.rows() == imag.rows() && real.cols() == imag.cols(), "to: real and imag should have the same size"); 00430 Mat<T> y(real.rows(), real.cols()); 00431 for (int i = 0; i < real.rows(); i++) { 00432 for (int j = 0; j < real.cols(); j++) { 00433 y(i, j) = T(real(i, j), imag(i, j)); 00434 } 00435 } 00436 return y; 00437 } 00438 00440 template<class T, class U> 00441 Array<typename ConvertU2T<T, U>::result> to(const Array<U> &x) 00442 { 00443 Array<typename ConvertU2T<T, U>::result> y(x.size()); 00444 for (int i = 0; i < x.size(); i++) { 00445 y(i) = to<T>(x(i)); 00446 } 00447 return y; 00448 } 00449 00451 template<class T, class U> 00452 Array<typename ConvertU2T<T, U>::result> to(const Array<U> &real, const Array<U> &imag) 00453 { 00454 it_assert_debug(real.size() == imag.size(), "to: real and imag should have the same size"); 00455 Array<typename ConvertU2T<T, U>::result> y(real.size()); 00456 for (int i = 0; i < real.size(); i++) { 00457 y(i) = to<T>(real(i), imag(i)); 00458 } 00459 return y; 00460 } 00461 00463 inline double unfix(const Fix &x) {return x.unfix();} 00465 inline std::complex<double> unfix(const CFix &x) {return x.unfix();} 00467 inline vec unfix(const fixvec &x) {return to_vec(x);} 00469 inline cvec unfix(const cfixvec &x) {return to_cvec(x);} 00471 inline mat unfix(const fixmat &x) {return to_mat(x);} 00473 inline cmat unfix(const cfixmat &x) {return to_cmat(x);} 00474 00476 inline double unfix(double x) {return x;} 00478 inline std::complex<double> unfix(const std::complex<double> &x) {return x;} 00480 inline vec unfix(const vec &x) {return x;} 00482 inline cvec unfix(const cvec &x) {return x;} 00484 inline mat unfix(const mat &x) {return x;} 00486 inline cmat unfix(const cmat &x) {return x;} 00487 00489 00491 template<class T> 00492 class Convert 00493 { 00494 public: 00495 typedef double to_double; 00496 }; 00498 template<> 00499 class Convert<CFix> 00500 { 00501 public: 00502 typedef std::complex<double> to_double; 00503 }; 00505 template<class T> 00506 class Convert<std::complex<T> > 00507 { 00508 public: 00509 typedef std::complex<double> to_double; 00510 }; 00512 template<class T> 00513 class Convert<Array<T> > 00514 { 00515 public: 00516 typedef Array<typename Convert<T>::to_double> to_double; // Recursive 00517 }; 00519 template<class T> 00520 class Convert<Vec<T> > 00521 { 00522 public: 00523 typedef Vec<typename Convert<T>::to_double> to_double; // Recursive 00524 }; 00526 template<class T> 00527 class Convert<Mat<T> > 00528 { 00529 public: 00530 typedef Mat<typename Convert<T>::to_double> to_double; // Recursive 00531 }; 00532 00534 00536 template<class T> 00537 Array<typename Convert<T>::to_double> unfix(const Array<T> &x) 00538 { 00539 Array<typename Convert<T>::to_double> y(x.size()); 00540 for (int i = 0; i < x.size(); i++) { 00541 y(i) = unfix(x(i)); 00542 } 00543 return y; 00544 } 00545 00547 Fix abs(const Fix &x); 00549 Fix real(const CFix &x); 00551 Fix imag(const CFix &x); 00553 CFix conj(const CFix &x); 00554 00556 00557 } // namespace itpp 00558 00559 #endif // #ifndef FIX_FUNCTIONS_H
Generated on Tue Nov 23 08:47:56 2010 for IT++ by Doxygen 1.6.1