ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
imat.cpp
Go to the documentation of this file.
1 
5 #include "fvar.hpp"
6 #ifdef __TURBOC__
7  #pragma hdrstop
8 #endif
9 
18 {
19  if (m == nullptr)
20  {
21  cerr << "Warning -- Trying to row shift empty imatrix in "
22  << "imatrix::rowshift(int).\n";
23  return;
24  }
25  m = m + rowmin() - min;
26  index_max += min - index_min;
27  index_min = min;
28 }
36 imatrix::imatrix(int nrl, int nrh)
37 {
38  allocate(nrl, nrh);
39 }
40 
45  imatrix imatrix::sub(int nrl,int nrh)
46  {
47  if (allocated(*this))
48  {
49  imatrix tmp(nrl,nrh);
50  for (int i=nrl; i<=nrh; i++)
51  {
52  tmp[i].shallow_copy((*this)(i));
53  }
54  return tmp;
55  }
56  else
57  {
58  return *this;
59  }
60  }
70 imatrix::imatrix(int nrl, int nrh, int ncl, int nch)
71 {
72  allocate(nrl, nrh, ncl, nch);
73 }
78  imatrix::imatrix(const ad_integer& nrl,const ad_integer& nrh,
79  const index_type& ncl,const index_type& nch)
80  {
81  allocate(nrl,nrh,ncl,nch);
82  }
91 imatrix::imatrix(int nrl, int nrh, const ivector& column)
92 {
93  allocate(nrl, nrh, column);
94 }
103 void imatrix::allocate(int nrl, int nrh, const ivector& column)
104 {
105  allocate(nrl, nrh);
106  for (int i = rowmin(); i <= rowmax(); ++i)
107  {
108  m[i].index_min = column.index_min;
109  m[i].index_max = column.index_max;
110  m[i].shape = column.shape;
111  if (m[i].shape)
112  {
113  (m[i].shape->ncopies)++;
114  m[i].v = column.v;
115  }
116  }
117 }
127 void imatrix::allocate(int nrl, int nrh, int ncl, int nch)
128 {
129  allocate(nrl, nrh);
130  ivector* pv = m + nrl;
131  for (int i = nrl; i <= nrh; ++i)
132  {
133  pv->allocate(ncl, nch);
134  ++pv;
135  }
136 }
139 {
140  index_min = 1;
141  index_max = 0;
142  m = nullptr;
143  shape = nullptr;
144 }
155 void imatrix::allocate(int nrl, int nrh, const ivector& ncl, const ivector& nch)
156 {
157  allocate(nrl, nrh);
158  ivector* pv = m + nrl;
159  int* pncli = ncl.get_v() + nrl;
160  int* pnchi = nch.get_v() + nrl;
161  for (int i = nrl; i <= nrh; ++i)
162  {
163  pv->allocate(*pncli, *pnchi);
164  ++pv;
165  ++pncli;
166  ++pnchi;
167  }
168 }
178 void imatrix::allocate(int nrl, int nrh, int ncl, const ivector& nch)
179 {
180  allocate(nrl, nrh);
181  ivector* pv = m + nrl;
182  int* pnchi = nch.get_v() + nrl;
183  for (int i = nrl; i <= nrh; ++i)
184  {
185  pv->allocate(ncl, *pnchi);
186  ++pv;
187  ++pnchi;
188  }
189 }
192 {
193  shallow_copy(other);
194 }
200 void imatrix::shallow_copy(const imatrix& other)
201 {
202  if (other.shape)
203  {
204  shape = other.shape;
205  ++(shape->ncopies);
206  m = other.m;
207  index_min = other.index_min;
208  index_max = other.index_max;
209  }
210  else
211  {
212 #ifdef DEBUG
213  cerr << "Warning -- Unable to shallow copy an unallocated imatrix.\n";
214 #endif
215  allocate();
216  }
217 }
218 
223 imatrix::imatrix(int nrl, int nrh, const ivector& ncl, const ivector& nch)
224  {
225  allocate(nrl,nrh,ncl,nch);
226  }
227 
232 imatrix::imatrix(int nrl, int nrh, int ncl, const ivector& nch)
233  {
234  allocate(nrl,nrh,ncl,nch);
235  }
236 
239 {
240  allocate();
241 }
244 {
245  deallocate();
246 }
249 {
250  if (shape)
251  {
252  if (shape->ncopies > 0)
253  {
254  --(shape->ncopies);
255  }
256  else
257  {
258  m = static_cast<ivector*>(shape->get_pointer());
259  delete [] m;
260  delete shape;
261  }
262  allocate();
263  }
264 #if defined(DIAG)
265  else
266  {
267  cerr << "Warning -- Unable to deallocate an unallocated imatrix.\n";
268  }
269 #endif
270 }
271 
279 void imatrix::allocate(int nrl, int nrh)
280 {
281  if (nrl > nrh)
282  {
283  return allocate();
284  }
285  index_min = nrl;
286  index_max = nrh;
287  if ((m = new ivector[rowsize()]) == 0)
288  {
289  cerr << "Error: Unable to allocate m in imatrix::allocate(int, int).\n";
290  ad_exit(1);
291  }
292  if ((shape = new mat_shapex(m)) == 0)
293  {
294  cerr << "Error: Unable to allocate shape in imatrix::allocate(int, int).\n";
295  ad_exit(1);
296  }
297  m -= indexmin();
298 }
ivector * m
Definition: imatrix.h:74
Uses polymorphism to get index information from various data types to be used in constructing and all...
Definition: fvar.hpp:7731
void allocate(void)
Does not allocate, but initializes imatrix members.
Definition: imat.cpp:138
Description not yet available.
Definition: fvar.hpp:2030
mat_shapex * shape
Definition: imatrix.h:75
Description not yet available.
Definition: imatrix.h:69
int indexmin() const
Definition: imatrix.h:138
void deallocate()
Deallocate imatrix memory.
Definition: imat.cpp:248
int allocated(const ivector &v)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: fvar_a59.cpp:13
int index_max
Definition: imatrix.h:73
~imatrix()
Destructor.
Definition: imat.cpp:243
int rowmin() const
Definition: imatrix.h:146
exitptr ad_exit
Definition: gradstrc.cpp:53
void shallow_copy(const imatrix &)
Shallow copy other data structure pointers.
Definition: imat.cpp:200
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
vector_shapex * shape
Definition: ivector.h:56
int index_max
Definition: ivector.h:54
#define min(a, b)
Definition: cbivnorm.cpp:188
unsigned int rowsize() const
Returns the number of rows.
Definition: imatrix.h:165
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
unsigned int ncopies
Copy counter to enable shallow copies.
Definition: vector_shapex.h:79
unsigned int ncopies
Definition: fvar.hpp:2034
imatrix(void)
Default constructor.
Definition: imat.cpp:238
int * v
Definition: ivector.h:55
dvector column(const dmatrix &matrix, int j)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat6.cpp:13
Stores integer.
Definition: fvar.hpp:7654
imatrix sub(int, int)
Description not yet available.
Definition: imat.cpp:45
int rowmax() const
Definition: imatrix.h:150
int index_min
Definition: imatrix.h:72
void allocate(const ad_integer &ncl, const index_type &ncu)
Allocate vector of integers with dimension [_ncl to _nch].
Definition: indextyp.cpp:488
int index_min
Definition: ivector.h:53
void rowshift(int min)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: imat.cpp:17