testNewVector.cc

Go to the documentation of this file.
00001 #include <stdexcept>
00002 #include <string>
00003 #include <ostream>
00004 #include <iostream>
00005 
00006 
00007 class Vector4 {
00008 public:
00009 
00010   //  virtual ~Vector4() = 0;
00011 
00012 public:
00013   double operator[](size_t index) {
00014     if (index < 0 || index > 3) {
00015       throw std::runtime_error("Tried to access an invalid 4-vector index.");
00016     } else {
00017       return _elements[index];
00018     }
00019   }
00020 
00021   double invariant() const {
00022     double metricdiag[4] = {1, -1, -1, -1};
00023     double invariant = 0;
00024     for (size_t i = 0; i < 4; ++i) {
00025       invariant += metricdiag[i] * _elements[i]*_elements[i];
00026     }
00027     return invariant;
00028   }
00029 
00030 protected:
00031   double _elements[4];
00032 };
00033 
00034 
00035 class Vector4X : public Vector4 {
00036 public:
00037   Vector4X(double t, double x, double y, double z) {
00038     this->t(t);
00039     this->x(x);
00040     this->y(y);
00041     this->z(z);
00042   }
00043 
00044   ~Vector4X() {}
00045 
00046 public:
00047   double t() const { return _elements[0]; }
00048   double x() const { return _elements[1]; }
00049   double y() const { return _elements[2]; }
00050   double z() const { return _elements[3]; }
00051   Vector4X& t(double t) { _elements[0] = t; return *this; }
00052   Vector4X& x(double x) { _elements[1] = x; return *this; }
00053   Vector4X& y(double y) { _elements[2] = y; return *this; }
00054   Vector4X& z(double z) { _elements[3] = z; return *this; }
00055   double interval() const { return this->invariant(); }
00056 };
00057 
00058 
00059 class Vector4P : public Vector4 {
00060 public:
00061   Vector4P(double E, double px, double py, double pz) {
00062     this->E(E);
00063     this->px(px);
00064     this->py(py);
00065     this->pz(pz);
00066   }
00067 
00068   ~Vector4P() {}
00069 
00070 public:
00071   double E() const { return _elements[0]; }
00072   double px() const { return _elements[1]; }
00073   double py() const { return _elements[2]; }
00074   double pz() const { return _elements[3]; }
00075   Vector4P& E(double E) { _elements[0] = E; return *this; }
00076   Vector4P& px(double px) { _elements[1] = px; return *this; }
00077   Vector4P& py(double py) { _elements[2] = py; return *this; }
00078   Vector4P& pz(double pz) { _elements[3] = pz; return *this; }
00079   double mass() const { return this->invariant(); }
00080 };
00081 
00082 
00083 std::ostream& operator<<(std::ostream& out, Vector4& v4) {
00084   out << "("  << v4[0] 
00085       << ", " << v4[1]
00086       << ", " << v4[2]
00087       << ", " << v4[3] << ")";
00088   return out;
00089 }
00090 
00091 
00092 int main() {
00093   Vector4X a(1,0,0,0);
00094   std::cout << a << ": interval = " << a.interval() << std::endl;
00095   a.z(1);
00096   std::cout << a << ": interval = " << a.interval() << std::endl;
00097   a.y(2).z(3);
00098   std::cout << a << ": interval = " << a.interval() << std::endl;
00099   return EXIT_SUCCESS;
00100 }