ROL
ROL_AbsoluteValue.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_ABSOLUTEVALUE_HPP
45 #define ROL_ABSOLUTEVALUE_HPP
46 
47 #include "ROL_Types.hpp"
48 #include "ROL_PositiveFunction.hpp"
49 
50 namespace ROL {
51 
58 };
59 
60 template<class Real>
61 class AbsoluteValue : public PositiveFunction<Real> {
62 private:
63  Real param_;
65 
66 public:
67  AbsoluteValue(Real param = 1.e2, EAbsoluteValue eav = ABSOLUTEVALUE_TRUE) : param_(param), eav_(eav) {
68  if ( eav != ABSOLUTEVALUE_TRUE && std::abs(param) < ROL_EPSILON ) { param_ = 1.e2; }
69  }
70  Real evaluate(Real input, int deriv) {
71  Real val = 0.0;
72  switch(this->eav_) {
73  case ABSOLUTEVALUE_TRUE: val = this->true_absolute_value(input,deriv); break;
74  case ABSOLUTEVALUE_SQUAREROOT: val = this->sqrt_absolute_value(input,deriv); break;
75  case ABSOLUTEVALUE_SQRTDENOM: val = this->sqrtd_absolute_value(input,deriv); break;
76  case ABSOLUTEVALUE_C2: val = this->c2_absolute_value(input,deriv); break;
77  default:
78  TEUCHOS_TEST_FOR_EXCEPTION( true, std::invalid_argument,
79  ">>> ERROR (ROL::AbsoluteValue): Absolute value approximation not defined!");
80  }
81  return val;
82  }
83 
84 private:
85  Real true_absolute_value( Real input, int deriv ) {
86  Real output = 0.0, e = 0.0;
87  if ( std::abs(this->param_) > ROL_EPSILON ) { e = 0.5/this->param_; }
88 
89  int region = 0;
90  if ( input < -e ) { region = -1; }
91  else if ( input > e ) { region = 1; }
92 
93  if ( deriv == 0 ) { output = std::abs(input); }
94  else if ( deriv == 1 ) { output = (input < 0.0 ? -1.0 : 1.0); }
95  else if ( deriv == 2 ) {
96  if ( region == -1 ) { output = 0.0; }
97  else if ( region == 0 ) { output = e*std::exp( -1.0/(1.0 - std::pow(std::abs(input/e),2.0)) ); }
98  else if ( region == 1 ) { output = 0.0; }
99  }
100  return output;
101  }
102 
103  Real sqrt_absolute_value( Real input, int deriv ) {
104  Real output = 0.0;
105  if ( deriv == 0 ) { output = std::sqrt(input*input + 1.0/this->param_); }
106  else if ( deriv == 1 ) { output = input/std::sqrt(input*input+1.0/this->param_); }
107  else if ( deriv == 2 ) { output = (1.0/this->param_)/std::pow(input*input+1.0/this->param_,1.5); }
108  return output;
109  }
110 
111  Real sqrtd_absolute_value( Real input, int deriv ) {
112  Real output = 0.0;
113  if ( deriv == 0 ) { output = input*input/std::sqrt(input*input + 1.0/this->param_); }
114  else if ( deriv == 1 ) { output = (2.0/this->param_*input+std::pow(input,3.0)) /
115  std::pow(input*input+1.0/this->param_,1.5); }
116  else if ( deriv == 2 ) { output = ((2.0/this->param_-input*input)/this->param_) /
117  std::pow(input*input+1.0/this->param_,2.5); }
118  return output;
119  }
120 
121  Real c2_absolute_value( Real input, int deriv ) {
122  Real output = 0.0, e = 1.0;
123  if ( std::abs(this->param_) > ROL_EPSILON ) { e = 0.5/this->param_; }
124 
125  int region = 0;
126  if ( input < -e ) { region = -1; }
127  else if ( input > e ) { region = 1; }
128 
129  if ( deriv == 0 ) {
130  if ( std::abs(region) == 1 ) { output = std::abs(input); }
131  else if ( region == 0 ) { output = 1.875*std::pow(input*e,2.0) -
132  1.25 *std::pow(input*e,4.0) +
133  0.375*std::pow(input*e,6.0); }
134  }
135  else if ( deriv == 1 ) {
136  if ( std::abs(region) == 1 ) { output = (input < 0.0 ? -1.0 : 1.0); }
137  else if ( region == 0 ) { output = e*2.0*1.875*input*e -
138  e*4.0*1.25 *std::pow(input*e,3.0) +
139  e*6.0*0.375*std::pow(input*e,5.0); }
140  }
141  else if ( deriv == 2 ) {
142  if ( std::abs(region) == 1 ) { output = 0.0; }
143  else if ( region == 0 ) { output = e* 2.0*1.875 -
144  e*e*12.0*1.25 *std::pow(input*e,2.0) +
145  e*e*30.0*0.375*std::pow(input*e,4.0); }
146  }
147  return output;
148  }
149 };
150 
151 }
152 
153 #endif
Real sqrtd_absolute_value(Real input, int deriv)
Contains definitions of custom data types in ROL.
Real c2_absolute_value(Real input, int deriv)
Real sqrt_absolute_value(Real input, int deriv)
Real evaluate(Real input, int deriv)
Real true_absolute_value(Real input, int deriv)
AbsoluteValue(Real param=1.e2, EAbsoluteValue eav=ABSOLUTEVALUE_TRUE)
static const double ROL_EPSILON
Platform-dependent machine epsilon.
Definition: ROL_Types.hpp:115