LSST Applications g0f08755f38+82efc23009,g12f32b3c4e+e7bdf1200e,g1653933729+a8ce1bb630,g1a0ca8cf93+50eff2b06f,g28da252d5a+52db39f6a5,g2bbee38e9b+37c5a29d61,g2bc492864f+37c5a29d61,g2cdde0e794+c05ff076ad,g3156d2b45e+41e33cbcdc,g347aa1857d+37c5a29d61,g35bb328faa+a8ce1bb630,g3a166c0a6a+37c5a29d61,g3e281a1b8c+fb992f5633,g414038480c+7f03dfc1b0,g41af890bb2+11b950c980,g5fbc88fb19+17cd334064,g6b1c1869cb+12dd639c9a,g781aacb6e4+a8ce1bb630,g80478fca09+72e9651da0,g82479be7b0+04c31367b4,g858d7b2824+82efc23009,g9125e01d80+a8ce1bb630,g9726552aa6+8047e3811d,ga5288a1d22+e532dc0a0b,gae0086650b+a8ce1bb630,gb58c049af0+d64f4d3760,gc28159a63d+37c5a29d61,gcf0d15dbbd+2acd6d4d48,gd7358e8bfb+778a810b6e,gda3e153d99+82efc23009,gda6a2b7d83+2acd6d4d48,gdaeeff99f8+1711a396fd,ge2409df99d+6b12de1076,ge79ae78c31+37c5a29d61,gf0baf85859+d0a5978c5a,gf3967379c6+4954f8c433,gfb92a5be7c+82efc23009,gfec2e1e490+2aaed99252,w.2024.46
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)