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