ADMB Documentation  11.1.1916
 All Classes Files Functions Variables Typedefs Friends Defines
d6arr.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: d6arr.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  void d6_array::initialize(void)
00020  {
00021    int mmin=indexmin();
00022    int mmax=indexmax();
00023    for (int i=mmin; i<=mmax; i++)
00024    {
00025      (*this)(i).initialize();
00026    }
00027  }
00028 
00033  d6_array::d6_array(const d6_array& _m2)
00034  {
00035    d6_array& m2=(d6_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 d6_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 d6_array::~d6_array()
00082 {
00083   deallocate();
00084 }
00085 
00090 d6_array& d6_array::operator=(const d6_array& m)
00091  {
00092    int mmin=indexmin();
00093    int mmax=indexmax();
00094    if (mmin!=m.indexmin() || mmax!=m.indexmax())
00095    {
00096      cerr << "Incompatible bounds in"
00097       " d6_array& d6_array:: operator =  (const d6_array& m)"
00098       << endl;
00099      ad_exit(1);
00100     }
00101    for (int i=mmin; i<=mmax; i++)
00102    {
00103      (*this)(i)=m(i);
00104    }
00105    return *this;
00106  }
00107 
00112 void d6_array::allocate(const d6_array& m1)
00113  {
00114    if ( (shape=new vector_shape(m1.indexmin(),m1.indexmax()))
00115        == 0)
00116    {
00117      cerr << " Error allocating memory in d5_array contructor" << endl;
00118    }
00119    int ss=size();
00120    if ( (t = new d5_array[ss]) == 0)
00121    {
00122      cerr << " Error allocating memory in d5_array contructor" << endl;
00123      ad_exit(21);
00124    }
00125    t -= indexmin();
00126    for (int i=indexmin(); i<=indexmax(); i++)
00127    {
00128      t[i].allocate(m1[i]);
00129    }
00130  }
00131 
00132   #ifndef OPT_LIB
00133 
00138     d5_array& d6_array::operator ( ) (int i)
00139     {
00140       #ifdef SAFE_ARRAYS
00141       if (i < indexmin() || i > indexmax())
00142       {
00143         ADMB_ARRAY_BOUNDS_ERROR("index out of bounds",
00144         "d5_array& d6_array::operator()(int i)", indexmin(), indexmax(), i);
00145       }
00146       #endif
00147       //return t[i];
00148       return elem(i);
00149     }
00150 
00155     d5_array& d6_array::operator [] (int i)
00156     {
00157       #ifdef SAFE_ARRAYS
00158       if (i < indexmin() || i > indexmax())
00159       {
00160         ADMB_ARRAY_BOUNDS_ERROR("index out of bounds",
00161         "d5_array& d6_array::operator[](int i)", indexmin(), indexmax(), i);
00162       }
00163       #endif
00164       return t[i];
00165     }
00166 
00171     d4_array& d6_array::operator ( ) (int i ,int j)
00172     {
00173       #ifdef SAFE_ARRAYS
00174       if (i < indexmin() || i > indexmax())
00175       {
00176         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00177         "d4_array& d6_array::operator()(int i, int j)",
00178         indexmin(), indexmax(), i);
00179       }
00180       #endif
00181       return elem(i)(j);
00182     }
00183 
00188     d3_array& d6_array::operator ( ) (int i,int j,int k)
00189     {
00190       #ifdef SAFE_ARRAYS
00191       if (i < indexmin() || i > indexmax())
00192       {
00193         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00194         "d3_array& d6_array::operator()(int i, int j, int k)",
00195         indexmin(), indexmax(), i);
00196       }
00197       #endif
00198       return elem(i)(j,k);
00199     }
00200 
00205     dmatrix& d6_array::operator ( ) (int i,int j,int k,int l)
00206     {
00207       #ifdef SAFE_ARRAYS
00208       if (i < indexmin() || i > indexmax())
00209       {
00210         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00211         "dmatrix& d6_array::operator()(int i, int j, int k, int l)",
00212         indexmin(), indexmax(), i);
00213       }
00214       #endif
00215       return elem(i)(j,k,l);
00216     }
00217 
00222     dvector& d6_array::operator ( ) (int i,int j,int k,int l,int m)
00223     {
00224       #ifdef SAFE_ARRAYS
00225       if (i < indexmin() || i > indexmax())
00226       {
00227         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00228         "dvector& d6_array::operator()(int i, int j, int k, int l, int m)",
00229         indexmin(), indexmax(), i);
00230       }
00231       #endif
00232       return elem(i)(j,k,l,m);
00233     }
00234 
00239     double& d6_array::operator ( ) (int i,int j,int k,int l,int m,int n)
00240     {
00241       #ifdef SAFE_ARRAYS
00242       if (i < indexmin() || i > indexmax())
00243       {
00244         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00245      "double& d6_array::operator()(int i, int j, int k, int l, int m, int n)",
00246         indexmin(), indexmax(), i);
00247       }
00248       #endif
00249       return elem(i)(j,k,l,m,n);
00250     }
00251 
00256 const d5_array& d6_array::operator()(int i) const
00257     {
00258       #ifdef SAFE_ARRAYS
00259         if (i<indexmin()||i>indexmax())
00260         { cerr << "Error  index out of bounds in\n"
00261             "d5_array& d6_array::operator ( )" << endl;
00262           ad_exit(1);
00263         }
00264       #endif
00265       //return t[i];
00266       return elem(i);
00267     }
00268 
00273 const d5_array& d6_array::operator[](int i) const
00274     {
00275       #ifdef SAFE_ARRAYS
00276         if (i<indexmin()||i>indexmax())
00277         { cerr << "Error  index out of bounds in\n"
00278             "d4_array& d6_array::operator []" << endl;
00279           ad_exit(1);
00280         }
00281       #endif
00282       return t[i];
00283     }
00284 
00289 const d4_array& d6_array::operator()(int i, int j) const
00290     {
00291       #ifdef SAFE_ARRAYS
00292         if (i<indexmin()||i>indexmax())
00293         { cerr << "Error hslice index out of bounds in\n"
00294             "dmatrix& d4_array::operator ( )" << endl;
00295           ad_exit(1);
00296         }
00297       #endif
00298       return elem(i)(j);
00299     }
00300 
00305 const d3_array& d6_array::operator()(int i, int j, int k) const
00306     {
00307       #ifdef SAFE_ARRAYS
00308         if (i<indexmin()||i>indexmax())
00309         { cerr << "Error hslice index out of bounds in\n"
00310           "dvector& d4_array::operator ( )" << endl;
00311           ad_exit(1);
00312         }
00313       #endif
00314       return elem(i)(j,k);
00315     }
00316 
00321 const dmatrix& d6_array::operator()(int i, int j, int k, int l) const
00322     {
00323       #ifdef SAFE_ARRAYS
00324         if (i<indexmin()||i>indexmax())
00325         { cerr << "Error hslice index out of bounds in\n"
00326             "double& d4_array::operator ( )"  << endl;
00327           ad_exit(1);
00328         }
00329       #endif
00330       return elem(i)(j,k,l);
00331     }
00332 
00337 const dvector& d6_array::operator()(int i, int j, int k, int l, int m) const
00338     {
00339       #ifdef SAFE_ARRAYS
00340         if (i<indexmin()||i>indexmax())
00341         { cerr << "Error hslice index out of bounds in\n"
00342             "double& d4_array::operator ( )"  << endl;
00343           ad_exit(1);
00344         }
00345       #endif
00346       return elem(i)(j,k,l,m);
00347     }
00348 
00353 const double& d6_array::operator()(int i, int j, int k, int l, int m, int n)
00354   const
00355     {
00356       #ifdef SAFE_ARRAYS
00357         if (i<indexmin()||i>indexmax())
00358         { cerr << "Error hslice index out of bounds in\n"
00359             "double& d4_array::operator ( )"  << endl;
00360           ad_exit(1);
00361         }
00362       #endif
00363       return elem(i)(j,k,l,m,n);
00364     }
00365 #endif
00366 
00371 d6_array::d6_array(int hsl,int hsu,int sl,int sh,int nrl,
00372    int nrh,int ncl,int nch,int l5,int u5,int l6,int u6)
00373 {
00374   allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch,l5,u5,l6,u6);
00375 }
00376 
00381 d6_array::d6_array(const ad_integer& hsl,const ad_integer& hsu,
00382   const index_type& sl,const index_type& sh,const index_type& nrl,
00383   const index_type& nrh,const index_type& ncl,const index_type& nch,
00384   const index_type& l5,const index_type& u5,
00385   const index_type& l6,const index_type& u6)
00386 {
00387   allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch,l5,u5,l6,u6);
00388 }
00389 
00394 void d6_array::allocate(int hsl,int hsu,int sl,int sh,int nrl,
00395    int nrh,int ncl,int nch,int l5,int u5,int l6,int u6)
00396  {
00397    if ( (shape=new vector_shape(hsl,hsu)) == 0)
00398    {
00399      cerr << " Error allocating memory in d5_array contructor\n";
00400      ad_exit(21);
00401    }
00402    int ss=size();
00403    if ( (t = new d5_array[ss]) == 0)
00404    {
00405      cerr << " Error allocating memory in d5_array contructor\n";
00406      ad_exit(21);
00407    }
00408    t -= indexmin();
00409    for (int i=hsl; i<=hsu; i++)
00410    {
00411      t[i].allocate(sl,sh,nrl,nrh,ncl,nch,l5,u5,l6,u6);
00412    }
00413  }
00414 
00419  void d6_array::allocate(const ad_integer& hsl,const ad_integer& hsu,
00420    const index_type& sl,const index_type& sh,const index_type& nrl,
00421    const index_type& nrh,const index_type& ncl,const index_type& nch,
00422    const index_type& l5,const index_type& u5,
00423    const index_type& l6,const index_type& u6)
00424  {
00425    if ( (shape=new vector_shape (hsl,hsu)) == 0)
00426    {
00427      cerr << " Error allocating memory in d5_array contructor\n";
00428    }
00429 
00430    int ss=size();
00431    if ( (t = new d5_array[ss]) == 0)
00432    {
00433      cerr << " Error allocating memory in d5_array contructor\n";
00434      ad_exit(21);
00435    }
00436    t -= indexmin();
00437    int i1=hsl;
00438    int i2=hsu;
00439    for (int i=i1; i<=i2; i++)
00440    {
00441      (*this)(i).allocate(ad_integer(sl(i)),ad_integer(sh(i)),nrl(i),nrh(i),
00442        ncl(i),nch(i), l5(i),u5(i),l6(i),u6(i));
00443    }
00444  }
00445 
00450 double sum(const d6_array& m)
00451 {
00452   double tmp=0.;
00453   for (int i=m.indexmin();i<=m.indexmax();i++)
00454   {
00455     tmp+=sum(m.elem(i));
00456   }
00457   return tmp;
00458 }