vector.hh
1 #ifndef VECTOR_H_
2 #define VECTOR_H_
3 //class Vector:
5 //This class represents a three-vector. Operators are overloaded to provide for the
6 //familiar operations of vector addition, subtraction, scalar multiplication and division,
7 //and the dot product. The x,y,z components can be output with the << operator onto an
8 //output stream.
9 //Methods are provided to take the vector product, find the magnitude, find a three-dimensional
10 //angle, and perform various rotations.
11 //
12 //The only methods that will alter an established vector are Reset and the three Set functions.
13 //All other methods (cross product, rotations, etc.) return a new Vector object.
15 #include <iostream>
16 
17 //#include <fstream>
18 //#include <sstream>
19 //#include <math.h>
20 //#include <string>
21 //#include <stdio.h>
22 //#include <stdlib.h>
23 
24 using std::ostream;
25 
27 class Vector {
28 public:
29  friend Vector operator +(const Vector& vector1, const Vector& vector2)
30  {
31  return Vector(vector1.x + vector2.x , vector1.y + vector2.y , vector1.z + vector2.z);
32  }
33  //Add two vectors component by component
34 
35  friend void operator +=(Vector& vector1, const Vector& vector2)
36  {
37  vector1 = vector1 + vector2;
38  }
39 
40  friend Vector operator -(const Vector& vector1, const Vector& vector2)
41  {
42  return Vector(vector1.x - vector2.x , vector1.y - vector2.y , vector1.z - vector2.z);
43  }
44  //Subtract two vectors component by component
45 
46  friend void operator -=(Vector& vector1, const Vector& vector2)
47  {
48  vector1 = vector1 - vector2;
49  }
50 
51  friend Vector operator -(const Vector& vec)
52  {
53  return Vector(-vec.x,-vec.y,-vec.z);
54  }
55  //Gives the negative of a vector
56 
57  friend double operator *(const Vector& vector1, const Vector& vector2)
58  {
59  return ((vector1.x * vector2.x) + (vector1.y * vector2.y) + (vector1.z * vector2.z));
60  }
61 
62  //Take the dot product of two vectors
63 
64  friend Vector operator /(const Vector &v, const double& a)
65  {
66  return Vector(v.x / a, v.y / a, v.z / a);
67  }
68  //Divide a vector by a scalar
69 
70  friend Vector operator *(const double& a, const Vector& v)
71  {
72  return Vector(a*v.x , a*v.y , a*v.z);
73  }
74 
75  friend Vector operator *(const Vector& v, const double& a)
76  {
77  return Vector(a*v.x , a*v.y , a*v.z);
78  }
79 
80  //Multiply a vector by a scalar
81 
82  friend ostream& operator <<(ostream& outs, const Vector& vec);
83  //Print the three rectangular components of the vector to the screen
84 
85  double operator [](int i) const
86  {
87  switch(i)
88  {
89  case 0: return x;
90  case 1: return y;
91  case 2: return z;
92  case 3: return phi;
93  case 4: return theta;
94  default:
95  return 0;
96  }
97  }
98  //Returns an element of the vector. vector[0] is the x component,
99  //vector[1] is the y component, and vector[2] is the z component.
100 
101  Vector(double x_inp,double y_inp,double z_inp);
102  //Constructor: Initialize a new vector with given values of x, y, and z.
103 
104  Vector(double *xarray);
105  //Constructor: Initialize a new vector with elements of xarray giving values
106  // of x,y,z.
107 
108  Vector(double theta, double phi);
109  //Constructor: Initialize a new vector with unit length and in the
110  //theta, phi direction.
111  //theta and phi must be in RADIANS!
112  //Accepts theta from 0 to PI, and any phi.
113 
114  Vector();
115  //Default constructor: Initialize a unit vector in the z direction.
116 
117  Vector RotateX(double angle) const;
118  //Returns the vector rotated counterclockwise (right handed coordinates)
119  //by "angle" radians about the X axis.
120  //N.B. : Returns a new Vector object. Does not change the vector it is called from.
121 
122  Vector RotateY(double angle) const;
123  //Returns the vector rotated counterclockwise (right handed coordinates)
124  //by "angle" radians about the Y axis.
125  //N.B. : Returns a new Vector object. Does not change the vector it is called from.
126 
127  Vector RotateZ(double angle) const;
128  //Returns the vector rotated counterclockwise (right handed coordinates)
129  //by "angle" radians about the Z axis.
130  //N.B. : Returns a new Vector object. Does not change the vector it is called from.
131 
132  Vector Cross(const Vector &vec) const;
133  //Takes the cross product this x vec.
134 
135  double Dot(const Vector &vec) const;
136 //Takes the dot product this x vec.
137 
138  Vector Rotate(double angle, const Vector &axis) const;
139  //Returns the vector that is this vector rotated around the vector "axis" by angle (in radians) "angle".
140 
141  Vector Zero();
142  //zero the vector
143 
144  double Mag2() const { return X()*X()* + Y()*Y() + Z()*Z(); }
145 
146  double Mag() const;
147  //Returns the magnitude of this vector.
148 
149  double Angle(const Vector &vec) const;
150  //Returns the 3-dimensional angle between this vector and the argument.
151 
152  Vector ChangeCoord(const Vector &new_x_axis,const Vector &new_y_axis) const;
153  Vector ChangeCoord(const Vector &new_z_axis) const;
154  //Returns this vector, rotated to a new coordinate system with the argument as the z-axis.
155  //The vector is rotated in such a way that a vector pointing in the z direction will be
156  //rotated onto the new axis.
157 
158  Vector Orthogonal() const;
159  // Returns a vector orthogonal to this one.
160 
161 
162  Vector Unit() const;
163  //Returns a unit vector in the same direction as this vector.
164 
165  //Accessor functions
166  double GetX() const { return x; }
167  double GetY() const { return y; }
168  double GetZ() const { return z; }
169  double X() const { return x; }
170  double Y() const { return y; }
171  double Z() const { return z; }
172 
173 
174 
175  double Theta() const;
176  double Phi() const;
177  void Print() const;
178 
179  //Mutator functions
180  void SetX(double inp) { x = inp; angles_need_updating = true ;}
181  void SetY(double inp) { y = inp; angles_need_updating = true ;}
182  void SetZ(double inp) { z = inp; angles_need_updating = true ;}
183  void SetXYZ(double inpx,double inpy,double inpz)
184  {
185  x = inpx;
186  y = inpy;
187  z = inpz;
188  angles_need_updating = true;
189  }
190  void Reset(double x_inp, double y_inp, double z_inp) { SetXYZ(x_inp, y_inp, z_inp); }
191 
192 protected:
193  //Class variables
194  double x; //x component of vector
195  double y; //y component of vector
196  double z; //z component of vector
197  mutable double theta; //theta component of vector in radians
198  mutable double phi; //phi component of vector in radians
199  mutable bool angles_need_updating;
200 
201  //Class private functions
202  void UpdateThetaPhi() const;
203  //This method finds theta and phi from the x,y,z Cartesian coordinates.
204  //It should be called at any time that the x,y,z components are modified,
205  //so that the theta and phi components are current at all times.
206 
207 }; //class Vector
208 
210 // Vector Constants
211 
212 static Vector x_axis = Vector(1,0,0);
213 static Vector y_axis = Vector(0,1,0);
214 static Vector z_axis = Vector(0,0,1);
215 #endif
Inelasticity distributions: stores parametrizations and picks inelasticities.
Definition: Primaries.h:38
This class represents a three-vector. Operators are overloaded to provide for the familiar operations...
Definition: vector.hh:27