n2p2 - A neural network potential package
Vec3D.h
Go to the documentation of this file.
1// n2p2 - A neural network potential package
2// Copyright (C) 2018 Andreas Singraber (University of Vienna)
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17#ifndef VEC3D_H
18#define VEC3D_H
19
20#include <cstddef> // std::size_t
21#include <cmath> // sqrt
22#include <stdexcept> // std::runtime_error
23
24namespace nnp
25{
26
28struct Vec3D
29{
31 double r[3];
32
35 Vec3D();
38 Vec3D(double x, double y, double z);
41 Vec3D(Vec3D const& source);
46 Vec3D& operator=(Vec3D const& rhs);
51 Vec3D& operator+=(Vec3D const& v);
56 Vec3D& operator-=(Vec3D const& v);
61 Vec3D& operator*=(double const a);
66 Vec3D& operator/=(double const a);
71 double operator*(Vec3D const& v) const;
76 double& operator[](std::size_t const index);
81 double const& operator[](std::size_t const index) const;
86 bool operator==(Vec3D const& rhs) const;
91 bool operator!=(Vec3D const& rhs) const;
96 double norm() const;
101 double norm2() const;
106 double l1norm() const;
109 Vec3D& normalize();
114 Vec3D cross(Vec3D const& v) const;
115};
116
121Vec3D operator+(Vec3D lhs, Vec3D const& rhs);
126Vec3D operator-(Vec3D lhs, Vec3D const& rhs);
136Vec3D operator*(Vec3D v, double const a);
141Vec3D operator/(Vec3D v, double const a);
146Vec3D operator*(double const a, Vec3D v);
147
149// Inlined function definitions //
151
153{
154 r[0] = 0.0;
155 r[1] = 0.0;
156 r[2] = 0.0;
157}
158
159inline Vec3D::Vec3D(double x, double y, double z)
160{
161 r[0] = x;
162 r[1] = y;
163 r[2] = z;
164}
165
166inline Vec3D::Vec3D(Vec3D const& source)
167{
168 r[0] = source.r[0];
169 r[1] = source.r[1];
170 r[2] = source.r[2];
171}
172
173inline Vec3D& Vec3D::operator=(Vec3D const& rhs)
174{
175 r[0] = rhs.r[0];
176 r[1] = rhs.r[1];
177 r[2] = rhs.r[2];
178
179 return *this;
180}
181
182inline Vec3D& Vec3D::operator+=(Vec3D const& rhs)
183{
184 r[0] += rhs.r[0];
185 r[1] += rhs.r[1];
186 r[2] += rhs.r[2];
187
188 return *this;
189}
190
191inline Vec3D& Vec3D::operator-=(Vec3D const& rhs)
192{
193 r[0] -= rhs.r[0];
194 r[1] -= rhs.r[1];
195 r[2] -= rhs.r[2];
196
197 return *this;
198}
199
200inline Vec3D& Vec3D::operator*=(double const a)
201{
202 r[0] *= a;
203 r[1] *= a;
204 r[2] *= a;
205
206 return *this;
207}
208
209inline Vec3D& Vec3D::operator/=(double const a)
210{
211 *this *= 1.0 / a;
212
213 return *this;
214}
215
216inline double Vec3D::operator*(Vec3D const& v) const
217{
218 return r[0] * v.r[0] + r[1] * v.r[1] + r[2] * v.r[2];
219}
220
221// Doxygen requires namespace prefix for arguments...
222inline double& Vec3D::operator[](std::size_t const index)
223{
224 if (index < 3) return r[index];
225 else
226 {
227 throw std::runtime_error("ERROR: 3D vector has only three"
228 " components.\n");
229 }
230}
231
232// Doxygen requires namespace prefix for arguments...
233inline double const& Vec3D::operator[](std::size_t const index) const
234{
235 if (index < 3) return r[index];
236 else
237 {
238 throw std::runtime_error("ERROR: 3D vector has only three"
239 " components.\n");
240 }
241}
242
243inline bool Vec3D::operator==(Vec3D const& rhs) const
244{
245 if (r[0] != rhs.r[0]) return false;
246 if (r[1] != rhs.r[1]) return false;
247 if (r[2] != rhs.r[2]) return false;
248 return true;
249}
250
251inline bool Vec3D::operator!=(Vec3D const& rhs) const
252{
253 return !(*this == rhs);
254}
255
256inline double Vec3D::norm() const
257{
258 return sqrt(r[0] * r[0] + r[1] * r[1] + r[2] * r[2]);
259}
260
261inline double Vec3D::norm2() const
262{
263 return r[0] * r[0] + r[1] * r[1] + r[2] * r[2];
264}
265
266inline double Vec3D::l1norm() const
267{
268 return fabs(r[0]) + fabs(r[1]) + fabs(r[2]);
269}
270
272{
273 double n = norm();
274 r[0] /= n;
275 r[1] /= n;
276 r[2] /= n;
277
278 return *this;
279}
280
281inline Vec3D Vec3D::cross(Vec3D const& v) const
282{
283 Vec3D w;
284
285 w.r[0] = r[1] * v.r[2] - r[2] * v.r[1];
286 w.r[1] = r[2] * v.r[0] - r[0] * v.r[2];
287 w.r[2] = r[0] * v.r[1] - r[1] * v.r[0];
288
289 return w;
290}
291
292inline Vec3D operator+(Vec3D lhs, Vec3D const& rhs)
293{
294 return lhs += rhs;
295}
296
297inline Vec3D operator-(Vec3D lhs, Vec3D const& rhs)
298{
299 return lhs -= rhs;
300}
301
303{
304 v *= -1.0;
305 return v;
306}
307
308inline Vec3D operator*(Vec3D v, double const a)
309{
310 return v *= a;
311}
312
313inline Vec3D operator/(Vec3D v, double const a)
314{
315 return v /= a;
316}
317
318inline Vec3D operator*(double const a, Vec3D v)
319{
320 return v *= a;
321}
322
323}
324
325#endif
Definition: Atom.h:28
Vec3D operator+(Vec3D lhs, Vec3D const &rhs)
Overload + operator to implement vector addition.
Definition: Vec3D.h:292
Vec3D operator*(Vec3D v, double const a)
Overload * operator to implement multiplication with scalar.
Definition: Vec3D.h:308
Vec3D operator-(Vec3D lhs, Vec3D const &rhs)
Overload - operator to implement vector subtraction.
Definition: Vec3D.h:297
Vec3D operator/(Vec3D v, double const a)
Overload / operator to implement division by scalar.
Definition: Vec3D.h:313
Vector in 3 dimensional real space.
Definition: Vec3D.h:29
bool operator==(Vec3D const &rhs) const
Compare if vectors are equal.
Definition: Vec3D.h:243
Vec3D & operator-=(Vec3D const &v)
Overload -= operator to implement in-place vector subtraction.
Definition: Vec3D.h:191
bool operator!=(Vec3D const &rhs) const
Compare if vectors are not equal.
Definition: Vec3D.h:251
double norm2() const
Calculate square of norm of vector.
Definition: Vec3D.h:261
double r[3]
cartesian coordinates.
Definition: Vec3D.h:31
Vec3D()
Constructor, initializes to zero.
Definition: Vec3D.h:152
Vec3D & normalize()
Normalize vector, norm equals 1.0 afterwards.
Definition: Vec3D.h:271
Vec3D & operator=(Vec3D const &rhs)
Overload = operator.
Definition: Vec3D.h:173
double l1norm() const
Calculate l1 norm of vector (taxicab metric).
Definition: Vec3D.h:266
Vec3D & operator/=(double const a)
Overload /= operator to implement division by scalar.
Definition: Vec3D.h:209
double operator*(Vec3D const &v) const
Overload * operator to implement scalar product.
Definition: Vec3D.h:216
double & operator[](std::size_t const index)
Overload [] operator to return coordinate by index.
Definition: Vec3D.h:222
Vec3D & operator+=(Vec3D const &v)
Overload += operator to implement in-place vector addition.
Definition: Vec3D.h:182
Vec3D cross(Vec3D const &v) const
Cross product, argument vector is second in product.
Definition: Vec3D.h:281
Vec3D & operator*=(double const a)
Overload *= operator to implement multiplication with scalar.
Definition: Vec3D.h:200
double norm() const
Calculate norm of vector.
Definition: Vec3D.h:256