ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
d6arr.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  void d6_array::initialize(void)
00019  {
00020    int mmin=indexmin();
00021    int mmax=indexmax();
00022    for (int i=mmin; i<=mmax; i++)
00023    {
00024      (*this)(i).initialize();
00025    }
00026  }
00027 
00032  d6_array::d6_array(const d6_array& _m2)
00033  {
00034    d6_array& m2=(d6_array&) _m2;
00035    if (m2.shape)
00036    {
00037      shape=m2.shape;
00038      (shape->ncopies)++;
00039      t = m2.t;
00040    }
00041    else
00042    {
00043      shape=NULL;
00044      t=NULL;
00045    }
00046  }
00047 
00052  void d6_array::deallocate()
00053  {
00054    if (shape)
00055    {
00056      if (shape->ncopies)
00057      {
00058        (shape->ncopies)--;
00059      }
00060      else
00061      {
00062        t += indexmin();
00063        delete [] t;
00064        t=NULL;
00065        delete shape;
00066        shape=NULL;
00067      }
00068    }
00069 #if defined(SAFE_ALL)
00070    else
00071    {
00072      cerr << "Warning -- trying to deallocate an unallocated d4_array"<<endl;
00073    }
00074 #endif
00075  }
00076 
00080 d6_array::~d6_array()
00081 {
00082   deallocate();
00083 }
00084 
00089 d6_array& d6_array::operator=(const d6_array& m)
00090  {
00091    int mmin=indexmin();
00092    int mmax=indexmax();
00093    if (mmin!=m.indexmin() || mmax!=m.indexmax())
00094    {
00095      cerr << "Incompatible bounds in"
00096       " d6_array& d6_array:: operator =  (const d6_array& m)"
00097       << endl;
00098      ad_exit(1);
00099     }
00100    for (int i=mmin; i<=mmax; i++)
00101    {
00102      (*this)(i)=m(i);
00103    }
00104    return *this;
00105  }
00106 
00111 void d6_array::allocate(const d6_array& m1)
00112  {
00113    if ( (shape=new vector_shape(m1.indexmin(),m1.indexmax()))
00114        == 0)
00115    {
00116      cerr << " Error allocating memory in d5_array contructor" << endl;
00117    }
00118    int ss=size();
00119    if ( (t = new d5_array[ss]) == 0)
00120    {
00121      cerr << " Error allocating memory in d5_array contructor" << endl;
00122      ad_exit(21);
00123    }
00124    t -= indexmin();
00125    for (int i=indexmin(); i<=indexmax(); i++)
00126    {
00127      t[i].allocate(m1[i]);
00128    }
00129  }
00130 
00131   #ifndef OPT_LIB
00132 
00137     d5_array& d6_array::operator ( ) (int i)
00138     {
00139       if (i < indexmin() || i > indexmax())
00140       {
00141         ADMB_ARRAY_BOUNDS_ERROR("index out of bounds",
00142         "d5_array& d6_array::operator()(int i)", indexmin(), indexmax(), i);
00143       }
00144       //return t[i];
00145       return elem(i);
00146     }
00147 
00152     d5_array& d6_array::operator [] (int i)
00153     {
00154       if (i < indexmin() || i > indexmax())
00155       {
00156         ADMB_ARRAY_BOUNDS_ERROR("index out of bounds",
00157         "d5_array& d6_array::operator[](int i)", indexmin(), indexmax(), i);
00158       }
00159       return t[i];
00160     }
00161 
00166     d4_array& d6_array::operator ( ) (int i ,int j)
00167     {
00168       if (i < indexmin() || i > indexmax())
00169       {
00170         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00171         "d4_array& d6_array::operator()(int i, int j)",
00172         indexmin(), indexmax(), i);
00173       }
00174       return elem(i)(j);
00175     }
00176 
00181     d3_array& d6_array::operator ( ) (int i,int j,int k)
00182     {
00183       if (i < indexmin() || i > indexmax())
00184       {
00185         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00186         "d3_array& d6_array::operator()(int i, int j, int k)",
00187         indexmin(), indexmax(), i);
00188       }
00189       return elem(i)(j,k);
00190     }
00191 
00196     dmatrix& d6_array::operator ( ) (int i,int j,int k,int l)
00197     {
00198       if (i < indexmin() || i > indexmax())
00199       {
00200         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00201         "dmatrix& d6_array::operator()(int i, int j, int k, int l)",
00202         indexmin(), indexmax(), i);
00203       }
00204       return elem(i)(j,k,l);
00205     }
00206 
00211     dvector& d6_array::operator ( ) (int i,int j,int k,int l,int m)
00212     {
00213       if (i < indexmin() || i > indexmax())
00214       {
00215         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00216         "dvector& d6_array::operator()(int i, int j, int k, int l, int m)",
00217         indexmin(), indexmax(), i);
00218       }
00219       return elem(i)(j,k,l,m);
00220     }
00221 
00226     double& d6_array::operator ( ) (int i,int j,int k,int l,int m,int n)
00227     {
00228       if (i < indexmin() || i > indexmax())
00229       {
00230         ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00231      "double& d6_array::operator()(int i, int j, int k, int l, int m, int n)",
00232         indexmin(), indexmax(), i);
00233       }
00234       return elem(i)(j,k,l,m,n);
00235     }
00236 
00241 const d5_array& d6_array::operator()(int i) const
00242     {
00243         if (i<indexmin()||i>indexmax())
00244         { cerr << "Error  index out of bounds in\n"
00245             "d5_array& d6_array::operator ( )" << endl;
00246           ad_exit(1);
00247         }
00248       //return t[i];
00249       return elem(i);
00250     }
00251 
00256 const d5_array& d6_array::operator[](int i) const
00257     {
00258         if (i<indexmin()||i>indexmax())
00259         { cerr << "Error  index out of bounds in\n"
00260             "d4_array& d6_array::operator []" << endl;
00261           ad_exit(1);
00262         }
00263       return t[i];
00264     }
00265 
00270 const d4_array& d6_array::operator()(int i, int j) const
00271     {
00272         if (i<indexmin()||i>indexmax())
00273         { cerr << "Error hslice index out of bounds in\n"
00274             "dmatrix& d4_array::operator ( )" << endl;
00275           ad_exit(1);
00276         }
00277       return elem(i)(j);
00278     }
00279 
00284 const d3_array& d6_array::operator()(int i, int j, int k) const
00285     {
00286         if (i<indexmin()||i>indexmax())
00287         { cerr << "Error hslice index out of bounds in\n"
00288           "dvector& d4_array::operator ( )" << endl;
00289           ad_exit(1);
00290         }
00291       return elem(i)(j,k);
00292     }
00293 
00298 const dmatrix& d6_array::operator()(int i, int j, int k, int l) const
00299     {
00300         if (i<indexmin()||i>indexmax())
00301         { cerr << "Error hslice index out of bounds in\n"
00302             "double& d4_array::operator ( )"  << endl;
00303           ad_exit(1);
00304         }
00305       return elem(i)(j,k,l);
00306     }
00307 
00312 const dvector& d6_array::operator()(int i, int j, int k, int l, int m) const
00313     {
00314         if (i<indexmin()||i>indexmax())
00315         { cerr << "Error hslice index out of bounds in\n"
00316             "double& d4_array::operator ( )"  << endl;
00317           ad_exit(1);
00318         }
00319       return elem(i)(j,k,l,m);
00320     }
00321 
00326 const double& d6_array::operator()(int i, int j, int k, int l, int m, int n)
00327   const
00328     {
00329         if (i<indexmin()||i>indexmax())
00330         { cerr << "Error hslice index out of bounds in\n"
00331             "double& d4_array::operator ( )"  << endl;
00332           ad_exit(1);
00333         }
00334       return elem(i)(j,k,l,m,n);
00335     }
00336 #endif
00337 
00342 d6_array::d6_array(int hsl,int hsu,int sl,int sh,int nrl,
00343    int nrh,int ncl,int nch,int l5,int u5,int l6,int u6)
00344 {
00345   allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch,l5,u5,l6,u6);
00346 }
00347 
00352 d6_array::d6_array(const ad_integer& hsl,const ad_integer& hsu,
00353   const index_type& sl,const index_type& sh,const index_type& nrl,
00354   const index_type& nrh,const index_type& ncl,const index_type& nch,
00355   const index_type& l5,const index_type& u5,
00356   const index_type& l6,const index_type& u6)
00357 {
00358   allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch,l5,u5,l6,u6);
00359 }
00360 
00365 void d6_array::allocate(int hsl,int hsu,int sl,int sh,int nrl,
00366    int nrh,int ncl,int nch,int l5,int u5,int l6,int u6)
00367  {
00368    if ( (shape=new vector_shape(hsl,hsu)) == 0)
00369    {
00370      cerr << " Error allocating memory in d5_array contructor\n";
00371      ad_exit(21);
00372    }
00373    int ss=size();
00374    if ( (t = new d5_array[ss]) == 0)
00375    {
00376      cerr << " Error allocating memory in d5_array contructor\n";
00377      ad_exit(21);
00378    }
00379    t -= indexmin();
00380    for (int i=hsl; i<=hsu; i++)
00381    {
00382      t[i].allocate(sl,sh,nrl,nrh,ncl,nch,l5,u5,l6,u6);
00383    }
00384  }
00385 
00390  void d6_array::allocate(const ad_integer& hsl,const ad_integer& hsu,
00391    const index_type& sl,const index_type& sh,const index_type& nrl,
00392    const index_type& nrh,const index_type& ncl,const index_type& nch,
00393    const index_type& l5,const index_type& u5,
00394    const index_type& l6,const index_type& u6)
00395  {
00396    if ( (shape=new vector_shape (hsl,hsu)) == 0)
00397    {
00398      cerr << " Error allocating memory in d5_array contructor\n";
00399    }
00400 
00401    int ss=size();
00402    if ( (t = new d5_array[ss]) == 0)
00403    {
00404      cerr << " Error allocating memory in d5_array contructor\n";
00405      ad_exit(21);
00406    }
00407    t -= indexmin();
00408    int i1=hsl;
00409    int i2=hsu;
00410    for (int i=i1; i<=i2; i++)
00411    {
00412      (*this)(i).allocate(ad_integer(sl(i)),ad_integer(sh(i)),nrl(i),nrh(i),
00413        ncl(i),nch(i), l5(i),u5(i),l6(i),u6(i));
00414    }
00415  }
00416 
00421 double sum(const d6_array& m)
00422 {
00423   double tmp=0.;
00424   for (int i=m.indexmin();i<=m.indexmax();i++)
00425   {
00426     tmp+=sum(m.elem(i));
00427   }
00428   return tmp;
00429 }