LSST Applications g04e9c324dd+8c5ae1fdc5,g134cb467dc+1b3060144d,g18429d2f64+f642bf4753,g199a45376c+0ba108daf9,g1fd858c14a+2dcf163641,g262e1987ae+7b8c96d2ca,g29ae962dfc+3bd6ecb08a,g2cef7863aa+aef1011c0b,g35bb328faa+8c5ae1fdc5,g3fd5ace14f+53e1a9e7c5,g4595892280+fef73a337f,g47891489e3+2efcf17695,g4d44eb3520+642b70b07e,g53246c7159+8c5ae1fdc5,g67b6fd64d1+2efcf17695,g67fd3c3899+b70e05ef52,g74acd417e5+317eb4c7d4,g786e29fd12+668abc6043,g87389fa792+8856018cbb,g89139ef638+2efcf17695,g8d7436a09f+3be3c13596,g8ea07a8fe4+9f5ccc88ac,g90f42f885a+a4e7b16d9b,g97be763408+ad77d7208f,g9dd6db0277+b70e05ef52,ga681d05dcb+a3f46e7fff,gabf8522325+735880ea63,gac2eed3f23+2efcf17695,gb89ab40317+2efcf17695,gbf99507273+8c5ae1fdc5,gd8ff7fe66e+b70e05ef52,gdab6d2f7ff+317eb4c7d4,gdc713202bf+b70e05ef52,gdfd2d52018+b10e285e0f,ge365c994fd+310e8507c4,ge410e46f29+2efcf17695,geaed405ab2+562b3308c0,gffca2db377+8c5ae1fdc5,w.2025.35
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 {
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)