ADMB Documentation  11.1.1015
 All Classes Files Functions Variables Typedefs Friends Defines
d5arr.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: d5arr.cpp 542 2012-07-10 21:04:06Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California 
00006  */
00011 #include "fvar.hpp"
00012 #include "admb_messages.h"
00013 //#include <d4arr.hpp>
00014 
00019 double sum(const d5_array& m)
00020 {
00021   double tmp=0.;
00022   for (int i=m.indexmin();i<=m.indexmax();i++)
00023   {
00024     tmp+=sum(m.elem(i));
00025   }
00026   return tmp;
00027 }
00028     
00033  d5_array::d5_array(const d5_array& _m2)
00034  {
00035    d5_array& m2=(d5_array&)_m2;
00036    if (m2.shape)
00037    {
00038      shape=m2.shape;
00039      (shape->ncopies)++;
00040      t = m2.t;
00041    }
00042    else
00043    {
00044      shape=NULL;
00045      t=NULL;
00046    }
00047  }
00048 
00053  void d5_array::deallocate()
00054  {
00055    if (shape)
00056    {
00057      if (shape->ncopies)
00058      {
00059        (shape->ncopies)--;
00060      }
00061      else
00062      {
00063        t += indexmin();
00064        delete [] t;
00065        t=NULL;
00066        delete shape;
00067        shape=NULL;
00068      }
00069    }
00070    else
00071    { 
00072 #    if defined(SAFE_ALL)
00073     // cerr << "Warning -- trying to deallocate an unallocated d4_array"<<endl;
00074 #    endif
00075    }
00076  }
00077 
00082  d5_array::~d5_array() 
00083  {
00084    deallocate();
00085  }
00086 
00091 d5_array& d5_array::operator=(const d5_array& m)
00092  {
00093    int mmin=indexmin();
00094    int mmax=indexmax();
00095    if (mmin!=m.indexmin() || mmax!=m.indexmax())
00096    { 
00097      cerr << "Incompatible bounds in"
00098       " d4_array& d4_array:: operator =  (const d4_array& m)"
00099       << endl;
00100      ad_exit(1);
00101     }
00102    for (int i=mmin; i<=mmax; i++)
00103    {
00104      (*this)(i)=m(i);
00105    }
00106    return *this;
00107  }
00108 
00113  void d5_array::initialize(void)
00114  {
00115    int mmin=indexmin();
00116    int mmax=indexmax();
00117    for (int i=mmin; i<=mmax; i++)
00118    {
00119      (*this)(i).initialize();
00120    }
00121  }
00122 
00127 void d5_array::allocate(const d5_array& m1)
00128  {
00129    if ( (shape=new vector_shape(m1.indexmin(),m1.indexmax()))
00130        == 0)
00131    {
00132      cerr << " Error allocating memory in d5_array contructor" << endl;
00133    }
00134    int ss=size();
00135    if ( (t = new d4_array[ss]) == 0)
00136    {
00137      cerr << " Error allocating memory in d5_array contructor" << endl;
00138      ad_exit(21);
00139    }
00140    t -= indexmin();
00141    for (int i=indexmin(); i<=indexmax(); i++)
00142    {
00143      t[i].allocate(m1[i]);
00144    }
00145  }
00146 
00147   #ifndef OPT_LIB
00148 
00153     d4_array& d5_array::operator ( ) (int i)
00154     {
00155       #ifdef SAFE_ARRAYS
00156       if (i < indexmin() || i > indexmax())
00157       { 
00158         ADMB_ARRAY_BOUNDS_ERROR("index out of bounds", "d4_array& d5_array::operator()(int i)", indexmin(), indexmax(), i);
00159       }
00160       #endif
00161       //return t[i];
00162       return elem(i);
00163     }
00164 
00169     d4_array& d5_array::operator [] (int i)
00170     {
00171       #ifdef SAFE_ARRAYS
00172       if (i < indexmin() || i > indexmax())
00173       {
00174         ADMB_ARRAY_BOUNDS_ERROR("index out of bounds", "d4_array& d5_array::operator[](int i)", indexmin(), indexmax(), i);
00175       }
00176       #endif
00177       return t[i];
00178     }
00179 
00184     d3_array& d5_array::operator ( ) (int i, int j)
00185     {
00186       #ifdef SAFE_ARRAYS
00187       if (i < indexmin() || i > indexmax())
00188       {
00189         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds", "d3_array& d5_array::operator[](int i, int j)", indexmin(), indexmax(), i);
00190       }
00191       #endif
00192       return elem(i)(j);
00193     }
00194 
00199     dmatrix& d5_array::operator ( ) (int i,int j,int k)
00200     {
00201       #ifdef SAFE_ARRAYS
00202       if (i < indexmin() || i > indexmax())
00203       {
00204         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds", "dmatrix& d5_array::operator[](int i, int j, int k)", indexmin(), indexmax(), i);
00205       }
00206       #endif
00207       return elem(i)(j,k);
00208     }
00209 
00214     dvector& d5_array::operator ( ) (int i,int j,int k,int l)
00215     {
00216       #ifdef SAFE_ARRAYS
00217       if (i < indexmin() || i > indexmax())
00218       {
00219         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds", "dvector& d5_array::operator[](int i, int j, int k, int l)", indexmin(), indexmax(), i);
00220       }
00221       #endif
00222       return elem(i)(j,k,l);
00223     }
00224 
00229     double& d5_array::operator ( ) (int i,int j,int k,int l,int m)
00230     {
00231       #ifdef SAFE_ARRAYS
00232       if (i < indexmin() || i > indexmax())
00233       {
00234         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds", "double& d5_array::operator[](int i, int j, int k, int l, int m)", indexmin(), indexmax(), i);
00235       }
00236       #endif
00237       return elem(i)(j,k,l,m);
00238     }
00239 
00240    #ifdef USE_CONST
00241 
00246 const d4_array& d5_array::operator()(int i) const
00247     {
00248       #ifdef SAFE_ARRAYS
00249         if (i<indexmin()||i>indexmax())
00250         { cerr << "Error  index out of bounds in\n"
00251             "d4_array& d5_array::operator ( )" << endl;
00252           ad_exit(1);
00253         }
00254       #endif
00255       return t[i];
00256     }
00257 
00262 const d4_array& d5_array::operator[](int i) const
00263     {
00264       #ifdef SAFE_ARRAYS
00265         if (i<indexmin()||i>indexmax())
00266         { cerr << "Error  index out of bounds in\n"
00267             "d4_array& d5_array::operator []" << endl;
00268           ad_exit(1);
00269         }
00270       #endif
00271       return t[i];
00272     }
00273 
00278 const d3_array& d5_array::operator()(int i, int j) const
00279     {
00280       #ifdef SAFE_ARRAYS
00281         if (i<indexmin()||i>indexmax())
00282         { cerr << "Error index out of bounds in\n"
00283             "d3_array& d5_array::operator ( )" << endl;
00284           ad_exit(1);
00285         }
00286       #endif
00287       return elem(i).elem(j);
00288     }
00289 
00294 const dmatrix& d5_array::operator()(int i, int j, int k) const
00295     {
00296       #ifdef SAFE_ARRAYS
00297         if (i<indexmin()||i>indexmax())
00298         { cerr << "Error index out of bounds in\n"
00299             "d3_array& d5_array::operator ( )" << endl;
00300           ad_exit(1);
00301         }
00302       #endif
00303       return elem(i)(j,k);
00304     }
00305 
00310 const dvector& d5_array::operator()(int i, int j, int k, int l) const
00311     {
00312       #ifdef SAFE_ARRAYS
00313         if (i<indexmin()||i>indexmax())
00314         { cerr << "Error hslice index out of bounds in\n"
00315             "dvector& d5_array::operator ( )"  << endl;
00316           ad_exit(1);
00317         }
00318       #endif
00319       return elem(i)(j,k,l);
00320     }
00321 
00326 const double& d5_array::operator()(int i, int j, int k, int l, int m) const
00327     {
00328       #ifdef SAFE_ARRAYS
00329         if (i<indexmin()||i>indexmax())
00330         { cerr << "Error hslice index out of bounds in\n"
00331             "dvector& d5_array::operator ( )"  << endl;
00332           ad_exit(1);
00333         }
00334       #endif
00335       return elem(i)(j,k,l,m);
00336     }
00337 
00338    #endif
00339   #endif
00340 
00345 d5_array::d5_array(int hsl,int hsu,int sl,int sh,int nrl,
00346    int nrh,int ncl,int nch,int l5,int u5)
00347 {
00348   allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch,l5,u5);
00349 }
00350 
00355 d5_array::d5_array(const ad_integer& hsl,const ad_integer& hsu,
00356   const index_type& sl,const index_type& sh,const index_type& nrl,
00357   const index_type& nrh,const index_type& ncl,const index_type& nch,
00358   const index_type& l5,const index_type& u5)
00359 {
00360   allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch,l5,u5);
00361 }
00362 
00367 void d5_array::allocate(int hsl,int hsu,int sl,int sh,int nrl,
00368    int nrh,int ncl,int nch,int l5,int u5)
00369  {
00370    if ( (shape=new vector_shape(hsl,hsu)) == 0)
00371    {
00372      cerr << " Error allocating memory in d5_array contructor\n";
00373      ad_exit(21);
00374    }
00375    int ss=size();
00376    if ( (t = new d4_array[ss]) == 0)
00377    {
00378      cerr << " Error allocating memory in d5_array contructor\n";
00379      ad_exit(21);
00380    }
00381    t -= indexmin();
00382    for (int i=hsl; i<=hsu; i++)
00383    {
00384      t[i].allocate(sl,sh,nrl,nrh,ncl,nch,l5,u5);
00385    }
00386  }
00387 
00392  d5_array::d5_array(int hsl,int hsu)
00393  {
00394    allocate(hsl,hsu);
00395  }
00396 
00401  void d5_array::allocate(int hsl,int hsu)
00402  {
00403    if ( (shape=new vector_shape(hsl,hsu)) == 0)
00404    {
00405      cerr << " Error allocating memory in d5_array contructor\n";
00406      ad_exit(21);
00407    }
00408    int ss=size();
00409    if ( (t = new d4_array[ss]) == 0)
00410    {
00411      cerr << " Error allocating memory in d5_array contructor\n";
00412      ad_exit(21);
00413    }
00414    t -= indexmin();
00415  }
00416 
00421  void d5_array::allocate(const ad_integer& hsl,const ad_integer& hsu,
00422    const index_type& sl,const index_type& sh,const index_type& nrl,
00423    const index_type& nrh,const index_type& ncl,const index_type& nch,
00424    const index_type& l5,const index_type& u5)
00425  {
00426    if ( (shape=new vector_shape (hsl,hsu)) == 0)
00427    {
00428      cerr << " Error allocating memory in d5_array contructor\n";
00429    }
00430 
00431    int ss=size();
00432    if ( (t = new d4_array[ss]) == 0)
00433    {
00434      cerr << " Error allocating memory in d5_array contructor\n";
00435      ad_exit(21);
00436    }
00437    t -= indexmin();
00438    int il=hsl;
00439    int iu=hsu;
00440    for (int i=il; i<=iu; i++)
00441    {
00442      (*this)(i).allocate(ad_integer(sl(i)),ad_integer(sh(i)),nrl(i),nrh(i),ncl(i),nch(i),
00443        l5(i),u5(i));
00444    }
00445  }