ADMB Documentation  11.1.1927
 All Classes Files Functions Variables Typedefs Friends Defines
d5arr.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: d5arr.cpp 1921 2014-04-23 18:47:05Z 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       if (i < indexmin() || i > indexmax())
00154       {
00155         ADMB_ARRAY_BOUNDS_ERROR("index out of bounds",
00156         "d4_array& d5_array::operator()(int i)", indexmin(), indexmax(), i);
00157       }
00158       //return t[i];
00159       return elem(i);
00160     }
00161 
00166     d4_array& d5_array::operator [] (int i)
00167     {
00168       if (i < indexmin() || i > indexmax())
00169       {
00170         ADMB_ARRAY_BOUNDS_ERROR("index out of bounds",
00171         "d4_array& d5_array::operator[](int i)", indexmin(), indexmax(), i);
00172       }
00173       return t[i];
00174     }
00175 
00180     d3_array& d5_array::operator ( ) (int i, int j)
00181     {
00182       if (i < indexmin() || i > indexmax())
00183       {
00184         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00185         "d3_array& d5_array::operator[](int i, int j)",
00186         indexmin(), indexmax(), i);
00187       }
00188       return elem(i)(j);
00189     }
00190 
00195     dmatrix& d5_array::operator ( ) (int i,int j,int k)
00196     {
00197       if (i < indexmin() || i > indexmax())
00198       {
00199         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00200         "dmatrix& d5_array::operator[](int i, int j, int k)",
00201         indexmin(), indexmax(), i);
00202       }
00203       return elem(i)(j,k);
00204     }
00205 
00210     dvector& d5_array::operator ( ) (int i,int j,int k,int l)
00211     {
00212       if (i < indexmin() || i > indexmax())
00213       {
00214         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00215         "dvector& d5_array::operator[](int i, int j, int k, int l)",
00216         indexmin(), indexmax(), i);
00217       }
00218       return elem(i)(j,k,l);
00219     }
00220 
00225     double& d5_array::operator ( ) (int i,int j,int k,int l,int m)
00226     {
00227       if (i < indexmin() || i > indexmax())
00228       {
00229         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00230         "double& d5_array::operator[](int i, int j, int k, int l, int m)",
00231         indexmin(), indexmax(), i);
00232       }
00233       return elem(i)(j,k,l,m);
00234     }
00235 
00240 const d4_array& d5_array::operator()(int i) const
00241     {
00242         if (i<indexmin()||i>indexmax())
00243         { cerr << "Error  index out of bounds in\n"
00244             "d4_array& d5_array::operator ( )" << endl;
00245           ad_exit(1);
00246         }
00247       return t[i];
00248     }
00249 
00254 const d4_array& d5_array::operator[](int i) const
00255     {
00256         if (i<indexmin()||i>indexmax())
00257         { cerr << "Error  index out of bounds in\n"
00258             "d4_array& d5_array::operator []" << endl;
00259           ad_exit(1);
00260         }
00261       return t[i];
00262     }
00263 
00268 const d3_array& d5_array::operator()(int i, int j) const
00269     {
00270         if (i<indexmin()||i>indexmax())
00271         { cerr << "Error index out of bounds in\n"
00272             "d3_array& d5_array::operator ( )" << endl;
00273           ad_exit(1);
00274         }
00275       return elem(i).elem(j);
00276     }
00277 
00282 const dmatrix& d5_array::operator()(int i, int j, int k) const
00283     {
00284         if (i<indexmin()||i>indexmax())
00285         { cerr << "Error index out of bounds in\n"
00286             "d3_array& d5_array::operator ( )" << endl;
00287           ad_exit(1);
00288         }
00289       return elem(i)(j,k);
00290     }
00291 
00296 const dvector& d5_array::operator()(int i, int j, int k, int l) const
00297     {
00298         if (i<indexmin()||i>indexmax())
00299         { cerr << "Error hslice index out of bounds in\n"
00300             "dvector& d5_array::operator ( )"  << endl;
00301           ad_exit(1);
00302         }
00303       return elem(i)(j,k,l);
00304     }
00305 
00310 const double& d5_array::operator()(int i, int j, int k, int l, int m) const
00311     {
00312         if (i<indexmin()||i>indexmax())
00313         { cerr << "Error hslice index out of bounds in\n"
00314             "dvector& d5_array::operator ( )"  << endl;
00315           ad_exit(1);
00316         }
00317       return elem(i)(j,k,l,m);
00318     }
00319 
00320 #endif
00321 
00326 d5_array::d5_array(int hsl,int hsu,int sl,int sh,int nrl,
00327    int nrh,int ncl,int nch,int l5,int u5)
00328 {
00329   allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch,l5,u5);
00330 }
00331 
00336 d5_array::d5_array(const ad_integer& hsl,const ad_integer& hsu,
00337   const index_type& sl,const index_type& sh,const index_type& nrl,
00338   const index_type& nrh,const index_type& ncl,const index_type& nch,
00339   const index_type& l5,const index_type& u5)
00340 {
00341   allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch,l5,u5);
00342 }
00343 
00348 void d5_array::allocate(int hsl,int hsu,int sl,int sh,int nrl,
00349    int nrh,int ncl,int nch,int l5,int u5)
00350  {
00351    if ( (shape=new vector_shape(hsl,hsu)) == 0)
00352    {
00353      cerr << " Error allocating memory in d5_array contructor\n";
00354      ad_exit(21);
00355    }
00356    int ss=size();
00357    if ( (t = new d4_array[ss]) == 0)
00358    {
00359      cerr << " Error allocating memory in d5_array contructor\n";
00360      ad_exit(21);
00361    }
00362    t -= indexmin();
00363    for (int i=hsl; i<=hsu; i++)
00364    {
00365      t[i].allocate(sl,sh,nrl,nrh,ncl,nch,l5,u5);
00366    }
00367  }
00368 
00373  d5_array::d5_array(int hsl,int hsu)
00374  {
00375    allocate(hsl,hsu);
00376  }
00377 
00382  void d5_array::allocate(int hsl,int hsu)
00383  {
00384    if ( (shape=new vector_shape(hsl,hsu)) == 0)
00385    {
00386      cerr << " Error allocating memory in d5_array contructor\n";
00387      ad_exit(21);
00388    }
00389    int ss=size();
00390    if ( (t = new d4_array[ss]) == 0)
00391    {
00392      cerr << " Error allocating memory in d5_array contructor\n";
00393      ad_exit(21);
00394    }
00395    t -= indexmin();
00396  }
00397 
00402  void d5_array::allocate(const ad_integer& hsl,const ad_integer& hsu,
00403    const index_type& sl,const index_type& sh,const index_type& nrl,
00404    const index_type& nrh,const index_type& ncl,const index_type& nch,
00405    const index_type& l5,const index_type& u5)
00406  {
00407    if ( (shape=new vector_shape (hsl,hsu)) == 0)
00408    {
00409      cerr << " Error allocating memory in d5_array contructor\n";
00410    }
00411 
00412    int ss=size();
00413    if ( (t = new d4_array[ss]) == 0)
00414    {
00415      cerr << " Error allocating memory in d5_array contructor\n";
00416      ad_exit(21);
00417    }
00418    t -= indexmin();
00419    int il=hsl;
00420    int iu=hsu;
00421    for (int i=il; i<=iu; i++)
00422    {
00423      (*this)(i).allocate(ad_integer(sl(i)),ad_integer(sh(i)),nrl(i),nrh(i),
00424        ncl(i),nch(i), l5(i),u5(i));
00425    }
00426  }