ADMB Documentation  11.1.2192
 All Classes Files Functions Variables Typedefs Friends Defines
f1b2vc1.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: f1b2vc1.cpp 1938 2014-04-26 06:13:12Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #include <df1b2fun.h>
00012 #include "admb_messages.h"
00013 
00018 df1b2vector operator + (const df1b2vector& _x,const df1b2vector& _y)
00019 {
00020   ADUNCONST(df1b2vector,x)
00021   ADUNCONST(df1b2vector,y)
00022   check_shape(x,y,"operator +");
00023 
00024   int mmin=x.indexmin();
00025   int mmax=x.indexmax();
00026   df1b2vector tmp;
00027   tmp.noallocate(mmin,mmax);
00028 
00029   for (int i=mmin;i<=mmax;i++)
00030   {
00031     tmp(i)=x(i)+y(i);
00032   }
00033   return tmp;
00034 }
00035 
00040 df1b2vector operator + (const df1b2vector& _x,const dvector& _y)
00041 {
00042   ADUNCONST(df1b2vector,x)
00043   ADUNCONST(dvector,y)
00044   check_shape(x,y,"operator +");
00045 
00046   int mmin=x.indexmin();
00047   int mmax=x.indexmax();
00048   df1b2vector tmp;
00049   tmp.noallocate(mmin,mmax);
00050 
00051   for (int i=mmin;i<=mmax;i++)
00052   {
00053     tmp(i)=x(i)+y(i);
00054   }
00055   return tmp;
00056 }
00057 
00062 df1b2vector operator + (const dvector& _x,const df1b2vector& _y)
00063 {
00064   ADUNCONST(dvector,x)
00065   ADUNCONST(df1b2vector,y)
00066   check_shape(x,y,"operator +");
00067 
00068   int mmin=x.indexmin();
00069   int mmax=x.indexmax();
00070   df1b2vector tmp;
00071   tmp.noallocate(mmin,mmax);
00072 
00073   for (int i=mmin;i<=mmax;i++)
00074   {
00075     tmp(i)=x(i)+y(i);
00076   }
00077   return tmp;
00078 }
00079 
00084 df1b2vector exp(const df1b2vector& _x)
00085 {
00086   ADUNCONST(df1b2vector,x)
00087   int mmin=x.indexmin();
00088   int mmax=x.indexmax();
00089   df1b2vector tmp;
00090   tmp.noallocate(mmin,mmax);
00091 
00092   for (int i=mmin;i<=mmax;i++)
00093   {
00094     tmp(i)=exp(x(i));
00095   }
00096   return tmp;
00097 }
00098 
00103 df1b2vector log(const df1b2vector& _x)
00104 {
00105   ADUNCONST(df1b2vector,x)
00106   int mmin=x.indexmin();
00107   int mmax=x.indexmax();
00108   df1b2vector tmp;
00109   tmp.noallocate(mmin,mmax);
00110 
00111   for (int i=mmin;i<=mmax;i++)
00112   {
00113     tmp(i)=log(x(i));
00114   }
00115   return tmp;
00116 }
00117 
00122 df1b2vector sin(const df1b2vector& _x)
00123 {
00124   ADUNCONST(df1b2vector,x)
00125   int mmin=x.indexmin();
00126   int mmax=x.indexmax();
00127   df1b2vector tmp;
00128   tmp.noallocate(mmin,mmax);
00129 
00130   for (int i=mmin;i<=mmax;i++)
00131   {
00132     tmp(i)=sin(x(i));
00133   }
00134   return tmp;
00135 }
00136 
00141 df1b2vector tan(const df1b2vector& _x)
00142 {
00143   ADUNCONST(df1b2vector,x)
00144   int mmin=x.indexmin();
00145   int mmax=x.indexmax();
00146   df1b2vector tmp;
00147   tmp.noallocate(mmin,mmax);
00148 
00149   for (int i=mmin;i<=mmax;i++)
00150   {
00151     tmp(i)=tan(x(i));
00152   }
00153   return tmp;
00154 }
00155 
00160 df1b2vector sqrt(const df1b2vector& _x)
00161 {
00162   ADUNCONST(df1b2vector,x)
00163   int mmin=x.indexmin();
00164   int mmax=x.indexmax();
00165   df1b2vector tmp;
00166   tmp.noallocate(mmin,mmax);
00167 
00168   for (int i=mmin;i<=mmax;i++)
00169   {
00170     tmp(i)=sqrt(x(i));
00171   }
00172   return tmp;
00173 }
00174 
00179 df1b2vector cos(const df1b2vector& _x)
00180 {
00181   ADUNCONST(df1b2vector,x)
00182   int mmin=x.indexmin();
00183   int mmax=x.indexmax();
00184   df1b2vector tmp;
00185   tmp.noallocate(mmin,mmax);
00186 
00187   for (int i=mmin;i<=mmax;i++)
00188   {
00189     tmp(i)=cos(x(i));
00190   }
00191   return tmp;
00192 }
00193 
00198 df1b2vector operator - (const df1b2vector& _x,const df1b2vector& _y)
00199 {
00200   ADUNCONST(df1b2vector,x)
00201   ADUNCONST(df1b2vector,y)
00202   check_shape(x,y,"operator -");
00203 
00204   int mmin=x.indexmin();
00205   int mmax=x.indexmax();
00206   df1b2vector tmp;
00207   tmp.noallocate(mmin,mmax);
00208 
00209   for (int i=mmin;i<=mmax;i++)
00210   {
00211     tmp(i)=x(i)-y(i);
00212   }
00213   return tmp;
00214 }
00215 
00220 df1b2vector operator - (const df1b2vector& _x,const dvector& _y)
00221 {
00222   ADUNCONST(df1b2vector,x)
00223   ADUNCONST(dvector,y)
00224   check_shape(x,y,"operator -");
00225 
00226   int mmin=x.indexmin();
00227   int mmax=x.indexmax();
00228   df1b2vector tmp;
00229   tmp.noallocate(mmin,mmax);
00230 
00231   for (int i=mmin;i<=mmax;i++)
00232   {
00233     tmp(i)=x(i)-y(i);
00234   }
00235   return tmp;
00236 }
00237 
00242 df1b2vector operator - (const dvector& _x,const df1b2vector& _y)
00243 {
00244   ADUNCONST(dvector,x)
00245   ADUNCONST(df1b2vector,y)
00246   check_shape(x,y,"operator -");
00247 
00248   int mmin=x.indexmin();
00249   int mmax=x.indexmax();
00250   df1b2vector tmp;
00251   tmp.noallocate(mmin,mmax);
00252 
00253   for (int i=mmin;i<=mmax;i++)
00254   {
00255     tmp(i)=x(i)-y(i);
00256   }
00257   return tmp;
00258 }
00259 
00264 df1b2vector& df1b2vector::operator += (const df1b2vector& _x)
00265 {
00266   ADUNCONST(df1b2vector,x)
00267   check_shape(*this,x,"operator +=");
00268 
00269   int mmin=x.indexmin();
00270   int mmax=x.indexmax();
00271 
00272   for (int i=mmin;i<=mmax;i++)
00273   {
00274     (*this)(i)+=x(i);
00275   }
00276   return *this;
00277 }
00278 
00283 df1b2vector& df1b2vector::operator += (const dvector& _x)
00284 {
00285   ADUNCONST(dvector,x)
00286   check_shape(*this,x,"operator +=");
00287 
00288   int mmin=x.indexmin();
00289   int mmax=x.indexmax();
00290 
00291   for (int i=mmin;i<=mmax;i++)
00292   {
00293     (*this)(i)+=x(i);
00294   }
00295   return *this;
00296 }
00297 
00302 df1b2vector& df1b2vector::operator -= (const df1b2vector& _x)
00303 {
00304   ADUNCONST(df1b2vector,x)
00305   check_shape(*this,x,"operator -");
00306 
00307   int mmin=x.indexmin();
00308   int mmax=x.indexmax();
00309 
00310   for (int i=mmin;i<=mmax;i++)
00311   {
00312     (*this)(i)-=x(i);
00313   }
00314   return *this;
00315 }
00316 
00321 df1b2vector& df1b2vector::operator -= (const dvector& _x)
00322 {
00323    ADUNCONST(dvector,x)
00324    check_shape(*this,x,"operator -");
00325 
00326    int mmin=x.indexmin();
00327    int mmax=x.indexmax();
00328 
00329    for (int i=mmin;i<=mmax;i++)
00330    {
00331      (*this)(i)-=x(i);
00332    }
00333    return *this;
00334 }
00335 
00340 df1b2vector& df1b2vector::operator *= (const df1b2variable& _x)
00341 {
00342   ADUNCONST(df1b2variable,x)
00343   int mmin=indexmin();
00344   int mmax=indexmax();
00345 
00346   for (int i=mmin;i<=mmax;i++)
00347   {
00348     (*this)(i)*=x;
00349   }
00350   return *this;
00351 }
00352 
00357 df1b2vector& df1b2vector::operator -= (const df1b2variable& _x)
00358 {
00359   ADUNCONST(df1b2variable,x)
00360   int mmin=indexmin();
00361   int mmax=indexmax();
00362 
00363   for (int i=mmin;i<=mmax;i++)
00364   {
00365     (*this)(i)-=x;
00366   }
00367   return *this;
00368 }
00369 
00374 df1b2vector& df1b2vector::operator += (const df1b2variable& _x)
00375 {
00376   ADUNCONST(df1b2variable,x)
00377   int mmin=indexmin();
00378   int mmax=indexmax();
00379 
00380   for (int i=mmin;i<=mmax;i++)
00381   {
00382     (*this)(i)+=x;
00383   }
00384   return *this;
00385 }
00386 
00391 df1b2vector& df1b2vector::operator += (double x)
00392 {
00393   int mmin=indexmin();
00394   int mmax=indexmax();
00395 
00396   for (int i=mmin;i<=mmax;i++)
00397   {
00398     (*this)(i)+=x;
00399   }
00400   return *this;
00401 }
00402 
00407 df1b2vector& df1b2vector::operator *= (const df1b2vector& _x)
00408 {
00409   ADUNCONST(df1b2vector,x)
00410   check_shape(*this,x,"operator *");
00411 
00412   int mmin=x.indexmin();
00413   int mmax=x.indexmax();
00414 
00415   for (int i=mmin;i<=mmax;i++)
00416   {
00417     (*this)(i)*=x(i);
00418   }
00419   return *this;
00420 }
00421 
00426 df1b2vector& df1b2vector::operator /= (const df1b2vector& _x)
00427 {
00428   ADUNCONST(df1b2vector,x)
00429   check_shape(*this,x,"operator /");
00430 
00431   int mmin=x.indexmin();
00432   int mmax=x.indexmax();
00433 
00434   for (int i=mmin;i<=mmax;i++)
00435   {
00436     (*this)(i)/=x(i);
00437   }
00438   return *this;
00439 }
00440 
00445 df1b2vector& df1b2vector::operator /= (const df1b2variable& _x)
00446 {
00447   ADUNCONST(df1b2variable,x)
00448   int mmin=indexmin();
00449   int mmax=indexmax();
00450 
00451   for (int i=mmin;i<=mmax;i++)
00452   {
00453     (*this)(i)/=x;
00454   }
00455   return *this;
00456 }
00457 
00458 #if !defined(OPT_LIB)
00459 
00463 df1b2variable& df1b2matrix::operator () (int i,int j) const
00464 {
00465   if (i < indexmin())
00466   {
00467     ADMB_ARRAY_BOUNDS_ERROR("Index too low",
00468     "df1b2variable& df1b2matrix::operator () (int i, int j) const",
00469     indexmin(), indexmax(), i);
00470   }
00471   if (i > indexmax())
00472   {
00473     ADMB_ARRAY_BOUNDS_ERROR("Index too high",
00474     "df1b2variable& df1b2matrix::operator () (int i, int j) const",
00475     indexmin(), indexmax(), i);
00476   }
00477   return (df1b2variable&)(v[i][j]);
00478 }
00479 
00484 df1b2vector& df1b2matrix::operator [] (int i) const
00485 {
00486   if (i < indexmin())
00487   {
00488     ADMB_ARRAY_BOUNDS_ERROR("Index too low",
00489     "df1b2vector& df1b2matrix::operator [] (int i) const",
00490     indexmin(), indexmax(), i);
00491   }
00492   if (i > indexmax())
00493   {
00494     ADMB_ARRAY_BOUNDS_ERROR("Index too high",
00495     "df1b2vector& df1b2matrix::operator [] (int i) const",
00496     indexmin(), indexmax(), i);
00497   }
00498   return (df1b2vector&)(v[i]);
00499 }
00500 
00505 df1b2vector& df1b2matrix::operator () (int i) const
00506 {
00507   if (i < indexmin())
00508   {
00509     ADMB_ARRAY_BOUNDS_ERROR("Index too low",
00510     "df1b2vector& df1b2matrix::operator () (int i) const",
00511     indexmin(), indexmax(), i);
00512   }
00513   if (i > indexmax())
00514   {
00515     ADMB_ARRAY_BOUNDS_ERROR("Index too high",
00516     "df1b2vector& df1b2matrix::operator () (int i) const",
00517     indexmin(), indexmax(), i);
00518   }
00519   return (df1b2vector&)(v[i]);
00520 }
00521 
00526 df1b2variable& df1b2vector::operator () (int i) const
00527 {
00528   if (i < indexmin())
00529   {
00530     ADMB_ARRAY_BOUNDS_ERROR("Index too low",
00531     "df1b2variable& df1b2vector::operator () (int i) const",
00532     indexmin(), indexmax(), i);
00533   }
00534   if (i > indexmax())
00535   {
00536     ADMB_ARRAY_BOUNDS_ERROR("Index too high",
00537     "df1b2variable& df1b2vector::operator () (int i) const",
00538     indexmin(), indexmax(), i);
00539   }
00540   return *(v+i);
00541   //return *((df1b2variable*)((char*)(v)+i*pointersize()));
00542 }
00543 /*
00544 const df1b2variable& df1b2vector::operator () (int i) const
00545 {
00546   if (i<indexmin())
00547   {
00548     cerr << "Index too low in df1b2variable& operator () (int i)"
00549       " value = " << i << endl;
00550     ad_exit(1);
00551   }
00552   if (i>indexmax())
00553   {
00554     cerr << "Index too high in df1b2variable& operator () (int i)"
00555       " value = " << i << endl;
00556     ad_exit(1);
00557   }
00558   //return *(v+i);
00559   return *((df1b2variable*)((char*)(v)+i*pointersize()));
00560 }
00561 */
00562 
00567 df1b2variable& df1b2vector::operator [] (int i) const
00568 {
00569   if (i < indexmin())
00570   {
00571     ADMB_ARRAY_BOUNDS_ERROR("Index too low",
00572     "df1b2variable& df1b2vector::operator [] (int i) const",
00573     indexmin(), indexmax(), i);
00574   }
00575   if (i > indexmax())
00576   {
00577     ADMB_ARRAY_BOUNDS_ERROR("Index too high",
00578     "df1b2variable& df1b2vector::operator [] (int i) const",
00579     indexmin(), indexmax(), i);
00580   }
00581   //return *(v+i);
00582   return *((df1b2variable*)((char*)(v)+i*pointersize()));
00583 }
00584 #endif
00585 
00590 df1b2vector::df1b2vector(int lb,int ub)
00591 {
00592   if (lb<=ub)
00593     allocate(lb,ub);
00594   else
00595     allocate();
00596 }
00597 
00601 df1b2vector::df1b2vector(const dvector& v)
00602 {
00603   if (!v)
00604   {
00605     allocate();
00606   }
00607   else
00608   {
00609     const int lb = v.indexmin();
00610     const int ub = v.indexmax();
00611     if (lb<=ub)
00612     {
00613       allocate(lb,ub);
00614       for (int i = lb; i <= ub; i++)
00615       {
00616         (*this)[i] = v[i];
00617       }
00618     }
00619   }
00620 }
00621 
00626 df1b2vector::df1b2vector(void)
00627 {
00628   allocate();
00629 }
00630 
00635 void df1b2vector::allocate(void)
00636 {
00637   shape=0;
00638   v=0;
00639 }
00640 
00645 void df1b2vector::allocate(int lb,int ub,const char * s)
00646 {
00647   allocate(lb,ub);
00648 }
00649 
00654 void df1b2vector::allocate(const ad_integer& lb,const ad_integer& ub)
00655 {
00656   allocate(int(lb),int(ub));
00657 }
00658 
00663 void df1b2vector::allocate(int lb,int ub)
00664 {
00665   index_min=lb;
00666   index_max=ub;
00667   int sz=ub-lb+1;
00668   if (index_min>index_max)
00669   {
00670     shape=0;
00671     v=0;
00672     sz=0;
00673   }
00674   else
00675   {
00676     //AD_ALLOCATE(v,df1b2variable,sz,df1b2vector);
00677     v=new df1b2variable[sz];
00678     if ( (shape=new vector_shapex(lb,ub,v)) == NULL)
00679     {
00680       cerr << "Error trying to allocate memory for df1b2vector\n";
00681       ad_exit(1);
00682     }
00683     v-=index_min;
00684   }
00685   if (sz>0)
00686   {
00687     int mmin=indexmin();
00688     int mmax=indexmax();
00689     int ind_1;
00690     if (mmax>mmin)
00691     {
00692       ind_1=ptrdiff_t((*this)(mmin+1).get_u())
00693         -ptrdiff_t((*this)(mmin).get_u());
00694     }
00695     else
00696     {
00697       ind_1=0;
00698     }
00699    /*
00700 #  if defined(SAFE_ALL)
00701     int i;
00702     for (i=mmin+1;i<mmax;i++)
00703     {
00704       int ind_i=ptrdiff_t((*this)(i+1).get_u())-ptrdiff_t((*this)(i).get_u());
00705       if (ind_i !=ind_1)
00706       {
00707         cerr << "Offset in df1b2vector constructor is not constant" << endl;
00708         ad_exit(1);
00709       }
00710       else
00711       {
00712         ind_1=ind_i;
00713       }
00714     }
00715 #endif
00716    */
00717   /*
00718     if (ind_1%sizeof(double))
00719     {
00720         cerr << "Offset in df1b2vector constructor is not"
00721           " multiple of sizeof(double)"  << endl;
00722         ad_exit(1);
00723     }
00724     offset=ind_1/sizeof(double);
00725   */
00726     offset=ind_1;
00727   }
00728   else
00729   {
00730     offset=0;
00731   }
00732 }
00733 
00738 void df1b2vector::noallocate(int lb,int ub)
00739 {
00740   index_min=lb;
00741   index_max=ub;
00742   int sz=ub-lb+1;
00743   df1b2variable::noallocate=1;
00744   AD_ALLOCATE(v,df1b2variable,sz,df1b2vector);
00745   df1b2variable::noallocate=0;
00746   if ( (shape=new vector_shapex(lb,ub,v)) == NULL)
00747   {
00748     cerr << "Error trying to allocate memory for df1b2vector\n";
00749     ad_exit(1);
00750   }
00751   v-=index_min;
00752   offset=0;
00753 }
00754 
00759 df1b2vector::~df1b2vector()
00760 {
00761   deallocate();
00762 }
00763 
00768 void df1b2vector::deallocate()
00769 {
00770   if (shape)
00771   {
00772     if (shape->ncopies)
00773     {
00774       (shape->ncopies)--;
00775     }
00776     else
00777     {
00778       v = (df1b2variable*) shape->trueptr;
00779       delete [] v;
00780       v=NULL;
00781       delete shape;
00782       shape=0;
00783     }
00784   }
00785 }
00786 
00791 df1b2vector::df1b2vector(const df1b2vector& _x)
00792 {
00793  copy(_x);
00794 }
00795 
00796 
00801 void df1b2vector::copy(const df1b2vector& _x)
00802 {
00803   ADUNCONST(df1b2vector,x)
00804   index_min=x.index_min;
00805   index_max=x.index_max;
00806   if (x.shape)
00807   {
00808     shape=x.shape;
00809     (shape->ncopies)++;
00810     v=x.v;
00811   }
00812   else
00813   {
00814     shape=0;
00815     v=0;
00816   }
00817 }
00818 
00823  df1b2vector::df1b2vector(const predf1b2vector& pdv)
00824  {
00825    shape=pdv.p->shape;
00826    if (shape)
00827    {
00828      (shape->ncopies)++;
00829    }
00830    else
00831    {
00832      cerr << "Taking a subvector  of an unallocated dvector"<<endl;
00833    }
00834    v = pdv.p->v;
00835    index_min=pdv.lb;
00836    index_max=pdv.ub;
00837  }
00838 
00843  void ADMB_getcallindex(const df1b2variable& x)
00844  {
00845    cout << "HERE" << endl;
00846  }
00847 
00852  void ADMB_getcallindex(const df1b2vector& x)
00853  {
00854    cout << "HERE" << endl;
00855  }
00856 
00861  void ADMB_getcallindex(const df1b2matrix& x)
00862  {
00863    cout << "HERE" << endl;
00864  }