LSST Applications g0d97872fb5+4fd969bb9d,g1653933729+34a971ddd9,g28da252d5a+072f89fe25,g2bbee38e9b+a99b0ab4cd,g2bc492864f+a99b0ab4cd,g2ca4be77d2+c0e3b27cd8,g2cdde0e794+704103fe75,g3156d2b45e+6e87dc994a,g347aa1857d+a99b0ab4cd,g35bb328faa+34a971ddd9,g3a166c0a6a+a99b0ab4cd,g3e281a1b8c+8ec26ec694,g4005a62e65+ba0306790b,g414038480c+9ed5ed841a,g569e0e2b34+cb4faa46ad,g5a97de2502+520531a62c,g717e5f8c0f+29153700a5,g7ede599f99+367733290c,g80478fca09+17051a22cc,g82479be7b0+f2f1ea0a87,g858d7b2824+29153700a5,g8b782ad322+29153700a5,g8cd86fa7b1+05420e7f7d,g9125e01d80+34a971ddd9,ga5288a1d22+e7f674aaf3,gae0086650b+34a971ddd9,gae74b0b5c6+45ef5cdc51,gb58c049af0+ace264a4f2,gc28159a63d+a99b0ab4cd,gcf0d15dbbd+8051a81198,gda6a2b7d83+8051a81198,gdaeeff99f8+7774323b41,gdf4d240d4a+34a971ddd9,ge2409df99d+cb167bac99,ge33fd446bb+29153700a5,ge79ae78c31+a99b0ab4cd,gf0baf85859+890af219f9,gf5289d68f6+9faa5c5784,w.2024.36
LSST Data Management Base Package
Loading...
Searching...
No Matches
limits.h
Go to the documentation of this file.
1// -*- LSST-C++ -*-
2/*
3 * This file is part of modelfit_parameters.
4 *
5 * Developed for the LSST Data Management System.
6 * This product includes software developed by the LSST Project
7 * (https://www.lsst.org).
8 * See the COPYRIGHT file at the top-level directory of this distribution
9 * for details of code ownership.
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 */
24
25#ifndef LSST_MODELFIT_PARAMETERS_LIMITS_H
26#define LSST_MODELFIT_PARAMETERS_LIMITS_H
27
28#include <cctype>
29#include <limits>
30#include <stdexcept>
31#include <string_view>
32
33#include "object.h"
34#include "transform.h"
35#include "type_name.h"
36
38
44template <typename T>
45class Limits : public Object {
46private:
47 T _min;
48 T _max;
49
50 constexpr void _check(const T& min, const T& max) const {
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)");
54 }
55
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)");
59 }
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)");
63 }
64
65public:
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); }
70
72 inline T get_min() const { return _min; };
74 inline T get_max() const { return _max; };
75
77
79 void set(T min, T max) {
80 _check(min, max);
81 _min = min;
82 _max = max;
83 }
85 void set_min(T min) {
86 _check_min(min);
87 _min = min;
88 };
90 void set_max(T max) {
91 _check_max(max);
92 _max = max;
93 };
94
95 std::string repr(bool name_keywords = false,
96 const std::string_view& namespace_separator = CC_NAMESPACE_SEPARATOR) const override {
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 + "')";
100 }
101 std::string str() const override {
102 return type_name_str<Limits<T>>(true) + "(" + std::to_string(_min) + ", " + std::to_string(_max)
103 + ", '" + name + "')";
104 }
105
108 std::string name_ = "")
109 : _min(min), _max(max), name(name_) {
110 _check(min, max);
111 }
112
114};
115
116} // namespace lsst::modelfit::parameters
117#endif // LSST_MODELFIT_PARAMETERS_LIMITS_H
int min
int max
Range-based limits for parameter values.
Definition limits.h:45
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
Return a full, callable string representation of this.
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
A generic object from the parameters library.
Definition object.h:39
static constexpr std::string_view CC_NAMESPACE_SEPARATOR
The C++ namespace separator.
Definition object.h:42
T isnan(T... args)
T to_string(T... args)