evtgen is hosted by Hepforge, IPPP Durham
EvtGen  2.0.0
Monte Carlo generator of particle decays, in particular the weak decays of heavy flavour particles such as B mesons.
EvtComplex.hh
Go to the documentation of this file.
1 
2 /***********************************************************************
3 * Copyright 1998-2020 CERN for the benefit of the EvtGen authors *
4 * *
5 * This file is part of EvtGen. *
6 * *
7 * EvtGen is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 3 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * EvtGen is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with EvtGen. If not, see <https://www.gnu.org/licenses/>. *
19 ***********************************************************************/
20 
21 #ifndef EVTCOMPLEX_HH
22 #define EVTCOMPLEX_HH
23 
24 #include "EvtGenBase/EvtConst.hh"
25 
26 #include <iostream>
27 #include <math.h>
28 
29 class EvtComplex {
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,
35  const EvtComplex& c2 );
36  inline friend EvtComplex operator/( const EvtComplex& c1,
37  const EvtComplex& c2 );
38  inline friend EvtComplex operator+( const EvtComplex& c1,
39  const EvtComplex& c2 );
40  inline friend EvtComplex operator-( const EvtComplex& c1,
41  const EvtComplex& c2 );
42  inline friend EvtComplex operator-( const EvtComplex& c );
43  inline friend EvtComplex conj( const EvtComplex& c );
44  inline friend double abs( const EvtComplex& c );
45  inline friend double abs2( const EvtComplex& c );
46  inline friend double arg( const EvtComplex& c );
47  inline friend double real( const EvtComplex& c );
48  inline friend double imag( const EvtComplex& c );
49  inline friend EvtComplex exp( const EvtComplex& c );
50  friend std::ostream& operator<<( std::ostream& s, const EvtComplex& c );
51 
52  public:
53  EvtComplex() : _rpart( 0.0 ), _ipart( 0.0 ) {}
54  EvtComplex( double rpart, double ipart = 0.0 ) :
55  _rpart( rpart ), _ipart( ipart )
56  {
57  }
58  EvtComplex( const EvtComplex& c ) : _rpart( c._rpart ), _ipart( c._ipart )
59  {
60  }
61  inline EvtComplex& operator*=( double d );
62  inline EvtComplex& operator/=( double d );
65  inline EvtComplex& operator=( const EvtComplex& c );
66  inline EvtComplex& operator+=( const EvtComplex& c );
67  inline EvtComplex& operator-=( const EvtComplex& c );
68  inline EvtComplex& operator+=( double d );
69  inline EvtComplex& operator-=( double d );
70  inline int operator==( const EvtComplex c );
71  inline int operator!=( const EvtComplex c );
72 
73  private:
74  double _rpart, _ipart;
75 };
76 
80 
82 {
83  _rpart = c._rpart;
84  _ipart = c._ipart;
85 
86  return *this;
87 }
88 
90 {
91  _rpart += c._rpart;
92  _ipart += c._ipart;
93 
94  return *this;
95 }
96 
98 {
99  _rpart -= c._rpart;
100  _ipart -= c._ipart;
101 
102  return *this;
103 }
104 
106 {
107  _rpart += d;
108 
109  return *this;
110 }
111 
113 {
114  _rpart -= d;
115 
116  return *this;
117 }
118 
119 EvtComplex operator*( double d, const EvtComplex& c )
120 {
121  return EvtComplex( c._rpart * d, c._ipart * d );
122 }
123 
124 EvtComplex operator*( const EvtComplex& c, double d )
125 {
126  return EvtComplex( c._rpart * d, c._ipart * d );
127 }
128 
129 EvtComplex operator/( const EvtComplex& c, double d )
130 {
131  return EvtComplex( c._rpart / d, c._ipart / d );
132 }
133 
135 {
136  _rpart *= d;
137  _ipart *= d;
138 
139  return *this;
140 }
141 
143 {
144  _rpart /= d;
145  _ipart /= d;
146 
147  return *this;
148 }
149 
150 EvtComplex operator/( double d, const EvtComplex& c )
151 {
152  double Num = d / ( c._rpart * c._rpart + c._ipart * c._ipart );
153 
154  return EvtComplex( Num * c._rpart, -Num * c._ipart );
155 }
156 
157 EvtComplex operator/( const EvtComplex& c1, const EvtComplex& c2 )
158 {
159  double inv = 1.0 / ( c2._rpart * c2._rpart + c2._ipart * c2._ipart );
160 
161  return EvtComplex( inv * ( c1._rpart * c2._rpart + c1._ipart * c2._ipart ),
162  inv * ( c1._ipart * c2._rpart - c1._rpart * c2._ipart ) );
163 }
164 
165 EvtComplex operator*( const EvtComplex& c1, const EvtComplex& c2 )
166 {
167  return EvtComplex( c1._rpart * c2._rpart - c1._ipart * c2._ipart,
168  c1._rpart * c2._ipart + c1._ipart * c2._rpart );
169 }
170 
171 EvtComplex operator-( const EvtComplex& c1, const EvtComplex& c2 )
172 {
173  return EvtComplex( c1._rpart - c2._rpart, c1._ipart - c2._ipart );
174 }
175 
176 EvtComplex operator+( const EvtComplex& c1, const EvtComplex& c2 )
177 {
178  return EvtComplex( c1._rpart + c2._rpart, c1._ipart + c2._ipart );
179 }
180 
182 {
183  return _rpart == c._rpart && _ipart == c._ipart;
184 }
185 
187 {
188  return _rpart != c._rpart || _ipart != c._ipart;
189 }
190 
192 {
193  return EvtComplex( -c._rpart, -c._ipart );
194 }
195 
197 {
198  return EvtComplex( c._rpart, -c._ipart );
199 }
200 
201 double abs( const EvtComplex& c )
202 {
203  double c2 = c._rpart * c._rpart + c._ipart * c._ipart;
204  if ( c2 <= 0.0 )
205  return 0.0;
206  return sqrt( c2 );
207 }
208 
209 double abs2( const EvtComplex& c )
210 {
211  return c._rpart * c._rpart + c._ipart * c._ipart;
212 }
213 
214 double arg( const EvtComplex& c )
215 {
216  if ( ( c._rpart == 0 ) && ( c._ipart == 0 ) ) {
217  return 0.0;
218  }
219  if ( c._rpart == 0 ) {
220  if ( c._ipart > 0 ) {
221  return EvtConst::pi / 2;
222  } else {
223  return -EvtConst::pi / 2;
224  }
225  } else {
226  return atan2( c._ipart, c._rpart );
227  }
228 }
229 
230 double real( const EvtComplex& c )
231 {
232  return c._rpart;
233 }
234 
235 double imag( const EvtComplex& c )
236 {
237  return c._ipart;
238 }
239 
241 {
242  return exp( c._rpart ) * EvtComplex( cos( c._ipart ), sin( c._ipart ) );
243 }
244 
245 #endif
friend EvtComplex exp(const EvtComplex &c)
Definition: EvtComplex.hh:240
friend EvtComplex operator+(const EvtComplex &c1, const EvtComplex &c2)
Definition: EvtComplex.hh:176
friend double arg(const EvtComplex &c)
Definition: EvtComplex.hh:214
EvtComplex & operator+=(const EvtComplex &c)
Definition: EvtComplex.hh:89
EvtComplex(const EvtComplex &c)
Definition: EvtComplex.hh:58
friend double imag(const EvtComplex &c)
Definition: EvtComplex.hh:235
double abs2(const EvtComplex &c)
Definition: EvtComplex.hh:209
EvtComplex operator *(double d, const EvtComplex &c)
Definition: EvtComplex.hh:119
int operator!=(const EvtComplex c)
Definition: EvtComplex.hh:186
EvtComplex conj(const EvtComplex &c)
Definition: EvtComplex.hh:196
EvtComplex & operator/=(double d)
Definition: EvtComplex.hh:142
static const double pi
Definition: EvtConst.hh:26
EvtComplex * EvtComplexPtr
Definition: EvtComplex.hh:77
double _rpart
Definition: EvtComplex.hh:74
friend double abs(const EvtComplex &c)
Definition: EvtComplex.hh:201
double abs(const EvtComplex &c)
Definition: EvtComplex.hh:201
double imag(const EvtComplex &c)
Definition: EvtComplex.hh:235
double _ipart
Definition: EvtComplex.hh:74
friend EvtComplex operator *(double d, const EvtComplex &c)
Definition: EvtComplex.hh:119
friend EvtComplex conj(const EvtComplex &c)
Definition: EvtComplex.hh:196
EvtComplex exp(const EvtComplex &c)
Definition: EvtComplex.hh:240
EvtComplexPtr * EvtComplexPtrPtr
Definition: EvtComplex.hh:78
EvtComplex operator-(const EvtComplex &c1, const EvtComplex &c2)
Definition: EvtComplex.hh:171
friend double real(const EvtComplex &c)
Definition: EvtComplex.hh:230
EvtComplex(double rpart, double ipart=0.0)
Definition: EvtComplex.hh:54
EvtComplex & operator=(const EvtComplex &c)
Definition: EvtComplex.hh:81
friend std::ostream & operator<<(std::ostream &s, const EvtComplex &c)
int operator==(const EvtComplex c)
Definition: EvtComplex.hh:181
friend EvtComplex operator-(const EvtComplex &c1, const EvtComplex &c2)
Definition: EvtComplex.hh:171
EvtComplex operator/(const EvtComplex &c, double d)
Definition: EvtComplex.hh:129
friend EvtComplex operator/(const EvtComplex &c, double d)
Definition: EvtComplex.hh:129
double arg(const EvtComplex &c)
Definition: EvtComplex.hh:214
EvtComplex & operator-=(const EvtComplex &c)
Definition: EvtComplex.hh:97
EvtComplex & operator *=(double d)
Definition: EvtComplex.hh:134
double real(const EvtComplex &c)
Definition: EvtComplex.hh:230
EvtComplex operator+(const EvtComplex &c1, const EvtComplex &c2)
Definition: EvtComplex.hh:176
friend double abs2(const EvtComplex &c)
Definition: EvtComplex.hh:209
EvtComplexPtrPtr * EvtComplexPtrPtrPtr
Definition: EvtComplex.hh:79