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 <utility> // std::move
22#include <cmath> // sqrt
23#include <stdexcept> // std::runtime_error
24
25namespace nnp
26{
27
29struct Vec3D
30{
32 double r[3];
33
36 Vec3D();
39 Vec3D(double x, double y, double z);
42 Vec3D(Vec3D const& source);
45 Vec3D(Vec3D&& source);
50 Vec3D& operator=(Vec3D const& rhs);
55 Vec3D& operator+=(Vec3D const& v);
60 Vec3D& operator-=(Vec3D const& v);
65 Vec3D& operator*=(double const a);
70 Vec3D& operator/=(double const a);
75 double operator*(Vec3D const& v) const;
82 Vec3D& operator*=(Vec3D const (& A) [3]);
87 double& operator[](std::size_t const index);
92 double const& operator[](std::size_t const index) const;
97 bool operator==(Vec3D const& rhs) const;
102 bool operator!=(Vec3D const& rhs) const;
107 double norm() const;
112 double norm2() const;
117 double l1norm() const;
120 Vec3D& normalize();
125 Vec3D cross(Vec3D const& v) const;
126};
127
132Vec3D operator+(Vec3D lhs, Vec3D const& rhs);
137Vec3D operator-(Vec3D lhs, Vec3D const& rhs);
147Vec3D operator*(Vec3D v, double const a);
152Vec3D operator*(Vec3D const (& A) [3], Vec3D v);
157Vec3D operator/(Vec3D v, double const a);
162Vec3D operator*(double const a, Vec3D v);
163
165// Inlined function definitions //
167
169{
170 r[0] = 0.0;
171 r[1] = 0.0;
172 r[2] = 0.0;
173}
174
175inline Vec3D::Vec3D(double x, double y, double z)
176{
177 r[0] = x;
178 r[1] = y;
179 r[2] = z;
180}
181
182inline Vec3D::Vec3D(Vec3D const& source)
183{
184 r[0] = source.r[0];
185 r[1] = source.r[1];
186 r[2] = source.r[2];
187}
188
189inline Vec3D::Vec3D(Vec3D&& source)
190{
191 r[0] = std::move(source.r[0]);
192 r[1] = std::move(source.r[1]);
193 r[2] = std::move(source.r[2]);
194}
195
196inline Vec3D& Vec3D::operator=(Vec3D const& rhs)
197{
198 r[0] = rhs.r[0];
199 r[1] = rhs.r[1];
200 r[2] = rhs.r[2];
201
202 return *this;
203}
204
205inline Vec3D& Vec3D::operator+=(Vec3D const& rhs)
206{
207 r[0] += rhs.r[0];
208 r[1] += rhs.r[1];
209 r[2] += rhs.r[2];
210
211 return *this;
212}
213
214inline Vec3D& Vec3D::operator-=(Vec3D const& rhs)
215{
216 r[0] -= rhs.r[0];
217 r[1] -= rhs.r[1];
218 r[2] -= rhs.r[2];
219
220 return *this;
221}
222
223inline Vec3D& Vec3D::operator*=(double const a)
224{
225 r[0] *= a;
226 r[1] *= a;
227 r[2] *= a;
228
229 return *this;
230}
231
232inline Vec3D& Vec3D::operator*=(Vec3D const (& A) [3])
233{
234 Vec3D w;
235 for (size_t i=0; i<3; ++i)
236 {
237 for (size_t j=0; j<3; ++j)
238 {
239 w.r[i] += A[j][i] * r[j];
240 }
241 }
242 *this = w;
243 return *this;
244}
245
246inline Vec3D& Vec3D::operator/=(double const a)
247{
248 *this *= 1.0 / a;
249
250 return *this;
251}
252
253inline double Vec3D::operator*(Vec3D const& v) const
254{
255 return r[0] * v.r[0] + r[1] * v.r[1] + r[2] * v.r[2];
256}
257
258
259// Doxygen requires namespace prefix for arguments...
260inline double& Vec3D::operator[](std::size_t const index)
261{
262 if (index < 3) return r[index];
263 else
264 {
265 throw std::runtime_error("ERROR: 3D vector has only three"
266 " components.\n");
267 }
268}
269
270// Doxygen requires namespace prefix for arguments...
271inline double const& Vec3D::operator[](std::size_t const index) const
272{
273 if (index < 3) return r[index];
274 else
275 {
276 throw std::runtime_error("ERROR: 3D vector has only three"
277 " components.\n");
278 }
279}
280
281inline bool Vec3D::operator==(Vec3D const& rhs) const
282{
283 if (r[0] != rhs.r[0]) return false;
284 if (r[1] != rhs.r[1]) return false;
285 if (r[2] != rhs.r[2]) return false;
286 return true;
287}
288
289inline bool Vec3D::operator!=(Vec3D const& rhs) const
290{
291 return !(*this == rhs);
292}
293
294inline double Vec3D::norm() const
295{
296 return sqrt(r[0] * r[0] + r[1] * r[1] + r[2] * r[2]);
297}
298
299inline double Vec3D::norm2() const
300{
301 return r[0] * r[0] + r[1] * r[1] + r[2] * r[2];
302}
303
304inline double Vec3D::l1norm() const
305{
306 return fabs(r[0]) + fabs(r[1]) + fabs(r[2]);
307}
308
310{
311 double n = norm();
312 r[0] /= n;
313 r[1] /= n;
314 r[2] /= n;
315
316 return *this;
317}
318
319inline Vec3D Vec3D::cross(Vec3D const& v) const
320{
321 Vec3D w;
322
323 w.r[0] = r[1] * v.r[2] - r[2] * v.r[1];
324 w.r[1] = r[2] * v.r[0] - r[0] * v.r[2];
325 w.r[2] = r[0] * v.r[1] - r[1] * v.r[0];
326
327 return w;
328}
329
330inline Vec3D operator+(Vec3D lhs, Vec3D const& rhs)
331{
332 return lhs += rhs;
333}
334
335inline Vec3D operator-(Vec3D lhs, Vec3D const& rhs)
336{
337 return lhs -= rhs;
338}
339
341{
342 v *= -1.0;
343 return v;
344}
345
346inline Vec3D operator*(Vec3D v, double const a)
347{
348 return v *= a;
349}
350
351inline Vec3D operator/(Vec3D v, double const a)
352{
353 return v /= a;
354}
355
356inline Vec3D operator*(double const a, Vec3D v)
357{
358 return v *= a;
359}
360
361inline Vec3D operator*(Vec3D const (& A) [3], Vec3D v)
362{
363 return v *= A;
364}
365
366}
367
368#endif
Definition: Atom.h:29
Vec3D operator+(Vec3D lhs, Vec3D const &rhs)
Overload + operator to implement vector addition.
Definition: Vec3D.h:330
Vec3D operator*(Vec3D v, double const a)
Overload * operator to implement multiplication with scalar.
Definition: Vec3D.h:346
Vec3D operator-(Vec3D lhs, Vec3D const &rhs)
Overload - operator to implement vector subtraction.
Definition: Vec3D.h:335
Vec3D operator/(Vec3D v, double const a)
Overload / operator to implement division by scalar.
Definition: Vec3D.h:351
Vector in 3 dimensional real space.
Definition: Vec3D.h:30
bool operator==(Vec3D const &rhs) const
Compare if vectors are equal.
Definition: Vec3D.h:281
Vec3D & operator-=(Vec3D const &v)
Overload -= operator to implement in-place vector subtraction.
Definition: Vec3D.h:214
bool operator!=(Vec3D const &rhs) const
Compare if vectors are not equal.
Definition: Vec3D.h:289
double norm2() const
Calculate square of norm of vector.
Definition: Vec3D.h:299
double r[3]
cartesian coordinates.
Definition: Vec3D.h:32
Vec3D()
Constructor, initializes to zero.
Definition: Vec3D.h:168
Vec3D & normalize()
Normalize vector, norm equals 1.0 afterwards.
Definition: Vec3D.h:309
Vec3D & operator=(Vec3D const &rhs)
Overload = operator.
Definition: Vec3D.h:196
double l1norm() const
Calculate l1 norm of vector (taxicab metric).
Definition: Vec3D.h:304
Vec3D & operator/=(double const a)
Overload /= operator to implement division by scalar.
Definition: Vec3D.h:246
double operator*(Vec3D const &v) const
Overload * operator to implement scalar product.
Definition: Vec3D.h:253
double & operator[](std::size_t const index)
Overload [] operator to return coordinate by index.
Definition: Vec3D.h:260
Vec3D & operator+=(Vec3D const &v)
Overload += operator to implement in-place vector addition.
Definition: Vec3D.h:205
Vec3D cross(Vec3D const &v) const
Cross product, argument vector is second in product.
Definition: Vec3D.h:319
Vec3D & operator*=(double const a)
Overload *= operator to implement multiplication with scalar.
Definition: Vec3D.h:223
double norm() const
Calculate norm of vector.
Definition: Vec3D.h:294