ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
d3arr.cpp
Go to the documentation of this file.
1 
6 #if (__cplusplus > 199711L)
7  #include <iterator>
8  #include <algorithm>
9 #endif
10 
11 #include "fvar.hpp"
12 #ifdef DEBUG
13  #include <cassert>
14 #endif
15 
21 double sum(const d3_array& darray)
22 {
23  double total = 0.0;
24 #if (__cplusplus <= 199711L)
25  for (int i = darray.indexmin(); i <= darray.indexmax(); ++i)
26  {
27  total += sum(darray.elem(i));
28  }
29 #else
30  std::for_each(darray.begin(), darray.end(), [&total](dmatrix& matrix) {
31  total += sum(matrix);
32  });
33 #endif
34  return total;
35 }
40  d3_array d3_array::sub(int nrl,int nrh)
41  {
42  if (allocated(*this))
43  {
44  d3_array tmp(nrl,nrh);
45  for (int i=nrl; i<=nrh; i++)
46  {
47  tmp[i].shallow_copy((*this)(i));
48  }
49  return tmp;
50  }
51  else
52  {
53  return *this;
54  }
55  }
62 #if defined(__INTEL_COMPILER) || defined(__OPENCC__)
63  { allocate(position.indexmin(), position.indexmax()); }
64 #else
65  :d3_array(position.indexmin(), position.indexmax()) { }
66 #endif
67 
74 d3_array::d3_array(int nrl, int nrh)
75 {
76  allocate(nrl, nrh);
77 }
78 
83  d3_array::d3_array(int sl,int sh,int nrl,int nrh,int ncl,int nch)
84  {
85  allocate(sl,sh,nrl,nrh,ncl,nch);
86 #ifndef OPT_LIB
87  initialize();
88 #endif
89  }
90 
95 d3_array::d3_array(int sl, int sh, int nrl, int nrh, const ivector& ncl,
96  int nch)
97  {
98  allocate(sl,sh,nrl,nrh,ncl,nch);
99 #ifndef OPT_LIB
100  initialize();
101 #endif
102  }
103 
108 d3_array::d3_array(int sl, int sh, int nrl, int nrh, const ivector& ncl,
109  const ivector& nch)
110  {
111  allocate(sl,sh,nrl,nrh,ncl,nch);
112 #ifndef OPT_LIB
113  initialize();
114 #endif
115  }
116 
128 void d3_array::allocate(int sl, int sh, int nrl, int nrh,
129  const ivector& ncl, int nch)
130 {
131 #ifdef DIAG
132  myheapcheck("Entering d3_array matrix(sl,sh,nrl,nrh,ncl,nch)" );
133 #endif
134 #ifndef OPT_LIB
135  if (sl != ncl.indexmin() || sh != ncl.indexmax())
136  {
137  cerr << "Incompatible d3_array bounds in "
138  << __FILE__ << ':' << __LINE__ << '\n';
139  ad_exit(1);
140  }
141 #endif
142  allocate(sl, sh);
143 
144  dmatrix* pti = t + sl;
145  int* pncli = ncl.get_v() + sl;
146  for (int i = sl; i <= sh; ++i)
147  {
148  pti->allocate(nrl, nrh, *pncli, nch);
149  ++pti;
150  ++pncli;
151  }
152 }
165  int sl, int sh,
166  int nrl, int nrh,
167  int ncl, const ivector& nch)
168 {
169 #ifdef DIAG
170  myheapcheck("Entering d3_array matrix(sl,sh,nrl,nrh,ncl,nch)" );
171 #endif
172 #ifndef OPT_LIB
173  if (sl != nch.indexmin() || sh != nch.indexmax())
174  {
175  cerr << "Incompatible d3_array bounds in "
176  << __FILE__ << ':' << __LINE__ << '\n';
177  ad_exit(1);
178  }
179 #endif
180  allocate(sl, sh);
181  dmatrix* pti = t + sl;
182  int* pnchi = nch.get_v() + sl;
183  for (int i = sl; i <= sh; ++i)
184  {
185  pti->allocate(nrl, nrh, ncl, *pnchi);
186  ++pti;
187  ++pnchi;
188  }
189 }
200 void d3_array::allocate(int sl, int sh, int nrl, int nrh, int ncl, int nch)
201 {
202  allocate(sl, sh);
203  dmatrix* pti = t + sl;
204  for (int i = sl; i <= sh; ++i)
205  {
206  pti->allocate(nrl, nrh, ncl, nch);
207  ++pti;
208  }
209 }
219 void d3_array::allocate(int sl, int sh, int nrl, int nrh)
220 {
221  allocate(sl, sh);
222  dmatrix* pti = t + sl;
223  for (int i = sl; i <= sh; ++i)
224  {
225  pti->allocate(nrl, nrh);
226  ++pti;
227  }
228 }
239  int sl, int sh,
240  const index_type& nrl,const index_type& nrh)
241 {
242  allocate(sl, sh);
243  dmatrix* pti = t + sl;
244  for (int i = sl; i <= sh; ++i)
245  {
246  pti->allocate(nrl(i), nrh(i));
247  ++pti;
248  }
249 }
257 void d3_array::allocate(int sl, int sh)
258 {
259  if (sl > sh)
260  {
261  return allocate();
262  }
263  if ((shape = new three_array_shape(sl, sh)) == 0)
264  {
265  cerr << " Error: d3_array unable to allocate memory in "
266  << __FILE__ << ':' << __LINE__ << '\n';
267  ad_exit(1);
268  }
269  if ((t = new dmatrix[slicesize()]) == 0)
270  {
271  cerr << " Error: d3_array unable to allocate memory in "
272  << __FILE__ << ':' << __LINE__ << '\n';
273  ad_exit(1);
274  }
275  t -= slicemin();
276  dmatrix* pti = t + sl;
277  for (int i = sl; i <= sh; ++i)
278  {
279  pti->allocate();
280  ++pti;
281  }
282 }
288 void d3_array::allocate(const d3_array& other)
289 {
290  int min = other.slicemin();
291  int max = other.slicemax();
292  allocate(min, max);
293  dmatrix* pti = t + min;
294  const dmatrix* potheri = &other(min);
295  for (int i = min; i <= max; ++i)
296  {
297  pti->allocate(*potheri);
298  ++pti;
299  ++potheri;
300  }
301 }
305  int sl, int sh,
306  const ivector& nrl, const ivector& nrh,
307  const imatrix& ncl, const imatrix& nch)
308 {
309  if (sl !=nrl.indexmin() || sh !=nrl.indexmax()
310  || sl !=nrh.indexmin() || sh !=nrh.indexmax())
311  {
312  cerr << "Incompatible d3_array bounds in "
313  << __FILE__ << ':' << __LINE__ << '\n';
314  ad_exit(1);
315  }
316  if ((shape = new three_array_shape(sl, sh)) == 0)
317  {
318  cerr << " Error: d3_array unable to allocate memory in "
319  << __FILE__ << ':' << __LINE__ << '\n';
320  ad_exit(1);
321  }
322  if ((t = new dmatrix[slicesize()]) == 0)
323  {
324  cerr << " Error: d3_array unable to allocate memory in "
325  << __FILE__ << ':' << __LINE__ << '\n';
326  ad_exit(1);
327  }
328  t -= sl;
329  dmatrix* pti = t + sl;
330  const int* pnrhi = nrh.get_v() + sl;
331  const int* pnrli = nrl.get_v() + sl;
332  const ivector* pnchi = &nch(sl);
333  const ivector* pncli = &ncl(sl);
334  for (int i = sl; i <= sh; ++i)
335  {
336  pti->allocate(*pnrli, *pnrhi, *pncli, *pnchi);
337  ++pti;
338  ++pnrhi;
339  ++pnrli;
340  ++pnchi;
341  ++pncli;
342  }
343 }
356  int sl, int sh,
357  const ivector& nrl, const ivector& nrh,
358  int ncl, int nch)
359 {
360  if (sl != nrl.indexmin() || sh != nrl.indexmax()
361  || sl != nrh.indexmin() || sh != nrh.indexmax())
362  {
363  cerr << "Incompatible d3_array bounds in "
364  << __FILE__ << ':' << __LINE__ << '\n';
365  ad_exit(1);
366  }
367  allocate(sl, sh);
368  dmatrix* pti = t + sl;
369  const int* pnrli = nrl.get_v() + sl;
370  const int* pnrhi = nrh.get_v() + sl;
371  for (int i = sl; i <= sh; ++i)
372  {
373  pti->allocate(*pnrli, *pnrhi, ncl, nch);
374  ++pti;
375  ++pnrli;
376  ++pnrhi;
377  }
378 }
391  int sl, int sh,
392  const ivector& nrl, int nrh,
393  int ncl, int nch)
394 {
395  allocate(sl, sh);
396  dmatrix* pti = t + sl;
397  const int* pnrli = nrl.get_v() + sl;
398  for (int i = sl; i <= sh; ++i)
399  {
400  pti->allocate(*pnrli, nrh, ncl, nch);
401  ++pti;
402  ++pnrli;
403  }
404 }
417  int sl, int sh,
418  int nrl, const ivector& nrh,
419  int ncl, int nch)
420 {
421  allocate(sl, sh);
422  dmatrix* pti = t + sl;
423  const int* pnrhi = nrh.get_v() + sl;
424  for (int i = sl; i <= sh; ++i)
425  {
426  pti->allocate(nrl, *pnrhi, ncl, nch);
427  ++pti;
428  ++pnrhi;
429  }
430 }
435 void d3_array::allocate(int sl, int sh, const ivector& nrl, const ivector& nrh,
436  int ncl, const imatrix& nch)
437  {
438  if (sl !=nrl.indexmin() || sh !=nrl.indexmax() ||
439  sl !=nrh.indexmin() || sh !=nrh.indexmax())
440  {
441  cerr << "Incompatible d3_array bounds in "
442  << __FILE__ << ':' << __LINE__ << '\n';
443  ad_exit(1);
444  }
445  if ((shape=new three_array_shape(sl, sh)) == 0)
446  {
447  cerr << " Error: d3_array unable to allocate memory in "
448  << __FILE__ << ':' << __LINE__ << '\n';
449  ad_exit(1);
450  }
451  if ( (t = new dmatrix[slicesize()]) == 0)
452  {
453  cerr << " Error: d3_array unable to allocate memory in "
454  << __FILE__ << ':' << __LINE__ << '\n';
455  ad_exit(1);
456  }
457  t -= sl;
458  dmatrix* pti = t + sl;
459  const int* pnrhi = nrh.get_v() + sl;
460  const int* pnrli = nrl.get_v() + sl;
461  const ivector* pnchi = &nch(sl);
462  for (int i = sl; i <= sh; ++i)
463  {
464  pti->allocate(*pnrli, *pnrhi, ncl, *pnchi);
465  ++pti;
466  ++pnrhi;
467  ++pnrli;
468  ++pnchi;
469  }
470  }
471 
483  int sl, int sh,
484  int nrl, const ivector& nrh,
485  int ncl, const imatrix& nch)
486 {
487  if (sl != nrh.indexmin() || sh != nrh.indexmax())
488  {
489  cerr << "Incompatible d3_array bounds in "
490  << __FILE__ << ':' << __LINE__ << '\n';
491  ad_exit(1);
492  }
493  if ((shape = new three_array_shape(sl, sh)) == 0)
494  {
495  cerr << " Error: d3_array unable to allocate memory in "
496  << __FILE__ << ':' << __LINE__ << '\n';
497  ad_exit(1);
498  }
499  if ( (t = new dmatrix[slicesize()]) == 0)
500  {
501  cerr << " Error: d3_array unable to allocate memory in "
502  << __FILE__ << ':' << __LINE__ << '\n';
503  ad_exit(1);
504  }
505  t -= slicemin();
506  dmatrix* pti = t + sl;
507  const int* pnrhi = nrh.get_v() + sl;
508  const ivector* pnchi = &nch(sl);
509  for (int i = sl; i <= sh; ++i)
510  {
511  pti->allocate(nrl, *pnrhi, ncl, *pnchi);
512  ++pti;
513  ++pnrhi;
514  ++pnchi;
515  }
516 }
528 void d3_array::allocate(int sl, int sh,
529  const ivector& nrl, const ivector& nrh,
530  const ivector& ncl, const ivector& nch)
531 {
532  if (sl != nrl.indexmin() || sh != nrl.indexmax()
533  || sl != nrh.indexmin() || sh != nrh.indexmax())
534  {
535  cerr << "Incompatible d3_array bounds in "
536  << __FILE__ << ':' << __LINE__ << '\n';
537  ad_exit(1);
538  }
539  allocate(sl, sh);
540  dmatrix* pti = t + sl;
541  const int* pnrhi = nrh.get_v() + sl;
542  const int* pnrli = nrl.get_v() + sl;
543  const int* pnchi = nch.get_v() + sl;
544  const int* pncli = ncl.get_v() + sl;
545  for (int i = sl; i <= sh; ++i)
546  {
547  pti->allocate(*pnrli, *pnrhi, *pncli, *pnchi);
548  ++pti;
549  ++pnrhi;
550  ++pnrli;
551  ++pnchi;
552  ++pncli;
553  }
554 }
568  int sl, int sh,
569  int nrl, int nrh,
570  const ivector& ncl, const ivector& nch)
571 {
572  if (sl != ncl.indexmin() || sh != ncl.indexmax()
573  || sl != nch.indexmin() || sh != nch.indexmax())
574  {
575  cerr << "Incompatible d3_array bounds in "
576  << __FILE__ << ':' << __LINE__ << '\n';
577  ad_exit(1);
578  }
579  allocate(sl, sh);
580  dmatrix* pti = t + sl;
581  const int* pnchi = nch.get_v() + sl;
582  const int* pncli = ncl.get_v() + sl;
583  for (int i = slicemin(); i <= slicemax(); ++i)
584  {
585  pti->allocate(nrl, nrh, *pncli, *pnchi);
586  ++pti;
587  ++pnchi;
588  ++pncli;
589  }
590 }
602 void d3_array::allocate(int sl, int sh,
603  int nrl, const ivector& nrh,
604  int ncl, const ivector& nch)
605 {
606  if (sl != nrh.indexmin() || sh != nrh.indexmax()
607  || sl != nch.indexmin() || sh != nch.indexmax())
608  {
609  cerr << "Incompatible d3_array bounds in "
610  << __FILE__ << ':' << __LINE__ << '\n';
611  ad_exit(1);
612  }
613  allocate(sl, sh);
614  dmatrix* pti = t + sl;
615  int* pnrhi = nrh.get_v() + sl;
616  int* pnchi = nch.get_v() + sl;
617  for (int i = sl; i <= sh; ++i)
618  {
619  pti->allocate(nrl, *pnrhi, ncl, *pnchi);
620  ++pti;
621  ++pnrhi;
622  ++pnchi;
623  }
624 }
625 
630 d3_array::d3_array(int sl, int sh, int nrl, const ivector& nrh, int ncl,
631  const ivector& nch)
632  {
633  allocate(sl,sh,nrl,nrh,ncl,nch);
634  }
635 
640 d3_array::d3_array(int sl, int sh, const ivector& nrl, const ivector& nrh,
641  const imatrix& ncl, const imatrix& nch)
642  {
643  allocate(sl,sh,nrl,nrh,ncl,nch);
644  }
645 
650 d3_array::d3_array(int sl, int sh, const ivector& nrl, const ivector& nrh,
651  int ncl, const imatrix& nch)
652  {
653  allocate(sl,sh,nrl,nrh,ncl,nch);
654  }
655 
660 d3_array::d3_array(int sl, int sh, int nrl, const ivector& nrh, int ncl,
661  const imatrix& nch)
662  {
663  allocate(sl,sh,nrl,nrh,ncl,nch);
664  }
665 
670 d3_array::d3_array(int sl, int sh, int nrl, const ivector& nrh, int ncl,
671  int nch)
672  {
673  allocate(sl,sh,nrl,nrh,ncl,nch);
674  }
675 
680 {
681  shallow_copy(other);
682 }
689 {
690  if (other.shape)
691  {
692  shape = other.shape;
693  ++(shape->ncopies);
694  t = other.t;
695  }
696  else
697  {
698 #ifdef DEBUG
699  cerr << "Warning -- Unable to shallow copy an unallocated d3_array.\n";
700 #endif
701  allocate();
702  }
703 }
706 {
707  // only initialize allocated objects
708  if (t)
709  {
710 #if (__cplusplus <= 199711L)
711  for (int i = slicemin(); i <= slicemax(); ++i)
712  {
713  t[i].initialize();
714  }
715 #else
716  std::for_each(begin(), end(), [](dmatrix& matrix) {
717  matrix.initialize();
718  });
719 #endif
720  }
721 }
724 {
725  if (shape)
726  {
727  if (shape->ncopies > 0)
728  {
729  --(shape->ncopies);
730  }
731  else
732  {
733  t += indexmin();
734  delete [] t;
735  delete shape;
736  }
737  allocate();
738  }
739 #if defined(DIAG)
740  else
741  {
742  cerr << "Warning -- Unable to deallocate an unallocated d3_array.\n";
743  //ad_exit(1);
744  }
745 #endif
746 }
749 {
750  deallocate();
751 }
759 : ncopies(0), slice_min(sl), slice_max(su)
760 {
761 }
void allocate(void)
Does NOT allocate, but initializes empty d3_array.
Definition: d3arr10.cpp:11
void myheapcheck(char *msg)
Does nothing.
Definition: dvector.cpp:669
Uses polymorphism to get index information from various data types to be used in constructing and all...
Definition: fvar.hpp:7731
unsigned int ncopies
Definition: fvar.hpp:3701
Description not yet available.
Definition: imatrix.h:69
void allocate(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat0.cpp:8
dmatrix * begin() const
Definition: fvar.hpp:3809
int indexmax() const
Definition: fvar.hpp:3822
int allocated(const ivector &v)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: fvar_a59.cpp:13
three_array_shape * shape
Definition: fvar.hpp:3730
double sum(const d3_array &darray)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr.cpp:21
Description not yet available.
Definition: fvar.hpp:3698
int indexmin() const
Definition: fvar.hpp:4909
exitptr ad_exit
Definition: gradstrc.cpp:53
dmatrix * t
Definition: fvar.hpp:3729
int slicemax() const
Definition: fvar.hpp:3830
d3_array(void)
Default constructor.
Definition: d3arr10.cpp:20
void shallow_copy(const d3_array &)
Shallow copy other data structure pointers.
Definition: d3arr.cpp:688
int * get_v() const
Definition: ivector.h:114
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
Description not yet available.
Definition: fvar.hpp:4902
d3_array sub(int, int)
Description not yet available.
Definition: d3arr.cpp:40
#define min(a, b)
Definition: cbivnorm.cpp:188
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
int indexmin() const
Definition: ivector.h:99
int slicemin() const
Definition: fvar.hpp:3826
int indexmax() const
Definition: ivector.h:104
Description not yet available.
Definition: fvar.hpp:2819
void initialize(void)
Initializes all elements of d3_array to zero.
Definition: d3arr.cpp:705
dmatrix * end() const
Definition: fvar.hpp:3813
~d3_array()
Destructor.
Definition: d3arr.cpp:748
int indexmin() const
Definition: fvar.hpp:3818
dmatrix & elem(int k)
Definition: fvar.hpp:3870
unsigned int slicesize() const
Definition: fvar.hpp:3853
#define max(a, b)
Definition: cbivnorm.cpp:189
Description not yet available.
Definition: fvar.hpp:3727
int indexmax() const
Definition: fvar.hpp:4913
void initialize(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat7.cpp:12
three_array_shape(int sl, int sh)
Stores dimensions for dvar3_array.
Definition: d3arr.cpp:758
void deallocate(void)
Deallocates d3_array memory.
Definition: d3arr.cpp:723