Diana Software
QComplex.cc
Go to the documentation of this file.
1 /*
2 *
3 * Class QComplex
4 *
5 */
6 
7 #include "QComplex.hh"
8 #include "QVectorC.hh"
9 #include <math.h>
10 #include <iostream>
11 #include <gsl/gsl_complex_math.h>
12 
13 QObjectImp(Diana::QComplex);
14 
15 
17 
19  : fOwner(true),
20  fSize(2),
21  fData(NULL)
22 {
23  fData = new double[2];
24 }
25 QComplex::QComplex(const double Re, const double Im)
26  : fOwner(true),
27  fSize(2),
28  fData(NULL)
29 {
30  fData=new double[2];
31  fData[0]=Re;
32  fData[1]=Im;
33 }
34 QComplex::QComplex(double* other)
35  : fOwner(false),
36  fSize(2),
37  fData(NULL)
38 {
39  fData = other;
40 }
41 QComplex::QComplex(gsl_complex other)
42  : fOwner(true),
43  fSize(2),
44  fData(NULL)
45 {
46  fData=new double[2];
47  fData[0]=other.dat[0];
48  fData[1]=other.dat[1];
49 }
50 
51 QComplex::QComplex(gsl_complex* other)
52  : fOwner(false),
53  fSize(2),
54  fData(NULL)
55 {
56  fData = other->dat;
57 }
59  : fOwner(true),
60  fSize(2),
61  fData(NULL)
62 {
63  fData=new double[2];
64  fData[0]=orig.fData[0];
65  fData[1]=orig.fData[1];
66 }
68 {
69  if(fOwner && fData) delete [] fData;
70  fData=NULL;
71 }
72 double QComplex::Re() const { return fData[0]; }
73 double QComplex::Im() const { return fData[1]; }
74 
75 void QComplex::SetRe(const double Re) { fData[0]=Re; }
76 void QComplex::SetIm(const double Im) { fData[1]=Im; }
77 
78 
79 void QComplex::Set(const double Re, const double Im)
80 { fData[0]=Re;fData[1]=Im; }
81 
82 void QComplex::SetPolar(const double mag, const double phase)
83 { fData[0]=mag*cos(phase); fData[1]=mag*sin(phase); }
84 
85 
87 {
88  return fData[0]*fData[0]+fData[1]*fData[1];
89 }
90 
91 double QComplex::GetMagnitude() const
92 {
93  return sqrt(GetModulusSquare());
94 }
95 
96 double QComplex::GetPhase() const
97 {
98  return atan2(fData[1],fData[0]);
99 }
100 
102 {
103  return QComplex(fData[0],-fData[1]);
104 }
105 
107 {
108  return QComplex(fData[0]+other.fData[0],fData[1]+other.fData[1]);
109 }
110 
112 {
113  return QComplex(fData[0]-other.fData[0],fData[1]-other.fData[1]);
114 }
115 
117 {
118  double re=fData[0]*other.fData[0]-fData[1]*other.fData[1];
119  double im=fData[0]*other.fData[1]+fData[1]*other.fData[0];
120  return QComplex(re,im);
121 }
122 
124 {
125  double re=fData[0]*other.fData[0]+fData[1]*other.fData[1];
126  double im=fData[1]*other.fData[0]-fData[0]*other.fData[1];
127  double den=other.GetModulusSquare();
128  return QComplex(re/den,im/den);
129 }
131 {
132  fData[0]+=other.fData[0];
133  fData[1]+=other.fData[1];
134  return *this;
135 }
136 
138 {
139  fData[0]-=other.fData[0];
140  fData[1]-=other.fData[1];
141  return *this;
142 }
143 
145 {
146  (*this)= (*this)*other;
147  return *this;
148 }
149 
151 {
152  (*this)= (*this)/other;
153  return *this;
154 }
155 
156 QComplex QComplex::operator+(const double other) const
157 {
158  return QComplex(fData[0]+other,fData[1]);
159 }
160 
161 QComplex QComplex::operator-(const double other) const
162 {
163  return QComplex(fData[0]-other,fData[1]);
164 }
165 
166 QComplex QComplex::operator*(const double other) const
167 {
168  return QComplex(fData[0]*other,fData[1]*other);
169 }
170 
171 QComplex QComplex::operator/(const double other) const
172 {
173  return QComplex(fData[0]/other,fData[1]/other);
174 }
175 QComplex& QComplex::operator+=(const double other)
176 {
177  fData[0]+=other;
178  return *this;
179 }
180 
181 QComplex& QComplex::operator-=(const double other)
182 {
183  fData[0]-=other;
184  return *this;
185 }
186 
187 QComplex& QComplex::operator*=(const double other)
188 {
189  fData[0]*=other;
190  fData[1]*=other;
191  return *this;
192 }
193 
194 QComplex& QComplex::operator/=(const double other)
195 {
196  fData[0]/=other;
197  fData[1]/=other;
198  return *this;
199 }
200 
202 {
203  // std::cout<<"QComplex::opeator=():fData: "<<fData<<std::endl;
204  fData[0]=other.fData[0];
205  fData[1]=other.fData[1];
206  return *this;
207 }
208 
210 {
211  return vec*(*this);
212 }
213 
214 /*
215 void QComplex::SetMath() const
216 {
217  if(!fMath)
218  {
219  fMath=(gsl_complex*) malloc(sizeof(gsl_complex));
220  &(fMath->dat[0])=fData;
221  }
222  }*/
223 gsl_complex QComplex::Get_gsl_complex() const
224 {
225  gsl_complex res;
226  res.dat[0]=fData[0];
227  res.dat[1]=fData[1];
228  return res;
229 }
230 
231 
232 /********************************************
233  NON MEMBER FUNCTIONS
234  *********************************************/
236 
237 Diana::QComplex operator*(double t, const Diana::QComplex &z){
238  Diana::QComplex tmp(z);
239  tmp*=t;
240  return tmp;
241 }
242 Diana::QComplex operator/(double t, const Diana::QComplex &z){
243  Diana::QComplex tmp(t,0);
244  return tmp/z;
245 }
246 Diana::QComplex pow(const Diana::QComplex &z,double a) {
247  gsl_complex iZ;
248  iZ.dat[0]=z.Re();
249  iZ.dat[1]=z.Im();
250  gsl_complex oZ=gsl_complex_pow_real(iZ,a);
251  return Diana::QComplex(oZ.dat[0],oZ.dat[1]);
252 }
253 Diana::QComplex pow(const Diana::QComplex &z,const Diana::QComplex &a) {
254  gsl_complex iZ,iA;
255  iZ.dat[0]=z.Re();iZ.dat[1]=z.Im();
256  iA.dat[0]=a.Re();iA.dat[1]=a.Im();
257  gsl_complex oZ=gsl_complex_pow(iZ,iA);
258  return Diana::QComplex(oZ.dat[0],oZ.dat[1]);
259 }
260 double Re(const Diana::QComplex &z) { return z.Re(); }
261 
262 double Im(const Diana::QComplex &z) { return z.Im(); }
263 
264 std::ostream& operator<<(std::ostream& s,const Diana::QComplex& z)
265 {
266  s<<"("<<z.Re();
267  if(z.Im()<0)
268  {
269  s<<" ";
270  }
271  else
272  {
273  s<<" +";
274  }
275  s<<z.Im()<<"i)";
276  return s;
277 }
278 
279 
TF1 a
Definition: CheckOF.C:21
QVector vec(3)
std::ostream & operator<<(std::ostream &s, const Diana::QComplex &z)
Definition: QComplex.cc:264
Diana::QComplex pow(const Diana::QComplex &z, double a)
Raise a complex number to a real power.
Definition: QComplex.cc:246
Diana::QComplex operator/(double t, const Diana::QComplex &z)
Definition: QComplex.cc:242
QObjectImp(Diana::QComplex)
double Im(const Diana::QComplex &z)
Function to get the imag part.
Definition: QComplex.cc:262
double Re(const Diana::QComplex &z)
Function to get the real part.
Definition: QComplex.cc:260
Q_END_NAMESPACE Diana::QComplex operator*(double t, const Diana::QComplex &z)
Definition: QComplex.cc:237
#define Q_END_NAMESPACE
Definition: QDiana.hh:22
#define Q_BEGIN_NAMESPACE
Definition: QDiana.hh:20
Diana::QVector sin(const Diana::QVector &v)
Definition: QVector.cc:815
Diana::QVector cos(const Diana::QVector &v)
Definition: QVector.cc:821
QComplex operator+(const QComplex &other) const
Definition: QComplex.cc:106
double * fData
Definition: QComplex.hh:84
QComplex & operator*=(const QComplex &other)
Definition: QComplex.cc:144
void SetIm(const double Im)
Definition: QComplex.cc:76
QComplex & operator+=(const QComplex &other)
Definition: QComplex.cc:130
~QComplex()
Definition: QComplex.cc:67
double Im() const
Definition: QComplex.cc:73
QComplex & operator-=(const QComplex &other)
Definition: QComplex.cc:137
double GetPhase() const
Definition: QComplex.cc:96
gsl_complex Get_gsl_complex() const
Definition: QComplex.cc:223
void SetRe(const double Re)
Definition: QComplex.cc:75
QComplex Conj() const
Definition: QComplex.cc:101
double Re() const
Definition: QComplex.cc:72
QComplex()
default constructor
Definition: QComplex.cc:18
QComplex operator-(const QComplex &other) const
Definition: QComplex.cc:111
bool fOwner
Definition: QComplex.hh:82
double GetModulusSquare() const
Definition: QComplex.cc:86
void Set(const double Re, const double Im)
Definition: QComplex.cc:79
const QComplex & operator=(const QComplex &other)
Definition: QComplex.cc:201
QComplex operator/(const QComplex &other) const
Definition: QComplex.cc:123
QComplex & operator/=(const QComplex &other)
Definition: QComplex.cc:150
double GetMagnitude() const
Definition: QComplex.cc:91
QComplex operator*(const QComplex &other) const
Definition: QComplex.cc:116
void SetPolar(const double mag, const double phase)
Definition: QComplex.cc:82
Interface for complex vectors in Diana analysis.
Definition: QVectorC.hh:25