ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dvector.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  */
12 #ifndef OPT_LIB
13  #ifndef __SUNPRO_CC
14  #include <algorithm>
15  using std::fill_n;
16  #endif
17 #endif
18 
19 #include "fvar.hpp"
20 
21 #ifdef DIAG
22  int heapcheck(void){return 0;}
23 #else
24  int heapcheck(void);
26 #endif
27 
28 #ifdef _MSC_VER
29 #include <memory.h>
30 #endif
31 #ifdef DEBUG
32  #include <cassert>
33  #include <climits>
34 #endif
35 
42 double avg(double x,double y)
43 {
44  return 0.5*(x+y);
45 }
46 
53 {
54  v += indexmin()-min;
56  index_min=min;
57  shape->shift(min);
58  return *this;
59 }
66 {
67  if (shape)
68  {
69  if (shape->ncopies)
70  {
71  (shape->ncopies)--;
72  }
73  else
74  {
75 #ifdef DEBUG
76  #ifdef DIAG
77  myheapcheck(" Entering ~dvector");
78  #endif
79  if (v == NULL)
80  {
81  cerr << " Trying to delete NULL pointer in ~dvector\n";
82  ad_exit(21);
83  }
84 #endif
85  deallocate();
86  }
87  }
88 }
93 {
94  if (shape)
95  {
96  v = (double*)(shape->trueptr);
97  if (v)
98  {
99  delete [] v;
100  v = NULL;
101  }
102 
103  delete shape;
104  shape = NULL;
105  }
106 }
111 {
112  if (shape)
113  {
114  if (shape->ncopies)
115  {
116  cerr << "trying to deallocate a dvector with copies" << endl;
117  ad_exit(1);
118  }
119 
120  deallocate();
121  }
122 }
123 
149 {
150  shallow_copy(other);
151 }
157 void dvector::shallow_copy(const dvector& other)
158 {
159 #if defined(DIAG)
160  cout << "starting out in dvector contructor\n";
161 #endif
162  shape = other.shape;
163  if (shape)
164  {
165  (shape->ncopies)++;
166  }
167 #ifdef DEBUG
168  else
169  {
170  cerr << "Making a copy of an unallocated dvector"<<endl;
171  }
172 #endif
173  v = other.v;
174  index_min = other.index_min;
175  index_max = other.index_max;
176 }
184 {
185  int mmin = pdv.p->indexmin();
186  int mmax = pdv.p->indexmax();
187  if (pdv.lb < mmin || pdv.ub > mmax)
188  {
189  cerr << "index out of bounds in dvector subvector operator" << endl;
190  ad_exit(1);
191  }
192 
193  shape = pdv.p->shape;
194  if (shape)
195  {
196  (shape->ncopies)++;
197  }
198  else
199  {
200  cerr << "Waring: Taking a subvector of an unallocated dvector.\n";
201  }
202 
203  v = pdv.p->v;
204  index_min = pdv.lb;
205  index_max = pdv.ub;
206 }
207 
216 {
217 #ifdef DIAG
218  myheapcheck("Entering dvector=");
219 #endif
220 
221  int min = index_min;
222  int max = index_max;
223  double* pvi = v + min;
224  for (int i = min; i <= max; ++i)
225  {
226  *pvi = x;
227  ++pvi;
228  }
229 #ifdef DIAG
230  myheapcheck("Leaving dvector=");
231 #endif
232  return *this;
233 }
234 
245 {
246  if (!(*this))
247  {
248  allocatec(t);
249  }
250 #if defined (AD_FAST_ASSIGN)
251  else if (!(t.shape->ncopies))
252  {
253  deallocate();
254  allocatec(t);
255  }
256 #endif
257  else
258  {
259  int min = indexmin();
260 
261 #ifndef OPT_LIB
262  int max = indexmax();
263  if (min != t.indexmin() || max != t.indexmax())
264  {
265  cerr << "Index bounds do not match in "
266  "dvector& operator = (const dvector&)\n";
267  ad_exit(1);
268  }
269 #endif
270 
271  if (v != t.v)
272  {
273  constexpr size_t sizeofdouble = sizeof(double);
274  double* pv = v + min;
275  double* pt = t.get_v() + min;
276  memcpy(pv, pt, size() * sizeofdouble);
277  }
278  }
279  return *this;
280 }
281 
293 {
294 #ifdef DIAG
295  myheapcheck("Entering dvector =");
296 #endif
297 
298  int min = indexmin();
299 #ifndef OPT_LIB
300  int max = indexmax();
301  if (min != t.indexmin() || max != t.indexmax())
302  {
303  cerr << "Index bounds do not match in "
304  "independent_variables& independent_variables::operator=(const dvector& t)\n";
305  ad_exit(1);
306  }
307 #endif
308  //double tmp;
309  // disconnect dvector pointer from old array
310  if (v != t.address())
311  {
312  constexpr size_t sizeofdouble = sizeof(double);
313  double* pv = v + min;
314  double* pt = t.get_v() + min;
315  memcpy(pv, pt, size() * sizeofdouble);
316  }
317 #ifdef DIAG
318  myheapcheck("Leaving dvector =");
319 #endif
320  return *this;
321 }
322 
329 dvector::dvector(unsigned int sz, double* x)
330 {
331 #ifdef DEBUG
332  assert(sz > 0 && sz - 1 <= INT_MAX);
333 #endif
334  allocate(0, (int)(sz - 1));
335  for (unsigned int i = 0; i < sz; i++)
336  {
337  //cout << "Doing the assignment in constructor\n";
338  v[i] = x[i];
339  }
340 }
341 
342 /*
343  dvector::operator double* ( )
344  {
345  return(v);
346  }
347 */
348 
357  {
358  allocate(dvp.indexmin(),dvp.indexmax());
359  }
360 
366  dvector::dvector( int ncl, int nch)
367  {
368  if (ncl>nch)
369  allocate();
370  else
371  allocate(ncl,nch);
372  }
373 
379  {
380  allocate();
381  }
382 
390  void dvector::safe_allocate(int ncl,int nch)
391  {
392  if (allocated())
393  {
394  cerr << "trying to allocate an already allocated dvector " << endl;
395  ad_exit(1);
396  }
397  else
398  {
399  allocate(ncl,nch);
400  }
401  }
402 
409 void dvector::allocate(int ncl, int nch)
410 {
411  if (ncl > nch)
412  {
413  return allocate();
414  }
415  //Originally +2
416  //int size = nch - ncl + 2;
417 
418  unsigned int size = static_cast<unsigned int>(nch - ncl + 1);
419  v = new double[size];
420 
421  if (v == NULL)
422  {
423  cerr << " Error: Unable to allocate v in"
424  << " dvector::allocate(int, int).\n";
425  ad_exit(1);
426  }
427 
428 #if defined(THREAD_SAFE)
429  shape = new ts_vector_shapex(ncl, nch, v);
430 #else
431  shape = new vector_shapex(ncl, nch, v);
432 #endif
433  if (shape == NULL)
434  {
435  cerr << " Error: Unable to allocate shape in"
436  << " dvector::allocate(int, int).\n";
437  ad_exit(1);
438  }
439 
440  //reset v(index_min) to v[0]
441  v -= ncl;
442 
443  index_min = ncl;
444  index_max = nch;
445 
446  initialize();
447 }
453 void dvector::allocate(const dvector& other)
454 {
455  allocate(other.indexmin(), other.indexmax());
456 }
457 
463 {
464  allocate(dv.indexmin(),dv.indexmax());
465 }
466 
473 {
474  if (!(*this))
475  {
476  if (t.shape)
477  {
478  shape=t.shape;
479  (shape->ncopies)++;
480  }
481  v = t.v;
484  }
485  else
486  {
487  cerr << "Trying to alocate to an already allocated dvector" << endl;
488  }
489 }
490 
496 {
497  shape = NULL;
498  v = NULL;
499  index_min = 1;
500  index_max = 0;
501 }
502 
512 double operator*(const dvector& t1, const dvector& t2)
513 {
514  int min = t1.indexmin();
515  int max = t1.indexmax();
516 #ifndef OPT_LIB
517  if (min != t2.indexmin() || max != t2.indexmax())
518  {
519  cerr << "Index bounds do not match in "
520  "dvector operator * (const dvector&, const dvector&)\n";
521  ad_exit(1);
522  }
523 #endif
524 
525  double tmp{0};
526 
527 #ifdef USE_ASSEMBLER
528  int min=t1.indexmin();
529  int n=t1.indexmax()-min+1;
530  dp_dotproduct(&tmp,&(t1(min)),&(t2(min)),n);
531 #else
532  double* pt1 = t1.get_v() + min;
533  double* pt2 = t2.get_v() + min;
534  for (int i = min; i <= max; ++i)
535  {
536  tmp += *pt1 * *pt2;
537  ++pt1;
538  ++pt2;
539  }
540 #endif
541 
542  return tmp;
543 }
544 
554 dvector operator+(const dvector& t1, const dvector& t2)
555 {
556  int min = t1.indexmin();
557  int max = t1.indexmax();
558 
559 #ifndef OPT_LIB
560  if (min != t2.indexmin() || max != t2.indexmax())
561  {
562  cerr << "Index bounds do not match in "
563  "dvector operator+(const dvector&, const dvector&)\n";
564  ad_exit(1);
565  }
566 #endif
567  dvector tmp(min, max);
568 
569  double* pt1 = t1.get_v() + min;
570  double* pt2 = t2.get_v() + min;
571  double* ptmp = tmp.get_v() + min;
572  for (int i = min; i <= max; ++i)
573  {
574  *ptmp += *pt1 + *pt2;
575 
576  ++pt1;
577  ++pt2;
578  ++ptmp;
579  }
580 
581  return tmp;
582 }
583 
593 dvector operator-(const dvector& t1, const dvector& t2)
594 {
595  int min = t1.indexmin();
596  int max = t1.indexmax();
597 
598 #ifndef OPT_LIB
599  if (min != t2.indexmin() || max != t2.indexmax())
600  {
601  cerr << "Index bounds do not match in "
602  "dvector operator+(const dvector&, const dvector&)\n";
603  ad_exit(1);
604  }
605 #endif
606  dvector tmp(min, max);
607 
608  double* pt1 = t1.get_v() + min;
609  double* pt2 = t2.get_v() + min;
610  double* ptmp = tmp.get_v() + min;
611  for (int i = min; i <= max; ++i)
612  {
613  *ptmp += *pt1 - *pt2;
614 
615  ++pt1;
616  ++pt2;
617  ++ptmp;
618  }
619 
620  return tmp;
621 }
622 
630 dvector operator*(const double x, const dvector& t1)
631 {
632  int min = t1.indexmin();
633  int max = t1.indexmax();
634 
635  dvector tmp(min, max);
636 
637  double* pt1 = t1.get_v() + min;
638  double* ptmp = tmp.get_v() + min;
639  for (int i = min; i <= max; ++i)
640  {
641  *ptmp = x * *pt1;
642 
643  ++pt1;
644  ++ptmp;
645  }
646 
647  return tmp;
648 }
649 /*
650 #ifdef __TURBOC__
651  void myheapcheck(char * msg)
652  {
653  if( ::heapcheck() == _HEAPCORRUPT )
654  {
655  cerr << msg << "Heap is corrupted.\n";
656  }
657  else
658  {
659  cerr << msg << "Heap is OK.\n";
660  }
661  }
662 #else
663 */
669  void myheapcheck(char * msg){}
670 
671 /*
672 #endif
673 */
674 
681 int max(int a,int b)
682 {
683  if (a>b)
684  return a;
685  else
686  return b;
687 }
688 
694 double cube(const double m)
695 {
696  return std::pow(m, 3);
697 }
698 
704 double fourth(const double m)
705 {
706  double m2=m*m;
707  return m2*m2;
708 }
709 
int ub
Definition: fvar.hpp:1905
void myheapcheck(char *msg)
Does nothing.
Definition: dvector.cpp:669
double * v
pointer to the data
Definition: dvector.h:53
void safe_deallocate(void)
Safely deallocates memory by reporting if shallow copies are still in scope.
Definition: dvector.cpp:110
void * trueptr
Address of first element in the vector.
Definition: vector_shapex.h:82
void memcpy(test_smartlist &dest, void *source, const size_t nsize)
memcpy for test_smartlist
Definition: df1b2f10.cpp:367
dvector()
Construct a dvector without allocating memory.
Definition: dvector.cpp:378
int allocated(void) const
Returns 1 (TRUE) if memory is allocated.
Definition: dvector.h:71
d3_array operator-(const d3_array &a, const d3_array &b)
Returns d3_array results with computed elements addition of a(i, j, k) + b(i, j, k).
Definition: d3arr2a.cpp:152
#define x
Vector of double precision numbers.
Definition: dvector.h:50
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
independent_variables & operator=(const dvector &t)
Assignment operator for dvector argument.
Definition: dvector.cpp:292
dvector & operator=(const dvector &t)
Assignment operator for dvector argument.
Definition: dvector.cpp:244
d3_array operator+(const d3_array &a, const d3_array &b)
Returns d3_array results with computed elements addition of a(i, j, k) + b(i, j, k).
Definition: d3arr2a.cpp:132
void allocatec(const dvector &dv)
Make shallow copy of dvector shape.
Definition: dvector.cpp:472
Description not yet available.
Definition: fvar.hpp:4814
friend double min(const dvector &)
Returns the minimum value of vector vec.
Definition: dvect7.cpp:107
int indexmin() const
Definition: fvar.hpp:4827
exitptr ad_exit
Definition: gradstrc.cpp:53
void allocate(void)
Allocate dvector without allocating memory.
Definition: dvector.cpp:495
d3_array cube(const d3_array &m)
Description not yet available.
Definition: d3arr5.cpp:17
Null class to allow specialized function overloads.
Definition: fvar.hpp:469
ADMB variable vector.
Definition: fvar.hpp:2172
dmatrix operator*(const d3_array &t, const dvector &v)
Description not yet available.
Definition: d3arr12.cpp:17
friend double max(const dvector &)
Returns the maximum value of vector vec.
Definition: dvect7.cpp:85
int heapcheck(void)
Does nothing.
prnstream & endl(prnstream &)
const double * address() const
Definition: dvector.h:144
Description not yet available.
Definition: fvar.hpp:1937
double avg(double x, double y)
Average of two numbers; constant objects.
Definition: dvector.cpp:42
int indexmax() const
Definition: fvar.hpp:4831
#define min(a, b)
Definition: cbivnorm.cpp:188
int lb
Definition: fvar.hpp:1904
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 safe_allocate(int, int)
Safely allocate memory for a dvector.
Definition: dvector.cpp:390
void shallow_copy(const dvector &)
Explicit shallow copy.
Definition: dvector.cpp:157
Description not yet available.
Definition: fvar.hpp:1901
void initialize(void)
Initialze all elements of dvector to zero.
Definition: dvect5.cpp:10
dvector & shift(int min)
Shift valid range of subscripts.
Definition: dvector.cpp:52
unsigned int ncopies
Copy counter to enable shallow copies.
Definition: vector_shapex.h:79
unsigned int size() const
Get number of elements in array.
Definition: dvector.h:209
int indexmin() const
Definition: fvar.hpp:2287
int index_min
minimum valid subscript
Definition: dvector.h:54
Holds &quot;shape&quot; information for vector objects.
Definition: vector_shapex.h:46
~dvector()
Default destructor.
Definition: dvector.cpp:65
vector_shapex * shape
pointer to vector &quot;shape&quot;
Definition: dvector.h:60
void shift(int min)
Description not yet available.
Definition: shape.cpp:33
#define max(a, b)
Definition: cbivnorm.cpp:189
int index_max
maximum valid subscript
Definition: dvector.h:55
double *& get_v(void)
Definition: dvector.h:148
int indexmax() const
Definition: fvar.hpp:2292
dvector * p
Definition: fvar.hpp:1903
double fourth(const double m)
Fourth power of a number; constant objects.
Definition: dvector.cpp:704
void deallocate(void)
Called by destructor to deallocate memory for a dvector object.
Definition: dvector.cpp:92
d3_array pow(const d3_array &m, int e)
Description not yet available.
Definition: d3arr6.cpp:17