ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fvar_arr.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 #if defined(__TURBOC__)
13  #pragma hdrstop
14  #include <alloc.h>
15 #endif
16 
17 #include <stdlib.h>
18 
19 #ifndef OPT_LIB
20  #include <cassert>
21  #include <climits>
22 #endif
23 
29 {
30  va += indexmin()-min;
32  index_min=min;
33  shape->shift(min);
34  return *this;
35 }
36 
42 {
43  int min = t.indexmin();
44  int max = t.indexmax();
45  allocate(min, max);
46  if (va)
47  {
48  double_and_int* pva = va + min;
49  double* pt = t.get_v() + min;
50  for (int i = min; i <= max; ++i)
51  {
52  pva->x = *pt;
53  ++pt;
54  ++pva;
55  }
56  make_indvar_list(*this);
57  }
58 }
59 
64 dvar_vector::dvar_vector(const dvector& t): va(nullptr)
65 {
66  if (!t)
67  {
68  allocate();
69  }
70  else
71  {
72  int min = t.indexmin();
73  int max = t.indexmax();
74  allocate(min, max);
75  initialize();
76  double_and_int* pva = va + min;
77  double* pt = t.get_v() + min;
78  for (int i = min; i <= max; ++i)
79  {
80  va[i].x=(t.v)[i];
81  pva->x = *pt;
82  ++pt;
83  ++pva;
84  }
85  }
86 }
87 
88 
89 //#ifdef __BORLANDC__
90  //prevariable dvar_vector::elem(int i) { return (va+i); }
91 //#endif
92 
97 dvar_vector::dvar_vector(unsigned int sz, double* x)
98 {
99 #ifndef OPT_LIB
100  assert(sz > 0 && sz <= INT_MAX);
101 #endif
102  allocate(0, (int)(sz - 1));
103  for (unsigned int i = 0; i < sz; i++)
104  {
105  va[i].x = x[i];
106  }
107 }
108 
113  dvar_vector::dvar_vector(int ncl,int nch)
114  {
115  if (ncl>nch)
116  allocate();
117  else
118  {
119  va=NULL;
120  allocate(ncl,nch);
121  }
122  #ifdef SAFE_INITIALIZE
123  initialize();
124  #endif
125 
126  #ifdef DIAG
127  cout << " Allocating dvar_vector with ptr_address\n "
128  << &va << " pointing at " << (va+indexmin()) << "\n";
129  #endif
130  }
131 
132 /*
133  dvar_vector::dvar_vector(const ad_integer& ncl,const ad_integer& nch)
134  {
135  allocate(ncl,nch);
136  #ifdef SAFE_INITIALIZE
137  initialize();
138  #endif
139 
140  #ifdef DIAG
141  cout << " Allocating dvar_vector with ptr_address\n "
142  << &va << " pointing at " << (va+indexmin()) << "\n";
143  #endif
144  }
145  */
146 
151 {
152  allocate();
153 }
154 
160 {
162 }
164 {
165  int min = t.indexmin();
166  int max = t.indexmax();
167  unsigned int size = (unsigned int)(max - min + 1);
169  {
170  ad_printf("Current maximum number of independent variables is %d\n",
172  ad_printf(" You need to increase the global variable "
173  "MAX_NVAR_OFFSET to %d\n", size);
174  ad_printf(" This can be done by putting the line\n"
175  " gradient_structure::set_MAX_NVAR_OFFSET(%d);\n", size);
176  ad_printf(" before the declaration of the gradient_structure object.\n"
177  " or the command line option -mno %d\n", size);
178  /*
179  cerr << "Current maximum number of independent variables is "
180  << gradient_structure::MAX_NVAR_OFFSET << "\n"
181  << " You need to increase the global variable MAX_NVAR_OFFSET to "
182  << size << "\n"
183  << " This can be done by putting the line\n"
184  << " 'gradient_structure::set_MAX_NVAR_OFFSET("
185  << size << ");'\n"
186  << " before the declaration of the gradient_structure object.\n"
187  << " or use the -mno 1149 command line option in AD Model Builder\n";
188  */
189  ad_exit(1);
190  }
191 
192  double_and_int* pt = t.va + min;
193  for (int i = min; i <= max; ++i)
194  {
195  unsigned int tmp = (unsigned int)(i - min);
196  INDVAR_LIST->put_address(tmp, &(pt->x));
197  ++pt;
198  }
199  NVAR = size;
200 }
201 
206 void copy_status(const ostream& _s, const dvar_vector& v)
207  {
208  ostream& s= (ostream&) _s;
209  s << " copy_flag ";
210  s <<"\n";
211  }
212 
217 {
218  va = NULL;
219  link_ptr = NULL;
220  shape = NULL;
221  index_min = 0;
222  index_max = -1;
223 }
228 {
229  allocate(v1.indexmin(), v1.indexmax());
230 }
235 {
236  allocate(v1.indexmin(), v1.indexmax());
237 }
238 
244  {
245  if (!(*this))
246  {
247  if (t.shape)
248  {
249  shape=t.shape;
250  (shape->ncopies)++;
251  }
252  else
253  {
254  //cerr << "Making a copy of an unallocated dvar_vector"<<endl;
255  }
256  link_ptr=t.link_ptr;
259  va = t.va;
260  }
261  else
262  {
263  cerr << "Trying to alocate to an already allocated dvar_vector" << endl;
264  }
265  }
266 
270 void dvar_vector::allocate(int ncl, int nch)
271 {
272  if (ncl > nch)
273  {
274  allocate();
275  }
276  else
277  {
278  index_min=ncl;
279  index_max=nch;
280  unsigned int itemp = (unsigned int)(nch - ncl + 1);
281 #ifdef DEBUG
282  if (itemp<=0)
283  {
284  cerr << "Error in dvar_vector constructor max index must be"
285  " >= minindex\n"
286  << "minindex = " << ncl << " maxindex = " << nch <<endl;
287  ad_exit(1);
288  }
289 #endif
291  if (!gs)
292  {
293  cerr << "Error: instance of gradient_structure is a nullptr.\n";
294  ad_exit(1);
295  }
296  if ((va = gs->ARR_LIST1->arr_new(itemp)) == 0)
297  {
298  cerr << " Error trying to allocate memory for dvar_vector\n";
299  ad_exit(1);
300  }
301  else
302  {
303  if ( (shape=new vector_shapex(ncl,nch,va)) == NULL)
304  {
305  cerr << "Error trying to allocate memory for dvar_vector\n";
306  ad_exit(1);
307  }
308  link_ptr=* (arr_link **) va;
309  va -= indexmin();
310  // if ( ((int)va) %8) cerr << "Array is not QWORD alligned" << endl;
311 #ifdef DIAG
312  myheapcheck("Leaving dvar_vector::allocate(ncl,nch)");
313 #endif
314  }
315  }
316 }
317 
322  void dvar_vector::allocate(const ad_integer& ncl,const ad_integer& nch)
323  {
324  allocate(int(ncl),int(nch));
325  }
indvar_offset_list * INDVAR_LIST
dvar_vector()
Default constructor.
Definition: fvar_arr.cpp:150
void myheapcheck(char *msg)
Does nothing.
Definition: dvector.cpp:669
double * v
pointer to the data
Definition: dvector.h:53
double_and_int * arr_new(unsigned int sz)
Definition: newdar.cpp:78
void allocatec(const dvar_vector &)
Description not yet available.
Definition: fvar_arr.cpp:243
dvar_vector & shift(int min)
Description not yet available.
Definition: fvar_arr.cpp:28
friend void make_indvar_list(const dvar_vector &)
Description not yet available.
Definition: fvar_arr.cpp:159
#define x
Vector of double precision numbers.
Definition: dvector.h:50
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
void allocate(void)
Does not allocate, but initializes dvar_vector to empty.
Definition: fvar_arr.cpp:216
exitptr ad_exit
Definition: gradstrc.cpp:53
ADMB variable vector.
Definition: fvar.hpp:2172
static unsigned int MAX_NVAR_OFFSET
Holds the data for the prevariable class.
Definition: fvar.hpp:191
prnstream & endl(prnstream &)
Description not yet available.
Definition: fvar.hpp:1937
#define min(a, b)
Definition: cbivnorm.cpp:188
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
void make_indvar_list(const dvar_vector &t)
Definition: fvar_arr.cpp:163
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
arr_link * link_ptr
Definition: fvar.hpp:2178
static _THREAD gradient_structure * _instance
unsigned int ncopies
Copy counter to enable shallow copies.
Definition: vector_shapex.h:79
int indexmin() const
Definition: fvar.hpp:2287
void initialize(void)
Description not yet available.
Definition: fvar_a24.cpp:17
int index_min
Definition: fvar.hpp:2176
void put_address(unsigned int &i, double *iaddress)
Definition: fvar.hpp:1296
Holds &quot;shape&quot; information for vector objects.
Definition: vector_shapex.h:46
void shift(int min)
Description not yet available.
Definition: shape.cpp:33
double_and_int * va
Definition: fvar.hpp:2175
Stores integer.
Definition: fvar.hpp:7654
void make_indvar_list(const dvar_vector &t)
Description not yet available.
Definition: fvar_arr.cpp:159
#define max(a, b)
Definition: cbivnorm.cpp:189
class for things related to the gradient structures, including dimension of arrays, size of buffers, etc.
double *& get_v(void)
Definition: dvector.h:148
int indexmax() const
Definition: fvar.hpp:2292
int ad_printf(FILE *stream, const char *format, Args...args)
Definition: fvar.hpp:9487
vector_shapex * shape
Definition: fvar.hpp:2179
double x
&lt; value of the variable
Definition: fvar.hpp:195
int index_max
Definition: fvar.hpp:2177
void copy_status(const ostream &_s, const dvar_vector &v)
Description not yet available.
Definition: fvar_arr.cpp:206