Loading...
Searching...
No Matches
vector_3d.hpp
Go to the documentation of this file.
1#pragma once
2
3/**
4 * Three-dimensional real-valued vector type.
5 *
6 * Provides the `Vector3D` struct used throughout Q3 for positions, momenta,
7 * lattice vectors, and reciprocal space vectors. Supports basic linear
8 * algebra operations: dot product, cross product, norm, and element-wise
9 * arithmetic.
10 */
11
12#include <cmath>
13
15
16namespace q3 {
17
18// ---------------------------------------------------------------------------
19
20// 3D real-valued vector
21
22// ---------------------------------------------------------------------------
23struct [[nodiscard]] Vector3D {
24 Real x{}, y{}, z{};
25
26 Vector3D() = default;
27 constexpr Vector3D(Real x_component, Real y_component, Real z_component)
28 : x(x_component), y(y_component), z(z_component) {}
29
30 [[nodiscard]] Real operator[](int component_index) const {
31 return (component_index == 0) ? x : (component_index == 1) ? y : z;
32 }
33 Real& operator[](int component_index) {
34 return (component_index == 0) ? x : (component_index == 1) ? y : z;
35 }
36
37 [[nodiscard]] Vector3D operator+(const Vector3D& other) const {
38 return {x + other.x, y + other.y, z + other.z};
39 }
40 [[nodiscard]] Vector3D operator-(const Vector3D& other) const {
41 return {x - other.x, y - other.y, z - other.z};
42 }
43 [[nodiscard]] Vector3D operator*(Real scalar) const {
44 return {x * scalar, y * scalar, z * scalar};
45 }
46 [[nodiscard]] Vector3D operator/(Real scalar) const {
47 return {x / scalar, y / scalar, z / scalar};
48 }
49 Vector3D& operator+=(const Vector3D& other) {
50 x += other.x;
51 y += other.y;
52 z += other.z;
53 return *this;
54 }
55 [[nodiscard]] Vector3D operator-() const {
56 return {-x, -y, -z};
57 }
58
59 [[nodiscard]] Real dot(const Vector3D& other) const {
60 return x * other.x + y * other.y + z * other.z;
61 }
62 [[nodiscard]] Vector3D cross(const Vector3D& other) const {
63 return {y * other.z - z * other.y, z * other.x - x * other.z, x * other.y - y * other.x};
64 }
65 [[nodiscard]] Real norm2() const {
66 return dot(*this);
67 }
68 [[nodiscard]] Real norm() const {
69 return std::sqrt(norm2());
70 }
71};
72
73[[nodiscard]] inline Vector3D operator*(Real scalar, const Vector3D& vector) {
74 return vector * scalar;
75}
76
77} // namespace q3
double Real
Vector3D operator*(Real scalar, const Vector3D &vector)
Definition vector_3d.hpp:73
Vector3D operator*(Real scalar) const
Definition vector_3d.hpp:43
Vector3D operator-() const
Definition vector_3d.hpp:55
Vector3D cross(const Vector3D &other) const
Definition vector_3d.hpp:62
Real norm2() const
Definition vector_3d.hpp:65
Real norm() const
Definition vector_3d.hpp:68
Vector3D operator+(const Vector3D &other) const
Definition vector_3d.hpp:37
Real operator[](int component_index) const
Definition vector_3d.hpp:30
Vector3D operator-(const Vector3D &other) const
Definition vector_3d.hpp:40
Vector3D & operator+=(const Vector3D &other)
Definition vector_3d.hpp:49
Vector3D operator/(Real scalar) const
Definition vector_3d.hpp:46
constexpr Vector3D(Real x_component, Real y_component, Real z_component)
Definition vector_3d.hpp:27
Vector3D()=default
Real & operator[](int component_index)
Definition vector_3d.hpp:33
Real dot(const Vector3D &other) const
Definition vector_3d.hpp:59