Line data Source code
1 : //--------------------------------------------------------------------------
2 : //
3 : // Environment:
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.
7 : //
8 : // Copyright Information: See EvtGen/COPYRIGHT
9 : // Copyright (C) 1998 Caltech, UCSB
10 : //
11 : // Module: EvtGen/EvtVector4R.hh
12 : //
13 : // Description: Class to describe complex numbers
14 : //
15 : // Modification history:
16 : //
17 : // RYD December 5, 1998 Created
18 : //
19 : //------------------------------------------------------------------------
20 :
21 : #ifndef EVTCOMPLEX_HH
22 : #define EVTCOMPLEX_HH
23 :
24 : #include <iostream>
25 : #include <math.h>
26 : #include "EvtGenBase/EvtConst.hh"
27 :
28 : class EvtComplex {
29 :
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);
47 :
48 : public:
49 0 : EvtComplex():_rpart(0.0),_ipart(0.0){}
50 192 : EvtComplex(double rpart,double ipart=0.0):_rpart(rpart),_ipart(ipart){}
51 0 : 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);
63 : private:
64 : double _rpart,_ipart;
65 : };
66 :
67 :
68 : typedef EvtComplex* EvtComplexPtr;
69 : typedef EvtComplexPtr* EvtComplexPtrPtr;
70 : typedef EvtComplexPtrPtr* EvtComplexPtrPtrPtr;
71 :
72 :
73 : EvtComplex& EvtComplex::operator=(const EvtComplex& c){
74 :
75 0 : _rpart=c._rpart;
76 0 : _ipart=c._ipart;
77 :
78 0 : return *this;
79 : }
80 :
81 : EvtComplex& EvtComplex::operator+=(const EvtComplex& c){
82 :
83 0 : _rpart+=c._rpart;
84 0 : _ipart+=c._ipart;
85 :
86 0 : return *this;
87 : }
88 :
89 : EvtComplex& EvtComplex::operator-=(const EvtComplex& c){
90 :
91 0 : _rpart-=c._rpart;
92 0 : _ipart-=c._ipart;
93 :
94 0 : return *this;
95 : }
96 :
97 : EvtComplex& EvtComplex::operator+=(double d){
98 :
99 0 : _rpart+=d;
100 :
101 0 : return *this;
102 : }
103 :
104 : EvtComplex& EvtComplex::operator-=(double d){
105 :
106 : _rpart-=d;
107 :
108 : return *this;
109 : }
110 :
111 : EvtComplex operator*(double d,const EvtComplex& c){
112 :
113 6 : return EvtComplex(c._rpart*d,c._ipart*d);
114 :
115 : }
116 :
117 : EvtComplex operator*(const EvtComplex& c, double d){
118 :
119 0 : return EvtComplex(c._rpart*d,c._ipart*d);
120 :
121 : }
122 :
123 :
124 :
125 : EvtComplex operator/(const EvtComplex& c,double d){
126 :
127 0 : return EvtComplex(c._rpart/d,c._ipart/d);
128 :
129 : }
130 :
131 : EvtComplex& EvtComplex::operator*=(double d){
132 :
133 0 : _rpart*=d;
134 0 : _ipart*=d;
135 :
136 0 : return *this;
137 :
138 : }
139 :
140 :
141 : EvtComplex& EvtComplex::operator/=(double d){
142 :
143 0 : _rpart/=d;
144 0 : _ipart/=d;
145 :
146 0 : return *this;
147 : }
148 :
149 : EvtComplex operator/(double d,const EvtComplex& c) {
150 :
151 0 : double Num=d/(c._rpart*c._rpart+c._ipart*c._ipart);
152 :
153 0 : return EvtComplex( Num*c._rpart, -Num*c._ipart );
154 :
155 0 : }
156 :
157 :
158 :
159 : EvtComplex operator/(const EvtComplex& c1,const EvtComplex& c2){
160 :
161 0 : double inv=1.0/(c2._rpart*c2._rpart+c2._ipart*c2._ipart);
162 :
163 0 : return EvtComplex(inv*(c1._rpart*c2._rpart+c1._ipart*c2._ipart),
164 0 : inv*(c1._ipart*c2._rpart-c1._rpart*c2._ipart));
165 :
166 0 : }
167 :
168 : EvtComplex operator*(const EvtComplex& c1,const EvtComplex& c2){
169 :
170 54 : return EvtComplex(c1._rpart*c2._rpart-c1._ipart*c2._ipart,
171 18 : c1._rpart*c2._ipart+c1._ipart*c2._rpart);
172 :
173 : }
174 :
175 : EvtComplex operator-(const EvtComplex& c1,const EvtComplex& c2){
176 :
177 6 : return EvtComplex(c1._rpart-c2._rpart,c1._ipart-c2._ipart);
178 :
179 : }
180 :
181 : EvtComplex operator+(const EvtComplex& c1,const EvtComplex& c2){
182 :
183 0 : return EvtComplex(c1._rpart+c2._rpart,c1._ipart+c2._ipart);
184 :
185 : }
186 :
187 : int EvtComplex::operator==(const EvtComplex c){
188 :
189 0 : return _rpart==c._rpart&&_ipart==c._ipart;
190 :
191 : }
192 :
193 : int EvtComplex::operator!=(const EvtComplex c){
194 :
195 0 : return _rpart!=c._rpart||_ipart!=c._ipart;
196 :
197 : }
198 :
199 :
200 : EvtComplex operator-(const EvtComplex& c){
201 :
202 0 : return EvtComplex(-c._rpart,-c._ipart);
203 :
204 : }
205 :
206 : EvtComplex conj(const EvtComplex& c){
207 :
208 0 : return EvtComplex(c._rpart,-c._ipart);
209 :
210 : }
211 :
212 : double abs(const EvtComplex& c){
213 :
214 0 : double c2=c._rpart*c._rpart+c._ipart*c._ipart;
215 0 : if (c2<=0.0) return 0.0;
216 0 : return sqrt(c2);
217 :
218 0 : }
219 :
220 :
221 : double abs2(const EvtComplex& c){
222 :
223 0 : return c._rpart*c._rpart+c._ipart*c._ipart;
224 : }
225 :
226 :
227 : double arg(const EvtComplex& c){
228 0 : if ((c._rpart==0)&&(c._ipart==0)) {return 0.0;}
229 0 : if (c._rpart==0){
230 0 : if (c._ipart>0){
231 0 : return EvtConst::pi/2;
232 : } else {
233 0 : return -EvtConst::pi/2;
234 : }
235 : } else {
236 0 : return atan2(c._ipart,c._rpart);
237 : }
238 0 : }
239 :
240 : double real(const EvtComplex& c){
241 :
242 0 : return c._rpart;
243 :
244 : }
245 :
246 : double imag(const EvtComplex& c){
247 :
248 0 : return c._ipart;
249 :
250 : }
251 :
252 : EvtComplex exp(const EvtComplex& c){
253 :
254 0 : return exp(c._rpart)*EvtComplex(cos(c._ipart),sin(c._ipart));
255 :
256 : }
257 :
258 :
259 :
260 : #endif
261 :
262 :
263 :
264 :
265 :
266 :
267 :
|