ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df12fun.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  */
11 //class df1b2matrix;
12 
13 #include "df12fun.h"
14 
16 
18 
23 {
24  v[0] = 0;
25  v[1] = 0;
26  v[2] = 0;
27 }
28 
33 {
34  v[0] = x.v[0];
35  v[1] = x.v[1];
36  v[2] = x.v[2];
37 }
42 {
43  index_min = m2.index_min;
44  index_max = m2.index_max;
45  shape =m2.shape;
46  if (shape)
47  {
48  (shape->ncopies)++;
49  }
50  v = m2.v;
51 }
56 {
57  deallocate();
58 }
59 
65  {
66  if(shape)
67  {
68  if (shape->ncopies)
69  {
70  (shape->ncopies)--;
71  }
72  else
73  {
75  delete [] v;
76  delete shape;
77  //Reinitialize to empty
78  allocate();
79  }
80  }
81  }
82 
88  {
89  int mmin=v.indexmin();
90  int mmax=v.indexmax();
91  dvector cv(mmin,mmax);
92  for (int i=mmin;i<=mmax;i++)
93  {
94  cv(i)=value(v(i));
95  }
96  return cv;
97  }
98 
104  {
105  int mmin=indexmin();
106  int mmax=indexmax();
107  for (int i=mmin;i<=mmax;i++)
108  {
109  (*this)(i)=0.0;
110  }
111  }
112 
117 {
118  allocate();
119 }
120 
126  {
127  allocate(min,max);
128  }
129 
137 {
138  index_min = min;
139  index_max = max;
140  v = new df1_two_variable[
141  static_cast<unsigned int>(max < min ? 0 : max - min + 1)];
142  if (v == 0)
143  {
144  cerr << "error allocating memory in df1_two_vector\n";
145  ad_exit(1);
146  }
147  if ((shape = new vector_shapex(min, max, v)) == NULL)
148  {
149  cerr << "Error trying to allocate memory for df1_two_vector\n";
150  ad_exit(1);
151  }
152  v -= min;
153 }
158 {
159  index_min = 0;
160  index_max = -1;
161  v = 0;
162  shape = 0;
163 }
164 
170  {
171  int rmin=v.indexmin();
172  int rmax=v.indexmax();
173  dmatrix cm(rmin,rmax);
174  for (int i=rmin;i<=rmax;i++)
175  {
176  int cmin=v(i).indexmin();
177  int cmax=v(i).indexmax();
178  cm(i).allocate(cmin,cmax);
179  for (int j=cmin;j<=cmax;j++)
180  {
181  cm(i,j)=value(v(i,j));
182  }
183  }
184  return cm;
185  }
186 
192  {
193  index_min=m2.index_min;
194  index_max=m2.index_max;
195  shape=m2.shape;
196  if (shape)
197  {
198  (shape->ncopies)++;
199  }
200  v = m2.v;
201  }
202 
208  {
209  deallocate();
210  }
211 
217  {
218  if (shape)
219  {
220  if (shape->ncopies)
221  {
222  (shape->ncopies)--;
223  }
224  else
225  {
226  v = (df1_two_vector*) (shape->get_pointer());
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_two_matrix::df1_two_matrix(int rmin, int rmax, int cmin, int cmax)
260 {
261  index_min = rmin;
262  index_max = rmax;
263  v = new df1_two_vector[
264  static_cast<unsigned int>(rmax < rmin ? 0 : rmax - rmin + 1)];
265  if (v == 0)
266  {
267  cerr << "error allocating memory in df1_two_matrix\n";
268  ad_exit(1);
269  }
270  if ((shape = new mat_shapex(v)) == NULL)
271  {
272  cerr << "Error trying to allocate memory for df1_two_vector\n";
273  }
274  v -= rmin;
275  for (int i = rmin; i <= rmax; ++i)
276  {
277  v[i].allocate(cmin, cmax);
278  }
279 }
280 
286 {
287  *get_u() -= *_v.get_u();
288  *get_u_x() -= *_v.get_u_x();
289  *get_u_y() -= *_v.get_u_y();
290 
291  return *this;
292 }
293 
299  {
300  /*
301  df1_three_variable x=*this * inv(y);
302  *this=x;
303  return *this;
304  */
305  // properly optimized code
306  double tmp=1.0/value(y);
307  *get_u()*=tmp;
308  *get_u_x() = *get_u_x()*tmp- *get_u()*tmp* *y.get_u_x();
309  *get_u_y() = *get_u_y()*tmp- *get_u()*tmp* *y.get_u_y();
310  return *this;
311  }
312 
318 {
320 
321  *z.get_u() = -(*v.get_u());
322  *z.get_u_x() = -(*v.get_u_x());
323  *z.get_u_y() = -(*v.get_u_y());
324 
325  return z;
326 }
327 
333 {
334  *get_u() += *_v.get_u();
335  *get_u_x() += *_v.get_u_x();
336  *get_u_y() += *_v.get_u_y();
337  return *this;
338 }
339 
344 {
345 /*
346  df1_three_variable x=*this * v;
347  *this=x;
348  return *this;
349 */
350  *get_u() *= _v;
351  *get_u_x() *= _v;
352  *get_u_y() *= _v;
353 
354  return *this;
355 }
361  {
362  df1_two_variable x=*this * v;
363  *this=x;
364  return *this;
365  }
366 
372 {
373  *get_u() += _v;
374 
375  return *this;
376 }
377 
383 {
384  *get_u() -= _v;
385 
386  return *this;
387 }
388 
394  double zp)
395 {
396  //*z.get_u() = u;
397  *z.get_u_x() = zp* *x.get_u_x();
398  *z.get_u_y() = zp* *x.get_u_y();
399 }
400 
406  const df1_two_variable& y, double u,
407  double f_u,double f_v)
408 {
409  *z.get_u() = u;
410 
411  *z.get_u_x() = f_u* *x.get_u_x()
412  + f_v* *y.get_u_x();
413 
414  *z.get_u_y() = f_u* *x.get_u_y()
415  + f_v* *y.get_u_y();
416 }
417 
423  {
425  double u=::sqrt(*x.get_u());
426  *z.get_u()=u;
427  //double xinv=1.0/(*x.get_u());
428  double zp=0.5/u;
429 
430 
431  set_derivatives(z,x,u,zp);
432 
433  return z;
434  }
435 
441  {
443  double cx=value(x);
444  double d=1.0/(1+square(cx));
445  //double d2=square(d);
446  double u=::atan(cx);
447  *z.get_u()=u;
448  double zp=d;
449 
450  set_derivatives(z,x,u,zp);
451  return z;
452  }
453 
459  {
461  double u=value(x);
462  *z.get_u()=u*u;
463  double zp=2.0*u;
464 
465  set_derivatives(z,x,u,zp);
466  return z;
467  }
468 
474  {
476  double u=::tan(*x.get_u());
477  *z.get_u()=u;
478  double v=1.0/::cos(*x.get_u());
479  //double w=::sin(*x.get_u());
480  double v2=v*v;
481  double zp=v2;
482 
483  set_derivatives(z,x,u,zp);
484  return z;
485  }
486 
492  {
494  double u=::sin(*x.get_u());
495  *z.get_u()=u;
496  double zp=::cos(*x.get_u());
497 
498  set_derivatives(z,x,u,zp);
499  return z;
500  }
501 
507  {
509  double u=::log(*x.get_u());
510  *z.get_u()=u;
511  double zp=1/(*x.get_u());
512 
513  set_derivatives(z,x,u,zp);
514  return z;
515  }
516 
522  {
524  double u=::exp(*x.get_u());
525  *z.get_u()=u;
526  double zp=u;
527 
528  set_derivatives(z,x,u,zp);
529  return z;
530  }
531 
537  {
538  return exp(y*log(x));
539  }
540 
546  {
547  return exp(y*log(x));
548  }
549 
555  {
557  double xinv=1.0/(*x.get_u());
558  *z.get_u()=xinv;
559  double zp=-xinv*xinv;
560  set_derivatives(z,x,xinv,zp);
561 
562  return z;
563  }
564 
570  {
571  *get_u() = *x.get_u();
572  *get_u_x() = *x.get_u_x();
573  *get_u_y() = *x.get_u_y();
574  return *this;
575  }
576 
582  {
583  *get_u() = x;
584  *get_u_x() =0.0;
585  *get_u_y() =0.0;
586  return *this;
587  }
588 
594  const df1_two_variable& y)
595  {
597  double u= *x.get_u() * *y.get_u();
598  *z.get_u() = u;
599  double f_u=*y.get_u();
600  double f_v=*x.get_u();
601  set_derivatives(z,x,y,u, f_u, f_v);
602  return z;
603  }
604 
610  const df1_two_variable& y)
611  {
613  *z.get_u() = x * *y.get_u();
614  *z.get_u_x() = x * *y.get_u_x();
615  *z.get_u_y() = x * *y.get_u_y();
616  return z;
617  }
618 
624  double x)
625  {
627  *z.get_u() = x * *y.get_u();
628  *z.get_u_x() = x * *y.get_u_x();
629  *z.get_u_y() = x * *y.get_u_y();
630  return z;
631  }
632 
638  double y)
639  {
640  double u=1/y;
641  return x*u;
642  }
643 
649  const df1_two_variable& y)
650  {
651  df1_two_variable u=inv(y);
652  return x*u;
653  }
654 
660  const df1_two_variable& y)
661  {
662  df1_two_variable u=inv(y);
663  return x*u;
664  }
665 
671  {
673  *z.get_u() = x + *y.get_u();
674  *z.get_u_x() = *y.get_u_x();
675  *z.get_u_y() = *y.get_u_y();
676  return z;
677  }
678 
684  {
686  *z.get_u() = *x.get_u() + y;
687  *z.get_u_x() = *x.get_u_x();
688  *z.get_u_y() = *x.get_u_y();
689  return z;
690  }
691 
697  const df1_two_variable& y)
698  {
700  *z.get_u() = *x.get_u() + *y.get_u();
701  *z.get_u_x() = *x.get_u_x() + *y.get_u_x();
702  *z.get_u_y() = *x.get_u_y()+*y.get_u_y();
703  return z;
704  }
705 
711  const df1_two_variable& y)
712  {
714  *z.get_u() = *x.get_u() - *y.get_u();
715  *z.get_u_x() = *x.get_u_x() - *y.get_u_x();
716  *z.get_u_y() = *x.get_u_y() - *y.get_u_y();
717  return z;
718  }
719 
725  const df1_two_variable& y)
726  {
728  *z.get_u() = x - *y.get_u();
729  *z.get_u_x() = - *y.get_u_x();
730  *z.get_u_y() = - *y.get_u_y();
731  return z;
732  }
733 
739  double y)
740  {
742  *z.get_u() = *x.get_u()-y;
743  *z.get_u_x() = *x.get_u_x();
744  *z.get_u_y() = *x.get_u_y();
745  return z;
746  }
747 
753  {
754  deallocate();
755  }
756 
762  {
763  num_ind_var=0;
764  }
765 
771 {
772  if (num_ind_var > 1)
773  {
774  cerr << "can only have 2 independent_variables in df1_two_variable"
775  " function" << endl;
776  ad_exit(1);
777  }
778  else
779  {
781  ind_var[num_ind_var++]=&v;
782  *get_u() = value(v);
783  switch(num_ind_var)
784  {
785  case 1:
786  *get_u_x() = 1.0;
787  *get_u_y() = 0.0;
788  break;
789  case 2:
790  *get_u_x() = 0.0;
791  *get_u_y() = 1.0;
792  break;
793  default:
794  cerr << "illegal num_ind_var value of " << num_ind_var
795  << " in df1_two_variable function" << endl;
796  ad_exit(1);
797  }
798  }
799 }
800 
806  {
807  *get_u() = v;
808  *get_u_x() = 0.0;
809  *get_u_y() = 0.0;
810  }
811 
812 
818 {
819  // kludge to deal with constantness
820  df1_two_matrix & M= (df1_two_matrix &) MM;
821  int rmin=M.indexmin();
822  int cmin=M(rmin).indexmin();
823  int rmax=M.indexmax();
824  int cmax=M(rmin).indexmax();
825  if (rmin !=1 || cmin !=1)
826  {
827  cerr << "minimum row and column inidices must equal 1 in "
828  "df1b2matrix choleski_decomp(const df1_two_atrix& MM)"
829  << endl;
830  ad_exit(1);
831  }
832  if (rmax !=cmax)
833  {
834  cerr << "Error in df1b2matrix choleski_decomp(const df1_two_matrix& MM)"
835  " Matrix not square" << endl;
836  ad_exit(1);
837  }
838 
839  int n=rmax-rmin+1;
840  df1_two_matrix L(1,n,1,n);
841 #ifndef SAFE_INITIALIZE
842  L.initialize();
843 #endif
844 
845  int i,j,k;
846  df1_two_variable tmp;
847 
848  if (value(M(1,1))<=0)
849  {
850  cerr << "Error matrix not positive definite in choleski_decomp"
851  <<endl;
852  ad_exit(1);
853  }
854 
855  L(1,1)=sqrt(M(1,1));
856  for (i=2;i<=n;i++)
857  {
858  L(i,1)=M(i,1)/L(1,1);
859  }
860 
861  for (i=2;i<=n;i++)
862  {
863  for (j=2;j<=i-1;j++)
864  {
865  tmp=M(i,j);
866  for (k=1;k<=j-1;k++)
867  {
868  tmp-=L(i,k)*L(j,k);
869  }
870  L(i,j)=tmp/L(j,j);
871  }
872  tmp=M(i,i);
873  for (k=1;k<=i-1;k++)
874  {
875  tmp-=L(i,k)*L(i,k);
876  }
877 
878  if (value(tmp)<=0)
879  {
880  cerr << "Error matrix not positive definite in choleski_decomp"
881  <<endl;
882  ad_exit(1);
883  }
884 
885  L(i,i)=sqrt(tmp);
886  }
887 
888  return L;
889 }
890 
892  {
894  if (value(x)>=0.0)
895  z=x;
896  else
897  z=-x;
898  return z;
899  }
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
Description not yet available.
double * get_u() const
Definition: df12fun.h:60
void * trueptr
Address of first element in the vector.
Definition: vector_shapex.h:82
df1_two_variable & operator=(const df1_two_variable &v)
Description not yet available.
Definition: df12fun.cpp:569
df1_two_variable * v
Definition: df12fun.h:100
Description not yet available.
Definition: fvar.hpp:2030
df1_two_variable & operator*=(double v)
Mulitiply value v with values in df1_two_variable.
Definition: df12fun.cpp:343
void allocate(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat0.cpp:8
void allocate(void)
Does NOT allocate, but initializes empty df1_two_vector.
Definition: df12fun.cpp:157
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
static int num_ind_var
Definition: df12fun.h:59
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
void initialize(void)
Description not yet available.
Definition: df12fun.cpp:103
df1_two_variable & operator+=(const df1_two_variable &v)
Description not yet available.
Definition: df12fun.cpp:332
void deallocate(void)
Description not yet available.
Definition: df12fun.cpp:761
df1_one_matrix choleski_decomp(const df1_one_matrix &MM)
Definition: df11fun.cpp:606
df1_two_variable & operator/=(const df1_two_variable &v)
Description not yet available.
Definition: df12fun.cpp:298
init_df1_two_variable(const prevariable &)
Description not yet available.
Definition: df12fun.cpp:770
dmatrix operator*(const d3_array &t, const dvector &v)
Description not yet available.
Definition: d3arr12.cpp:17
void deallocate(void)
Description not yet available.
Definition: df12fun.cpp:216
~df1_two_matrix()
Description not yet available.
Definition: df12fun.cpp:207
void * get_pointer(void)
Definition: fvar.hpp:2046
df1_two_vector(void)
Default constructor.
Definition: df12fun.cpp:116
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
df1_two_variable()
Default constructor.
Definition: df12fun.cpp:22
#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
vector_shapex * shape
Definition: df12fun.h:99
Description not yet available.
Definition: fvar.hpp:2819
int indexmax(void) const
Definition: df12fun.h:103
unsigned int ncopies
Copy counter to enable shallow copies.
Definition: vector_shapex.h:79
unsigned int ncopies
Definition: fvar.hpp:2034
void initialize(void)
Description not yet available.
Definition: df12fun.cpp:241
Description not yet available.
Definition: df12fun.h:131
int indexmin(void) const
Definition: df12fun.h:138
df1_two_variable & operator-=(const df1_two_variable &v)
Description not yet available.
Definition: df12fun.cpp:285
Description not yet available.
Definition: df12fun.h:95
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
static prevariable * ind_var[]
Definition: df12fun.h:58
mat_shapex * shape
Definition: df12fun.h:135
int index_max
Definition: df12fun.h:98
int indexmax(void) const
Definition: df12fun.h:139
double v[3]
Definition: df12fun.h:56
double * get_u_y() const
Definition: df12fun.h:62
Description not yet available.
Definition: df12fun.h:54
df1_two_vector * v
Definition: df12fun.h:136
int indexmin(void) const
Definition: df12fun.h:102
df1_two_matrix(int rmin, int rmax, int cmin, int cmax)
Construct matrix of df1_two_variable with dimension [rmin to rmax] x [cmin to cmax].
Definition: df12fun.cpp:259
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_two_vector()
Destructor.
Definition: df12fun.cpp:55
void deallocate(void)
Description not yet available.
Definition: df12fun.cpp:64
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
int index_min
Definition: df12fun.h:97
~init_df1_two_variable()
Description not yet available.
Definition: df12fun.cpp:752
double * get_u_x() const
Definition: df12fun.h:61
d3_array pow(const d3_array &m, int e)
Description not yet available.
Definition: d3arr6.cpp:17