IT++ Logo Newcom Logo

fix_functions.h

Go to the documentation of this file.
00001 
00034 #ifndef FIX_FUNCTIONS_H
00035 #define FIX_FUNCTIONS_H
00036 
00037 #include <itpp/fixedpoint/cfix.h>
00038 #include <itpp/base/vec.h>
00039 #include <itpp/base/mat.h>
00040 #include <itpp/base/array.h>
00041 
00042 
00043 using std::complex;
00044 
00045 namespace itpp {
00046 
00049 
00051   template<class T> inline bool is_fix(const T &x) {return false;}
00053   template<> inline bool is_fix(const Fix &x) {return true;}
00055   template<> inline bool is_fix(const fixvec &x) {return true;}
00057   template<> inline bool is_fix(const fixmat &x) {return true;}
00059   template<> inline bool is_fix(const CFix &x) {return true;}
00061   template<> inline bool is_fix(const cfixvec &x) {return true;}
00063   template<> inline bool is_fix(const cfixmat &x) {return true;}
00065   template<class T> inline bool is_fix(const Array<T> &x) {return is_fix(T());}
00066 
00068   inline void set_fix(Fix &y, double x, int n) {y.set(x, n);}
00070   inline void set_fix(Fix &y, double x, int n, q_mode q) {y.set(x, n, q);}
00072   inline void set_fix(fixvec &y, const vec &x, int n)
00073   {
00074     y.set_size(x.length());
00075     for (int i=0; i<y.size(); i++) y(i).set(x(i), n);
00076   }
00078   inline void set_fix(fixvec &y, const vec &x, int n, q_mode q)
00079   {
00080     y.set_size(x.length());
00081     for (int i=0; i<y.size(); i++) y(i).set(x(i), n, q);
00082   }
00084   inline void set_fix(fixmat &y, const mat &x, int n)
00085   {
00086     y.set_size(x.rows(), x.cols());
00087     for (int i=0; i<y.size(); i++) y(i).set(x(i), n);
00088   }
00090   inline void set_fix(fixmat &y, const mat &x, int n, q_mode q)
00091   {
00092     y.set_size(x.rows(), x.cols());
00093     for (int i=0; i<y.size(); i++) y(i).set(x(i), n, q);
00094   }
00096   inline void set_fix(double &y, double x, int n) {y = x;}
00098   inline void set_fix(double &y, double x, int n, q_mode q) {y = x;}
00100   inline void set_fix(vec &y, const vec &x, int n) {y = x;}
00102   inline void set_fix(vec &y, const vec &x, int n, q_mode q) {y = x;}
00104   inline void set_fix(mat &y, const mat &x, int n) {y = x;}
00106   inline void set_fix(mat &y, const mat &x, int n, q_mode q) {y = x;}
00107 
00109   inline void set_fix(CFix &y, complex<double> x, int n) {y.set(x, n);}
00111   inline void set_fix(CFix &y, double real, double imag, int n) {y.set(real, imag, n);}
00113   inline void set_fix(CFix &y, complex<double> x, int n, q_mode q) {y.set(x, n, q);}
00115   inline void set_fix(CFix &y, double real, double imag, int n, q_mode q) {y.set(real, imag, n, q);}
00117   inline void set_fix(cfixvec &y, const cvec &x, int n)
00118   {
00119     y.set_size(x.length());
00120     for (int i=0; i<y.size(); i++) y(i).set(x(i), n);
00121   }
00123   inline void set_fix(cfixvec &y, const vec &real, const vec &imag, int n)
00124   {
00125     it_assert1(real.length()==imag.length(), "set_fix: real and imag should have the same size");
00126     y.set_size(real.length());
00127     for (int i=0; i<y.size(); i++) y(i).set(real(i), imag(i), n);
00128   }
00130   inline void set_fix(cfixvec &y, const cvec &x, int n, q_mode q)
00131   {
00132     y.set_size(x.length());
00133     for (int i=0; i<y.size(); i++) y(i).set(x(i), n, q);
00134   }
00136   inline void set_fix(cfixvec &y, const vec &real, const vec &imag, int n, q_mode q)
00137   {
00138     it_assert1(real.length()==imag.length(), "set_fix: real and imag should have the same size");
00139     y.set_size(real.length());
00140     for (int i=0; i<y.size(); i++) y(i).set(real(i), imag(i), n, q);
00141   }
00143   inline void set_fix(cfixmat &y, const cmat &x, int n)
00144   {
00145     y.set_size(x.rows(), x.cols());
00146     for (int i=0; i<y.size(); i++) y(i).set(x(i), n);
00147   }
00149   inline void set_fix(cfixmat &y, const mat &real, const mat &imag, int n)
00150   {
00151     it_assert1(real.rows()==imag.rows() && real.cols()==imag.cols(), "set_fix: real and imag should have the same size");
00152     y.set_size(real.rows(), real.cols());
00153     for (int i=0; i<y.size(); i++) y(i).set(real(i), imag(i), n);
00154   }
00156   inline void set_fix(cfixmat &y, const cmat &x, int n, q_mode q)
00157   {
00158     y.set_size(x.rows(), x.cols());
00159     for (int i=0; i<y.size(); i++) y(i).set(x(i), n, q);
00160   }
00162   inline void set_fix(cfixmat &y, const mat &real, const mat &imag, int n, q_mode q)
00163   {
00164     it_assert1(real.rows()==imag.rows() && real.cols()==imag.cols(), "set_fix: real and imag should have the same size");
00165     y.set_size(real.rows(), real.cols());
00166     for (int i=0; i<y.size(); i++) y(i).set(real(i), imag(i), n, q);
00167   }
00169   inline void set_fix(complex<double> &y, const complex<double> &x, int n) {y = x;}
00171   inline void set_fix(complex<double> &y, double real, double imag, int n) {y = complex<double>(real, imag);}
00173   inline void set_fix(complex<double> &y, const complex<double> &x, int n, q_mode q) {y = x;}
00175   inline void set_fix(complex<double> &y, double real, double imag, int n, q_mode q) {y = complex<double>(real, imag);}
00177   inline void set_fix(cvec &y, const cvec &x, int n) {y = x;}
00179   inline void set_fix(cvec &y, const vec &real, const vec &imag, int n) {y = to_cvec(real, imag);}
00181   inline void set_fix(cvec &y, const cvec &x, int n, q_mode q) {y = x;}
00183   inline void set_fix(cvec &y, const vec &real, const vec &imag, int n, q_mode q) {y = to_cvec(real, imag);}
00185   inline void set_fix(cmat &y, const cmat &x, int n) {y = x;}
00187   inline void set_fix(cmat &y, const mat &real, const mat &imag, int n) {y = to_cmat(real, imag);}
00189   inline void set_fix(cmat &y, const cmat &x, int n, q_mode q) {y = x;}
00191   inline void set_fix(cmat &y, const mat &real, const mat &imag, int n, q_mode q) {y = to_cmat(real, imag);}
00192 
00194   template<class T1, class T2> inline void set_fix(Array<T1> &y, const Array<T2> &x, int n)
00195   {
00196     y.set_size(x.size());
00197     for (int i=0; i<y.size(); i++) set_fix(y(i), x(i), n);
00198   }
00200   template<class T1, class T2> inline void set_fix(Array<T1> &y, const Array<T2> &real, const Array<T2> &imag, int n)
00201   {
00202     it_assert1(real.size()==imag.size(), "set_fix: real and imag should have the same size");
00203     y.set_size(real.size());
00204     for (int i=0; i<y.size(); i++) set_fix(y(i), real(i), imag(i), n);
00205   }
00207   template<class T1, class T2> inline void set_fix(Array<T1> &y, const Array<T2> &x, int n, q_mode q)
00208   {
00209     y.set_size(x.size());
00210     for (int i=0; i<y.size(); i++) set_fix(y(i), x(i), n, q);
00211   }
00213   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)
00214   {
00215     it_assert1(real.size()==imag.size(), "set_fix: real and imag should have the same size");
00216     y.set_size(real.size());
00217     for (int i=0; i<y.size(); i++) set_fix(y(i), real(i), imag(i), n, q);
00218   }
00219 
00221   inline void lshift_fix(Fix &y, int n) {y.lshift(n);}
00223   inline void rshift_fix(Fix &y, int n) {y.rshift(n);}
00225   inline void rshift_fix(Fix &y, int n, q_mode q) {y.rshift(n, q);}
00227   inline void lshift_fix(fixvec &y, int n)
00228   {for (int i=0; i<y.size(); i++) y(i).lshift(n);}
00230   inline void rshift_fix(fixvec &y, int n)
00231   {for (int i=0; i<y.size(); i++) y(i).rshift(n);}
00233   inline void rshift_fix(fixvec &y, int n, q_mode q)
00234   {for (int i=0; i<y.size(); i++) y(i).rshift(n, q);}
00236   inline void lshift_fix(fixmat &y, int n)
00237   {for (int i=0; i<y.size(); i++) y(i).lshift(n);}
00239   inline void rshift_fix(fixmat &y, int n)
00240   {for (int i=0; i<y.size(); i++) y(i).rshift(n);}
00242   inline void rshift_fix(fixmat &y, int n, q_mode q)
00243   {for (int i=0; i<y.size(); i++) y(i).rshift(n, q);}
00245   inline void lshift_fix(double &y, int n) {}
00247   inline void rshift_fix(double &y, int n) {}
00249   inline void rshift_fix(double &y, int n, q_mode q) {}
00251   inline void lshift_fix(vec &y, int n) {}
00253   inline void rshift_fix(vec &y, int n) {}
00255   inline void rshift_fix(vec &y, int n, q_mode q) {}
00257   inline void lshift_fix(mat &y, int n) {}
00259   inline void rshift_fix(mat &y, int n) {}
00261   inline void rshift_fix(mat &y, int n, q_mode q) {}
00263   inline void lshift_fix(CFix &y, int n) {y.lshift(n);}
00265   inline void rshift_fix(CFix &y, int n) {y.rshift(n);}
00267   inline void rshift_fix(CFix &y, int n, q_mode q) {y.rshift(n, q);}
00269   inline void lshift_fix(cfixvec &y, int n)
00270   {for (int i=0; i<y.size(); i++) y(i).lshift(n);}
00272   inline void rshift_fix(cfixvec &y, int n)
00273   {for (int i=0; i<y.size(); i++) y(i).rshift(n);}
00275   inline void rshift_fix(cfixvec &y, int n, q_mode q)
00276   {for (int i=0; i<y.size(); i++) y(i).rshift(n, q);}
00278   inline void lshift_fix(cfixmat &y, int n)
00279   {for (int i=0; i<y.size(); i++) y(i).lshift(n);}
00281   inline void rshift_fix(cfixmat &y, int n)
00282   {for (int i=0; i<y.size(); i++) y(i).rshift(n);}
00284   inline void rshift_fix(cfixmat &y, int n, q_mode q)
00285   {for (int i=0; i<y.size(); i++) y(i).rshift(n, q);}
00287   inline void lshift_fix(complex<double> &y, int n) {}
00289   inline void rshift_fix(complex<double> &y, int n) {}
00291   inline void rshift_fix(complex<double> &y, int n, q_mode q) {}
00293   inline void lshift_fix(cvec &y, int n) {}
00295   inline void rshift_fix(cvec &y, int n) {}
00297   inline void rshift_fix(cvec &y, int n, q_mode q) {}
00299   inline void lshift_fix(cmat &y, int n) {}
00301   inline void rshift_fix(cmat &y, int n) {}
00303   inline void rshift_fix(cmat &y, int n, q_mode q) {}
00305   template<class T> inline void lshift_fix(Array<T> &y, int n)
00306   {for (int i=0; i<y.size(); i++) lshift_fix(y(i), n);}
00308   template<class T> inline void rshift_fix(Array<T> &y, int n)
00309   {for (int i=0; i<y.size(); i++) rshift_fix(y(i), n);}
00311   template<class T> inline void rshift_fix(Array<T> &y, int n, q_mode q)
00312   {for (int i=0; i<y.size(); i++) rshift_fix(y(i), n, q);}
00313 
00315   inline void assert_fixshift(const double x, const int shift) {}
00317   inline void assert_fixshift(const complex<double> &x, const int shift) {}
00319   inline void assert_fixshift(const Fix &x, const int shift)
00320   {it_assert1(x.get_shift()==shift, "Shift should be " + to_str(shift) + " but it is " + to_str(x.get_shift()) + ".");}
00322   inline void assert_fixshift(const CFix &x, const int shift)
00323   {it_assert1(x.get_shift()==shift, "Shift should be " + to_str(shift) + " but it is " + to_str(x.get_shift()) + ".");}
00324 
00326   vec to_vec(const fixvec &v);
00328   cvec to_cvec(const cfixvec &v);
00330   mat to_mat(const fixmat &m);
00332   cmat to_cmat(const cfixmat &m);
00333 
00334 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00335 
00337   template<class T, class U>
00338   class ConvertU2T {
00339   public:
00340     typedef T result;
00341   };
00343   template<class T, class U>
00344   class ConvertU2T<T, Array<U> > {
00345   public:
00346     typedef Array<typename ConvertU2T<T, U>::result> result;  // Recursive
00347   };
00349   template<class T, class U>
00350   class ConvertU2T<T, Vec<U> > {
00351   public:
00352     typedef Vec<T> result;
00353   };
00355   template<class T, class U>
00356   class ConvertU2T<T, Mat<U> > {
00357   public:
00358     typedef Mat<T> result;
00359   };
00360 
00361 #endif
00362 
00364   template<class T> inline T to(const 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 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(const double real, const 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<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_assert1(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<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_assert1(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_assert1(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 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(const double &x) {return x;}
00477   inline complex<double> unfix(const 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 
00487 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00488 
00490   template<class T>
00491   class Convert {
00492   public:
00493     typedef double to_double;
00494   };
00496   template<>
00497   class Convert<CFix> {
00498   public:
00499     typedef complex<double> to_double;
00500   };
00502   template<class T>
00503   class Convert<complex<T> > {
00504   public:
00505     typedef complex<double> to_double;
00506   };
00508   template<class T>
00509   class Convert<Array<T> > {
00510   public:
00511     typedef Array<typename Convert<T>::to_double> to_double;  // Recursive
00512   };
00514   template<class T>
00515   class Convert<Vec<T> > {
00516   public:
00517     typedef Vec<typename Convert<T>::to_double> to_double;  // Recursive
00518   };
00520   template<class T>
00521   class Convert<Mat<T> > {
00522   public:
00523     typedef Mat<typename Convert<T>::to_double> to_double;  // Recursive
00524   };
00525 
00526 #endif //DOXYGEN_SHOULD_SKIP_THIS
00527 
00529   template<class T>
00530   Array<typename Convert<T>::to_double> unfix(const Array<T> &x)
00531   {
00532     Array<typename Convert<T>::to_double> y(x.size());
00533     for (int i=0; i<x.size(); i++) {
00534       y(i) = unfix(x(i));
00535     }
00536     return y;
00537   }
00538 
00540   Fix abs(const Fix &x);
00542   Fix real(const CFix &x);
00544   Fix imag(const CFix &x);
00546   CFix conj(const CFix &x);
00547 
00549 
00550 } // namespace itpp
00551 
00552 #endif // #ifndef FIX_FUNCTIONS_H
SourceForge Logo

Generated on Wed Apr 18 11:23:33 2007 for IT++ by Doxygen 1.5.2