ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
f1b2vc5.cpp
Go to the documentation of this file.
1 
6 #include <df1b2fun.h>
7 #ifndef OPT_LIB
8  #include <cassert>
9 #endif
10 
15 void check_shape(const df1b2vector & _x,const dvector & _y,const char * s)
16 {
19  if (x.indexmin() != y.indexmin() || x.indexmax() != y.indexmax())
20  {
21  cerr << "Incompatible shapes in df1b2vector function" << s << endl;
22  ad_exit(1);
23  }
24 }
25 
30 void check_shape(const df1b2vector & _x,const df1b2vector & _y,const char * s)
31 {
34  if (x.indexmin() != y.indexmin() || x.indexmax() != y.indexmax())
35  {
36  cerr << "Incompatible shapes in df1b2vector function" << s << endl;
37  ad_exit(1);
38  }
39 }
40 
45 void check_shape(const dvector & _x,const df1b2vector & _y,const char * s)
46 {
49  if (x.indexmin() != y.indexmin() || x.indexmax() != y.indexmax())
50  {
51  cerr << "Incompatible shapes in df1b2vector function" << s << endl;
52  ad_exit(1);
53  }
54 }
55 
60 void check_shape(const df1b2vector & _x,const df1b2matrix & _y,const char * s)
61 {
64  if (x.indexmin() != y(y.indexmin()).indexmin() ||
65  x.indexmax() != y(y.indexmin()).indexmax())
66  {
67  cerr << "Incompatible shapes in df1b2vector function" << s << endl;
68  ad_exit(1);
69  }
70 }
71 
77 {
78  if (allocated())
79  {
81  check_shape(*this,x,"df1b2vector& df1b2vector::operator =");
82  int mmin=x.indexmin();
83  int mmax=x.indexmax();
84  for (int i=mmin;i<=mmax;i++)
85  {
86  (*this)(i)=x(i);
87  }
88  }
89  else
90  {
91  copy(_x);
92  }
93  return *this;
94 }
95 
101 {
103  check_shape(*this,x,"df1b2vector& df1b2vector::operator =");
104  int mmin=x.indexmin();
105  int mmax=x.indexmax();
106  for (int i=mmin;i<=mmax;i++) (*this)(i)=x(i);
107  return *this;
108 }
109 
115 {
117  int mmin=indexmin();
118  int mmax=indexmax();
119  for (int i=mmin;i<=mmax;i++) (*this)(i)=x;
120  return *this;
121 }
128 {
129  int mmin=indexmin();
130  int mmax=indexmax();
131  for (int i=mmin;i<=mmax;i++) (*this)(i)=x;
132  return *this;
133 }
134 
140 {
143  //check_shape(x,M,"operator *");
144  int rmin=M.indexmin();
145  int rmax=M.indexmax();
146  //int mmin=x.indexmin();
147  //int mmax=x.indexmax();
148  df1b2vector tmp(rmin,rmax);
149  tmp.initialize();
150  for (int i=rmin;i<=rmax;i++)
151  {
152  tmp(i)=M(i)*x;
153  }
154  return tmp;
155 }
156 
162 {
165  //check_shape(x,M,"operator *");
166  int rmin=M.indexmin();
167  int rmax=M.indexmax();
168  //int mmin=x.indexmin();
169  //int mmax=x.indexmax();
170  df1b2vector tmp(rmin,rmax);
171  tmp.initialize();
172  for (int i=rmin;i<=rmax;i++)
173  {
174  tmp(i)=M(i)*x;
175  }
176  return tmp;
177 }
186 {
189  //check_shape(x,M,"operator *");
190  int rmin = M.indexmin();
191  int cmin = M(rmin).indexmin();
192  int cmax = M(rmin).indexmax();
193  int mmin = x.indexmin();
194  int mmax = x.indexmax();
195  df1b2vector tmp(cmin, cmax);
196 #ifndef OPT_LIB
197  tmp.initialize();
198 #endif
199  for (int i = cmin; i <= cmax; ++i)
200  {
201  for (int j = mmin; j <= mmax; ++j)
202  {
203  tmp(i) += M(j, i) * x(j);
204  }
205  }
206  return tmp;
207 }
216 {
219  //check_shape(x,M,"operator *");
220  int rmin = M.indexmin();
221  int cmin = M(rmin).indexmin();
222  int cmax = M(rmin).indexmax();
223  int mmin = x.indexmin();
224  int mmax = x.indexmax();
225  df1b2vector tmp(cmin, cmax);
226 #ifndef OPT_LIB
227  tmp.initialize();
228 #endif
229  for (int i = cmin; i <= cmax; ++i)
230  {
231  for (int j = mmin; j <= mmax; ++j)
232  {
233  tmp(i) += M(j, i) * x(j);
234  }
235  }
236  return tmp;
237 }
246 {
249  //check_shape(x,M,"operator *");
250  int rmin = M.indexmin();
251  int cmin = M(rmin).indexmin();
252  int cmax = M(rmin).indexmax();
253  int mmin = x.indexmin();
254  int mmax = x.indexmax();
255  df1b2vector tmp(cmin, cmax);
256 #ifndef OPT_LIB
257  tmp.initialize();
258 #endif
259  for (int i = cmin; i <= cmax; ++i)
260  {
261  for (int j = mmin; j <= mmax; ++j)
262  {
263  tmp(i) += M(j, i) * x(j);
264  }
265  }
266  return tmp;
267 }
268 
274 {
275  df1b2matrix& M = (df1b2matrix&)_MM;
276  df1b2matrix& N = (df1b2matrix&)_NN;
277  //check_shape(x,M,"operator *");
278  int rmin=M.indexmin();
279  int rmax=M.indexmax();
280  int kmin=N.rowmin();
281  int kmax=N.rowmax();
282  int cmin=N(kmin).indexmin();
283  int cmax=N(kmin).indexmax();
284  if (M(rmin).indexmin()!=N.indexmin() || M(rmin).indexmax()!=N.indexmax())
285  {
286  cerr << "incompatible matrix sizes" << endl;
287  ad_exit(1);
288  }
289  df1b2matrix tmp(rmin,rmax,cmin,cmax);
290  tmp.initialize();
291  for (int i=rmin;i<=rmax;i++)
292  {
293  for (int j=cmin;j<=cmax;j++)
294  {
295  for (int k=kmin;k<=kmax;k++)
296  {
297  tmp(i,j)+=M(i,k)*N(k,j);
298  }
299  }
300  }
301  return tmp;
302 }
303 
309 {
312  //check_shape(x,M,"operator *");
313  int rmin=M.indexmin();
314  int rmax=M.indexmax();
315  int cmin=x.indexmin();
316  int cmax=x.indexmax();
317  df1b2vector tmp(rmin,rmax);
318  for (int i=rmin;i<=rmax;i++)
319  for (int j=cmin;j<=cmax;j++)
320  tmp(i)+=M(i,j)*x(j);
321  return tmp;
322 }
323 
329 {
330  df1b2matrix& M = (df1b2matrix&)_MM;
331  df1b2matrix& N = (df1b2matrix&)_NN;
332  int rmin=M.indexmin();
333  int rmax=M.indexmax();
334  df1b2matrix tmp(rmin,rmax);
335  for (int i=rmin;i<=rmax;i++)
336  {
337  int cmin=M(i).indexmin();
338  int cmax=M(i).indexmax();
339  tmp(i).noallocate(cmin,cmax);
340  for (int j=cmin;j<=cmax;j++)
341  {
342  tmp(i,j)=M(i,j)*N(i,j);
343  }
344  }
345  return tmp;
346 }
347 
352 df1b2matrix elem_prod(const dmatrix& _MM,const df1b2matrix& _NN)
353 {
354  dmatrix& M = (dmatrix&)_MM;
355  df1b2matrix& N = (df1b2matrix&)_NN;
356  int rmin=M.indexmin();
357  int rmax=M.indexmax();
358  df1b2matrix tmp(rmin,rmax);
359  for (int i=rmin;i<=rmax;i++)
360  {
361  int cmin=M(i).indexmin();
362  int cmax=M(i).indexmax();
363  tmp(i).noallocate(cmin,cmax);
364  for (int j=cmin;j<=cmax;j++)
365  {
366  tmp(i,j)=M(i,j)*N(i,j);
367  }
368  }
369  return tmp;
370 }
371 
376 df1b2matrix elem_prod(const df1b2matrix& _MM,const dmatrix& _NN)
377 {
378  df1b2matrix& M = (df1b2matrix&)_MM;
379  dmatrix& N = (dmatrix&)_NN;
380  int rmin=M.indexmin();
381  int rmax=M.indexmax();
382  df1b2matrix tmp(rmin,rmax);
383  for (int i=rmin;i<=rmax;i++)
384  {
385  int cmin=M(i).indexmin();
386  int cmax=M(i).indexmax();
387  tmp(i).noallocate(cmin,cmax);
388  for (int j=cmin;j<=cmax;j++)
389  {
390  tmp(i,j)=M(i,j)*N(i,j);
391  }
392  }
393  return tmp;
394 }
395 
401 {
402  df1b2matrix& M = (df1b2matrix&)_MM;
403  df1b2matrix& N = (df1b2matrix&)_NN;
404  int rmin=M.indexmin();
405  int rmax=M.indexmax();
406  df1b2matrix tmp(rmin,rmax);
407  for (int i=rmin;i<=rmax;i++)
408  {
409  int cmin=M(i).indexmin();
410  int cmax=M(i).indexmax();
411  tmp(i).noallocate(cmin,cmax);
412  for (int j=cmin;j<=cmax;j++)
413  {
414  tmp(i,j)=M(i,j)/N(i,j);
415  }
416  }
417  return tmp;
418 }
419 
424 df1b2matrix elem_div(const dmatrix& _MM,const df1b2matrix& _NN)
425 {
426  dmatrix& M = (dmatrix&)_MM;
427  df1b2matrix& N = (df1b2matrix&)_NN;
428  int rmin=M.indexmin();
429  int rmax=M.indexmax();
430  df1b2matrix tmp(rmin,rmax);
431  for (int i=rmin;i<=rmax;i++)
432  {
433  int cmin=M(i).indexmin();
434  int cmax=M(i).indexmax();
435  tmp(i).noallocate(cmin,cmax);
436  for (int j=cmin;j<=cmax;j++)
437  {
438  tmp(i,j)=M(i,j)/N(i,j);
439  }
440  }
441  return tmp;
442 }
443 
448 df1b2matrix elem_div(const df1b2matrix& _MM,const dmatrix& _NN)
449 {
450  df1b2matrix& M = (df1b2matrix&)_MM;
451  dmatrix& N = (dmatrix&)_NN;
452  int rmin=M.indexmin();
453  int rmax=M.indexmax();
454  df1b2matrix tmp(rmin,rmax);
455  for (int i=rmin;i<=rmax;i++)
456  {
457  int cmin=M(i).indexmin();
458  int cmax=M(i).indexmax();
459  tmp(i).noallocate(cmin,cmax);
460  for (int j=cmin;j<=cmax;j++)
461  {
462  tmp(i,j)=M(i,j)/N(i,j);
463  }
464  }
465  return tmp;
466 }
467 
473 {
474  ADUNCONST(dvector,v)
476  int rmin=v.indexmin();
477  int rmax=v.indexmax();
478  df1b2vector tmp;
479  tmp.noallocate(rmin,rmax);
480  for (int i=rmin;i<=rmax;i++)
481  {
482  tmp(i)=v(i)/w(i);
483  }
484  return tmp;
485 }
486 
492 {
495  int rmin=v.indexmin();
496  int rmax=v.indexmax();
497  df1b2vector tmp;
498  tmp.noallocate(rmin,rmax);
499  for (int i=rmin;i<=rmax;i++)
500  {
501  tmp(i)=v(i)/w(i);
502  }
503  return tmp;
504 }
505 
511 {
514  int rmin=v.indexmin();
515  int rmax=v.indexmax();
516  df1b2vector tmp;
517  tmp.noallocate(rmin,rmax);
518  for (int i=rmin;i<=rmax;i++)
519  {
520  tmp(i)=v(i)*w(i);
521  }
522  return tmp;
523 }
524 
530 {
533  int rmin=v.indexmin();
534  int rmax=v.indexmax();
535  df1b2vector tmp;
536  tmp.noallocate(rmin,rmax);
537  for (int i=rmin;i<=rmax;i++)
538  {
539  tmp(i)=v(i)*w(i);
540  }
541  return tmp;
542 }
543 
549 {
550  ADUNCONST(dvector,v)
552  int rmin=v.indexmin();
553  int rmax=v.indexmax();
554  df1b2vector tmp;
555  tmp.noallocate(rmin,rmax);
556  for (int i=rmin;i<=rmax;i++)
557  {
558  tmp(i)=v(i)*w(i);
559  }
560  return tmp;
561 }
562 
568 {
571  int rmin=v.indexmin();
572  int rmax=v.indexmax();
573  df1b2vector tmp;
574  tmp.noallocate(rmin,rmax);
575  for (int i=rmin;i<=rmax;i++)
576  {
577  tmp(i)=v(i)/w(i);
578  }
579  return tmp;
580 }
581 
586 df1b2matrix::df1b2matrix(int nrl,int nrh,int ncl,int nch)
587 {
588  if (nrl>nrh)
589  {
590  allocate();
591  }
592  else
593  {
594  allocate(nrl,nrh,ncl,nch);
595  }
596 }
597 
602 df1b2matrix::df1b2matrix(int nrl,int nrh)
603 {
604  if (nrl>nrh)
605  {
606  allocate();
607  }
608  else
609  {
610  allocate(nrl,nrh);
611  }
612 }
613 
618 df1b2matrix::df1b2matrix(int nrl,int nrh,const index_type& ncl,
619  const index_type& nch)
620 {
621  if (nrl>nrh)
622  {
623  allocate();
624  }
625  else
626  {
627  allocate(nrl,nrh,ncl,nch);
628  }
629 }
630 
636 {
637  allocate();
638 }
639 
644 void df1b2matrix::allocate(int nrl,int nrh,int ncl,int nch,const char * s)
645 {
646  allocate(nrl,nrh,ncl,nch);
647 }
648 
653 void df1b2matrix::allocate(int nrl,int nrh,const index_type& ncl,
654  const index_type& nch,const char * s)
655 {
656  allocate(nrl,nrh,ncl,nch);
657 }
658 
663 void df1b2matrix::allocate(int nrl,int nrh,int ncl,int nch)
664 {
665  index_min=nrl;
666  index_max=nrh;
667  if ( (v = new df1b2vector[size()]) == 0)
668  {
669  cerr << " Error allocating memory in df1b2matrix contructor\n";
670  ad_exit(21);
671  }
672  if ( (shape=new mat_shapex(v)) == 0)
673  {
674  cerr << " Error allocating memory in df1b2matrix contructor\n";
675  }
676  v -= indexmin();
677  for (int i=nrl; i<=nrh; i++)
678  {
679  v[i].allocate(ncl,nch);
680  }
681 }
682 
687 void df1b2matrix::allocate(int nrl,int nrh,const index_type& ncl,
688  const index_type& nch)
689 {
690  index_min=nrl;
691  index_max=nrh;
692  if ( (v = new df1b2vector[size()]) == 0)
693  {
694  cerr << " Error allocating memory in df1b2matrix contructor\n";
695  ad_exit(21);
696  }
697  if ( (shape=new mat_shapex(v)) == 0)
698  {
699  cerr << " Error allocating memory in df1b2matrix contructor\n";
700  }
701  v -= indexmin();
702  for (int i=nrl; i<=nrh; i++)
703  {
704  v[i].allocate(ncl(i),nch(i));
705  }
706 }
711 {
714  v=x.v;
715  shape=x.shape;
716  if (shape) (shape->ncopies)++;
717 }
723 void df1b2matrix::allocate(int nrl,int nrh)
724 {
725  index_min=nrl;
726  index_max=nrh;
727  if ( (v = new df1b2vector[size()]) == 0)
728  {
729  cerr << " Error allocating memory in df1b2matrix contructor\n";
730  ad_exit(21);
731  }
732  if ( (shape=new mat_shapex(v)) == 0)
733  {
734  cerr << " Error allocating memory in df1b2matrix contructor\n";
735  }
736  v -= indexmin();
737  /*
738  for (int i=nrl; i<=nrh; i++)
739  {
740  v[i].allocate(ncl,nch);
741  }
742  */
743 }
748 {
749  if (shape)
750  {
751  if (shape->ncopies)
752  {
753  (shape->ncopies)--;
754  }
755  else
756  {
757  deallocate();
758  }
759  }
760 }
761 
767 {
768  if (shape)
769  {
771  delete [] v;
772  delete shape;
773  allocate();
774  }
775 }
776 
782 {
783  index_min=1;
784  index_max=0;
785  v=0;
786  shape=0;
787 }
788 
795 void df1b2matrix::colfill(const int j, const df1b2vector& values)
796 {
797  //RETURN_ARRAYS_INCREMENT(); makes no sense for df1b2 stuff
798  for (int i = rowmin(); i <= rowmax(); ++i)
799  {
800  (*this)[i][j] = values[i];
801  }
802  //RETURN_ARRAYS_DECREMENT();
803 }
804 
810 {
812  df1b2variable tmp;
813  tmp=0.0;
814  int mmin=x.indexmin();
815  int mmax=x.indexmax();
816  for (int i=mmin;i<=mmax;i++)
817  {
818  tmp+=x(i);
819  }
820  return tmp;
821 }
822 
828 {
830  df1b2variable tmp;
831  tmp=0.0;
832  int mmin=x.indexmin();
833  int mmax=x.indexmax();
834  double fn=mmax-mmin+1;
835  for (int i=mmin;i<=mmax;i++)
836  {
837  tmp+=x(i);
838  }
839  return tmp/fn;
840 }
841 
847 {
849  df1b2variable tmp;
850  tmp=0.0;
851  int mmin=x.indexmin();
852  int mmax=x.indexmax();
853  for (int i=mmin;i<=mmax;i++)
854  {
855  tmp+=square(x(i));
856  }
857  return tmp;
858 }
859 df1b2variable sumsq(const df1b2vector& _x) {return(norm2(_x));}
860 
866 {
868  df1b2variable tmp;
869  tmp=0.0;
870  int mmin=x.indexmin();
871  int mmax=x.indexmax();
872  for (int i=mmin;i<=mmax;i++)
873  {
874  tmp+=square(x(i));
875  }
876  return sqrt(tmp);
877 }
878 
884 {
886  df1b2variable tmp;
887  tmp=0.0;
888  int mmin=x.indexmin();
889  int mmax=x.indexmax();
890  for (int i=mmin;i<=mmax;i++)
891  {
892  tmp+=norm2(x(i));
893  }
894  return tmp;
895 }
896 df1b2variable sumsq(const df1b2matrix& _x) {return(norm2(_x));}
897 
903 {
905  df1b2variable tmp;
906  tmp=0.0;
907  int mmin=x.indexmin();
908  int mmax=x.indexmax();
909  for (int i=mmin;i<=mmax;i++)
910  {
911  tmp+=norm2(x(i));
912  }
913  return sqrt(tmp);
914 }
915 
921 {
923  df1b2variable tmp;
924  tmp=0.0;
925  int mmin=x.indexmin();
926  int mmax=x.indexmax();
927  for (int i=mmin;i<=mmax;i++)
928  {
929  tmp+=sum(x(i));
930  }
931  return tmp;
932 }
933 
939 {
941  df1b2variable tmp;
942  tmp=0.0;
943  int mmin=x.indexmin();
944  int mmax=x.indexmax();
945  int nitems=0;
946  for (int i=mmin;i<=mmax;i++)
947  {
948  tmp+=sum(x(i));
949  nitems+=x(i).indexmax()-x(i).indexmin()+1;
950  }
951 #ifndef OPT_LIB
952  assert(nitems > 0);
953 #endif
954  return tmp/nitems;
955 }
df1b2matrix(void)
Description not yet available.
Definition: f1b2vc5.cpp:635
d3_array elem_prod(const d3_array &a, const d3_array &b)
Returns d3_array results with computed elements product of a(i, j, k) * b(i, j, k).
Definition: d3arr2a.cpp:92
Uses polymorphism to get index information from various data types to be used in constructing and all...
Definition: fvar.hpp:7731
Description not yet available.
Definition: fvar.hpp:2030
#define N
Definition: rngen.cpp:56
double sumsq(const d3_array &a)
Definition: d3arr2a.cpp:181
#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: df1b2fun.h:1054
~df1b2matrix()
Destructor.
Definition: f1b2vc5.cpp:747
int indexmin() const
Definition: fvar.hpp:2917
void allocate(int lb, int ub, const char *)
Description not yet available.
Definition: f1b2vc1.cpp:639
double sum(const d3_array &darray)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr.cpp:21
void copy(const df1b2vector &)
Description not yet available.
Definition: f1b2vc1.cpp:791
Description not yet available.
Definition: df1b2fun.h:953
d3_array elem_div(const d3_array &a, const d3_array &b)
Returns d3_array results with computed elements division of a(i, j, k) / b(i, j, k).
Definition: d3arr2a.cpp:112
exitptr ad_exit
Definition: gradstrc.cpp:53
void initialize(void)
Description not yet available.
Definition: f1b2vc2.cpp:157
int indexmin(void) const
Definition: df1b2fun.h:969
int rowmin(void) const
Definition: df1b2fun.h:1053
df1b2vector * v
Definition: df1b2fun.h:1046
double mean(const dvector &vec)
Returns computed mean of vec.
Definition: cranfill.cpp:43
double norm(const d3_array &a)
Return computed norm value of a.
Definition: d3arr2a.cpp:190
Description not yet available.
Definition: df1b2fun.h:266
int index_min
Definition: df1b2fun.h:1044
dmatrix operator*(const d3_array &t, const dvector &v)
Description not yet available.
Definition: d3arr12.cpp:17
mat_shapex * shape
Definition: df1b2fun.h:1047
void * get_pointer(void)
Definition: fvar.hpp:2046
df1b2vector & operator=(const df3_one_vector &)
Definition: df3fun.cpp:760
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
void noallocate(int lib, int ub)
Description not yet available.
Definition: f1b2vc1.cpp:733
int index_max
Definition: df1b2fun.h:1045
#define M
Definition: rngen.cpp:57
void deallocate(void)
Description not yet available.
Definition: f1b2vc5.cpp:766
Description not yet available.
Definition: fvar.hpp:2819
double norm2(const d3_array &a)
Return sum of squared elements in a.
Definition: d3arr2a.cpp:167
values
Definition: adjson.h:22
unsigned int ncopies
Definition: fvar.hpp:2034
void check_shape(const df1b2vector &x, const df1b2vector &y, const char *s)
Description not yet available.
Definition: f1b2vc5.cpp:30
int indexmax() const
Definition: fvar.hpp:2921
Description not yet available.
Definition: df1b2fun.h:1042
#define w
int indexmax(void) const
Definition: df1b2fun.h:970
int indexmax(void) const
Definition: df1b2fun.h:1055
Description not yet available.
void initialize(void)
Description not yet available.
Definition: f1b2vc3.cpp:287
void colfill(const int j, const df1b2vector &v)
Copy vector values into column j of df1b2matrix.
Definition: f1b2vc5.cpp:795
void allocate(void)
Description not yet available.
Definition: f1b2vc5.cpp:781
unsigned int size() const
Definition: df1b2fun.h:1057
int allocated(void)
Definition: df1b2fun.h:968
int rowmax(void) const
Definition: df1b2fun.h:1056
double square(const double value)
Return square of value; constant object.
Definition: d3arr4.cpp:16