Vector.h
Go to the documentation of this file.
1 /*
2  This file is part of the Ristra wonton project.
3  Please see the license file at the root of this repository, or at:
4  https://github.com/laristra/wonton/blob/master/LICENSE
5 */
6 
8 // Copyright 2016 Los Alamos National Laboratory //
9 // //
10 // Original Author: Paul Henning, from the SCF library. //
11 // //
12 // Modified for GK by: Brian Jean //
13 // TSM, ASCI Problem Setup Team //
14 // Applied Physics Division //
15 // Los Alamos National Laboratory //
16 // 505.665.6374 //
17 // baj@lanl.gov //
18 // //
19 // Modified for Wonton by: Rao Garimella, rao@lanl.gov //
21 
22 #ifndef WONTON_VECTOR_H_
23 #define WONTON_VECTOR_H_
24 
25 #include <cmath>
26 #include <cassert>
27 #include <iostream>
28 #include <vector>
29 #include <limits>
30 #include "wonton/support/wonton.h"
31 
32 namespace Wonton {
33 
40 template <int D> class Vector {
41  private:
42  double m_comp[D] {};
43 
44  public:
45 
48  Vector() {
49  for (int i = 0; i < D; i++)
50  m_comp[i] = 0.0;
51  }
52 
58  Vector(const double& xm_comp) {
59  for (int i = 0; i < D; i++)
60  m_comp[i] = xm_comp;
61  }
62 
68  Vector(const double& xm_comp, const double& ym_comp) {
69  assert(D == 2);
70  m_comp[0] = xm_comp;
71  m_comp[1] = ym_comp;
72  }
73 
79  Vector(const double& xm_comp, const double& ym_comp, const double& zm_comp) {
80  assert(D == 3);
81  m_comp[0] = xm_comp;
82  m_comp[1] = ym_comp;
83  m_comp[2] = zm_comp;
84  }
85 
89  inline Vector(std::vector<double> const& invec) {
90  assert(D == invec.size());
91  for (int i = 0; i < D; i++) m_comp[i] = invec[i];
92  }
93 
96  const double& operator[](const int& i) const {
97  return m_comp[i];
98  }
99 
102  double& operator[](const int& i) {
103  return m_comp[i];
104  }
105 
108  Vector operator-() const {
109  Vector v;
110  for (int i = 0; i < D; i++) v.m_comp[i] = -m_comp[i];
111  return v;
112  }
113 
116  Vector& operator+=(const Vector<D>& rhs) {
117  for (int i = 0; i < D; i++) m_comp[i] += rhs.m_comp[i];
118  return *this;
119  }
120 
123  Vector& operator-=(const Vector<D>& rhs) {
124  for (int i = 0; i < D; i++) m_comp[i] -= rhs.m_comp[i];
125  return *this;
126  }
127 
130  Vector& operator*=(const double& s) {
131  for (int i = 0; i < D; i++) m_comp[i] *= s;
132  return *this;
133  }
134 
137  Vector& operator/=(const double& s) {
138  for (int i = 0; i < D; i++) m_comp[i] /= s;
139  return *this;
140  }
141 
148  double norm(bool doSqrt = true) const {
149  double result = 0.0;
150  for (int i = 0; i < D; i++) result += (m_comp[i] * m_comp[i]);
151  return doSqrt ? sqrt(result) : result;
152  }
153 
158  double one_norm() const {
159  double result = 0.0;
160  for (int i = 0; i < D; i++) result += std::fabs(m_comp[i]);
161  return result;
162  }
163 
168  double max_norm() const {
169  double result = std::fabs(m_comp[0]);
170  for (int i = 0; i < D - 1; i++) {
171  double abs_val = std::fabs(m_comp[i + 1]);
172  if (result < abs_val)
173  result = abs_val;
174  }
175  return result;
176  }
177 
180  void normalize() {
181  double s = norm();
182  *this /= s;
183  }
184 
187  void zero() {
188  for (int i = 0; i < D; i++) m_comp[i] = 0;
189  }
190 
197  bool is_zero(double dst_tol) const {
198  return (norm() < dst_tol);
199  }
200 
207  void fill(double val) {
208  for (int i = 0; i < D; i++) m_comp[i] = val;
209  }
210 
217  void axis(int nonZero) {
218  zero();
219  m_comp[nonZero] = 1;
220  }
221 
223  std::istream& readFromStream(std::istream& is) {
224  for (int i = 0; i < D; i++)
225  is >> m_comp[i];
226  return is;
227  }
228 
230  std::ostream& writeToStream(std::ostream& os) const {
231  for (int i = 0; i < D; i++) {
232  if (i > 0) os << ' ';
233  os << m_comp[i];
234  }
235  return os;
236  }
237 };
238 
241 
244 
246 template<int D>
248 double dot(const Vector<D>& a, const Vector<D>& b) {
249  double r = 0.0;
250  for (int i = 0; i < D; i++) r += a[i] * b[i];
251  return r;
252 }
253 
255 template<int D>
257 Vector<D> operator+(const Vector<D>& a, const Vector<D>& b) {
258  return Vector<D>(a) += b;
259 }
260 
262 template<int D>
264 Vector<D> operator-(const Vector<D>& a, const Vector<D>& b) {
265  return Vector<D>(a) -= b;
266 }
267 
269 template<int D>
271 Vector<D> operator*(const Vector<D>& a, const double& s) {
272  return Vector<D>(a) *= s;
273 }
274 
276 template<int D>
278 Vector<D> operator*(const double& s, const Vector<D>& a) {
279  return Vector<D>(a) *= s;
280 }
281 
283 template<int D>
285 Vector<D> operator/(const Vector<D>& a, const double& s) {
286  return Vector<D>(a) /= s;
287 }
288 
290 template<int D>
291 inline
292 std::ostream& operator<<(std::ostream& os, const Vector<D>& v) {
293  return v.writeToStream(os);
294 }
295 
297 template<int D> inline std::istream&
298 operator>>(std::istream& is, Vector<D>& v) {
299  return v.readFromStream(is);
300 }
301 
304 double cross(const Vector<2>& a, const Vector<2>& b) {
305  return (a[0] * b[1] - a[1] * b[0]);
306 }
307 
310 Vector<3> cross(const Vector<3>& a, const Vector<3>& b) {
311  Vector<3> r;
312  r[0] = a[1] * b[2] - a[2] * b[1];
313  r[1] = a[2] * b[0] - a[0] * b[2];
314  r[2] = a[0] * b[1] - a[1] * b[0];
315  return r;
316 }
317 
324 template<int D>
326 double MaxComponent(const Vector<D>& v, int& icomp) {
327  double max = v[0];
328  icomp = 0;
329  for (int i = 1; i < D; i++)
330  if (max < v[i]) {
331  max = v[i];
332  icomp = i;
333  }
334  return max;
335 }
336 
337 } // namespace Wonton
338 
339 
340 #endif // WONTON_VECTOR_H_
WONTON_INLINE Vector(const double &xm_comp, const double &ym_comp)
Specialized constructor for 2d Vectors.
Definition: Vector.h:68
WONTON_INLINE bool is_zero(double dst_tol) const
Check if this Vector is a zero Vector.
Definition: Vector.h:197
WONTON_INLINE Vector & operator*=(const double &s)
Scalar multiplication of this Vector by s.
Definition: Vector.h:130
WONTON_INLINE Vector()
Default constructor - zero Vector in D-space.
Definition: Vector.h:48
Vector< 3 > Vector3
Alias for creating a 3D vector.
Definition: Vector.h:240
WONTON_INLINE double MaxComponent(const Vector< D > &v, int &icomp)
Obtain the value and index of the maximum component of a Vector.
Definition: Vector.h:326
WONTON_INLINE Vector(const double &xm_comp, const double &ym_comp, const double &zm_comp)
Specialized constructor for 3d Vectors.
Definition: Vector.h:79
WONTON_INLINE double cross(const Vector< 2 > &a, const Vector< 2 > &b)
Cross product operator for two 2d vectors, .
Definition: Vector.h:304
WONTON_INLINE double norm(bool doSqrt=true) const
Calculate the norm of a Vector.
Definition: Vector.h:148
WONTON_INLINE void fill(double val)
Convenience method for constructing a Vector with all components equal to a given value...
Definition: Vector.h:207
Factorize a number N into D equal (or nearly equal) factors.
Definition: adaptive_refinement_mesh.h:31
WONTON_INLINE Point< D > operator*(const Point< D > &p, double s)
Definition: Point.h:247
WONTON_INLINE Vector & operator/=(const double &s)
Scalar division of this Vector by s.
Definition: Vector.h:137
WONTON_INLINE double & operator[](const int &i)
Return component i of the Vector.
Definition: Vector.h:102
WONTON_INLINE Vector & operator-=(const Vector< D > &rhs)
Subtract the Vector rhs from this vector.
Definition: Vector.h:123
std::ostream & writeToStream(std::ostream &os) const
Pretty printing of a Vector to an output stream.
Definition: Vector.h:230
std::istream & readFromStream(std::istream &is)
Read in a Vector from an input stream.
Definition: Vector.h:223
WONTON_INLINE Point< D > operator/(const Point< D > &p, double s)
Definition: Point.h:259
WONTON_INLINE void normalize()
Convert this Vector into a unit Vector.
Definition: Vector.h:180
WONTON_INLINE Vector operator-() const
Negative of this vector.
Definition: Vector.h:108
Vector(std::vector< double > const &invec)
Constructor from a std:vector.
Definition: Vector.h:89
WONTON_INLINE void zero()
Convert this Vector into a zero Vector.
Definition: Vector.h:187
Vector< 2 > Vector2
Alias for creating a 3D vector.
Definition: Vector.h:243
WONTON_INLINE double max_norm() const
Calculate the max norm of a Vector.
Definition: Vector.h:168
WONTON_INLINE Vector(const double &xm_comp)
Initialize all components to same value (also constructor for 1d vectors)
Definition: Vector.h:58
WONTON_INLINE const double & operator[](const int &i) const
Return component i of the Vector.
Definition: Vector.h:96
std::istream & operator>>(std::istream &is, Vector< D > &v)
Read in a Vector from an input stream.
Definition: Vector.h:298
WONTON_INLINE void axis(int nonZero)
Convenience method for constructing a unit Vector along a particular axis.
Definition: Vector.h:217
#define WONTON_INLINE
Definition: wonton.h:235
WONTON_INLINE double one_norm() const
Calculate the 1-norm of a Vector.
Definition: Vector.h:158
WONTON_INLINE Point< D > operator+(const Point< D > &p, const Vector< D > &v)
Definition: Point.h:227
Represents a vector in N-dimensional space.
Definition: Vector.h:40
WONTON_INLINE double dot(const Vector< D > &a, const Vector< D > &b)
Dot product of two vectors, .
Definition: Vector.h:248
WONTON_INLINE Vector & operator+=(const Vector< D > &rhs)
Add the Vector rhs to this Vector.
Definition: Vector.h:116