ADMB Documentation  11.1.1897
 All Classes Files Functions Variables Typedefs Friends Defines
d5arr.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: d5arr.cpp 1832 2014-04-03 23:24:02Z 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 #if defined(ADWARN_DEV)
00071    else
00072    {
00073      cerr << "Warning -- trying to deallocate an unallocated d4_array"<<endl;
00074    }
00075 #endif
00076  }
00077 
00081 d5_array::~d5_array()
00082 {
00083   deallocate();
00084 }
00089 d5_array& d5_array::operator=(const d5_array& m)
00090  {
00091    int mmin=indexmin();
00092    int mmax=indexmax();
00093    if (mmin!=m.indexmin() || mmax!=m.indexmax())
00094    {
00095      cerr << "Incompatible bounds in"
00096       " d4_array& d4_array:: operator =  (const d4_array& m)"
00097       << endl;
00098      ad_exit(1);
00099     }
00100    for (int i=mmin; i<=mmax; i++)
00101    {
00102      (*this)(i)=m(i);
00103    }
00104    return *this;
00105  }
00106 
00111  void d5_array::initialize(void)
00112  {
00113    int mmin=indexmin();
00114    int mmax=indexmax();
00115    for (int i=mmin; i<=mmax; i++)
00116    {
00117      (*this)(i).initialize();
00118    }
00119  }
00120 
00125 void d5_array::allocate(const d5_array& m1)
00126  {
00127    if ( (shape=new vector_shape(m1.indexmin(),m1.indexmax()))
00128        == 0)
00129    {
00130      cerr << " Error allocating memory in d5_array contructor" << endl;
00131    }
00132    int ss=size();
00133    if ( (t = new d4_array[ss]) == 0)
00134    {
00135      cerr << " Error allocating memory in d5_array contructor" << endl;
00136      ad_exit(21);
00137    }
00138    t -= indexmin();
00139    for (int i=indexmin(); i<=indexmax(); i++)
00140    {
00141      t[i].allocate(m1[i]);
00142    }
00143  }
00144 
00145 #ifndef OPT_LIB
00146 
00151     d4_array& d5_array::operator ( ) (int i)
00152     {
00153       #ifdef SAFE_ARRAYS
00154       if (i < indexmin() || i > indexmax())
00155       {
00156         ADMB_ARRAY_BOUNDS_ERROR("index out of bounds",
00157         "d4_array& d5_array::operator()(int i)", indexmin(), indexmax(), i);
00158       }
00159       #endif
00160       //return t[i];
00161       return elem(i);
00162     }
00163 
00168     d4_array& d5_array::operator [] (int i)
00169     {
00170       #ifdef SAFE_ARRAYS
00171       if (i < indexmin() || i > indexmax())
00172       {
00173         ADMB_ARRAY_BOUNDS_ERROR("index out of bounds",
00174         "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",
00190         "d3_array& d5_array::operator[](int i, int j)",
00191         indexmin(), indexmax(), i);
00192       }
00193       #endif
00194       return elem(i)(j);
00195     }
00196 
00201     dmatrix& d5_array::operator ( ) (int i,int j,int k)
00202     {
00203       #ifdef SAFE_ARRAYS
00204       if (i < indexmin() || i > indexmax())
00205       {
00206         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00207         "dmatrix& d5_array::operator[](int i, int j, int k)",
00208         indexmin(), indexmax(), i);
00209       }
00210       #endif
00211       return elem(i)(j,k);
00212     }
00213 
00218     dvector& d5_array::operator ( ) (int i,int j,int k,int l)
00219     {
00220       #ifdef SAFE_ARRAYS
00221       if (i < indexmin() || i > indexmax())
00222       {
00223         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00224         "dvector& d5_array::operator[](int i, int j, int k, int l)",
00225         indexmin(), indexmax(), i);
00226       }
00227       #endif
00228       return elem(i)(j,k,l);
00229     }
00230 
00235     double& d5_array::operator ( ) (int i,int j,int k,int l,int m)
00236     {
00237       #ifdef SAFE_ARRAYS
00238       if (i < indexmin() || i > indexmax())
00239       {
00240         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00241         "double& d5_array::operator[](int i, int j, int k, int l, int m)",
00242         indexmin(), indexmax(), i);
00243       }
00244       #endif
00245       return elem(i)(j,k,l,m);
00246     }
00247 
00252 const d4_array& d5_array::operator()(int i) const
00253     {
00254       #ifdef SAFE_ARRAYS
00255         if (i<indexmin()||i>indexmax())
00256         { cerr << "Error  index out of bounds in\n"
00257             "d4_array& d5_array::operator ( )" << endl;
00258           ad_exit(1);
00259         }
00260       #endif
00261       return t[i];
00262     }
00263 
00268 const d4_array& d5_array::operator[](int i) const
00269     {
00270       #ifdef SAFE_ARRAYS
00271         if (i<indexmin()||i>indexmax())
00272         { cerr << "Error  index out of bounds in\n"
00273             "d4_array& d5_array::operator []" << endl;
00274           ad_exit(1);
00275         }
00276       #endif
00277       return t[i];
00278     }
00279 
00284 const d3_array& d5_array::operator()(int i, int j) const
00285     {
00286       #ifdef SAFE_ARRAYS
00287         if (i<indexmin()||i>indexmax())
00288         { cerr << "Error index out of bounds in\n"
00289             "d3_array& d5_array::operator ( )" << endl;
00290           ad_exit(1);
00291         }
00292       #endif
00293       return elem(i).elem(j);
00294     }
00295 
00300 const dmatrix& d5_array::operator()(int i, int j, int k) const
00301     {
00302       #ifdef SAFE_ARRAYS
00303         if (i<indexmin()||i>indexmax())
00304         { cerr << "Error index out of bounds in\n"
00305             "d3_array& d5_array::operator ( )" << endl;
00306           ad_exit(1);
00307         }
00308       #endif
00309       return elem(i)(j,k);
00310     }
00311 
00316 const dvector& d5_array::operator()(int i, int j, int k, int l) const
00317     {
00318       #ifdef SAFE_ARRAYS
00319         if (i<indexmin()||i>indexmax())
00320         { cerr << "Error hslice index out of bounds in\n"
00321             "dvector& d5_array::operator ( )"  << endl;
00322           ad_exit(1);
00323         }
00324       #endif
00325       return elem(i)(j,k,l);
00326     }
00327 
00332 const double& d5_array::operator()(int i, int j, int k, int l, int m) const
00333     {
00334       #ifdef SAFE_ARRAYS
00335         if (i<indexmin()||i>indexmax())
00336         { cerr << "Error hslice index out of bounds in\n"
00337             "dvector& d5_array::operator ( )"  << endl;
00338           ad_exit(1);
00339         }
00340       #endif
00341       return elem(i)(j,k,l,m);
00342     }
00343 
00344 #endif
00345 
00350 d5_array::d5_array(int hsl,int hsu,int sl,int sh,int nrl,
00351    int nrh,int ncl,int nch,int l5,int u5)
00352 {
00353   allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch,l5,u5);
00354 }
00355 
00360 d5_array::d5_array(const ad_integer& hsl,const ad_integer& hsu,
00361   const index_type& sl,const index_type& sh,const index_type& nrl,
00362   const index_type& nrh,const index_type& ncl,const index_type& nch,
00363   const index_type& l5,const index_type& u5)
00364 {
00365   allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch,l5,u5);
00366 }
00367 
00372 void d5_array::allocate(int hsl,int hsu,int sl,int sh,int nrl,
00373    int nrh,int ncl,int nch,int l5,int u5)
00374  {
00375    if ( (shape=new vector_shape(hsl,hsu)) == 0)
00376    {
00377      cerr << " Error allocating memory in d5_array contructor\n";
00378      ad_exit(21);
00379    }
00380    int ss=size();
00381    if ( (t = new d4_array[ss]) == 0)
00382    {
00383      cerr << " Error allocating memory in d5_array contructor\n";
00384      ad_exit(21);
00385    }
00386    t -= indexmin();
00387    for (int i=hsl; i<=hsu; i++)
00388    {
00389      t[i].allocate(sl,sh,nrl,nrh,ncl,nch,l5,u5);
00390    }
00391  }
00392 
00397  d5_array::d5_array(int hsl,int hsu)
00398  {
00399    allocate(hsl,hsu);
00400  }
00401 
00406  void d5_array::allocate(int hsl,int hsu)
00407  {
00408    if ( (shape=new vector_shape(hsl,hsu)) == 0)
00409    {
00410      cerr << " Error allocating memory in d5_array contructor\n";
00411      ad_exit(21);
00412    }
00413    int ss=size();
00414    if ( (t = new d4_array[ss]) == 0)
00415    {
00416      cerr << " Error allocating memory in d5_array contructor\n";
00417      ad_exit(21);
00418    }
00419    t -= indexmin();
00420  }
00421 
00426  void d5_array::allocate(const ad_integer& hsl,const ad_integer& hsu,
00427    const index_type& sl,const index_type& sh,const index_type& nrl,
00428    const index_type& nrh,const index_type& ncl,const index_type& nch,
00429    const index_type& l5,const index_type& u5)
00430  {
00431    if ( (shape=new vector_shape (hsl,hsu)) == 0)
00432    {
00433      cerr << " Error allocating memory in d5_array contructor\n";
00434    }
00435 
00436    int ss=size();
00437    if ( (t = new d4_array[ss]) == 0)
00438    {
00439      cerr << " Error allocating memory in d5_array contructor\n";
00440      ad_exit(21);
00441    }
00442    t -= indexmin();
00443    int il=hsl;
00444    int iu=hsu;
00445    for (int i=il; i<=iu; i++)
00446    {
00447      (*this)(i).allocate(ad_integer(sl(i)),ad_integer(sh(i)),nrl(i),nrh(i),
00448        ncl(i),nch(i), l5(i),u5(i));
00449    }
00450  }