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