ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dfqromb.cpp
Go to the documentation of this file.
1 
17 #include <admodel.h>
18 //#define EPS 1.0e-4
19 #define JMAX 50
20 //#define JMAXP JMAX+1
21 //#define K 5
22 
23 class model_parameters;
24 
25 dvariable trapzd(_func func, double a, double b, int n);
26 dvariable trapzd(_func func, double a, const dvariable& b, int n);
27 dvariable trapzd(_func func, const dvariable& a, const dvariable& b, int n);
28 dvariable trapzd(_func func, const dvariable& a, double b, int n);
29 
30 void polint(const dvector& xa, const dvar_vector& ya,int n,double x,
31  const dvariable& y, const dvariable& dy);
32 
40 dvariable function_minimizer::adromb(_func func, double a, double b, int ns)
41 {
42  const double base = 4;
43  int MAXN = min(JMAX, ns);
44  dvar_vector s(1,MAXN+1);
45 
46  for(int j=1; j<=MAXN+1; j++)
47  {
48  s[j] = trapzd(func,a,b,j);
49  }
50 
51  for(int iter=1; iter<=MAXN+1; iter++)
52  {
53  for(int j=1; j<=MAXN+1-iter; j++)
54  {
55  s[j] = (pow(base,iter)*s[j+1]-s[j])/(pow(base,iter)-1);
56  }
57  }
58 
59  return s[1];
60 }
61 
69 dvariable function_minimizer::adromb(_func func, const dvariable& a, double b, int ns)
70 {
71  const double base = 4;
72  int MAXN = min(JMAX, ns);
73  dvar_vector s(1,MAXN+1);
74 
75  for(int j=1; j<=MAXN+1; j++)
76  {
77  s[j] = trapzd(func,a,b,j);
78  }
79 
80  for(int iter=1; iter<=MAXN+1; iter++)
81  {
82  for(int j=1; j<=MAXN+1-iter; j++)
83  {
84  s[j] = (pow(base,iter)*s[j+1]-s[j])/(pow(base,iter)-1);
85  }
86  }
87 
88  return s[1];
89 }
90 
98 dvariable function_minimizer::adromb(_func func, double a, const dvariable& b, int ns)
99 {
100  const double base = 4;
101  int MAXN = min(JMAX, ns);
102  dvar_vector s(1,MAXN+1);
103 
104  for(int j=1; j<=MAXN+1; j++)
105  {
106  s[j] = trapzd(func,a,b,j);
107  }
108 
109  for(int iter=1; iter<=MAXN+1; iter++)
110  {
111  for(int j=1; j<=MAXN+1-iter; j++)
112  {
113  s[j] = (pow(base,iter)*s[j+1]-s[j])/(pow(base,iter)-1);
114  }
115  }
116 
117  return s[1];
118 }
119 
128 {
129  const double base = 4;
130  int MAXN = min(JMAX, ns);
131  dvar_vector s(1,MAXN+1);
132 
133  for(int j=1; j<=MAXN+1; j++)
134  {
135  s[j] = trapzd(func,a,b,j);
136  }
137 
138  for(int iter=1; iter<=MAXN+1; iter++)
139  {
140  for(int j=1; j<=MAXN+1-iter; j++)
141  {
142  s[j] = (pow(base,iter)*s[j+1]-s[j])/(pow(base,iter)-1);
143  }
144  }
145 
146  return s[1];
147 }
148 
156 dvariable function_minimizer::trapzd(_func func, double a, double b, int n)
157 {
158  double x,num_interval,hn;
159  dvariable sum;
160  static dvariable s;
161  static int interval;
162  int j;
163  if (n == 1) {
164  interval=1;
165  return (s=0.5*(b-a)*(func(a)+func(b)));
166  } else {
167  num_interval=interval;
168  hn=(b-a)/num_interval;
169  x=a+0.5*hn;
170  for (sum=0.0,j=1;j<=interval;j++,x+=hn) sum += func(x);
171  interval *= 2;
172  s=0.5*(s+(b-a)*sum/num_interval);
173  return s;
174  }
175 }
176 
184 dvariable function_minimizer::trapzd(_func func, const dvariable& a, double b, int n)
185 {
186  double num_interval;
187  dvariable x,sum,hn;
188  static dvariable s;
189  static int interval;
190  int j;
191  if (n == 1) {
192  interval=1;
193  return (s=0.5*(b-a)*(func(a)+func(b)));
194  } else {
195  num_interval=interval;
196  hn=(b-a)/num_interval;
197  x=a+0.5*hn;
198  for (sum=0.0,j=1;j<=interval;j++,x+=hn) sum += func(x);
199  interval *= 2;
200  s=0.5*(s+(b-a)*sum/num_interval);
201  return s;
202  }
203 }
204 
211 dvariable function_minimizer::trapzd(_func func, double a, const dvariable& b, int n)
212 {
213  double num_interval;
214  dvariable sum,hn,x;
215  static dvariable s;
216 
217  static int interval;
218  int j;
219 
220  if (n == 1) {
221  interval=1;
222  return (s=0.5*(b-a)*(func(a)+func(b)));
223  } else {
224  num_interval=interval;
225  hn=(b-a)/num_interval;
226  x=a+0.5*hn;
227  for (sum=0.0,j=1;j<=interval;j++,x+=hn) sum += func(x);
228  interval *= 2;
229  s=0.5*(s+(b-a)*sum/num_interval);
230  return s;
231  }
232 }
233 
241 {
242  double num_interval;
243  dvariable sum,hn,x;
244  static dvariable s;
245  static int interval;
246  int j;
247 
248  if (n == 1) {
249  interval=1;
250  return (s=0.5*(b-a)*(func(a)+func(b)));
251  } else {
252  num_interval=interval;
253  hn=(b-a)/num_interval;
254  x=a+0.5*hn;
255  for (sum=0.0,j=1;j<=interval;j++,x+=hn) sum += func(x);
256  interval *= 2;
257  s=0.5*(s+(b-a)*sum/num_interval);
258  return s;
259  }
260 }
261 //#undef EPS
262 #undef JMAX
263 //#undef JMAXP
264 //#undef K
265 
266  //Not used elsewhere
267 void polint(const dvector& xa, const dvar_vector& ya,int n,double x,
268  const dvariable& _y, const dvariable& _dy)
269 {
270  dvariable& y=(dvariable&) _y;
271  dvariable& dy=(dvariable&) _dy;
272  int i,m,ns=1;
273  double dif,dift,ho,hp;
274  dvariable den,w;
275 
276  dif=fabs(x-xa[1]);
277  dvar_vector c(1,n);
278  dvar_vector d(1,n);
279  for (i=1;i<=n;i++) {
280  if ( (dift=fabs(x-xa[i])) < dif) {
281  ns=i;
282  dif=dift;
283  }
284  c[i]=ya[i];
285  d[i]=ya[i];
286  }
287  y=ya[ns--];
288  for (m=1;m<n;m++) {
289  for (i=1;i<=n-m;i++) {
290  ho=xa[i]-x;
291  hp=xa[i+m]-x;
292  w=c[i+1]-d[i];
293  if ( (den=ho-hp) == 0.0)
294  {
295  cerr << "Error in routine POLINT" << endl;
296  ad_exit(1);
297  }
298  den=w/den;
299  d[i]=hp*den;
300  c[i]=ho*den;
301  }
302  y += (dy=(2*ns < (n-m) ? c[ns+1] : d[ns--]));
303  }
304 }
#define x
Vector of double precision numbers.
Definition: dvector.h:50
void polint(const dvector &xa, const dvar_vector &ya, int n, double x, const dvariable &y, const dvariable &dy)
Definition: dfqromb.cpp:267
std::function< dvariable(const dvariable &)> _func
Definition: admodel.h:1831
double sum(const d3_array &darray)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr.cpp:21
exitptr ad_exit
Definition: gradstrc.cpp:53
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
ADMB variable vector.
Definition: fvar.hpp:2172
dvariable trapzd(_func func, double a, double b, int n)
prnstream & endl(prnstream &)
#define min(a, b)
Definition: cbivnorm.cpp:188
Description not yet available.
#define w
dvariable trapzd(void *, double a, double b, int n)
#define JMAX
Definition: dfqromb.cpp:19
Fundamental data type for reverse mode automatic differentiation.
Definition: fvar.hpp:1518
d3_array pow(const d3_array &m, int e)
Description not yet available.
Definition: d3arr6.cpp:17
dvariable adromb(_func func, double a, double b, int ns=9)
Romberg integration.
Definition: dfqromb.cpp:40