ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
d5arr.cpp
Go to the documentation of this file.
1 
5 #if (__cplusplus > 199711L)
6  #include <algorithm>
7  #include <iterator>
8 #endif
9 #include "fvar.hpp"
10 #include "admb_messages.h"
11 
17 double sum(const d5_array& darray)
18 {
19  double total = 0.0;
20 
21 #if (__cplusplus <= 199711L)
22  for (int i = darray.indexmin(); i <= darray.indexmax(); ++i)
23  {
24  total += sum(darray.elem(i));
25  }
26 #else
27  if (!darray == false)
28  {
29  auto begin = &darray.elem(darray.indexmin());
30  auto end = begin + darray.size();
31  std::for_each(begin, end, [&total](const d4_array& darray)
32  {
33  total += sum(darray);
34  });
35  }
36 #endif
37 
38  return total;
39 }
42 {
43  shallow_copy(other);
44 }
50 void d5_array::shallow_copy(const d5_array& other)
51 {
52  if (other.shape)
53  {
54  shape = other.shape;
55  ++(shape->ncopies);
56  t = other.t;
57  }
58  else
59  {
60 #ifdef DEBUG
61  cerr << "Warning -- Unable to shallow copy an unallocated d5_array.\n";
62 #endif
63  allocate();
64  }
65 }
68 {
69  if (shape)
70  {
71  if (shape->ncopies)
72  {
73  --(shape->ncopies);
74  }
75  else
76  {
77  t += indexmin();
78  delete [] t;
79  delete shape;
80  }
81  allocate();
82  }
83 #if defined(DIAG)
84  else
85  {
86  cerr << "Warning -- Unable to deallocate an unallocated d5_array.\n";
87  }
88 #endif
89 }
92 {
93  deallocate();
94 }
101 {
102  int min = indexmin();
103  int max = indexmax();
104  if (min != other.indexmin() || max != other.indexmax())
105  {
106  cerr << "Incompatible bounds in"
107  << " d5_array& d5_array::operator=(const d5_array&).\n";
108  ad_exit(1);
109  }
110  for (int i = min; i <= max; ++i)
111  {
112  elem(i) = other.elem(i);
113  }
114  return *this;
115 }
118 {
119 #if (__cplusplus <= 199711L)
120  for (int i = indexmin(); i <= indexmax(); ++i)
121  {
122  elem(i).initialize();
123  }
124 #else
125  if (operator!() == false)
126  {
127  auto begin = &elem(indexmin());
128  auto end = begin + size();
129  std::for_each(begin, end, [](d4_array& darray)
130  {
131  darray.initialize();
132  });
133  }
134 #endif
135 }
141 void d5_array::allocate(const d5_array& other)
142 {
143  allocate(other.indexmin(), other.indexmax());
144  for (int i = indexmin(); i <= indexmax(); ++i)
145  {
146  elem(i).allocate(other.elem(i));
147  }
148 }
149 
150 #ifndef OPT_LIB
151 
156  d4_array& d5_array::operator ( ) (int i)
157  {
158  if (i < indexmin() || i > indexmax())
159  {
160  ADMB_ARRAY_BOUNDS_ERROR("index out of bounds",
161  "d4_array& d5_array::operator()(int i)", indexmin(), indexmax(), i);
162  }
163  //return t[i];
164  return elem(i);
165  }
166 
172  {
173  if (i < indexmin() || i > indexmax())
174  {
175  ADMB_ARRAY_BOUNDS_ERROR("index out of bounds",
176  "d4_array& d5_array::operator[](int i)", indexmin(), indexmax(), i);
177  }
178  return t[i];
179  }
180 
185  d3_array& d5_array::operator ( ) (int i, int j)
186  {
187  if (i < indexmin() || i > indexmax())
188  {
189  ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
190  "d3_array& d5_array::operator[](int i, int j)",
191  indexmin(), indexmax(), i);
192  }
193  return elem(i)(j);
194  }
195 
200  dmatrix& d5_array::operator ( ) (int i,int j,int k)
201  {
202  if (i < indexmin() || i > indexmax())
203  {
204  ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
205  "dmatrix& d5_array::operator[](int i, int j, int k)",
206  indexmin(), indexmax(), i);
207  }
208  return elem(i)(j,k);
209  }
210 
215  dvector& d5_array::operator ( ) (int i,int j,int k,int l)
216  {
217  if (i < indexmin() || i > indexmax())
218  {
219  ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
220  "dvector& d5_array::operator[](int i, int j, int k, int l)",
221  indexmin(), indexmax(), i);
222  }
223  return elem(i)(j,k,l);
224  }
225 
230  double& d5_array::operator ( ) (int i,int j,int k,int l,int m)
231  {
232  if (i < indexmin() || i > indexmax())
233  {
234  ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
235  "double& d5_array::operator[](int i, int j, int k, int l, int m)",
236  indexmin(), indexmax(), i);
237  }
238  return elem(i)(j,k,l,m);
239  }
240 
245 const d4_array& d5_array::operator()(int i) const
246  {
247  if (i<indexmin()||i>indexmax())
248  { cerr << "Error index out of bounds in\n"
249  "d4_array& d5_array::operator ( )" << endl;
250  ad_exit(1);
251  }
252  return t[i];
253  }
254 
259 const d4_array& d5_array::operator[](int i) const
260  {
261  if (i<indexmin()||i>indexmax())
262  { cerr << "Error index out of bounds in\n"
263  "d4_array& d5_array::operator []" << endl;
264  ad_exit(1);
265  }
266  return t[i];
267  }
268 
273 const d3_array& d5_array::operator()(int i, int j) const
274  {
275  if (i<indexmin()||i>indexmax())
276  { cerr << "Error index out of bounds in\n"
277  "d3_array& d5_array::operator ( )" << endl;
278  ad_exit(1);
279  }
280  return elem(i).elem(j);
281  }
282 
287 const dmatrix& d5_array::operator()(int i, int j, int k) const
288  {
289  if (i<indexmin()||i>indexmax())
290  { cerr << "Error index out of bounds in\n"
291  "d3_array& d5_array::operator ( )" << endl;
292  ad_exit(1);
293  }
294  return elem(i)(j,k);
295  }
296 
301 const dvector& d5_array::operator()(int i, int j, int k, int l) const
302  {
303  if (i<indexmin()||i>indexmax())
304  { cerr << "Error hslice index out of bounds in\n"
305  "dvector& d5_array::operator ( )" << endl;
306  ad_exit(1);
307  }
308  return elem(i)(j,k,l);
309  }
310 
315 const double& d5_array::operator()(int i, int j, int k, int l, int m) const
316  {
317  if (i<indexmin()||i>indexmax())
318  { cerr << "Error hslice index out of bounds in\n"
319  "dvector& d5_array::operator ( )" << endl;
320  ad_exit(1);
321  }
322  return elem(i)(j,k,l,m);
323  }
324 
325 #endif
326 
331 d5_array::d5_array(int hsl,int hsu,int sl,int sh,int nrl,
332  int nrh,int ncl,int nch,int l5,int u5)
333 {
334  allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch,l5,u5);
335 }
336 
342  const index_type& sl,const index_type& sh,const index_type& nrl,
343  const index_type& nrh,const index_type& ncl,const index_type& nch,
344  const index_type& l5,const index_type& u5)
345 {
346  allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch,l5,u5);
347 }
348 
352 void d5_array::allocate(int hsl,int hsu,int sl,int sh,int nrl,
353  int nrh,int ncl,int nch,int l5,int u5)
354 {
355  if ((shape = new vector_shape(hsl, hsu)) == 0)
356  {
357  cerr << " Error allocating memory in d5_array contructor\n";
358  ad_exit(21);
359  }
360  unsigned int ss = size();
361  if ((t = new d4_array[ss]) == 0)
362  {
363  cerr << " Error allocating memory in d5_array contructor\n";
364  ad_exit(21);
365  }
366  t -= indexmin();
367  for (int i = hsl; i <= hsu; ++i)
368  {
369  t[i].allocate(sl, sh, nrl, nrh, ncl, nch, l5, u5);
370  }
371 }
378 d5_array::d5_array(int hsl,int hsu)
379 {
380  allocate(hsl,hsu);
381 }
388 void d5_array::allocate(int hsl, int hsu)
389 {
390  if ( (shape=new vector_shape(hsl,hsu)) == 0)
391  {
392  cerr << " Error allocating memory in d5_array contructor\n";
393  ad_exit(21);
394  }
395  unsigned int ss = size();
396  if ( (t = new d4_array[ss]) == 0)
397  {
398  cerr << " Error allocating memory in d5_array contructor\n";
399  ad_exit(21);
400  }
401  t -= indexmin();
402 }
403 
407 void d5_array::allocate(const ad_integer& hsl,const ad_integer& hsu,
408  const index_type& sl,const index_type& sh,const index_type& nrl,
409  const index_type& nrh,const index_type& ncl,const index_type& nch,
410  const index_type& l5,const index_type& u5)
411 {
412  if ((shape = new vector_shape (hsl,hsu)) == 0)
413  {
414  cerr << " Error allocating memory in d5_array contructor\n";
415  }
416  unsigned int ss = size();
417  if ((t = new d4_array[ss]) == 0)
418  {
419  cerr << " Error allocating memory in d5_array contructor\n";
420  ad_exit(21);
421  }
422  t -= indexmin();
423  int il = hsl;
424  int iu = hsu;
425  for (int i = il; i <= iu; ++i)
426  {
427  (*this)(i).allocate(ad_integer(sl(i)),ad_integer(sh(i)),nrl(i),nrh(i),
428  ncl(i),nch(i), l5(i),u5(i));
429  }
430 }
Uses polymorphism to get index information from various data types to be used in constructing and all...
Definition: fvar.hpp:7731
void allocate(int hsl, int hsu, int sl, int sh, int nrl, int nrh, int ncl, int nch)
Allocate arrays with dimension [hsl to hsu] x [sl to sh] x [nrl to nrh] x [ncl to nch]...
Definition: d4arr.cpp:344
unsigned int ncopies
Definition: fvar.hpp:522
Description not yet available.
Definition: fvar.hpp:509
vector_shape * shape
Definition: fvar.hpp:6333
d4_array * t
Definition: fvar.hpp:6334
Vector of double precision numbers.
Definition: dvector.h:50
double sum(const d3_array &darray)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr.cpp:21
exitptr ad_exit
Definition: gradstrc.cpp:53
d5_array & operator=(const d5_array &)
Assigns element values from other to d5_array.
Definition: d5arr.cpp:100
int j
Definition: fvar.hpp:3636
Description not yet available.
Definition: fvar.hpp:5161
prnstream & endl(prnstream &)
#define min(a, b)
Definition: cbivnorm.cpp:188
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
d5_array()
Description not yet available.
Definition: d5arr3.cpp:17
Description not yet available.
Definition: fvar.hpp:2819
d4_array & elem(int i)
Definition: fvar.hpp:6371
#define ADMB_ARRAY_BOUNDS_ERROR(message, function, lower_bounds, upper_bounds, index)
Definition: admb_messages.h:47
void deallocate(void)
Deallocate d5_array memory.
Definition: d5arr.cpp:67
int indexmin() const
Definition: fvar.hpp:6475
void shallow_copy(const d5_array &)
Shallow copy pointers from other data structure.
Definition: d5arr.cpp:50
void initialize()
Description not yet available.
Definition: d4arr.cpp:534
d4_array & operator[](int i)
Definition: fvar.hpp:6416
void allocate(void)
Description not yet available.
Definition: d5arr3.cpp:26
d3_array & elem(int i)
Definition: fvar.hpp:5220
int indexmax() const
Definition: fvar.hpp:6479
void initialize()
Initialize all elements of d5_array to zero.
Definition: d5arr.cpp:117
Description not yet available.
Definition: fvar.hpp:6331
d4_array & operator()(int i)
Definition: fvar.hpp:6412
Stores integer.
Definition: fvar.hpp:7654
#define max(a, b)
Definition: cbivnorm.cpp:189
Description not yet available.
Definition: fvar.hpp:3727
~d5_array()
Destructor.
Definition: d5arr.cpp:91
unsigned int size() const
Definition: fvar.hpp:6483