diff --git a/dune/solvers/common/Makefile.am b/dune/solvers/common/Makefile.am index f93219a8cefea8027ab0e4412d14c8a02e1ce3fa..01aa34682ae5404647a22b2f902cdfbe91f4bb1c 100644 --- a/dune/solvers/common/Makefile.am +++ b/dune/solvers/common/Makefile.am @@ -4,6 +4,7 @@ commondir = $(includedir)/dune/solvers/common common_HEADERS = boxconstraint.hh \ genericvectortools.hh \ canignore.hh \ + interval.hh \ numproc.hh \ permutationmanager.hh \ preconditioner.hh \ diff --git a/dune/solvers/common/interval.hh b/dune/solvers/common/interval.hh new file mode 100644 index 0000000000000000000000000000000000000000..6b5e63606819dfd4685c54ef1cae45369c9b1c5d --- /dev/null +++ b/dune/solvers/common/interval.hh @@ -0,0 +1,79 @@ +#ifndef DUNE_SOLVERS_COMMON_INTERVAL_HH +#define DUNE_SOLVERS_COMMON_INTERVAL_HH + +#include <iostream> +#include <algorithm> + + + +#include <dune/common/array.hh> + + + +/** \brief Encapsulates a closed interval + * \tparam field_type The type used for real numbers + */ +template <class field_type> +class Interval +{ +public: + /** \brief Array-like access + */ + field_type& operator[](int i) + { + return data_[i]; + } + + /** \brief Const array-like access + */ + const field_type& operator[](int i) const + { + return data_[i]; + } + + /** \brief Project a scalar onto the interval + */ + field_type projectIn(const field_type& x) const + { + return std::max(std::min(x,data_[1]), data_[0]); + } + + /** \brief Fast projection onto the interval if you know that your value + * is smaller than your upper bound + */ + field_type projectFromBelow(const field_type& x) const + { + return std::max(x,data_[0]); + }; + + /** \brief Fast projection onto the interval if you know that your value + * is larger than your lower bound + */ + field_type projectFromAbove(const field_type& x) const + { + return std::min(x,data_[1]); + }; + + /** \brief Return true if zero is contained in the interval + * \param safety An additive safety distance + */ + bool containsZero(const field_type& safety) const + { + return (data_[0] <= safety) and (-safety <= data_[1]); + }; + +private: + + /** \brief The actual data */ + Dune::array<field_type,2> data_; +}; + +//! Output operator for Interval +template <class field_type> +inline std::ostream& operator<< (std::ostream& s, const Interval<field_type>& interval) +{ + s << "[" << interval[0] << ", " << interval[1] << "]"; + return s; +} + +#endif