ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dmat.cpp
Go to the documentation of this file.
1 
5 #include "fvar.hpp"
6 
11  struct dvec_ptr_ptr
12  {
13  void ** m;
14  };
15 
20 dmatrix::dmatrix(int nrl, int nrh, int ncl, int nch)
21 {
22  allocate(nrl, nrh, ncl, nch);
23 }
33 void dmatrix::allocate(int nrl, int nrh, int ncl, int nch)
34 {
35  allocate(nrl, nrh);
36  dvector* pm = m + nrl;
37  for (int i = nrl; i <= nrh; ++i)
38  {
39  pm->allocate(ncl, nch);
40  ++pm;
41  }
42 }
47  dmatrix dmatrix::sub(int nrl,int nrh)
48  {
49  if (allocated(*this))
50  {
51  dmatrix tmp(nrl,nrh);
52  for (int i=nrl; i<=nrh; i++)
53  {
54  tmp[i].shallow_copy((*this)(i));
55  }
56  return tmp;
57  }
58  else
59  {
60  return *this;
61  }
62  }
63 
68  dmatrix::dmatrix(int nrl,int nrh)
69  {
70  allocate(nrl,nrh);
71  }
72 
79 void dmatrix::allocate(int nrl, int nrh)
80 {
81  if (nrh < nrl)
82  {
83  allocate();
84  }
85  else
86  {
87  index_min = nrl;
88  index_max = nrh;
89  if ((m = new dvector[rowsize()]) == 0)
90  {
91  cerr << " Error allocating in dmatrix::allocate(int, int)\n";
92  ad_exit(1);
93  }
94  if ((shape = new mat_shapex(m)) == 0)
95  {
96  cerr << " Error allocating in dmatrix::allocate(int, int)\n";
97  ad_exit(1);
98  }
99  m -= rowmin();
100  }
101 }
108 {
109  allocate(static_cast<int>(nrl), static_cast<int>(nrh));
110 }
117 double min(const dmatrix& m)
118 {
119  double minimum = min(m(m.rowmin()));
120  for (int i = m.rowmin() + 1; i <= m.rowmax(); ++i)
121  {
122  double value = min(m(i));
123  if (value < minimum) minimum = value;
124  }
125  return minimum;
126 }
133 double max(const dmatrix& m)
134 {
135  double maximum = max(m(m.rowmin()));
136  for (int i = m.rowmin() + 1; i <= m.rowmax(); ++i)
137  {
138  double value = max(m(i));
139  if (value > maximum) maximum = value;
140  }
141  return maximum;
142 }
148 void dmatrix::allocate(const dmatrix& other)
149 {
150  int min = other.rowmin();
151  int max = other.rowmax();
152  allocate(min, max);
153 
154  dvector* pm = m + min;
155  const dvector* pother = &other(min);
156  for (int i = min; i <= max; ++i)
157  {
158  pm->allocate(*pother);
159  ++pother;
160  ++pm;
161  }
162 }
171 bool ivector_check(const ivector& vector, int indexmin, int indexmax)
172 {
173  return !(vector.indexmin() == indexmin && vector.indexmax() == indexmax);
174 }
184 void dmatrix::allocate(int nrl, int nrh, const ivector& ncl, const ivector& nch)
185 {
186 #ifndef OPT_LIB
187  if (nrl != ncl.indexmin() || nrh != ncl.indexmax()
188  || nrl != nch.indexmin() || nrh != nch.indexmax())
189  {
190  cerr << "Incompatible array bounds in "
191  << "dmatrix(int, int, const ivector&,const ivector&)\n";
192  ad_exit(1);
193  }
194 #endif
195  allocate(nrl, nrh);
196  dvector* pm = m + nrl;
197  int* pncl = ncl.get_v() + nrl;
198  int* pnch = nch.get_v() + nrl;
199  for (int i = nrl; i <= nrh; ++i)
200  {
201  pm->allocate(*pncl, *pnch);
202  ++pncl;
203  ++pnch;
204  ++pm;
205  }
206 }
216 void dmatrix::allocate(int nrl, int nrh, int ncl, const ivector& nch)
217 {
218 #ifndef OPT_LIB
219  if (nrl != nch.indexmin() || nrh != nch.indexmax())
220  {
221  cerr << "Incompatible array bounds in "
222  << "dmatrix(int nrl, int nrh, int ncl, const ivector& nch)\n";
223  ad_exit(1);
224  }
225 #endif
226  allocate(nrl, nrh);
227  dvector* pm = m + nrl;
228  const int* pnch = nch.get_v() + nrl;
229  for (int i = nrl; i <= nrh; ++i)
230  {
231  pm->allocate(ncl, *pnch);
232  ++pm;
233  ++pnch;
234  }
235 }
245 void dmatrix::allocate(int nrl, int nrh, const ivector& ncl, int nch)
246 {
247 #ifndef OPT_LIB
248  if (nrl != ncl.indexmin() || nrh != ncl.indexmax())
249  {
250  cerr << "Incompatible array bounds in "
251  << "dmatrix(int nrl, int nrh, int ncl, const ivector& nch)\n";
252  ad_exit(1);
253  }
254 #endif
255  allocate(nrl, nrh);
256  dvector* pm = m + nrl;
257  const int* pncl = ncl.get_v() + nrl;
258  for (int i = nrl; i <= nrh; ++i)
259  {
260  pm->allocate(*pncl, nch);
261  ++pncl;
262  ++pm;
263  }
264 }
265 
266 /*
267  dmatrix::dmatrix(void)
268  {
269  #ifdef DIAG
270  myheapcheck("Entering dmatrix(nrl,nrh,ncl,nch)" );
271  #endif
272 
273  shape = NULL;
274  m=NULL;
275  }
276 */
277 
282 dmatrix::dmatrix(int nrl, int nrh, const ivector& ncl, const ivector& nch)
283  {
284  allocate(nrl,nrh,ncl,nch);
285  }
286 
291 dmatrix::dmatrix(int nrl, int nrh, int ncl, const ivector& nch)
292  {
293  allocate(nrl,nrh,ncl,nch);
294  }
295 
298 {
299  allocate();
300  shallow_copy(other);
301 }
307 void dmatrix::shallow_copy(const dmatrix& other)
308 {
309  if (other.shape)
310  {
311  shape = other.shape;
312  ++(shape->ncopies);
313  m = other.m;
314 
315  index_min = other.index_min;
316  index_max = other.index_max;
317  }
318 #ifdef DEBUG
319  else
320  {
321  cerr << "Warning -- Unable to shallow copy an unallocated dmatrix.\n";
322  }
323 #endif
324 }
327 {
328  deallocate();
329 }
336 dvector cube(const dvector& vec)
337 {
338  dvector results;
339  results.allocate(vec);
340  for (int i = results.indexmin(); i <= results.indexmax(); ++i)
341  {
342  results(i) = cube(vec(i));
343  }
344  return results;
345 }
352 dmatrix cube(const dmatrix& mat)
353 {
354  dmatrix results;
355  results.allocate(mat);
356  for (int i = results.rowmin(); i <= results.rowmax(); ++i)
357  {
358  results(i) = cube(mat(i));
359  }
360  return results;
361 }
364 {
365  if (shape)
366  {
367  if (shape->ncopies)
368  {
369  --(shape->ncopies);
370  }
371  else
372  {
373  m = static_cast<dvector*>(shape->get_pointer());
374  delete [] m;
375  delete shape;
376  }
377  allocate();
378  }
379 #if defined(DIAG)
380  else
381  {
382  cerr << "Warning -- Unable to deallocate an unallocated dmatrix.\n";
383  }
384 #endif
385 }
int index_min
Definition: fvar.hpp:2822
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat.cpp:11
Description not yet available.
Definition: fvar.hpp:2030
void allocate(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat0.cpp:8
void deallocate()
Deallocate dmatrix memory.
Definition: dmat.cpp:363
void ** m
Definition: dmat.cpp:13
dmatrix(void)
Default constructor.
Definition: dmat0.cpp:16
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 allocate(int ncl, int ncu)
Allocate memory for a dvector.
Definition: dvector.cpp:409
exitptr ad_exit
Definition: gradstrc.cpp:53
int index_max
Definition: fvar.hpp:2823
dmatrix sub(int, int)
Description not yet available.
Definition: dmat.cpp:47
d3_array cube(const d3_array &m)
Description not yet available.
Definition: d3arr5.cpp:17
mat_shapex * shape
Definition: fvar.hpp:2825
bool ivector_check(const ivector &vector, int indexmin, int indexmax)
Return true if vector dimensions match with indexmin and indexmax, otherwise false.
Definition: dmat.cpp:171
void * get_pointer(void)
Definition: fvar.hpp:2046
int * get_v() const
Definition: ivector.h:114
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
int rowmax() const
Definition: fvar.hpp:2929
#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.
int indexmin() const
Definition: ivector.h:99
dvector * m
Definition: fvar.hpp:2824
int indexmax() const
Definition: ivector.h:104
Description not yet available.
Definition: fvar.hpp:2819
~dmatrix()
Destructor.
Definition: dmat.cpp:326
unsigned int ncopies
Definition: fvar.hpp:2034
void shallow_copy(const dmatrix &)
Shallow copy values and dimensions from other to dmatrix.
Definition: dmat.cpp:307
unsigned int rowsize() const
Definition: fvar.hpp:2934
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
Stores integer.
Definition: fvar.hpp:7654
#define max(a, b)
Definition: cbivnorm.cpp:189
int rowmin() const
Definition: fvar.hpp:2925