ADMB Documentation  11.1.1897
 All Classes Files Functions Variables Typedefs Friends Defines
f4arr.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: f4arr.cpp 1715 2014-03-03 19:38:48Z 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  dvar4_array::dvar4_array(int nrl,int nrh)
00020  {
00021    allocate(nrl,nrh);
00022  }
00023 
00028  dvar4_array::dvar4_array(const dvar4_array& m2)
00029  {
00030    if (m2.shape)
00031    {
00032      shape=m2.shape;
00033      (shape->ncopies)++;
00034      t = m2.t;
00035    }
00036    else
00037    {
00038      shape=NULL;
00039      t=NULL;
00040    }
00041  }
00042 
00047  void dvar4_array::shallow_copy(const dvar4_array& m2)
00048  {
00049    if (m2.shape)
00050    {
00051      shape=m2.shape;
00052      (shape->ncopies)++;
00053      t = m2.t;
00054    }
00055    else
00056    {
00057      shape=NULL;
00058      t=NULL;
00059    }
00060  }
00061 
00066  dvar4_array dvar4_array::sub(int nrl,int nrh)
00067  {
00068    if (allocated(*this))
00069    {
00070      dvar4_array tmp(nrl,nrh);
00071      for (int i=nrl; i<=nrh; i++)
00072      {
00073        tmp[i].shallow_copy((*this)(i));
00074      }
00075      return tmp;
00076    }
00077    else
00078    {
00079      return *this;
00080    }
00081  }
00082 
00087  void dvar4_array::deallocate()
00088  {
00089    if (shape)
00090    {
00091      if (shape->ncopies)
00092      {
00093        (shape->ncopies)--;
00094      }
00095      else
00096      {
00097        t += hslicemin();
00098        delete [] t;
00099        t=NULL;
00100        delete shape;
00101        shape=NULL;
00102      }
00103    }
00104 #    if defined(SAKE_ARRAYS)
00105    else
00106    {
00107      cerr << "Warning -- trying to deallocate an unallocated d4_array"<<endl;
00108    }
00109 #    endif
00110  }
00111 
00115 dvar4_array::~dvar4_array()
00116 {
00117   deallocate();
00118 }
00119 
00120 #ifndef OPT_LIB
00121 
00126     dvar3_array& dvar4_array::operator ( ) (int i)
00127     {
00128       #ifdef SAFE_ARRAYS
00129       if (i < hslicemin() || i > hslicemax())
00130       {
00131         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00132         "dvar3_array& dvar4_array::operator() (int i)",
00133         hslicemin(), hslicemax(), i);
00134       }
00135       #endif
00136       return t[i];
00137     }
00138 
00143     dvar3_array& dvar4_array::operator [] (int i)
00144     {
00145       #ifdef SAFE_ARRAYS
00146       if (i < hslicemin() || i > hslicemax())
00147       {
00148         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00149         "dvar3_array& dvar4_array::operator[] (int i)",
00150         hslicemin(), hslicemax(), i);
00151       }
00152       #endif
00153       return t[i];
00154     }
00155 
00160     dvar_matrix& dvar4_array::operator ( ) (int i ,int j)
00161     {
00162       #ifdef SAFE_ARRAYS
00163       if (i < hslicemin() || i > hslicemax())
00164       {
00165         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00166         "dvar_matrix& dvar4_array::operator() (int i, int j)",
00167         hslicemin(), hslicemax(), i);
00168       }
00169       #endif
00170       return ((*this)(i))(j);
00171     }
00172 
00177     dvar_vector& dvar4_array::operator ( ) (int i,int j,int k)
00178     {
00179       #ifdef SAFE_ARRAYS
00180       if (i < hslicemin() || i > hslicemax())
00181       {
00182         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00183         "dvar_vector& dvar4_array::operator() (int i, int j, int k)",
00184         hslicemin(), hslicemax(), i);
00185       }
00186       #endif
00187       return (((*this)(i,j))(k));
00188     }
00189 
00194     prevariable dvar4_array::operator ( ) (int i,int j,int k,int l)
00195     {
00196       #ifdef SAFE_ARRAYS
00197       if (i < hslicemin() || i > hslicemax())
00198       {
00199         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00200         "prevariable& dvar4_array::operator() (int i, int j, int k)",
00201         hslicemin(), hslicemax(), i);
00202       }
00203       #endif
00204       return ( ((*this)(i,j,k))(l));
00205     }
00206 
00211 const dvar3_array& dvar4_array::operator()(int i) const
00212     {
00213       #ifdef SAFE_ARRAYS
00214         if (i<hslicemin()||i>hslicemax())
00215         { cerr << "Error hslice index out of bounds in\n"
00216             "dvar3_array& dvar4_array::operator ( )" << endl;
00217           ad_exit(1);
00218         }
00219       #endif
00220       return t[i];
00221     }
00222 
00227 const dvar3_array& dvar4_array::operator[](int i) const
00228     {
00229       #ifdef SAFE_ARRAYS
00230         if (i<hslicemin()||i>hslicemax())
00231         { cerr << "Error hslice index out of bounds in\n"
00232             "dvar3_array& dvar4_array::operator ( )" << endl;
00233           ad_exit(1);
00234         }
00235       #endif
00236       return t[i];
00237     }
00238 
00243 const dvar_matrix& dvar4_array::operator()(int i, int j) const
00244     {
00245       #ifdef SAFE_ARRAYS
00246         if (i<hslicemin()||i>hslicemax())
00247         { cerr << "Error hslice index out of bounds in\n"
00248             "dvarmatrix& dvar4_array::operator ( )" << endl;
00249           ad_exit(1);
00250         }
00251       #endif
00252       return ((*this)(i))(j);
00253     }
00254 
00259 const dvar_vector& dvar4_array::operator()(int i, int j, int k) const
00260     {
00261       #ifdef SAFE_ARRAYS
00262         if (i<hslicemin()||i>hslicemax())
00263         { cerr << "Error hslice index out of bounds in\n"
00264             "dvarvector& dvar4_array::operator ( )" << endl;
00265           ad_exit(1);
00266         }
00267       #endif
00268       return (((*this)(i,j))(k));
00269     }
00270 
00275 const prevariable dvar4_array::operator()(int i, int j, int k, int l) const
00276     {
00277       #ifdef SAFE_ARRAYS
00278         if (i<hslicemin()||i>hslicemax())
00279         { cerr << "Error hslice index out of bounds in\n"
00280             "double& dvar4_array::operator ( )"  << endl;
00281           ad_exit(1);
00282         }
00283       #endif
00284       return ( ((*this)(i,j,k))(l));
00285     }
00286 #endif
00287 
00292 dvar4_array& dvar4_array::operator=(const d4_array& m)
00293  {
00294    int mmin=hslicemin();
00295    int mmax=hslicemax();
00296    if (mmin!=m.hslicemin() || mmax!=m.hslicemax())
00297    {
00298      cerr << "Incompatible bounds in"
00299       " dvar4_array& dvar4_array:: operator =  (const dvar4_array& m)"
00300       << endl;
00301      ad_exit(1);
00302     }
00303    for (int i=mmin; i<=mmax; i++)
00304    {
00305      (*this)(i)=m(i);
00306    }
00307    return *this;
00308  }
00309 
00314  dvar4_array& dvar4_array::operator=(const dvar4_array& m)
00315  {
00316    int mmin=hslicemin();
00317    int mmax=hslicemax();
00318    if (mmin!=m.hslicemin() || mmax!=m.hslicemax())
00319    {
00320      cerr << "Incompatible bounds in"
00321       " dvar4_array& dvar4_array:: operator =  (const dvar4_array& m)"
00322       << endl;
00323      ad_exit(1);
00324     }
00325    for (int i=mmin; i<=mmax; i++)
00326    {
00327      (*this)(i)=m(i);
00328    }
00329    return *this;
00330  }
00331 
00336 void dvar4_array::allocate(int hsl,int hsu,int sl,int sh,int nrl,
00337    int nrh,int ncl,int nch)
00338  {
00339    if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00340    {
00341      cerr << " Error allocating memory in dvar3_array contructor\n";
00342    }
00343    int ss=hslicesize();
00344    if ( (t = new dvar3_array[ss]) == 0)
00345    {
00346      cerr << " Error allocating memory in dvar3_array contructor\n";
00347      ad_exit(21);
00348    }
00349    t -= hslicemin();
00350    for (int i=hsl; i<=hsu; i++)
00351    {
00352      (*this)(i).allocate(sl,sh,nrl,nrh,ncl,nch);
00353    }
00354  }
00355 
00360 void dvar4_array::allocate(int hsl, int hsu, int sl, int sh, int nrl,
00361   int nrh, const ivector& ncl, const ivector& nch)
00362  {
00363    if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00364    {
00365      cerr << " Error allocating memory in dvar4_array contructor\n";
00366    }
00367 
00368    int ss=hslicesize();
00369    if ( (t = new dvar3_array[ss]) == 0)
00370    {
00371      cerr << " Error allocating memory in dvar3_array contructor\n";
00372      ad_exit(21);
00373    }
00374    t -= hslicemin();
00375    for (int i=hsl; i<=hsu; i++)
00376    {
00377      (*this)(i).allocate(sl,sh,nrl,nrh,ncl,nch);
00378    }
00379  }
00380 
00385  void dvar4_array::allocate(ad_integer hsl,ad_integer hsu,const index_type& sl,
00386    const index_type& sh,const index_type& nrl,
00387    const index_type& nrh, const index_type& ncl,const index_type& nch)
00388  {
00389    if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00390    {
00391      cerr << " Error allocating memory in dvar4_array contructor\n";
00392    }
00393    int ss=hslicesize();
00394    if ( (t = new dvar3_array[ss]) == 0)
00395    {
00396      cerr << " Error allocating memory in dvar3_array contructor\n";
00397      ad_exit(21);
00398    }
00399    t -= hslicemin();
00400    int il=hsl;
00401    int iu=hsu;
00402    for (int i=il; i<=iu; i++)
00403    {
00404      (*this)(i).allocate(sl(i),sh(i),nrl(i),nrh(i),ncl(i),nch(i));
00405    }
00406  }
00407 
00412  void dvar4_array::allocate(ad_integer hsl,ad_integer hsu,const index_type& sl,
00413    const index_type& sh,const index_type& nrl,
00414    const index_type& nrh)
00415  {
00416    if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00417    {
00418      cerr << " Error allocating memory in dvar4_array contructor\n";
00419    }
00420    int ss=hslicesize();
00421    if ( (t = new dvar3_array[ss]) == 0)
00422    {
00423      cerr << " Error allocating memory in dvar3_array contructor\n";
00424      ad_exit(21);
00425    }
00426    t -= hslicemin();
00427    int il=hsl;
00428    int iu=hsu;
00429    for (int i=il; i<=iu; i++)
00430    {
00431      (*this)(i).allocate(sl(i),sh(i),nrl(i),nrh(i));
00432    }
00433  }
00434 
00439  void dvar4_array::allocate(ad_integer hsl,ad_integer hsu,const index_type& sl,
00440    const index_type& sh)
00441  {
00442    if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00443    {
00444      cerr << " Error allocating memory in dvar4_array contructor\n";
00445    }
00446    int ss=hslicesize();
00447    if ( (t = new dvar3_array[ss]) == 0)
00448    {
00449      cerr << " Error allocating memory in dvar3_array contructor\n";
00450      ad_exit(21);
00451    }
00452    t -= hslicemin();
00453    int il=hsl;
00454    int iu=hsu;
00455    for (int i=il; i<=iu; i++)
00456    {
00457      (*this)(i).allocate(sl(i),sh(i));
00458    }
00459  }
00460 
00465  void dvar4_array::allocate(ad_integer hsl,ad_integer hsu)
00466  {
00467    if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00468    {
00469      cerr << " Error allocating memory in dvar4_array contructor\n";
00470    }
00471    int ss=hslicesize();
00472    if ( (t = new dvar3_array[ss]) == 0)
00473    {
00474      cerr << " Error allocating memory in dvar3_array contructor\n";
00475      ad_exit(21);
00476    }
00477    t -= hslicemin();
00478    int il=hsl;
00479    int iu=hsu;
00480    for (int i=il; i<=iu; i++)
00481    {
00482      (*this)(i).allocate();
00483    }
00484  }
00485 
00490 void dvar4_array::allocate(int hsl, int hsu, int sl, int sh, const ivector& nrl,
00491   const ivector& nrh, const ivector& ncl, const ivector& nch)
00492  {
00493    if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00494    {
00495      cerr << " Error allocating memory in dvar4_array contructor\n";
00496    }
00497    int ss=hslicesize();
00498    if ( (t = new dvar3_array[ss]) == 0)
00499    {
00500      cerr << " Error allocating memory in dvar3_array contructor\n";
00501      ad_exit(21);
00502    }
00503    t -= hslicemin();
00504    for (int i=hsl; i<=hsu; i++)
00505    {
00506      (*this)(i).allocate(sl,sh,nrl(i),nrh(i),ncl(i),nch(i));
00507    }
00508  }
00509 
00510 
00515  dvar4_array::dvar4_array(int hsl,int hsu,int sl,int sh,int nrl,
00516    int nrh,int ncl,int nch)
00517  {
00518    allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
00519  }
00520 
00525  dvar4_array::dvar4_array(ad_integer hsl,ad_integer hsu,const index_type& sl,
00526    const index_type& sh, const index_type& nrl,const index_type& nrh,
00527    const index_type& ncl,const index_type& nch)
00528  {
00529    allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
00530  }
00531 
00536  dvar4_array::dvar4_array(int hsl,int hsu, int sl,int sh,ivector nrl,
00537    ivector nrh,ivector ncl,ivector nch)
00538  {
00539    allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
00540  }
00541 
00546  void dvar4_array::initialize()
00547  {
00548    if (!(!(*this)))  // only initialize allocated objects
00549    {
00550      for (int i=hslicemin();i<=hslicemax();i++)
00551      {
00552        elem(i).initialize();
00553      }
00554    }
00555  }
00556 
00561 dvar4_array::dvar4_array(int hsl, int hsu, int sl, const ivector& sh,
00562   int nrl, const imatrix& nrh, int ncl, int nch)
00563  {
00564    allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
00565  }
00566 
00571 void dvar4_array::allocate(int hsl, int hsu, int sl, const ivector& sh,
00572   int nrl, const imatrix& nrh, int ncl, int nch)
00573  {
00574    //int rmin=nrh.rowmin();
00575    //int cmin=nrh(rmin).indexmin();
00576    if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00577    {
00578      cerr << " Error allocating memory in d4_array contructor\n";
00579    }
00580 
00581    int ss=hslicesize();
00582    if ( (t = new dvar3_array[ss]) == 0)
00583    {
00584      cerr << " Error allocating memory in d3_array contructor\n";
00585      ad_exit(21);
00586    }
00587    t -= hslicemin();
00588    for (int i=hsl; i<=hsu; i++)
00589    {
00590      (*this)(i).allocate(sl,sh(i),nrl,nrh(i),ncl,nch);
00591    }
00592  }
00593 
00598  dvar4_array::dvar4_array(const d4_array& m1)
00599  {
00600    allocate(m1);
00601    for (int i=hslicemin(); i<=hslicemax(); i++)
00602    {
00603      t[i]=m1[i];
00604    }
00605  }
00606 
00611  void dvar4_array::allocate(const d4_array& m1)
00612  {
00613    if ( (shape=new four_array_shape(m1.hslicemin(),m1.hslicemax()))
00614        == 0)
00615    {
00616      cerr << " Error allocating memory in dvar4_array contructor" << endl;
00617    }
00618    int ss=hslicesize();
00619    if ( (t = new dvar3_array[ss]) == 0)
00620    {
00621      cerr << " Error allocating memory in dvar4_array contructor" << endl;
00622      ad_exit(21);
00623    }
00624    t -= hslicemin();
00625    for (int i=hslicemin(); i<=hslicemax(); i++)
00626    {
00627      t[i].allocate(m1[i]);
00628    }
00629  }
00630 
00635  void dvar4_array::allocate(const dvar4_array& m1)
00636  {
00637    if ( (shape=new four_array_shape(m1.hslicemin(),m1.hslicemax()))
00638        == 0)
00639    {
00640      cerr << " Error allocating memory in dvar4_array contructor" << endl;
00641    }
00642    int ss=hslicesize();
00643    if ( (t = new dvar3_array[ss]) == 0)
00644    {
00645      cerr << " Error allocating memory in dvar4_array contructor" << endl;
00646      ad_exit(21);
00647    }
00648    t -= hslicemin();
00649    for (int i=hslicemin(); i<=hslicemax(); i++)
00650    {
00651      t[i].allocate(m1[i]);
00652    }
00653  }
00654 
00655 /*
00656 dvar4_array::dvar4_array(int hsl,int hsu, int sl, const ivector& sh,int nrl,
00657     const imatrix& nrh,int ncl,int nch)
00658   {
00659     allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
00660   }
00661 
00662   void dvar4_array::allocate(int hsl,int hsu, int sl, const ivector& sh,int nrl,
00663     const imatrix& nrh,int ncl,int nch)
00664   {
00665    int rmin=nrh.rowmin();
00666    int cmin=nrh(nrh.rowmin()).indexmin;
00667    if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00668    {
00669      cerr << " Error allocating memory in dvar4_array contructor\n";
00670    }
00671    int ss=hslicesize();
00672    if ( (t = new dvar3_array[ss]) == 0)
00673    {
00674      cerr << " Error allocating memory in dvar3_array contructor\n";
00675      ad_exit(21);
00676    }
00677    t -= hslicemin();
00678    for (int i=hsl; i<=hsu; i++)
00679    {
00680      (*this)(i).allocate(sl,sh(i),nrl,nrh(i),ncl,nch(i));
00681    }
00682  }
00683 */