bitrl & cuberl Documentation
Simulation engine for reinforcement learning agents
Loading...
Searching...
No Matches
math_utils.h
Go to the documentation of this file.
1#include "bitrl/bitrl_types.h"
2
6
7#include <algorithm>
8#include <cassert>
9#include <cmath>
10#include <vector>
11
12namespace bitrl
13{
14namespace utils::maths
15{
16
22template <typename T> int_t sign(const T &expr)
23{
24
25 if (expr < 0)
26 {
27 return -1;
28 }
29
30 return 1;
31}
32
38template <typename T> int_t sign(const T &expr1, const T &expr2) { return sign(expr1 * expr2); }
39
44template <typename T> T sum_sqr(const DynVec<T> &vec)
45{
46
47 T sum_ = T(0);
48 for (uint_t i = 0; i < vec.size(); ++i)
49 {
50 sum_ += vec[i] * vec[i];
51 }
52
53 return sum_;
54}
55
61template <typename T> T sqr(const T &v) { return v * v; }
62
68template <typename VectorType> uint_t arg_max(const VectorType &vec)
69{
70 return static_cast<uint_t>(
71 std::distance(vec.begin(), std::max_element(vec.begin(), vec.end())));
72}
73
79template <typename VectorType> uint_t arg_min(const VectorType &vec)
80{
81 return static_cast<uint_t>(
82 std::distance(vec.begin(), std::min_element(vec.begin(), vec.end())));
83}
84
88template <typename VectorType> typename VectorType::value_type max(const VectorType &vec)
89{
90 return *std::max_element(vec.begin(), vec.end());
91}
92
96template <typename VectorType> typename VectorType::value_type min(const VectorType &vec)
97{
98 return *std::min_element(vec.begin(), vec.end());
99}
100
101namespace
102{
103
110template <typename T> auto lin_value(T start, T stop, uint_t index, uint_t n)
111{
112
113 assert(n > 1 && index < n);
114 const auto amount = static_cast<T>(index) / (n - 1);
115 const auto v = std::lerp(start, stop, amount);
116 return v;
117}
118
119} // namespace
120
125template <typename T> std::vector<T> lin_space(T start, T stop, uint_t n)
126{
127
128 auto v = std::vector<T>{};
129 v.reserve(n);
130 for (auto i = 0u; i < n; ++i)
131 {
132 v.push_back(lin_value(start, stop, i, n));
133 }
134 return v;
135}
136
137}
138} // namespace bitrl
VectorType::value_type max(const VectorType &vec)
Returns the max element in the vector.
Definition math_utils.h:88
T sum_sqr(const DynVec< T > &vec)
Compute the sum of squares of the elements of the given vector.
Definition math_utils.h:44
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:68
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:79
VectorType::value_type min(const VectorType &vec)
Returns the minimum element in the vector.
Definition math_utils.h:96
T sqr(const T &v)
Definition math_utils.h:61
int_t sign(const T &expr)
Definition math_utils.h:22
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:125
OutT resolve(const std::string &name, const std::map< std::string, std::any > &input)
Definition std_map_utils.h:25
Definition bitrl_consts.h:14
int int_t
integer type
Definition bitrl_types.h:33
Eigen::RowVectorX< T > DynVec
Dynamically sized row vector.
Definition bitrl_types.h:74
std::size_t uint_t
uint_t
Definition bitrl_types.h:43