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