1#ifndef BITSIM_MATHS_MATH_UTILS_H
2#define BITSIM_MATHS_MATH_UTILS_H
37template <
typename T>
int_t sign(
const T &expr1,
const T &expr2) {
return sign(expr1 * expr2); }
47 for (
index_t i = 0; i < vec.size(); ++i)
49 sum_ += vec[i] * vec[i];
60template <
typename T> T
sqr(
const T &v) {
return v * v; }
75 return static_cast<uint_t>(
76 std::distance(vec.begin(), std::max_element(vec.begin(), vec.end())));
86 return static_cast<uint_t>(
87 std::distance(vec.begin(), std::min_element(vec.begin(), vec.end())));
93template <
typename VectorType>
typename VectorType::value_type
max(
const VectorType &vec)
95 return *std::max_element(vec.begin(), vec.end());
101template <
typename VectorType>
typename VectorType::value_type
min(
const VectorType &vec)
103 return *std::min_element(vec.begin(), vec.end());
115template <
typename T>
auto lin_value(T start, T stop,
uint_t index,
uint_t n)
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);
133 auto v = std::vector<T>{};
135 for (
auto i = 0u; i < n; ++i)
137 v.push_back(lin_value(start, stop, i, n));
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