bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
math_utils.h
Go to the documentation of this file.
1#ifndef BITSIM_MATHS_MATH_UTILS_H
2#define BITSIM_MATHS_MATH_UTILS_H
3
5
6#include <algorithm>
7#include <cassert>
8#include <cmath>
9#include <vector>
10
11namespace bitsim
12{
13namespace maths
14{
15
21template <typename T> int_t sign(const T &expr)
22{
23
24 if (expr < 0)
25 {
26 return -1;
27 }
28
29 return 1;
30}
31
37template <typename T> int_t sign(const T &expr1, const T &expr2) { return sign(expr1 * expr2); }
38
43template <typename T> T sum_sqr(const DynVec<T> &vec)
44{
45
46 T sum_ = T(0);
47 for (index_t i = 0; i < vec.size(); ++i)
48 {
49 sum_ += vec[i] * vec[i];
50 }
51
52 return sum_;
53}
54
60template <typename T> T sqr(const T &v) { return v * v; }
61
66inline real_t wrap_angle(real_t angle) { return std::atan2(std::sin(angle), std::cos(angle)); }
67
73template <typename VectorType> uint_t arg_max(const VectorType &vec)
74{
75 return static_cast<uint_t>(
76 std::distance(vec.begin(), std::max_element(vec.begin(), vec.end())));
77}
78
84template <typename VectorType> uint_t arg_min(const VectorType &vec)
85{
86 return static_cast<uint_t>(
87 std::distance(vec.begin(), std::min_element(vec.begin(), vec.end())));
88}
89
93template <typename VectorType> typename VectorType::value_type max(const VectorType &vec)
94{
95 return *std::max_element(vec.begin(), vec.end());
96}
97
101template <typename VectorType> typename VectorType::value_type min(const VectorType &vec)
102{
103 return *std::min_element(vec.begin(), vec.end());
104}
105
106namespace
107{
108
115template <typename T> auto lin_value(T start, T stop, uint_t index, uint_t n)
116{
117
118 assert(n > 1 && index < n);
119 const auto amount = static_cast<T>(index) / (n - 1);
120 const auto v = std::lerp(start, stop, amount);
121 return v;
122}
123
124} // namespace
125
130template <typename T> std::vector<T> lin_space(T start, T stop, uint_t n)
131{
132
133 auto v = std::vector<T>{};
134 v.reserve(n);
135 for (auto i = 0u; i < n; ++i)
136 {
137 v.push_back(lin_value(start, stop, i, n));
138 }
139 return v;
140}
141
142} // namespace maths
143} // namespace bitsim
144
145#endif // BITSIM_MATHS_MATH_UTILS_H
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
Definition math_utils.h:14
int_t sign(const T &expr)
Definition math_utils.h:21
VectorType::value_type min(const VectorType &vec)
Returns the minimum element in the vector.
Definition math_utils.h:101
T sum_sqr(const DynVec< T > &vec)
Compute the sum of squares of the elements of the given vector.
Definition math_utils.h:43
T sqr(const T &v)
Definition math_utils.h:60
uint_t arg_max(const VectorType &vec)
Returns the index of the element that has the maximum value in the array. Implementation taken from h...
Definition math_utils.h:73
real_t wrap_angle(real_t angle)
The angle [rad] wrapped to , so that angles that differ by whole turns compare equal.
Definition math_utils.h:66
std::vector< T > lin_space(T start, T stop, uint_t n)
Generate n evenly linearly spaced numbers between [start, stop]. Analogous to NumPy linspace.
Definition math_utils.h:130
uint_t arg_min(const VectorType &vec)
Returns the index of the element that has the minimum value in the array. Implementation taken from h...
Definition math_utils.h:84
VectorType::value_type max(const VectorType &vec)
Returns the max element in the vector.
Definition math_utils.h:93
Definition dc_motor_backend_base.h:9
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
Eigen::Index index_t
The type Eigen indexes and sizes matrices and vectors with.
Definition bitsim_types.h:45
double real_t
real_t
Definition bitsim_types.h:20
int int_t
integer type
Definition bitsim_types.h:30
Eigen::RowVectorX< T > DynVec
Dynamically sized row vector.
Definition bitsim_types.h:101