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];
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];
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]);
78template <
class T, std::
size_t SIZE>
79[[nodiscard]]
constexpr bool less(
const std::array<T, SIZE> &a,
const std::array<T, SIZE> &b) {
81 for (std::size_t d = 0; d < SIZE; ++d) {
82 result = result and (a[d] < b[d]);
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]);
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];
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];
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;
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;
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;
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;
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];
232template <
class T, std::
size_t SIZE>
233[[nodiscard]]
constexpr T
dot(
const std::array<T, SIZE> &a,
const std::array<T, SIZE> &b) {
235 for (std::size_t i = 0; i < SIZE; i++) {
236 result += a[i] * b[i];
249[[nodiscard]]
constexpr std::array<T, 3>
cross(
const std::array<T, 3> &a,
const std::array<T, 3> &b) {
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],
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));
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<>());
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]);
303template <
class T, std::
size_t SIZE>
304[[nodiscard]]
constexpr std::array<T, SIZE>
normalize(
const std::array<T, SIZE> &a) {
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]);
331template <
class T, std::
size_t SIZE>
332[[nodiscard]]
constexpr std::array<int, SIZE>
floorToInt(
const std::array<T, SIZE> &a) {
333 std::array<int, SIZE> result{};
334 for (std::size_t d = 0; d < SIZE; ++d) {
335 result[d] =
static_cast<int>(std::floor(a[d]));
347template <
class T, std::
size_t SIZE>
348[[nodiscard]]
constexpr std::array<int, SIZE>
ceilToInt(
const std::array<T, SIZE> &a) {
349 std::array<int, SIZE> result{};
350 for (std::size_t d = 0; d < SIZE; ++d) {
351 result[d] =
static_cast<int>(std::ceil(a[d]));
365template <
class T, std::
size_t SIZE>
366[[nodiscard]]
bool isNearRel(
const std::array<T, SIZE> &a,
const std::array<T, SIZE> &b,
367 double maxRelativeDifference = 1e-9) {
368 bool arraysAreNear =
true;
369 for (std::size_t i = 0; i < SIZE; ++i) {
370 arraysAreNear = arraysAreNear and utils::Math::isNearRel(a[i], b[i], maxRelativeDifference);
372 return arraysAreNear;
385template <
class T, std::
size_t SIZE>
386[[nodiscard]]
bool isNearRel(
const std::vector<std::array<T, SIZE>> &a,
const std::vector<std::array<T, SIZE>> &b,
387 double maxRelativeDifference = 1e-9) {
388 const auto size = a.size();
389 if (size != b.size()) {
392 bool arraysAreNear =
true;
393 for (std::size_t i = 0; i < size; ++i) {
396 return arraysAreNear;
408[[nodiscard]]
bool isEqual(
const std::vector<T> &a,
const std::vector<T> &b) {
409 if (a.size() != b.size()) {
412 bool arraysAreEqual =
true;
413 for (std::size_t i = 0; i < a.size(); ++i) {
414 arraysAreEqual = arraysAreEqual and (a[i] == b[i]);
416 return arraysAreEqual;
420inline namespace literals {
430template <
class T, std::
size_t SIZE>
431constexpr std::array<T, SIZE>
operator+(
const std::array<T, SIZE> &a,
const std::array<T, SIZE> &b) {
443template <
class T, std::
size_t SIZE>
444constexpr std::array<T, SIZE> &
operator+=(std::array<T, SIZE> &a,
const std::array<T, SIZE> &b) {
445 for (std::size_t d = 0; d < SIZE; ++d) {
459template <
class T, std::
size_t SIZE>
460constexpr std::array<T, SIZE>
operator-(
const std::array<T, SIZE> &a,
const std::array<T, SIZE> &b) {
472template <
class T, std::
size_t SIZE>
473constexpr std::array<T, SIZE> &
operator-=(std::array<T, SIZE> &a,
const std::array<T, SIZE> &b) {
474 for (std::size_t d = 0; d < SIZE; ++d) {
488template <
class T, std::
size_t SIZE>
489constexpr std::array<T, SIZE>
operator*(
const std::array<T, SIZE> &a,
const std::array<T, SIZE> &b) {
501template <
class T, std::
size_t SIZE>
502constexpr std::array<T, SIZE> &
operator*=(std::array<T, SIZE> &a,
const std::array<T, SIZE> &b) {
503 for (std::size_t d = 0; d < SIZE; ++d) {
517template <
class T, std::
size_t SIZE>
518constexpr std::array<T, SIZE>
operator/(
const std::array<T, SIZE> &a,
const std::array<T, SIZE> &b) {
530template <
class T, std::
size_t SIZE>
531constexpr std::array<T, SIZE>
operator/(
const std::array<T, SIZE> &a, T b) {
543template <
class T, std::
size_t SIZE>
544constexpr std::array<T, SIZE>
operator/(T a,
const std::array<T, SIZE> &b) {
556template <
class T, std::
size_t SIZE>
557constexpr std::array<T, SIZE> &
operator/=(std::array<T, SIZE> &a,
const std::array<T, SIZE> &b) {
558 for (std::size_t d = 0; d < SIZE; ++d) {
572template <
class T, std::
size_t SIZE>
573constexpr std::array<T, SIZE>
operator+(
const std::array<T, SIZE> &a, T s) {
585template <
class T, std::
size_t SIZE>
586constexpr std::array<T, SIZE> &
operator+=(std::array<T, SIZE> &a, T s) {
587 for (std::size_t d = 0; d < SIZE; ++d) {
601template <
class T, std::
size_t SIZE>
602constexpr std::array<T, SIZE>
operator-(
const std::array<T, SIZE> &a, T s) {
614template <
class T, std::
size_t SIZE>
615constexpr std::array<T, SIZE> &
operator-=(std::array<T, SIZE> &a, T s) {
616 for (std::size_t d = 0; d < SIZE; ++d) {
630template <
class T, std::
size_t SIZE>
631constexpr std::array<T, SIZE>
operator*(
const std::array<T, SIZE> &a, T s) {
643template <
class T, std::
size_t SIZE>
644constexpr std::array<T, SIZE> &
operator*=(std::array<T, SIZE> &a, T s) {
645 for (std::size_t d = 0; d < SIZE; ++d) {
662template <
class T, std::
size_t SIZE>
664 const std::array<T, SIZE> &bMin,
const std::array<T, SIZE> &bMax) {
665 using namespace autopas::utils::ArrayMath::literals;
669 const auto aToB =
max(std::array<T, SIZE>{}, aMin - bMax);
670 const auto bToA =
max(std::array<T, SIZE>{}, bMin - aMax);
672 return dot(aToB, aToB) +
dot(bToA, bToA);
Namespace to handle mathematical operations of std::array.
Definition: namespaces.h:49
constexpr std::array< int, SIZE > floorToInt(const std::array< T, SIZE > &a)
Floors all array elements and converts them to integers.
Definition: ArrayMath.h:332
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:366
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 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:489
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:502
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< 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:557
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:518
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:473
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:408
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:663
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:444
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:460
constexpr std::array< int, SIZE > ceilToInt(const std::array< T, SIZE > &a)
Ceils all array elements and converts them to integers.
Definition: ArrayMath.h:348
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:431