ADMB Documentation  11.1.1016
 All Classes Files Functions Variables Typedefs Friends Defines
d4arr.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: d4arr.cpp 818 2013-03-12 23:07:07Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California 
00006  */
00011 #include "fvar.hpp"
00012 #include "d4arr.hpp"
00013 #include "admb_messages.h"
00014 
00019  d4_array::d4_array(int nrl,int nrh)
00020  {
00021    allocate(nrl,nrh);
00022  }
00023 
00028 four_array_shape::four_array_shape(int hsl,int hsu) //,int sl,int su,int rl,
00029  // int ru,int cl,int cu)
00030 {
00031   hslice_min=hsl;
00032   hslice_max=hsu;
00033   //slice_min=sl;
00034   //slice_max=su;
00035   //row_min=rl;
00036   //row_max=ru;
00037   //col_min=cl;
00038   //col_max=cu;
00039   ncopies=0;
00040 }
00041 
00046 double sum(const d4_array& m)
00047 {
00048   double tmp=0.;
00049   for (int i=m.indexmin();i<=m.indexmax();i++)
00050   {
00051     tmp+=sum(m.elem(i));
00052   }
00053   return tmp;
00054 }
00055     
00060  d4_array d4_array::sub(int nrl,int nrh)
00061  {
00062    if (allocated(*this))
00063    {
00064      d4_array tmp(nrl,nrh);
00065      for (int i=nrl; i<=nrh; i++)
00066      {
00067        tmp[i].shallow_copy((*this)(i));
00068      }
00069      return tmp;
00070    }
00071    else
00072    {
00073      return *this;
00074    }
00075  }
00076 
00081  d4_array::d4_array(const d4_array& m2)
00082  {
00083    if (m2.shape)
00084    {
00085      shape=m2.shape;
00086      (shape->ncopies)++;
00087      t = m2.t;
00088    }
00089    else
00090    {
00091      shape=NULL;
00092      t=NULL;
00093    }
00094  }
00095 
00100  void d4_array::shallow_copy(const d4_array& m2)
00101  {
00102    if (m2.shape)
00103    {
00104      shape=m2.shape;
00105      (shape->ncopies)++;
00106      t = m2.t;
00107    }
00108    else
00109    {
00110      shape=NULL;
00111      t=NULL;
00112    }
00113  }
00114 
00119  void d4_array::deallocate()
00120  {
00121    if (shape)
00122    {
00123      if (shape->ncopies)
00124      {
00125        (shape->ncopies)--;
00126      }
00127      else
00128      {
00129        t += hslicemin();
00130        delete [] t;
00131        delete shape;
00132      }
00133    }
00134    else
00135    { 
00136 #    if defined(SAFE_ALL)
00137    //  cerr << "Warning -- trying to deallocate an unallocated d4_array"<<endl;
00138 #    endif 
00139    }
00140  }
00141 
00146  d4_array::~d4_array() 
00147  {
00148    deallocate();
00149  }
00150 
00155 d4_array& d4_array::operator=(const d4_array& m)
00156  {
00157    int mmin=hslicemin();
00158    int mmax=hslicemax();
00159    if (mmin!=m.hslicemin() || mmax!=m.hslicemax())
00160    { 
00161      cerr << "Incompatible bounds in"
00162       " d4_array& d4_array:: operator =  (const d4_array& m)"
00163       << endl;
00164      ad_exit(1);
00165     }
00166    for (int i=mmin; i<=mmax; i++)
00167    {
00168      (*this)(i)=m(i);
00169    }
00170    return *this;
00171  }
00172 
00177 void d4_array::allocate(const d4_array& m1)
00178  {
00179    if ( (shape=new four_array_shape(m1.hslicemin(),m1.hslicemax()))
00180        == 0)
00181    {
00182      cerr << " Error allocating memory in dvar4_array contructor" << endl;
00183    }
00184    int ss=hslicesize();
00185    if ( (t = new d3_array[ss]) == 0)
00186    {
00187      cerr << " Error allocating memory in dvar4_array contructor" << endl;
00188      ad_exit(21);
00189    }
00190    t -= hslicemin();
00191    for (int i=hslicemin(); i<=hslicemax(); i++)
00192    {
00193      t[i].allocate(m1[i]);
00194    }
00195  }
00196 
00197   #ifndef OPT_LIB
00198 
00203     d3_array& d4_array::operator () (int i)
00204     {
00205       #ifdef SAFE_ARRAYS
00206       if (i < hslicemin() || i > hslicemax())
00207       {
00208         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds", "d3_array& d4_array::operator() (int i)", hslicemin(), hslicemax(), i);
00209       }
00210       #endif
00211       return t[i];
00212     }
00213 
00218     d3_array& d4_array::operator [] (int i)
00219     {
00220       #ifdef SAFE_ARRAYS
00221       if (i < hslicemin() || i > hslicemax())
00222       {
00223         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds", "d3_array& d4_array::operator[] (int i)", hslicemin(), hslicemax(), i);
00224       }
00225       #endif
00226       return t[i];
00227     }
00228 
00233     dmatrix& d4_array::operator ( ) (int i ,int j)
00234     {
00235       #ifdef SAFE_ARRAYS
00236       if (i < hslicemin() || i > hslicemax())
00237       {
00238         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds", "dmatrix& d4_array::operator() (int i, int j)", hslicemin(), hslicemax(), i);
00239       }
00240       #endif
00241       return ((*this)(i))(j);
00242     }
00243 
00248     dvector& d4_array::operator ( ) (int i,int j,int k)
00249     {
00250       #ifdef SAFE_ARRAYS
00251       if (i < hslicemin() || i > hslicemax())
00252       {
00253         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds", "dvector& d4_array::operator() (int i, int j, int k)", hslicemin(), hslicemax(), i);
00254       }
00255       #endif
00256       return (((*this)(i,j))(k));
00257     }
00258 
00263     double& d4_array::operator ( ) (int i,int j,int k,int l)
00264     {
00265       #ifdef SAFE_ARRAYS
00266       if (i < hslicemin() || i > hslicemax())
00267       {
00268         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds", "double& d4_array::operator() (int i, int j, int k, int l)", hslicemin(), hslicemax(), i);
00269       }
00270       #endif
00271       return ( ((*this)(i,j,k))(l));
00272     }
00273 
00274    #ifdef USE_CONST
00275 
00280 const d3_array& d4_array::operator()(int i) const
00281     {
00282       #ifdef SAFE_ARRAYS
00283         if (i<hslicemin()||i>hslicemax())
00284         { cerr << "Error hslice index out of bounds in\n"
00285             "d3_array& d4_array::operator ( )" << endl;
00286           ad_exit(1);
00287         }
00288       #endif
00289       return t[i];
00290     }
00291 
00296 const d3_array& d4_array::operator[](int i) const
00297     {
00298       #ifdef SAFE_ARRAYS
00299         if (i<hslicemin()||i>hslicemax())
00300         { cerr << "Error hslice index out of bounds in\n"
00301             "d3_array& d4_array::operator ( )" << endl;
00302           ad_exit(1);
00303         }
00304       #endif
00305       return t[i];
00306     }
00307 
00312 const dmatrix& d4_array::operator()(int i, int j) const
00313     {
00314       #ifdef SAFE_ARRAYS
00315         if (i<hslicemin()||i>hslicemax())
00316         { cerr << "Error hslice index out of bounds in\n"
00317             "dmatrix& d4_array::operator ( )" << endl;
00318           ad_exit(1);
00319         }
00320       #endif
00321       return ((*this)(i))(j);
00322     }
00323 
00328 const dvector& d4_array::operator()(int i, int j, int k) const
00329     {
00330       #ifdef SAFE_ARRAYS
00331         if (i<hslicemin()||i>hslicemax())
00332         { cerr << "Error hslice index out of bounds in\n"
00333       "dvector& d4_array::operator ( )" << endl;
00334           ad_exit(1);
00335         }
00336       #endif
00337       return (((*this)(i,j))(k));
00338     }
00339 
00344 const double& d4_array::operator()(int i, int j, int k, int l) const
00345     {
00346       #ifdef SAFE_ARRAYS
00347         if (i<hslicemin()||i>hslicemax())
00348         { cerr << "Error hslice index out of bounds in\n"
00349             "double& d4_array::operator ( )"  << endl;
00350           ad_exit(1);
00351         }
00352       #endif
00353       return ( ((*this)(i,j,k))(l));
00354     }
00355 
00356    #endif
00357   #endif
00358 
00363 void d4_array::allocate(int hsl,int hsu,int sl,int sh,int nrl,
00364    int nrh,int ncl,int nch)
00365  {
00366    if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00367    {
00368      cerr << " Error allocating memory in d3_array contructor\n";
00369      ad_exit(21);
00370    }
00371    int ss=hslicesize();
00372    if ( (t = new d3_array[ss]) == 0)
00373    {
00374      cerr << " Error allocating memory in d3_array contructor\n";
00375      ad_exit(21);
00376    }
00377    t -= hslicemin();
00378    for (int i=hsl; i<=hsu; i++)
00379    {
00380      (*this)(i).allocate(sl,sh,nrl,nrh,ncl,nch);
00381    }
00382  }
00383 
00388  void d4_array::allocate(int hsl,int hsu,int sl,int sh,int nrl,
00389    int nrh, const ivector& ncl, const ivector& nch)
00390  {
00391    if ( (shape=new four_array_shape (hsl,hsu)) == 0)
00392    {
00393      cerr << " Error allocating memory in d4_array contructor\n";
00394    }
00395 
00396    int ss=hslicesize();
00397    if ( (t = new d3_array[ss]) == 0)
00398    {
00399      cerr << " Error allocating memory in d3_array contructor\n";
00400      ad_exit(21);
00401    }
00402    t -= hslicemin();
00403    for (int i=hsl; i<=hsu; i++)
00404    {
00405      (*this)(i).allocate(sl,sh,nrl,nrh,ncl,nch);
00406    }
00407  }
00408 
00413 void d4_array::allocate(int hsl, int hsu, int sl, int sh, const ivector& nrl,
00414                         const ivector& nrh, const ivector& ncl, const ivector& nch)
00415  {
00416    if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00417    {
00418      cerr << " Error allocating memory in d4_array contructor\n";
00419    }
00420 
00421    int ss=hslicesize();
00422    if ( (t = new d3_array[ss]) == 0)
00423    {
00424      cerr << " Error allocating memory in d3_array contructor\n";
00425      ad_exit(21);
00426    }
00427    t -= hslicemin();
00428    for (int i=hsl; i<=hsu; i++)
00429    {
00430      (*this)(i).allocate(sl,sh,nrl(i),nrh(i),ncl(i),nch(i));
00431    }
00432  }
00433 
00438 d4_array::d4_array(int hsl, int hsu, int sl, const ivector& sh,
00439                    int nrl, const imatrix& nrh, int ncl, int nch)
00440  {
00441    allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
00442  }
00443 
00448 void d4_array::allocate(int hsl, int hsu, int sl, const ivector& sh,
00449                         int nrl, const imatrix& nrh, int ncl, int nch)
00450  {
00451    //int rmin=nrh.rowmin();
00452    //int cmin=nrh(rmin).indexmin();
00453    if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00454    {
00455      cerr << " Error allocating memory in d4_array contructor\n";
00456    }
00457 
00458    int ss=hslicesize();
00459    if ( (t = new d3_array[ss]) == 0)
00460    {
00461      cerr << " Error allocating memory in d3_array contructor\n";
00462      ad_exit(21);
00463    }
00464    t -= hslicemin();
00465    for (int i=hsl; i<=hsu; i++)
00466    {
00467      (*this)(i).allocate(sl,sh(i),nrl,nrh(i),ncl,nch);
00468    }
00469  }
00470 
00475  d4_array::d4_array(int hsl,int hsu,int sl,int sh,int nrl,
00476    int nrh,int ncl,int nch)
00477  {
00478    allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
00479  }
00480 
00485  d4_array::d4_array(int hsl,int hsu, int sl,int sh,ivector nrl,ivector nrh,
00486     ivector ncl,ivector nch)
00487  {
00488    allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
00489 
00490  }
00491 
00496  void d4_array::initialize()
00497  {
00498    if (!(!(*this)))  // only initialize allocated objects
00499    {
00500      for (int i=hslicemin();i<=hslicemax();i++)
00501      {
00502        elem(i).initialize();
00503      }
00504    }
00505  }
00506 
00511 d4_array::d4_array(int hsl, int hsu, int sl, const ivector& sh, int nrl,
00512                    const imatrix& nrh, int ncl, const i3_array& nch)
00513 {
00514   allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
00515 }
00516 
00521 void d4_array::allocate(int hsl, int hsu, int sl, const ivector& sh, int nrl,
00522                         const imatrix& nrh, int ncl, const i3_array& nch)
00523 {
00524   //int rmin=nrh.rowmin();
00525   //int cmin=nrh(rmin).indexmin();
00526   //int sl1=nch.slicemin();
00527   //int rmin1=nch(sl1).rowmin();
00528   //int cmin1=nch(sl1,rmin1).indexmin();
00529   if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00530   {
00531     cerr << " Error allocating memory in d4_array contructor\n";
00532   }
00533   int ss=hslicesize();
00534   if ( (t = new d3_array[ss]) == 0)
00535   {
00536     cerr << " Error allocating memory in d3_array contructor\n";
00537     ad_exit(21);
00538   }
00539   t -= hslicemin();
00540   for (int i=hsl; i<=hsu; i++)
00541   {
00542     (*this)(i).allocate(sl,sh(i),nrl,nrh(i),ncl,nch(i));
00543   }
00544 }
00545 
00550 d4_array::d4_array(int hsl,int hsu,const index_type& sl,
00551     const index_type& sh,const index_type& nrl,const index_type& nrh,
00552     const index_type& ncl,const index_type& nch)
00553 {
00554   allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
00555 }
00556 
00561 void d4_array::allocate(ad_integer hsl,ad_integer hsu,const index_type& sl,
00562     const index_type& sh,const index_type& nrl,const index_type& nrh,
00563     const index_type& ncl,const index_type& nch)
00564 {
00565    if (hsl>hsu)
00566    {
00567      allocate();
00568      return;
00569    }
00570   int ss=hsu-hsl+1;
00571   if ( (t = new d3_array[ss]) == 0)
00572   {
00573     cerr << " Error allocating memory in d3_array contructor\n";
00574     ad_exit(21);
00575   }
00576   if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00577   {
00578     cerr << " Error allocating memory in d3_array contructor\n";
00579     ad_exit(21);
00580   }
00581   t -= int(hsl);
00582   for (int i=hsl; i<=hsu; i++)
00583   {
00584     const index_type& rsl=sl[i];
00585     const index_type& rsh=sh[i];
00586     const index_type& rnrl=nrl[i];
00587     const index_type& rnrh=nrh[i];
00588     const index_type& rncl=ncl[i];
00589     const index_type& rnch=nch[i];
00590 
00591     const ad_integer& aa=ad_integer(rsl);
00592     const ad_integer& bb=ad_integer(rsh);
00593 
00594     (*this)(i).allocate(aa,bb,rnrl,rnrh,rncl,rnch);
00595 
00596     //(*this)(i).allocate(sl[i],sh[i],nrl[i],nrh[i],ncl[i],nch[i]);
00597   }
00598 }
00599 
00604 void d4_array::allocate(int hsl,int hsu,const index_type& sl,
00605     const index_type& sh,const index_type& nrl,const index_type& nrh,
00606     const index_type& ncl,const index_type& nch)
00607 {
00608    if (hsl>hsu)
00609    {
00610      allocate();
00611      return;
00612    }
00613   int ss=hsu-hsl+1;
00614   if ( (t = new d3_array[ss]) == 0)
00615   {
00616     cerr << " Error allocating memory in d3_array contructor\n";
00617     ad_exit(21);
00618   }
00619   if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00620   {
00621     cerr << " Error allocating memory in d3_array contructor\n";
00622     ad_exit(21);
00623   }
00624   t -= int(hsl);
00625   for (int i=hsl; i<=hsu; i++)
00626   {
00627     const index_type& rsl=sl[i];
00628     const index_type& rsh=sh[i];
00629     const index_type& rnrl=nrl[i];
00630     const index_type& rnrh=nrh[i];
00631     const index_type& rncl=ncl[i];
00632     const index_type& rnch=nch[i];
00633 
00634     const ad_integer& aa=ad_integer(rsl);
00635     const ad_integer& bb=ad_integer(rsh);
00636 
00637     (*this)(i).allocate(aa,bb,rnrl,rnrh,rncl,rnch);
00638 
00639     //(*this)(i).allocate(sl[i],sh[i],nrl[i],nrh[i],ncl[i],nch[i]);
00640   }
00641 }
00642 
00647 void d4_array::allocate(ad_integer hsl,ad_integer hsu,const index_type& sl,
00648     const index_type& sh,const index_type& nrl,const index_type& nrh)
00649 {
00650    if (hsl>hsu)
00651    {
00652      allocate();
00653      return;
00654    }
00655   int ss=hsu-hsl+1;
00656   if ( (t = new d3_array[ss]) == 0)
00657   {
00658     cerr << " Error allocating memory in d3_array contructor\n";
00659     ad_exit(21);
00660   }
00661   if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00662   {
00663     cerr << " Error allocating memory in d3_array contructor\n";
00664     ad_exit(21);
00665   }
00666   t -= int(hsl);
00667   for (int i=hsl; i<=hsu; i++)
00668   {
00669     const index_type& rsl=sl[i];
00670     const index_type& rsh=sh[i];
00671     const index_type& rnrl=nrl[i];
00672     const index_type& rnrh=nrh[i];
00673     const ad_integer& aa=ad_integer(rsl);
00674     const ad_integer& bb=ad_integer(rsh);
00675     (*this)(i).allocate(aa,bb,rnrl,rnrh);
00676   }
00677 }
00678 
00683 void d4_array::allocate(ad_integer hsl,ad_integer hsu,const index_type& sl,
00684     const index_type& sh)
00685 {
00686    if (hsl>hsu)
00687    {
00688      allocate();
00689      return;
00690    }
00691   int ss=hsu-hsl+1;
00692   if ( (t = new d3_array[ss]) == 0)
00693   {
00694     cerr << " Error allocating memory in d3_array contructor\n";
00695     ad_exit(21);
00696   }
00697   if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00698   {
00699     cerr << " Error allocating memory in d3_array contructor\n";
00700     ad_exit(21);
00701   }
00702   t -= int(hsl);
00703   for (int i=hsl; i<=hsu; i++)
00704   {
00705     const index_type& rsl=sl[i];
00706     const index_type& rsh=sh[i];
00707     const ad_integer& aa=ad_integer(rsl);
00708     const ad_integer& bb=ad_integer(rsh);
00709     (*this)(i).allocate(aa,bb);
00710   }
00711 }
00712 
00717 void d4_array::allocate(ad_integer hsl,ad_integer hsu)
00718 {
00719    if (hsl>hsu)
00720    {
00721      allocate();
00722      return;
00723    }
00724   int ss=hsu-hsl+1;
00725   if ( (t = new d3_array[ss]) == 0)
00726   {
00727     cerr << " Error allocating memory in d3_array contructor\n";
00728     ad_exit(21);
00729   }
00730   if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00731   {
00732     cerr << " Error allocating memory in d3_array contructor\n";
00733     ad_exit(21);
00734   }
00735   t -= int(hsl);
00736   for (int i=hsl; i<=hsu; i++)
00737   {
00738     (*this)(i).allocate();
00739   }
00740 }