ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
f1b2vc1.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 <df1b2fun.h>
12 #include "admb_messages.h"
13 
19 {
22  check_shape(x,y,"operator +");
23 
24  int mmin=x.indexmin();
25  int mmax=x.indexmax();
26  df1b2vector tmp;
27  tmp.noallocate(mmin,mmax);
28 
29  for (int i=mmin;i<=mmax;i++)
30  {
31  tmp(i)=x(i)+y(i);
32  }
33  return tmp;
34 }
35 
41 {
44  check_shape(x,y,"operator +");
45 
46  int mmin=x.indexmin();
47  int mmax=x.indexmax();
48  df1b2vector tmp;
49  tmp.noallocate(mmin,mmax);
50 
51  for (int i=mmin;i<=mmax;i++)
52  {
53  tmp(i)=x(i)+y(i);
54  }
55  return tmp;
56 }
57 
63 {
66  check_shape(x,y,"operator +");
67 
68  int mmin=x.indexmin();
69  int mmax=x.indexmax();
70  df1b2vector tmp;
71  tmp.noallocate(mmin,mmax);
72 
73  for (int i=mmin;i<=mmax;i++)
74  {
75  tmp(i)=x(i)+y(i);
76  }
77  return tmp;
78 }
79 
85 {
87  int mmin=x.indexmin();
88  int mmax=x.indexmax();
89  df1b2vector tmp;
90  tmp.noallocate(mmin,mmax);
91 
92  for (int i=mmin;i<=mmax;i++)
93  {
94  tmp(i)=exp(x(i));
95  }
96  return tmp;
97 }
98 
104 {
106  int mmin=x.indexmin();
107  int mmax=x.indexmax();
108  df1b2vector tmp;
109  tmp.noallocate(mmin,mmax);
110 
111  for (int i=mmin;i<=mmax;i++)
112  {
113  tmp(i)=log(x(i));
114  }
115  return tmp;
116 }
117 
123 {
125  int mmin=x.indexmin();
126  int mmax=x.indexmax();
127  df1b2vector tmp;
128  tmp.noallocate(mmin,mmax);
129 
130  for (int i=mmin;i<=mmax;i++)
131  {
132  tmp(i)=sin(x(i));
133  }
134  return tmp;
135 }
136 
142 {
144  int mmin=x.indexmin();
145  int mmax=x.indexmax();
146  df1b2vector tmp;
147  tmp.noallocate(mmin,mmax);
148 
149  for (int i=mmin;i<=mmax;i++)
150  {
151  tmp(i)=tan(x(i));
152  }
153  return tmp;
154 }
155 
161 {
163  int mmin=x.indexmin();
164  int mmax=x.indexmax();
165  df1b2vector tmp;
166  tmp.noallocate(mmin,mmax);
167 
168  for (int i=mmin;i<=mmax;i++)
169  {
170  tmp(i)=sqrt(x(i));
171  }
172  return tmp;
173 }
174 
180 {
182  int mmin=x.indexmin();
183  int mmax=x.indexmax();
184  df1b2vector tmp;
185  tmp.noallocate(mmin,mmax);
186 
187  for (int i=mmin;i<=mmax;i++)
188  {
189  tmp(i)=cos(x(i));
190  }
191  return tmp;
192 }
193 
199 {
202  check_shape(x,y,"operator -");
203 
204  int mmin=x.indexmin();
205  int mmax=x.indexmax();
206  df1b2vector tmp;
207  tmp.noallocate(mmin,mmax);
208 
209  for (int i=mmin;i<=mmax;i++)
210  {
211  tmp(i)=x(i)-y(i);
212  }
213  return tmp;
214 }
215 
221 {
223  ADUNCONST(dvector,y)
224  check_shape(x,y,"operator -");
225 
226  int mmin=x.indexmin();
227  int mmax=x.indexmax();
228  df1b2vector tmp;
229  tmp.noallocate(mmin,mmax);
230 
231  for (int i=mmin;i<=mmax;i++)
232  {
233  tmp(i)=x(i)-y(i);
234  }
235  return tmp;
236 }
237 
243 {
246  check_shape(x,y,"operator -");
247 
248  int mmin=x.indexmin();
249  int mmax=x.indexmax();
250  df1b2vector tmp;
251  tmp.noallocate(mmin,mmax);
252 
253  for (int i=mmin;i<=mmax;i++)
254  {
255  tmp(i)=x(i)-y(i);
256  }
257  return tmp;
258 }
259 
265 {
267  check_shape(*this,x,"operator +=");
268 
269  int mmin=x.indexmin();
270  int mmax=x.indexmax();
271 
272  for (int i=mmin;i<=mmax;i++)
273  {
274  (*this)(i)+=x(i);
275  }
276  return *this;
277 }
278 
284 {
286  check_shape(*this,x,"operator +=");
287 
288  int mmin=x.indexmin();
289  int mmax=x.indexmax();
290 
291  for (int i=mmin;i<=mmax;i++)
292  {
293  (*this)(i)+=x(i);
294  }
295  return *this;
296 }
297 
303 {
305  check_shape(*this,x,"operator -");
306 
307  int mmin=x.indexmin();
308  int mmax=x.indexmax();
309 
310  for (int i=mmin;i<=mmax;i++)
311  {
312  (*this)(i)-=x(i);
313  }
314  return *this;
315 }
316 
322 {
324  check_shape(*this,x,"operator -");
325 
326  int mmin=x.indexmin();
327  int mmax=x.indexmax();
328 
329  for (int i=mmin;i<=mmax;i++)
330  {
331  (*this)(i)-=x(i);
332  }
333  return *this;
334 }
335 
341 {
343  int mmin=indexmin();
344  int mmax=indexmax();
345 
346  for (int i=mmin;i<=mmax;i++)
347  {
348  (*this)(i)*=x;
349  }
350  return *this;
351 }
352 
358 {
360  int mmin=indexmin();
361  int mmax=indexmax();
362 
363  for (int i=mmin;i<=mmax;i++)
364  {
365  (*this)(i)-=x;
366  }
367  return *this;
368 }
369 
375 {
377  int mmin=indexmin();
378  int mmax=indexmax();
379 
380  for (int i=mmin;i<=mmax;i++)
381  {
382  (*this)(i)+=x;
383  }
384  return *this;
385 }
386 
392 {
393  int mmin=indexmin();
394  int mmax=indexmax();
395 
396  for (int i=mmin;i<=mmax;i++)
397  {
398  (*this)(i)+=x;
399  }
400  return *this;
401 }
402 
408 {
410  check_shape(*this,x,"operator *");
411 
412  int mmin=x.indexmin();
413  int mmax=x.indexmax();
414 
415  for (int i=mmin;i<=mmax;i++)
416  {
417  (*this)(i)*=x(i);
418  }
419  return *this;
420 }
421 
427 {
429  check_shape(*this,x,"operator /");
430 
431  int mmin=x.indexmin();
432  int mmax=x.indexmax();
433 
434  for (int i=mmin;i<=mmax;i++)
435  {
436  (*this)(i)/=x(i);
437  }
438  return *this;
439 }
440 
446 {
448  int mmin=indexmin();
449  int mmax=indexmax();
450 
451  for (int i=mmin;i<=mmax;i++)
452  {
453  (*this)(i)/=x;
454  }
455  return *this;
456 }
457 
458 #if !defined(OPT_LIB)
459 
463 df1b2variable& df1b2matrix::operator () (int i,int j) const
464 {
465  if (i < indexmin())
466  {
467  ADMB_ARRAY_BOUNDS_ERROR("Index too low",
468  "df1b2variable& df1b2matrix::operator () (int i, int j) const",
469  indexmin(), indexmax(), i);
470  }
471  if (i > indexmax())
472  {
473  ADMB_ARRAY_BOUNDS_ERROR("Index too high",
474  "df1b2variable& df1b2matrix::operator () (int i, int j) const",
475  indexmin(), indexmax(), i);
476  }
477  return (df1b2variable&)(v[i][j]);
478 }
479 
485 {
486  if (i < indexmin())
487  {
488  ADMB_ARRAY_BOUNDS_ERROR("Index too low",
489  "df1b2vector& df1b2matrix::operator [] (int i) const",
490  indexmin(), indexmax(), i);
491  }
492  if (i > indexmax())
493  {
494  ADMB_ARRAY_BOUNDS_ERROR("Index too high",
495  "df1b2vector& df1b2matrix::operator [] (int i) const",
496  indexmin(), indexmax(), i);
497  }
498  return (df1b2vector&)(v[i]);
499 }
500 
506 {
507  if (i < indexmin())
508  {
509  ADMB_ARRAY_BOUNDS_ERROR("Index too low",
510  "df1b2vector& df1b2matrix::operator () (int i) const",
511  indexmin(), indexmax(), i);
512  }
513  if (i > indexmax())
514  {
515  ADMB_ARRAY_BOUNDS_ERROR("Index too high",
516  "df1b2vector& df1b2matrix::operator () (int i) const",
517  indexmin(), indexmax(), i);
518  }
519  return (df1b2vector&)(v[i]);
520 }
521 
527 {
528  if (i < indexmin())
529  {
530  ADMB_ARRAY_BOUNDS_ERROR("Index too low",
531  "df1b2variable& df1b2vector::operator () (int i) const",
532  indexmin(), indexmax(), i);
533  }
534  if (i > indexmax())
535  {
536  ADMB_ARRAY_BOUNDS_ERROR("Index too high",
537  "df1b2variable& df1b2vector::operator () (int i) const",
538  indexmin(), indexmax(), i);
539  }
540  return *(v+i);
541  //return *((df1b2variable*)((char*)(v)+i*pointersize()));
542 }
543 /*
544 const df1b2variable& df1b2vector::operator () (int i) const
545 {
546  if (i<indexmin())
547  {
548  cerr << "Index too low in df1b2variable& operator () (int i)"
549  " value = " << i << endl;
550  ad_exit(1);
551  }
552  if (i>indexmax())
553  {
554  cerr << "Index too high in df1b2variable& operator () (int i)"
555  " value = " << i << endl;
556  ad_exit(1);
557  }
558  //return *(v+i);
559  return *((df1b2variable*)((char*)(v)+i*pointersize()));
560 }
561 */
562 
568 {
569  if (i < indexmin())
570  {
571  ADMB_ARRAY_BOUNDS_ERROR("Index too low",
572  "df1b2variable& df1b2vector::operator [] (int i) const",
573  indexmin(), indexmax(), i);
574  }
575  if (i > indexmax())
576  {
577  ADMB_ARRAY_BOUNDS_ERROR("Index too high",
578  "df1b2variable& df1b2vector::operator [] (int i) const",
579  indexmin(), indexmax(), i);
580  }
581  //return *(v+i);
582  return *((df1b2variable*)((char*)(v)+i*pointersize()));
583 }
584 #endif
585 
590 df1b2vector::df1b2vector(int lb, int ub):
591  v(NULL), shape(NULL)
592 {
593  if (lb <= ub)
594  {
595  allocate(lb, ub);
596  }
597 }
598 
603  v(NULL), shape(NULL)
604 {
605  if (v.allocated())
606  {
607  const int lb = v.indexmin();
608  const int ub = v.indexmax();
609  if (lb <= ub)
610  {
611  allocate(lb, ub);
612  for (int i = lb; i <= ub; i++)
613  {
614  (*this)[i] = v[i];
615  }
616  }
617  }
618 }
621  v(NULL), shape(NULL)
622 {
623  index_min = 1;
624  index_max = 0;
625 }
628 {
629  shape=0;
630  v=0;
631  index_min = 1;
632  index_max = 0;
633 }
634 
639 void df1b2vector::allocate(int lb,int ub,const char * s)
640 {
641  allocate(lb,ub);
642 }
643 
648 void df1b2vector::allocate(const ad_integer& lb,const ad_integer& ub)
649 {
650  allocate(int(lb),int(ub));
651 }
652 
659 void df1b2vector::allocate(int lb, int ub)
660 {
661  if (v)
662  {
663  deallocate();
664  }
665  index_min = lb;
666  index_max = ub;
667  int sz = ub < lb ? 0 : ub - lb + 1;
668  if (index_min > index_max)
669  {
670  shape = 0;
671  v = 0;
672  sz = 0;
673  }
674  else
675  {
676  //AD_ALLOCATE(v,df1b2variable,sz,df1b2vector);
677  v=new df1b2variable[static_cast<unsigned int>(sz)];
678  if ( (shape=new vector_shapex(lb,ub,v)) == NULL)
679  {
680  cerr << "Error trying to allocate memory for df1b2vector\n";
681  ad_exit(1);
682  }
683  v-=index_min;
684  }
685  if (sz>0)
686  {
687  int mmin = indexmin();
688  int mmax = indexmax();
689  int ind_1 = 0;
690  if (mmax > mmin)
691  {
692  ind_1 = adptr_diff((*this)(mmin+1).get_u(), (*this)(mmin).get_u());
693  }
694  /*
695 # if defined(SAFE_ALL)
696  int i;
697  for (i=mmin+1;i<mmax;i++)
698  {
699  int ind_i=ptrdiff_t((*this)(i+1).get_u())-ptrdiff_t((*this)(i).get_u());
700  if (ind_i !=ind_1)
701  {
702  cerr << "Offset in df1b2vector constructor is not constant" << endl;
703  ad_exit(1);
704  }
705  else
706  {
707  ind_1=ind_i;
708  }
709  }
710 #endif
711  */
712  /*
713  if (ind_1%sizeof(double))
714  {
715  cerr << "Offset in df1b2vector constructor is not"
716  " multiple of sizeof(double)" << endl;
717  ad_exit(1);
718  }
719  offset=ind_1/sizeof(double);
720  */
721  offset = ind_1;
722  }
723  else
724  {
725  offset = 0;
726  }
727 }
728 
733 void df1b2vector::noallocate(int lb, int ub)
734 {
735  index_min = lb;
736  index_max = ub;
737  unsigned int sz = static_cast<unsigned int>(ub < lb ? 0 : ub - lb + 1);
741  if ((shape = new vector_shapex(lb, ub, v)) == NULL)
742  {
743  cerr << "Error trying to allocate memory for df1b2vector\n";
744  ad_exit(1);
745  }
746  v -= index_min;
747  offset = 0;
748 }
753 {
754  deallocate();
755 }
760 {
761  if (shape)
762  {
763  if (shape->ncopies)
764  {
765  (shape->ncopies)--;
766  }
767  else
768  {
769  if (shape->trueptr)
770  {
772  delete [] v;
773  }
774 
775  delete shape;
776  }
777  allocate();
778  }
779 }
784 {
785  copy(x);
786 }
792 {
794  index_min=x.index_min;
795  index_max=x.index_max;
796  if (x.shape)
797  {
798  shape=x.shape;
799  (shape->ncopies)++;
800  v=x.v;
801  }
802  else
803  {
804  shape=0;
805  v=0;
806  }
807 }
808 
814 {
815  shape=pdv.p->shape;
816  if (shape)
817  {
818  (shape->ncopies)++;
819  }
820  else
821  {
822  cerr << "Taking a subvector of an unallocated dvector"<<endl;
823  }
824  v = pdv.p->v;
825  index_min=pdv.lb;
826  index_max=pdv.ub;
827 }
828 
834  {
835  cout << "HERE" << endl;
836  }
837 
843  {
844  cout << "HERE" << endl;
845  }
846 
852  {
853  cout << "HERE" << endl;
854  }
d3_array tan(const d3_array &arr3)
Returns d3_array results with computed tan from elements in arr3.
Definition: d3arr2a.cpp:73
void * trueptr
Address of first element in the vector.
Definition: vector_shapex.h:82
df1b2vector & operator[](int i) const
Definition: df1b2fun.h:1102
int allocated(void) const
Returns 1 (TRUE) if memory is allocated.
Definition: dvector.h:71
int pointersize() const
Definition: df1b2fun.h:966
void ADMB_getcallindex(int)
Definition: fvar.hpp:9243
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
#define ADUNCONST(type, obj)
Creates a shallow copy of obj that is not CONST.
Definition: fvar.hpp:140
Vector of double precision numbers.
Definition: dvector.h:50
int indexmin(void) const
Definition: df1b2fun.h:1054
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
static int noallocate
Definition: df1b2fun.h:294
void copy(const df1b2vector &)
Description not yet available.
Definition: f1b2vc1.cpp:791
Description not yet available.
Definition: df1b2fun.h:953
int adptr_diff(void *x, void *y)
Definition: df1b2fn2.cpp:110
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
d3_array sin(const d3_array &arr3)
Returns d3_array results with computed sin from elements in arr3.
Definition: d3arr2a.cpp:43
exitptr ad_exit
Definition: gradstrc.cpp:53
#define AD_ALLOCATE(ptr, type, n, classname)
Definition: df1b2fun.h:179
int indexmin(void) const
Definition: df1b2fun.h:969
df1b2variable & operator()(int i) const
Definition: df1b2fun.h:994
df1b2variable & operator()(int i, int j) const
Definition: df1b2fun.h:1098
df1b2vector * v
Definition: df1b2fun.h:1046
Description not yet available.
Definition: df1b2fun.h:266
prnstream & endl(prnstream &)
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2c.cpp:11
void noallocate(int lib, int ub)
Description not yet available.
Definition: f1b2vc1.cpp:733
vector_shapex * shape
Definition: df1b2fun.h:956
~df1b2vector()
Destructor.
Definition: f1b2vc1.cpp:752
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
d3_array exp(const d3_array &arr3)
Returns d3_array results with computed exp from elements in arr3.
Definition: d3arr2a.cpp:28
void deallocate(void)
If no other copies exists, free allocated memory.
Definition: f1b2vc1.cpp:759
unsigned int ncopies
Copy counter to enable shallow copies.
Definition: vector_shapex.h:79
#define ADMB_ARRAY_BOUNDS_ERROR(message, function, lower_bounds, upper_bounds, index)
Definition: admb_messages.h:47
void check_shape(const df1b2vector &x, const df1b2vector &y, const char *s)
Description not yet available.
Definition: f1b2vc5.cpp:30
df1b2variable & operator[](int i) const
Definition: df1b2fun.h:998
Holds &quot;shape&quot; information for vector objects.
Definition: vector_shapex.h:46
df1b2vector(void)
Default constructor.
Definition: f1b2vc1.cpp:620
d3_array cos(const d3_array &arr3)
Returns d3_array results with computed cos from elements in arr3.
Definition: d3arr2a.cpp:58
df1b2vector & operator/=(const df1b2vector &x)
Description not yet available.
Definition: f1b2vc1.cpp:426
Description not yet available.
Definition: df1b2fun.h:1042
int indexmax(void) const
Definition: df1b2fun.h:970
df1b2vector & operator*=(const df1b2vector &x)
Description not yet available.
Definition: f1b2vc1.cpp:407
df1b2vector * p
Definition: df1b2fun.h:249
int indexmax(void) const
Definition: df1b2fun.h:1055
Description not yet available.
df1b2variable * v
Definition: df1b2fun.h:955
Stores integer.
Definition: fvar.hpp:7654
Description not yet available.
Definition: df1b2fun.h:247
void allocate(void)
Initialize df1b2vector to empty.
Definition: f1b2vc1.cpp:627
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2a.cpp:13
df1b2vector & operator-=(const df1b2vector &x)
Description not yet available.
Definition: f1b2vc1.cpp:302
df1b2vector & operator+=(const df1b2vector &x)
Description not yet available.
Definition: f1b2vc1.cpp:264