00001
00029 #ifndef CFIX_H
00030 #define CFIX_H
00031
00032 #include <itpp/fixed/fix.h>
00033
00034
00035 namespace itpp
00036 {
00037
00038
00039 template<class Num_T> class Vec;
00040 template<class Num_T> class Mat;
00041
00044
00050 class CFix : public Fix_Base
00051 {
00052 template<int, e_mode, o_mode, q_mode> friend class CFixed;
00053 public:
00055 CFix(double r = 0.0, double i = 0.0, int s = 0, int w = MAX_WORDLEN, e_mode e = TC, o_mode o = WRAP, q_mode q = TRN, Stat *ptr = 0)
00056 : Fix_Base(s, w, e, o, q, ptr), re(scale_and_apply_modes(r)), im(scale_and_apply_modes(i)) {}
00058 CFix(std::complex<double> x, double, int s = 0, int w = MAX_WORDLEN, e_mode e = TC, o_mode o = WRAP, q_mode q = TRN, Stat *ptr = 0)
00059 : Fix_Base(s, w, e, o, q, ptr), re(scale_and_apply_modes(std::real(x))), im(scale_and_apply_modes(std::imag(x))) {}
00061 explicit CFix(const Fix_Factory &f)
00062 : Fix_Base(0, f.wordlen, f.emode, f.omode, f.qmode, f.stat_ptr), re(0), im(0) {}
00064 CFix(fixrep r, fixrep i, int s, int, int)
00065 : Fix_Base(s), re(r), im(i) {}
00067 CFix(const Fix &r, const Fix &i = 0.0, int w = MAX_WORDLEN, e_mode e = TC, o_mode o = WRAP, q_mode q = TRN, Stat *ptr = 0)
00068 : Fix_Base(assert_shifts(r, i), w, e, o, q, ptr), re(r.re), im(i.re) {}
00070 CFix(const CFix &x, double, int w = MAX_WORDLEN, e_mode e = TC, o_mode o = WRAP, q_mode q = TRN, Stat *ptr = 0)
00071 : Fix_Base(x.shift, w, e, o, q, ptr), re(x.re), im(x.im) {}
00073 virtual ~CFix() {}
00074
00076 CFix& operator=(const CFix &x);
00078 CFix& operator=(const Fix &x);
00080 CFix& operator=(const std::complex<double> &x);
00082 CFix& operator=(const int x);
00084 CFix& operator+=(const CFix &x);
00086 CFix& operator+=(const Fix &x);
00088 CFix& operator+=(const int x);
00090 CFix& operator-=(const CFix &x);
00092 CFix& operator-=(const Fix &x);
00094 CFix& operator-=(const int x);
00096 CFix& operator*=(const CFix &x);
00098 CFix& operator*=(const Fix &x);
00100 CFix& operator*=(const int x);
00102 CFix& operator/=(const CFix &x);
00104 CFix& operator/=(const Fix &x);
00106 CFix& operator/=(const int x);
00108 CFix operator-() const;
00110 CFix& operator<<=(const int n);
00112 CFix& operator>>=(const int n);
00113
00115 void set(double real, double imag, int n);
00117 void set(double real, double imag, int n, q_mode q);
00119 void set(const std::complex<double> &x, int n);
00121 void set(const std::complex<double> &x, int n, q_mode q);
00123 void set_re(fixrep x) {re = apply_o_mode(x);}
00125 void set_im(fixrep x) {im = apply_o_mode(x);}
00126
00128 void lshift(int n);
00130 void rshift(int n);
00132 void rshift(int n, q_mode q);
00133
00135 virtual void print() const;
00137 fixrep get_re() const {return re;}
00139 fixrep get_im() const {return im;}
00141 std::complex<double> unfix() const;
00142
00143 #ifndef NO_IMPLICIT_FIX_CONVERSION
00145 operator std::complex<double>() const {
00146 it_assert_debug(shift >= -63 && shift <= 64, "CFix::operator complex<double>: Illegal shift!");
00147 return std::complex<double>(double(re)*DOUBLE_POW2[64 - shift],
00148 double(im)*DOUBLE_POW2[64 - shift]);
00149 }
00150 #endif
00151
00153 friend int assert_shifts(const CFix &x, const CFix &y);
00155 friend int assert_shifts(const CFix &x, const Fix &y);
00157 friend int assert_shifts(const CFix &x, int y);
00158
00159 protected:
00160 fixrep re;
00161 fixrep im;
00162 };
00163
00165 int assert_shifts(const CFix &x, const CFix &y);
00167 int assert_shifts(const CFix &x, const Fix &y);
00169 int assert_shifts(const CFix &x, int y);
00170
00172 std::istream &operator>>(std::istream &is, CFix &x);
00174 std::ostream &operator<<(std::ostream &os, const CFix &x);
00175
00177 typedef Vec<CFix> cfixvec;
00179 typedef Mat<CFix> cfixmat;
00180
00181
00182 template<> void cfixvec::set(const char *values);
00183
00184 template<> void cfixmat::set(const char *values);
00185
00187
00188 }
00189
00190 #endif // #ifndef CFIX_H