sisi4s
Loading...
Searching...
No Matches
Vector.hpp
Go to the documentation of this file.
1#ifndef VECTOR_DEFINED
2#define VECTOR_DEFINED
3
5#include <iostream>
6
7namespace sisi4s {
8template <typename F = double, int D = 3>
9class Vector {
10public:
11 typedef F FieldType;
12
14 for (int d(0); d < D; ++d) { coordinate[d] = 0; }
15 }
16 Vector(Vector<F, D> const &v) {
17 for (int d(0); d < D; ++d) { coordinate[d] = v.coordinate[d]; }
18 }
19
20 // TODO: fully support move semantics
21
23 for (int d(0); d < D; ++d) { coordinate[d] = v.coordinate[d]; }
24 return *this;
25 }
26
28 for (int d(0); d < D; ++d) { coordinate[d] += v.coordinate[d]; }
29 return *this;
30 }
31
33 for (int d(0); d < D; ++d) { coordinate[d] -= v.coordinate[d]; }
34 return *this;
35 }
36
38 for (int d(0); d < D; ++d) { coordinate[d] *= r; }
39 return *this;
40 }
41
43 for (int d(0); d < D; ++d) { coordinate[d] /= r; }
44 return *this;
45 }
46
49 for (int d(0); d < D; ++d) {
50 t.coordinate[d] = coordinate[d] + v.coordinate[d];
51 }
52 return t;
53 }
54
57 for (int d(0); d < D; ++d) {
58 t.coordinate[d] = coordinate[d] - v.coordinate[d];
59 }
60 return t;
61 }
62
63 Vector<F, D> operator/(const F r) const {
65 for (int d(0); d < D; ++d) { t.coordinate[d] = coordinate[d] / r; }
66 return t;
67 }
68
69 Vector<F, D> operator*(const F r) const {
71 for (int d(0); d < D; ++d) { t.coordinate[d] = coordinate[d] * r; }
72 return t;
73 }
74
75 // Specialization for D=3
78 t.coordinate[0] =
79 coordinate[1] * v.coordinate[2] - coordinate[2] * v.coordinate[1];
80 t.coordinate[1] =
81 coordinate[2] * v.coordinate[0] - coordinate[0] * v.coordinate[2];
82 t.coordinate[2] =
83 coordinate[0] * v.coordinate[1] - coordinate[1] * v.coordinate[0];
84 return t;
85 }
86
87 F dot(Vector<F, D> const &v) const {
88 F sum(0);
89 for (int d(0); d < D; ++d) {
90 sum += sisi4s::dot(coordinate[d], v.coordinate[d]);
91 }
92 return sum;
93 }
94
95 // TODO: improve name
96 int approximately(Vector<F, D> const &v, const double epsilon = 1e-10) const {
97 Vector<F, D> u(*this);
98 u -= v;
99 return std::real(u.dot(u)) < epsilon;
100 }
101
102 // TODO: improve name
103 double distance(Vector<F, D> const &v) const {
104 Vector<F, D> u(*this);
105 u -= v;
106 return std::real(u.dot(u));
107 }
108
109 double length() const {
110 Vector<F, D> u(*this);
111 return std::sqrt(std::real(u.dot(u)));
112 }
113
114 F operator[](int d) const { return coordinate[d]; }
115
116 F &operator[](int d) { return coordinate[d]; }
117
118 F at(int d) const { return coordinate[d]; }
119
120 F &at(int d) { return coordinate[d]; }
121
122 // TODO: rename to less(n,m) or similar
123 static bool sortByLength(Vector<F, D> const &n, Vector<F, D> const &m) {
124 return n.length() < m.length();
125 }
126
127 bool operator<(Vector<F, D> const &v) const {
128 const double epsilon(1e-10);
129 for (int d(0); d < D; ++d) {
130 if (coordinate[d] < v.coordinate[d] - epsilon) {
131 return true;
132 } else if (coordinate[d] > v.coordinate[d] + epsilon) {
133 return false;
134 }
135 }
136 return false;
137 }
139};
140
141template <typename F = double, int D = 3>
142inline Vector<F, D> operator*(const F f, const Vector<F, D> &v) {
143 return Vector<F, D>(v * f);
144}
145
146template <typename F = double, int D = 3>
147inline Vector<F, D> &&operator*(const F f, Vector<F, D> &&v) {
148 v *= f;
149 return v;
150}
151
152template <typename F = double, int D = 3>
153inline std::ostream &operator<<(std::ostream &stream,
154 sisi4s::Vector<F, D> const &v) {
155 for (int d(0); d < D - 1; ++d) { stream << v.coordinate[d] << ","; }
156 stream << v.coordinate[D - 1];
157 return stream;
158}
159} // namespace sisi4s
160
161#endif
std::ostream & operator<<(std::ostream &s, const FcidumpReader::FcidumpHeader &h)
Definition FcidumpWriter.cxx:25
Definition Vector.hpp:9
double length() const
Definition Vector.hpp:109
Vector< F, D > operator/(const F r) const
Definition Vector.hpp:63
F & operator[](int d)
Definition Vector.hpp:116
bool operator<(Vector< F, D > const &v) const
Definition Vector.hpp:127
Vector< F, D > & operator/=(const F r)
Definition Vector.hpp:42
Vector< F, D > operator+(Vector< F, D > const &v) const
Definition Vector.hpp:47
F coordinate[D]
Definition Vector.hpp:138
Vector< F, D > operator*(const F r) const
Definition Vector.hpp:69
static bool sortByLength(Vector< F, D > const &n, Vector< F, D > const &m)
Definition Vector.hpp:123
F FieldType
Definition Vector.hpp:11
Vector()
Definition Vector.hpp:13
F at(int d) const
Definition Vector.hpp:118
F operator[](int d) const
Definition Vector.hpp:114
int approximately(Vector< F, D > const &v, const double epsilon=1e-10) const
Definition Vector.hpp:96
F dot(Vector< F, D > const &v) const
Definition Vector.hpp:87
Vector< F, D > operator-(Vector< F, D > const &v) const
Definition Vector.hpp:55
Vector< F, D > & operator*=(const F r)
Definition Vector.hpp:37
double distance(Vector< F, D > const &v) const
Definition Vector.hpp:103
F & at(int d)
Definition Vector.hpp:120
Vector< F, 3 > cross(Vector< F, 3 > const &v)
Definition Vector.hpp:76
Vector(Vector< F, D > const &v)
Definition Vector.hpp:16
Vector< F, D > & operator-=(Vector< F, D > const &v)
Definition Vector.hpp:32
Vector< F, D > & operator+=(Vector< F, D > const &v)
Definition Vector.hpp:27
Vector< F, D > & operator=(Vector< F, D > const &v)
Definition Vector.hpp:22
Definition Algorithm.hpp:10
std::string operator*(const std::string &s, const sisi4s::Permutation< N > &pi)
Definition CcsdPerturbativeTriples.cxx:24
F dot(F const x, F const y)
Definition MathFunctions.hpp:45