SCOREC core
Parallel unstructured mesh tools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
mthVector.h
Go to the documentation of this file.
1 /******************************************************************************
2 
3  Copyright 2015 Scientific Computation Research Center,
4  Rensselaer Polytechnic Institute. All rights reserved.
5 
6  This work is open source software, licensed under the terms of the
7  BSD license as described in the LICENSE file in the top-level directory.
8 
9 *******************************************************************************/
10 
11 #ifndef MTH_VECTOR_H
12 #define MTH_VECTOR_H
13 
14 #include "canArray.h"
15 #include <cmath>
16 #include <ostream>
17 
21 namespace mth {
22 
28 template <class T, unsigned N=0>
29 class Vector : public can::Array<T,N>
30 {
31  public:
33  Vector() {}
37  Vector(unsigned n) {(void)n;}
39  Vector(T const* v)
40  {
41  for (unsigned i=0; i < N; ++i)
42  (*this)[i] = v[i];
43  }
47  T& operator()(unsigned i) {return (*this)[i];}
49  T const& operator()(unsigned i) const {return (*this)[i];}
52  {
53  for (unsigned i=0; i < N; ++i)
54  (*this)[i] += b[i];
55  return (*this);
56  }
59  {
60  Vector<T,N> r;
61  for (unsigned i=0; i < N; ++i)
62  r[i] = (*this)[i] + b[i];
63  return r;
64  }
67  {
68  for (unsigned i=0; i < N; ++i)
69  (*this)[i] -= b[i];
70  return (*this);
71  }
74  {
75  Vector<T,N> r;
76  for (unsigned i=0; i < N; ++i)
77  r[i] = (*this)[i] - b[i];
78  return r;
79  }
84  Vector<T,N> operator*(T const& s) const
85  {
86  Vector<T,N> r;
87  for (unsigned i=0; i < N; ++i)
88  r[i] = (*this)[i] * s;
89  return r;
90  }
93  Vector<T,N> operator/(T const& s) const
94  {
95  Vector<T,N> r;
96  for (unsigned i=0; i < N; ++i)
97  r[i] = (*this)[i] / s;
98  return r;
99  }
104  T operator*(Vector<T,N> const& b) const
105  {
106  T r = (T)0.0;
107  for (unsigned i=0; i < N; ++i)
108  r += (*this)[i] * b[i];
109  return r;
110  }
112  T getLength() const {return sqrt((*this)*(*this));}
114  Vector<T,N> normalize() const {return (*this)/getLength();}
116  void zero()
117  {
118  for (unsigned i=0; i < N; ++i)
119  (*this)[i] = (T)0.0;
120  }
121 };
122 
131 template <class T>
132 class Vector<T,0> : public can::Array<T,0>
133 {
134  public:
136  Vector() {}
138  Vector(unsigned n) : can::Array<T>(n) {}
142  T& operator()(unsigned i) {return (*this)[i];}
144  T const& operator()(unsigned i) const {return (*this)[i];}
147  {
148  for (unsigned i=0; i < this->sz; ++i)
149  (*this)[i] += b[i];
150  return *this;
151  }
154  {
155  for (unsigned i=0; i < this->sz; ++i)
156  (*this)[i] -= b[i];
157  return *this;
158  }
160  Vector<T,0>& operator*=(T const& s)
161  {
162  for (unsigned i=0; i < this->sz; ++i)
163  (*this)[i] *= s;
164  return *this;
165  }
167  Vector<T,0>& operator/=(T const& s)
168  {
169  for (unsigned i=0; i < this->sz; ++i)
170  (*this)[i] *= s;
171  return *this;
172  }
174  T getLength() const {return sqrt((*this)*(*this));}
176  void zero()
177  {
178  for (unsigned i=0; i < this->sz; ++i)
179  (*this)[i] = (T)0.0;
180  }
181  double operator*(Vector<T,0> const& b)
182  {
183  double s = 0;
184  for (unsigned i=0; i < this->sz; ++i)
185  s += (*this)[i] * b[i];
186  return s;
187  }
188 };
189 
194 template <class T>
195 class Vector3 : public Vector<T,3>
196 {
197  public:
199  Vector3() {}
201  Vector3(Vector<T,3> const& other) : Vector<T,3>(other) {};
203  Vector3(T const& a, T const& b, T const& c)
204  {
205  (*this)[0] = a;
206  (*this)[1] = b;
207  (*this)[2] = c;
208  }
210  Vector3(T const* abc)
211  {
212  (*this)[0] = abc[0];
213  (*this)[1] = abc[1];
214  (*this)[2] = abc[2];
215  }
217  void toArray(T* abc) const
218  {
219  abc[0] = (*this)[0];
220  abc[1] = (*this)[1];
221  abc[2] = (*this)[2];
222  }
224  void fromArray(T const* abc)
225  {
226  (*this)[0] = abc[0];
227  (*this)[1] = abc[1];
228  (*this)[2] = abc[2];
229  }
231  T& x() {return (*this[0]);}
233  T& y() {return (*this[1]);}
235  T& z() {return (*this[2]);}
237  T const& x() const {return (*this)[0];}
239  T const& y() const {return (*this)[1];}
241  T const& z() const {return (*this)[2];}
242 };
243 
244 }
245 
246 template <class T, unsigned N>
247 std::ostream& operator<<(std::ostream& s, mth::Vector<T,N> const& a)
248 {
249  for (unsigned i=0; i < a.size(); ++i)
250  s << a[i] << ' ';
251  s << '\n';
252  return s;
253 }
254 
255 #endif
Vector< T, N > normalize() const
divide the vector by its magnitude
Definition: mthVector.h:114
T getLength() const
get the vector magnitude
Definition: mthVector.h:174
T & y()
mutable y component
Definition: mthVector.h:233
void zero()
zero the vector
Definition: mthVector.h:176
T const & x() const
immutable x component
Definition: mthVector.h:237
Vector< T, 0 > & operator+=(Vector< T, 0 > const &b)
add a vector to this vector
Definition: mthVector.h:146
T getLength() const
get the vector magnitude
Definition: mthVector.h:112
compile-time (static) vector of size N
Definition: mthVector.h:29
Vector< T, 0 > & operator-=(Vector< T, 0 > const &b)
subtract a vector from this vector
Definition: mthVector.h:153
Vector< T, N > operator*(T const &s) const
multiply a vector times a scalar
Definition: mthVector.h:84
convenience wrapper over apf::Vector&lt;3&gt;
Definition: mthVector.h:195
Vector()
default constructor - no allocation
Definition: mthVector.h:136
Array()
default constructor - necessary
Definition: canArray.h:27
T const & operator()(unsigned i) const
immutable index operator
Definition: mthVector.h:49
T & x()
mutable x component
Definition: mthVector.h:231
T operator*(Vector< T, N > const &b) const
vector dot product
Definition: mthVector.h:104
Vector< T, N > operator+(Vector< T, N > const &b) const
add two vectors
Definition: mthVector.h:58
void fromArray(T const *abc)
read vector from array
Definition: mthVector.h:224
Vector3(Vector< T, 3 > const &other)
copy constructor
Definition: mthVector.h:201
Vector< T, N > & operator-=(Vector< T, N > const &b)
subtract a vector from this vector
Definition: mthVector.h:66
compile-time size array
Vector< T, N > operator/(T const &s) const
divide a vector by a scalar
Definition: mthVector.h:93
compile-time (static) array of size N
Definition: canArray.h:23
Vector< T, 0 > & operator*=(T const &s)
multiply this vector by a scalar
Definition: mthVector.h:160
Vector< T, N > operator-(Vector< T, N > const &b) const
subtract two vectors
Definition: mthVector.h:73
Vector3()
default constructor
Definition: mthVector.h:199
T & z()
mutable z component
Definition: mthVector.h:235
Vector< T, N > & operator+=(Vector< T, N > const &b)
add a vector to this vector
Definition: mthVector.h:51
void zero()
zero the vector
Definition: mthVector.h:116
double sqrt(double A)
wrapper for standard sqrt function
Definition: mthAD.h:588
T const & operator()(unsigned i) const
immutable index operator
Definition: mthVector.h:144
T const & y() const
immutable y component
Definition: mthVector.h:239
Vector(T const *v)
construct from an array
Definition: mthVector.h:39
T & operator()(unsigned i)
mutable index operator
Definition: mthVector.h:142
Vector(unsigned n)
construct with n elems
Definition: mthVector.h:37
T const & z() const
immutable z component
Definition: mthVector.h:241
Vector< T, 0 > & operator/=(T const &s)
divide this vector by a scalar
Definition: mthVector.h:167
Vector3(T const *abc)
construct from an array
Definition: mthVector.h:210
Vector()
default constructor
Definition: mthVector.h:33
run-time (dynamic) vector
Definition: mthVector.h:132
T & operator()(unsigned i)
mutable index operator
Definition: mthVector.h:47
Vector(unsigned n)
construct with n elements
Definition: mthVector.h:138
void toArray(T *abc) const
write vector to array
Definition: mthVector.h:217
Vector3(T const &a, T const &b, T const &c)
construct from 3 values
Definition: mthVector.h:203