ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df11fun.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: David Fournier
5  * Copyright (c) 2008-2012 Regents of the University of California
6  */
7 
8 char df12fun_notice[50]="copyright (c) 2006 otter research ltd";
9 
10 #include "df11fun.h"
11 
14 
19 {
20  v[0] = 0;
21  v[1] = 0;
22 }
27 {
28  v[0] = x.v[0];
29  v[1] = x.v[1];
30 }
35 {
36  index_min = m2.index_min;
37  index_max = m2.index_max;
38  shape = m2.shape;
39  if (shape)
40  {
41  (shape->ncopies)++;
42  }
43  v = m2.v;
44 }
47 {
48  deallocate();
49 }
52 {
53  if(shape)
54  {
55  if (shape->ncopies)
56  {
57  (shape->ncopies)--;
58  }
59  else
60  {
62  delete [] v;
63  delete shape;
64  allocate();
65  }
66  }
67 }
68 
70  {
71  int mmin=v.indexmin();
72  int mmax=v.indexmax();
73  dvector cv(mmin,mmax);
74  for (int i=mmin;i<=mmax;i++)
75  {
76  cv(i)=value(v(i));
77  }
78  return cv;
79  }
80 
82  {
83  int mmin=indexmin();
84  int mmax=indexmax();
85  for (int i=mmin;i<=mmax;i++)
86  {
87  (*this)(i)=0.0;
88  }
89  }
90 
95 {
96  allocate();
97 }
98 
100  {
101  allocate(min,max);
102  }
103 
112 {
113  index_min = min;
114  index_max = max;
115  v = new df1_one_variable[
116  static_cast<unsigned int>(max < min ? 0 : max - min + 1)];
117  if (v == 0)
118  {
119  cerr << "error allocating memory in df1_one_vector" << endl;
120  ad_exit(1);
121  }
122  if ((shape = new vector_shapex(min, max, v)) == NULL)
123  {
124  cerr << "Error trying to allocate memory for df1_one_vector\n";
125  ad_exit(1);
126  }
127  v -= min;
128 }
131 {
132  index_min=0;
133  index_max=-1;
134  v=0;
135  shape=0;
136 }
138  {
139  int rmin=v.indexmin();
140  int rmax=v.indexmax();
141  dmatrix cm(rmin,rmax);
142  for (int i=rmin;i<=rmax;i++)
143  {
144  int cmin=v(i).indexmin();
145  int cmax=v(i).indexmax();
146  cm(i).allocate(cmin,cmax);
147  for (int j=cmin;j<=cmax;j++)
148  {
149  cm(i,j)=value(v(i,j));
150  }
151  }
152  return cm;
153  }
154 
156  {
157  index_min=m2.index_min;
158  index_max=m2.index_max;
159  shape=m2.shape;
160  if (shape)
161  {
162  (shape->ncopies)++;
163  }
164  v = m2.v;
165  }
166 
168  {
169  deallocate();
170  }
173 {
174  if (shape)
175  {
176  if (shape->ncopies)
177  {
178  (shape->ncopies)--;
179  }
180  else
181  {
183  delete [] v;
184  v = nullptr;
185  delete shape;
186  shape = nullptr;
187  index_min = 0;
188  index_max = -1;
189  }
190  }
191 }
192 
193 
195  {
196  int mmin=indexmin();
197  int mmax=indexmax();
198  for (int i=mmin;i<=mmax;i++)
199  {
200  (*this)(i).initialize();
201  }
202  }
203 
204 
214 df1_one_matrix::df1_one_matrix(int rmin, int rmax, int cmin, int cmax)
215 {
216  index_min = rmin;
217  index_max = rmax;
218  v = new df1_one_vector[
219  static_cast<unsigned int>(rmax < rmin ? 0 : rmax - rmin + 1)];
220  if (v == 0)
221  {
222  cerr << "error allocating memory in df1_one_matrix" << endl;
223  ad_exit(1);
224  }
225  if ((shape = new mat_shapex(v)) == NULL)
226  {
227  cerr << "Error trying to allocate memory for df1_one_vector\n";
228  ad_exit(1);
229  }
230  v -= rmin;
231  for (int i = rmin; i <= rmax; ++i)
232  {
233  v[i].allocate(cmin, cmax);
234  }
235 }
236 
238 {
239  *get_u() -= *_v.get_u();
240  *get_u_x() -= *_v.get_u_x();
241 
242  return *this;
243 }
245 {
247 
248  *z.get_u() = -(*v.get_u());
249  *z.get_u_x() = -(*v.get_u_x());
250 
251  return z;
252 }
254 {
255  *get_u() += *_v.get_u();
256  *get_u_x() += *_v.get_u_x();
257 
258  return *this;
259 }
261 {
262  df1_one_variable x = *this * _v;
263  *this = x;
264 
265  return *this;
266 }
268 {
269  *get_u() += _v;
270 
271  return *this;
272 }
274 {
275  *get_u() -= _v;
276 
277  return *this;
278 }
279 
281  double zp)
282 {
283  //*z.get_u() = u;
284  *z.get_u_x() = zp* *x.get_u_x();
285 }
286 
288  const df1_one_variable& y, double u,
289  double f_u,double f_v)
290 {
291  *z.get_u() = u;
292 
293  *z.get_u_x() = f_u* *x.get_u_x()
294  + f_v* *y.get_u_x();
295 }
296 
298  {
300  double u=::sqrt(*x.get_u());
301  *z.get_u()=u;
302  //double xinv=1.0/(*x.get_u());
303  double zp=0.5/u;
304 
305  set_derivatives(z,x,u,zp);
306 
307  return z;
308  }
309 
310 
311 
313  {
315  double cx=value(x);
316  double d=1.0/(1+square(cx));
317  //double d2=square(d);
318  double u=::atan(cx);
319  *z.get_u()=u;
320  double zp=d;
321 
322  set_derivatives(z,x,u,zp);
323  return z;
324  }
325 
327  {
329  double u=value(x);
330  *z.get_u()=u*u;
331  double zp=2.0*u;
332 
333  set_derivatives(z,x,u,zp);
334  return z;
335  }
336 
338  {
340  double u=::tan(*x.get_u());
341  *z.get_u()=u;
342  double v=1.0/::cos(*x.get_u());
343  //double w=::sin(*x.get_u());
344  double v2=v*v;
345  double zp=v2;
346 
347  set_derivatives(z,x,u,zp);
348  return z;
349  }
350 
352  {
354  double u=::sin(*x.get_u());
355  *z.get_u()=u;
356  double zp=::cos(*x.get_u());
357 
358  set_derivatives(z,x,u,zp);
359  return z;
360  }
361 
363  {
365  double u=::log(*x.get_u());
366  *z.get_u()=u;
367  double zp=1/(*x.get_u());
368 
369  set_derivatives(z,x,u,zp);
370  return z;
371  }
372 
374  {
376  double u=::exp(*x.get_u());
377  *z.get_u()=u;
378  double zp=u;
379 
380  set_derivatives(z,x,u,zp);
381  return z;
382  }
383 
385  {
387  double xinv=1.0/(*x.get_u());
388  *z.get_u()=xinv;
389  double zp=-xinv*xinv;
390  set_derivatives(z,x,xinv,zp);
391 
392  return z;
393  }
394 
400  {
401  int mmin=v.indexmin();
402  int mmax=v.indexmax();
403  dvector cv(mmin,mmax);
404  for (int i=mmin;i<=mmax;i++)
405  {
406  cv(i)=*(v(i).get_udot());
407  }
408  return cv;
409  }
410 
416  {
417  int rmin=v.indexmin();
418  int rmax=v.indexmax();
419  dmatrix cm(rmin,rmax);
420  for (int i=rmin;i<=rmax;i++)
421  {
422  int cmin=v(i).indexmin();
423  int cmax=v(i).indexmax();
424  cm(i).allocate(cmin,cmax);
425  cm(i)=first_derivatives(v(i));
426  }
427  return cm;
428  }
429 
431  {
432  *get_u() = *x.get_u();
433  *get_u_x() = *x.get_u_x();
434  return *this;
435  }
436 
438  {
439  *get_u() = x;
440  *get_u_x() =0.0;
441  return *this;
442  }
443 
444 
446  const df1_one_variable& x,
447  const df1_one_variable& y)
448 {
450  double u= *x.get_u() * *y.get_u();
451  *z.get_u() = u;
452  double f_u=*y.get_u();
453  double f_v=*x.get_u();
454  set_derivatives(z,x,y,u, f_u, f_v);
455  return z;
456 }
457 
459  const df1_one_variable& y)
460  {
462  *z.get_u() = x * *y.get_u();
463  *z.get_u_x() = x * *y.get_u_x();
464  return z;
465  }
466 
467 
469  double x)
470  {
472  *z.get_u() = x * *y.get_u();
473  *z.get_u_x() = x * *y.get_u_x();
474  return z;
475  }
476 
477 
478 
480  double y)
481  {
482  double u=1/y;
483  return x*u;
484  }
485 
487  const df1_one_variable& y)
488  {
489  df1_one_variable u=inv(y);
490  return x*u;
491  }
492 
494  const df1_one_variable& y)
495  {
496  df1_one_variable u=inv(y);
497  return x*u;
498  }
499 
500 
502  {
504  *z.get_u() = x + *y.get_u();
505  *z.get_u_x() = *y.get_u_x();
506  return z;
507  }
508 
510  {
512  *z.get_u() = *x.get_u() + y;
513  *z.get_u_x() = *x.get_u_x();
514  return z;
515  }
516 
517 
518 
520  const df1_one_variable& y)
521  {
523  *z.get_u() = *x.get_u() + *y.get_u();
524  *z.get_u_x() = *x.get_u_x() + *y.get_u_x();
525  return z;
526  }
527 
529  const df1_one_variable& y)
530  {
532  *z.get_u() = *x.get_u() - *y.get_u();
533  *z.get_u_x() = *x.get_u_x() - *y.get_u_x();
534  return z;
535  }
536 
538  const df1_one_variable& y)
539  {
541  *z.get_u() = x - *y.get_u();
542  *z.get_u_x() = - *y.get_u_x();
543  return z;
544  }
545 
547  double y)
548  {
550  *z.get_u() = *x.get_u()-y;
551  *z.get_u_x() = *x.get_u_x();
552  return z;
553  }
554 
559 {
560  deallocate();
561 }
566 {
567  num_ind_var=0;
568 }
569 
576 {
577  if (num_ind_var != 0)
578  {
579  cerr << "can only have 1 independent_variables in df1_one_variable"
580  " function" << endl;
581  ad_exit(1);
582  }
583 
585  num_ind_var = 1;
586  ind_var[0] = &var;
587  *get_u() = value(var);
588  switch(num_ind_var)
589  {
590  case 1:
591  *get_u_x() = 1.0;
592  break;
593  default:
594  cerr << "illegal num_ind_var value of " << num_ind_var
595  << " in df1_one_variable function" << endl;
596  ad_exit(1);
597  }
598 }
599 
601  {
602  *get_u() = v;
603  *get_u_x() = 0.0;
604  }
605 
607 {
608  // kludge to deal with constantness
609  df1_one_matrix & M= (df1_one_matrix &) MM;
610  int rmin=M.indexmin();
611  int cmin=M(rmin).indexmin();
612  int rmax=M.indexmax();
613  int cmax=M(rmin).indexmax();
614  if (rmin !=1 || cmin !=1)
615  {
616  cerr << "minimum row and column inidices must equal 1 in "
617  "df1b2matrix choleski_decomp(const df1_one_atrix& MM)"
618  << endl;
619  ad_exit(1);
620  }
621  if (rmax !=cmax)
622  {
623  cerr << "Error in df1b2matrix choleski_decomp(const df1_one_matrix& MM)"
624  " Matrix not square" << endl;
625  ad_exit(1);
626  }
627 
628  int n=rmax-rmin+1;
629  df1_one_matrix L(1,n,1,n);
630 #ifndef SAFE_INITIALIZE
631  L.initialize();
632 #endif
633 
634  int i,j,k;
635  df1_one_variable tmp;
636 
637  if (value(M(1,1))<=0)
638  {
639  cerr << "Error matrix not positive definite in choleski_decomp"
640  <<endl;
641  ad_exit(1);
642  }
643 
644  L(1,1)=sqrt(M(1,1));
645  for (i=2;i<=n;i++)
646  {
647  L(i,1)=M(i,1)/L(1,1);
648  }
649 
650  for (i=2;i<=n;i++)
651  {
652  for (j=2;j<=i-1;j++)
653  {
654  tmp=M(i,j);
655  for (k=1;k<=j-1;k++)
656  {
657  tmp-=L(i,k)*L(j,k);
658  }
659  L(i,j)=tmp/L(j,j);
660  }
661  tmp=M(i,i);
662  for (k=1;k<=i-1;k++)
663  {
664  tmp-=L(i,k)*L(i,k);
665  }
666 
667  if (value(tmp)<=0)
668  {
669  cerr << "Error matrix not positive definite in choleski_decomp"
670  <<endl;
671  ad_exit(1);
672  }
673 
674  L(i,i)=sqrt(tmp);
675  }
676 
677  return L;
678 }
Base class for dvariable.
Definition: fvar.hpp:1315
d3_array tan(const d3_array &arr3)
Returns d3_array results with computed tan from elements in arr3.
Definition: d3arr2a.cpp:73
void * trueptr
Address of first element in the vector.
Definition: vector_shapex.h:82
Description not yet available.
Definition: fvar.hpp:2030
~init_df1_one_variable()
Destructor.
Definition: df11fun.cpp:558
void allocate(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat0.cpp:8
d3_array operator-(const d3_array &a, const d3_array &b)
Returns d3_array results with computed elements addition of a(i, j, k) + b(i, j, k).
Definition: d3arr2a.cpp:152
#define x
#define ADUNCONST(type, obj)
Creates a shallow copy of obj that is not CONST.
Definition: fvar.hpp:140
Vector of double precision numbers.
Definition: dvector.h:50
int indexmin(void) const
Definition: df11fun.h:120
~df1_one_vector()
Destructor.
Definition: df11fun.cpp:46
static prevariable * ind_var[]
Definition: df11fun.h:51
d3_array operator+(const d3_array &a, const d3_array &b)
Returns d3_array results with computed elements addition of a(i, j, k) + b(i, j, k).
Definition: d3arr2a.cpp:132
int index_max
Definition: df11fun.h:84
df1_one_variable atan(const df1_one_variable &x)
Definition: df11fun.cpp:312
d3_array sin(const d3_array &arr3)
Returns d3_array results with computed sin from elements in arr3.
Definition: d3arr2a.cpp:43
int indexmin(void) const
Definition: df11fun.h:88
exitptr ad_exit
Definition: gradstrc.cpp:53
void set_derivatives(df1_one_variable &z, const df1_one_variable &x, double u, double zp)
Definition: df11fun.cpp:280
df1_one_matrix(int rmin, int rmax, int cmin, int cmax)
Construct matrix of df1_one_variable with dimensions [rmin to rmax] x [cmin to cmax].
Definition: df11fun.cpp:214
df1_one_variable & operator*=(const df1_one_variable &v)
Definition: df11fun.cpp:260
void initialize(void)
Definition: df11fun.cpp:194
void initialize(void)
Definition: df11fun.cpp:81
int index_min
Definition: df11fun.h:83
double v[2]
Definition: df11fun.h:49
int indexmax(void) const
Definition: df11fun.h:121
df1_one_matrix choleski_decomp(const df1_one_matrix &MM)
Definition: df11fun.cpp:606
df1_one_variable & operator=(const df1_one_variable &v)
Definition: df11fun.cpp:430
dmatrix operator*(const d3_array &t, const dvector &v)
Description not yet available.
Definition: d3arr12.cpp:17
vector_shapex * shape
Definition: df11fun.h:85
df1_one_variable(void)
Default constructor.
Definition: df11fun.cpp:18
int j
Definition: fvar.hpp:3636
void * get_pointer(void)
Definition: fvar.hpp:2046
df1_one_variable & operator+=(const df1_one_variable &v)
Definition: df11fun.cpp:253
void deallocate(void)
Deallocate df1_one_matrix, then set to empty.
Definition: df11fun.cpp:172
prnstream & endl(prnstream &)
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2c.cpp:11
dvector * d
Definition: fvar.hpp:3649
double var(const dvector &vec)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: cranfill.cpp:23
#define min(a, b)
Definition: cbivnorm.cpp:188
void deallocate(void)
Resets num_ind_var count to zero.
Definition: df11fun.cpp:565
df1_one_variable * v
Definition: df11fun.h:86
d3_array exp(const d3_array &arr3)
Returns d3_array results with computed exp from elements in arr3.
Definition: d3arr2a.cpp:28
#define M
Definition: rngen.cpp:57
dvector first_derivatives(const df1_one_vector &v)
Description not yet available.
Definition: df11fun.cpp:399
Description not yet available.
Definition: fvar.hpp:2819
unsigned int ncopies
Copy counter to enable shallow copies.
Definition: vector_shapex.h:79
double * get_u_x(void) const
Definition: df11fun.h:54
unsigned int ncopies
Definition: fvar.hpp:2034
double * get_u(void) const
Definition: df11fun.h:53
mat_shapex * shape
Definition: df11fun.h:117
static int num_ind_var
Definition: df11fun.h:52
Holds &quot;shape&quot; information for vector objects.
Definition: vector_shapex.h:46
d3_array cos(const d3_array &arr3)
Returns d3_array results with computed cos from elements in arr3.
Definition: d3arr2a.cpp:58
init_df1_one_variable(const prevariable &)
Construct init_df1_one_variable with _var.
Definition: df11fun.cpp:575
df1_one_variable & operator-=(const df1_one_variable &v)
Definition: df11fun.cpp:237
void deallocate(void)
Deallocate df1_one_vector, then set to empty.
Definition: df11fun.cpp:51
d3_array operator/(const d3_array &m, const double d)
Author: David Fournier.
Definition: d3arr2b.cpp:14
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
#define max(a, b)
Definition: cbivnorm.cpp:189
df1_one_vector(void)
Default constructor.
Definition: df11fun.cpp:94
double square(const double value)
Return square of value; constant object.
Definition: d3arr4.cpp:16
df1_one_variable inv(const df1_one_variable &x)
Definition: df11fun.cpp:384
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2a.cpp:13
void allocate(void)
Reinitialize df1_one_vector to empty.
Definition: df11fun.cpp:130
df1_one_vector * v
Definition: df11fun.h:118
int indexmax(void) const
Definition: df11fun.h:89
char df12fun_notice[50]
Definition: df11fun.cpp:8