ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fvar_mat.cpp
Go to the documentation of this file.
1 
6 #include "fvar.hpp"
8 #ifndef OPT_LIB
9  #include <cassert>
10 #endif
11 
16 {
17  allocate();
18 }
25 {
26  if (!(other))
27  {
28  //cerr << "Making a copy of an unallocated dvar_matrix" << endl;
29  allocate();
30  }
31  else
32  {
33  index_min = other.index_min;
34  index_max = other.index_max;
35  shape = other.shape;
36  if (shape)
37  {
38  (shape->ncopies)++;
39  }
40  m = other.m;
41  }
42 }
47 {
48  index_min = other.index_min;
49  index_max = other.index_max;
50  if ((m = new dvar_vector[rowsize()]) == 0)
51  {
52  cerr << " Error allocating memory in dvar_matrix contructor"<<endl;
53  ad_exit(21);
54  }
55  if ( (shape =new mat_shapex(m)) == 0)
56  {
57  cerr << " Error allocating memory in dvar_matrix contructor"<<endl;
58  }
59  m -= index_min;
60  const dvector* pother = &other(index_min);
61  dvar_vector* pm = m + index_min;
62  for (int i = index_min; i <= index_max; ++i)
63  {
64  pm->allocate(pother->indexmin(), pother->indexmax());
65  *pm = *pother;
66  ++pother;
67  ++pm;
68  }
69 }
74 {
75  if (shape)
76  {
77  if (shape->ncopies)
78  {
79  (shape->ncopies)--;
80  }
81  else
82  {
83  deallocate();
84  }
85  }
86 }
87 
96 dvar_matrix::dvar_matrix(int nrl, int nrh, int ncl, int nch)
97 {
98  allocate(nrl,nrh,ncl,nch);
99 #ifndef OPT_LIB
100  initialize();
101 #endif
102 }
103 
109  {
110  allocate(nrl,nrh);
111 #ifndef OPT_LIB
112  initialize();
113 #endif
114  }
115 
120  dvar_matrix::dvar_matrix(int nrl,int nrh)
121  {
122  allocate(nrl,nrh);
123 #ifndef OPT_LIB
124  initialize();
125 #endif
126  }
127 
133  {
134  int min = pibnm.indexmin();
135  int max = pibnm.indexmax();
136  allocate(min, max);
137 
138 #ifndef OPT_LIB
139  initialize();
140 #endif
141 
142  for (int i = min; i <= max; ++i)
143  {
144  dvar_vector v(pibnm(i));
145  this->operator()(i) = v;
146  }
147  }
148 
153  dvar_matrix dvar_matrix::sub(int nrl,int nrh)
154  {
155  if (allocated(*this))
156  {
157  dvar_matrix tmp(nrl,nrh);
158  for (int i=nrl; i<=nrh; i++)
159  {
160  tmp[i].shallow_copy((*this)(i));
161  }
162  return tmp;
163  }
164  else
165  {
166  return *this;
167  }
168  }
169 
178 void dvar_matrix::allocate(int nrl, int nrh)
179 {
180  if (nrl > nrh)
181  {
182  allocate();
183  }
184  else
185  {
186  index_min = nrl;
187  index_max = nrh;
188  if ((m = new dvar_vector[rowsize()]) == 0)
189  {
190  cerr << " Error allocating memory in dvar_matrix::allocate(int, int)\n";
191  ad_exit(1);
192  }
193  if ((shape = new mat_shapex(m)) == 0)
194  {
195  cerr << " Error allocating memory in dvar_matrix::allocate(int, int)\n";
196  ad_exit(1);
197  }
198  m -= rowmin();
199 
200  dvar_vector* pva = m + nrl;
201  for (int i = nrl; i <= nrh; ++i)
202  {
203  pva->allocate();
204  ++pva;
205  }
206  }
207 }
216 void dvar_matrix::allocate(int nrl, int nrh, int ncl, int nch)
217 {
218  allocate(nrl, nrh);
219  dvar_vector* pva = m + nrl;
220  for (int i = nrl; i <= nrh; ++i)
221  {
222  pva->allocate(ncl, nch);
223  ++pva;
224  }
225 }
235 {
236  allocate(static_cast<int>(nrl), static_cast<int>(nrh));
237 }
238 
243 {
244  if (m1.shape)
245  {
246  int nrl = m1.rowmin();
247  int nrh = m1.rowmax();
248  index_min = nrl;
249  index_max = nrh;
250  if ((m = new dvar_vector[rowsize()]) == 0)
251  {
252  cerr << " Error allocating memory in dvar_matrix contructor\n";
253  ad_exit(21);
254  }
255  if ((shape = new mat_shapex(m)) == 0)
256  {
257  cerr << " Error allocating memory in dvar_matrix contructor\n";
258  }
259  m -= rowmin();
260  dvar_vector* pva = m + nrl;
261  const dvector* pm1 = &m1(nrl);
262  for (int i = nrl; i <= nrh; ++i)
263  {
264  pva->allocate(*pm1);
265  ++pva;
266  ++pm1;
267  }
268  }
269  else
270  {
271  //cerr << "Warning -- trying to make a dvar_matrix copy of an "
272  // " unallocated dmatrix" << endl;
273  allocate();
274  }
275 }
280 {
281  if (m1.shape)
282  {
283  int nrl = m1.rowmin();
284  int nrh = m1.rowmax();
285  index_min = nrl;
286  index_max = nrh;
287  if ((m = new dvar_vector[rowsize()]) == 0)
288  {
289  cerr << " Error allocating memory in dvar_matrix contructor"<<endl;
290  ad_exit(21);
291  }
292  if ((shape=new mat_shapex(m)) == 0)
293  {
294  cerr << " Error allocating memory in dvar_matrix contructor"<<endl;
295  }
296  m -= rowmin();
297  dvar_vector* pva = m + nrl;
298  const dvar_vector* pm1 = &m1(nrl);
299  for (int i = nrl; i <= nrh; ++i)
300  {
301  pva->allocate(*pm1);
302  ++pva;
303  ++pm1;
304  }
305  }
306  else
307  {
308  //cerr << "Warning -- trying to make a dvar_matrix copy of an "
309  // "unallocated dvar_matrix" << endl;
310  allocate();
311  }
312 }
313 
318 dvar_matrix::dvar_matrix(int nrl, int nrh, const ivector& ncl,
319  const ivector& nch)
320  {
321  allocate(nrl,nrh,ncl,nch);
322 #ifndef OPT_LIB
323  initialize();
324 #endif
325  }
326 
337  int nrl, int nrh,
338  const ivector& ncl, const ivector& nch)
339 {
340  if (nrl > nrh)
341  {
342  allocate();
343  }
344  else
345  {
346  if (nrl != ncl.indexmin() || nrh != ncl.indexmax()
347  || nrl != nch.indexmin() || nrh != nch.indexmax())
348  {
349  cerr << "Incompatible array bounds in "
350  "dvar_matrix(int nrl, int nrh, const ivector& ncl, const ivector& nch)"
351  << endl ;
352  ad_exit(1);
353  }
354  index_min = nrl;
355  index_max = nrh;
356  if ((m = new dvar_vector[rowsize()]) == 0)
357  {
358  cerr << " Error allocating memory in dvar_matrix contructor"<<endl;
359  ad_exit(21);
360  }
361  if ((shape = new mat_shapex(m)) == 0)
362  {
363  cerr << " Error allocating memory in dvar_matrix contructor"<<endl;
364  }
365  m -= rowmin();
366  dvar_vector* pva = m + nrl;
367  int* pncli = ncl.get_v() + nrl;
368  int* pnchi = nch.get_v() + nrl;
369  for (int i = nrl; i <= nrh; ++i)
370  {
371  pva->allocate(*pncli, *pnchi);
372  ++pva;
373  ++pncli;
374  ++pnchi;
375  }
376  }
377 }
378 
383 dvar_matrix::dvar_matrix(int nrl, int nrh, int ncl, const ivector& nch)
384  {
385  allocate(nrl,nrh,ncl,nch);
386 #ifndef OPT_LIB
387  initialize();
388 #endif
389  }
390 
399 void dvar_matrix::allocate(int nrl, int nrh, int ncl, const ivector& nch)
400 {
401  if (nrl>nrh)
402  {
403  allocate();
404  }
405  else
406  {
407  if (nrl != nch.indexmin() || nrh != nch.indexmax())
408  {
409  cerr << "Incompatible array bounds in "
410  "dvar_matrix(int nrl, int nrh, const int& ncl, const ivector& nch)"
411  << endl;
412  ad_exit(1);
413  }
414  index_min = nrl;
415  index_max = nrh;
416  if ((m = new dvar_vector[rowsize()]) == 0)
417  {
418  cerr << " Error allocating memory in dvar_matrix contructor"<<endl;
419  ad_exit(21);
420  }
421  if ((shape = new mat_shapex(m)) == 0)
422  {
423  cerr << " Error allocating memory in dvar_matrix contructor"<<endl;
424  }
425  m -= rowmin();
426  dvar_vector* pm = m + nrl;
427  int* pnchi = nch.get_v() + nrl;
428  for (int i = nrl; i <= nrh; ++i)
429  {
430  pm->allocate(ncl, *pnchi);
431  ++pm;
432  ++pnchi;
433  }
434  }
435 }
444 void dvar_matrix::allocate(int nrl, int nrh, const ivector& ncl, int nch)
445 {
446  allocate(nrl, nrh);
447  dvar_vector* pva = m + nrl;
448  for (int i = nrl; i <= nrh; ++i)
449  {
450  pva->allocate(ncl(i), nch);
451  ++pva;
452  }
453 }
460 {
461  if (other.shape)
462  {
463  shape = other.shape;
464  ++(shape->ncopies);
465  m = other.m;
466 
467  index_min = other.index_min;
468  index_max = other.index_max;
469  }
470  else
471  {
472 #ifdef DEBUG
473  cerr << "Warning -- Unable to shallow copy an unallocated dvar_matrix.\n";
474 #endif
475  allocate();
476  }
477 }
480 {
481  index_min = 1;
482  index_max = 0;
483  shape = nullptr;
484  m = nullptr;
485 }
488 {
489  if (shape)
490  {
491  if (shape->ncopies > 0)
492  {
493  --(shape->ncopies);
494  }
495  else
496  {
497  m = static_cast<dvar_vector*>(shape->get_pointer());
498  delete [] m;
499  delete shape;
500  }
501  allocate();
502  }
503 #if defined(DIAG)
504  else
505  {
506  cerr << "Warning -- Unable to deallocate an unallocated dvar_matrix.\n";
507  }
508 #endif
509 }
516 {
517  if (!allocated(*this))
518  {
519  shallow_copy(other);
520  }
521  else
522  {
523  int min = index_min;
524  int max = index_max;
525 #ifndef OPT_LIB
526  if (min != other.rowmin() || max != other.rowmax())
527  {
528  cerr << "Error: Incompatible array bounds in "
529  "dvar_matrix& dvar_matrix::operator=(const dvar_matrix&)\n";
530  ad_exit(1);
531  }
532 #endif
533  // check for condition that both matrices don't point to the same object
534  if (m != other.m)
535  {
536  dvar_vector* pm = m + min;
537  const dvar_vector* pother = &other(min);
538  for (int i = min; i <= max; ++i)
539  {
540  *pm = *pother;
541  ++pm;
542  ++pother;
543  }
544  }
545  }
546  return *this;
547 }
554 {
555  int min = index_min;
556  int max = index_max;
557 
558 #ifndef OPT_LIB
559  if (min != other.rowmin() || max != other.rowmax())
560  {
561  cerr << "Error: Incompatible array bounds in "
562  << "dvar_matrix& dvar_matrix::operator=(const dmatrix&)\n";
563  ad_exit(1);
564  }
565 #endif
566  if (m)
567  {
568  dvar_vector* pm = m + min;
569  const dvector* pother = &other(min);
570  for (int i = min; i <= max; ++i)
571  {
572  *pm = *pother;
573  ++pm;
574  ++pother;
575  }
576  }
577  return *this;
578 }
579 
584 void copy_status(const ostream& _s, const dvar_matrix& m1)
585  {
586  ostream& s= (ostream&) _s;
587  s << " matrix_copy flags \n";
588 
589  for (int i=m1.rowmin(); i<=m1.rowmax(); i++)
590  {
591  copy_status(s,m1[i]);
592  }
593  s <<"\n";
594  }
int index_min
Definition: fvar.hpp:2822
int rowmax(void) const
Definition: fvar.hpp:2564
Description not yet available.
Definition: fvar.hpp:2030
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
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
void initialize(void)
Zero initialize allocated dvar_matrix, then saves adjoint function and position data.
Definition: fvar_ma7.cpp:48
exitptr ad_exit
Definition: gradstrc.cpp:53
int index_max
Definition: fvar.hpp:2823
~dvar_matrix()
Destructor.
Definition: fvar_mat.cpp:73
Null class to allow specialized function overloads.
Definition: fvar.hpp:469
mat_shapex * shape
Definition: fvar.hpp:2825
ADMB variable vector.
Definition: fvar.hpp:2172
mat_shapex * shape
Definition: fvar.hpp:2485
void * get_pointer(void)
Definition: fvar.hpp:2046
int * get_v() const
Definition: ivector.h:114
void shallow_copy(const dvar_matrix &)
Shallow copy other data structure pointers.
Definition: fvar_mat.cpp:459
prnstream & endl(prnstream &)
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
int rowmax() const
Definition: fvar.hpp:2929
void deallocate()
Deallocate dvar_matrix memory.
Definition: fvar_mat.cpp:487
int indexmin() const
Returns lower row index.
#define min(a, b)
Definition: cbivnorm.cpp:188
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
void allocate(void)
Does not allocate, but initializes members.
Definition: fvar_mat.cpp:479
int indexmin() const
Definition: ivector.h:99
int indexmax() const
Definition: ivector.h:104
int rowmin(void) const
Definition: fvar.hpp:2560
Description not yet available.
Definition: fvar.hpp:2819
dvar_matrix sub(int, int)
Description not yet available.
Definition: fvar_mat.cpp:153
int index_min
Definition: fvar.hpp:2482
unsigned int ncopies
Definition: fvar.hpp:2034
unsigned int rowsize() const
Definition: fvar.hpp:2578
void allocate(int, int)
Allocate dvar_vector with indexmin = ncl and indexmax = nch.
Definition: fvar_arr.cpp:270
dvar_matrix()
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: fvar_mat.cpp:15
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
dvar_matrix & operator=(const dvar_matrix &)
Assigns other values to dvar_matrix.
Definition: fvar_mat.cpp:515
Class definition of matrix with derivitive information .
Definition: fvar.hpp:2480
dvar_vector & operator()(int i)
Definition: fvar.hpp:2776
Stores integer.
Definition: fvar.hpp:7654
#define max(a, b)
Definition: cbivnorm.cpp:189
int index_max
Definition: fvar.hpp:2483
int indexmax() const
Returns upper row index.
int rowmin() const
Definition: fvar.hpp:2925
dvar_vector * m
Definition: fvar.hpp:2484
void copy_status(const ostream &_s, const dvar_vector &v)
Description not yet available.
Definition: fvar_arr.cpp:206