25#ifndef LSST_MODELFIT_PARAMETERS_LIMITS_H
26#define LSST_MODELFIT_PARAMETERS_LIMITS_H
37namespace lsst::modelfit::parameters {
50 constexpr void _check(
const T& min,
const T& max)
const {
51 if (std::isnan(min) || std::isnan(max))
52 throw std::runtime_error(
str() +
" can't be initialized with NaN limits");
53 if (!(min <= max))
throw std::invalid_argument(
str() +
" can't be initialized with !(min <= max)");
56 inline void _check_min(
const T& min)
const {
57 if (std::isnan(min))
throw std::invalid_argument(
str() +
" set_min given NaN");
58 if (!(min <= _max))
throw std::invalid_argument(
str() +
" set_min !(min_new <= max)");
60 inline void _check_max(
const T& max)
const {
61 if (std::isnan(max))
throw std::invalid_argument(
str() +
" set_max given NaN");
62 if (!(max >= _min))
throw std::invalid_argument(
str() +
" set_max !(min <= max_new)");
67 inline bool check(T value)
const {
return value >= _min && value <= _max; }
69 inline T
clip(T value)
const {
return value > _max ? _max : (value < _min ? _min : value); }
72 inline T
get_min()
const {
return _min; };
74 inline T
get_max()
const {
return _max; };
79 void set(T min, T max) {
95 std::string
repr(
bool name_keywords =
false,
97 return type_name_str<Limits<T>>(
false, namespace_separator) +
"(" + (name_keywords ?
"min=" :
"")
98 + std::to_string(_min) +
", " + (name_keywords ?
"max=" :
"") + std::to_string(_max) +
", "
99 + (name_keywords ?
"name='" :
"") + name +
"')";
101 std::string
str()
const override {
102 return type_name_str<Limits<T>>(
true) +
"(" + std::to_string(_min) +
", " + std::to_string(_max)
103 +
", '" + name +
"')";
107 Limits(T min = -std::numeric_limits<T>::infinity(), T max = std::numeric_limits<T>::infinity(),
108 std::string name_ =
"")
109 : _min(min), _max(max), name(name_) {
bool check(T value) const
Check if a value is within the limits.
Definition limits.h:67
std::string repr(bool name_keywords=false, const std::string_view &namespace_separator=CC_NAMESPACE_SEPARATOR) const override
Definition limits.h:95
T clip(T value) const
Return the closest value to the input that is within the limits.
Definition limits.h:69
T get_max() const
Return the maximum.
Definition limits.h:74
T get_min() const
Return the minimum.
Definition limits.h:72
void set_min(T min)
Set the minimum.
Definition limits.h:85
void set(T min, T max)
Set the minimum and maximum.
Definition limits.h:79
void set_max(T max)
Set the maximum.
Definition limits.h:90
Limits(T min=-std::numeric_limits< T >::infinity(), T max=std::numeric_limits< T >::infinity(), std::string name_="")
Initialize limits from the minimum and maximum value.
Definition limits.h:107
std::string str() const override
Return a brief, human-readable string representation of this.
Definition limits.h:101
static constexpr std::string_view CC_NAMESPACE_SEPARATOR
The C++ namespace separator.
Definition object.h:42