BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
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), m_has_upper_limit(false), m_lower_limit(0.), m_upper_limit(0.)
23 {
24 }
25 
26 RealLimits::RealLimits(bool has_lower_limit, bool has_upper_limit, double lower_limit,
27  double upper_limit)
28  : m_has_lower_limit(has_lower_limit)
29  , m_has_upper_limit(has_upper_limit)
30  , m_lower_limit(lower_limit)
31  , m_upper_limit(upper_limit)
32 {
33 }
34 
36 {
37  return m_has_lower_limit;
38 }
39 
40 double RealLimits::lowerLimit() const
41 {
42  return m_lower_limit;
43 }
44 
45 void RealLimits::setLowerLimit(double value)
46 {
47  m_lower_limit = value;
48  m_has_lower_limit = true;
49 }
50 
52 {
53  m_lower_limit = 0.;
54  m_has_lower_limit = false;
55 }
56 
58 {
59  return m_has_upper_limit;
60 }
61 
62 double RealLimits::upperLimit() const
63 {
64  return m_upper_limit;
65 }
66 
67 void RealLimits::setUpperLimit(double value)
68 {
69  m_upper_limit = value;
70  m_has_upper_limit = true;
71 }
72 
74 {
75  m_upper_limit = 0.;
76  m_has_upper_limit = false;
77 }
78 
80 {
82 }
83 
84 void RealLimits::setLimits(double xmin, double xmax)
85 {
86  setLowerLimit(xmin);
87  setUpperLimit(xmax);
88 }
89 
91 {
94 }
95 
96 bool RealLimits::isInRange(double value) const
97 {
98  if (hasLowerLimit() && value < m_lower_limit)
99  return false;
100  if (hasUpperLimit() && value >= m_upper_limit)
101  return false;
102  return true;
103 }
104 
106 {
107  return RealLimits(true, false, bound_value, 0.);
108 }
109 
111 {
112  return lowerLimited(std::numeric_limits<double>::min());
113 }
114 
116 {
117  return lowerLimited(0.);
118 }
119 
121 {
122  return RealLimits(false, true, 0., bound_value);
123 }
124 
125 RealLimits RealLimits::limited(double left_bound_value, double right_bound_value)
126 {
127  return RealLimits(true, true, left_bound_value, right_bound_value);
128 }
129 
131 {
132  return RealLimits();
133 }
134 
135 std::string RealLimits::toString() const
136 {
137  std::ostringstream result;
138 
139  if (isLimitless())
140  result << "unlimited";
141 
142  else if (isPositive())
143  result << "positive";
144 
145  else if (isNonnegative())
146  result << "nonnegative";
147 
148  else if (isLowerLimited())
149  result << "lowerLimited(" << std::fixed << std::setprecision(2) << lowerLimit() << ")";
150 
151  else if (isUpperLimited())
152  result << "upperLimited(" << std::fixed << std::setprecision(2) << upperLimit() << ")";
153 
154  else if (isLimited())
155  result << "limited(" << std::fixed << std::setprecision(2) << lowerLimit() << ","
156  << std::fixed << std::setprecision(2) << upperLimit() << ")";
157 
158  return result.str();
159 }
160 
161 bool RealLimits::operator==(const RealLimits& other) const
162 {
163  return (m_has_lower_limit == other.m_has_lower_limit)
165  && (m_lower_limit == other.m_lower_limit) && (m_upper_limit == other.m_upper_limit);
166 }
167 
168 bool RealLimits::operator!=(const RealLimits& other) const
169 {
170  return !(*this == other);
171 }
172 
174 {
175  return !hasLowerLimit() && !hasUpperLimit();
176 }
177 
179 {
180  return hasLowerLimit() && !hasUpperLimit()
181  && lowerLimit() == std::numeric_limits<double>::min();
182 }
183 
185 {
186  return hasLowerLimit() && !hasUpperLimit() && lowerLimit() == 0.0;
187 }
188 
190 {
191  return hasLowerLimit() && !hasUpperLimit();
192 }
193 
195 {
196  return !hasLowerLimit() && hasUpperLimit();
197 }
198 
200 {
201  return hasLowerLimit() && hasUpperLimit();
202 }
Defines class RealLimits.
Limits for a real fit parameter.
Definition: RealLimits.h:24
bool isLimited() const
Definition: RealLimits.cpp:199
static RealLimits limitless()
Creates an object withoud bounds (default)
Definition: RealLimits.cpp:130
std::string toString() const
Definition: RealLimits.cpp:135
bool m_has_lower_limit
Definition: RealLimits.h:104
void removeLowerLimit()
remove lower limit
Definition: RealLimits.cpp:51
static RealLimits upperLimited(double bound_value)
Creates an object bounded from the right.
Definition: RealLimits.cpp:120
bool isLowerLimited() const
Definition: RealLimits.cpp:189
bool isPositive() const
Definition: RealLimits.cpp:178
bool operator==(const RealLimits &other) const
Definition: RealLimits.cpp:161
bool hasUpperLimit() const
if has upper limit
Definition: RealLimits.cpp:57
static RealLimits lowerLimited(double bound_value)
Creates an object bounded from the left.
Definition: RealLimits.cpp:105
bool hasLowerAndUpperLimits() const
if has lower and upper limit
Definition: RealLimits.cpp:79
static RealLimits positive()
Creates an object which can have only positive values (>0., zero is not included)
Definition: RealLimits.cpp:110
bool isInRange(double value) const
returns true if proposed value is in limits range
Definition: RealLimits.cpp:96
double m_lower_limit
parameter has upper bound
Definition: RealLimits.h:106
bool m_has_upper_limit
parameter has lower bound
Definition: RealLimits.h:105
double upperLimit() const
Returns upper limit.
Definition: RealLimits.cpp:62
void setLowerLimit(double value)
Sets lower limit.
Definition: RealLimits.cpp:45
double lowerLimit() const
Returns lower limit.
Definition: RealLimits.cpp:40
void removeUpperLimit()
remove upper limit
Definition: RealLimits.cpp:73
bool isNonnegative() const
Definition: RealLimits.cpp:184
bool operator!=(const RealLimits &other) const
Definition: RealLimits.cpp:168
void setLimits(double xmin, double xmax)
Sets lower and upper limits.
Definition: RealLimits.cpp:84
bool isLimitless() const
Definition: RealLimits.cpp:173
static RealLimits nonnegative()
Creates an object which can have only positive values with 0. included.
Definition: RealLimits.cpp:115
double m_upper_limit
minimum allowed value
Definition: RealLimits.h:107
void removeLimits()
remove limits
Definition: RealLimits.cpp:90
bool isUpperLimited() const
Definition: RealLimits.cpp:194
static RealLimits limited(double left_bound_value, double right_bound_value)
Creates an object bounded from the left and right.
Definition: RealLimits.cpp:125
bool hasLowerLimit() const
if has lower limit
Definition: RealLimits.cpp:35
void setUpperLimit(double value)
Sets upper limit.
Definition: RealLimits.cpp:67