Newer
Older
// Vector4 template class definition file. -*- C++ -*-
/* Copyright 2008 Mike Williams (mwill@jlab.org)
*
* This file is part of qft++.
*
* qft++ is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* qft++ is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with qft++. If not, see <http://www.gnu.org/licenses/>.
*/
// Author: Mike Williams
#ifndef _Vector4_H
#define _Vector4_H
//_____________________________________________________________________________
/** @file Vector4.h
* @brief Vector4 template class definition file.
*/
//_____________________________________________________________________________
#include <cmath>
#include "qft++/tensor/Tensor.hh"
using namespace std;
//_____________________________________________________________________________
/** @class Vector4
* @brief \f$ x_{\mu}, p_{\mu} \f$ : 4-vectors and 4-momenta.
*
* @author Mike Williams
*
* This class provides extra functionality needed for 4-vectors. The class
* inherits from Tensor and in most cases is used as such.
*/
//_____________________________________________________________________________
template <typename _Tp> class Vector4 : public Tensor<_Tp> {
public:
// create/copy/destroy:
Vector4() : Tensor<_Tp>::Tensor(1) {/** Default Constructor */}
/// Constructor (initialize the 4-vector to be (t,x,y,z))
Vector4(typename Type<_Tp>::ParamType __t,typename Type<_Tp>::ParamType __x,
typename Type<_Tp>::ParamType __y,typename Type<_Tp>::ParamType __z)
: Tensor<_Tp>::Tensor(1) {
this->SetV4(__t,__x,__y,__z);
}
/// copy Constructor to convert float to _Tp
Vector4(const Vector4<float>* __v4)
: Tensor<_Tp>::Tensor(1) {
this->SetV4(__v4->T(),__v4->X(),__v4->Y(),__v4->Z());
}
/// Copy Constructor
Vector4(const Vector4<_Tp> &__v4) : Tensor<_Tp>::Tensor(__v4) {}
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
virtual ~Vector4(){/** Destructor */}
// Setters:
/// Set the 4-vector to (t,x,y,z)
void SetV4(typename Type<_Tp>::ParamType __t,
typename Type<_Tp>::ParamType __x,
typename Type<_Tp>::ParamType __y,
typename Type<_Tp>::ParamType __z){
this->Element(0) = __t;
this->Element(1) = __x;
this->Element(2) = __y;
this->Element(3) = __z;
}
/// Set the 4-vector to (e,px,py,pz)
inline void SetP4(typename Type<_Tp>::ParamType __e,
typename Type<_Tp>::ParamType __px,
typename Type<_Tp>::ParamType __py,
typename Type<_Tp>::ParamType __pz){
this->SetV4(__e,__px,__py,__pz);
}
// Getters:
/// Get the time component of the 4-vector
inline const _Tp& T() const {
return this->Element(0);
}
/// Get the x component of the 4-vector
inline const _Tp& X() const {
return this->Element(1);
}
/// Get the y component of the 4-vector
inline const _Tp& Y() const {
return this->Element(2);
}
/// Get the z component of the 4-vector
inline const _Tp& Z() const {
return this->Element(3);
}
/// Get the time component of the 4-vector
inline _Tp& T() {
return this->Element(0);
}
/// Get the x component of the 4-vector
inline _Tp& X() {
return this->Element(1);
}
/// Get the y component of the 4-vector
inline _Tp& Y() {
return this->Element(2);
}
/// Get the z component of the 4-vector
inline _Tp& Z() {
return this->Element(3);
}
/// Get the energy component of the 4-vector
inline const _Tp& E() const {
return this->Element(0);
}
/// Get the x component of the 4-vector
inline const _Tp& Px() const {
return this->Element(1);
}
/// Get the y component of the 4-vector
inline const _Tp& Py() const {
return this->Element(2);
}
/// Get the z component of the 4-vector
inline const _Tp& Pz() const {
return this->Element(3);
}
/// Get the energy component of the 4-vector
inline _Tp& E() {
return this->Element(0);
}
/// Get the x component of the 4-vector
inline _Tp& Px() {
return this->Element(1);
}
/// Get the y component of the 4-vector
inline _Tp& Py() {
return this->Element(2);
}
/// Get the z component of the 4-vector
inline _Tp& Pz() {
return this->Element(3);
}
// operators:
/// Assignment operator
template <typename T> Vector4<_Tp>& operator=(const Tensor<T> &__tensor){
if(__tensor.Rank() != 1) {
cout << "Error! Attempt to set Vector4 equal to a tensor w/ rank != 1"
<< endl;
}
assert(__tensor.Rank() == 1);
this->SetV4(__tensor(0),__tensor(1),__tensor(2),__tensor(3));
return *this;
}
/// Sets @a this = @a this + @a v4
template <typename T> Vector4<_Tp>& operator+=(const Vector4<T> &__v4){
*this = (*this) + __v4;
return *this;
}
/// Sets @a this = @a this - @a v4
template <typename T> Vector4<_Tp>& operator-=(const Vector4<T> &__v4){
*this = (*this) - __v4;
return *this;
}
template <typename T> bool operator==(const Vector4<T> &__v4){
bool result=true;
if( fabs(Px()-__v4.Px()) > 1e-6) result=false;
else if(fabs(Py()-__v4.Py()) > 1e-6) result=false;
else if(fabs(Pz()-__v4.Pz()) > 1e-6) result=false;
else if(fabs(E()-__v4.E()) > 1e-6) result=false;
return result;
}
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Functions:
/** Is this a valid 4-momentum?
* Returns @a true if this is a valid 4-momentum. Valid means that all
* of its elements are real numbers and its \f$ \beta \leq 1 \f$.
*/
bool IsP4() const {
if((imag(this->E()) != 0.) || (imag(this->Px()) != 0.)
||(imag(this->Py()) != 0.) || (imag(this->Pz()) != 0.)) return false;
if(this->Beta() > 1.0) return false;
return true;
}
/// Returns the magnitude of the momentum (\f$\sqrt{px^2 + py^2 + pz^2}\f$)
inline _Tp P() const {
return sqrt((this->Px())*(this->Px()) + (this->Py())*(this->Py())
+ (this->Pz())*(this->Pz()));
}
/// Returns \f$ \beta = \frac{p}{E} \f$
inline _Tp Beta() const {
return (this->P()/this->E());
}
/// Returns \f$ \gamma = \frac{1}{\sqrt{1 - \beta^2}} \f$
inline _Tp Gamma() const {
return (1.0/sqrt(1.0 - (this->Beta()*this->Beta())));
}
/// Returns \f$ \sqrt{p_{\mu} p^{\mu}} \f$
inline _Tp Mass() const {
return sqrt(this->Mass2());
}
/// Returns \f$ p_{\mu} p^{\mu} \f$
inline _Tp Mass2() const {
return (*this)*(*this);
}
/// Returns \f$ \sqrt{p_{\mu} p^{\mu}} \f$
inline _Tp M() const {
return sqrt(this->Mass2());
}
/// Returns \f$ p_{\mu} p^{\mu} \f$
inline _Tp M2() const {
return (*this)*(*this);
}
/// Returns the magnitude of the 4-vector (\f$\sqrt{x^2 + y^2 + z^2}\f$)
inline _Tp R() const {
return this->P();
}
/// Returns \f$ \sqrt{x_{\mu} x^{\mu}} \f$
inline _Tp Length() const {
return this->Mass();
}
/// Returns \f$ x_{\mu} x^{\mu} \f$
inline _Tp Length2() const {
return (*this)*(*this);
}
/// Returns \f$ \sqrt{x_{\mu} x^{\mu}} \f$
inline _Tp L() const {
return this->Mass();
}
/// Returns \f$ x_{\mu} x^{\mu} \f$
inline _Tp L2() const {
return (*this)*(*this);
}
/// Returns \f$ cos(\theta) = \frac{z}{r} \f$
inline _Tp CosTheta() const {
return this->Z()/this->R();
}
/// Returns \f$\sqrt{x^2 + y^2}\f$
inline _Tp Rho() const {
return sqrt((this->Px())*(this->Px()) + (this->Py())*(this->Py()));
}
/// Returns \f$\sqrt{px^2 + py^2}\f$
inline _Tp Pxy() const {
return sqrt((this->Px())*(this->Px()) + (this->Py())*(this->Py()));
}
/// Returns \f$ \theta = cos^{-1}(\frac{z}{r}) \f$
inline _Tp Theta() const {
return acos(this->Z()/this->R());
}
/// Returns \f$ \theta = tan^{-1}(\frac{y}{x}) \f$ \f$ (-\pi,\pi) \f$
inline _Tp Phi() const {
return atan2(this->Y(),this->X());
}
};
//_____________________________________________________________________________
//
// some non-member operaors used to keep the type Vector4
//_____________________________________________________________________________
/// 4-vector addition
template <typename T1,typename T2>
Vector4<typename AddType<T1,T2>::Type>
operator+(const Vector4<T1> &__v4a,const Vector4<T2> &__v4b) {
Vector4<typename AddType<T1,T2>::Type> ret;
ret = __v4a.operator+(__v4b);
return ret;
}
//_____________________________________________________________________________
/// 4-vector subtraction
template <typename T1,typename T2>
Vector4<typename SubType<T1,T2>::Type>
operator-(const Vector4<T1> &__v4a,const Vector4<T2> &__v4b) {
Vector4<typename SubType<T1,T2>::Type> ret;
ret = __v4a.operator-(__v4b);
return ret;
}
//_____________________________________________________________________________
/// 4-vector contraction (returns the type)
template <typename T1,typename T2> typename MultType<T1,T2>::Type
operator*(const Vector4<T1> &__v4a,const Vector4<T2> &__v4b) {
return (__v4a.operator*(__v4b)).Element();
}
//_____________________________________________________________________________
#endif /* _Vector4_H */