ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
d5arr.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 "fvar.hpp"
00012 #include "admb_messages.h"
00013 
00018 double sum(const d5_array& m)
00019 {
00020   double tmp=0.;
00021   for (int i=m.indexmin();i<=m.indexmax();i++)
00022   {
00023     tmp+=sum(m.elem(i));
00024   }
00025   return tmp;
00026 }
00027 
00032  d5_array::d5_array(const d5_array& _m2)
00033  {
00034    d5_array& m2=(d5_array&)_m2;
00035    if (m2.shape)
00036    {
00037      shape=m2.shape;
00038      (shape->ncopies)++;
00039      t = m2.t;
00040    }
00041    else
00042    {
00043      shape=NULL;
00044      t=NULL;
00045    }
00046  }
00047 
00052  void d5_array::deallocate()
00053  {
00054    if (shape)
00055    {
00056      if (shape->ncopies)
00057      {
00058        (shape->ncopies)--;
00059      }
00060      else
00061      {
00062        t += indexmin();
00063        delete [] t;
00064        t=NULL;
00065        delete shape;
00066        shape=NULL;
00067      }
00068    }
00069 #if defined(SAFE_ALL)
00070    else
00071    {
00072      cerr << "Warning -- trying to deallocate an unallocated d4_array"<<endl;
00073    }
00074 #endif
00075  }
00076 
00080 d5_array::~d5_array()
00081 {
00082   deallocate();
00083 }
00088 d5_array& d5_array::operator=(const d5_array& m)
00089  {
00090    int mmin=indexmin();
00091    int mmax=indexmax();
00092    if (mmin!=m.indexmin() || mmax!=m.indexmax())
00093    {
00094      cerr << "Incompatible bounds in"
00095       " d4_array& d4_array:: operator =  (const d4_array& m)"
00096       << endl;
00097      ad_exit(1);
00098     }
00099    for (int i=mmin; i<=mmax; i++)
00100    {
00101      (*this)(i)=m(i);
00102    }
00103    return *this;
00104  }
00105 
00110  void d5_array::initialize(void)
00111  {
00112    int mmin=indexmin();
00113    int mmax=indexmax();
00114    for (int i=mmin; i<=mmax; i++)
00115    {
00116      (*this)(i).initialize();
00117    }
00118  }
00119 
00124 void d5_array::allocate(const d5_array& m1)
00125  {
00126    if ( (shape=new vector_shape(m1.indexmin(),m1.indexmax()))
00127        == 0)
00128    {
00129      cerr << " Error allocating memory in d5_array contructor" << endl;
00130    }
00131    int ss=size();
00132    if ( (t = new d4_array[ss]) == 0)
00133    {
00134      cerr << " Error allocating memory in d5_array contructor" << endl;
00135      ad_exit(21);
00136    }
00137    t -= indexmin();
00138    for (int i=indexmin(); i<=indexmax(); i++)
00139    {
00140      t[i].allocate(m1[i]);
00141    }
00142  }
00143 
00144 #ifndef OPT_LIB
00145 
00150     d4_array& d5_array::operator ( ) (int i)
00151     {
00152       if (i < indexmin() || i > indexmax())
00153       {
00154         ADMB_ARRAY_BOUNDS_ERROR("index out of bounds",
00155         "d4_array& d5_array::operator()(int i)", indexmin(), indexmax(), i);
00156       }
00157       //return t[i];
00158       return elem(i);
00159     }
00160 
00165     d4_array& d5_array::operator [] (int i)
00166     {
00167       if (i < indexmin() || i > indexmax())
00168       {
00169         ADMB_ARRAY_BOUNDS_ERROR("index out of bounds",
00170         "d4_array& d5_array::operator[](int i)", indexmin(), indexmax(), i);
00171       }
00172       return t[i];
00173     }
00174 
00179     d3_array& d5_array::operator ( ) (int i, int j)
00180     {
00181       if (i < indexmin() || i > indexmax())
00182       {
00183         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00184         "d3_array& d5_array::operator[](int i, int j)",
00185         indexmin(), indexmax(), i);
00186       }
00187       return elem(i)(j);
00188     }
00189 
00194     dmatrix& d5_array::operator ( ) (int i,int j,int k)
00195     {
00196       if (i < indexmin() || i > indexmax())
00197       {
00198         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00199         "dmatrix& d5_array::operator[](int i, int j, int k)",
00200         indexmin(), indexmax(), i);
00201       }
00202       return elem(i)(j,k);
00203     }
00204 
00209     dvector& d5_array::operator ( ) (int i,int j,int k,int l)
00210     {
00211       if (i < indexmin() || i > indexmax())
00212       {
00213         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00214         "dvector& d5_array::operator[](int i, int j, int k, int l)",
00215         indexmin(), indexmax(), i);
00216       }
00217       return elem(i)(j,k,l);
00218     }
00219 
00224     double& d5_array::operator ( ) (int i,int j,int k,int l,int m)
00225     {
00226       if (i < indexmin() || i > indexmax())
00227       {
00228         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00229         "double& d5_array::operator[](int i, int j, int k, int l, int m)",
00230         indexmin(), indexmax(), i);
00231       }
00232       return elem(i)(j,k,l,m);
00233     }
00234 
00239 const d4_array& d5_array::operator()(int i) const
00240     {
00241         if (i<indexmin()||i>indexmax())
00242         { cerr << "Error  index out of bounds in\n"
00243             "d4_array& d5_array::operator ( )" << endl;
00244           ad_exit(1);
00245         }
00246       return t[i];
00247     }
00248 
00253 const d4_array& d5_array::operator[](int i) const
00254     {
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       return t[i];
00261     }
00262 
00267 const d3_array& d5_array::operator()(int i, int j) const
00268     {
00269         if (i<indexmin()||i>indexmax())
00270         { cerr << "Error index out of bounds in\n"
00271             "d3_array& d5_array::operator ( )" << endl;
00272           ad_exit(1);
00273         }
00274       return elem(i).elem(j);
00275     }
00276 
00281 const dmatrix& d5_array::operator()(int i, int j, int k) const
00282     {
00283         if (i<indexmin()||i>indexmax())
00284         { cerr << "Error index out of bounds in\n"
00285             "d3_array& d5_array::operator ( )" << endl;
00286           ad_exit(1);
00287         }
00288       return elem(i)(j,k);
00289     }
00290 
00295 const dvector& d5_array::operator()(int i, int j, int k, int l) const
00296     {
00297         if (i<indexmin()||i>indexmax())
00298         { cerr << "Error hslice index out of bounds in\n"
00299             "dvector& d5_array::operator ( )"  << endl;
00300           ad_exit(1);
00301         }
00302       return elem(i)(j,k,l);
00303     }
00304 
00309 const double& d5_array::operator()(int i, int j, int k, int l, int m) const
00310     {
00311         if (i<indexmin()||i>indexmax())
00312         { cerr << "Error hslice index out of bounds in\n"
00313             "dvector& d5_array::operator ( )"  << endl;
00314           ad_exit(1);
00315         }
00316       return elem(i)(j,k,l,m);
00317     }
00318 
00319 #endif
00320 
00325 d5_array::d5_array(int hsl,int hsu,int sl,int sh,int nrl,
00326    int nrh,int ncl,int nch,int l5,int u5)
00327 {
00328   allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch,l5,u5);
00329 }
00330 
00335 d5_array::d5_array(const ad_integer& hsl,const ad_integer& hsu,
00336   const index_type& sl,const index_type& sh,const index_type& nrl,
00337   const index_type& nrh,const index_type& ncl,const index_type& nch,
00338   const index_type& l5,const index_type& u5)
00339 {
00340   allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch,l5,u5);
00341 }
00342 
00347 void d5_array::allocate(int hsl,int hsu,int sl,int sh,int nrl,
00348    int nrh,int ncl,int nch,int l5,int u5)
00349  {
00350    if ( (shape=new vector_shape(hsl,hsu)) == 0)
00351    {
00352      cerr << " Error allocating memory in d5_array contructor\n";
00353      ad_exit(21);
00354    }
00355    int ss=size();
00356    if ( (t = new d4_array[ss]) == 0)
00357    {
00358      cerr << " Error allocating memory in d5_array contructor\n";
00359      ad_exit(21);
00360    }
00361    t -= indexmin();
00362    for (int i=hsl; i<=hsu; i++)
00363    {
00364      t[i].allocate(sl,sh,nrl,nrh,ncl,nch,l5,u5);
00365    }
00366  }
00367 
00372  d5_array::d5_array(int hsl,int hsu)
00373  {
00374    allocate(hsl,hsu);
00375  }
00376 
00381  void d5_array::allocate(int hsl,int hsu)
00382  {
00383    if ( (shape=new vector_shape(hsl,hsu)) == 0)
00384    {
00385      cerr << " Error allocating memory in d5_array contructor\n";
00386      ad_exit(21);
00387    }
00388    int ss=size();
00389    if ( (t = new d4_array[ss]) == 0)
00390    {
00391      cerr << " Error allocating memory in d5_array contructor\n";
00392      ad_exit(21);
00393    }
00394    t -= indexmin();
00395  }
00396 
00401  void d5_array::allocate(const ad_integer& hsl,const ad_integer& hsu,
00402    const index_type& sl,const index_type& sh,const index_type& nrl,
00403    const index_type& nrh,const index_type& ncl,const index_type& nch,
00404    const index_type& l5,const index_type& u5)
00405  {
00406    if ( (shape=new vector_shape (hsl,hsu)) == 0)
00407    {
00408      cerr << " Error allocating memory in d5_array contructor\n";
00409    }
00410 
00411    int ss=size();
00412    if ( (t = new d4_array[ss]) == 0)
00413    {
00414      cerr << " Error allocating memory in d5_array contructor\n";
00415      ad_exit(21);
00416    }
00417    t -= indexmin();
00418    int il=hsl;
00419    int iu=hsu;
00420    for (int i=il; i<=iu; i++)
00421    {
00422      (*this)(i).allocate(ad_integer(sl(i)),ad_integer(sh(i)),nrl(i),nrh(i),
00423        ncl(i),nch(i), l5(i),u5(i));
00424    }
00425  }