ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
f3arr.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 #include "fvar.hpp"
12 
18  {
19  if (!(!(*this))) // only initialize allocated objects
20  {
21  int min = indexmin();
22  int max = indexmax();
23  dvar_matrix* pti = t + min;
24  for (int i = min; i <= max; ++i)
25  {
26  pti->initialize();
27 
28  ++pti;
29  }
30  }
31  }
32 
37  dvar3_array dvar3_array::sub(int nrl,int nrh)
38  {
39  if (allocated(*this))
40  {
41  dvar3_array tmp(nrl,nrh);
42  for (int i=nrl; i<=nrh; i++)
43  {
44  tmp[i].shallow_copy((*this)(i));
45  }
46  return tmp;
47  }
48  else
49  {
50  return *this;
51  }
52  }
53 
58  dvar3_array::dvar3_array (int nrl,int nrh)
59  {
60  allocate(nrl,nrh);
61  }
62 
67  dvar3_array::dvar3_array(int sl,int sh,int nrl,int nrh,int ncl,int nch)
68  {
69  if (sh<sl)
70  {
71  allocate();
72  return;
73  }
74  allocate(sl,sh,nrl,nrh,ncl,nch);
75 #ifndef OPT_LIB
76  initialize();
77 #endif
78  }
79 
91 void dvar3_array::allocate(int sl,int sh,int nrl,int nrh,int ncl,int nch)
92 {
93  if (sh < sl)
94  {
95  allocate();
96  return;
97  }
98 #ifdef DIAG
99  myheapcheck("Entering dvar3_array matrix(sl,sh,nrl,nrh,ncl,nch)" );
100 #endif
101  if ((shape = new three_array_shape(sl, sh)) == 0)
102  {
103  cerr << " Error allocating memory in dvar3_array contructor" << endl;
104  }
105  if ((t = new dvar_matrix[slicesize()]) == 0)
106  {
107  cerr << " Error allocating memory in dvar3_array contructor" << endl;
108  ad_exit(21);
109  }
110  t -= slicemin();
111 
112  dvar_matrix* pti = t + sl;
113  for (int i = sl; i <= sh; ++i)
114  {
115  pti->allocate(nrl, nrh, ncl, nch);
116  ++pti;
117  }
118 }
119 
128 void dvar3_array::allocate(int sl,int sh,int nrl,int nrh)
129 {
130  if (sh < sl)
131  {
132  allocate();
133  return;
134  }
135 #ifdef DIAG
136  myheapcheck("Entering dvar3_array matrix(sl,sh,nrl,nrh,ncl,nch)" );
137 #endif
138  if ((shape = new three_array_shape(sl, sh)) == 0)
139  {
140  cerr << " Error allocating memory in dvar3_array constructor" << endl;
141  }
142  if ( (t = new dvar_matrix[slicesize()]) == 0)
143  {
144  cerr << " Error allocating memory in dvar3_array constructor" << endl;
145  ad_exit(21);
146  }
147  t -= slicemin();
148 
149  dvar_matrix* pti = t + sl;
150  for (int i = sl; i <= sh; ++i)
151  {
152  pti->allocate(nrl, nrh);
153  ++pti;
154  }
155 }
156 
161 void dvar3_array::allocate(int sl,int sh,
162  const index_type& nrl, const index_type& nrh)
163 {
164  if (sh < sl)
165  {
166  allocate();
167  return;
168  }
169 #ifdef DIAG
170  myheapcheck("Entering dvar3_array matrix(sl,sh,nrl,nrh,ncl,nch)" );
171 #endif
172  if ((shape = new three_array_shape(sl, sh)) == 0)
173  {
174  cerr << " Error allocating memory in dvar3_array contructor" << endl;
175  }
176  if ( (t = new dvar_matrix[slicesize()]) == 0)
177  {
178  cerr << " Error allocating memory in dvar3_array contructor" << endl;
179  ad_exit(21);
180  }
181  t -= slicemin();
182  dvar_matrix* pti = t + sl;
183  for (int i=sl; i<=sh; i++)
184  {
185  pti->allocate(nrl(i),nrh(i));
186  ++pti;
187  }
188 }
189 
195 void dvar3_array::allocate(int sl,int sh)
196 {
197  if (sh < sl)
198  {
199  allocate();
200  return;
201  }
202 #ifdef DIAG
203  myheapcheck("Entering dvar3_array matrix(sl,sh,nrl,nrh,ncl,nch)" );
204 #endif
205  if ((shape = new three_array_shape(sl, sh)) == 0)
206  {
207  cerr << " Error allocating memory in dvar3_array contructor" << endl;
208  }
209  if ((t = new dvar_matrix[slicesize()]) == 0)
210  {
211  cerr << " Error allocating memory in dvar3_array contructor" << endl;
212  ad_exit(21);
213  }
214  t -= slicemin();
215  dvar_matrix* pti = t + sl;
216  for (int i = sl; i <= sh; ++i)
217  {
218  pti->allocate();
219  ++pti;
220  }
221 }
222 
227 dvar3_array::dvar3_array(int sl, int sh, int nrl, int nrh,
228  const ivector& ncl, int nch)
229  {
230  if (sh<sl)
231  {
232  allocate();
233  return;
234  }
235  allocate(sl,sh,nrl,nrh,ncl,nch);
236 #ifndef OPT_LIB
237  initialize();
238 #endif
239  }
240 
252 void dvar3_array::allocate(int sl, int sh, int nrl, int nrh,
253  const ivector& ncl, int nch)
254 {
255  if (sh < sl)
256  {
257  allocate();
258  return;
259  }
260 #ifdef DIAG
261  myheapcheck("Entering dvar3_array matrix(sl,sh,nrl,nrh,ncl,nch)" );
262 #endif
263  if ((shape=new three_array_shape(sl,sh)) == 0)
264  {
265  cerr << " Error allocating memory in dvar3_array contructor" << endl;
266  }
267  if ((t = new dvar_matrix[slicesize()]) == 0)
268  {
269  cerr << " Error allocating memory in dvar3_array contructor" << endl;
270  ad_exit(21);
271  }
272  t -= slicemin();
273  dvar_matrix* pti = t + sl;
274  int* pncli = ncl.get_v() + sl;
275  for (int i = sl; i <= sh; ++i)
276  {
277  pti->allocate(nrl, nrh, *pncli, nch);
278  ++pti;
279  ++pncli;
280  }
281 }
282 
294 void dvar3_array::allocate(int sl, int sh, int nrl, int nrh,
295  const ivector& ncl, const ivector& nch)
296 {
297  if (sh<sl)
298  {
299  allocate();
300  return;
301  }
302 #ifdef DIAG
303  myheapcheck("Entering dvar3_array matrix(sl,sh,nrl,nrh,ncl,nch)" );
304 #endif
305  if (sl !=ncl.indexmin() || sh !=ncl.indexmax()
306  || sl !=nch.indexmin() || sh !=nch.indexmax())
307  {
308  cerr << "Incompatible array bounds in "
309  "dmatrix(int nrl,int nrh, const ivector& ncl, const ivector& nch)" << endl;
310  ad_exit(1);
311  }
312  if ((shape = new three_array_shape(sl, sh)) == 0)
313  {
314  cerr << " Error allocating memory in dvar3_array contructor" << endl;
315  }
316  if ( (t = new dvar_matrix[slicesize()]) == 0)
317  {
318  cerr << " Error allocating memory in dvar3_array contructor" << endl;
319  ad_exit(21);
320  }
321  t -= slicemin();
322  dvar_matrix* pti = t + sl;
323  const int* pncli = ncl.get_v() + sl;
324  const int* pnchi = nch.get_v() + sl;
325  for (int i = sl; i <= sh; ++i)
326  {
327  pti->allocate(nrl, nrh, ncl(i), nch(i));
328  ++pti;
329  ++pncli;
330  ++pnchi;
331  }
332 }
344 void dvar3_array::allocate(int sl, int sh, int nrl, int nrh,
345  int ncl, const ivector& nch)
346 {
347  if (sh<sl)
348  {
349  allocate();
350  return;
351  }
352 #ifdef DIAG
353  myheapcheck("Entering dvar3_array matrix(sl,sh,nrl,nrh,ncl,nch)" );
354 #endif
355  if (sl !=nch.indexmin() || sh !=nch.indexmax())
356  {
357  cerr << "Incompatible array bounds in "
358  "dmatrix(int nrl,int nrh,int ncl, const ivector& nch)" << endl;
359  ad_exit(1);
360  }
361  if ((shape=new three_array_shape(sl,sh)) == 0)
362  {
363  cerr << " Error allocating memory in dvar3_array contructor" << endl;
364  }
365  if ((t = new dvar_matrix[slicesize()]) == 0)
366  {
367  cerr << " Error allocating memory in dvar3_array contructor" << endl;
368  ad_exit(21);
369  }
370  t -= slicemin();
371  dvar_matrix* pti = t + sl;
372  const int* pnchi = nch.get_v() + sl;
373  for (int i = sl; i <= sh; ++i)
374  {
375  pti->allocate(nrl, nrh, ncl, *pnchi);
376  ++pti;
377  ++pnchi;
378  }
379 }
380 
386  {
387  allocate(m1);
388  int min = slicemin();
389  int max = slicemax();
390  dvar_matrix* pti = t + min;
391  const dmatrix* pm1i = &m1(min);
392  for (int i = min; i <= max; ++i)
393  {
394  *pti = *pm1i;
395 
396  ++pti;
397  ++pm1i;
398  }
399  }
400 
405 {
406 #ifdef DIAG
407  myheapcheck("Entering dvar3_array matrix(const d3_array& m1)" );
408 #endif
409  if ((shape=new three_array_shape(m1.indexmin(),m1.indexmax())) == 0)
410  {
411  cerr << " Error allocating memory in dvar3_array contructor" << endl;
412  }
413  if ((t = new dvar_matrix[slicesize()]) == 0)
414  {
415  cerr << " Error allocating memory in dvar3_array contructor" << endl;
416  ad_exit(21);
417  }
418  int min = slicemin();
419  int max = slicemax();
420  t -= min;
421  dvar_matrix* pti = t + min;
422  const dmatrix* pm1i = &m1(min);
423  for (int i = min; i <= max; ++i)
424  {
425  pti->allocate(*pm1i);
426  ++pti;
427  ++pm1i;
428  }
429 }
434 {
435 #ifdef DIAG
436  myheapcheck("Entering dvar3_array matrix(const d3_array& m1)" );
437 #endif
438  if ((shape = new three_array_shape(m1.slicemin(), m1.slicemax())) == 0)
439  {
440  cerr << " Error allocating memory in dvar3_array contructor" << endl;
441  }
442  if ((t = new dvar_matrix[slicesize()]) == 0)
443  {
444  cerr << " Error allocating memory in dvar3_array contructor" << endl;
445  ad_exit(21);
446  }
447  int min = slicemin();
448  int max = slicemax();
449  t -= min;
450  dvar_matrix* pti = t + min;
451  const dvar_matrix* pm1i = &m1(min);
452  for (int i = min; i <= max; ++i)
453  {
454  pti->allocate(*pm1i);
455  ++pti;
456  ++pm1i;
457  }
458 }
459 
464 dvar3_array::dvar3_array(int sl, int sh, const ivector& nrl, const ivector& nrh,
465  ivector& ncl, const ivector& nch)
466  {
467  if (sh<sl)
468  {
469  allocate();
470  return;
471  }
472  allocate(sl,sh,nrl,nrh,ncl,nch);
473 #ifndef OPT_LIB
474  initialize();
475 #endif
476  }
477 
489 void dvar3_array::allocate(int sl, int sh, const ivector& nrl,
490  const ivector& nrh, const ivector& ncl, const ivector& nch)
491 {
492  if (sh < sl)
493  {
494  allocate();
495  return;
496  }
497 #ifdef DIAG
498  myheapcheck("Entering d3_array matrix(sl,sh,nrl,nrh,ncl,nch)" );
499 #endif
500  if ((shape = new three_array_shape(sl, sh)) == 0)
501  {
502  cerr << " Error allocating memory in dvar3_array contructor" << endl;
503  }
504  if ((t = new dvar_matrix[slicesize()]) == 0)
505  {
506  cerr << " Error allocating memory in dvar3_array contructor" << endl;
507  ad_exit(21);
508  }
509  t -= slicemin();
510  int* pnrli = nrl.get_v() + sl;
511  int* pnrhi = nrh.get_v() + sl;
512  int* pncli = ncl.get_v() + sl;
513  int* pnchi = nch.get_v() + sl;
514  dvar_matrix* pti = t + sl;
515  for (int i = sl; i <= sh; ++i)
516  {
517  pti->allocate(*pnrli, *pnrhi, *pncli, *pnchi);
518  ++pti;
519  ++pnrli;
520  ++pnrhi;
521  ++pncli;
522  ++pnchi;
523  }
524 }
525 
530 dvar3_array::dvar3_array(int sl, int sh, int nrl, const ivector& nrh,
531  int ncl, const ivector& nch)
532  {
533  if (sh<sl)
534  {
535  allocate();
536  return;
537  }
538  allocate(sl,sh,nrl,nrh,ncl,nch);
539 #ifndef OPT_LIB
540  initialize();
541 #endif
542  }
543 
555 void dvar3_array::allocate(int sl, int sh, int nrl, const ivector& nrh,
556  int ncl, const ivector& nch)
557 {
558  if (sh<sl)
559  {
560  allocate();
561  return;
562  }
563 #ifdef DIAG
564  myheapcheck("Entering d3_array matrix(sl,sh,nrl,nrh,ncl,nch)" );
565 #endif
566  if ((shape=new three_array_shape(sl,sh)) == 0)
567  {
568  cerr << " Error allocating memory in dvar3_array contructor" << endl;
569  }
570  if ((t = new dvar_matrix[slicesize()]) == 0)
571  {
572  cerr << " Error allocating memory in dvar3_array contructor" << endl;
573  ad_exit(21);
574  }
575  t -= slicemin();
576  int* pnrhi = nrh.get_v() + sl;
577  int* pnchi = nch.get_v() + sl;
578  dvar_matrix* pti = t + sl;
579  for (int i = sl; i <= sh; ++i)
580  {
581  pti->allocate(nrl, *pnrhi, ncl, *pnchi);
582  ++pti;
583  ++pnrhi;
584  ++pnchi;
585  }
586 }
587 
592 dvar3_array::dvar3_array(int sl, int sh, int nrl, const ivector& nrh,
593  int ncl,int nch)
594  {
595  if (sh<sl)
596  {
597  allocate();
598  return;
599  }
600  allocate(sl,sh,nrl,nrh,ncl,nch);
601 #ifndef OPT_LIB
602  initialize();
603 #endif
604  }
605 
617 void dvar3_array::allocate(int sl, int sh, int nrl, const ivector& nrh,
618  int ncl,int nch)
619 {
620 #ifdef DIAG
621  myheapcheck("Entering d3_array matrix(sl,sh,nrl,nrh,ncl,nch)" );
622 #endif
623  if ((shape = new three_array_shape(sl, sh)) == 0)
624  {
625  cerr << " Error allocating memory in dvar3_array contructor" << endl;
626  }
627  if ((t = new dvar_matrix[slicesize()]) == 0)
628  {
629  cerr << " Error allocating memory in dvar3_array contructor" << endl;
630  ad_exit(21);
631  }
632  t -= slicemin();
633  int* pnrhi = nrh.get_v() + sl;
634  dvar_matrix* pti = t + sl;
635  for (int i = sl; i <= sh; ++i)
636  {
637  pti->allocate(nrl, *pnrhi, ncl, nch);
638  ++pti;
639  ++pnrhi;
640  }
641 }
642 
645 {
646  shallow_copy(other);
647 }
654 {
655  if (other.shape)
656  {
657  shape = other.shape;
658  ++(shape->ncopies);
659  t = other.t;
660  }
661  else
662  {
663 #ifdef DEBUG
664  cerr << "Warning -- Unable to shallow copy an unallocated dvar3_array.\n";
665 #endif
666  allocate();
667  }
668 }
671 {
672  deallocate();
673 }
676 {
677  if (shape)
678  {
679  if (shape->ncopies > 0)
680  {
681  --(shape->ncopies);
682  }
683  else
684  {
685  t += indexmin();
686  delete [] t;
687  delete shape;
688  }
689  allocate();
690  }
691 #if defined(DIAG)
692  else
693  {
694  cerr << "Warning -- Unable to deallocate an unallocated dvar3_array.\n";
695  }
696 #endif
697 }
698 
710 void dvar3_array::allocate(int sl, int sh, const ivector& nrl,
711  const ivector& nrh, int ncl, int nch)
712 {
713  if (sl != nrl.indexmin() || sh != nrl.indexmax()
714  || sl != nrh.indexmin() || sh != nrh.indexmax())
715  {
716  cerr << "Incompatible array bounds in "
717  "dmatrix(int nrl,int nrh, const ivector& ncl, const ivector& nch)" << endl;
718  ad_exit(1);
719  }
720  if ( (shape=new three_array_shape(sl,sh)) == 0)
721  {
722  cerr << "Error allocating memory in dvar3_array contructor" << endl;
723  }
724  if ((t = new dvar_matrix[slicesize()]) == 0)
725  {
726  cerr << " Error allocating memory in dvar3_array contructor" << endl;
727  ad_exit(21);
728  }
729  t -= slicemin();
730  int* pnrli = nrl.get_v() + sl;
731  int* pnrhi = nrh.get_v() + sl;
732  dvar_matrix* pti = t + sl;
733  for (int i = sl; i <= sh; ++i)
734  {
735  pti->allocate(*pnrli, *pnrhi, ncl, nch);
736  ++pti;
737  ++pnrli;
738  ++pnrhi;
739  }
740 }
752 void dvar3_array::allocate(int sl, int sh, const ivector& nrl, int nrh,
753  int ncl, int nch)
754 {
755  if (sh < sl)
756  {
757  allocate();
758  return;
759  }
760  if (sl !=nrl.indexmin() || sh !=nrl.indexmax())
761  {
762  cerr << "Incompatible array bounds in "
763  "dmatrix(int nrl,int nrh, const ivector& ncl, const ivector& nch)" << endl;
764  ad_exit(1);
765  }
766  if ( (shape=new three_array_shape(sl,sh)) == 0)
767  {
768  cerr << " Error allocating memory in dvar3_array contructor" << endl;
769  }
770  if ( (t = new dvar_matrix[slicesize()]) == 0)
771  {
772  cerr << " Error allocating memory in dvar3_array contructor" << endl;
773  ad_exit(21);
774  }
775  t -= slicemin();
776  int* pnrli = nrl.get_v() + sl;
777  dvar_matrix* pti = t + sl;
778  for (int i = sl; i <= sh; ++i)
779  {
780  pti->allocate(*pnrli, nrh, ncl, nch);
781  ++pti;
782  ++pnrli;
783  }
784 }
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
int slicemin() const
Definition: fvar.hpp:4274
unsigned int ncopies
Definition: fvar.hpp:3701
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
void initialize(void)
Zero initialize allocated dvar_matrix, then saves adjoint function and position data.
Definition: fvar_ma7.cpp:48
void initialize(void)
Description not yet available.
Definition: f3arr.cpp:17
int slicemax() const
Definition: fvar.hpp:4275
Description not yet available.
Definition: fvar.hpp:3698
exitptr ad_exit
Definition: gradstrc.cpp:53
int indexmax() const
Definition: fvar.hpp:4273
dvar_matrix * t
Definition: fvar.hpp:4199
dvar3_array sub(int, int)
Description not yet available.
Definition: f3arr.cpp:37
dvar3_array(void)
Default constructor.
Definition: f3arr17.cpp:11
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
#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 indexmax() const
Definition: ivector.h:104
Description not yet available.
Definition: fvar.hpp:2819
Description not yet available.
Definition: fvar.hpp:4197
void allocate(void)
Does not allocte, but initialize class members.
Definition: f3arr17.cpp:18
void shallow_copy(const dvar3_array &)
Shallow copy other data structure pointers.
Definition: f3arr.cpp:653
Class definition of matrix with derivitive information .
Definition: fvar.hpp:2480
unsigned int slicesize() const
Definition: fvar.hpp:4277
three_array_shape * shape
Definition: fvar.hpp:4200
int indexmin() const
Definition: fvar.hpp:3818
void allocate(int nrl, int nrh, int ncl, int nch)
Allocates AD variable matrix with dimensions nrl to nrh by ncl to nch.
Definition: fvar_mat.cpp:216
#define max(a, b)
Definition: cbivnorm.cpp:189
Description not yet available.
Definition: fvar.hpp:3727
int indexmin() const
Definition: fvar.hpp:4272
~dvar3_array()
Destructor.
Definition: f3arr.cpp:670
void deallocate()
Deallocate dvar3_array memory.
Definition: f3arr.cpp:675