Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.22 KB | None | 0 0
  1.  
  2. // PHYS 30762 Programming in C++
  3. // Assignment 7, 24/03/17, Owen Williams
  4.  
  5. // Program which has a generalised vector class
  6. // A second class for Minkowski 4-vectors which inherits from the generalised vector class
  7.  
  8. #include<iostream>
  9. #include<stdlib.h> // for c style exit
  10. #include<cmath>
  11. using namespace std;
  12.  
  13. //class GeneralVector: class for a general n dimensional vector.
  14. class GeneralVector {
  15. // Friends
  16. friend ostream & operator<<(ostream &os, const GeneralVector &vector);
  17. friend istream & operator>>(istream &is, GeneralVector &vector);
  18.  
  19. protected:
  20. double *vdata;
  21. int vlength;
  22. public:
  23. // Default constructor
  24. GeneralVector(){ vdata = 0; vlength = 0; }
  25. // Parameterized constructor
  26. GeneralVector(int vlength);
  27. // Copy constructor
  28. GeneralVector(GeneralVector&);
  29. // Move constructor
  30. GeneralVector(GeneralVector&&);
  31. // Destructor
  32. ~GeneralVector(){ cout << "Destructor called" << endl; delete[] vdata; }
  33.  
  34. // Access functions
  35. int getlength() const { return vlength; } // Return the length of the vector
  36. double & operator()(int x) const {
  37. if (x < vlength){
  38. return vdata[x]; // Return the ith coordinate of the vector if it exists
  39. }
  40. cout << "You are trying to access a vector component that does not exist." << endl;
  41. exit(1);
  42. }
  43.  
  44. // Other functions
  45. // Copy Assignment operator
  46. GeneralVector& operator=(GeneralVector&);
  47. // Move Assignment operator
  48. GeneralVector& operator=(GeneralVector&&);
  49.  
  50. //Function to set the data from an inherited class
  51. void setdata(int x, double d) { vdata[x] = d; }
  52.  
  53. // Addition, subtraction and multiplication of a general vector
  54. GeneralVector operator+(const GeneralVector &vectorObject)const {
  55. //check that the two vectors are of the same length
  56. if (vlength != vectorObject.getlength()){
  57. cout << "The vectors could not be added because they were not the same length." << endl;
  58. GeneralVector temp;
  59. return temp;
  60. }
  61. GeneralVector temp(vlength);
  62. temp.vdata = new double[vlength];
  63. //add each of the components of the vector
  64. for (int i = 0; i < vlength; i++){
  65. temp.vdata[i] = vdata[i] + vectorObject.vdata[i];
  66. }
  67. return temp;
  68. }
  69.  
  70. GeneralVector operator-(const GeneralVector &vectorObject)const {
  71. //check that the two vectors are of the same length
  72. if (vlength != vectorObject.getlength()){
  73. cout << "The vectors could not be subtracted because they were not the same length." << endl;
  74. GeneralVector temp;
  75. return temp;
  76. }
  77. GeneralVector temp(vlength);
  78. temp.vdata = new double[vlength];
  79. //subtract each of the components of the vector
  80. for (int i = 0; i < vlength; i++){
  81. temp.vdata[i] = vdata[i] + vectorObject.vdata[i];
  82. }
  83. return temp;
  84. }
  85.  
  86. // Overload * operator to multiply by a constant
  87. GeneralVector operator*(double constantValue)const {
  88. //check that the two vectors are of the same length
  89. GeneralVector temp(vlength);
  90. temp.vdata = new double[vlength];
  91. //subtract each of the components of the vector
  92. for (int i = 0; i < vlength; i++){
  93. temp.vdata[i] = vdata[i] * constantValue;
  94. }
  95. return temp;
  96. }
  97.  
  98. // Overload * operator for another vector
  99. void operator*(const GeneralVector &vectorObject) {
  100. cout << "If you would like to use the dot product, please use vector1.dotproduct(vector2).";
  101. }
  102.  
  103.  
  104. //Calculate the dot product of two vectors
  105. double dotproduct(const GeneralVector &vectorObject) const{
  106. //check that the two vectors are of the same length
  107. if (vlength != vectorObject.getlength()){
  108. cout << "The dot product of the vectors could not be found as they are not the same length." << endl;
  109. exit(1);
  110. }
  111. double temp = 0;
  112. //multiply each of the components of the vector
  113. for (int i = 0; i < vlength; i++){
  114. temp += vdata[i] * vectorObject.vdata[i];
  115. }
  116. return temp;
  117. }
  118. };
  119.  
  120. //class for minowski vectors. Vectors must be 2 or 4 components. Modified dot product for minowski vector space.
  121. class FourVector: public GeneralVector {
  122.  
  123. protected:
  124.  
  125. public:
  126. // Default constructor
  127. FourVector(){
  128. vlength = 4;
  129. vdata = new double[vlength];
  130. //multiply each of the components of the vector
  131. for (int i = 0; i < vlength; i++){
  132. vdata[i] = 0;
  133. }
  134. }
  135. // Parameterized constructor
  136. FourVector(double ct, const GeneralVector &vectorObject);
  137. FourVector(double, double, double, double);
  138.  
  139. //Calculate the dot product of two vectors
  140. double dotproduct(const FourVector &vectorObject) const{
  141. //check that the two vectors are of the same length
  142. if (vlength != vectorObject.getlength()){
  143. cout << "The dot product of the vectors could not be found as they are not the same length." << endl;
  144. exit(1);
  145. }
  146. double temp = 0;
  147. //multiply each of the components of the vector
  148. for (int i = 0; i < vlength; i++){
  149. if (i == 0){
  150. temp += vdata[i] * vectorObject.vdata[i];
  151. }
  152. else {
  153. temp -= vdata[i] * vectorObject.vdata[i];
  154. }
  155. }
  156. return temp;
  157. }
  158.  
  159. FourVector LorentzBoost(const GeneralVector &vectorObject){
  160. //Calculate gamma
  161. double betasquared = vectorObject.dotproduct(vectorObject);
  162. double gamma = 1 / sqrt(1 - betasquared);
  163. //Create 3 vector for components
  164. GeneralVector threeVector(3);
  165. for (int i = 0; i < threeVector.getlength(); i++){
  166. threeVector.setdata(i, vdata[i + 1]);
  167. }
  168.  
  169. //First component
  170. double firstComponent = gamma *(vdata[0] - vectorObject.dotproduct(threeVector));
  171. //Spacial components
  172. GeneralVector multipliedVector = vectorObject*(((gamma - 1)*threeVector.dotproduct(vectorObject) / betasquared) - (gamma*vdata[0]));
  173. GeneralVector boostedVector = threeVector + multipliedVector;
  174. FourVector temp(firstComponent, boostedVector);
  175. return temp;
  176. }
  177. };
  178.  
  179. // Member functions defined outside class
  180. GeneralVector::GeneralVector(int length){
  181. vlength = length;
  182. if (vlength<1){
  183. cout << "Error: can not create a vector with no components." << endl;
  184. exit(1);
  185. }
  186. vdata = new double[vlength];
  187. for (int i = 0; i<vlength; i++) vdata[i] = 0;
  188. }
  189.  
  190. // Function to overload << operator to print out a matrix object nicely
  191. ostream & operator<<(ostream &os, const GeneralVector &vectorObject){
  192. if (vectorObject.getlength() < 1){
  193. cout << "Error: can't print vector with no components." << endl;
  194. return os;
  195. }
  196. cout << "(";
  197. for (int i = 0; i < vectorObject.getlength(); i++){
  198. cout << vectorObject(i);
  199. if (i != vectorObject.getlength() - 1){
  200. cout << ", ";
  201. }
  202. }
  203. cout << ")";
  204. return os;
  205. }
  206.  
  207.  
  208. istream& operator>>(istream &is, GeneralVector &vectorObject){
  209. int inputLength;
  210. cout << "What is the length of the vector? ";
  211. cin >> inputLength;
  212. vectorObject.vlength = inputLength;
  213. vectorObject.vdata = new double[vectorObject.vlength];
  214.  
  215. for (int i = 0; i < vectorObject.vlength; i++){
  216. cout << "Please enter vector component " << i << ": ";
  217. double input;
  218. cin >> input;
  219. vectorObject(i) = input;
  220. }
  221. return is;
  222. }
  223.  
  224. // Copy constructor for deep copying
  225. GeneralVector::GeneralVector(GeneralVector &vectorObject){
  226. // Copy size and declare new array
  227. vdata = 0;
  228. vlength = vectorObject.getlength();
  229. if (vlength>0)
  230. {
  231. vdata = new double[vlength];
  232. // Copy values into new array
  233. for (int i = 0; i<vlength; i++) vdata[i] = vectorObject.vdata[i];
  234. }
  235. }
  236.  
  237. // Move constructor
  238. GeneralVector::GeneralVector(GeneralVector && vectorObject){
  239. // steal the data
  240. cout << "move constructor called";
  241. vlength = vectorObject.vlength;
  242. vdata = vectorObject.vdata;
  243. vectorObject.vdata = 0;//nullptr;
  244. vectorObject.vlength = 0;
  245. }
  246.  
  247. // Assignment operator for deep copying
  248. GeneralVector & GeneralVector::operator=(GeneralVector &vectorObject){
  249. if (&vectorObject == this) return *this; // no self assignment
  250. // First delete data for this object
  251. delete[] vdata;
  252. vdata = 0;
  253. vlength = 0;
  254. // Now copy the length and the array containing the information
  255. vlength = vectorObject.getlength();
  256. if (vlength>0)
  257. {
  258. vdata = new double[vlength];
  259. // Copy values into new array
  260. for (int i = 0; i<vlength; i++) vdata[i] = vectorObject.vdata[i];
  261. }
  262. return *this;
  263. }
  264.  
  265. // Move Assignment operator
  266. GeneralVector & GeneralVector ::operator=(GeneralVector && vectorObject){
  267. cout << "move assignment\n";
  268. std::swap(vdata, vectorObject.vdata);
  269. std::swap(vlength, vectorObject.vlength);
  270. return *this; // Special pointer!!!
  271. }
  272.  
  273. FourVector::FourVector(double x0, double x1, double x2, double x3){
  274. vlength = 4;
  275. vdata = new double[vlength];
  276. vdata[0] = x0;
  277. vdata[1] = x1;
  278. vdata[2] = x2;
  279. vdata[3] = x3;
  280. }
  281.  
  282. FourVector::FourVector(double ct, const GeneralVector &vectorObject){
  283. vlength = 4;
  284. if (vectorObject.getlength()!=3){
  285. cout << "Error: the three vector has no components." << endl;
  286. exit(1);
  287. }
  288. vdata = new double[vlength];
  289. vdata[0] = ct;
  290. for (int i = 0; i<vectorObject.getlength(); i++) vdata[i+1] = vectorObject(i);
  291. }
  292.  
  293.  
  294. //end of class functions
  295.  
  296.  
  297.  
  298. // Main program
  299. int main()
  300. {
  301. /*
  302. GeneralVector a1;
  303. cin >> a1;
  304. cout << a1 << endl;
  305.  
  306.  
  307. GeneralVector a2;
  308. cin >> a2;
  309. cout << a2 << endl;
  310.  
  311. GeneralVector a3 = a1 + a2;
  312. cout << a3;
  313.  
  314. GeneralVector a4 = a1.dotproduct(a2);
  315. cout << a4 << endl;
  316.  
  317. cout << a1(2) << endl;
  318. cout << a1(3) << endl;*/
  319.  
  320. GeneralVector b1;
  321. cin >> b1;
  322. cout << b1 << endl;
  323.  
  324. FourVector f1(1,0,0,0);
  325. cout << f1;
  326.  
  327. FourVector f2 = f1.LorentzBoost(b1);
  328. cout << f2;
  329.  
  330.  
  331. return 0;
  332. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement