ROL
ROL_PlusFunction.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
44 #ifndef ROL_PLUSFUNCTION_HPP
45 #define ROL_PLUSFUNCTION_HPP
46 
47 #include "ROL_Types.hpp"
48 #include "ROL_PositiveFunction.hpp"
49 #include "ROL_Distribution.hpp"
50 
51 namespace ROL {
52 
53 template<class Real>
54 class PlusFunction : public PositiveFunction<Real> {
55 private:
56  Teuchos::RCP<Distribution<Real> > dist_;
57  Real param_;
58 
59 public:
60  PlusFunction(Teuchos::RCP<Distribution<Real> > &dist, Real param = 1.e-2) : dist_(dist) {
61  param_ = ((param <= 0) ? 1.e-2 : param);
62  }
63 
64  Real evaluate(Real input, int deriv) {
65  Real val = 0.0;
66  switch(deriv) {
67  case 0: val = param_*dist_->intcdf(input/param_); break;
68  case 1: val = dist_->cdf(input/param_); break;
69  case 2: val = dist_->pdf(input/param_)/param_; break;
70  }
71  return val;
72  }
73 
74  void test(Real x) {
75  // FIRST DERIVATIVE
76  Real vx = evaluate(x,0);
77  Real vy = 0.0;
78  Real dv = evaluate(x,1);
79  Real t = 1.0;
80  Real diff = 0.0;
81  Real err = 0.0;
82  std::cout << std::right << std::setw(20) << "CHECK PLUS FUNCTION: p'(x) with x = "
83  << x << " is correct?\n";
84  std::cout << std::right << std::setw(20) << "t"
85  << std::setw(20) << "p'(x)"
86  << std::setw(20) << "(p(x+t)-p(x))/t"
87  << std::setw(20) << "Error"
88  << "\n";
89  for (int i = 0; i < 13; i++) {
90  vy = evaluate(x+t,0);
91  diff = (vy-vx)/t;
92  err = std::abs(diff-dv);
93  std::cout << std::scientific << std::setprecision(11) << std::right
94  << std::setw(20) << t
95  << std::setw(20) << dv
96  << std::setw(20) << diff
97  << std::setw(20) << err
98  << "\n";
99  t *= 0.1;
100  }
101  std::cout << "\n";
102  // SECOND DERIVATIVE
103  vx = evaluate(x,1);
104  vy = 0.0;
105  dv = evaluate(x,2);
106  t = 1.0;
107  diff = 0.0;
108  err = 0.0;
109  std::cout << std::right << std::setw(20) << "CHECK PLUS FUNCTION: p''(x) with x = "
110  << x << " is correct?\n";
111  std::cout << std::right << std::setw(20) << "t"
112  << std::setw(20) << "p''(x)"
113  << std::setw(20) << "(p'(x+t)-p'(x))/t"
114  << std::setw(20) << "Error"
115  << "\n";
116  for (int i = 0; i < 13; i++) {
117  vy = evaluate(x+t,1);
118  diff = (vy-vx)/t;
119  err = std::abs(diff-dv);
120  std::cout << std::scientific << std::setprecision(11) << std::right
121  << std::setw(20) << t
122  << std::setw(20) << dv
123  << std::setw(20) << diff
124  << std::setw(20) << err
125  << "\n";
126  t *= 0.1;
127  }
128  std::cout << "\n";
129  }
130 };
131 
132 }
133 
134 #endif
Contains definitions of custom data types in ROL.
Teuchos::RCP< Distribution< Real > > dist_
PlusFunction(Teuchos::RCP< Distribution< Real > > &dist, Real param=1.e-2)
Real evaluate(Real input, int deriv)