ROL
ROL_Uniform.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef ROL_UNIFORM_HPP
11 #define ROL_UNIFORM_HPP
12 
13 #include "ROL_Distribution.hpp"
14 #include "ROL_ParameterList.hpp"
15 
16 namespace ROL {
17 
18 template<class Real>
19 class Uniform : public Distribution<Real> {
20 private:
21  Real a_;
22  Real b_;
23 
24 public:
25  Uniform(const Real lo = 0., const Real up = 1.)
26  : a_((lo < up) ? lo : up), b_((up > lo) ? up : lo) {}
27 
28  Uniform(ROL::ParameterList &parlist) {
29  a_ = parlist.sublist("SOL").sublist("Distribution").sublist("Uniform").get("Lower Bound",0.);
30  b_ = parlist.sublist("SOL").sublist("Distribution").sublist("Uniform").get("Upper Bound",1.);
31  Real tmp = a_;
32  a_ = std::min(a_,b_);
33  b_ = std::max(b_,tmp);
34  }
35 
36  Real evaluatePDF(const Real input) const {
37  return ((input >= a_ && input <= b_) ? 1.0/(b_-a_) : 0.0);
38  }
39 
40  Real evaluateCDF(const Real input) const {
41  return ((input < a_) ? 0.0 : ((input > b_) ? 1.0 : (input-a_)/(b_-a_)));
42  }
43 
44  Real integrateCDF(const Real input) const {
45  return ((input < a_) ? 0.0 : ((input > b_) ? input - 0.5*(a_+b_) :
46  0.5*std::pow(input-a_,2.0)/(b_-a_)));
47  }
48 
49  Real invertCDF(const Real input) const {
50  return a_ + input*(b_-a_);
51  }
52 
53  Real moment(const size_t m) const {
54  return (std::pow(b_,m+1)-std::pow(a_,m+1))/((Real)(m+1)*(b_-a_));
55  }
56 
57  Real lowerBound(void) const {
58  return a_;
59  }
60 
61  Real upperBound(void) const {
62  return b_;
63  }
64 
65  void test(std::ostream &outStream = std::cout ) const {
66  size_t size = 5;
67  std::vector<Real> X(size,0.);
68  std::vector<int> T(size,0);
69  X[0] = a_-4.*(Real)rand()/(Real)RAND_MAX;
70  T[0] = 0;
71  X[1] = a_;
72  T[1] = 1;
73  X[2] = (b_-a_)*(Real)rand()/(Real)RAND_MAX + a_;
74  T[2] = 0;
75  X[3] = b_;
76  T[3] = 1;
77  X[4] = b_+4.*(Real)rand()/(Real)RAND_MAX;
78  T[4] = 0;
79  Distribution<Real>::test(X,T,outStream);
80  }
81 };
82 
83 }
84 
85 #endif
Real evaluatePDF(const Real input) const
Definition: ROL_Uniform.hpp:36
Real moment(const size_t m) const
Definition: ROL_Uniform.hpp:53
Real evaluateCDF(const Real input) const
Definition: ROL_Uniform.hpp:40
Real lowerBound(void) const
Definition: ROL_Uniform.hpp:57
Uniform(const Real lo=0., const Real up=1.)
Definition: ROL_Uniform.hpp:25
Real invertCDF(const Real input) const
Definition: ROL_Uniform.hpp:49
Uniform(ROL::ParameterList &parlist)
Definition: ROL_Uniform.hpp:28
virtual void test(std::ostream &outStream=std::cout) const
void test(std::ostream &outStream=std::cout) const
Definition: ROL_Uniform.hpp:65
Real upperBound(void) const
Definition: ROL_Uniform.hpp:61
Real integrateCDF(const Real input) const
Definition: ROL_Uniform.hpp:44