ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
f1b2vc1.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id$
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   v(NULL), shape(NULL)
00592 {
00593   if (lb <= ub)
00594   {
00595     allocate(lb, ub);
00596   }
00597 }
00598 
00602 df1b2vector::df1b2vector(const dvector& v):
00603   v(NULL), shape(NULL)
00604 {
00605   if (v.allocated())
00606   {
00607     const int lb = v.indexmin();
00608     const int ub = v.indexmax();
00609     if (lb <= ub)
00610     {
00611       allocate(lb, ub);
00612       for (int i = lb; i <= ub; i++)
00613       {
00614         (*this)[i] = v[i];
00615       }
00616     }
00617   }
00618 }
00622 df1b2vector::df1b2vector():
00623   v(NULL), shape(NULL)
00624 {
00625 }
00626 
00631 void df1b2vector::allocate(void)
00632 {
00633   shape=0;
00634   v=0;
00635 }
00636 
00641 void df1b2vector::allocate(int lb,int ub,const char * s)
00642 {
00643   allocate(lb,ub);
00644 }
00645 
00650 void df1b2vector::allocate(const ad_integer& lb,const ad_integer& ub)
00651 {
00652   allocate(int(lb),int(ub));
00653 }
00654 
00659 void df1b2vector::allocate(int lb,int ub)
00660 {
00661   if (v)
00662   {
00663     deallocate();
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 = 0;
00690     if (mmax>mmin)
00691     {
00692       ind_1=adptr_diff((*this)(mmin+1).get_u(), (*this)(mmin).get_u());
00693     }
00694    /*
00695 #  if defined(SAFE_ALL)
00696     int i;
00697     for (i=mmin+1;i<mmax;i++)
00698     {
00699       int ind_i=ptrdiff_t((*this)(i+1).get_u())-ptrdiff_t((*this)(i).get_u());
00700       if (ind_i !=ind_1)
00701       {
00702         cerr << "Offset in df1b2vector constructor is not constant" << endl;
00703         ad_exit(1);
00704       }
00705       else
00706       {
00707         ind_1=ind_i;
00708       }
00709     }
00710 #endif
00711    */
00712   /*
00713     if (ind_1%sizeof(double))
00714     {
00715         cerr << "Offset in df1b2vector constructor is not"
00716           " multiple of sizeof(double)"  << endl;
00717         ad_exit(1);
00718     }
00719     offset=ind_1/sizeof(double);
00720   */
00721     offset=ind_1;
00722   }
00723   else
00724   {
00725     offset=0;
00726   }
00727 }
00728 
00733 void df1b2vector::noallocate(int lb,int ub)
00734 {
00735   index_min=lb;
00736   index_max=ub;
00737   int sz=ub-lb+1;
00738   df1b2variable::noallocate=1;
00739   AD_ALLOCATE(v,df1b2variable,sz,df1b2vector);
00740   df1b2variable::noallocate=0;
00741   if ( (shape=new vector_shapex(lb,ub,v)) == NULL)
00742   {
00743     cerr << "Error trying to allocate memory for df1b2vector\n";
00744     ad_exit(1);
00745   }
00746   v-=index_min;
00747   offset=0;
00748 }
00752 df1b2vector::~df1b2vector()
00753 {
00754   deallocate();
00755 }
00759 void df1b2vector::deallocate()
00760 {
00761   if (shape)
00762   {
00763     if (shape->ncopies)
00764     {
00765       (shape->ncopies)--;
00766     }
00767     else
00768     {
00769       if (shape->trueptr)
00770       {
00771         v = (df1b2variable*)shape->trueptr;
00772         delete [] v;
00773       }
00774 
00775       delete shape;
00776     }
00777     v = NULL;
00778     shape = NULL;
00779   }
00780 }
00784 df1b2vector::df1b2vector(const df1b2vector& x)
00785 {
00786  copy(x);
00787 }
00792 void df1b2vector::copy(const df1b2vector& _x)
00793 {
00794   ADUNCONST(df1b2vector,x)
00795   index_min=x.index_min;
00796   index_max=x.index_max;
00797   if (x.shape)
00798   {
00799     shape=x.shape;
00800     (shape->ncopies)++;
00801     v=x.v;
00802   }
00803   else
00804   {
00805     shape=0;
00806     v=0;
00807   }
00808 }
00809 
00814 df1b2vector::df1b2vector(const predf1b2vector& pdv)
00815 {
00816   shape=pdv.p->shape;
00817   if (shape)
00818   {
00819     (shape->ncopies)++;
00820   }
00821   else
00822   {
00823     cerr << "Taking a subvector  of an unallocated dvector"<<endl;
00824   }
00825   v = pdv.p->v;
00826   index_min=pdv.lb;
00827   index_max=pdv.ub;
00828 }
00829 
00834  void ADMB_getcallindex(const df1b2variable& x)
00835  {
00836    cout << "HERE" << endl;
00837  }
00838 
00843  void ADMB_getcallindex(const df1b2vector& x)
00844  {
00845    cout << "HERE" << endl;
00846  }
00847 
00852  void ADMB_getcallindex(const df1b2matrix& x)
00853  {
00854    cout << "HERE" << endl;
00855  }