BioDynaMo  v1.05.119-a4ff3934
math.h
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2021 CERN & University of Surrey for the benefit of the
4 // BioDynaMo collaboration. All Rights Reserved.
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // See the LICENSE file distributed with this work for details.
10 // See the NOTICE file distributed with this work for additional information
11 // regarding copyright ownership.
12 //
13 // -----------------------------------------------------------------------------
14 
15 #ifndef CORE_UTIL_MATH_H_
16 #define CORE_UTIL_MATH_H_
17 
18 #include <array>
19 #include <cmath>
20 #include <numeric>
21 #include <vector>
22 
23 #include <TMath.h>
24 
26 #include "core/util/log.h"
27 #include "core/util/random.h"
28 
29 namespace bdm {
30 
31 struct Math {
33  static constexpr real_t kPi = static_cast<real_t>(TMath::Pi());
35  static constexpr real_t kInfinity = 1e20;
36 
37  static real_t ToDegree(real_t rad) { return rad * (180 / kPi); }
38  static real_t ToRadian(real_t deg) { return deg * (kPi / 180); }
39 
40  // Helper function that returns distance (L2 norm) between two positions in 3D
41  static real_t GetL2Distance(const Real3& pos1, const Real3& pos2) {
42  Real3 dist_array;
43  dist_array[0] = pos2[0] - pos1[0];
44  dist_array[1] = pos2[1] - pos1[1];
45  dist_array[2] = pos2[2] - pos1[2];
46  return dist_array.Norm();
47  }
48 
53  template <std::size_t N>
55  const MathArray<real_t, N>& b) {
56  MathArray<real_t, N> result;
57  result[0] = a[1] * b[2] - a[2] * b[1];
58  result[1] = a[2] * b[0] - a[0] * b[2];
59  result[2] = a[0] * b[1] - a[1] * b[0];
60  return result;
61  }
62 
68  static Real3 Perp3(const Real3& a, real_t random) {
69  Real3 vect_perp;
70  if (a[0] == 0.0) {
71  vect_perp[0] = 1.0;
72  vect_perp[1] = 0.0;
73  vect_perp[2] = 0.0;
74  vect_perp = RotAroundAxis(vect_perp, 6.35 * random, a);
75  } else {
76  vect_perp[0] = a[1];
77  vect_perp[1] = -a[0];
78  vect_perp[2] = 0.0;
79  vect_perp.Normalize();
80  vect_perp = RotAroundAxis(vect_perp, 6.35 * random, a);
81  }
82  return vect_perp;
83  }
84 
92  static Real3 RotAroundAxis(const Real3& vector, real_t theta,
93  const Real3& axis) {
94  auto naxis = axis;
95  naxis.Normalize();
96 
97  auto temp_1 = naxis * (vector * naxis);
98  auto temp_2 = (vector - temp_1) * std::cos(-theta);
99  auto temp_3 = CrossProduct(vector, naxis) * std::sin(-theta);
100 
101  return temp_1 + temp_2 + temp_3;
102  }
103 
108  static real_t AngleRadian(const Real3& a, const Real3& b) {
109  return std::acos(a * b / (a.Norm() * b.Norm()));
110  }
111 
116  static Real3 ProjectionOnto(const Real3& a, const Real3& b) {
117  real_t k = (a * b) / (b * b);
118  return b * k;
119  }
120 
122  static real_t MSE(const std::vector<real_t>& v1,
123  const std::vector<real_t>& v2) {
124  if (v1.size() != v2.size()) {
125  Log::Fatal("Math::MSE", "vectors must have same length");
126  }
127  real_t error = 0;
128  for (size_t i = 0; i < v1.size(); ++i) {
129  auto diff = v2[i] - v1[i];
130  error += diff * diff;
131  }
132  return error / v1.size();
133  }
134 };
135 
136 } // namespace bdm
137 
138 #endif // CORE_UTIL_MATH_H_
bdm::Math
Definition: math.h:31
bdm
Definition: agent.cc:39
bdm::Math::MSE
static real_t MSE(const std::vector< real_t > &v1, const std::vector< real_t > &v2)
Returns the mean squared error between two vectors.
Definition: math.h:122
bdm::Math::Perp3
static Real3 Perp3(const Real3 &a, real_t random)
Definition: math.h:68
bdm::real_t
double real_t
Definition: real_t.h:21
random.h
bdm::Math::AngleRadian
static real_t AngleRadian(const Real3 &a, const Real3 &b)
Definition: math.h:108
bdm::Math::CrossProduct
static MathArray< real_t, N > CrossProduct(const MathArray< real_t, N > &a, const MathArray< real_t, N > &b)
Definition: math.h:54
bdm::MathArray::Norm
T Norm() const
Definition: math_array.h:352
bdm::Math::kInfinity
static constexpr real_t kInfinity
Helpful constant to identify 'infinity'.
Definition: math.h:35
math_array.h
bdm::Log::Fatal
static void Fatal(const std::string &location, const Args &... parts)
Prints fatal error message.
Definition: log.h:115
log.h
bdm::Math::RotAroundAxis
static Real3 RotAroundAxis(const Real3 &vector, real_t theta, const Real3 &axis)
Definition: math.h:92
bdm::Math::ToRadian
static real_t ToRadian(real_t deg)
Definition: math.h:38
bdm::Math::ToDegree
static real_t ToDegree(real_t rad)
Definition: math.h:37
bdm::Math::kPi
static constexpr real_t kPi
value of pi
Definition: math.h:33
bdm::Math::GetL2Distance
static real_t GetL2Distance(const Real3 &pos1, const Real3 &pos2)
Definition: math.h:41
bdm::MathArray< real_t, 3 >
bdm::Math::ProjectionOnto
static Real3 ProjectionOnto(const Real3 &a, const Real3 &b)
Definition: math.h:116
bdm::MathArray::Normalize
void Normalize()
Normalize the array in-place.
Definition: math_array.h:364