BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
RealLimits.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Fit/Param/RealLimits.cpp
6 //! @brief Implements class Limits.
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 #include "Fit/Param/RealLimits.h"
16 #include <iomanip>
17 #include <iostream>
18 #include <limits>
19 #include <sstream>
20 
22  : m_has_lower_limit(false)
23  , m_has_upper_limit(false)
24  , m_lower_limit(0.)
25  , m_upper_limit(0.)
26 {
27 }
28 
29 RealLimits::RealLimits(bool has_lower_limit, bool has_upper_limit, double lower_limit,
30  double upper_limit)
31  : m_has_lower_limit(has_lower_limit)
32  , m_has_upper_limit(has_upper_limit)
33  , m_lower_limit(lower_limit)
34  , m_upper_limit(upper_limit)
35 {
36 }
37 
39 {
41  m_upper_limit * factor);
42 }
43 
45 {
46  return m_has_lower_limit;
47 }
48 
49 double RealLimits::lowerLimit() const
50 {
51  return m_lower_limit;
52 }
53 
54 void RealLimits::setLowerLimit(double value)
55 {
56  m_lower_limit = value;
57  m_has_lower_limit = true;
58 }
59 
61 {
62  m_lower_limit = 0.;
63  m_has_lower_limit = false;
64 }
65 
67 {
68  return m_has_upper_limit;
69 }
70 
71 double RealLimits::upperLimit() const
72 {
73  return m_upper_limit;
74 }
75 
76 void RealLimits::setUpperLimit(double value)
77 {
78  m_upper_limit = value;
79  m_has_upper_limit = true;
80 }
81 
83 {
84  m_upper_limit = 0.;
85  m_has_upper_limit = false;
86 }
87 
89 {
91 }
92 
93 void RealLimits::setLimits(double xmin, double xmax)
94 {
95  setLowerLimit(xmin);
96  setUpperLimit(xmax);
97 }
98 
100 {
103 }
104 
105 bool RealLimits::isInRange(double value) const
106 {
107  if (hasLowerLimit() && value < m_lower_limit)
108  return false;
109  if (hasUpperLimit() && value >= m_upper_limit)
110  return false;
111  return true;
112 }
113 
115 {
116  return RealLimits(true, false, bound_value, 0.);
117 }
118 
120 {
121  return lowerLimited(std::numeric_limits<double>::min());
122 }
123 
125 {
126  return lowerLimited(0.);
127 }
128 
130 {
131  return RealLimits(false, true, 0., bound_value);
132 }
133 
134 RealLimits RealLimits::limited(double left_bound_value, double right_bound_value)
135 {
136  return RealLimits(true, true, left_bound_value, right_bound_value);
137 }
138 
140 {
141  return RealLimits();
142 }
143 
144 std::string RealLimits::toString() const
145 {
146  std::ostringstream result;
147 
148  if (isLimitless())
149  result << "unlimited";
150 
151  else if (isPositive())
152  result << "positive";
153 
154  else if (isNonnegative())
155  result << "nonnegative";
156 
157  else if (isLowerLimited())
158  result << "lowerLimited(" << std::fixed << std::setprecision(2) << lowerLimit() << ")";
159 
160  else if (isUpperLimited())
161  result << "upperLimited(" << std::fixed << std::setprecision(2) << upperLimit() << ")";
162 
163  else if (isLimited())
164  result << "limited(" << std::fixed << std::setprecision(2) << lowerLimit() << ","
165  << std::fixed << std::setprecision(2) << upperLimit() << ")";
166 
167  return result.str();
168 }
169 
170 void RealLimits::check(const std::string& name, const double value) const
171 {
172  if (!isInRange(value)) {
173  std::ostringstream message;
174  message << "Parameter " << name << ": value " << value << " is out of bounds [" << *this
175  << "]\n";
176  throw std::runtime_error(message.str());
177  }
178 }
179 
180 bool RealLimits::operator==(const RealLimits& other) const
181 {
182  return (m_has_lower_limit == other.m_has_lower_limit)
184  && (m_lower_limit == other.m_lower_limit) && (m_upper_limit == other.m_upper_limit);
185 }
186 
187 bool RealLimits::operator!=(const RealLimits& other) const
188 {
189  return !(*this == other);
190 }
191 
193 {
194  return !hasLowerLimit() && !hasUpperLimit();
195 }
196 
198 {
199  return hasLowerLimit() && !hasUpperLimit()
200  && lowerLimit() == std::numeric_limits<double>::min();
201 }
202 
204 {
205  return hasLowerLimit() && !hasUpperLimit() && lowerLimit() == 0.0;
206 }
207 
209 {
210  return hasLowerLimit() && !hasUpperLimit();
211 }
212 
214 {
215  return !hasLowerLimit() && hasUpperLimit();
216 }
217 
219 {
220  return hasLowerLimit() && hasUpperLimit();
221 }
Defines class RealLimits.
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
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