Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // PHYS 30762 Programming in C++
- // Assignment 7, 24/03/17, Owen Williams
- // Program which has a generalised vector class
- // A second class for Minkowski 4-vectors which inherits from the generalised vector class
- #include<iostream>
- #include<stdlib.h> // for c style exit
- #include<cmath>
- using namespace std;
- //class GeneralVector: class for a general n dimensional vector.
- class GeneralVector {
- // Friends
- friend ostream & operator<<(ostream &os, const GeneralVector &vector);
- friend istream & operator>>(istream &is, GeneralVector &vector);
- protected:
- double *vdata;
- int vlength;
- public:
- // Default constructor
- GeneralVector(){ vdata = 0; vlength = 0; }
- // Parameterized constructor
- GeneralVector(int vlength);
- // Copy constructor
- GeneralVector(GeneralVector&);
- // Move constructor
- GeneralVector(GeneralVector&&);
- // Destructor
- ~GeneralVector(){ cout << "Destructor called" << endl; delete[] vdata; }
- // Access functions
- int getlength() const { return vlength; } // Return the length of the vector
- double & operator()(int x) const {
- if (x < vlength){
- return vdata[x]; // Return the ith coordinate of the vector if it exists
- }
- cout << "You are trying to access a vector component that does not exist." << endl;
- exit(1);
- }
- // Other functions
- // Copy Assignment operator
- GeneralVector& operator=(GeneralVector&);
- // Move Assignment operator
- GeneralVector& operator=(GeneralVector&&);
- //Function to set the data from an inherited class
- void setdata(int x, double d) { vdata[x] = d; }
- // Addition, subtraction and multiplication of a general vector
- GeneralVector operator+(const GeneralVector &vectorObject)const {
- //check that the two vectors are of the same length
- if (vlength != vectorObject.getlength()){
- cout << "The vectors could not be added because they were not the same length." << endl;
- GeneralVector temp;
- return temp;
- }
- GeneralVector temp(vlength);
- temp.vdata = new double[vlength];
- //add each of the components of the vector
- for (int i = 0; i < vlength; i++){
- temp.vdata[i] = vdata[i] + vectorObject.vdata[i];
- }
- return temp;
- }
- GeneralVector operator-(const GeneralVector &vectorObject)const {
- //check that the two vectors are of the same length
- if (vlength != vectorObject.getlength()){
- cout << "The vectors could not be subtracted because they were not the same length." << endl;
- GeneralVector temp;
- return temp;
- }
- GeneralVector temp(vlength);
- temp.vdata = new double[vlength];
- //subtract each of the components of the vector
- for (int i = 0; i < vlength; i++){
- temp.vdata[i] = vdata[i] + vectorObject.vdata[i];
- }
- return temp;
- }
- // Overload * operator to multiply by a constant
- GeneralVector operator*(double constantValue)const {
- //check that the two vectors are of the same length
- GeneralVector temp(vlength);
- temp.vdata = new double[vlength];
- //subtract each of the components of the vector
- for (int i = 0; i < vlength; i++){
- temp.vdata[i] = vdata[i] * constantValue;
- }
- return temp;
- }
- // Overload * operator for another vector
- void operator*(const GeneralVector &vectorObject) {
- cout << "If you would like to use the dot product, please use vector1.dotproduct(vector2).";
- }
- //Calculate the dot product of two vectors
- double dotproduct(const GeneralVector &vectorObject) const{
- //check that the two vectors are of the same length
- if (vlength != vectorObject.getlength()){
- cout << "The dot product of the vectors could not be found as they are not the same length." << endl;
- exit(1);
- }
- double temp = 0;
- //multiply each of the components of the vector
- for (int i = 0; i < vlength; i++){
- temp += vdata[i] * vectorObject.vdata[i];
- }
- return temp;
- }
- };
- //class for minowski vectors. Vectors must be 2 or 4 components. Modified dot product for minowski vector space.
- class FourVector: public GeneralVector {
- protected:
- public:
- // Default constructor
- FourVector(){
- vlength = 4;
- vdata = new double[vlength];
- //multiply each of the components of the vector
- for (int i = 0; i < vlength; i++){
- vdata[i] = 0;
- }
- }
- // Parameterized constructor
- FourVector(double ct, const GeneralVector &vectorObject);
- FourVector(double, double, double, double);
- //Calculate the dot product of two vectors
- double dotproduct(const FourVector &vectorObject) const{
- //check that the two vectors are of the same length
- if (vlength != vectorObject.getlength()){
- cout << "The dot product of the vectors could not be found as they are not the same length." << endl;
- exit(1);
- }
- double temp = 0;
- //multiply each of the components of the vector
- for (int i = 0; i < vlength; i++){
- if (i == 0){
- temp += vdata[i] * vectorObject.vdata[i];
- }
- else {
- temp -= vdata[i] * vectorObject.vdata[i];
- }
- }
- return temp;
- }
- FourVector LorentzBoost(const GeneralVector &vectorObject){
- //Calculate gamma
- double betasquared = vectorObject.dotproduct(vectorObject);
- double gamma = 1 / sqrt(1 - betasquared);
- //Create 3 vector for components
- GeneralVector threeVector(3);
- for (int i = 0; i < threeVector.getlength(); i++){
- threeVector.setdata(i, vdata[i + 1]);
- }
- //First component
- double firstComponent = gamma *(vdata[0] - vectorObject.dotproduct(threeVector));
- //Spacial components
- GeneralVector multipliedVector = vectorObject*(((gamma - 1)*threeVector.dotproduct(vectorObject) / betasquared) - (gamma*vdata[0]));
- GeneralVector boostedVector = threeVector + multipliedVector;
- FourVector temp(firstComponent, boostedVector);
- return temp;
- }
- };
- // Member functions defined outside class
- GeneralVector::GeneralVector(int length){
- vlength = length;
- if (vlength<1){
- cout << "Error: can not create a vector with no components." << endl;
- exit(1);
- }
- vdata = new double[vlength];
- for (int i = 0; i<vlength; i++) vdata[i] = 0;
- }
- // Function to overload << operator to print out a matrix object nicely
- ostream & operator<<(ostream &os, const GeneralVector &vectorObject){
- if (vectorObject.getlength() < 1){
- cout << "Error: can't print vector with no components." << endl;
- return os;
- }
- cout << "(";
- for (int i = 0; i < vectorObject.getlength(); i++){
- cout << vectorObject(i);
- if (i != vectorObject.getlength() - 1){
- cout << ", ";
- }
- }
- cout << ")";
- return os;
- }
- istream& operator>>(istream &is, GeneralVector &vectorObject){
- int inputLength;
- cout << "What is the length of the vector? ";
- cin >> inputLength;
- vectorObject.vlength = inputLength;
- vectorObject.vdata = new double[vectorObject.vlength];
- for (int i = 0; i < vectorObject.vlength; i++){
- cout << "Please enter vector component " << i << ": ";
- double input;
- cin >> input;
- vectorObject(i) = input;
- }
- return is;
- }
- // Copy constructor for deep copying
- GeneralVector::GeneralVector(GeneralVector &vectorObject){
- // Copy size and declare new array
- vdata = 0;
- vlength = vectorObject.getlength();
- if (vlength>0)
- {
- vdata = new double[vlength];
- // Copy values into new array
- for (int i = 0; i<vlength; i++) vdata[i] = vectorObject.vdata[i];
- }
- }
- // Move constructor
- GeneralVector::GeneralVector(GeneralVector && vectorObject){
- // steal the data
- cout << "move constructor called";
- vlength = vectorObject.vlength;
- vdata = vectorObject.vdata;
- vectorObject.vdata = 0;//nullptr;
- vectorObject.vlength = 0;
- }
- // Assignment operator for deep copying
- GeneralVector & GeneralVector::operator=(GeneralVector &vectorObject){
- if (&vectorObject == this) return *this; // no self assignment
- // First delete data for this object
- delete[] vdata;
- vdata = 0;
- vlength = 0;
- // Now copy the length and the array containing the information
- vlength = vectorObject.getlength();
- if (vlength>0)
- {
- vdata = new double[vlength];
- // Copy values into new array
- for (int i = 0; i<vlength; i++) vdata[i] = vectorObject.vdata[i];
- }
- return *this;
- }
- // Move Assignment operator
- GeneralVector & GeneralVector ::operator=(GeneralVector && vectorObject){
- cout << "move assignment\n";
- std::swap(vdata, vectorObject.vdata);
- std::swap(vlength, vectorObject.vlength);
- return *this; // Special pointer!!!
- }
- FourVector::FourVector(double x0, double x1, double x2, double x3){
- vlength = 4;
- vdata = new double[vlength];
- vdata[0] = x0;
- vdata[1] = x1;
- vdata[2] = x2;
- vdata[3] = x3;
- }
- FourVector::FourVector(double ct, const GeneralVector &vectorObject){
- vlength = 4;
- if (vectorObject.getlength()!=3){
- cout << "Error: the three vector has no components." << endl;
- exit(1);
- }
- vdata = new double[vlength];
- vdata[0] = ct;
- for (int i = 0; i<vectorObject.getlength(); i++) vdata[i+1] = vectorObject(i);
- }
- //end of class functions
- // Main program
- int main()
- {
- /*
- GeneralVector a1;
- cin >> a1;
- cout << a1 << endl;
- GeneralVector a2;
- cin >> a2;
- cout << a2 << endl;
- GeneralVector a3 = a1 + a2;
- cout << a3;
- GeneralVector a4 = a1.dotproduct(a2);
- cout << a4 << endl;
- cout << a1(2) << endl;
- cout << a1(3) << endl;*/
- GeneralVector b1;
- cin >> b1;
- cout << b1 << endl;
- FourVector f1(1,0,0,0);
- cout << f1;
- FourVector f2 = f1.LorentzBoost(b1);
- cout << f2;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement