bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
lp_metric_impl.h
Go to the documentation of this file.
1#ifndef LP_METRIC_IMPL_H
2#define LP_METRIC_IMPL_H
3
4#include <cmath>
5#include <stdexcept>
6#include <string>
7
8namespace bitrl
9{
10namespace maths
11{
12
13template <int P, bool TTakeRoot> real_t LpMetric<P, TTakeRoot>::tolerance_value = 1.0e-8;
14
15template <int P, bool TTakeRoot>
17{
18 static_assert(P >= 1, "LpMetric needs P >= 1");
19
20 if (v1.size() != v2.size())
21 {
22 throw std::invalid_argument("LpMetric: the vectors have sizes " +
23 std::to_string(v1.size()) + " and " +
24 std::to_string(v2.size()));
25 }
26
27 const auto diff = (v1 - v2).cwiseAbs();
28 if constexpr (P == 1)
29 {
30 // the root of the L1 norm is the norm itself
31 return diff.sum();
32 }
33 else if constexpr (P == 2)
34 {
35 const auto sqr_norm = diff.squaredNorm();
36 return TTakeRoot ? std::sqrt(sqr_norm) : sqr_norm;
37 }
38 else
39 {
40 const real_t sum = diff.array().pow(static_cast<real_t>(P)).sum();
41 return TTakeRoot ? std::pow(sum, 1.0 / P) : sum;
42 }
43}
44
45template <int P, bool TTakeRoot>
46real_t LpMetric<P, TTakeRoot>::evaluate(const std::vector<real_t> &v1,
47 const std::vector<real_t> &v2)
48{
49 using map_type = Eigen::Map<const DynVec<real_t>>;
50 return evaluate(DynVec<real_t>(map_type(v1.data(), static_cast<Eigen::Index>(v1.size()))),
51 DynVec<real_t>(map_type(v2.data(), static_cast<Eigen::Index>(v2.size()))));
52}
53
54template <int P, bool TTakeRoot>
59
60} // namespace maths
61} // namespace bitrl
62
63#endif // LP_METRIC_IMPL_H
static real_t tolerance_value
Definition lp_metric.h:21
real_t operator()(const DynVec< real_t > &v1, const DynVec< real_t > &v2) const
Overload operator().
Definition lp_metric_impl.h:55
static real_t evaluate(const DynVec< real_t > &v1, const DynVec< real_t > &v2)
evaluate
Definition lp_metric_impl.h:16
Definition lp_metric.h:10
std::iterator_traits< IteratorType >::value_type sum(IteratorType begin, IteratorType end, bool parallel=true)
Definition vector_math.h:89
Various utilities used when working with RL problems.
Definition bitrl_log.h:25
double real_t
real_t
Definition bitsim_types.h:20
Eigen::RowVectorX< T > DynVec
Dynamically sized row vector.
Definition bitsim_types.h:101