00001 00030 #ifndef FIX_FUNCTIONS_H 00031 #define FIX_FUNCTIONS_H 00032 00033 #include <itpp/fixed/cfix.h> 00034 #include <itpp/base/vec.h> 00035 #include <itpp/base/mat.h> 00036 #include <itpp/base/array.h> 00037 #include <itpp/base/converters.h> 00038 00039 00040 namespace itpp 00041 { 00042 00045 00047 template<class T> inline bool is_fix(const T &) {return false;} 00049 template<> inline bool is_fix(const Fix &) {return true;} 00051 template<> inline bool is_fix(const fixvec &) {return true;} 00053 template<> inline bool is_fix(const fixmat &) {return true;} 00055 template<> inline bool is_fix(const CFix &) {return true;} 00057 template<> inline bool is_fix(const cfixvec &) {return true;} 00059 template<> inline bool is_fix(const cfixmat &) {return true;} 00061 template<class T> inline bool is_fix(const Array<T> &) {return is_fix(T());} 00062 00064 inline void set_fix(Fix &y, double x, int n) {y.set(x, n);} 00066 inline void set_fix(Fix &y, double x, int n, q_mode q) {y.set(x, n, q);} 00068 inline void set_fix(fixvec &y, const vec &x, int n) 00069 { 00070 y.set_size(x.length()); 00071 for (int i = 0; i < y.size(); i++) y(i).set(x(i), n); 00072 } 00074 inline void set_fix(fixvec &y, const vec &x, int n, q_mode q) 00075 { 00076 y.set_size(x.length()); 00077 for (int i = 0; i < y.size(); i++) y(i).set(x(i), n, q); 00078 } 00080 inline void set_fix(fixmat &y, const mat &x, int n) 00081 { 00082 y.set_size(x.rows(), x.cols()); 00083 for (int i = 0; i < y.size(); i++) y(i).set(x(i), n); 00084 } 00086 inline void set_fix(fixmat &y, const mat &x, int n, q_mode q) 00087 { 00088 y.set_size(x.rows(), x.cols()); 00089 for (int i = 0; i < y.size(); i++) y(i).set(x(i), n, q); 00090 } 00092 inline void set_fix(double &y, double x, int) {y = x;} 00094 inline void set_fix(double &y, double x, int, q_mode) {y = x;} 00096 inline void set_fix(vec &y, const vec &x, int) {y = x;} 00098 inline void set_fix(vec &y, const vec &x, int, q_mode) {y = x;} 00100 inline void set_fix(mat &y, const mat &x, int) {y = x;} 00102 inline void set_fix(mat &y, const mat &x, int, q_mode) {y = x;} 00103 00105 inline void set_fix(CFix &y, std::complex<double> x, int n) {y.set(x, n);} 00107 inline void set_fix(CFix &y, double real, double imag, int n) {y.set(real, imag, n);} 00109 inline void set_fix(CFix &y, std::complex<double> x, int n, q_mode q) {y.set(x, n, q);} 00111 inline void set_fix(CFix &y, double real, double imag, int n, q_mode q) {y.set(real, imag, n, q);} 00113 inline void set_fix(cfixvec &y, const cvec &x, int n) 00114 { 00115 y.set_size(x.length()); 00116 for (int i = 0; i < y.size(); i++) y(i).set(x(i), n); 00117 } 00119 inline void set_fix(cfixvec &y, const vec &real, const vec &imag, int n) 00120 { 00121 it_assert_debug(real.length() == imag.length(), "set_fix: real and imag should have the same size"); 00122 y.set_size(real.length()); 00123 for (int i = 0; i < y.size(); i++) y(i).set(real(i), imag(i), n); 00124 } 00126 inline void set_fix(cfixvec &y, const cvec &x, int n, q_mode q) 00127 { 00128 y.set_size(x.length()); 00129 for (int i = 0; i < y.size(); i++) y(i).set(x(i), n, q); 00130 } 00132 inline void set_fix(cfixvec &y, const vec &real, const vec &imag, int n, q_mode q) 00133 { 00134 it_assert_debug(real.length() == imag.length(), "set_fix: real and imag should have the same size"); 00135 y.set_size(real.length()); 00136 for (int i = 0; i < y.size(); i++) y(i).set(real(i), imag(i), n, q); 00137 } 00139 inline void set_fix(cfixmat &y, const cmat &x, int n) 00140 { 00141 y.set_size(x.rows(), x.cols()); 00142 for (int i = 0; i < y.size(); i++) y(i).set(x(i), n); 00143 } 00145 inline void set_fix(cfixmat &y, const mat &real, const mat &imag, int n) 00146 { 00147 it_assert_debug(real.rows() == imag.rows() && real.cols() == imag.cols(), "set_fix: real and imag should have the same size"); 00148 y.set_size(real.rows(), real.cols()); 00149 for (int i = 0; i < y.size(); i++) y(i).set(real(i), imag(i), n); 00150 } 00152 inline void set_fix(cfixmat &y, const cmat &x, int n, q_mode q) 00153 { 00154 y.set_size(x.rows(), x.cols()); 00155 for (int i = 0; i < y.size(); i++) y(i).set(x(i), n, q); 00156 } 00158 inline void set_fix(cfixmat &y, const mat &real, const mat &imag, int n, q_mode q) 00159 { 00160 it_assert_debug(real.rows() == imag.rows() && real.cols() == imag.cols(), "set_fix: real and imag should have the same size"); 00161 y.set_size(real.rows(), real.cols()); 00162 for (int i = 0; i < y.size(); i++) y(i).set(real(i), imag(i), n, q); 00163 } 00165 inline void set_fix(std::complex<double> &y, const std::complex<double> &x, int) {y = x;} 00167 inline void set_fix(std::complex<double> &y, double real, double imag, int) {y = std::complex<double>(real, imag);} 00169 inline void set_fix(std::complex<double> &y, const std::complex<double> &x, int, q_mode) {y = x;} 00171 inline void set_fix(std::complex<double> &y, double real, double imag, int, q_mode) {y = std::complex<double>(real, imag);} 00173 inline void set_fix(cvec &y, const cvec &x, int) {y = x;} 00175 inline void set_fix(cvec &y, const vec &real, const vec &imag, int) {y = to_cvec(real, imag);} 00177 inline void set_fix(cvec &y, const cvec &x, int, q_mode) {y = x;} 00179 inline void set_fix(cvec &y, const vec &real, const vec &imag, int, q_mode) {y = to_cvec(real, imag);} 00181 inline void set_fix(cmat &y, const cmat &x, int) {y = x;} 00183 inline void set_fix(cmat &y, const mat &real, const mat &imag, int) {y = to_cmat(real, imag);} 00185 inline void set_fix(cmat &y, const cmat &x, int, q_mode) {y = x;} 00187 inline void set_fix(cmat &y, const mat &real, const mat &imag, int, q_mode) {y = to_cmat(real, imag);} 00188 00190 template<class T1, class T2> inline void set_fix(Array<T1> &y, const Array<T2> &x, int n) 00191 { 00192 y.set_size(x.size()); 00193 for (int i = 0; i < y.size(); i++) set_fix(y(i), x(i), n); 00194 } 00196 template<class T1, class T2> inline void set_fix(Array<T1> &y, const Array<T2> &real, const Array<T2> &imag, int n) 00197 { 00198 it_assert_debug(real.size() == imag.size(), "set_fix: real and imag should have the same size"); 00199 y.set_size(real.size()); 00200 for (int i = 0; i < y.size(); i++) set_fix(y(i), real(i), imag(i), n); 00201 } 00203 template<class T1, class T2> inline void set_fix(Array<T1> &y, const Array<T2> &x, int n, q_mode q) 00204 { 00205 y.set_size(x.size()); 00206 for (int i = 0; i < y.size(); i++) set_fix(y(i), x(i), n, q); 00207 } 00209 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) 00210 { 00211 it_assert_debug(real.size() == imag.size(), "set_fix: real and imag should have the same size"); 00212 y.set_size(real.size()); 00213 for (int i = 0; i < y.size(); i++) set_fix(y(i), real(i), imag(i), n, q); 00214 } 00215 00217 inline void lshift_fix(Fix &y, int n) {y.lshift(n);} 00219 inline void rshift_fix(Fix &y, int n) {y.rshift(n);} 00221 inline void rshift_fix(Fix &y, int n, q_mode q) {y.rshift(n, q);} 00223 inline void lshift_fix(fixvec &y, int n) 00224 {for(int i = 0; i < y.size(); i++) y(i).lshift(n);} 00226 inline void rshift_fix(fixvec &y, int n) 00227 {for(int i = 0; i < y.size(); i++) y(i).rshift(n);} 00229 inline void rshift_fix(fixvec &y, int n, q_mode q) 00230 {for(int i = 0; i < y.size(); i++) y(i).rshift(n, q);} 00232 inline void lshift_fix(fixmat &y, int n) 00233 {for(int i = 0; i < y.size(); i++) y(i).lshift(n);} 00235 inline void rshift_fix(fixmat &y, int n) 00236 {for(int i = 0; i < y.size(); i++) y(i).rshift(n);} 00238 inline void rshift_fix(fixmat &y, int n, q_mode q) 00239 {for(int i = 0; i < y.size(); i++) y(i).rshift(n, q);} 00241 inline void lshift_fix(double &, int) {} 00243 inline void rshift_fix(double &, int) {} 00245 inline void rshift_fix(double &, int, q_mode) {} 00247 inline void lshift_fix(vec &, int) {} 00249 inline void rshift_fix(vec &, int) {} 00251 inline void rshift_fix(vec &, int, q_mode) {} 00253 inline void lshift_fix(mat &, int) {} 00255 inline void rshift_fix(mat &, int) {} 00257 inline void rshift_fix(mat &, int, q_mode) {} 00259 inline void lshift_fix(CFix &y, int n) {y.lshift(n);} 00261 inline void rshift_fix(CFix &y, int n) {y.rshift(n);} 00263 inline void rshift_fix(CFix &y, int n, q_mode q) {y.rshift(n, q);} 00265 inline void lshift_fix(cfixvec &y, int n) 00266 {for(int i = 0; i < y.size(); i++) y(i).lshift(n);} 00268 inline void rshift_fix(cfixvec &y, int n) 00269 {for(int i = 0; i < y.size(); i++) y(i).rshift(n);} 00271 inline void rshift_fix(cfixvec &y, int n, q_mode q) 00272 {for(int i = 0; i < y.size(); i++) y(i).rshift(n, q);} 00274 inline void lshift_fix(cfixmat &y, int n) 00275 {for(int i = 0; i < y.size(); i++) y(i).lshift(n);} 00277 inline void rshift_fix(cfixmat &y, int n) 00278 {for(int i = 0; i < y.size(); i++) y(i).rshift(n);} 00280 inline void rshift_fix(cfixmat &y, int n, q_mode q) 00281 {for(int i = 0; i < y.size(); i++) y(i).rshift(n, q);} 00283 inline void lshift_fix(std::complex<double> &, int) {} 00285 inline void rshift_fix(std::complex<double> &, int) {} 00287 inline void rshift_fix(std::complex<double> &, int, q_mode) {} 00289 inline void lshift_fix(cvec &, int) {} 00291 inline void rshift_fix(cvec &, int) {} 00293 inline void rshift_fix(cvec &, int, q_mode) {} 00295 inline void lshift_fix(cmat &, int) {} 00297 inline void rshift_fix(cmat &, int) {} 00299 inline void rshift_fix(cmat &, int, q_mode) {} 00301 template<class T> inline void lshift_fix(Array<T> &y, int n) 00302 {for(int i = 0; i < y.size(); i++) lshift_fix(y(i), n);} 00304 template<class T> inline void rshift_fix(Array<T> &y, int n) 00305 {for(int i = 0; i < y.size(); i++) rshift_fix(y(i), n);} 00307 template<class T> inline void rshift_fix(Array<T> &y, int n, q_mode q) 00308 {for(int i = 0; i < y.size(); i++) rshift_fix(y(i), n, q);} 00309 00311 inline void assert_fixshift(double, int) {} 00313 inline void assert_fixshift(const std::complex<double> &, int) {} 00315 inline void assert_fixshift(const Fix &x, int shift) 00316 {it_assert_debug(x.get_shift() == shift, "Shift should be " + to_str(shift) + " but it is " + to_str(x.get_shift()) + ".");} 00318 inline void assert_fixshift(const CFix &x, int shift) 00319 {it_assert_debug(x.get_shift() == shift, "Shift should be " + to_str(shift) + " but it is " + to_str(x.get_shift()) + ".");} 00320 00322 vec to_vec(const fixvec &v); 00324 cvec to_cvec(const cfixvec &v); 00326 mat to_mat(const fixmat &m); 00328 cmat to_cmat(const cfixmat &m); 00329 00331 00333 template<class T, class U> 00334 class ConvertU2T 00335 { 00336 public: 00337 typedef T result; 00338 }; 00340 template<class T, class U> 00341 class ConvertU2T<T, Array<U> > 00342 { 00343 public: 00344 typedef Array<typename ConvertU2T<T, U>::result> result; // Recursive 00345 }; 00347 template<class T, class U> 00348 class ConvertU2T<T, Vec<U> > 00349 { 00350 public: 00351 typedef Vec<T> result; 00352 }; 00354 template<class T, class U> 00355 class ConvertU2T<T, Mat<U> > 00356 { 00357 public: 00358 typedef Mat<T> result; 00359 }; 00360 00362 00364 template<class T> inline T to(double x) {return T(x);} 00366 template<class T> inline T to(const Fix &x) {return T(x);} 00368 template<class T> inline T to(const std::complex<double> &x) {return T(x);} 00370 template<class T> inline T to(const CFix &x) {return T(x);} 00372 template<class T> inline T to(double real, double imag) {return T(real, imag);} 00374 template<class T> inline T to(const Fix &real, const Fix &imag) {return T(real, imag);} 00375 00377 template<class T, class U> Vec<T> to(const Vec<U> &x) 00378 { 00379 Vec<T> y(x.length()); 00380 for (int i = 0; i < x.length(); i++) { 00381 y(i) = T(x(i)); 00382 } 00383 return y; 00384 } 00386 template<> inline vec to<double>(const vec &x) {return x;} 00388 template<> inline cvec to<std::complex<double> >(const cvec &x) {return x;} 00390 template<> inline fixvec to<Fix>(const fixvec &x) {return x;} 00392 template<> inline cfixvec to<CFix>(const cfixvec &x) {return x;} 00393 00395 template<class T, class U> Vec<T> to(const Vec<U> &real, const Vec<U> &imag) 00396 { 00397 it_assert_debug(real.length() == imag.length(), "to: real and imag should have the same size"); 00398 Vec<T> y(real.length()); 00399 for (int i = 0; i < real.length(); i++) { 00400 y(i) = T(real(i), imag(i)); 00401 } 00402 return y; 00403 } 00404 00406 template<class T, class U> Mat<T> to(const Mat<U> &x) 00407 { 00408 Mat<T> y(x.rows(), x.cols()); 00409 for (int i = 0; i < x.rows(); i++) { 00410 for (int j = 0; j < x.cols(); j++) { 00411 y(i, j) = T(x(i, j)); 00412 } 00413 } 00414 return y; 00415 } 00417 template<> inline mat to<double>(const mat &x) {return x;} 00419 template<> inline cmat to<std::complex<double> >(const cmat &x) {return x;} 00421 template<> inline fixmat to<Fix>(const fixmat &x) {return x;} 00423 template<> inline cfixmat to<CFix>(const cfixmat &x) {return x;} 00424 00426 template<class T, class U> Mat<T> to(const Mat<U> &real, const Mat<U> &imag) 00427 { 00428 it_assert_debug(real.rows() == imag.rows() && real.cols() == imag.cols(), "to: real and imag should have the same size"); 00429 Mat<T> y(real.rows(), real.cols()); 00430 for (int i = 0; i < real.rows(); i++) { 00431 for (int j = 0; j < real.cols(); j++) { 00432 y(i, j) = T(real(i, j), imag(i, j)); 00433 } 00434 } 00435 return y; 00436 } 00437 00439 template<class T, class U> 00440 Array<typename ConvertU2T<T, U>::result> to(const Array<U> &x) 00441 { 00442 Array<typename ConvertU2T<T, U>::result> y(x.size()); 00443 for (int i = 0; i < x.size(); i++) { 00444 y(i) = to<T>(x(i)); 00445 } 00446 return y; 00447 } 00448 00450 template<class T, class U> 00451 Array<typename ConvertU2T<T, U>::result> to(const Array<U> &real, const Array<U> &imag) 00452 { 00453 it_assert_debug(real.size() == imag.size(), "to: real and imag should have the same size"); 00454 Array<typename ConvertU2T<T, U>::result> y(real.size()); 00455 for (int i = 0; i < real.size(); i++) { 00456 y(i) = to<T>(real(i), imag(i)); 00457 } 00458 return y; 00459 } 00460 00462 inline double unfix(const Fix &x) {return x.unfix();} 00464 inline std::complex<double> unfix(const CFix &x) {return x.unfix();} 00466 inline vec unfix(const fixvec &x) {return to_vec(x);} 00468 inline cvec unfix(const cfixvec &x) {return to_cvec(x);} 00470 inline mat unfix(const fixmat &x) {return to_mat(x);} 00472 inline cmat unfix(const cfixmat &x) {return to_cmat(x);} 00473 00475 inline double unfix(double x) {return x;} 00477 inline std::complex<double> unfix(const std::complex<double> &x) {return x;} 00479 inline vec unfix(const vec &x) {return x;} 00481 inline cvec unfix(const cvec &x) {return x;} 00483 inline mat unfix(const mat &x) {return x;} 00485 inline cmat unfix(const cmat &x) {return x;} 00486 00488 00490 template<class T> 00491 class Convert 00492 { 00493 public: 00494 typedef double to_double; 00495 }; 00497 template<> 00498 class Convert<CFix> 00499 { 00500 public: 00501 typedef std::complex<double> to_double; 00502 }; 00504 template<class T> 00505 class Convert<std::complex<T> > 00506 { 00507 public: 00508 typedef std::complex<double> to_double; 00509 }; 00511 template<class T> 00512 class Convert<Array<T> > 00513 { 00514 public: 00515 typedef Array<typename Convert<T>::to_double> to_double; // Recursive 00516 }; 00518 template<class T> 00519 class Convert<Vec<T> > 00520 { 00521 public: 00522 typedef Vec<typename Convert<T>::to_double> to_double; // Recursive 00523 }; 00525 template<class T> 00526 class Convert<Mat<T> > 00527 { 00528 public: 00529 typedef Mat<typename Convert<T>::to_double> to_double; // Recursive 00530 }; 00531 00533 00535 template<class T> 00536 Array<typename Convert<T>::to_double> unfix(const Array<T> &x) 00537 { 00538 Array<typename Convert<T>::to_double> y(x.size()); 00539 for (int i = 0; i < x.size(); i++) { 00540 y(i) = unfix(x(i)); 00541 } 00542 return y; 00543 } 00544 00546 Fix abs(const Fix &x); 00548 Fix real(const CFix &x); 00550 Fix imag(const CFix &x); 00552 CFix conj(const CFix &x); 00553 00555 00556 } // namespace itpp 00557 00558 #endif // #ifndef FIX_FUNCTIONS_H
Generated on Wed Jul 27 2011 16:27:05 for IT++ by Doxygen 1.7.4