#ifndef VECTOR_H #define VECTOR_H #include #include #define DIM 3 template class vector { public: T x[D]; public: vector(); vector(const T[D]); vector(const vector&); ~vector(); vector& operator+=(const vector&); vector& operator-=(const vector&); vector& operator*=(const T); vector& operator/=(const T); vector operator+(const vector&) const; vector operator-(const vector&) const; vector operator*(const T) const; vector operator/(const T) const; vector operator%(const T) const; bool operator==(const vector &a) const; vector integer() const; vector Double() const; static vector integer(const vector&); static vector Double(const vector&); T& operator[](const unsigned int); double dot(const vector&) const; static double dot(const vector&, const vector&); double norm_squared() const; static double norm_squared(const vector&); void read(std::ifstream&); void write(std::ofstream&) const; }; template std::ostream& operator<<(std::ostream&, const vector&); // constructor // ~~~~~~~~~~~ template vector::vector() { for(int k=0; k vector::vector(const T x_i[D]) { for(int k=0; k vector::vector(const vector &v) { for(int k=0; k vector::~vector() { } // += // ~~ template inline vector& vector::operator+=(const vector &v) { for(int k=0; k inline vector& vector::operator-=(const vector &v) { for(int k=0; k inline vector& vector::operator*=(const T s) { for(int k=0; k inline vector& vector::operator/=(const T s) { for(int k=0; k inline vector vector::operator+(const vector &a) const { vector c; for(int k=0; k inline vector vector::operator-(const vector &a) const { vector c; for(int k=0; k inline vector vector::operator*(const T s) const { vector c; for(int k=0; k inline vector vector::operator/(const T s) const { vector c; for(int k=0; k inline bool vector::operator==(const vector &a) const { for(int k=0; k inline vector vector::operator%(const T s) const { vector c; for(int k=0; k inline vector vector::integer() const { vector c; for(int k=0; k inline vector vector::integer(const vector& v) { return v.integer(); } // double // ~~~~~~~ template inline vector vector::Double() const { vector c; for(int k=0; k inline vector vector::Double(const vector& v) { return v.Double(); } // [] // ~~ template inline T& vector::operator[](const unsigned int i) { return x[i]; } // Dot // ~~~ template inline double vector::dot(const vector &a) const { double d=0; for(int k=0; k inline double vector::dot(const vector &a, const vector &b) { return a.dot(b); } // NormSquared // ~~~~~~~~~~~ template inline double vector::norm_squared() const { return dot(*this, *this); } template inline double vector::norm_squared(const vector& v) { return v.norm_squared(); } // read // ~~~~ template void vector::read(std::ifstream& in) { in.read((char*)x, sizeof(T)*D); } // write // ~~~~~ template void vector::write(std::ofstream& out) const { out.write((const char*)x, sizeof(T)*D); } // Insertion // ~~~~~~~~~ template std::ostream& operator<<(std::ostream& os, const vector& v) { os << "("; for(int k=0; k class vector_field { public: int elements; private: vector* f; vector size; // number of grid points for each dimension vector offset; public: vector_field(); vector_field(const vector&); ~vector_field(); vector get_size() const; void set_size(const vector&); vector& get(const vector&); void read(std::ifstream&); void write(std::ofstream&) const; static void swap(vector_field&, vector_field&); }; // vector_field // ~~~~~~~~~~~~ template vector_field::vector_field() : f(0), elements(0) { } // vector_field // ~~~~~~~~~~~~ template vector_field::vector_field(const vector& s) : f(0) { set_size(s); } // ~vector_field // ~~~~~~~~~~~~~ template vector_field::~vector_field() { if(f != 0) delete[] f; } // get_size // ~~~~~~~~ template inline vector vector_field::get_size() const { return size; } // set_size // ~~~~~~~~ template void vector_field::set_size(const vector& s) { if(f != 0) delete[] f; size = s; elements = 1; for(int i=0; i[elements]; } // get // ~~~ template inline vector& vector_field::get(const vector& pos) { int p=0; for(int i=0; i void vector_field::read(std::ifstream& in) { in.read((char*)f, elements*sizeof(T)*V); } // write // ~~~~~ template void vector_field::write(std::ofstream& out) const { out.write((const char*)f, elements*sizeof(T)*V); } // swap // ~~~~ template void vector_field::swap(vector_field& v1, vector_field& v2) { vector* f; f = v1.f; v1.f = v2.f; v2.f = f; } #endif