1 //--------------------------------------------------------------------------
4 // This software is part of the EvtGen package developed jointly
5 // for the BaBar and CLEO collaborations. If you use all or part
6 // of it, please give an appropriate acknowledgement.
8 // Copyright Information: See EvtGen/COPYRIGHT
9 // Copyright (C) 1998 Caltech, UCSB
11 // Module: EvtGen/EvtVector4R.hh
13 // Description: Class to describe complex numbers
15 // Modification history:
17 // RYD December 5, 1998 Created
19 //------------------------------------------------------------------------
26 #include "EvtGenBase/EvtConst.hh"
30 inline friend EvtComplex operator*(double d,const EvtComplex& c);
31 inline friend EvtComplex operator*(const EvtComplex& c,double d);
32 inline friend EvtComplex operator/(const EvtComplex& c,double d);
33 inline friend EvtComplex operator/(double d,const EvtComplex& c);
34 inline friend EvtComplex operator*(const EvtComplex& c1,const EvtComplex& c2);
35 inline friend EvtComplex operator/(const EvtComplex& c1,const EvtComplex& c2);
36 inline friend EvtComplex operator+(const EvtComplex& c1,const EvtComplex& c2);
37 inline friend EvtComplex operator-(const EvtComplex& c1,const EvtComplex& c2);
38 inline friend EvtComplex operator-(const EvtComplex& c);
39 inline friend EvtComplex conj(const EvtComplex& c);
40 inline friend double abs(const EvtComplex& c);
41 inline friend double abs2(const EvtComplex& c);
42 inline friend double arg(const EvtComplex& c);
43 inline friend double real(const EvtComplex& c);
44 inline friend double imag(const EvtComplex& c);
45 inline friend EvtComplex exp(const EvtComplex& c);
46 friend std::ostream& operator<<(std::ostream& s, const EvtComplex& c);
49 EvtComplex():_rpart(0.0),_ipart(0.0){}
50 EvtComplex(double rpart,double ipart=0.0):_rpart(rpart),_ipart(ipart){}
51 EvtComplex(const EvtComplex& c):_rpart(c._rpart),_ipart(c._ipart){}
52 inline EvtComplex& operator*=(double d);
53 inline EvtComplex& operator/=(double d);
54 EvtComplex& operator*=(EvtComplex c);
55 EvtComplex& operator/=(EvtComplex c);
56 inline EvtComplex& operator=(const EvtComplex& c);
57 inline EvtComplex& operator+=(const EvtComplex& c);
58 inline EvtComplex& operator-=(const EvtComplex& c);
59 inline EvtComplex& operator+=(double d);
60 inline EvtComplex& operator-=(double d);
61 inline int operator==(const EvtComplex c);
62 inline int operator!=(const EvtComplex c);
68 typedef EvtComplex* EvtComplexPtr;
69 typedef EvtComplexPtr* EvtComplexPtrPtr;
70 typedef EvtComplexPtrPtr* EvtComplexPtrPtrPtr;
73 EvtComplex& EvtComplex::operator=(const EvtComplex& c){
81 EvtComplex& EvtComplex::operator+=(const EvtComplex& c){
89 EvtComplex& EvtComplex::operator-=(const EvtComplex& c){
97 EvtComplex& EvtComplex::operator+=(double d){
104 EvtComplex& EvtComplex::operator-=(double d){
111 EvtComplex operator*(double d,const EvtComplex& c){
113 return EvtComplex(c._rpart*d,c._ipart*d);
117 EvtComplex operator*(const EvtComplex& c, double d){
119 return EvtComplex(c._rpart*d,c._ipart*d);
125 EvtComplex operator/(const EvtComplex& c,double d){
127 return EvtComplex(c._rpart/d,c._ipart/d);
131 EvtComplex& EvtComplex::operator*=(double d){
141 EvtComplex& EvtComplex::operator/=(double d){
149 EvtComplex operator/(double d,const EvtComplex& c) {
151 double Num=d/(c._rpart*c._rpart+c._ipart*c._ipart);
153 return EvtComplex( Num*c._rpart, -Num*c._ipart );
159 EvtComplex operator/(const EvtComplex& c1,const EvtComplex& c2){
161 double inv=1.0/(c2._rpart*c2._rpart+c2._ipart*c2._ipart);
163 return EvtComplex(inv*(c1._rpart*c2._rpart+c1._ipart*c2._ipart),
164 inv*(c1._ipart*c2._rpart-c1._rpart*c2._ipart));
168 EvtComplex operator*(const EvtComplex& c1,const EvtComplex& c2){
170 return EvtComplex(c1._rpart*c2._rpart-c1._ipart*c2._ipart,
171 c1._rpart*c2._ipart+c1._ipart*c2._rpart);
175 EvtComplex operator-(const EvtComplex& c1,const EvtComplex& c2){
177 return EvtComplex(c1._rpart-c2._rpart,c1._ipart-c2._ipart);
181 EvtComplex operator+(const EvtComplex& c1,const EvtComplex& c2){
183 return EvtComplex(c1._rpart+c2._rpart,c1._ipart+c2._ipart);
187 int EvtComplex::operator==(const EvtComplex c){
189 return _rpart==c._rpart&&_ipart==c._ipart;
193 int EvtComplex::operator!=(const EvtComplex c){
195 return _rpart!=c._rpart||_ipart!=c._ipart;
200 EvtComplex operator-(const EvtComplex& c){
202 return EvtComplex(-c._rpart,-c._ipart);
206 EvtComplex conj(const EvtComplex& c){
208 return EvtComplex(c._rpart,-c._ipart);
212 double abs(const EvtComplex& c){
214 double c2=c._rpart*c._rpart+c._ipart*c._ipart;
215 if (c2<=0.0) return 0.0;
221 double abs2(const EvtComplex& c){
223 return c._rpart*c._rpart+c._ipart*c._ipart;
227 double arg(const EvtComplex& c){
228 if ((c._rpart==0)&&(c._ipart==0)) {return 0.0;}
231 return EvtConst::pi/2;
233 return -EvtConst::pi/2;
236 return atan2(c._ipart,c._rpart);
240 double real(const EvtComplex& c){
246 double imag(const EvtComplex& c){
252 EvtComplex exp(const EvtComplex& c){
254 return exp(c._rpart)*EvtComplex(cos(c._ipart),sin(c._ipart));