1 #ifndef SOPT_RELATIVE_VARIATION_H
2 #define SOPT_RELATIVE_VARIATION_H
4 #include "sopt/config.h"
12 template <
typename TYPE>
24 : tolerance_(c.tolerance_), previous_(c.previous_){};
33 bool operator()(Eigen::ArrayBase<T>
const &input);
42 std::string
const &
name()
const {
return name_; }
54 template <
typename TYPE>
63 std::string
const &
name =
"")
71 : relative_tolerance_(c.relative_tolerance_),
72 previous_(c.previous_),
73 is_first_(c.is_first_){};
82 relative_tolerance_ = e;
89 absolute_tolerance_ = e;
94 std::string
const &
name()
const {
return name_; }
104 Real relative_tolerance_;
105 Real absolute_tolerance_;
110 template <
typename SCALAR>
111 template <
typename T>
113 if (previous_.size() != input.size()) {
117 auto const norm = (input - previous_).matrix().squaredNorm();
119 SOPT_LOW_LOG(
" - {} relative variation: {} <? {}", name(), std::sqrt(norm), tolerance());
120 return norm < tolerance() * tolerance();
123 template <
typename SCALAR>
130 auto const average = (std::abs(previous_) + std::abs(current)) * 0.5;
131 auto const diff = std::abs(previous_ - current);
133 diff <= relative_tolerance() * std::abs(average) or std::abs(
diff) < absolute_tolerance();
134 SOPT_LOW_LOG(
" - {} relative variation: {} < {} * {} or {} < {} is {}", name(),
diff,
135 relative_tolerance(), average,
diff, absolute_tolerance(),
136 result ?
"true" :
"false");
RelativeVariation(RelativeVariation const &c)
Copy constructor.
TYPE Scalar
Underlying scalar type.
bool operator()(Eigen::MatrixBase< T > const &input)
True if object has changed by less than tolerance.
RelativeVariation & tolerance(Real &e) const
Allowed variation.
RelativeVariation const & name(std::string const &name)
typename real_type< Scalar >::type Real
Underlying scalar type.
std::string const & name() const
Name that gets interpolated into the log's output.
Real tolerance() const
Allowed variation.
RelativeVariation(Real tolerance=1e-12, std::string const &name="")
Maximum variation from one step to the next.
Real relative_tolerance() const
Allowed variation.
RelativeVariation< Scalar > & absolute_tolerance(Real &e)
Allowed variation.
ScalarRelativeVariation const & name(std::string const &name)
bool operator()(Scalar const ¤t)
True if object has changed by less than tolerance.
ScalarRelativeVariation(Real relative_tolerance=1e-12, Real absolute_tolerance=1e-12, std::string const &name="")
Maximum variation from one step to the next.
Real absolute_tolerance() const
Allowed variation.
RelativeVariation< Scalar > & relative_tolerance(Real &e)
Allowed variation.
typename real_type< Scalar >::type Real
Underlying scalar type.
TYPE Scalar
Underlying scalar type.
ScalarRelativeVariation(ScalarRelativeVariation const &c)
Copy constructor.
std::string const & name() const
Name that gets interpolated into the log's output.
Computes inner-most element type.
#define SOPT_LOW_LOG(...)
Low priority message.
Vector< T > diff(const Vector< T > &x)
Numerical derivative of 1d vector.
Eigen::Array< T, Eigen::Dynamic, 1 > Array
A 1-dimensional list of elements of given type.