IT++ Logo

fix_functions.h

Go to the documentation of this file.
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 
00045 
00047   template<class T> inline bool is_fix(const T &x) {return false;}
00049   template<> inline bool is_fix(const Fix &x) {return true;}
00051   template<> inline bool is_fix(const fixvec &x) {return true;}
00053   template<> inline bool is_fix(const fixmat &x) {return true;}
00055   template<> inline bool is_fix(const CFix &x) {return true;}
00057   template<> inline bool is_fix(const cfixvec &x) {return true;}
00059   template<> inline bool is_fix(const cfixmat &x) {return true;}
00061   template<class T> inline bool is_fix(const Array<T> &x) {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 n) {y = x;}
00094   inline void set_fix(double &y, double x, int n, q_mode q) {y = x;}
00096   inline void set_fix(vec &y, const vec &x, int n) {y = x;}
00098   inline void set_fix(vec &y, const vec &x, int n, q_mode q) {y = x;}
00100   inline void set_fix(mat &y, const mat &x, int n) {y = x;}
00102   inline void set_fix(mat &y, const mat &x, int n, q_mode q) {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 n) {y = x;}
00167   inline void set_fix(std::complex<double> &y, double real, double imag, int n) {y = std::complex<double>(real, imag);}
00169   inline void set_fix(std::complex<double> &y, const std::complex<double> &x, int n, q_mode q) {y = x;}
00171   inline void set_fix(std::complex<double> &y, double real, double imag, int n, q_mode q) {y = std::complex<double>(real, imag);}
00173   inline void set_fix(cvec &y, const cvec &x, int n) {y = x;}
00175   inline void set_fix(cvec &y, const vec &real, const vec &imag, int n) {y = to_cvec(real, imag);}
00177   inline void set_fix(cvec &y, const cvec &x, int n, q_mode q) {y = x;}
00179   inline void set_fix(cvec &y, const vec &real, const vec &imag, int n, q_mode q) {y = to_cvec(real, imag);}
00181   inline void set_fix(cmat &y, const cmat &x, int n) {y = x;}
00183   inline void set_fix(cmat &y, const mat &real, const mat &imag, int n) {y = to_cmat(real, imag);}
00185   inline void set_fix(cmat &y, const cmat &x, int n, q_mode q) {y = x;}
00187   inline void set_fix(cmat &y, const mat &real, const mat &imag, int n, q_mode q) {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 &y, int n) {}
00243   inline void rshift_fix(double &y, int n) {}
00245   inline void rshift_fix(double &y, int n, q_mode q) {}
00247   inline void lshift_fix(vec &y, int n) {}
00249   inline void rshift_fix(vec &y, int n) {}
00251   inline void rshift_fix(vec &y, int n, q_mode q) {}
00253   inline void lshift_fix(mat &y, int n) {}
00255   inline void rshift_fix(mat &y, int n) {}
00257   inline void rshift_fix(mat &y, int n, q_mode q) {}
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> &y, int n) {}
00285   inline void rshift_fix(std::complex<double> &y, int n) {}
00287   inline void rshift_fix(std::complex<double> &y, int n, q_mode q) {}
00289   inline void lshift_fix(cvec &y, int n) {}
00291   inline void rshift_fix(cvec &y, int n) {}
00293   inline void rshift_fix(cvec &y, int n, q_mode q) {}
00295   inline void lshift_fix(cmat &y, int n) {}
00297   inline void rshift_fix(cmat &y, int n) {}
00299   inline void rshift_fix(cmat &y, int n, q_mode q) {}
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(const double x, const int shift) {}
00313   inline void assert_fixshift(const std::complex<double> &x, const int shift) {}
00315   inline void assert_fixshift(const Fix &x, const 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, const 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   public:
00336     typedef T result;
00337   };
00339   template<class T, class U>
00340   class ConvertU2T<T, Array<U> > {
00341   public:
00342     typedef Array<typename ConvertU2T<T, U>::result> result;  // Recursive
00343   };
00345   template<class T, class U>
00346   class ConvertU2T<T, Vec<U> > {
00347   public:
00348     typedef Vec<T> result;
00349   };
00351   template<class T, class U>
00352   class ConvertU2T<T, Mat<U> > {
00353   public:
00354     typedef Mat<T> result;
00355   };
00356 
00358 
00360   template<class T> inline T to(const double x) {return T(x);}
00362   template<class T> inline T to(const Fix &x) {return T(x);}
00364   template<class T> inline T to(const std::complex<double> &x) {return T(x);}
00366   template<class T> inline T to(const CFix &x) {return T(x);}
00368   template<class T> inline T to(const double real, const double imag) {return T(real, imag);}
00370   template<class T> inline T to(const Fix &real, const Fix &imag) {return T(real, imag);}
00371 
00373   template<class T, class U> Vec<T> to(const Vec<U> &x)
00374   {
00375     Vec<T> y(x.length());
00376     for (int i=0; i<x.length(); i++) {
00377       y(i) = T(x(i));
00378     }
00379     return y;
00380   }
00382   template<> inline vec to<double>(const vec &x) {return x;}
00384   template<> inline cvec to<std::complex<double> >(const cvec &x) {return x;}
00386   template<> inline fixvec to<Fix>(const fixvec &x) {return x;}
00388   template<> inline cfixvec to<CFix>(const cfixvec &x) {return x;}
00389 
00391   template<class T, class U> Vec<T> to(const Vec<U> &real, const Vec<U> &imag)
00392   {
00393     it_assert_debug(real.length()==imag.length(), "to: real and imag should have the same size");
00394     Vec<T> y(real.length());
00395     for (int i=0; i<real.length(); i++) {
00396       y(i) = T(real(i), imag(i));
00397     }
00398     return y;
00399   }
00400 
00402   template<class T, class U> Mat<T> to(const Mat<U> &x)
00403   {
00404     Mat<T> y(x.rows(), x.cols());
00405     for (int i=0; i<x.rows(); i++) {
00406       for (int j=0; j<x.cols(); j++) {
00407         y(i,j) = T(x(i,j));
00408       }
00409     }
00410     return y;
00411   }
00413   template<> inline mat to<double>(const mat &x) {return x;}
00415   template<> inline cmat to<std::complex<double> >(const cmat &x) {return x;}
00417   template<> inline fixmat to<Fix>(const fixmat &x) {return x;}
00419   template<> inline cfixmat to<CFix>(const cfixmat &x) {return x;}
00420 
00422   template<class T, class U> Mat<T> to(const Mat<U> &real, const Mat<U> &imag)
00423   {
00424     it_assert_debug(real.rows()==imag.rows() && real.cols()==imag.cols(), "to: real and imag should have the same size");
00425     Mat<T> y(real.rows(), real.cols());
00426     for (int i=0; i<real.rows(); i++) {
00427       for (int j=0; j<real.cols(); j++) {
00428         y(i,j) = T(real(i,j), imag(i,j));
00429       }
00430     }
00431     return y;
00432   }
00433 
00435   template<class T, class U>
00436   Array<typename ConvertU2T<T, U>::result> to(const Array<U> &x)
00437   {
00438     Array<typename ConvertU2T<T, U>::result> y(x.size());
00439     for (int i=0; i<x.size(); i++) {
00440       y(i) = to<T>(x(i));
00441     }
00442     return y;
00443   }
00444 
00446   template<class T, class U>
00447   Array<typename ConvertU2T<T, U>::result> to(const Array<U> &real, const Array<U> &imag)
00448   {
00449     it_assert_debug(real.size()==imag.size(), "to: real and imag should have the same size");
00450     Array<typename ConvertU2T<T, U>::result> y(real.size());
00451     for (int i=0; i<real.size(); i++) {
00452       y(i) = to<T>(real(i), imag(i));
00453     }
00454     return y;
00455   }
00456 
00458   inline double unfix(const Fix &x) {return x.unfix();}
00460   inline std::complex<double> unfix(const CFix &x) {return x.unfix();}
00462   inline vec unfix(const fixvec &x) {return to_vec(x);}
00464   inline cvec unfix(const cfixvec &x) {return to_cvec(x);}
00466   inline mat unfix(const fixmat &x) {return to_mat(x);}
00468   inline cmat unfix(const cfixmat &x) {return to_cmat(x);}
00469 
00471   inline double unfix(const double &x) {return x;}
00473   inline std::complex<double> unfix(const std::complex<double> &x) {return x;}
00475   inline vec unfix(const vec &x) {return x;}
00477   inline cvec unfix(const cvec &x) {return x;}
00479   inline mat unfix(const mat &x) {return x;}
00481   inline cmat unfix(const cmat &x) {return x;}
00482 
00484 
00486   template<class T>
00487   class Convert {
00488   public:
00489     typedef double to_double;
00490   };
00492   template<>
00493   class Convert<CFix> {
00494   public:
00495     typedef std::complex<double> to_double;
00496   };
00498   template<class T>
00499   class Convert<std::complex<T> > {
00500   public:
00501     typedef std::complex<double> to_double;
00502   };
00504   template<class T>
00505   class Convert<Array<T> > {
00506   public:
00507     typedef Array<typename Convert<T>::to_double> to_double;  // Recursive
00508   };
00510   template<class T>
00511   class Convert<Vec<T> > {
00512   public:
00513     typedef Vec<typename Convert<T>::to_double> to_double;  // Recursive
00514   };
00516   template<class T>
00517   class Convert<Mat<T> > {
00518   public:
00519     typedef Mat<typename Convert<T>::to_double> to_double;  // Recursive
00520   };
00521 
00523 
00525   template<class T>
00526   Array<typename Convert<T>::to_double> unfix(const Array<T> &x)
00527   {
00528     Array<typename Convert<T>::to_double> y(x.size());
00529     for (int i=0; i<x.size(); i++) {
00530       y(i) = unfix(x(i));
00531     }
00532     return y;
00533   }
00534 
00536   Fix abs(const Fix &x);
00538   Fix real(const CFix &x);
00540   Fix imag(const CFix &x);
00542   CFix conj(const CFix &x);
00543 
00545 
00546 } // namespace itpp
00547 
00548 #endif // #ifndef FIX_FUNCTIONS_H
SourceForge Logo

Generated on Mon Jan 7 22:28:58 2008 for IT++ by Doxygen 1.5.4