ADMB Documentation  11.1.1903
 All Classes Files Functions Variables Typedefs Friends Defines
d4arr.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: d4arr.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 "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 #if defined(ADWARN_DEV)
00135    else
00136    {
00137      cerr << "Warning -- trying to deallocate an unallocated d4_array"<<endl;
00138    }
00139 #endif
00140  }
00141 
00145 d4_array::~d4_array()
00146 {
00147   deallocate();
00148 }
00149 
00154 d4_array& d4_array::operator=(const d4_array& m)
00155  {
00156    int mmin=hslicemin();
00157    int mmax=hslicemax();
00158    if (mmin!=m.hslicemin() || mmax!=m.hslicemax())
00159    {
00160      cerr << "Incompatible bounds in"
00161       " d4_array& d4_array:: operator =  (const d4_array& m)"
00162       << endl;
00163      ad_exit(1);
00164     }
00165    for (int i=mmin; i<=mmax; i++)
00166    {
00167      (*this)(i)=m(i);
00168    }
00169    return *this;
00170  }
00171 
00176 void d4_array::allocate(const d4_array& m1)
00177  {
00178    if ( (shape=new four_array_shape(m1.hslicemin(),m1.hslicemax()))
00179        == 0)
00180    {
00181      cerr << " Error allocating memory in dvar4_array contructor" << endl;
00182    }
00183    int ss=hslicesize();
00184    if ( (t = new d3_array[ss]) == 0)
00185    {
00186      cerr << " Error allocating memory in dvar4_array contructor" << endl;
00187      ad_exit(21);
00188    }
00189    t -= hslicemin();
00190    for (int i=hslicemin(); i<=hslicemax(); i++)
00191    {
00192      t[i].allocate(m1[i]);
00193    }
00194  }
00195 
00196   #ifndef OPT_LIB
00197 
00202     d3_array& d4_array::operator () (int i)
00203     {
00204       #ifdef SAFE_ARRAYS
00205       if (i < hslicemin() || i > hslicemax())
00206       {
00207         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00208         "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",
00224         "d3_array& d4_array::operator[] (int i)",
00225         hslicemin(), hslicemax(), i);
00226       }
00227       #endif
00228       return t[i];
00229     }
00230 
00235     dmatrix& d4_array::operator ( ) (int i ,int j)
00236     {
00237       #ifdef SAFE_ARRAYS
00238       if (i < hslicemin() || i > hslicemax())
00239       {
00240         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00241         "dmatrix& d4_array::operator() (int i, int j)",
00242         hslicemin(), hslicemax(), i);
00243       }
00244       #endif
00245       return ((*this)(i))(j);
00246     }
00247 
00252     dvector& d4_array::operator ( ) (int i,int j,int k)
00253     {
00254       #ifdef SAFE_ARRAYS
00255       if (i < hslicemin() || i > hslicemax())
00256       {
00257         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00258         "dvector& d4_array::operator() (int i, int j, int k)",
00259         hslicemin(), hslicemax(), i);
00260       }
00261       #endif
00262       return (((*this)(i,j))(k));
00263     }
00264 
00269     double& d4_array::operator ( ) (int i,int j,int k,int l)
00270     {
00271       #ifdef SAFE_ARRAYS
00272       if (i < hslicemin() || i > hslicemax())
00273       {
00274         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00275         "double& d4_array::operator() (int i, int j, int k, int l)",
00276         hslicemin(), hslicemax(), i);
00277       }
00278       #endif
00279       return ( ((*this)(i,j,k))(l));
00280     }
00281 
00286 const d3_array& d4_array::operator()(int i) const
00287     {
00288       #ifdef SAFE_ARRAYS
00289         if (i<hslicemin()||i>hslicemax())
00290         { cerr << "Error hslice index out of bounds in\n"
00291             "d3_array& d4_array::operator ( )" << endl;
00292           ad_exit(1);
00293         }
00294       #endif
00295       return t[i];
00296     }
00297 
00302 const d3_array& d4_array::operator[](int i) const
00303     {
00304       #ifdef SAFE_ARRAYS
00305         if (i<hslicemin()||i>hslicemax())
00306         { cerr << "Error hslice index out of bounds in\n"
00307             "d3_array& d4_array::operator ( )" << endl;
00308           ad_exit(1);
00309         }
00310       #endif
00311       return t[i];
00312     }
00313 
00318 const dmatrix& d4_array::operator()(int i, int j) const
00319     {
00320       #ifdef SAFE_ARRAYS
00321         if (i<hslicemin()||i>hslicemax())
00322         { cerr << "Error hslice index out of bounds in\n"
00323             "dmatrix& d4_array::operator ( )" << endl;
00324           ad_exit(1);
00325         }
00326       #endif
00327       return ((*this)(i))(j);
00328     }
00329 
00334 const dvector& d4_array::operator()(int i, int j, int k) const
00335     {
00336       #ifdef SAFE_ARRAYS
00337         if (i<hslicemin()||i>hslicemax())
00338         { cerr << "Error hslice index out of bounds in\n"
00339           "dvector& d4_array::operator ( )" << endl;
00340           ad_exit(1);
00341         }
00342       #endif
00343       return (((*this)(i,j))(k));
00344     }
00345 
00350 const double& d4_array::operator()(int i, int j, int k, int l) const
00351     {
00352       #ifdef SAFE_ARRAYS
00353         if (i<hslicemin()||i>hslicemax())
00354         { cerr << "Error hslice index out of bounds in\n"
00355             "double& d4_array::operator ( )"  << endl;
00356           ad_exit(1);
00357         }
00358       #endif
00359       return ( ((*this)(i,j,k))(l));
00360     }
00361 
00362 #endif
00363 
00368 void d4_array::allocate(int hsl,int hsu,int sl,int sh,int nrl,
00369    int nrh,int ncl,int nch)
00370  {
00371    if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00372    {
00373      cerr << " Error allocating memory in d3_array contructor\n";
00374      ad_exit(21);
00375    }
00376    int ss=hslicesize();
00377    if ( (t = new d3_array[ss]) == 0)
00378    {
00379      cerr << " Error allocating memory in d3_array contructor\n";
00380      ad_exit(21);
00381    }
00382    t -= hslicemin();
00383    for (int i=hsl; i<=hsu; i++)
00384    {
00385      (*this)(i).allocate(sl,sh,nrl,nrh,ncl,nch);
00386    }
00387  }
00388 
00393  void d4_array::allocate(int hsl,int hsu,int sl,int sh,int nrl,
00394    int nrh, const ivector& ncl, const ivector& nch)
00395  {
00396    if ( (shape=new four_array_shape (hsl,hsu)) == 0)
00397    {
00398      cerr << " Error allocating memory in d4_array contructor\n";
00399    }
00400 
00401    int ss=hslicesize();
00402    if ( (t = new d3_array[ss]) == 0)
00403    {
00404      cerr << " Error allocating memory in d3_array contructor\n";
00405      ad_exit(21);
00406    }
00407    t -= hslicemin();
00408    for (int i=hsl; i<=hsu; i++)
00409    {
00410      (*this)(i).allocate(sl,sh,nrl,nrh,ncl,nch);
00411    }
00412  }
00413 
00418 void d4_array::allocate(int hsl, int hsu, int sl, int sh, const ivector& nrl,
00419   const ivector& nrh, const ivector& ncl, const ivector& nch)
00420  {
00421    if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00422    {
00423      cerr << " Error allocating memory in d4_array contructor\n";
00424    }
00425 
00426    int ss=hslicesize();
00427    if ( (t = new d3_array[ss]) == 0)
00428    {
00429      cerr << " Error allocating memory in d3_array contructor\n";
00430      ad_exit(21);
00431    }
00432    t -= hslicemin();
00433    for (int i=hsl; i<=hsu; i++)
00434    {
00435      (*this)(i).allocate(sl,sh,nrl(i),nrh(i),ncl(i),nch(i));
00436    }
00437  }
00438 
00443 d4_array::d4_array(int hsl, int hsu, int sl, const ivector& sh,
00444   int nrl, const imatrix& nrh, int ncl, int nch)
00445 {
00446   allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
00447 }
00448 
00453 void d4_array::allocate(int hsl, int hsu, int sl, const ivector& sh,
00454   int nrl, const imatrix& nrh, int ncl, int nch)
00455  {
00456    //int rmin=nrh.rowmin();
00457    //int cmin=nrh(rmin).indexmin();
00458    if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00459    {
00460      cerr << " Error allocating memory in d4_array contructor\n";
00461    }
00462 
00463    int ss=hslicesize();
00464    if ( (t = new d3_array[ss]) == 0)
00465    {
00466      cerr << " Error allocating memory in d3_array contructor\n";
00467      ad_exit(21);
00468    }
00469    t -= hslicemin();
00470    for (int i=hsl; i<=hsu; i++)
00471    {
00472      (*this)(i).allocate(sl,sh(i),nrl,nrh(i),ncl,nch);
00473    }
00474  }
00475 
00480  d4_array::d4_array(int hsl,int hsu,int sl,int sh,int nrl,
00481    int nrh,int ncl,int nch)
00482  {
00483    allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
00484  }
00485 
00490 d4_array::d4_array(int hsl,int hsu, int sl,int sh,ivector nrl,ivector nrh,
00491   ivector ncl,ivector nch)
00492 {
00493   allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
00494 }
00495 
00500  void d4_array::initialize()
00501  {
00502    if (!(!(*this)))  // only initialize allocated objects
00503    {
00504      for (int i=hslicemin();i<=hslicemax();i++)
00505      {
00506        elem(i).initialize();
00507      }
00508    }
00509  }
00510 
00515 d4_array::d4_array(int hsl, int hsu, int sl, const ivector& sh, int nrl,
00516   const imatrix& nrh, int ncl, const i3_array& nch)
00517 {
00518   allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
00519 }
00520 
00525 void d4_array::allocate(int hsl, int hsu, int sl, const ivector& sh, int nrl,
00526   const imatrix& nrh, int ncl, const i3_array& nch)
00527 {
00528   //int rmin=nrh.rowmin();
00529   //int cmin=nrh(rmin).indexmin();
00530   //int sl1=nch.slicemin();
00531   //int rmin1=nch(sl1).rowmin();
00532   //int cmin1=nch(sl1,rmin1).indexmin();
00533   if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00534   {
00535     cerr << " Error allocating memory in d4_array contructor\n";
00536   }
00537   int ss=hslicesize();
00538   if ( (t = new d3_array[ss]) == 0)
00539   {
00540     cerr << " Error allocating memory in d3_array contructor\n";
00541     ad_exit(21);
00542   }
00543   t -= hslicemin();
00544   for (int i=hsl; i<=hsu; i++)
00545   {
00546     (*this)(i).allocate(sl,sh(i),nrl,nrh(i),ncl,nch(i));
00547   }
00548 }
00549 
00554 d4_array::d4_array(int hsl,int hsu,const index_type& sl,
00555     const index_type& sh,const index_type& nrl,const index_type& nrh,
00556     const index_type& ncl,const index_type& nch)
00557 {
00558   allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
00559 }
00560 
00565 void d4_array::allocate(ad_integer hsl,ad_integer hsu,const index_type& sl,
00566     const index_type& sh,const index_type& nrl,const index_type& nrh,
00567     const index_type& ncl,const index_type& nch)
00568 {
00569    if (hsl>hsu)
00570    {
00571      allocate();
00572      return;
00573    }
00574   int ss=hsu-hsl+1;
00575   if ( (t = new d3_array[ss]) == 0)
00576   {
00577     cerr << " Error allocating memory in d3_array contructor\n";
00578     ad_exit(21);
00579   }
00580   if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00581   {
00582     cerr << " Error allocating memory in d3_array contructor\n";
00583     ad_exit(21);
00584   }
00585   t -= int(hsl);
00586   for (int i=hsl; i<=hsu; i++)
00587   {
00588     const index_type& rsl=sl[i];
00589     const index_type& rsh=sh[i];
00590     const index_type& rnrl=nrl[i];
00591     const index_type& rnrh=nrh[i];
00592     const index_type& rncl=ncl[i];
00593     const index_type& rnch=nch[i];
00594 
00595     const ad_integer& aa=ad_integer(rsl);
00596     const ad_integer& bb=ad_integer(rsh);
00597 
00598     (*this)(i).allocate(aa,bb,rnrl,rnrh,rncl,rnch);
00599 
00600     //(*this)(i).allocate(sl[i],sh[i],nrl[i],nrh[i],ncl[i],nch[i]);
00601   }
00602 }
00603 
00608 void d4_array::allocate(int hsl,int hsu,const index_type& sl,
00609     const index_type& sh,const index_type& nrl,const index_type& nrh,
00610     const index_type& ncl,const index_type& nch)
00611 {
00612    if (hsl>hsu)
00613    {
00614      allocate();
00615      return;
00616    }
00617   int ss=hsu-hsl+1;
00618   if ( (t = new d3_array[ss]) == 0)
00619   {
00620     cerr << " Error allocating memory in d3_array contructor\n";
00621     ad_exit(21);
00622   }
00623   if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00624   {
00625     cerr << " Error allocating memory in d3_array contructor\n";
00626     ad_exit(21);
00627   }
00628   t -= int(hsl);
00629   for (int i=hsl; i<=hsu; i++)
00630   {
00631     const index_type& rsl=sl[i];
00632     const index_type& rsh=sh[i];
00633     const index_type& rnrl=nrl[i];
00634     const index_type& rnrh=nrh[i];
00635     const index_type& rncl=ncl[i];
00636     const index_type& rnch=nch[i];
00637 
00638     const ad_integer& aa=ad_integer(rsl);
00639     const ad_integer& bb=ad_integer(rsh);
00640 
00641     (*this)(i).allocate(aa,bb,rnrl,rnrh,rncl,rnch);
00642 
00643     //(*this)(i).allocate(sl[i],sh[i],nrl[i],nrh[i],ncl[i],nch[i]);
00644   }
00645 }
00646 
00651 void d4_array::allocate(ad_integer hsl,ad_integer hsu,const index_type& sl,
00652     const index_type& sh,const index_type& nrl,const index_type& nrh)
00653 {
00654    if (hsl>hsu)
00655    {
00656      allocate();
00657      return;
00658    }
00659   int ss=hsu-hsl+1;
00660   if ( (t = new d3_array[ss]) == 0)
00661   {
00662     cerr << " Error allocating memory in d3_array contructor\n";
00663     ad_exit(21);
00664   }
00665   if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00666   {
00667     cerr << " Error allocating memory in d3_array contructor\n";
00668     ad_exit(21);
00669   }
00670   t -= int(hsl);
00671   for (int i=hsl; i<=hsu; i++)
00672   {
00673     const index_type& rsl=sl[i];
00674     const index_type& rsh=sh[i];
00675     const index_type& rnrl=nrl[i];
00676     const index_type& rnrh=nrh[i];
00677     const ad_integer& aa=ad_integer(rsl);
00678     const ad_integer& bb=ad_integer(rsh);
00679     (*this)(i).allocate(aa,bb,rnrl,rnrh);
00680   }
00681 }
00682 
00687 void d4_array::allocate(ad_integer hsl,ad_integer hsu,const index_type& sl,
00688     const index_type& sh)
00689 {
00690    if (hsl>hsu)
00691    {
00692      allocate();
00693      return;
00694    }
00695   int ss=hsu-hsl+1;
00696   if ( (t = new d3_array[ss]) == 0)
00697   {
00698     cerr << " Error allocating memory in d3_array contructor\n";
00699     ad_exit(21);
00700   }
00701   if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00702   {
00703     cerr << " Error allocating memory in d3_array contructor\n";
00704     ad_exit(21);
00705   }
00706   t -= int(hsl);
00707   for (int i=hsl; i<=hsu; i++)
00708   {
00709     const index_type& rsl=sl[i];
00710     const index_type& rsh=sh[i];
00711     const ad_integer& aa=ad_integer(rsl);
00712     const ad_integer& bb=ad_integer(rsh);
00713     (*this)(i).allocate(aa,bb);
00714   }
00715 }
00716 
00721 void d4_array::allocate(ad_integer hsl,ad_integer hsu)
00722 {
00723    if (hsl>hsu)
00724    {
00725      allocate();
00726      return;
00727    }
00728   int ss=hsu-hsl+1;
00729   if ( (t = new d3_array[ss]) == 0)
00730   {
00731     cerr << " Error allocating memory in d3_array contructor\n";
00732     ad_exit(21);
00733   }
00734   if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00735   {
00736     cerr << " Error allocating memory in d3_array contructor\n";
00737     ad_exit(21);
00738   }
00739   t -= int(hsl);
00740   for (int i=hsl; i<=hsu; i++)
00741   {
00742     (*this)(i).allocate();
00743   }
00744 }