File: RealLimits.h

package info (click to toggle)
bornagain 23.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,956 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 356; ruby: 173; xml: 130; makefile: 45; ansic: 24
file content (88 lines) | stat: -rw-r--r-- 2,907 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      Fit/Param/RealLimits.h
//! @brief     Defines class RealLimits.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#ifndef BORNAGAIN_FIT_PARAM_REALLIMITS_H
#define BORNAGAIN_FIT_PARAM_REALLIMITS_H

#include <ostream>
#include <string>

//! Limits for a real fit parameter.

class RealLimits {
public:
    //! Default constructor creates a "limitless" instance.
    RealLimits();

    //! Creates an object bounded from the left
    static RealLimits lowerLimited(double bound_value);

    //! Creates an object which can have only positive values (>0., zero is not included)
    static RealLimits positive();

    //! Creates an object which can have only positive values with 0. included
    static RealLimits nonnegative();

    //! Creates an object bounded from the right
    static RealLimits upperLimited(double bound_value);

    //! Creates an object bounded from the left and right
    static RealLimits limited(double left_bound_value, double right_bound_value);

    //! Creates an object without bounds (default)
    static RealLimits limitless();

    //! Returns lower limit
    double min() const { return m_lower_limit; }

    //! Returns upper limit
    double max() const { return m_upper_limit; }

    //! Returns true if proposed value is in limits range
    bool isInRange(double value) const;

    //! Returns value if it is inside. Otherwise returns the lower or upper bound.
    double clamp(double value) const;

    std::string toString() const;

    //! Prints class
    friend std::ostream& operator<<(std::ostream& ostr, const RealLimits& m)
    {
        ostr << m.toString();
        return ostr;
    }

    bool operator==(const RealLimits& other) const;
    bool operator!=(const RealLimits& other) const;

    bool hasLowerLimit() const { return m_has_lower_limit; }
    bool hasUpperLimit() const { return m_has_upper_limit; }
    bool isLimitless() const;
    bool isPositive() const;
    bool isNonnegative() const;
    bool isLowerLimited() const;
    bool isUpperLimited() const;
    bool isLimited() const;

private:
    RealLimits(bool has_lower_limit, bool has_upper_limit, double lower_limit, double upper_limit);

    bool m_has_lower_limit; //! parameter has lower bound
    bool m_has_upper_limit; //! parameter has upper bound
    double m_lower_limit;   //! minimum allowed value
    double m_upper_limit;   //! maximum allowed value
};

#endif // BORNAGAIN_FIT_PARAM_REALLIMITS_H