SOPT
Sparse OPTimisation
relative_variation.h
Go to the documentation of this file.
1 #ifndef SOPT_RELATIVE_VARIATION_H
2 #define SOPT_RELATIVE_VARIATION_H
3 
4 #include "sopt/config.h"
5 #include <string>
6 #include <Eigen/Core>
7 #include "sopt/logging.h"
8 #include "sopt/maths.h"
9 
10 namespace sopt {
11 
12 template <typename TYPE>
14  public:
16  using Scalar = TYPE;
18  using Real = typename real_type<Scalar>::type;
20  RelativeVariation(Real tolerance = 1e-12, std::string const &name = "")
21  : tolerance_(tolerance), previous_(typename Array<Scalar>::Index(0)), name_(name){};
24  : tolerance_(c.tolerance_), previous_(c.previous_){};
25 
27  template <typename T>
28  bool operator()(Eigen::MatrixBase<T> const &input) {
29  return operator()(input.array());
30  }
32  template <typename T>
33  bool operator()(Eigen::ArrayBase<T> const &input);
35  Real tolerance() const { return tolerance_; }
38  tolerance_ = e;
39  return *this;
40  }
42  std::string const &name() const { return name_; }
43  RelativeVariation const &name(std::string const &name) {
44  name_ = name;
45  return *this;
46  }
47 
48  protected:
49  Real tolerance_;
50  Array<Scalar> previous_;
51  std::string name_;
52 };
53 
54 template <typename TYPE>
56  public:
58  using Scalar = TYPE;
60  using Real = typename real_type<Scalar>::type;
63  std::string const &name = "")
64  : name_(name),
65  relative_tolerance_(relative_tolerance),
66  absolute_tolerance_(absolute_tolerance),
67  previous_(0),
68  is_first_(true){};
71  : relative_tolerance_(c.relative_tolerance_),
72  previous_(c.previous_),
73  is_first_(c.is_first_){};
74 
76  bool operator()(Scalar const &current);
77 
79  Real relative_tolerance() const { return relative_tolerance_; }
82  relative_tolerance_ = e;
83  return *this;
84  }
86  Real absolute_tolerance() const { return relative_tolerance_; }
89  absolute_tolerance_ = e;
90  return *this;
91  }
92 
94  std::string const &name() const { return name_; }
95  ScalarRelativeVariation const &name(std::string const &name) {
96  name_ = name;
97  return *this;
98  }
100  Scalar previous() const { return previous_; }
101 
102  protected:
103  std::string name_;
104  Real relative_tolerance_;
105  Real absolute_tolerance_;
106  Scalar previous_;
107  bool is_first_;
108 };
109 
110 template <typename SCALAR>
111 template <typename T>
112 bool RelativeVariation<SCALAR>::operator()(Eigen::ArrayBase<T> const &input) {
113  if (previous_.size() != input.size()) {
114  previous_ = input;
115  return false;
116  }
117  auto const norm = (input - previous_).matrix().squaredNorm();
118  previous_ = input;
119  SOPT_LOW_LOG(" - {} relative variation: {} <? {}", name(), std::sqrt(norm), tolerance());
120  return norm < tolerance() * tolerance();
121 }
122 
123 template <typename SCALAR>
125  if (is_first_) {
126  previous_ = current;
127  is_first_ = false;
128  return false;
129  }
130  auto const average = (std::abs(previous_) + std::abs(current)) * 0.5;
131  auto const diff = std::abs(previous_ - current);
132  auto const result =
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");
137  previous_ = current;
138  return result;
139 }
140 } // namespace sopt
141 
142 #endif
sopt::t_real Scalar
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 &current)
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.
Definition: real_type.h:42
#define SOPT_LOW_LOG(...)
Low priority message.
Definition: logging.h:227
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.
Definition: types.h:34