BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
RealLimits.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Fit/Param/RealLimits.h
6 //! @brief Defines class RealLimits.
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #ifndef BORNAGAIN_FIT_PARAM_REALLIMITS_H
16 #define BORNAGAIN_FIT_PARAM_REALLIMITS_H
17 
18 #include <ostream>
19 #include <string>
20 
21 //! Limits for a real fit parameter.
22 //! @ingroup fitting
23 
24 class RealLimits {
25 public:
26  //! Default constructor creates a "limitless" instance.
27  RealLimits();
28 
29  //! Creates a copy with scaled boundaries, if applicable
30  RealLimits scaledLimits(double factor) const;
31 
32  //! if has lower limit
33  bool hasLowerLimit() const;
34 
35  //! Returns lower limit
36  double lowerLimit() const;
37 
38  //! Sets lower limit
39  void setLowerLimit(double value);
40 
41  //! remove lower limit
42  void removeLowerLimit();
43 
44  //! if has upper limit
45  bool hasUpperLimit() const;
46 
47  //! Returns upper limit
48  double upperLimit() const;
49 
50  //! Sets upper limit
51  void setUpperLimit(double value);
52 
53  //! remove upper limit
54  void removeUpperLimit();
55 
56  //! if has lower and upper limit
57  bool hasLowerAndUpperLimits() const;
58 
59  //! Sets lower and upper limits
60  void setLimits(double xmin, double xmax);
61 
62  //! remove limits
63  void removeLimits();
64 
65  //! Returns true if proposed value is in limits range
66  bool isInRange(double value) const;
67 
68  //! Creates an object bounded from the left
69  static RealLimits lowerLimited(double bound_value);
70 
71  //! Creates an object which can have only positive values (>0., zero is not included)
72  static RealLimits positive();
73 
74  //! Creates an object which can have only positive values with 0. included
75  static RealLimits nonnegative();
76 
77  //! Creates an object bounded from the right
78  static RealLimits upperLimited(double bound_value);
79 
80  //! Creates an object bounded from the left and right
81  static RealLimits limited(double left_bound_value, double right_bound_value);
82 
83  //! Creates an object without bounds (default)
84  static RealLimits limitless();
85 
86  std::string toString() const;
87 
88  //! Throws if value is outside limits. Parameter 'name' is for exception message.
89  void check(const std::string& name, double value) const;
90 
91  //! Prints class
92  friend std::ostream& operator<<(std::ostream& ostr, const RealLimits& m)
93  {
94  ostr << m.toString();
95  return ostr;
96  }
97 
98  bool operator==(const RealLimits& other) const;
99  bool operator!=(const RealLimits& other) const;
100 
101  bool isLimitless() const;
102  bool isPositive() const;
103  bool isNonnegative() const;
104  bool isLowerLimited() const;
105  bool isUpperLimited() const;
106  bool isLimited() const;
107 
108 protected:
109  RealLimits(bool has_lower_limit, bool has_upper_limit, double lower_limit, double upper_limit);
110 
111  bool m_has_lower_limit; //! parameter has lower bound
112  bool m_has_upper_limit; //! parameter has upper bound
113  double m_lower_limit; //! minimum allowed value
114  double m_upper_limit; //! maximum allowed value
115 };
116 
117 #endif // BORNAGAIN_FIT_PARAM_REALLIMITS_H
Limits for a real fit parameter.
Definition: RealLimits.h:24
bool isLimited() const
Definition: RealLimits.cpp:218
static RealLimits limitless()
Creates an object without bounds (default)
Definition: RealLimits.cpp:139
std::string toString() const
Definition: RealLimits.cpp:144
bool m_has_lower_limit
Definition: RealLimits.h:111
void removeLowerLimit()
remove lower limit
Definition: RealLimits.cpp:60
static RealLimits upperLimited(double bound_value)
Creates an object bounded from the right.
Definition: RealLimits.cpp:129
bool isLowerLimited() const
Definition: RealLimits.cpp:208
bool isPositive() const
Definition: RealLimits.cpp:197
bool operator==(const RealLimits &other) const
Definition: RealLimits.cpp:180
bool hasUpperLimit() const
if has upper limit
Definition: RealLimits.cpp:66
static RealLimits lowerLimited(double bound_value)
Creates an object bounded from the left.
Definition: RealLimits.cpp:114
bool hasLowerAndUpperLimits() const
if has lower and upper limit
Definition: RealLimits.cpp:88
static RealLimits positive()
Creates an object which can have only positive values (>0., zero is not included)
Definition: RealLimits.cpp:119
friend std::ostream & operator<<(std::ostream &ostr, const RealLimits &m)
Prints class.
Definition: RealLimits.h:92
RealLimits scaledLimits(double factor) const
Creates a copy with scaled boundaries, if applicable.
Definition: RealLimits.cpp:38
bool isInRange(double value) const
Returns true if proposed value is in limits range.
Definition: RealLimits.cpp:105
double m_lower_limit
parameter has upper bound
Definition: RealLimits.h:113
bool m_has_upper_limit
parameter has lower bound
Definition: RealLimits.h:112
double upperLimit() const
Returns upper limit.
Definition: RealLimits.cpp:71
RealLimits()
Default constructor creates a "limitless" instance.
Definition: RealLimits.cpp:21
void setLowerLimit(double value)
Sets lower limit.
Definition: RealLimits.cpp:54
void check(const std::string &name, double value) const
Throws if value is outside limits. Parameter 'name' is for exception message.
Definition: RealLimits.cpp:170
double lowerLimit() const
Returns lower limit.
Definition: RealLimits.cpp:49
void removeUpperLimit()
remove upper limit
Definition: RealLimits.cpp:82
bool isNonnegative() const
Definition: RealLimits.cpp:203
bool operator!=(const RealLimits &other) const
Definition: RealLimits.cpp:187
void setLimits(double xmin, double xmax)
Sets lower and upper limits.
Definition: RealLimits.cpp:93
bool isLimitless() const
Definition: RealLimits.cpp:192
static RealLimits nonnegative()
Creates an object which can have only positive values with 0. included.
Definition: RealLimits.cpp:124
double m_upper_limit
minimum allowed value
Definition: RealLimits.h:114
void removeLimits()
remove limits
Definition: RealLimits.cpp:99
bool isUpperLimited() const
Definition: RealLimits.cpp:213
static RealLimits limited(double left_bound_value, double right_bound_value)
Creates an object bounded from the left and right.
Definition: RealLimits.cpp:134
bool hasLowerLimit() const
if has lower limit
Definition: RealLimits.cpp:44
void setUpperLimit(double value)
Sets upper limit.
Definition: RealLimits.cpp:76