ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
d4arr.cpp
Go to the documentation of this file.
1 
5 #include "fvar.hpp"
6 #include "admb_messages.h"
7 
15 d4_array::d4_array(int nrl, int nrh)
16 {
17  allocate(nrl, nrh);
18 }
27  ncopies(0),
28  hslice_min(hsl),
29  hslice_max(hsu)
30 {
31  //,int sl,int su,int rl,
32  // int ru,int cl,int cu)
33  //slice_min=sl;
34  //slice_max=su;
35  //row_min=rl;
36  //row_max=ru;
37  //col_min=cl;
38  //col_max=cu;
39 }
45 double sum(const d4_array& darray)
46 {
47  double total = 0.0;
48 
49  int min = darray.indexmin();
50  int max = darray.indexmax();
51  const d3_array* pdarrayi = &darray(min);
52  for (int i = min; i <= max; ++i)
53  {
54  total += sum(*pdarrayi);
55  ++pdarrayi;
56  }
57  return total;
58 }
59 
64  d4_array d4_array::sub(int nrl,int nrh)
65  {
66  if (allocated(*this))
67  {
68  d4_array tmp(nrl,nrh);
69  for (int i=nrl; i<=nrh; i++)
70  {
71  tmp[i].shallow_copy((*this)(i));
72  }
73  return tmp;
74  }
75  else
76  {
77  return *this;
78  }
79  }
82 {
83  shallow_copy(other);
84 }
90 void d4_array::shallow_copy(const d4_array& other)
91 {
92  if (other.shape)
93  {
94  shape = other.shape;
95  ++(shape->ncopies);
96  t = other.t;
97  }
98  else
99  {
100 #ifdef DEBUG
101  cerr << "Warning -- Unable to shallow copy an unallocated d4_array.\n";
102 #endif
103  allocate();
104  }
105 }
108 {
109  if (shape)
110  {
111  if (shape->ncopies > 0)
112  {
113  --(shape->ncopies);
114  }
115  else
116  {
117  t += indexmin();
118  delete [] t;
119  delete shape;
120  }
121  allocate();
122  }
123 #if defined(DIAG)
124  else
125  {
126  cerr << "Warning -- Unable to deallocate an unallocated d4_array.\n";
127  //ad_exit(1);
128  }
129 #endif
130 }
133 {
134  deallocate();
135 }
142 {
143  int min = hslicemin();
144  int max = hslicemax();
145  if (min != other.hslicemin() || max != other.hslicemax())
146  {
147  cerr << "Incompatible bounds in"
148  << " d4_array& d4_array::operator=(const d4_array&).\n";
149  ad_exit(1);
150  }
151  d3_array* pti = t + min;
152  const d3_array* potheri = &other(min);
153  for (int i = min; i <= max; ++i)
154  {
155  *pti = *potheri;
156 
157  ++pti;
158  ++potheri;
159  }
160  return *this;
161 }
167 void d4_array::allocate(const d4_array& other)
168 {
169  int min = other.hslicemin();
170  int max = other.hslicemax();
171  allocate(min, max);
172  d3_array* pti = t + min;
173  const d3_array* potheri = &other(min);
174  for (int i = min; i <= max; ++i)
175  {
176  pti->allocate(*potheri);
177 
178  ++pti;
179  ++potheri;
180  }
181 }
182 
183  #ifndef OPT_LIB
184 
190  {
191  if (i < hslicemin() || i > hslicemax())
192  {
193  ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
194  "d3_array& d4_array::operator() (int i)", hslicemin(), hslicemax(), i);
195  }
196  return t[i];
197  }
198 
204  {
205  if (i < hslicemin() || i > hslicemax())
206  {
207  ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
208  "d3_array& d4_array::operator[] (int i)",
209  hslicemin(), hslicemax(), i);
210  }
211  return t[i];
212  }
213 
218  dmatrix& d4_array::operator ( ) (int i ,int j)
219  {
220  if (i < hslicemin() || i > hslicemax())
221  {
222  ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
223  "dmatrix& d4_array::operator() (int i, int j)",
224  hslicemin(), hslicemax(), i);
225  }
226  return ((*this)(i))(j);
227  }
228 
233  dvector& d4_array::operator ( ) (int i,int j,int k)
234  {
235  if (i < hslicemin() || i > hslicemax())
236  {
237  ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
238  "dvector& d4_array::operator() (int i, int j, int k)",
239  hslicemin(), hslicemax(), i);
240  }
241  return (((*this)(i,j))(k));
242  }
243 
248  double& d4_array::operator ( ) (int i,int j,int k,int l)
249  {
250  if (i < hslicemin() || i > hslicemax())
251  {
252  ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
253  "double& d4_array::operator() (int i, int j, int k, int l)",
254  hslicemin(), hslicemax(), i);
255  }
256  return ( ((*this)(i,j,k))(l));
257  }
258 
263 const d3_array& d4_array::operator()(int i) const
264  {
265  if (i<hslicemin()||i>hslicemax())
266  { cerr << "Error hslice index out of bounds in\n"
267  "d3_array& d4_array::operator ( )" << endl;
268  ad_exit(1);
269  }
270  return t[i];
271  }
272 
277 const d3_array& d4_array::operator[](int i) const
278  {
279  if (i<hslicemin()||i>hslicemax())
280  { cerr << "Error hslice index out of bounds in\n"
281  "d3_array& d4_array::operator ( )" << endl;
282  ad_exit(1);
283  }
284  return t[i];
285  }
286 
291 const dmatrix& d4_array::operator()(int i, int j) const
292  {
293  if (i<hslicemin()||i>hslicemax())
294  { cerr << "Error hslice index out of bounds in\n"
295  "dmatrix& d4_array::operator ( )" << endl;
296  ad_exit(1);
297  }
298  return ((*this)(i))(j);
299  }
300 
305 const dvector& d4_array::operator()(int i, int j, int k) const
306  {
307  if (i<hslicemin()||i>hslicemax())
308  { cerr << "Error hslice index out of bounds in\n"
309  "dvector& d4_array::operator ( )" << endl;
310  ad_exit(1);
311  }
312  return (((*this)(i,j))(k));
313  }
314 
319 const double& d4_array::operator()(int i, int j, int k, int l) const
320  {
321  if (i<hslicemin()||i>hslicemax())
322  { cerr << "Error hslice index out of bounds in\n"
323  "double& d4_array::operator ( )" << endl;
324  ad_exit(1);
325  }
326  return ( ((*this)(i,j,k))(l));
327  }
328 
329 #endif
330 
345  int hsl, int hsu,
346  int sl, int sh,
347  int nrl, int nrh,
348  int ncl, int nch)
349 {
350  if ((shape = new four_array_shape(hsl, hsu)) == 0)
351  {
352  cerr << " Error: d4_array unable to allocate memory in "
353  << __FILE__ << ':' << __LINE__ << '\n';
354  ad_exit(1);
355  }
356  if ( (t = new d3_array[hslicesize()]) == 0)
357  {
358  cerr << " Error: d4_array unable to allocate memory in "
359  << __FILE__ << ':' << __LINE__ << '\n';
360  ad_exit(1);
361  }
362  t -= hslicemin();
363  d3_array* pti = t + hsl;
364  for (int i = hsl; i <= hsu; ++i)
365  {
366  pti->allocate(sl, sh, nrl, nrh, ncl, nch);
367  ++pti;
368  }
369 }
384  int hsl, int hsu,
385  int sl, int sh,
386  int nrl, int nrh,
387  const ivector& ncl, const ivector& nch)
388 {
389  if ((shape = new four_array_shape(hsl, hsu)) == 0)
390  {
391  cerr << " Error: d4_array unable to allocate memory in "
392  << __FILE__ << ':' << __LINE__ << '\n';
393  ad_exit(1);
394  }
395  if ((t = new d3_array[hslicesize()]) == 0)
396  {
397  cerr << " Error: d4_array unable to allocate memory in "
398  << __FILE__ << ':' << __LINE__ << '\n';
399  ad_exit(1);
400  }
401  t -= hslicemin();
402  d3_array* pti = t + hsl;
403  for (int i = hsl; i <= hsu; ++i)
404  {
405  pti->allocate(sl, sh, nrl, nrh, ncl, nch);
406  ++pti;
407  }
408 }
423  int hsl, int hsu,
424  int sl, int sh,
425  const ivector& nrl, const ivector& nrh,
426  const ivector& ncl, const ivector& nch)
427 {
428  if ( (shape = new four_array_shape(hsl, hsu)) == 0)
429  {
430  cerr << " Error: d4_array unable to allocate memory in "
431  << __FILE__ << ':' << __LINE__ << '\n';
432  ad_exit(1);
433  }
434  if ( (t = new d3_array[hslicesize()]) == 0)
435  {
436  cerr << " Error: d4_array unable to allocate memory in "
437  << __FILE__ << ':' << __LINE__ << '\n';
438  ad_exit(1);
439  }
440  t -= hslicemin();
441  d3_array* pti = t + hsl;
442  const int* pnrli = nrl.get_v() + hsl;
443  const int* pnrhi = nrh.get_v() + hsl;
444  const int* pncli = ncl.get_v() + hsl;
445  const int* pnchi = nch.get_v() + hsl;
446  for (int i = hsl; i <= hsu; ++i)
447  {
448  pti->allocate(sl, sh, *pnrli, *pnrhi, *pncli, *pnchi);
449  ++pti;
450  ++pnrli;
451  ++pnrhi;
452  ++pncli;
453  ++pnchi;
454  }
455 }
460 d4_array::d4_array(int hsl, int hsu, int sl, const ivector& sh,
461  int nrl, const imatrix& nrh, int ncl, int nch)
462 {
463  allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
464 }
465 
480  int hsl, int hsu,
481  int sl, const ivector& sh,
482  int nrl, const imatrix& nrh,
483  int ncl, int nch)
484 {
485  if ((shape = new four_array_shape(hsl, hsu)) == 0)
486  {
487  cerr << " Error: d4_array unable to allocate memory in "
488  << __FILE__ << ':' << __LINE__ << '\n';
489  ad_exit(1);
490  }
491  if ((t = new d3_array[hslicesize()]) == 0)
492  {
493  cerr << " Error: d4_array unable to allocate memory in "
494  << __FILE__ << ':' << __LINE__ << '\n';
495  ad_exit(1);
496  }
497  t -= hslicemin();
498  d3_array* pti = t + hsl;
499  const int* pshi = sh.get_v() + hsl;
500  const ivector* pnrhi = &nrh(hsl);
501  for (int i = hsl; i <= hsu; ++i)
502  {
503  pti->allocate(sl, *pshi, nrl, *pnrhi, ncl, nch);
504  ++pti;
505  ++pshi;
506  ++pnrhi;
507  }
508 }
509 
514  d4_array::d4_array(int hsl,int hsu,int sl,int sh,int nrl,
515  int nrh,int ncl,int nch)
516  {
517  allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
518  }
519 
524 d4_array::d4_array(int hsl,int hsu, int sl,int sh,ivector nrl,ivector nrh,
525  ivector ncl,ivector nch)
526 {
527  allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
528 }
529 
535 {
536  if (!(!(*this))) // only initialize allocated objects
537  {
538  int min = hslicemin();
539  int max = hslicemax();
540  d3_array* pti = t + min;
541  for (int i = min; i <= max; ++i)
542  {
543  pti->initialize();
544  ++pti;
545  }
546  }
547 }
548 
553 d4_array::d4_array(int hsl, int hsu, int sl, const ivector& sh, int nrl,
554  const imatrix& nrh, int ncl, const i3_array& nch)
555 {
556  allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
557 }
558 
564  int hsl, int hsu,
565  int sl, const ivector& sh,
566  int nrl, const imatrix& nrh,
567  int ncl, const i3_array& nch)
568 {
569  if ((shape = new four_array_shape(hsl, hsu)) == 0)
570  {
571  cerr << " Error: d4_array unable to allocate memory in "
572  << __FILE__ << ':' << __LINE__ << '\n';
573  ad_exit(1);
574  }
575  if ((t = new d3_array[hslicesize()]) == 0)
576  {
577  cerr << " Error: d4_array unable to allocate memory in "
578  << __FILE__ << ':' << __LINE__ << '\n';
579  ad_exit(1);
580  }
581  t -= hslicemin();
582  d3_array* pti = t + hsl;
583  const int* pshi = sh.get_v() + hsl;
584  const ivector* pnrhi = &nrh(hsl);
585  const imatrix* pnchi = &nch(hsl);
586  for (int i = hsl; i <= hsu; ++i)
587  {
588  pti->allocate(sl, *pshi, nrl, *pnrhi, ncl, *pnchi);
589 
590  ++pti;
591  ++pshi;
592  ++pnchi;
593  }
594 }
595 
600 d4_array::d4_array(int hsl,int hsu,const index_type& sl,
601  const index_type& sh,const index_type& nrl,const index_type& nrh,
602  const index_type& ncl,const index_type& nch)
603 {
604  allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
605 }
606 
621  ad_integer hsl, ad_integer hsu,
622  const index_type& sl, const index_type& sh,
623  const index_type& nrl, const index_type& nrh,
624  const index_type& ncl, const index_type& nch)
625 {
626  unsigned int ss =
627  static_cast<unsigned int>(hsu < hsl ? 0 : hsu - hsl + 1);
628  if (ss > 0)
629  {
630  if ( (t = new d3_array[ss]) == 0)
631  {
632  cerr << " Error: d4_array unable to allocate memory in "
633  << __FILE__ << ':' << __LINE__ << '\n';
634  ad_exit(1);
635  }
636  if ( (shape=new four_array_shape(hsl,hsu)) == 0)
637  {
638  cerr << " Error: d4_array unable to allocate memory in "
639  << __FILE__ << ':' << __LINE__ << '\n';
640  ad_exit(1);
641  }
642  t -= int(hsl);
643  d3_array* pti = t + hsl;
644  for (int i=hsl; i<=hsu; i++)
645  {
646  const index_type& rsl=sl[i];
647  const index_type& rsh=sh[i];
648  const index_type& rnrl=nrl[i];
649  const index_type& rnrh=nrh[i];
650  const index_type& rncl=ncl[i];
651  const index_type& rnch=nch[i];
652  const ad_integer& aa=ad_integer(rsl);
653  const ad_integer& bb=ad_integer(rsh);
654  pti->allocate(aa,bb,rnrl,rnrh,rncl,rnch);
655  ++pti;
656  //(*this)(i).allocate(sl[i],sh[i],nrl[i],nrh[i],ncl[i],nch[i]);
657  }
658  }
659  else
660  {
661  allocate();
662  }
663 }
678  int hsl, int hsu,
679  const index_type& sl, const index_type& sh,
680  const index_type& nrl, const index_type& nrh,
681  const index_type& ncl,const index_type& nch)
682 {
683  unsigned int ss =
684  static_cast<unsigned int>(hsu < hsl ? 0 : hsu - hsl + 1);
685  if (ss > 0)
686  {
687  if ((t = new d3_array[ss]) == 0)
688  {
689  cerr << " Error: d4_array unable to allocate memory in "
690  << __FILE__ << ':' << __LINE__ << '\n';
691  }
692  if ((shape = new four_array_shape(hsl, hsu)) == 0)
693  {
694  cerr << " Error: d4_array unable to allocate memory in "
695  << __FILE__ << ':' << __LINE__ << '\n';
696  }
697  t -= hsl;
698  d3_array* pti = t + hsl;
699  for (int i=hsl; i<=hsu; i++)
700  {
701  const index_type& rsl=sl[i];
702  const index_type& rsh=sh[i];
703  const index_type& rnrl=nrl[i];
704  const index_type& rnrh=nrh[i];
705  const index_type& rncl=ncl[i];
706  const index_type& rnch=nch[i];
707  const ad_integer& aa=ad_integer(rsl);
708  const ad_integer& bb=ad_integer(rsh);
709  pti->allocate(aa,bb,rnrl,rnrh,rncl,rnch);
710  ++pti;
711  //(*this)(i).allocate(sl[i],sh[i],nrl[i],nrh[i],ncl[i],nch[i]);
712  }
713  }
714  else
715  {
716  allocate();
717  }
718 }
731  ad_integer hsl, ad_integer hsu,
732  const index_type& sl, const index_type& sh,
733  const index_type& nrl, const index_type& nrh)
734 {
735  unsigned int ss =
736  static_cast<unsigned int>(hsu < hsl ? 0 : hsu - hsl + 1);
737  if (ss > 0)
738  {
739  if ((t = new d3_array[ss]) == 0)
740  {
741  cerr << " Error: d4_array unable to allocate memory in "
742  << __FILE__ << ':' << __LINE__ << '\n';
743  }
744  if ((shape=new four_array_shape(hsl, hsu)) == 0)
745  {
746  cerr << " Error: d4_array unable to allocate memory in "
747  << __FILE__ << ':' << __LINE__ << '\n';
748  }
749  t -= int(hsl);
750  d3_array* pti = t + hsl;
751  for (int i = hsl; i <= hsu; ++i)
752  {
753  const index_type& rsl = sl[i];
754  const index_type& rsh = sh[i];
755  const index_type& rnrl = nrl[i];
756  const index_type& rnrh = nrh[i];
757  const ad_integer& aa = ad_integer(rsl);
758  const ad_integer& bb = ad_integer(rsh);
759  pti->allocate(aa, bb, rnrl, rnrh);
760  ++pti;
761  }
762  }
763  else
764  {
765  allocate();
766  }
767 }
768 
779  ad_integer hsl,ad_integer hsu,
780  const index_type& sl, const index_type& sh)
781 {
782  unsigned int ss =
783  static_cast<unsigned int>(hsu < hsl ? 0 : hsu - hsl + 1);
784  if (ss > 0)
785  {
786  if ( (t = new d3_array[ss]) == 0)
787  {
788  cerr << " Error: d4_array unable to allocate memory in "
789  << __FILE__ << ':' << __LINE__ << '\n';
790  ad_exit(21);
791  }
792  if ( (shape=new four_array_shape(hsl,hsu)) == 0)
793  {
794  cerr << " Error: d4_array unable to allocate memory in "
795  << __FILE__ << ':' << __LINE__ << '\n';
796  ad_exit(21);
797  }
798  t -= int(hsl);
799  d3_array* pti = t + hsl;
800  for (int i=hsl; i<=hsu; i++)
801  {
802  const index_type& rsl=sl[i];
803  const index_type& rsh=sh[i];
804  const ad_integer& aa=ad_integer(rsl);
805  const ad_integer& bb=ad_integer(rsh);
806  pti->allocate(aa,bb);
807  ++pti;
808  }
809  }
810  else
811  {
812  allocate();
813  }
814 }
823 {
824  if (hsl > hsu)
825  {
826  allocate();
827  }
828  else
829  {
830  unsigned int size = static_cast<unsigned int>(hsu - hsl + 1);
831  if ((t = new d3_array[size]) == 0)
832  {
833  cerr << " Error: d4_array unable to allocate memory in "
834  << __FILE__ << ':' << __LINE__ << '\n';
835  ad_exit(1);
836  }
837  if ((shape = new four_array_shape(hsl, hsu)) == 0)
838  {
839  cerr << " Error: d4_array unable to allocate memory in "
840  << __FILE__ << ':' << __LINE__ << '\n';
841  ad_exit(1);
842  }
843  t -= static_cast<int>(hsl);
844  d3_array* pti = t + hsl;
845  for (int i = hsl; i <= hsu; ++i)
846  {
847  pti->allocate();
848  ++pti;
849  }
850  }
851 }
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: imatrix.h:69
const d3_array & operator()(int i) const
Definition: fvar.hpp:5401
int allocated(const ivector &v)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: fvar_a59.cpp:13
Vector of double precision numbers.
Definition: dvector.h:50
void allocate(const ad_integer &sl, const ad_integer &sh, const index_type &nrl, const index_type &nrh, const index_type &ncl, const index_type &nch)
Allocate array of matrices with dimensions [sl to sh] x [nrl to nrh] x [ncl to nch].
Definition: indextyp.cpp:327
void allocate(void)
Does not allocate, but initializes pointers to NULL.
Definition: d4arr3.cpp:14
double sum(const d3_array &darray)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr.cpp:21
four_array_shape(int hsl, int hsu)
Construct four_array_shape with initial values and setting min and max.
Definition: d4arr.cpp:26
int hslicemax() const
Definition: fvar.hpp:5317
exitptr ad_exit
Definition: gradstrc.cpp:53
unsigned int hslicesize() const
Definition: fvar.hpp:5356
void shallow_copy(const d4_array &)
Copies pointer locations from other to d4_array.
Definition: d4arr.cpp:90
d3_array * t
Definition: fvar.hpp:5164
int j
Definition: fvar.hpp:3636
Description not yet available.
Definition: fvar.hpp:5161
int * get_v() const
Definition: ivector.h:114
prnstream & endl(prnstream &)
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
const d3_array & operator[](int i) const
Definition: fvar.hpp:5405
#define min(a, b)
Definition: cbivnorm.cpp:188
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
~d4_array()
Destructor.
Definition: d4arr.cpp:132
Description not yet available.
Definition: fvar.hpp:2819
friend class four_array_shape
Definition: fvar.hpp:5264
four_array_shape * shape
Definition: fvar.hpp:5163
#define ADMB_ARRAY_BOUNDS_ERROR(message, function, lower_bounds, upper_bounds, index)
Definition: admb_messages.h:47
int indexmin() const
Definition: fvar.hpp:5305
void initialize()
Description not yet available.
Definition: d4arr.cpp:534
void initialize(int sl, int sh, int nrl, const ivector &nrh, int ncl, const ivector &nch)
void deallocate(void)
Deallocates d4_array memory.
Definition: d4arr.cpp:107
int indexmax() const
Definition: fvar.hpp:5309
d4_array & operator=(const d4_array &)
Assigns element values from other to d4_array.
Definition: d4arr.cpp:141
d4_array sub(int, int)
Description not yet available.
Definition: d4arr.cpp:64
unsigned int ncopies
Definition: fvar.hpp:5140
Stores integer.
Definition: fvar.hpp:7654
Description not yet available.
Definition: fvar.hpp:3944
#define max(a, b)
Definition: cbivnorm.cpp:189
int hslicemin() const
Definition: fvar.hpp:5313
Description not yet available.
Definition: fvar.hpp:3727
d4_array()
Default constructor.
Definition: d4arr3.cpp:9