00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef ROOT_TComplex
00013 #define ROOT_TComplex
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef ROOT_Riosfwd
00022 #include "Riosfwd.h"
00023 #endif
00024 #ifndef ROOT_TMath
00025 #include "TMath.h"
00026 #endif
00027
00028
00029 class TComplex {
00030
00031 protected:
00032 Double_t fRe;
00033 Double_t fIm;
00034
00035 public:
00036
00037 TComplex(): fRe(0), fIm(0) {}
00038 TComplex(Double_t re, Double_t im=0, Bool_t polar=kFALSE);
00039 virtual ~TComplex() {}
00040
00041
00042 static TComplex I() {return TComplex(0,1);}
00043 static TComplex One() {return TComplex(1,0);}
00044
00045
00046 Double_t Re() const {return fRe;}
00047 Double_t Im() const {return fIm;}
00048 Double_t Rho() const {return TMath::Sqrt(fRe*fRe+fIm*fIm);}
00049 Double_t Rho2() const {return fRe*fRe+fIm*fIm;}
00050 Double_t Theta() const {return (fIm||fRe)?TMath::ATan2(fIm,fRe):0;}
00051 TComplex operator()(Double_t x, Double_t y, Bool_t polar=kFALSE)
00052 { if (polar) { fRe = x*TMath::Cos(y); fIm = x*TMath::Sin(y); }
00053 else { fRe = x; fIm = y; } return *this; }
00054
00055
00056 TComplex operator *(const TComplex & c) const
00057 {return TComplex(fRe*c.fRe-fIm*c.fIm,fRe*c.fIm+fIm*c.fRe);}
00058 TComplex operator +(const TComplex & c) const
00059 {return TComplex(fRe+c.fRe, fIm+c.fIm);}
00060 TComplex operator /(const TComplex & c) const
00061 {return TComplex(fRe*c.fRe+fIm*c.fIm,-fRe*c.fIm+fIm*c.fRe)/c.Rho2();}
00062 TComplex operator -(const TComplex & c) const
00063 {return TComplex(fRe-c.fRe, fIm-c.fIm);}
00064
00065 TComplex operator *=(const TComplex & c)
00066 {return ((*this) = (*this) * c);}
00067 TComplex operator +=(const TComplex & c)
00068 {return ((*this) = (*this) + c);}
00069 TComplex operator /=(const TComplex & c)
00070 {return ((*this) = (*this) / c);}
00071 TComplex operator -=(const TComplex & c)
00072 {return ((*this) = (*this) - c);}
00073
00074 TComplex operator -()
00075 {return TComplex(-fRe,-fIm);}
00076 TComplex operator +()
00077 {return *this;}
00078
00079
00080 TComplex operator *(Double_t c) const
00081 {return TComplex(fRe*c,fIm*c);}
00082 TComplex operator +(Double_t c) const
00083 {return TComplex(fRe+c, fIm);}
00084 TComplex operator /(Double_t c) const
00085 {return TComplex(fRe/c,fIm/c);}
00086 TComplex operator -(Double_t c) const
00087 {return TComplex(fRe-c, fIm);}
00088
00089
00090 friend TComplex operator *(Double_t d, const TComplex & c)
00091 {return TComplex(d*c.fRe,d*c.fIm);}
00092 friend TComplex operator +(Double_t d, const TComplex & c)
00093 {return TComplex(d+c.fRe, c.fIm);}
00094 friend TComplex operator /(Double_t d, const TComplex & c)
00095 {return TComplex(d*c.fRe,-d*c.fIm)/c.Rho2();}
00096 friend TComplex operator -(Double_t d, const TComplex & c)
00097 {return TComplex(d-c.fRe, -c.fIm);}
00098
00099
00100 operator Double_t () const {return fRe;}
00101 operator Float_t () const {return static_cast<Float_t>(fRe);}
00102 operator Int_t () const {return static_cast<Int_t>(fRe);}
00103
00104
00105 static TComplex Sqrt(const TComplex &c)
00106 {return TComplex(TMath::Sqrt(c.Rho()),0.5*c.Theta(),kTRUE);}
00107
00108 static TComplex Exp(const TComplex &c)
00109 {return TComplex(TMath::Exp(c.fRe),c.fIm,kTRUE);}
00110 static TComplex Log(const TComplex &c)
00111 {return TComplex(0.5*TMath::Log(c.Rho2()),c.Theta());}
00112 static TComplex Log2(const TComplex &c)
00113 {return Log(c)/TMath::Log(2);}
00114 static TComplex Log10(const TComplex &c)
00115 {return Log(c)/TMath::Log(10);}
00116
00117 static TComplex Sin(const TComplex &c)
00118 {return TComplex(TMath::Sin(c.fRe)*TMath::CosH(c.fIm),
00119 TMath::Cos(c.fRe)*TMath::SinH(c.fIm));}
00120 static TComplex Cos(const TComplex &c)
00121 {return TComplex(TMath::Cos(c.fRe)*TMath::CosH(c.fIm),
00122 -TMath::Sin(c.fRe)*TMath::SinH(c.fIm));}
00123 static TComplex Tan(const TComplex &c)
00124 {TComplex cc=Cos(c); return Sin(c)*Conjugate(cc)/cc.Rho2();}
00125
00126 static TComplex ASin(const TComplex &c)
00127 {return -I()*Log(I()*c+TMath::Sign(1.,c.Im())*Sqrt(1.-c*c));}
00128 static TComplex ACos(const TComplex &c)
00129 {return -I()*Log(c+TMath::Sign(1.,c.Im())*Sqrt(c*c-1.));}
00130 static TComplex ATan(const TComplex &c)
00131 {return -0.5*I()*Log((1.+I()*c)/(1.-I()*c));}
00132
00133 static TComplex SinH(const TComplex &c)
00134 {return TComplex(TMath::SinH(c.fRe)*TMath::Cos(c.fIm),
00135 TMath::CosH(c.fRe)*TMath::Sin(c.fIm));}
00136 static TComplex CosH(const TComplex &c)
00137 {return TComplex(TMath::CosH(c.fRe)*TMath::Cos(c.fIm),
00138 TMath::SinH(c.fRe)*TMath::Sin(c.fIm));}
00139 static TComplex TanH(const TComplex &c)
00140 {TComplex cc=CosH(c); return SinH(c)*Conjugate(cc)/cc.Rho2();}
00141
00142 static TComplex ASinH(const TComplex &c)
00143 {return Log(c+TMath::Sign(1.,c.Im())*Sqrt(c*c+1.));}
00144 static TComplex ACosH(const TComplex &c)
00145 {return Log(c+TMath::Sign(1.,c.Im())*Sqrt(c*c-1.));}
00146 static TComplex ATanH(const TComplex &c)
00147 {return 0.5*Log((1.+c)/(1.-c));}
00148
00149 static Double_t Abs(const TComplex &c)
00150 {return c.Rho();}
00151
00152 static TComplex Power(const TComplex& x, const TComplex& y)
00153 {Double_t lrho=TMath::Log(x.Rho());
00154 Double_t theta=x.Theta();
00155 return TComplex(TMath::Exp(lrho*y.Re()-theta*y.Im()),
00156 lrho*y.Im()+theta*y.Re(),kTRUE);}
00157 static TComplex Power(const TComplex& x, Double_t y)
00158 {return TComplex(TMath::Power(x.Rho(),y),x.Theta()*y,kTRUE);}
00159 static TComplex Power(Double_t x, const TComplex& y)
00160 {Double_t lrho=TMath::Log(TMath::Abs(x));
00161 Double_t theta=(x>0)?0:TMath::Pi();
00162 return TComplex(TMath::Exp(lrho*y.Re()-theta*y.Im()),
00163 lrho*y.Im()+theta*y.Re(),kTRUE);}
00164 static TComplex Power(const TComplex& x, Int_t y)
00165 {return TComplex(TMath::Power(x.Rho(),y),x.Theta()*y,kTRUE);}
00166
00167 static Int_t Finite(const TComplex& c)
00168 {return TMath::Min(TMath::Finite(c.Re()),TMath::Finite(c.Im()));}
00169 static Int_t IsNaN(const TComplex& c)
00170 {return TMath::Max(TMath::IsNaN(c.Re()),TMath::IsNaN(c.Im()));}
00171
00172 static TComplex Min(const TComplex &a, const TComplex &b)
00173 {return a.Rho()<=b.Rho()?a:b;}
00174 static TComplex Max(const TComplex &a, const TComplex &b)
00175 {return a.Rho()>=b.Rho()?a:b;}
00176 static TComplex Normalize(const TComplex &c)
00177 {return TComplex(1.,c.Theta(),kTRUE);}
00178 static TComplex Conjugate(const TComplex &c)
00179 {return TComplex(c.Re(),-c.Im());}
00180 static TComplex Range(const TComplex &lb, const TComplex &ub, const TComplex &c)
00181 {return Max(lb,Min(c,ub));}
00182
00183
00184 friend ostream& operator<<(ostream& out, const TComplex& c);
00185 friend istream& operator>>(istream& in, TComplex& c);
00186
00187 ClassDef(TComplex,1)
00188 };
00189
00190 #endif