Psi4
vector3.h
Go to the documentation of this file.
1 /*
2  * @BEGIN LICENSE
3  *
4  * Psi4: an open-source quantum chemistry software package
5  *
6  * Copyright (c) 2007-2018 The Psi4 Developers.
7  *
8  * The copyrights for code used from other parties are included in
9  * the corresponding files.
10  *
11  * This file is part of Psi4.
12  *
13  * Psi4 is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU Lesser General Public License as published by
15  * the Free Software Foundation, version 3.
16  *
17  * Psi4 is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License along
23  * with Psi4; if not, write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  *
26  * @END LICENSE
27  */
28 
29 #ifndef _psi_src_lib_libmints__vector3_h
30 #define _psi_src_lib_libmints__vector3_h
31 
32 #include <array>
33 #include <cmath>
34 #include <sstream>
35 
36 namespace psi {
37 
38 class Vector3 {
39  private:
40  double v_[3];
41 
42  public:
43  Vector3() { v_[0] = v_[1] = v_[2] = 0.0; }
44  Vector3(const double p[3]) {
45  v_[0] = p[0];
46  v_[1] = p[1];
47  v_[2] = p[2];
48  }
49  Vector3(double d) { v_[0] = v_[1] = v_[2] = d; }
50  Vector3(double x, double y, double z) {
51  v_[0] = x;
52  v_[1] = y;
53  v_[2] = z;
54  }
55  Vector3(const Vector3& p) {
56  v_[0] = p.v_[0];
57  v_[1] = p.v_[1];
58  v_[2] = p.v_[2];
59  }
60  Vector3(const std::array<double, 3> p) {
61  v_[0] = p[0];
62  v_[1] = p[1];
63  v_[2] = p[2];
64  }
65 
66  void operator=(const double* x) {
67  v_[0] = x[0];
68  v_[1] = x[1];
69  v_[2] = x[2];
70  }
71  void operator=(const Vector3& x) {
72  v_[0] = x.v_[0];
73  v_[1] = x.v_[1];
74  v_[2] = x.v_[2];
75  }
76  void operator=(double d) {
77  v_[0] = d;
78  v_[1] = d;
79  v_[2] = d;
80  }
81  void operator+=(const Vector3& x) {
82  v_[0] += x.v_[0];
83  v_[1] += x.v_[1];
84  v_[2] += x.v_[2];
85  }
86  void operator-=(const Vector3& x) {
87  v_[0] -= x.v_[0];
88  v_[1] -= x.v_[1];
89  v_[2] -= x.v_[2];
90  }
91  void operator*=(double m) {
92  v_[0] *= m;
93  v_[1] *= m;
94  v_[2] *= m;
95  }
96  void operator/=(double m) {
97  v_[0] /= m;
98  v_[1] /= m;
99  v_[2] /= m;
100  }
101 
102  Vector3 operator*(double d) const;
103  Vector3 operator/(double d) const;
105  Vector3 result;
106  result.v_[0] = v_[0] + x.v_[0];
107  result.v_[1] = v_[1] + x.v_[1];
108  result.v_[2] = v_[2] + x.v_[2];
109  return result;
110  }
111  Vector3 operator-(const Vector3& x) const {
112  Vector3 result;
113  result.v_[0] = v_[0] - x.v_[0];
114  result.v_[1] = v_[1] - x.v_[1];
115  result.v_[2] = v_[2] - x.v_[2];
116  return result;
117  }
118  Vector3 operator*(const Vector3& x) const {
119  Vector3 result;
120  result.v_[0] = v_[0] * x.v_[0];
121  result.v_[1] = v_[1] * x.v_[1];
122  result.v_[2] = v_[2] * x.v_[2];
123  return result;
124  }
125  Vector3 operator-() { return Vector3(-v_[0], -v_[1], -v_[2]); }
126 
127  double& operator[](int i) { return v_[i]; }
128  const double& operator[](int i) const { return v_[i]; }
129 
131  bool operator==(const Vector3& RHS) const {
132  return (v_[0] == RHS.v_[0] && v_[1] == RHS.v_[1] && v_[2] == RHS.v_[2]);
133  }
134 
136  bool operator!=(const Vector3& RHS) const { return !(*this == RHS); }
137 
138  double get(int i) {
139  if (i >= 0 && i <= 2)
140  return v_[i];
141  else
142  return 0.0;
143  }
144 
145  void set(int i, double val) {
146  if (i >= 0 && i <= 2) v_[i] = val;
147  }
148 
149  double dot(const Vector3& x) const { return v_[0] * x.v_[0] + v_[1] * x.v_[1] + v_[2] * x.v_[2]; }
150 
151  double distance(const Vector3&) const;
152  void normalize();
153  double norm() const { return sqrt(this->dot(*this)); }
154  void rotate(double theta, Vector3& v);
155  Vector3 perp_unit(const Vector3& v) const;
156 
157  Vector3 cross(const Vector3&) const;
158 
159  std::string to_string() const {
160  std::stringstream s;
161  s << "[ " << v_[0] << ", " << v_[1] << ", " << v_[2] << " ]";
162  return s.str();
163  }
164 };
165 
166 Vector3 operator*(double, const Vector3&);
167 
168 #include "vector3.i"
169 
170 } // namespace psi
171 
172 #endif
void operator*=(double m)
Definition: vector3.h:91
Vector3 cross(const Vector3 &) const
Definition: vector3.h:66
void operator+=(const Vector3 &x)
Definition: vector3.h:81
void operator=(const Vector3 &x)
Definition: vector3.h:71
const double & operator[](int i) const
Definition: vector3.h:128
std::string to_string() const
Definition: vector3.h:159
void rotate(double theta, Vector3 &v)
Definition: vector3.h:72
Definition: vector3.h:38
Definition: pointgrp.h:104
Vector3 operator/(double d) const
Definition: vector3.h:42
Vector3 operator-()
Definition: vector3.h:125
Vector3(double d)
Definition: vector3.h:49
Vector3 operator*(double d) const
Definition: vector3.h:40
double & operator[](int i)
Definition: vector3.h:127
bool operator==(const Vector3 &RHS) const
Checks for exact equality (i.e. no tolerances)
Definition: vector3.h:131
Vector3 perp_unit(const Vector3 &v) const
Definition: vector3.h:89
Vector3 operator+(const Vector3 &x)
Definition: vector3.h:104
double v_[3]
Definition: vector3.h:40
Vector3(const double p[3])
Definition: vector3.h:44
double dot(const Vector3 &x) const
Definition: vector3.h:149
void operator-=(const Vector3 &x)
Definition: vector3.h:86
Vector3 operator-(const Vector3 &x) const
Definition: vector3.h:111
void operator=(const double *x)
Definition: vector3.h:66
Vector3()
Definition: vector3.h:43
void normalize()
Definition: vector3.h:57
Vector3 operator*(double, const Vector3 &)
Definition: vector3.h:32
double distance(const Vector3 &) const
Definition: vector3.h:50
Vector3(const std::array< double, 3 > p)
Definition: vector3.h:60
double norm() const
Definition: vector3.h:153
bool operator!=(const Vector3 &RHS) const
True if the coordinates differ in any bit.
Definition: vector3.h:136
Vector3(double x, double y, double z)
Definition: vector3.h:50
Vector3 operator*(const Vector3 &x) const
Definition: vector3.h:118
void operator=(double d)
Definition: vector3.h:76
Vector3(const Vector3 &p)
Definition: vector3.h:55
void operator/=(double m)
Definition: vector3.h:96
void set(int i, double val)
Definition: vector3.h:145