ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
f4arr.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  dvar4_array::dvar4_array(int nrl,int nrh)
00019  {
00020    allocate(nrl,nrh);
00021  }
00022 
00027  dvar4_array::dvar4_array(const dvar4_array& m2)
00028  {
00029    if (m2.shape)
00030    {
00031      shape=m2.shape;
00032      (shape->ncopies)++;
00033      t = m2.t;
00034    }
00035    else
00036    {
00037      shape=NULL;
00038      t=NULL;
00039    }
00040  }
00041 
00046  void dvar4_array::shallow_copy(const dvar4_array& m2)
00047  {
00048    if (m2.shape)
00049    {
00050      shape=m2.shape;
00051      (shape->ncopies)++;
00052      t = m2.t;
00053    }
00054    else
00055    {
00056      shape=NULL;
00057      t=NULL;
00058    }
00059  }
00060 
00065  dvar4_array dvar4_array::sub(int nrl,int nrh)
00066  {
00067    if (allocated(*this))
00068    {
00069      dvar4_array tmp(nrl,nrh);
00070      for (int i=nrl; i<=nrh; i++)
00071      {
00072        tmp[i].shallow_copy((*this)(i));
00073      }
00074      return tmp;
00075    }
00076    else
00077    {
00078      return *this;
00079    }
00080  }
00081 
00086  void dvar4_array::deallocate()
00087  {
00088    if (shape)
00089    {
00090      if (shape->ncopies)
00091      {
00092        (shape->ncopies)--;
00093      }
00094      else
00095      {
00096        t += hslicemin();
00097        delete [] t;
00098        t=NULL;
00099        delete shape;
00100        shape=NULL;
00101      }
00102    }
00103 #    if defined(SAKE_ARRAYS)
00104    else
00105    {
00106      cerr << "Warning -- trying to deallocate an unallocated d4_array"<<endl;
00107    }
00108 #    endif
00109  }
00110 
00114 dvar4_array::~dvar4_array()
00115 {
00116   deallocate();
00117 }
00118 
00119 #ifndef OPT_LIB
00120 
00125     dvar3_array& dvar4_array::operator ( ) (int i)
00126     {
00127       if (i < hslicemin() || i > hslicemax())
00128       {
00129         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00130         "dvar3_array& dvar4_array::operator() (int i)",
00131         hslicemin(), hslicemax(), i);
00132       }
00133       return t[i];
00134     }
00135 
00140     dvar3_array& dvar4_array::operator [] (int i)
00141     {
00142       if (i < hslicemin() || i > hslicemax())
00143       {
00144         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00145         "dvar3_array& dvar4_array::operator[] (int i)",
00146         hslicemin(), hslicemax(), i);
00147       }
00148       return t[i];
00149     }
00150 
00155     dvar_matrix& dvar4_array::operator ( ) (int i ,int j)
00156     {
00157       if (i < hslicemin() || i > hslicemax())
00158       {
00159         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00160         "dvar_matrix& dvar4_array::operator() (int i, int j)",
00161         hslicemin(), hslicemax(), i);
00162       }
00163       return ((*this)(i))(j);
00164     }
00165 
00170     dvar_vector& dvar4_array::operator ( ) (int i,int j,int k)
00171     {
00172       if (i < hslicemin() || i > hslicemax())
00173       {
00174         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00175         "dvar_vector& dvar4_array::operator() (int i, int j, int k)",
00176         hslicemin(), hslicemax(), i);
00177       }
00178       return (((*this)(i,j))(k));
00179     }
00180 
00185     prevariable dvar4_array::operator ( ) (int i,int j,int k,int l)
00186     {
00187       if (i < hslicemin() || i > hslicemax())
00188       {
00189         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00190         "prevariable& dvar4_array::operator() (int i, int j, int k)",
00191         hslicemin(), hslicemax(), i);
00192       }
00193       return ( ((*this)(i,j,k))(l));
00194     }
00195 
00200 const dvar3_array& dvar4_array::operator()(int i) const
00201     {
00202         if (i<hslicemin()||i>hslicemax())
00203         { cerr << "Error hslice index out of bounds in\n"
00204             "dvar3_array& dvar4_array::operator ( )" << endl;
00205           ad_exit(1);
00206         }
00207       return t[i];
00208     }
00209 
00214 const dvar3_array& dvar4_array::operator[](int i) const
00215     {
00216         if (i<hslicemin()||i>hslicemax())
00217         { cerr << "Error hslice index out of bounds in\n"
00218             "dvar3_array& dvar4_array::operator ( )" << endl;
00219           ad_exit(1);
00220         }
00221       return t[i];
00222     }
00223 
00228 const dvar_matrix& dvar4_array::operator()(int i, int j) const
00229     {
00230         if (i<hslicemin()||i>hslicemax())
00231         { cerr << "Error hslice index out of bounds in\n"
00232             "dvarmatrix& dvar4_array::operator ( )" << endl;
00233           ad_exit(1);
00234         }
00235       return ((*this)(i))(j);
00236     }
00237 
00242 const dvar_vector& dvar4_array::operator()(int i, int j, int k) const
00243     {
00244         if (i<hslicemin()||i>hslicemax())
00245         { cerr << "Error hslice index out of bounds in\n"
00246             "dvarvector& dvar4_array::operator ( )" << endl;
00247           ad_exit(1);
00248         }
00249       return (((*this)(i,j))(k));
00250     }
00251 
00256 const prevariable dvar4_array::operator()(int i, int j, int k, int l) const
00257     {
00258         if (i<hslicemin()||i>hslicemax())
00259         { cerr << "Error hslice index out of bounds in\n"
00260             "double& dvar4_array::operator ( )"  << endl;
00261           ad_exit(1);
00262         }
00263       return ( ((*this)(i,j,k))(l));
00264     }
00265 #endif
00266 
00271 dvar4_array& dvar4_array::operator=(const d4_array& m)
00272  {
00273    int mmin=hslicemin();
00274    int mmax=hslicemax();
00275    if (mmin!=m.hslicemin() || mmax!=m.hslicemax())
00276    {
00277      cerr << "Incompatible bounds in"
00278       " dvar4_array& dvar4_array:: operator =  (const dvar4_array& m)"
00279       << endl;
00280      ad_exit(1);
00281     }
00282    for (int i=mmin; i<=mmax; i++)
00283    {
00284      (*this)(i)=m(i);
00285    }
00286    return *this;
00287  }
00288 
00293  dvar4_array& dvar4_array::operator=(const dvar4_array& m)
00294  {
00295    int mmin=hslicemin();
00296    int mmax=hslicemax();
00297    if (mmin!=m.hslicemin() || mmax!=m.hslicemax())
00298    {
00299      cerr << "Incompatible bounds in"
00300       " dvar4_array& dvar4_array:: operator =  (const dvar4_array& m)"
00301       << endl;
00302      ad_exit(1);
00303     }
00304    for (int i=mmin; i<=mmax; i++)
00305    {
00306      (*this)(i)=m(i);
00307    }
00308    return *this;
00309  }
00310 
00315 void dvar4_array::allocate(int hsl,int hsu,int sl,int sh,int nrl,
00316    int nrh,int ncl,int nch)
00317  {
00318    if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00319    {
00320      cerr << " Error allocating memory in dvar3_array contructor\n";
00321    }
00322    int ss=hslicesize();
00323    if ( (t = new dvar3_array[ss]) == 0)
00324    {
00325      cerr << " Error allocating memory in dvar3_array contructor\n";
00326      ad_exit(21);
00327    }
00328    t -= hslicemin();
00329    for (int i=hsl; i<=hsu; i++)
00330    {
00331      (*this)(i).allocate(sl,sh,nrl,nrh,ncl,nch);
00332    }
00333  }
00334 
00339 void dvar4_array::allocate(int hsl, int hsu, int sl, int sh, int nrl,
00340   int nrh, const ivector& ncl, const ivector& nch)
00341  {
00342    if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00343    {
00344      cerr << " Error allocating memory in dvar4_array contructor\n";
00345    }
00346 
00347    int ss=hslicesize();
00348    if ( (t = new dvar3_array[ss]) == 0)
00349    {
00350      cerr << " Error allocating memory in dvar3_array contructor\n";
00351      ad_exit(21);
00352    }
00353    t -= hslicemin();
00354    for (int i=hsl; i<=hsu; i++)
00355    {
00356      (*this)(i).allocate(sl,sh,nrl,nrh,ncl,nch);
00357    }
00358  }
00359 
00364  void dvar4_array::allocate(ad_integer hsl,ad_integer hsu,const index_type& sl,
00365    const index_type& sh,const index_type& nrl,
00366    const index_type& nrh, const index_type& ncl,const index_type& nch)
00367  {
00368    if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00369    {
00370      cerr << " Error allocating memory in dvar4_array contructor\n";
00371    }
00372    int ss=hslicesize();
00373    if ( (t = new dvar3_array[ss]) == 0)
00374    {
00375      cerr << " Error allocating memory in dvar3_array contructor\n";
00376      ad_exit(21);
00377    }
00378    t -= hslicemin();
00379    int il=hsl;
00380    int iu=hsu;
00381    for (int i=il; i<=iu; i++)
00382    {
00383      (*this)(i).allocate(sl(i),sh(i),nrl(i),nrh(i),ncl(i),nch(i));
00384    }
00385  }
00386 
00391  void dvar4_array::allocate(ad_integer hsl,ad_integer hsu,const index_type& sl,
00392    const index_type& sh,const index_type& nrl,
00393    const index_type& nrh)
00394  {
00395    if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00396    {
00397      cerr << " Error allocating memory in dvar4_array contructor\n";
00398    }
00399    int ss=hslicesize();
00400    if ( (t = new dvar3_array[ss]) == 0)
00401    {
00402      cerr << " Error allocating memory in dvar3_array contructor\n";
00403      ad_exit(21);
00404    }
00405    t -= hslicemin();
00406    int il=hsl;
00407    int iu=hsu;
00408    for (int i=il; i<=iu; i++)
00409    {
00410      (*this)(i).allocate(sl(i),sh(i),nrl(i),nrh(i));
00411    }
00412  }
00413 
00418  void dvar4_array::allocate(ad_integer hsl,ad_integer hsu,const index_type& sl,
00419    const index_type& sh)
00420  {
00421    if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00422    {
00423      cerr << " Error allocating memory in dvar4_array contructor\n";
00424    }
00425    int ss=hslicesize();
00426    if ( (t = new dvar3_array[ss]) == 0)
00427    {
00428      cerr << " Error allocating memory in dvar3_array contructor\n";
00429      ad_exit(21);
00430    }
00431    t -= hslicemin();
00432    int il=hsl;
00433    int iu=hsu;
00434    for (int i=il; i<=iu; i++)
00435    {
00436      (*this)(i).allocate(sl(i),sh(i));
00437    }
00438  }
00439 
00444  void dvar4_array::allocate(ad_integer hsl,ad_integer hsu)
00445  {
00446    if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00447    {
00448      cerr << " Error allocating memory in dvar4_array contructor\n";
00449    }
00450    int ss=hslicesize();
00451    if ( (t = new dvar3_array[ss]) == 0)
00452    {
00453      cerr << " Error allocating memory in dvar3_array contructor\n";
00454      ad_exit(21);
00455    }
00456    t -= hslicemin();
00457    int il=hsl;
00458    int iu=hsu;
00459    for (int i=il; i<=iu; i++)
00460    {
00461      (*this)(i).allocate();
00462    }
00463  }
00464 
00469 void dvar4_array::allocate(int hsl, int hsu, int sl, int sh, const ivector& nrl,
00470   const ivector& nrh, const ivector& ncl, const ivector& nch)
00471  {
00472    if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00473    {
00474      cerr << " Error allocating memory in dvar4_array contructor\n";
00475    }
00476    int ss=hslicesize();
00477    if ( (t = new dvar3_array[ss]) == 0)
00478    {
00479      cerr << " Error allocating memory in dvar3_array contructor\n";
00480      ad_exit(21);
00481    }
00482    t -= hslicemin();
00483    for (int i=hsl; i<=hsu; i++)
00484    {
00485      (*this)(i).allocate(sl,sh,nrl(i),nrh(i),ncl(i),nch(i));
00486    }
00487  }
00488 
00489 
00494  dvar4_array::dvar4_array(int hsl,int hsu,int sl,int sh,int nrl,
00495    int nrh,int ncl,int nch)
00496  {
00497    allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
00498  }
00499 
00504  dvar4_array::dvar4_array(ad_integer hsl,ad_integer hsu,const index_type& sl,
00505    const index_type& sh, const index_type& nrl,const index_type& nrh,
00506    const index_type& ncl,const index_type& nch)
00507  {
00508    allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
00509  }
00510 
00515  dvar4_array::dvar4_array(int hsl,int hsu, int sl,int sh,ivector nrl,
00516    ivector nrh,ivector ncl,ivector nch)
00517  {
00518    allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
00519  }
00520 
00525  void dvar4_array::initialize()
00526  {
00527    if (!(!(*this)))  // only initialize allocated objects
00528    {
00529      for (int i=hslicemin();i<=hslicemax();i++)
00530      {
00531        elem(i).initialize();
00532      }
00533    }
00534  }
00535 
00540 dvar4_array::dvar4_array(int hsl, int hsu, int sl, const ivector& sh,
00541   int nrl, const imatrix& nrh, int ncl, int nch)
00542  {
00543    allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
00544  }
00545 
00550 void dvar4_array::allocate(int hsl, int hsu, int sl, const ivector& sh,
00551   int nrl, const imatrix& nrh, int ncl, int nch)
00552  {
00553    //int rmin=nrh.rowmin();
00554    //int cmin=nrh(rmin).indexmin();
00555    if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00556    {
00557      cerr << " Error allocating memory in d4_array contructor\n";
00558    }
00559 
00560    int ss=hslicesize();
00561    if ( (t = new dvar3_array[ss]) == 0)
00562    {
00563      cerr << " Error allocating memory in d3_array contructor\n";
00564      ad_exit(21);
00565    }
00566    t -= hslicemin();
00567    for (int i=hsl; i<=hsu; i++)
00568    {
00569      (*this)(i).allocate(sl,sh(i),nrl,nrh(i),ncl,nch);
00570    }
00571  }
00572 
00577  dvar4_array::dvar4_array(const d4_array& m1)
00578  {
00579    allocate(m1);
00580    for (int i=hslicemin(); i<=hslicemax(); i++)
00581    {
00582      t[i]=m1[i];
00583    }
00584  }
00585 
00590  void dvar4_array::allocate(const d4_array& m1)
00591  {
00592    if ( (shape=new four_array_shape(m1.hslicemin(),m1.hslicemax()))
00593        == 0)
00594    {
00595      cerr << " Error allocating memory in dvar4_array contructor" << endl;
00596    }
00597    int ss=hslicesize();
00598    if ( (t = new dvar3_array[ss]) == 0)
00599    {
00600      cerr << " Error allocating memory in dvar4_array contructor" << endl;
00601      ad_exit(21);
00602    }
00603    t -= hslicemin();
00604    for (int i=hslicemin(); i<=hslicemax(); i++)
00605    {
00606      t[i].allocate(m1[i]);
00607    }
00608  }
00609 
00614  void dvar4_array::allocate(const dvar4_array& m1)
00615  {
00616    if ( (shape=new four_array_shape(m1.hslicemin(),m1.hslicemax()))
00617        == 0)
00618    {
00619      cerr << " Error allocating memory in dvar4_array contructor" << endl;
00620    }
00621    int ss=hslicesize();
00622    if ( (t = new dvar3_array[ss]) == 0)
00623    {
00624      cerr << " Error allocating memory in dvar4_array contructor" << endl;
00625      ad_exit(21);
00626    }
00627    t -= hslicemin();
00628    for (int i=hslicemin(); i<=hslicemax(); i++)
00629    {
00630      t[i].allocate(m1[i]);
00631    }
00632  }
00633 
00634 /*
00635 dvar4_array::dvar4_array(int hsl,int hsu, int sl, const ivector& sh,int nrl,
00636     const imatrix& nrh,int ncl,int nch)
00637   {
00638     allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
00639   }
00640 
00641   void dvar4_array::allocate(int hsl,int hsu, int sl, const ivector& sh,int nrl,
00642     const imatrix& nrh,int ncl,int nch)
00643   {
00644    int rmin=nrh.rowmin();
00645    int cmin=nrh(nrh.rowmin()).indexmin;
00646    if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00647    {
00648      cerr << " Error allocating memory in dvar4_array contructor\n";
00649    }
00650    int ss=hslicesize();
00651    if ( (t = new dvar3_array[ss]) == 0)
00652    {
00653      cerr << " Error allocating memory in dvar3_array contructor\n";
00654      ad_exit(21);
00655    }
00656    t -= hslicemin();
00657    for (int i=hsl; i<=hsu; i++)
00658    {
00659      (*this)(i).allocate(sl,sh(i),nrl,nrh(i),ncl,nch(i));
00660    }
00661  }
00662 */