Code-Eli  0.3.6
newton_raphson_constrained_method.hpp
Go to the documentation of this file.
1 /*********************************************************************************
2 * Copyright (c) 2013 David D. Marshall <ddmarsha@calpoly.edu>
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * David D. Marshall - initial code and implementation
11 ********************************************************************************/
12 
13 #ifndef eli_mutil_nls_newton_raphson_constrained_method_hpp
14 #define eli_mutil_nls_newton_raphson_constrained_method_hpp
15 
16 #include <limits>
17 #include <algorithm>
18 
19 #include "eli/code_eli.hpp"
20 
22 
23 namespace eli
24 {
25  namespace mutil
26  {
27  namespace nls
28  {
29  template<typename data__>
31  {
32  public:
33  typedef data__ data_type;
34 
36  {
41  };
42 
43  public:
46  {
47  }
48 
50  : newton_raphson_method<data_type>(nrm), xmin(nrm.xmin), xmax(nrm.xmax), xmin_cond(nrm.xmin_cond), xmax_cond(nrm.xmax_cond)
51  {
52  }
53 
55  {
56  }
57 
58  void set_periodic_condition(const data_type &dmin, const data_type &dmax)
59  {
60  xmin=dmin;
61  xmax=dmax;
64  }
66  {
69  }
70 
72  void set_lower_condition(const data_type &d, end_condition_usage ec)
73  {
74  if ( (xmin_cond==NRC_PERIODIC) && (ec!=NRC_PERIODIC) )
75  {
77  }
78  if (ec==NRC_PERIODIC)
79  {
81  }
82 
83  xmin=d;
84  xmin_cond=ec;
85  }
86  void get_lower_condition(data_type &d, end_condition_usage &ec)
87  {
88  d=xmin;
89  ec=xmin_cond;
90  }
91 
93  void set_upper_condition(const data_type &d, end_condition_usage ec)
94  {
95  if ( (xmax_cond==NRC_PERIODIC) && (ec!=NRC_PERIODIC) )
96  {
98  }
99  if (ec==NRC_PERIODIC)
100  {
102  }
103 
104  xmax=d;
105  xmax_cond=ec;
106  }
107  void get_upper_condition(data_type &d, end_condition_usage &ec)
108  {
109  d=xmax;
110  ec=xmax_cond;
111  }
112 
113  private:
114  virtual data_type calculate_delta_factor(const data_type &x, const data_type &dx) const
115  {
116  data_type xnew(x+dx);
117 
118  // check if min threshold is hit
119  switch(xmin_cond)
120  {
121  case(NRC_EXCLUSIVE):
122  {
123  if (xnew<xmin)
124  {
125  return (xmin-x);
126  }
127  break;
128  }
129  case(NRC_INCLUSIVE):
130  {
131  if (xnew<=xmin)
132  {
133  return (xmin-x)*(1-std::numeric_limits<data_type>::epsilon());
134  }
135  break;
136  }
137  case(NRC_PERIODIC):
138  {
139  data_type period(xmax-xmin);
140 
141  assert(xmax>xmin);
142  assert(period>0);
143 
144  if (xnew<xmin)
145  {
146  xnew-=period*std::floor((xnew-xmin)/period);
147  assert(xnew>=xmin);
148  assert(xnew<=xmax);
149  }
150  break;
151  }
152  default:
153  case(NRC_NOT_USED):
154  {
155  break;
156  }
157  }
158 
159  // check if max threshold is hit
160  switch(xmax_cond)
161  {
162  case(NRC_EXCLUSIVE):
163  {
164  if (xnew>xmax)
165  {
166  return (xmax-x);
167  }
168  break;
169  }
170  case(NRC_INCLUSIVE):
171  {
172  if (xnew>=xmax)
173  {
174  return (xmax-x)*(1-std::numeric_limits<data_type>::epsilon());
175  }
176  break;
177  }
178  case(NRC_PERIODIC):
179  {
180  data_type period(xmax-xmin);
181 
182  assert(xmax>xmin);
183  assert(period>0);
184 
185  if (xnew>xmax)
186  {
187  xnew-=period*std::ceil((xnew-xmax)/period);
188  assert(xnew>=xmin);
189  assert(xnew<=xmax);
190  }
191  break;
192  }
193  default:
194  case(NRC_NOT_USED):
195  {
196  break;
197  }
198  }
199 
200  // no threshold met
201  return xnew-x;
202  }
203 
204  private:
205  data_type xmin, xmax;
207  };
208  }
209  }
210 }
211 #endif
void set_periodic_condition(const data_type &dmin, const data_type &dmax)
Definition: newton_raphson_constrained_method.hpp:58
Definition: math.hpp:20
end_condition_usage
Definition: newton_raphson_constrained_method.hpp:35
Definition: newton_raphson_method.hpp:27
void get_upper_condition(data_type &d, end_condition_usage &ec)
Definition: newton_raphson_constrained_method.hpp:107
data_type xmin
Definition: newton_raphson_constrained_method.hpp:205
void unset_upper_condition()
Definition: newton_raphson_constrained_method.hpp:92
void set_upper_condition(const data_type &d, end_condition_usage ec)
Definition: newton_raphson_constrained_method.hpp:93
Definition: newton_raphson_constrained_method.hpp:37
Definition: newton_raphson_constrained_method.hpp:40
Definition: newton_raphson_constrained_method.hpp:30
~newton_raphson_constrained_method()
Definition: newton_raphson_constrained_method.hpp:54
end_condition_usage xmin_cond
Definition: newton_raphson_constrained_method.hpp:206
Definition: newton_raphson_constrained_method.hpp:38
newton_raphson_constrained_method(const newton_raphson_constrained_method< data_type > &nrm)
Definition: newton_raphson_constrained_method.hpp:49
data__ data_type
Definition: newton_raphson_constrained_method.hpp:33
void get_lower_condition(data_type &d, end_condition_usage &ec)
Definition: newton_raphson_constrained_method.hpp:86
void unset_conditions()
Definition: newton_raphson_constrained_method.hpp:65
void set_lower_condition(const data_type &d, end_condition_usage ec)
Definition: newton_raphson_constrained_method.hpp:72
newton_raphson_constrained_method()
Definition: newton_raphson_constrained_method.hpp:44
void unset_lower_condition()
Definition: newton_raphson_constrained_method.hpp:71
Definition: newton_raphson_constrained_method.hpp:39
end_condition_usage xmax_cond
Definition: newton_raphson_constrained_method.hpp:206
virtual data_type calculate_delta_factor(const data_type &x, const data_type &dx) const
Definition: newton_raphson_constrained_method.hpp:114
data_type xmax
Definition: newton_raphson_constrained_method.hpp:205