ROL
ROL_Triangle.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_TRIANGLE_HPP
11 #define ROL_TRIANGLE_HPP
12 
13 #include "ROL_Distribution.hpp"
14 #include "ROL_ParameterList.hpp"
15 
16 namespace ROL {
17 
18 template<class Real>
19 class Triangle : public Distribution<Real> {
20 private:
21  Real a_;
22  Real b_;
23  Real c_;
24 
25 public:
26  Triangle(const Real a = 0., const Real b = 0.5, const Real c = 1.)
27  : a_(std::min(a,std::min(b,c))),
28  b_(std::max(std::min(a,b),std::min(std::max(a,b),c))),
29  c_(std::max(a,std::max(b,c))) {}
30 
31  Triangle(ROL::ParameterList &parlist) {
32  Real a = parlist.sublist("SOL").sublist("Distribution").sublist("Triangle").get("Lower Bound",0.);
33  Real b = parlist.sublist("SOL").sublist("Distribution").sublist("Triangle").get("Peak Location",0.5);
34  Real c = parlist.sublist("SOL").sublist("Distribution").sublist("Triangle").get("Upper Bound",1.);
35  a_ = std::min(a,std::min(b,c));
36  b_ = std::max(std::min(a,b),std::min(std::max(a,b),c));
37  c_ = std::max(a,std::max(b,c));
38  }
39 
40  Real evaluatePDF(const Real input) const {
41  Real d1 = b_-a_, d2 = c_-b_, d = c_-a_;
42  return ((input >= a_ && input < b_) ? 2.0*(input-a_)/(d*d1) :
43  ((input >= b_ && input < c_) ? 2.0*(c_-input)/(d*d2) :
44  0.0));
45  }
46 
47  Real evaluateCDF(const Real input) const {
48  Real d1 = b_-a_, d2 = c_-b_, d = c_-a_;
49  return ((input < a_) ? 0.0 :
50  ((input >= a_ && input < b_) ?
51  std::pow(input-a_,2.0)/(d*d1) :
52  ((input >= b_ && input < c_) ?
53  1.0-std::pow(c_-input,2.0)/(d*d2) :
54  1.0)));
55  }
56 
57  Real integrateCDF(const Real input) const {
58  Real d1 = b_-a_, d2 = c_-b_, d = c_-a_;
59  return ((input < a_) ? 0.0 :
60  ((input >= a_ && input < b_) ?
61  std::pow(input-a_,3.0)/(3.0*d*d1) :
62  ((input >= b_ && input < c_) ?
63  d1*d1/(3.0*d)+(input-b_)+(std::pow(c_-input,3.0)-d2*d2*d2)/(3.0*d*d2) :
64  d1*d1/(3.0*d)+(input-b_)-d2*d2/(3.0*d))));
65  }
66 
67  Real invertCDF(const Real input) const {
68  Real d1 = b_-a_, d2 = c_-b_, d = c_-a_;
69  return ((input <= d1/d) ? a_ + std::sqrt(input*d1*d) :
70  c_ - std::sqrt((1.0-input)*d2*d));
71  }
72 
73  Real moment(const size_t m) const {
74  Real d1 = b_-a_, d2 = c_-b_, d = c_-a_;
75  Real am1 = std::pow(a_,m+1), am2 = a_*am1;
76  Real bm1 = std::pow(b_,m+1), bm2 = b_*bm1;
77  Real cm1 = std::pow(c_,m+1), cm2 = c_*cm1;
78  return (2./d)*(((bm2-am2)/((Real)m+2)-a_*(bm1-am1)/((Real)m+1))/d1
79  +(c_*(cm1-bm1)/((Real)m+1)-(cm2-bm2)/((Real)m+2))/d2);
80  }
81 
82  Real lowerBound(void) const {
83  return a_;
84  }
85 
86  Real upperBound(void) const {
87  return c_;
88  }
89 
90  void test(std::ostream &outStream = std::cout ) const {
91  size_t size = 7;
92  std::vector<Real> X(size,0.);
93  std::vector<int> T(size,0);
94  X[0] = a_-4.*(Real)rand()/(Real)RAND_MAX;
95  T[0] = 0;
96  X[1] = a_;
97  T[1] = 1;
98  X[2] = (b_-a_)*(Real)rand()/(Real)RAND_MAX + a_;
99  T[2] = 0;
100  X[3] = b_;
101  T[3] = 1;
102  X[4] = (c_-b_)*(Real)rand()/(Real)RAND_MAX + b_;
103  T[4] = 0;
104  X[5] = c_;
105  T[5] = 1;
106  X[6] = c_+4.*(Real)rand()/(Real)RAND_MAX;
107  T[6] = 0;
108  Distribution<Real>::test(X,T,outStream);
109  }
110 };
111 
112 }
113 
114 #endif
void test(std::ostream &outStream=std::cout) const
Triangle(const Real a=0., const Real b=0.5, const Real c=1.)
Real moment(const size_t m) const
Real upperBound(void) const
Real integrateCDF(const Real input) const
Real lowerBound(void) const
Real invertCDF(const Real input) const
virtual void test(std::ostream &outStream=std::cout) const
Real evaluatePDF(const Real input) const
Triangle(ROL::ParameterList &parlist)
Real evaluateCDF(const Real input) const