ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df13fun.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: David Fournier
5  * Copyright (c) 2009-2012 ADMB Foundation
6  *
7  */
13 #include "df13fun.h"
14 
16 
18 
24 {
25  v[0] = 0;
26  v[1] = 0;
27  v[2] = 0;
28  v[3] = 0;
29 }
34 {
35  initialize();
36 }
41 {
42  v[0] = x.v[0];
43  v[1] = x.v[1];
44  v[2] = x.v[2];
45  v[3] = x.v[3];
46 }
51 {
54  shape=m2.shape;
55  if (shape)
56  {
57  (shape->ncopies)++;
58  }
59  v = m2.v;
60 }
61 
64 {
65  deallocate();
66 }
69 {
70  if(shape)
71  {
72  if (shape->ncopies)
73  {
74  (shape->ncopies)--;
75  }
76  else
77  {
79  delete [] v;
80  v = NULL;
81  delete shape;
82  allocate();
83  }
84  }
85 }
86 
92  {
93  int mmin=v.indexmin();
94  int mmax=v.indexmax();
95  dvector cv(mmin,mmax);
96  for (int i=mmin;i<=mmax;i++)
97  {
98  cv(i)=value(v(i));
99  }
100  return cv;
101  }
102 
108  {
109  int mmin=indexmin();
110  int mmax=indexmax();
111  for (int i=mmin;i<=mmax;i++)
112  {
113  (*this)(i)=0.0;
114  }
115  }
116 
122  {
123  allocate();
124  }
125 
131  {
132  allocate(min,max);
133  }
134 
142 {
143  index_min = min;
144  index_max = max;
145  v = new df1_three_variable[
146  static_cast<unsigned int>(max < min ? 0 : max - min + 1)];
147  if (v == 0)
148  {
149  cerr << "error allocating memory in df1_three_vector" << endl;
150  ad_exit(1);
151  }
152  if ((shape = new vector_shapex(min, max, v)) == NULL)
153  {
154  cerr << "Error trying to allocate memory for df1_three_vector\n";
155  ad_exit(1);
156  }
157  v-=min;
158 }
159 
165  {
166  index_min=0;
167  index_max=-1;
168  v=0;
169  shape=0;
170  }
171 
177  {
178  int rmin=v.indexmin();
179  int rmax=v.indexmax();
180  dmatrix cm(rmin,rmax);
181  for (int i=rmin;i<=rmax;i++)
182  {
183  int cmin=v(i).indexmin();
184  int cmax=v(i).indexmax();
185  cm(i).allocate(cmin,cmax);
186  for (int j=cmin;j<=cmax;j++)
187  {
188  cm(i,j)=value(v(i,j));
189  }
190  }
191  return cm;
192  }
193 
199  {
200  index_min=m2.index_min;
201  index_max=m2.index_max;
202  shape=m2.shape;
203  if (shape)
204  {
205  (shape->ncopies)++;
206  }
207  v = m2.v;
208  }
209 
212 {
213  deallocate();
214 }
217 {
218  if (shape)
219  {
220  if (shape->ncopies)
221  {
222  (shape->ncopies)--;
223  }
224  else
225  {
227  delete [] v;
228  v=0;
229  delete shape;
230  shape=0;
231  index_min = 0;
232  index_max = -1;
233  }
234  }
235 }
236 
242  {
243  int mmin=indexmin();
244  int mmax=indexmax();
245  for (int i=mmin;i<=mmax;i++)
246  {
247  (*this)(i).initialize();
248  }
249  }
250 
259 df1_three_matrix::df1_three_matrix(int rmin, int rmax, int cmin, int cmax)
260 {
261  index_min = rmin;
262  index_max = rmax;
263  v = new df1_three_vector[
264  static_cast<unsigned int>(rmax < rmin ? 0 : rmax - rmin + 1)];
265  if (v == 0)
266  {
267  cerr << "error allocating memory in df1_three_matrix" << endl;
268  ad_exit(1);
269  }
270  if ((shape = new mat_shapex(v)) == NULL)
271  {
272  cerr << "Error trying to allocate memory for df1_three_vector\n";
273  ad_exit(1);
274  }
275  v -= rmin;
276  for (int i = rmin; i <= rmax; ++i)
277  {
278  v[i].allocate(cmin, cmax);
279  }
280 }
281 
287 {
288  *get_u() -= *_v.get_u();
289  *get_u_x() -= *_v.get_u_x();
290  *get_u_y() -= *_v.get_u_y();
291  *get_u_z() -= *_v.get_u_z();
292 
293  return *this;
294 }
295 
301 {
303 
304  *z.get_u() = -(*v.get_u());
305  *z.get_u_x() = -(*v.get_u_x());
306  *z.get_u_y() = -(*v.get_u_y());
307  *z.get_u_z() = -(*v.get_u_z());
308 
309  return z;
310 }
311 
317 {
318  *get_u() += *_v.get_u();
319  *get_u_x() += *_v.get_u_x();
320  *get_u_y() += *_v.get_u_y();
321  *get_u_z() += *_v.get_u_z();
322 
323  return *this;
324 }
325 
331 {
332 /*
333 df1_three_variable x=*this * v;
334 *this=x;
335 return *this;
336 */
337  *get_u() *= _v;
338  *get_u_x() = *get_u_x() * _v;
339  *get_u_y() = *get_u_y() * _v;
340  *get_u_z() = *get_u_z() * _v;
341 
342  return *this;
343 }
344 
350  {
351  /*
352  df1_three_variable x=*this * v;
353  *this=x;
354  return *this;
355  */
356  double tmp=value(y);
357  *get_u_x() = *get_u_x()*tmp+ *get_u() * *y.get_u_x();
358  *get_u_y() = *get_u_y()*tmp+ *get_u() * *y.get_u_y();
359  *get_u_z() = *get_u_z()*tmp+ *get_u() * *y.get_u_z();
360  // need to do this last
361  *get_u()*=tmp;
362  return *this;
363  }
364 
370  {
371  /*
372  df1_three_variable x=*this * (1.0/y);
373  *this=x;
374  return *this;
375  */
376  double tmp=1.0/y;
377  *get_u()*=tmp;
378  *get_u_x() = *get_u_x()*tmp;
379  *get_u_y() = *get_u_y()*tmp;
380  *get_u_z() = *get_u_z()*tmp;
381  return *this;
382  }
383 
389  {
390  double tmp=1.0/value(y);
391  *get_u()*=tmp;
392  return *this;
393  }
394 
400  {
401  /*
402  df1_three_variable x=*this * inv(y);
403  *this=x;
404  return *this;
405  */
406  // properly optimized code
407  double tmp=1.0/value(y);
408  *get_u()*=tmp;
409  *get_u_x() = *get_u_x()*tmp- *get_u()*tmp* *y.get_u_x();
410  *get_u_y() = *get_u_y()*tmp- *get_u()*tmp* *y.get_u_y();
411  *get_u_z() = *get_u_z()*tmp- *get_u()*tmp* *y.get_u_z();
412  return *this;
413  }
414 
420 {
421  *get_u() += _v;
422 
423  return *this;
424 }
425 
431 {
432  *get_u() -= _v;
433 
434  return *this;
435 }
436 
442  double u, double zp)
443 {
444  //*z.get_u() = u;
445  *z.get_u_x() = zp* *x.get_u_x();
446  *z.get_u_y() = zp* *x.get_u_y();
447  *z.get_u_z() = zp* *x.get_u_z();
448 }
449 
455  const df1_three_variable& y, double u,
456  double f_u,double f_v)
457 {
458  *z.get_u() = u;
459 
460  *z.get_u_x() = f_u* *x.get_u_x()
461  + f_v* *y.get_u_x();
462 
463  *z.get_u_y() = f_u* *x.get_u_y()
464  + f_v* *y.get_u_y();
465 
466  *z.get_u_z() = f_u* *x.get_u_z()
467  + f_v* *y.get_u_z();
468 }
469 
475  {
477  double u=::sqrt(*x.get_u());
478  *z.get_u()=u;
479  //double xinv=1.0/(*x.get_u());
480  double zp=0.5/u;
481 
482  set_derivatives(z,x,u,zp);
483 
484  return z;
485  }
486 
492  {
494  double cx=value(x);
495  double d=1.0/(1+square(cx));
496  //double d2=square(d);
497  double u=::atan(cx);
498  *z.get_u()=u;
499  double zp=d;
500 
501  set_derivatives(z,x,u,zp);
502  return z;
503  }
504 
510  {
512  double u=value(x);
513  *z.get_u()=u*u;
514  double zp=2.0*u;
515 
516  set_derivatives(z,x,u,zp);
517  return z;
518  }
519 
525  {
527  double u=::tan(*x.get_u());
528  *z.get_u()=u;
529  double v=1.0/::cos(*x.get_u());
530  //double w=::sin(*x.get_u());
531  double v2=v*v;
532  double zp=v2;
533 
534  set_derivatives(z,x,u,zp);
535  return z;
536  }
537 
543  {
545  double u=::sin(*x.get_u());
546  *z.get_u()=u;
547  double zp=::cos(*x.get_u());
548 
549  set_derivatives(z,x,u,zp);
550  return z;
551  }
552 
558  {
560  if (value(x)>=0.0)
561  z=x;
562  else
563  z=-x;
564  return z;
565  }
566 
572  {
574  double u=::log(*x.get_u());
575  *z.get_u()=u;
576  double zp=1/(*x.get_u());
577 
578  set_derivatives(z,x,u,zp);
579  return z;
580  }
581 
587  {
589  double u=::exp(*x.get_u());
590  *z.get_u()=u;
591  double zp=u;
592 
593  set_derivatives(z,x,u,zp);
594  return z;
595  }
596 
602  {
604  double xinv=1.0/(*x.get_u());
605  *z.get_u()=xinv;
606  double zp=-xinv*xinv;
607  set_derivatives(z,x,xinv,zp);
608 
609  return z;
610  }
611 
617  {
618  *get_u() = *x.get_u();
619  *get_u_x() = *x.get_u_x();
620  *get_u_y() = *x.get_u_y();
621  *get_u_z() = *x.get_u_z();
622  return *this;
623  }
624 
630  {
631  *get_u() = x;
632  *get_u_x() =0.0;
633  *get_u_y() =0.0;
634  *get_u_z() =0.0;
635  return *this;
636  }
637 
643  const df1_three_variable& y)
644  {
646  double u= *x.get_u() * *y.get_u();
647  *z.get_u() = u;
648  double f_u=*y.get_u();
649  double f_v=*x.get_u();
650  set_derivatives(z,x,y,u, f_u, f_v);
651  return z;
652  }
653 
659  const df1_three_variable& y)
660  {
662  *z.get_u() = x * *y.get_u();
663  *z.get_u_x() = x * *y.get_u_x();
664  *z.get_u_y() = x * *y.get_u_y();
665  *z.get_u_z() = x * *y.get_u_z();
666  return z;
667  }
668 
674  double x)
675  {
677  *z.get_u() = x * *y.get_u();
678  *z.get_u_x() = x * *y.get_u_x();
679  *z.get_u_y() = x * *y.get_u_y();
680  *z.get_u_z() = x * *y.get_u_z();
681  return z;
682  }
683 
689  double y)
690  {
691  double u=1/y;
692  return x*u;
693  }
694 
700  const df1_three_variable& y)
701  {
702  df1_three_variable u=inv(y);
703  return x*u;
704  }
705 
711  const df1_three_variable& y)
712  {
713  df1_three_variable u=inv(y);
714  return x*u;
715  }
716 
722  {
723  return exp(y*log(x));
724  }
725 
731  {
733  *z.get_u() = x + *y.get_u();
734  *z.get_u_x() = *y.get_u_x();
735  *z.get_u_y() = *y.get_u_y();
736  *z.get_u_z() = *y.get_u_z();
737  return z;
738  }
739 
745  {
747  *z.get_u() = *x.get_u() + y;
748  *z.get_u_x() = *x.get_u_x();
749  *z.get_u_y() = *x.get_u_y();
750  *z.get_u_z() = *x.get_u_z();
751  return z;
752  }
753 
759  const df1_three_variable& y)
760  {
762  *z.get_u() = *x.get_u() + *y.get_u();
763  *z.get_u_x() = *x.get_u_x() + *y.get_u_x();
764  *z.get_u_y() = *x.get_u_y()+*y.get_u_y();
765  *z.get_u_z() = *x.get_u_z()+*y.get_u_z();
766  return z;
767  }
768 
774  const df1_three_variable& y)
775  {
777  *z.get_u() = *x.get_u() - *y.get_u();
778  *z.get_u_x() = *x.get_u_x() - *y.get_u_x();
779  *z.get_u_y() = *x.get_u_y() - *y.get_u_y();
780  *z.get_u_z() = *x.get_u_z() - *y.get_u_z();
781  return z;
782  }
783 
789  const df1_three_variable& y)
790  {
792  *z.get_u() = x - *y.get_u();
793  *z.get_u_x() = - *y.get_u_x();
794  *z.get_u_y() = - *y.get_u_y();
795  *z.get_u_z() = - *y.get_u_z();
796  return z;
797  }
798 
804  double y)
805  {
807  *z.get_u() = *x.get_u()-y;
808  *z.get_u_x() = *x.get_u_x();
809  *z.get_u_y() = *x.get_u_y();
810  *z.get_u_z() = *x.get_u_z();
811  return z;
812  }
813 
818 {
819  deallocate();
820 }
821 
827  {
828  num_ind_var=0;
829  }
830 
836 {
837  if (num_ind_var > 2)
838  {
839  cerr << "can only have 2 independent_variables in df1_three_variable"
840  " function" << endl;
841  ad_exit(1);
842  }
843  else
844  {
846  ind_var[num_ind_var++]=&v;
847  *get_u() = value(v);
848  switch(num_ind_var)
849  {
850  case 1:
851  *get_u_x() = 1.0;
852  *get_u_y() = 0.0;
853  *get_u_z() = 0.0;
854  break;
855  case 2:
856  *get_u_x() = 0.0;
857  *get_u_y() = 1.0;
858  *get_u_z() = 0.0;
859  break;
860  case 3:
861  *get_u_x() = 0.0;
862  *get_u_y() = 0.0;
863  *get_u_z() = 1.0;
864  break;
865  default:
866  cerr << "illegal num_ind_var value of " << num_ind_var
867  << " in df1_three_variable function" << endl;
868  ad_exit(1);
869  }
870  }
871 }
872 
878  {
879  *get_u() = v;
880  *get_u_x() = 0.0;
881  *get_u_y() = 0.0;
882  *get_u_z() = 0.0;
883  }
884 
890 {
891  // kludge to deal with constantness
893  int rmin=M.indexmin();
894  int cmin=M(rmin).indexmin();
895  int rmax=M.indexmax();
896  int cmax=M(rmin).indexmax();
897  if (rmin !=1 || cmin !=1)
898  {
899  cerr << "minimum row and column inidices must equal 1 in "
900  "df1b2matrix choleski_decomp(const df1_three_atrix& MM)"
901  << endl;
902  ad_exit(1);
903  }
904  if (rmax !=cmax)
905  {
906  cerr << "Error in df1b2matrix choleski_decomp(const df1_three_matrix& MM)"
907  " Matrix not square" << endl;
908  ad_exit(1);
909  }
910 
911  int n=rmax-rmin+1;
912  df1_three_matrix L(1,n,1,n);
913 #ifndef SAFE_INITIALIZE
914  L.initialize();
915 #endif
916 
917  int i,j,k;
918  df1_three_variable tmp;
919 
920  if (value(M(1,1))<=0)
921  {
922  cerr << "Error matrix not positive definite in choleski_decomp"
923  <<endl;
924  ad_exit(1);
925  }
926 
927  L(1,1)=sqrt(M(1,1));
928  for (i=2;i<=n;i++)
929  {
930  L(i,1)=M(i,1)/L(1,1);
931  }
932 
933  for (i=2;i<=n;i++)
934  {
935  for (j=2;j<=i-1;j++)
936  {
937  tmp=M(i,j);
938  for (k=1;k<=j-1;k++)
939  {
940  tmp-=L(i,k)*L(j,k);
941  }
942  L(i,j)=tmp/L(j,j);
943  }
944  tmp=M(i,i);
945  for (k=1;k<=i-1;k++)
946  {
947  tmp-=L(i,k)*L(i,k);
948  }
949 
950  if (value(tmp)<=0)
951  {
952  cerr << "Error matrix not positive definite in choleski_decomp"
953  <<endl;
954  ad_exit(1);
955  }
956 
957  L(i,i)=sqrt(tmp);
958  }
959 
960  return L;
961 }
962 
968 {
969  const prevariable * px=df1_three_variable::ind_var[0];
970  const prevariable * py=df1_three_variable::ind_var[1];
971  const prevariable * pz=df1_three_variable::ind_var[2];
972  double dfx= *v.get_u_x();
973  double dfy= *v.get_u_y();
974  double dfz= *v.get_u_z();
975  value(*this)=*v.get_u();
976 
978  &(value(*this)),&(value(*px)),dfx,&(value(*py)),dfy,&(value(*pz)),
979  dfz);
980 
981  return *this;
982 }
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
df1_three_variable & operator+=(const df1_three_variable &v)
Description not yet available.
Definition: df13fun.cpp:316
Description not yet available.
Definition: df13fun.h:56
mat_shapex * shape
Definition: df13fun.h:142
void * trueptr
Address of first element in the vector.
Definition: vector_shapex.h:82
void initialize(void)
Description not yet available.
Definition: df13fun.cpp:107
Description not yet available.
Definition: fvar.hpp:2030
void deallocate(void)
Description not yet available.
Definition: df13fun.cpp:826
void allocate(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat0.cpp:8
df1_three_vector * v
Definition: df13fun.h:143
double * get_u_z(void) const
Definition: df13fun.h:65
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 indexmax(void) const
Definition: df13fun.h:146
~df1_three_matrix()
Destructor.
Definition: df13fun.cpp:211
df1_three_variable & operator-=(const df1_three_variable &v)
Description not yet available.
Definition: df13fun.cpp:286
Description not yet available.
df1_three_matrix(int rmin, int rmax, int cmin, int cmax)
Allocate matrix of df1_three_variable with dimension [min to max] x [cmin to cmax].
Definition: df13fun.cpp:259
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
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
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
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
double * get_u(void) const
Definition: df13fun.h:62
double * get_u_y(void) const
Definition: df13fun.h:64
df1_three_variable * v
Definition: df13fun.h:107
double * get_u_x(void) const
Definition: df13fun.h:63
void default_evaluation3ind(void)
Description not yet available.
Definition: def_eval.cpp:174
df1_one_matrix choleski_decomp(const df1_one_matrix &MM)
Definition: df11fun.cpp:606
dmatrix operator*(const d3_array &t, const dvector &v)
Description not yet available.
Definition: d3arr12.cpp:17
void set_gradient_stack(void(*func)(void), double *dep_addr, double *ind_addr1=NULL, double mult1=0, double *ind_addr2=NULL, double mult2=0)
Description not yet available.
Definition: fvar.hpp:1045
void initialize(void)
Description not yet available.
Definition: df13fun.cpp:23
df1_three_vector(void)
Description not yet available.
Definition: df13fun.cpp:121
void * get_pointer(void)
Definition: fvar.hpp:2046
~df1_three_vector()
Destructor.
Definition: df13fun.cpp:63
df1_three_variable & my_diveq(const df1_three_variable &v)
Description not yet available.
Definition: df13fun.cpp:388
prnstream & endl(prnstream &)
void deallocate(void)
Deallocate df1_three_vector, then set as empty.
Definition: df13fun.cpp:68
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2c.cpp:11
int indexmax(void) const
Definition: df13fun.h:110
#define min(a, b)
Definition: cbivnorm.cpp:188
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
Description not yet available.
Definition: df13fun.h:102
Description not yet available.
Definition: fvar.hpp:2819
friend double & value(const prevariable &v1)
Definition: fvar.hpp:1495
unsigned int ncopies
Copy counter to enable shallow copies.
Definition: vector_shapex.h:79
Description not yet available.
Definition: df13fun.h:138
df1_three_variable & operator/=(const df1_three_variable &v)
Description not yet available.
Definition: df13fun.cpp:399
unsigned int ncopies
Definition: fvar.hpp:2034
df1_three_variable & operator=(const df1_three_variable &v)
Description not yet available.
Definition: df13fun.cpp:616
df1_three_variable(void)
Default constructor.
Definition: df13fun.cpp:33
dvariable & operator=(const double x)
Assigns a value to a dvariable object.
Definition: fvar.hpp:1595
Holds &quot;shape&quot; information for vector objects.
Definition: vector_shapex.h:46
init_df1_three_variable(const prevariable &)
Description not yet available.
Definition: df13fun.cpp:835
d3_array cos(const d3_array &arr3)
Returns d3_array results with computed cos from elements in arr3.
Definition: d3arr2a.cpp:58
~init_df1_three_variable()
Destructor.
Definition: df13fun.cpp:817
int indexmin(void) const
Definition: df13fun.h:109
double v[4]
Definition: df13fun.h:58
void initialize(void)
Description not yet available.
Definition: df13fun.cpp:241
int indexmin(void) const
Definition: df13fun.h:145
static int num_ind_var
Definition: df13fun.h:61
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
void allocate(void)
Description not yet available.
Definition: df13fun.cpp:164
void deallocate(void)
Deallocate df1_three_vector, then set as empty.
Definition: df13fun.cpp:216
static _THREAD grad_stack * GRAD_STACK1
#define max(a, b)
Definition: cbivnorm.cpp:189
static prevariable * ind_var[]
Definition: df13fun.h:60
df1_three_variable & operator*=(const df1_three_variable &v)
Description not yet available.
Definition: df13fun.cpp:349
vector_shapex * shape
Definition: df13fun.h:106
double square(const double value)
Return square of value; constant object.
Definition: d3arr4.cpp:16
Fundamental data type for reverse mode automatic differentiation.
Definition: fvar.hpp:1518
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
d3_array pow(const d3_array &m, int e)
Description not yet available.
Definition: d3arr6.cpp:17