LSST Applications g063fba187b+cac8b7c890,g0f08755f38+6aee506743,g1653933729+a8ce1bb630,g168dd56ebc+a8ce1bb630,g1a2382251a+b4475c5878,g1dcb35cd9c+8f9bc1652e,g20f6ffc8e0+6aee506743,g217e2c1bcf+73dee94bd0,g28da252d5a+1f19c529b9,g2bbee38e9b+3f2625acfc,g2bc492864f+3f2625acfc,g3156d2b45e+6e55a43351,g32e5bea42b+1bb94961c2,g347aa1857d+3f2625acfc,g35bb328faa+a8ce1bb630,g3a166c0a6a+3f2625acfc,g3e281a1b8c+c5dd892a6c,g3e8969e208+a8ce1bb630,g414038480c+5927e1bc1e,g41af890bb2+8a9e676b2a,g7af13505b9+809c143d88,g80478fca09+6ef8b1810f,g82479be7b0+f568feb641,g858d7b2824+6aee506743,g89c8672015+f4add4ffd5,g9125e01d80+a8ce1bb630,ga5288a1d22+2903d499ea,gb58c049af0+d64f4d3760,gc28159a63d+3f2625acfc,gcab2d0539d+b12535109e,gcf0d15dbbd+46a3f46ba9,gda6a2b7d83+46a3f46ba9,gdaeeff99f8+1711a396fd,ge79ae78c31+3f2625acfc,gef2f8181fd+0a71e47438,gf0baf85859+c1f95f4921,gfa517265be+6aee506743,gfa999e8aa5+17cd334064,w.2024.51
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)