Loading [MathJax]/extensions/tex2jax.js
LSST Applications g04a91732dc+7fec47d7bc,g07dc498a13+5ab4d22ec3,g0fba68d861+565de8e5d5,g1409bbee79+5ab4d22ec3,g1a7e361dbc+5ab4d22ec3,g1fd858c14a+11200c7927,g20f46db602+25d63fd678,g35bb328faa+fcb1d3bbc8,g4d2262a081+61302e889d,g4d39ba7253+d05e267ece,g4e0f332c67+5d362be553,g53246c7159+fcb1d3bbc8,g60b5630c4e+d05e267ece,g78460c75b0+2f9a1b4bcd,g786e29fd12+cf7ec2a62a,g7b71ed6315+fcb1d3bbc8,g8048e755c2+a1301e4c20,g8852436030+163ceb82d8,g89139ef638+5ab4d22ec3,g89e1512fd8+fbb808ebce,g8d6b6b353c+d05e267ece,g9125e01d80+fcb1d3bbc8,g989de1cb63+5ab4d22ec3,g9f33ca652e+8abe617c77,ga9baa6287d+d05e267ece,gaaedd4e678+5ab4d22ec3,gabe3b4be73+1e0a283bba,gb1101e3267+fefe9ce5b1,gb58c049af0+f03b321e39,gb90eeb9370+824c420ec4,gc741bbaa4f+77ddc33078,gcf25f946ba+163ceb82d8,gd315a588df+0f88d5218e,gd6cbbdb0b4+c8606af20c,gd9a9a58781+fcb1d3bbc8,gde0f65d7ad+e6bd566e97,ge278dab8ac+932305ba37,ge82c20c137+76d20ab76d,w.2025.10
LSST Data Management Base Package
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 {
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)");
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 }
84
85 void set_min(T min) {
86 _check_min(min);
87 _min = min;
88 };
89
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
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 infinity(T... args)
T isnan(T... args)
std::string type_name_str(bool strip_namespace=false, const std::string_view &namespace_str=detail::NAMESPACE_SEPARATOR)
Get a string representation of an arbitrary C++ type, potentially modifying its namespace prefix.
Definition type_name.h:101
T to_string(T... args)