Gemmi C++ API
Loading...
Searching...
No Matches
math.hpp
Go to the documentation of this file.
1// Copyright 2018 Global Phasing Ltd.
2//
3// Math utilities. 3D linear algebra.
4
5#ifndef GEMMI_MATH_HPP_
6#define GEMMI_MATH_HPP_
7
8#include <cmath> // for fabs, cos, sqrt, round
9#include <algorithm> // for min
10#include <array>
11#include <stdexcept> // for out_of_range
12#include <type_traits> // for enable_if, is_integral
13
14namespace gemmi {
15
16constexpr double pi() { return 3.1415926535897932384626433832795029; }
17
18// The value used in converting between energy[eV] and wavelength[Angstrom].
19// $ units -d15 'h * c / eV / angstrom'
20constexpr double hc() { return 12398.4197386209; }
21
22// The Bohr radius (a0) in Angstroms.
23constexpr double bohrradius() { return 0.529177210903; }
24
25// for Mott-Bethe factor
26constexpr double mott_bethe_const() { return 1. / (2 * pi() * pi() * bohrradius()); }
27
28// Used in conversion of ADPs (atomic displacement parameters).
29constexpr double u_to_b() { return 8 * pi() * pi(); }
30
31constexpr double deg(double angle) { return 180.0 / pi() * angle; }
32constexpr double rad(double angle) { return pi() / 180.0 * angle; }
33
34constexpr float sq(float x) { return x * x; }
35constexpr double sq(double x) { return x * x; }
36
37inline int iround(double d) { return static_cast<int>(std::round(d)); }
38
39inline double angle_abs_diff(double a, double b, double full=360.0) {
40 double d = std::fabs(a - b);
41 if (d > full)
42 d -= std::floor(d / full) * full;
43 return std::min(d, full - d);
44}
45
46template <typename Real>
47struct Vec3_ {
48 Real x, y, z;
49
50 Vec3_() : x(0), y(0), z(0) {}
51 Vec3_(Real x_, Real y_, Real z_) : x(x_), y(y_), z(z_) {}
52 explicit Vec3_(std::array<int, 3> h) : x(h[0]), y(h[1]), z(h[2]) {}
53
54 Real& at(int i) {
55 switch (i) {
56 case 0: return x;
57 case 1: return y;
58 case 2: return z;
59 default: throw std::out_of_range("Vec3 index must be 0, 1 or 2.");
60 }
61 }
62 Real at(int i) const { return const_cast<Vec3_*>(this)->at(i); }
63
64 Vec3_ operator-() const { return {-x, -y, -z}; }
65 Vec3_ operator-(const Vec3_& o) const { return {x-o.x, y-o.y, z-o.z}; }
66 Vec3_ operator+(const Vec3_& o) const { return {x+o.x, y+o.y, z+o.z}; }
67 Vec3_ operator*(Real d) const { return {x*d, y*d, z*d}; }
68 Vec3_ operator/(Real d) const { return *this * (1.0/d); }
69 Vec3_& operator-=(const Vec3_& o) { *this = *this - o; return *this; }
70 Vec3_& operator+=(const Vec3_& o) { *this = *this + o; return *this; }
71 Vec3_& operator*=(Real d) { *this = *this * d; return *this; }
72 Vec3_& operator/=(Real d) { return operator*=(1.0/d); }
73
74 Vec3_ negated() const { return {-x, -y, -z}; }
75 Real dot(const Vec3_& o) const { return x*o.x + y*o.y + z*o.z; }
76 Vec3_ cross(const Vec3_& o) const {
77 return {y*o.z - z*o.y, z*o.x - x*o.z, x*o.y - y*o.x};
78 }
79 Real length_sq() const { return x * x + y * y + z * z; }
80 Real length() const { return std::sqrt(length_sq()); }
81 Vec3_ changed_magnitude(Real m) const { return operator*(m / length()); }
82 Vec3_ normalized() const { return changed_magnitude(1.0); }
83 Real dist_sq(const Vec3_& o) const { return (*this - o).length_sq(); }
84 Real dist(const Vec3_& o) const { return std::sqrt(dist_sq(o)); }
85 Real cos_angle(const Vec3_& o) const {
86 return dot(o) / std::sqrt(length_sq() * o.length_sq());
87 }
88 Real angle(const Vec3_& o) const {
89 return std::acos(std::max(-1., std::min(1., cos_angle(o))));
90 }
91 bool approx(const Vec3_& o, Real epsilon) const {
92 return std::fabs(x - o.x) <= epsilon &&
93 std::fabs(y - o.y) <= epsilon &&
94 std::fabs(z - o.z) <= epsilon;
95 }
96 bool has_nan() const {
97 return std::isnan(x) || std::isnan(y) || std::isnan(z);
98 }
99};
100
103
104inline Vec3 operator*(double d, const Vec3& v) { return v * d; }
105
108inline Vec3 rotate_about_axis(const Vec3& v, const Vec3& axis, double theta) {
109 double sin_theta = std::sin(theta);
110 double cos_theta = std::cos(theta);
111 return v * cos_theta + axis.cross(v) * sin_theta +
112 axis * (axis.dot(v) * (1 - cos_theta));
113}
114
115struct Mat33 {
116 double a[3][3] = { {1.,0.,0.}, {0.,1.,0.}, {0.,0.,1.} };
117
118 // make it accessible with ".a"
119 typedef double row_t[3];
120 const row_t& operator[](int i) const { return a[i]; }
121 row_t& operator[](int i) { return a[i]; }
122
123 Mat33() = default;
124 explicit Mat33(double d) : a{{d, d, d}, {d, d, d}, {d, d, d}} {}
125 Mat33(double a1, double a2, double a3, double b1, double b2, double b3,
126 double c1, double c2, double c3)
127 : a{{a1, a2, a3}, {b1, b2, b3}, {c1, c2, c3}} {}
128
129 static Mat33 from_columns(const Vec3& c1, const Vec3& c2, const Vec3& c3) {
130 return Mat33(c1.x, c2.x, c3.x, c1.y, c2.y, c3.y, c1.z, c2.z, c3.z);
131 }
132
133 Vec3 row_copy(int i) const {
134 if (i < 0 || i > 2)
135 throw std::out_of_range("Mat33 row index must be 0, 1 or 2.");
136 return Vec3(a[i][0], a[i][1], a[i][2]);
137 }
138
139 Vec3 column_copy(int i) const {
140 if (i < 0 || i > 2)
141 throw std::out_of_range("Mat33 column index must be 0, 1 or 2.");
142 return Vec3(a[0][i], a[1][i], a[2][i]);
143 }
144
145 Mat33 operator+(const Mat33& b) const {
146 return Mat33(a[0][0] + b[0][0], a[0][1] + b[0][1], a[0][2] + b[0][2],
147 a[1][0] + b[1][0], a[1][1] + b[1][1], a[1][2] + b[1][2],
148 a[2][0] + b[2][0], a[2][1] + b[2][1], a[2][2] + b[2][2]);
149 }
150 Mat33 operator-(const Mat33& b) const {
151 return Mat33(a[0][0] - b[0][0], a[0][1] - b[0][1], a[0][2] - b[0][2],
152 a[1][0] - b[1][0], a[1][1] - b[1][1], a[1][2] - b[1][2],
153 a[2][0] - b[2][0], a[2][1] - b[2][1], a[2][2] - b[2][2]);
154 }
155
156 Vec3 multiply(const Vec3& p) const {
157 return {a[0][0] * p.x + a[0][1] * p.y + a[0][2] * p.z,
158 a[1][0] * p.x + a[1][1] * p.y + a[1][2] * p.z,
159 a[2][0] * p.x + a[2][1] * p.y + a[2][2] * p.z};
160 }
161 Vec3 left_multiply(const Vec3& p) const {
162 return {a[0][0] * p.x + a[1][0] * p.y + a[2][0] * p.z,
163 a[0][1] * p.x + a[1][1] * p.y + a[2][1] * p.z,
164 a[0][2] * p.x + a[1][2] * p.y + a[2][2] * p.z};
165 }
166 // p has elements from the main diagonal of a 3x3 diagonal matrix
168 return Mat33(a[0][0] * p.x, a[0][1] * p.y, a[0][2] * p.z,
169 a[1][0] * p.x, a[1][1] * p.y, a[1][2] * p.z,
170 a[2][0] * p.x, a[2][1] * p.y, a[2][2] * p.z);
171 }
172 Mat33 multiply(const Mat33& b) const {
173 Mat33 r;
174 for (int i = 0; i != 3; ++i)
175 for (int j = 0; j != 3; ++j)
176 r[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j] + a[i][2] * b[2][j];
177 return r;
178 }
179 Mat33 transpose() const {
180 return Mat33(a[0][0], a[1][0], a[2][0],
181 a[0][1], a[1][1], a[2][1],
182 a[0][2], a[1][2], a[2][2]);
183 }
184 double trace() const { return a[0][0] + a[1][1] + a[2][2]; }
185
186 bool approx(const Mat33& other, double epsilon) const {
187 for (int i = 0; i < 3; ++i)
188 for (int j = 0; j < 3; ++j)
189 if (std::fabs(a[i][j] - other.a[i][j]) > epsilon)
190 return false;
191 return true;
192 }
193 bool has_nan() const {
194 for (int i = 0; i < 3; ++i)
195 for (int j = 0; j < 3; ++j)
196 if (std::isnan(a[i][j]))
197 return true;
198 return false;
199 }
200
201 double determinant() const {
202 return a[0][0] * (a[1][1]*a[2][2] - a[2][1]*a[1][2]) +
203 a[0][1] * (a[1][2]*a[2][0] - a[2][2]*a[1][0]) +
204 a[0][2] * (a[1][0]*a[2][1] - a[2][0]*a[1][1]);
205 }
206 Mat33 inverse() const {
207 Mat33 inv;
208 double inv_det = 1.0 / determinant();
209 inv[0][0] = inv_det * (a[1][1] * a[2][2] - a[2][1] * a[1][2]);
210 inv[0][1] = inv_det * (a[0][2] * a[2][1] - a[0][1] * a[2][2]);
211 inv[0][2] = inv_det * (a[0][1] * a[1][2] - a[0][2] * a[1][1]);
212 inv[1][0] = inv_det * (a[1][2] * a[2][0] - a[1][0] * a[2][2]);
213 inv[1][1] = inv_det * (a[0][0] * a[2][2] - a[0][2] * a[2][0]);
214 inv[1][2] = inv_det * (a[1][0] * a[0][2] - a[0][0] * a[1][2]);
215 inv[2][0] = inv_det * (a[1][0] * a[2][1] - a[2][0] * a[1][1]);
216 inv[2][1] = inv_det * (a[2][0] * a[0][1] - a[0][0] * a[2][1]);
217 inv[2][2] = inv_det * (a[0][0] * a[1][1] - a[1][0] * a[0][1]);
218 return inv;
219 }
220 bool is_identity() const {
221 return a[0][0] == 1 && a[0][1] == 0 && a[0][2] == 0 &&
222 a[1][0] == 0 && a[1][1] == 1 && a[1][2] == 0 &&
223 a[2][0] == 0 && a[2][1] == 0 && a[2][2] == 1;
224 }
225
226 double column_dot(int i, int j) const {
227 return a[0][i] * a[0][j] + a[1][i] * a[1][j] + a[2][i] * a[2][j];
228 }
229
230 bool is_upper_triangular() const {
231 return a[1][0] == 0 && a[2][0] == 0 && a[2][1] == 0;
232 }
233};
234
236 double a11 = 0, a12 = 0, a13 = 0;
237 double a22 = 0, a23 = 0;
238 double a33 = 0;
240 void operator=(const Mat33& m) {
241 if (m.is_upper_triangular()) {
242 a11 = m[0][0];
243 a12 = m[0][1];
244 a13 = m[0][2];
245 a22 = m[1][1];
246 a23 = m[1][2];
247 a33 = m[2][2];
248 } else {
249 a11 = a12 = a13 = a22 = a23 = a33 = NAN;
250 }
251 }
252 Vec3 multiply(const Vec3& p) const {
253 return {a11 * p.x + a12 * p.y + a13 * p.z,
254 a22 * p.y + a23 * p.z,
255 a33 * p.z};
256 }
257};
258
259// Symmetric matrix 3x3. Used primarily for an ADP tensor.
260template<typename T> struct SMat33 {
261 T u11, u22, u33, u12, u13, u23;
262
263 // The PDB ANISOU record has the above order, but in a different context
264 // (such as metric tensor) the order of Voigt notation may be preferred.
265 std::array<T, 6> elements_pdb() const { return {{u11, u22, u33, u12, u13, u23}}; }
266 std::array<T, 6> elements_voigt() const { return {{u11, u22, u33, u23, u13, u12}}; }
267
268 Mat33 as_mat33() const {
269 return Mat33(u11, u12, u13, u12, u22, u23, u13, u23, u33);
270 }
271
272 T trace() const { return u11 + u22 + u33; }
273 bool nonzero() const { return trace() != 0; }
274
275 bool all_zero() const {
276 return u11 == 0 && u22 == 0 && u33 == 0 && u12 == 0 && u13 == 0 && u23 == 0;
277 }
278
279 void scale(T s) const {
280 u11 *= s; u22 *= s; u33 *= s; u12 *= s; u13 *= s; u23 *= s;
281 };
282
283 template<typename Real>
285 return SMat33<Real>{u11*s, u22*s, u33*s, u12*s, u13*s, u23*s};
286 }
287
288 // returns U + kI
290 return {u11+k, u22+k, u33+k, u12, u13, u23};
291 }
292
293 // returns squared norm r^T U r where U is this matrix and vector r is arg
294 template<typename VT>
295 auto r_u_r(const Vec3_<VT>& r) const -> decltype(r.x+u11) {
296 return r.x * r.x * u11 + r.y * r.y * u22 + r.z * r.z * u33 +
297 2 * (r.x * r.y * u12 + r.x * r.z * u13 + r.y * r.z * u23);
298 }
299 double r_u_r(const std::array<int,3>& h) const {
300 // it's faster to first convert ints to doubles (Vec3)
301 return r_u_r(Vec3(h));
302 }
303
304 Vec3 multiply(const Vec3& p) const {
305 return {u11 * p.x + u12 * p.y + u13 * p.z,
306 u12 * p.x + u22 * p.y + u23 * p.z,
307 u13 * p.x + u23 * p.y + u33 * p.z};
308 }
309
310 SMat33 operator-(const SMat33& o) const {
311 return {u11-o.u11, u22-o.u22, u33-o.u33, u12-o.u12, u13-o.u13, u23-o.u23};
312 }
313 SMat33 operator+(const SMat33& o) const {
314 return {u11+o.u11, u22+o.u22, u33+o.u33, u12+o.u12, u13+o.u13, u23+o.u23};
315 }
316
317 // return M U M^T
318 template<typename Real=double>
320 // slightly faster than m.multiply(as_mat33()).multiply(m.transpose());
321 auto elem = [&](int i, int j) {
322 return static_cast<Real>(
323 m[i][0] * (m[j][0] * u11 + m[j][1] * u12 + m[j][2] * u13) +
324 m[i][1] * (m[j][0] * u12 + m[j][1] * u22 + m[j][2] * u23) +
325 m[i][2] * (m[j][0] * u13 + m[j][1] * u23 + m[j][2] * u33));
326 };
327 return SMat33<Real>{elem(0, 0), elem(1, 1), elem(2, 2),
328 elem(0, 1), elem(0, 2), elem(1, 2)};
329 }
330
331 T determinant() const {
332 return u11 * (u22*u33 - u23*u23) +
333 u12 * (u23*u13 - u33*u12) +
334 u13 * (u12*u23 - u13*u22);
335 }
336
337 SMat33 inverse() const {
338 SMat33 inv;
339 T inv_det = 1.0f / determinant();
340 inv.u11 = inv_det * (u22 * u33 - u23 * u23);
341 inv.u22 = inv_det * (u11 * u33 - u13 * u13);
342 inv.u33 = inv_det * (u11 * u22 - u12 * u12);
343 inv.u12 = inv_det * (u13 * u23 - u12 * u33);
344 inv.u13 = inv_det * (u12 * u23 - u13 * u22);
345 inv.u23 = inv_det * (u12 * u13 - u11 * u23);
346 return inv;
347 }
348
351 std::array<double, 3> calculate_eigenvalues() const {
352 double p1 = u12*u12 + u13*u13 + u23*u23;
353 if (p1 == 0)
354 return {{u11, u22, u33}};
355 double q = (1./3.) * trace();
356 SMat33<double> b{u11 - q, u22 - q, u33 - q, u12, u13, u23};
357 double p2 = sq(b.u11) + sq(b.u22) + sq(b.u33) + 2 * p1;
358 double p = std::sqrt((1./6.) * p2);
359 double r = b.determinant() / ((1./3.) * p2 * p);
360 double phi = 0;
361 if (r <= -1)
362 phi = (1./3.) * pi();
363 else if (r < 1)
364 phi = (1./3.) * std::acos(r);
365 double eig1 = q + 2 * p * std::cos(phi);
366 double eig3 = q + 2 * p * std::cos(phi + 2./3.*pi());
367 return {{eig1, 3 * q - eig1 - eig3, eig3}};
368 }
369};
370
371struct Transform {
374
376 Mat33 minv = mat.inverse();
377 return {minv, minv.multiply(vec).negated()};
378 }
379
380 Vec3 apply(const Vec3& x) const { return mat.multiply(x) + vec; }
381
382 Transform combine(const Transform& b) const {
383 return {mat.multiply(b.mat), vec + mat.multiply(b.vec)};
384 }
385
386 bool is_identity() const {
387 return mat.is_identity() && vec.x == 0. && vec.y == 0. && vec.z == 0.;
388 }
389 void set_identity() { mat = Mat33(); vec = Vec3(); }
390
391 bool has_nan() const {
392 return mat.has_nan() || vec.has_nan();
393 }
394
395 bool approx(const Transform& o, double epsilon) const {
396 return mat.approx(o.mat, epsilon) && vec.approx(o.vec, epsilon);
397 }
398};
399
400template<typename Pos>
401struct Box {
404 void extend(const Pos& p) {
405 if (p.x < minimum.x) minimum.x = p.x;
406 if (p.y < minimum.y) minimum.y = p.y;
407 if (p.z < minimum.z) minimum.z = p.z;
408 if (p.x > maximum.x) maximum.x = p.x;
409 if (p.y > maximum.y) maximum.y = p.y;
410 if (p.z > maximum.z) maximum.z = p.z;
411 }
412 Pos get_size() const { return maximum - minimum; }
413 void add_margins(const Pos& p) { minimum -= p; maximum += p; }
414 void add_margin(double m) { add_margins(Pos(m, m, m)); }
415};
416
417// internally used functions
418namespace impl {
419// MSVC is missing isnan(IntegralType), so we define is_nan as a replacement
420template<typename T>
421typename std::enable_if<std::is_integral<T>::value, bool>::type
422is_nan(T) { return false; }
423template<typename T>
424typename std::enable_if<std::is_floating_point<T>::value, bool>::type
425is_nan(T a) { return std::isnan(a); }
426
427template<typename T>
428typename std::enable_if<std::is_integral<T>::value, bool>::type
429is_same(T a, T b) { return a == b; }
430template<typename T>
431typename std::enable_if<std::is_floating_point<T>::value, bool>::type
432is_same(T a, T b) { return std::isnan(b) ? std::isnan(a) : a == b; }
433} // namespace impl
434
435} // namespace gemmi
436#endif
constexpr double deg(double angle)
Definition math.hpp:31
constexpr double bohrradius()
Definition math.hpp:23
int iround(double d)
Definition math.hpp:37
Vec3 rotate_about_axis(const Vec3 &v, const Vec3 &axis, double theta)
Rodrigues' rotation formula: rotate vector v about given axis of rotation (which must be a unit vecto...
Definition math.hpp:108
constexpr double hc()
Definition math.hpp:20
constexpr double mott_bethe_const()
Definition math.hpp:26
Vec3 operator*(double d, const Vec3 &v)
Definition math.hpp:104
Op & operator*=(Op &a, const Op &b)
Definition symmetry.hpp:194
double angle_abs_diff(double a, double b, double full=360.0)
Definition math.hpp:39
constexpr double rad(double angle)
Definition math.hpp:32
constexpr float sq(float x)
Definition math.hpp:34
Vec3_< double > Vec3
Definition math.hpp:101
constexpr double pi()
Definition math.hpp:16
constexpr double u_to_b()
Definition math.hpp:29
Pos get_size() const
Definition math.hpp:412
void extend(const Pos &p)
Definition math.hpp:404
void add_margins(const Pos &p)
Definition math.hpp:413
void add_margin(double m)
Definition math.hpp:414
bool is_upper_triangular() const
Definition math.hpp:230
double a[3][3]
Definition math.hpp:116
Vec3 column_copy(int i) const
Definition math.hpp:139
row_t & operator[](int i)
Definition math.hpp:121
double determinant() const
Definition math.hpp:201
Mat33 operator+(const Mat33 &b) const
Definition math.hpp:145
Mat33 multiply(const Mat33 &b) const
Definition math.hpp:172
Vec3 left_multiply(const Vec3 &p) const
Definition math.hpp:161
Mat33(double a1, double a2, double a3, double b1, double b2, double b3, double c1, double c2, double c3)
Definition math.hpp:125
Mat33()=default
bool approx(const Mat33 &other, double epsilon) const
Definition math.hpp:186
Mat33 multiply_by_diagonal(const Vec3 &p) const
Definition math.hpp:167
Mat33 transpose() const
Definition math.hpp:179
double trace() const
Definition math.hpp:184
bool has_nan() const
Definition math.hpp:193
double row_t[3]
Definition math.hpp:119
Mat33(double d)
Definition math.hpp:124
const row_t & operator[](int i) const
Definition math.hpp:120
bool is_identity() const
Definition math.hpp:220
Vec3 row_copy(int i) const
Definition math.hpp:133
Mat33 inverse() const
Definition math.hpp:206
static Mat33 from_columns(const Vec3 &c1, const Vec3 &c2, const Vec3 &c3)
Definition math.hpp:129
Vec3 multiply(const Vec3 &p) const
Definition math.hpp:156
double column_dot(int i, int j) const
Definition math.hpp:226
Mat33 operator-(const Mat33 &b) const
Definition math.hpp:150
SMat33 operator+(const SMat33 &o) const
Definition math.hpp:313
T trace() const
Definition math.hpp:272
void scale(T s) const
Definition math.hpp:279
SMat33 inverse() const
Definition math.hpp:337
Mat33 as_mat33() const
Definition math.hpp:268
std::array< T, 6 > elements_voigt() const
Definition math.hpp:266
SMat33 operator-(const SMat33 &o) const
Definition math.hpp:310
Vec3 multiply(const Vec3 &p) const
Definition math.hpp:304
SMat33< Real > scaled(Real s) const
Definition math.hpp:284
T determinant() const
Definition math.hpp:331
bool nonzero() const
Definition math.hpp:273
SMat33< Real > transformed_by(const Mat33 &m) const
Definition math.hpp:319
bool all_zero() const
Definition math.hpp:275
std::array< double, 3 > calculate_eigenvalues() const
Based on https://en.wikipedia.org/wiki/Eigenvalue_algorithm To calculate both eigenvalues and eigenve...
Definition math.hpp:351
double r_u_r(const std::array< int, 3 > &h) const
Definition math.hpp:299
auto r_u_r(const Vec3_< VT > &r) const -> decltype(r.x+u11)
Definition math.hpp:295
SMat33< T > added_kI(T k) const
Definition math.hpp:289
std::array< T, 6 > elements_pdb() const
Definition math.hpp:265
Vec3 apply(const Vec3 &x) const
Definition math.hpp:380
bool approx(const Transform &o, double epsilon) const
Definition math.hpp:395
Transform combine(const Transform &b) const
Definition math.hpp:382
void set_identity()
Definition math.hpp:389
bool is_identity() const
Definition math.hpp:386
bool has_nan() const
Definition math.hpp:391
Transform inverse() const
Definition math.hpp:375
Vec3 multiply(const Vec3 &p) const
Definition math.hpp:252
void operator=(const Mat33 &m)
Definition math.hpp:240