AutoPas  3.0.0
Loading...
Searching...
No Matches
ArrayMath.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <array>
11#include <cmath>
12#include <numeric>
13#include <sstream>
14
15#include "Math.h"
16
18
27template <class T, std::size_t SIZE>
28[[nodiscard]] constexpr std::array<T, SIZE> add(const std::array<T, SIZE> &a, const std::array<T, SIZE> &b) {
29 std::array<T, SIZE> result{};
30 for (std::size_t d = 0; d < SIZE; ++d) {
31 result[d] = a[d] + b[d];
32 }
33 return result;
34}
35
44template <class T, std::size_t SIZE>
45[[nodiscard]] constexpr std::array<T, SIZE> sub(const std::array<T, SIZE> &a, const std::array<T, SIZE> &b) {
46 std::array<T, SIZE> result{};
47 for (std::size_t d = 0; d < SIZE; ++d) {
48 result[d] = a[d] - b[d];
49 }
50 return result;
51}
52
61template <class T, std::size_t SIZE>
62[[nodiscard]] constexpr std::array<T, SIZE> min(const std::array<T, SIZE> &a, const std::array<T, SIZE> &b) {
63 std::array<T, SIZE> result{};
64 for (std::size_t d = 0; d < SIZE; ++d) {
65 result[d] = std::min<T>(a[d], b[d]);
66 }
67 return result;
68}
69
78template <class T, std::size_t SIZE>
79[[nodiscard]] constexpr bool less(const std::array<T, SIZE> &a, const std::array<T, SIZE> &b) {
80 bool result = true;
81 for (std::size_t d = 0; d < SIZE; ++d) {
82 result = result and (a[d] < b[d]);
83 }
84 return result;
85}
86
95template <class T, std::size_t SIZE>
96[[nodiscard]] constexpr std::array<T, SIZE> max(const std::array<T, SIZE> &a, const std::array<T, SIZE> &b) {
97 std::array<T, SIZE> result{};
98 for (std::size_t d = 0; d < SIZE; ++d) {
99 result[d] = std::max<T>(a[d], b[d]);
100 }
101 return result;
102}
103
112template <class T, std::size_t SIZE>
113[[nodiscard]] constexpr std::array<T, SIZE> mul(const std::array<T, SIZE> &a, const std::array<T, SIZE> &b) {
114 std::array<T, SIZE> result{};
115 for (std::size_t d = 0; d < SIZE; ++d) {
116 result[d] = a[d] * b[d];
117 }
118 return result;
119}
120
129template <class T, std::size_t SIZE>
130[[nodiscard]] constexpr std::array<T, SIZE> div(const std::array<T, SIZE> &a, const std::array<T, SIZE> &b) {
131 std::array<T, SIZE> result{};
132 for (std::size_t d = 0; d < SIZE; ++d) {
133 result[d] = a[d] / b[d];
134 }
135 return result;
136}
137
146template <class T, std::size_t SIZE>
147[[nodiscard]] constexpr std::array<T, SIZE> addScalar(const std::array<T, SIZE> &a, T s) {
148 std::array<T, SIZE> result{};
149 for (std::size_t d = 0; d < SIZE; ++d) {
150 result[d] = a[d] + s;
151 }
152 return result;
153}
154
163template <class T, std::size_t SIZE>
164[[nodiscard]] constexpr std::array<T, SIZE> subScalar(const std::array<T, SIZE> &a, T s) {
165 std::array<T, SIZE> result{};
166 for (std::size_t d = 0; d < SIZE; ++d) {
167 result[d] = a[d] - s;
168 }
169 return result;
170}
171
180template <class T, std::size_t SIZE>
181[[nodiscard]] constexpr std::array<T, SIZE> mulScalar(const std::array<T, SIZE> &a, T s) {
182 std::array<T, SIZE> result{};
183 for (std::size_t d = 0; d < SIZE; ++d) {
184 result[d] = a[d] * s;
185 }
186 return result;
187}
188
197template <class T, std::size_t SIZE>
198[[nodiscard]] constexpr std::array<T, SIZE> divScalar(const std::array<T, SIZE> &a, T s) {
199 std::array<T, SIZE> result{};
200 for (std::size_t d = 0; d < SIZE; ++d) {
201 result[d] = a[d] / s;
202 }
203 return result;
204}
205
214template <class T, std::size_t SIZE>
215[[nodiscard]] constexpr std::array<T, SIZE> divScalar(T s, const std::array<T, SIZE> &a) {
216 std::array<T, SIZE> result{};
217 for (std::size_t d = 0; d < SIZE; ++d) {
218 result[d] = s / a[d];
219 }
220 return result;
221}
222
232template <class T, std::size_t SIZE>
233[[nodiscard]] constexpr T dot(const std::array<T, SIZE> &a, const std::array<T, SIZE> &b) {
234 T result = 0.;
235 for (std::size_t i = 0; i < SIZE; i++) {
236 result += a[i] * b[i];
237 }
238 return result;
239}
240
248template <class T>
249[[nodiscard]] constexpr std::array<T, 3> cross(const std::array<T, 3> &a, const std::array<T, 3> &b) {
250 return {
251 a[1] * b[2] - a[2] * b[1],
252 a[2] * b[0] - a[0] * b[2],
253 a[0] * b[1] - a[1] * b[0],
254 };
255}
256
264template <class T, std::size_t SIZE>
265[[nodiscard]] constexpr T L2Norm(const std::array<T, SIZE> &a) {
266 return std::sqrt(dot(a, a));
267}
268
275template <class T>
276[[nodiscard]] constexpr typename T::value_type prod(const T &a) {
277 return std::accumulate(a.cbegin(), a.cend(), static_cast<typename T::value_type>(1), std::multiplies<>());
278}
279
287template <class T, std::size_t SIZE>
288[[nodiscard]] constexpr std::array<T, SIZE> abs(const std::array<T, SIZE> &a) {
289 std::array<T, SIZE> result{};
290 for (std::size_t d = 0; d < SIZE; ++d) {
291 result[d] = std::abs(a[d]);
292 }
293 return result;
294}
295
303template <class T, std::size_t SIZE>
304[[nodiscard]] constexpr std::array<T, SIZE> normalize(const std::array<T, SIZE> &a) {
305 return mulScalar(a, static_cast<T>(1) / L2Norm(a));
306}
307
315template <class T, std::size_t SIZE>
316[[nodiscard]] constexpr std::array<T, SIZE> ceil(const std::array<T, SIZE> &a) {
317 std::array<T, SIZE> result{};
318 for (std::size_t d = 0; d < SIZE; ++d) {
319 result[d] = std::ceil(a[d]);
320 }
321 return result;
322}
323
333template <typename target_T = int, typename float_T, std::size_t SIZE>
334[[nodiscard]] constexpr std::array<target_T, SIZE> floorAndCast(const std::array<float_T, SIZE> &a) {
335 std::array<target_T, SIZE> result{};
336 for (std::size_t d = 0; d < SIZE; ++d) {
337 result[d] = static_cast<target_T>(std::floor(a[d]));
338 }
339 return result;
340}
341
351template <class target_T = int, class float_T, std::size_t SIZE>
352[[nodiscard]] constexpr std::array<target_T, SIZE> ceilAndCast(const std::array<float_T, SIZE> &a) {
353 std::array<target_T, SIZE> result{};
354 for (std::size_t d = 0; d < SIZE; ++d) {
355 result[d] = static_cast<target_T>(std::ceil(a[d]));
356 }
357 return result;
358}
359
369template <class T, std::size_t SIZE>
370[[nodiscard]] bool isNearRel(const std::array<T, SIZE> &a, const std::array<T, SIZE> &b,
371 double maxRelativeDifference = 1e-9) {
372 bool arraysAreNear = true;
373 for (std::size_t i = 0; i < SIZE; ++i) {
374 arraysAreNear = arraysAreNear and utils::Math::isNearRel(a[i], b[i], maxRelativeDifference);
375 }
376 return arraysAreNear;
377}
378
389template <class T, std::size_t SIZE>
390[[nodiscard]] bool isNearRel(const std::vector<std::array<T, SIZE>> &a, const std::vector<std::array<T, SIZE>> &b,
391 double maxRelativeDifference = 1e-9) {
392 const auto size = a.size();
393 if (size != b.size()) {
394 return false;
395 }
396 bool arraysAreNear = true;
397 for (std::size_t i = 0; i < size; ++i) {
398 arraysAreNear = arraysAreNear and utils::ArrayMath::isNearRel(a[i], b[i], maxRelativeDifference);
399 }
400 return arraysAreNear;
401}
402
411template <class T>
412[[nodiscard]] bool isEqual(const std::vector<T> &a, const std::vector<T> &b) {
413 if (a.size() != b.size()) {
414 return false;
415 }
416 bool arraysAreEqual = true;
417 for (std::size_t i = 0; i < a.size(); ++i) {
418 arraysAreEqual = arraysAreEqual and (a[i] == b[i]);
419 }
420 return arraysAreEqual;
421}
422
431template <typename new_T, typename old_T, std::size_t SIZE>
432[[nodiscard]] constexpr std::array<new_T, SIZE> staticCastArray(const std::array<old_T, SIZE> &a) {
433 std::array<new_T, SIZE> result{};
434 for (std::size_t d = 0; d < SIZE; ++d) {
435 result[d] = static_cast<new_T>(a[d]);
436 }
437 return result;
438}
439
440// namespace for templated operators
441inline namespace literals {
442
451template <class T, std::size_t SIZE>
452constexpr std::array<T, SIZE> operator+(const std::array<T, SIZE> &a, const std::array<T, SIZE> &b) {
453 return add(a, b);
454}
455
464template <class T, std::size_t SIZE>
465constexpr std::array<T, SIZE> &operator+=(std::array<T, SIZE> &a, const std::array<T, SIZE> &b) {
466 for (std::size_t d = 0; d < SIZE; ++d) {
467 a[d] += b[d];
468 }
469 return a;
470}
471
480template <class T, std::size_t SIZE>
481constexpr std::array<T, SIZE> operator-(const std::array<T, SIZE> &a, const std::array<T, SIZE> &b) {
482 return sub(a, b);
483}
484
493template <class T, std::size_t SIZE>
494constexpr std::array<T, SIZE> &operator-=(std::array<T, SIZE> &a, const std::array<T, SIZE> &b) {
495 for (std::size_t d = 0; d < SIZE; ++d) {
496 a[d] -= b[d];
497 }
498 return a;
499}
500
509template <class T, std::size_t SIZE>
510constexpr std::array<T, SIZE> operator*(const std::array<T, SIZE> &a, const std::array<T, SIZE> &b) {
511 return mul(a, b);
512}
513
522template <class T, std::size_t SIZE>
523constexpr std::array<T, SIZE> &operator*=(std::array<T, SIZE> &a, const std::array<T, SIZE> &b) {
524 for (std::size_t d = 0; d < SIZE; ++d) {
525 a[d] *= b[d];
526 }
527 return a;
528}
529
538template <class T, std::size_t SIZE>
539constexpr std::array<T, SIZE> operator/(const std::array<T, SIZE> &a, const std::array<T, SIZE> &b) {
540 return div(a, b);
541}
542
551template <class T, std::size_t SIZE>
552constexpr std::array<T, SIZE> operator/(const std::array<T, SIZE> &a, T b) {
553 return divScalar(a, b);
554}
555
564template <class T, std::size_t SIZE>
565constexpr std::array<T, SIZE> operator/(T a, const std::array<T, SIZE> &b) {
566 return divScalar(a, b);
567}
568
577template <class T, std::size_t SIZE>
578constexpr std::array<T, SIZE> &operator/=(std::array<T, SIZE> &a, const std::array<T, SIZE> &b) {
579 for (std::size_t d = 0; d < SIZE; ++d) {
580 a[d] /= b[d];
581 }
582 return a;
583}
584
593template <class T, std::size_t SIZE>
594constexpr std::array<T, SIZE> operator+(const std::array<T, SIZE> &a, T s) {
595 return addScalar(a, s);
596}
597
606template <class T, std::size_t SIZE>
607constexpr std::array<T, SIZE> &operator+=(std::array<T, SIZE> &a, T s) {
608 for (std::size_t d = 0; d < SIZE; ++d) {
609 a[d] += s;
610 }
611 return a;
612}
613
622template <class T, std::size_t SIZE>
623constexpr std::array<T, SIZE> operator-(const std::array<T, SIZE> &a, T s) {
624 return subScalar(a, s);
625}
626
635template <class T, std::size_t SIZE>
636constexpr std::array<T, SIZE> &operator-=(std::array<T, SIZE> &a, T s) {
637 for (std::size_t d = 0; d < SIZE; ++d) {
638 a[d] -= s;
639 }
640 return a;
641}
642
651template <class T, std::size_t SIZE>
652constexpr std::array<T, SIZE> operator*(const std::array<T, SIZE> &a, T s) {
653 return mulScalar(a, s);
654}
655
664template <class T, std::size_t SIZE>
665constexpr std::array<T, SIZE> &operator*=(std::array<T, SIZE> &a, T s) {
666 for (std::size_t d = 0; d < SIZE; ++d) {
667 a[d] *= s;
668 }
669 return a;
670}
671
672} // namespace literals
673
683template <class T, std::size_t SIZE>
684double boxDistanceSquared(const std::array<T, SIZE> &aMin, const std::array<T, SIZE> &aMax,
685 const std::array<T, SIZE> &bMin, const std::array<T, SIZE> &bMax) {
686 using namespace autopas::utils::ArrayMath::literals;
689
690 const auto aToB = max(std::array<T, SIZE>{}, aMin - bMax);
691 const auto bToA = max(std::array<T, SIZE>{}, bMin - aMax);
692
693 return dot(aToB, aToB) + dot(bToA, bToA);
694}
695} // namespace autopas::utils::ArrayMath
Namespace to handle mathematical operations of std::array.
Definition: namespaces.h:49
bool isNearRel(const std::array< T, SIZE > &a, const std::array< T, SIZE > &b, double maxRelativeDifference=1e-9)
Returns true if arrays are elementwise relatively near each other.
Definition: ArrayMath.h:370
constexpr T dot(const std::array< T, SIZE > &a, const std::array< T, SIZE > &b)
Generates the dot product of two arrays.
Definition: ArrayMath.h:233
constexpr std::array< target_T, SIZE > floorAndCast(const std::array< float_T, SIZE > &a)
Floors all array elements and converts them to a different type.
Definition: ArrayMath.h:334
constexpr std::array< new_T, SIZE > staticCastArray(const std::array< old_T, SIZE > &a)
static_casts all elements of an array to a new type.
Definition: ArrayMath.h:432
constexpr T L2Norm(const std::array< T, SIZE > &a)
Computes the L2Norm / Euclidean norm.
Definition: ArrayMath.h:265
constexpr std::array< T, SIZE > operator*(const std::array< T, SIZE > &a, const std::array< T, SIZE > &b)
Multiplies two array's element wise and returns the result.
Definition: ArrayMath.h:510
constexpr std::array< T, SIZE > ceil(const std::array< T, SIZE > &a)
For each element in a, computes the smallest integer value not less than the element.
Definition: ArrayMath.h:316
constexpr std::array< T, SIZE > & operator*=(std::array< T, SIZE > &a, const std::array< T, SIZE > &b)
Assignment operator to multiply two arrays.
Definition: ArrayMath.h:523
constexpr std::array< T, SIZE > div(const std::array< T, SIZE > &a, const std::array< T, SIZE > &b)
Divides two array's element-wise and returns the result.
Definition: ArrayMath.h:130
constexpr std::array< T, SIZE > mulScalar(const std::array< T, SIZE > &a, T s)
Multiplies a scalar s to each element of array a and returns the result.
Definition: ArrayMath.h:181
constexpr std::array< target_T, SIZE > ceilAndCast(const std::array< float_T, SIZE > &a)
Ceils all array elements and converts them to a different type.
Definition: ArrayMath.h:352
constexpr std::array< T, SIZE > sub(const std::array< T, SIZE > &a, const std::array< T, SIZE > &b)
Subtracts array b from array a and returns the result.
Definition: ArrayMath.h:45
constexpr std::array< T, SIZE > add(const std::array< T, SIZE > &a, const std::array< T, SIZE > &b)
Adds two arrays, returns the result.
Definition: ArrayMath.h:28
constexpr std::array< T, SIZE > max(const std::array< T, SIZE > &a, const std::array< T, SIZE > &b)
Takes elementwise maximum and returns the result.
Definition: ArrayMath.h:96
constexpr std::array< T, SIZE > divScalar(const std::array< T, SIZE > &a, T s)
Divides an array element-wise by a given scalar and returns the result.
Definition: ArrayMath.h:198
constexpr std::array< T, SIZE > & operator/=(std::array< T, SIZE > &a, const std::array< T, SIZE > &b)
Assignment operator to divide two arrays.
Definition: ArrayMath.h:578
constexpr std::array< T, SIZE > abs(const std::array< T, SIZE > &a)
Computes the absolute value for all elements in a.
Definition: ArrayMath.h:288
constexpr std::array< T, SIZE > subScalar(const std::array< T, SIZE > &a, T s)
Subtracts a scalar s from each element of array a and returns the result.
Definition: ArrayMath.h:164
constexpr std::array< T, SIZE > addScalar(const std::array< T, SIZE > &a, T s)
Adds a scalar s to each element of array a and returns the result.
Definition: ArrayMath.h:147
constexpr std::array< T, SIZE > operator/(const std::array< T, SIZE > &a, const std::array< T, SIZE > &b)
Divides two array's element-wise and returns the result.
Definition: ArrayMath.h:539
constexpr std::array< T, SIZE > & operator-=(std::array< T, SIZE > &a, const std::array< T, SIZE > &b)
Assignment operator to subtract two arrays.
Definition: ArrayMath.h:494
constexpr bool less(const std::array< T, SIZE > &a, const std::array< T, SIZE > &b)
True iff for all d the following holds: a[d] < b[d].
Definition: ArrayMath.h:79
constexpr T::value_type prod(const T &a)
Computes the product of all elements in a.
Definition: ArrayMath.h:276
constexpr std::array< T, SIZE > min(const std::array< T, SIZE > &a, const std::array< T, SIZE > &b)
Takes elementwise minimum, returns the result.
Definition: ArrayMath.h:62
constexpr std::array< T, 3 > cross(const std::array< T, 3 > &a, const std::array< T, 3 > &b)
Generates the cross product of two arrays of 3 floats.
Definition: ArrayMath.h:249
constexpr std::array< T, SIZE > mul(const std::array< T, SIZE > &a, const std::array< T, SIZE > &b)
Multiplies two array's element wise and returns the result.
Definition: ArrayMath.h:113
bool isEqual(const std::vector< T > &a, const std::vector< T > &b)
Returns true if vectors are elementwise equal to each other.
Definition: ArrayMath.h:412
double boxDistanceSquared(const std::array< T, SIZE > &aMin, const std::array< T, SIZE > &aMax, const std::array< T, SIZE > &bMin, const std::array< T, SIZE > &bMax)
Calculate the squared minimum distance between two boxes, which are aligned to the Cartesian grid.
Definition: ArrayMath.h:684
constexpr std::array< T, SIZE > normalize(const std::array< T, SIZE > &a)
Generates a normalized array (|a| = 1).
Definition: ArrayMath.h:304
constexpr std::array< T, SIZE > & operator+=(std::array< T, SIZE > &a, const std::array< T, SIZE > &b)
Assignment operator to add two arrays.
Definition: ArrayMath.h:465
constexpr std::array< T, SIZE > operator-(const std::array< T, SIZE > &a, const std::array< T, SIZE > &b)
Subtracts array b from array a and returns the result.
Definition: ArrayMath.h:481
constexpr std::array< T, SIZE > operator+(const std::array< T, SIZE > &a, const std::array< T, SIZE > &b)
Adds two arrays, returns the result.
Definition: ArrayMath.h:452