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 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 <cmath>
33 #include <sstream>
34 
35 namespace psi {
36 
37 class Vector3
38 {
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]; v_[1] = p[1]; v_[2] = p[2];
46  }
47  Vector3(double d) {
48  v_[0] = v_[1] = v_[2] = d;
49  }
50  Vector3(double x, double y, double z) {
51  v_[0] = x; v_[1] = y; v_[2] = z;
52  }
53  Vector3(const Vector3& p) {
54  v_[0] = p.v_[0]; v_[1] = p.v_[1]; v_[2] = p.v_[2];
55  }
56 
57  void operator=(const double *x) {
58  v_[0] = x[0];
59  v_[1] = x[1];
60  v_[2] = x[2];
61  }
62  void operator=(const Vector3& x) {
63  v_[0] = x.v_[0];
64  v_[1] = x.v_[1];
65  v_[2] = x.v_[2];
66  }
67  void operator=(double d) {
68  v_[0] = d;
69  v_[1] = d;
70  v_[2] = d;
71  }
72  void operator+=(const Vector3& x) {
73  v_[0] += x.v_[0];
74  v_[1] += x.v_[1];
75  v_[2] += x.v_[2];
76  }
77  void operator-=(const Vector3& x) {
78  v_[0] -= x.v_[0];
79  v_[1] -= x.v_[1];
80  v_[2] -= x.v_[2];
81  }
82  void operator*=(double m) {
83  v_[0] *= m;
84  v_[1] *= m;
85  v_[2] *= m;
86  }
87  void operator/=(double m) {
88  v_[0] /= m;
89  v_[1] /= m;
90  v_[2] /= m;
91  }
92 
93  Vector3 operator*(double d) const;
94  Vector3 operator/(double d) const;
96  Vector3 result;
97  result.v_[0] = v_[0] + x.v_[0];
98  result.v_[1] = v_[1] + x.v_[1];
99  result.v_[2] = v_[2] + x.v_[2];
100  return result;
101  }
102  Vector3 operator-(const Vector3& x) const {
103  Vector3 result;
104  result.v_[0] = v_[0] - x.v_[0];
105  result.v_[1] = v_[1] - x.v_[1];
106  result.v_[2] = v_[2] - x.v_[2];
107  return result;
108  }
109  Vector3 operator*(const Vector3& x) const {
110  Vector3 result;
111  result.v_[0] = v_[0] * x.v_[0];
112  result.v_[1] = v_[1] * x.v_[1];
113  result.v_[2] = v_[2] * x.v_[2];
114  return result;
115  }
116  Vector3 operator-() { return Vector3(-v_[0], -v_[1], -v_[2]); }
117 
118  double& operator[](int i) { return v_[i]; }
119  const double& operator[](int i) const { return v_[i]; }
120 
122  bool operator==(const Vector3& RHS)const
123  {
124  return (v_[0]==RHS.v_[0] && v_[1]==RHS.v_[1] && v_[2]==RHS.v_[2]);
125  }
126 
128  bool operator!=(const Vector3& RHS)const
129  {
130  return !(*this==RHS);
131  }
132 
133  double get(int i) {
134  if (i >= 0 && i <= 2) return v_[i];
135  else return 0.0;
136  }
137 
138  void set(int i, double val) {
139  if (i >= 0 && i <= 2) v_[i] = val;
140  }
141 
142  double dot(const Vector3& x) const {
143  return v_[0]*x.v_[0] + v_[1]*x.v_[1] + v_[2]*x.v_[2];
144  }
145 
146  double distance(const Vector3&) const;
147  void normalize();
148  double norm() const { return sqrt(this->dot(*this)); }
149  void rotate(double theta, Vector3& v);
150  Vector3 perp_unit(const Vector3& v) const;
151 
152  Vector3 cross(const Vector3&) const;
153 
154  std::string to_string() const {
155  std::stringstream s;
156  s << "[ " << v_[0] << ", " << v_[1] << ", " << v_[2] << " ]";
157  return s.str();
158  }
159 };
160 
161 Vector3 operator*(double, const Vector3&);
162 
163 #include "vector3.i"
164 
165 }
166 
167 #endif
void operator*=(double m)
Definition: vector3.h:82
Vector3 cross(const Vector3 &) const
Definition: vector3.h:75
void operator+=(const Vector3 &x)
Definition: vector3.h:72
void operator=(const Vector3 &x)
Definition: vector3.h:62
const double & operator[](int i) const
Definition: vector3.h:119
std::string to_string() const
Definition: vector3.h:154
void rotate(double theta, Vector3 &v)
Definition: vector3.h:83
Definition: vector3.h:37
Definition: pointgrp.h:106
Vector3 operator/(double d) const
Definition: vector3.h:46
Vector3 operator-()
Definition: vector3.h:116
Vector3(double d)
Definition: vector3.h:47
Vector3 operator*(double d) const
Definition: vector3.h:41
double & operator[](int i)
Definition: vector3.h:118
bool operator==(const Vector3 &RHS) const
Checks for exact equality (i.e. no tolerances)
Definition: vector3.h:122
Vector3 perp_unit(const Vector3 &v) const
Definition: vector3.h:101
Vector3 operator+(const Vector3 &x)
Definition: vector3.h:95
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:142
void operator-=(const Vector3 &x)
Definition: vector3.h:77
Vector3 operator-(const Vector3 &x) const
Definition: vector3.h:102
void operator=(const double *x)
Definition: vector3.h:57
Vector3()
Definition: vector3.h:43
void normalize()
Definition: vector3.h:63
Vector3 operator*(double, const Vector3 &)
Definition: vector3.h:32
double distance(const Vector3 &) const
Definition: vector3.h:55
double norm() const
Definition: vector3.h:148
bool operator!=(const Vector3 &RHS) const
True if the coordinates differ in any bit.
Definition: vector3.h:128
Vector3(double x, double y, double z)
Definition: vector3.h:50
Vector3 operator*(const Vector3 &x) const
Definition: vector3.h:109
void operator=(double d)
Definition: vector3.h:67
Vector3(const Vector3 &p)
Definition: vector3.h:53
void operator/=(double m)
Definition: vector3.h:87
void set(int i, double val)
Definition: vector3.h:138