void param_init_bounded_number_vector::allocate(int min1,int max1, const double_index_type & bmin,const double_index_type & bmax, const index_type& phase_start,const char * s) { int size = max1 - min1 + 1; if (size > 0) { v = new param_init_bounded_number[size]; if (!v) { cerr << " error trying to allocate memory in " "param_init_bounded_number_vector " << endl; exit(1); } index_min=min1; index_max=max1; v-=indexmin(); for (int i=indexmin();i<=indexmax();i++) { if (it) v[i].set_initial_value(ad_double((*it)[i])); adstring ss=s + adstring("[") + str(i) + adstring("]"); v[i].allocate(ad_double(bmin[i]),ad_double(bmax[i]), ad_integer(phase_start[i]),(char*)(ss) ); } } }
/** * Description not yet available. * \param */ ivector& ivector::operator=(const ivector& t) { // disconnect ivector pointer from old array if (::allocated(*this)) { if (v != t.v) { if (indexmin() != t.indexmin() || indexmax() != t.indexmax()) { cerr << " Array sizes do not match in ivector operator" " =(const ivector&)" << endl; ad_exit(1); } for ( int i=indexmin(); i<=indexmax(); i++) { elem(i) = t.elem(i); } } } else { shallow_copy(t); } return (*this); }
/** * Description not yet available. * \param */ void lvector::fill_multinomial(const int& seed, const dvector& p) // Fils a dvector with random numbers drawn from a multinomial distribution { double sum=mean(p)*p.size(); int pmin=p.indexmin(); int pmax=p.indexmax(); dvector tmp(pmin,pmax); dvector tmp1(pmin,pmax); dvector choose(indexmin(),indexmax()); choose.fill_randu(seed); tmp=p/sum; tmp1(pmin)=tmp(pmin); for (int j=pmin+1;j<=pmax-1;j++) { tmp1(j)=tmp1(j-1)+tmp(j); } tmp1(pmax)=1.0; for (int i=indexmin();i<=indexmax();i++) { int j=pmin; while (choose(i)>tmp1(j)) { j++; } (*this)(i)=j; } }
/** * Description not yet available. * \param */ void dvar_matrix::initialize(void) { if (!(!(*this))) // only initialize allocated objects { if (indexmin()>indexmax()) cout << "error" << endl; int imin=indexmin(); int imax=indexmax(); for (int i=imin;i<=imax;i++) { if (allocated(elem(i))) { dvar_vector& tmp=elem(i); int jmin=tmp.indexmin(); int jmax=tmp.indexmax(); double * pd=&(tmp.elem_value(jmin)); for (int j=jmin;j<=jmax;j++) { *pd++=0.0; } } } save_identifier_string("p"); save_dvar_matrix_position(); gradient_structure::GRAD_STACK1-> set_gradient_stack(dfmatinit); save_identifier_string("q"); } }
/** Allocate vector of integers with dimension [_ncl to _nch]. \param _ncl lower vector index \param _nch upper vector index */ void ivector::allocate(const ad_integer& _ncl, const index_type& _nch) { index_min = _ncl; index_max = ad_integer(_nch); unsigned int ss = static_cast<unsigned int>( index_max < index_min ? 0 : index_max - index_min + 1); if (ss > 0) { if ((v = new int[ss]) == 0) { cerr << " Error: ivector unable to allocate memory in " << __FILE__ << ':' << __LINE__ << '\n'; ad_exit(1); } if ((shape = new vector_shapex(index_min, index_max, v)) == NULL) { cerr << " Error: ivector unable to allocate memory in " << __FILE__ << ':' << __LINE__ << '\n'; ad_exit(1); } v -= indexmin(); #ifdef SAFE_INITIALIZE for (int i = indexmin(); i <= indexmax(); ++i) { v[i] = 0; } #endif } else { allocate(); } }
/** * Description not yet available. * \param */ i3_array& i4_array::operator [] (int i) { if (i < indexmin() || i > indexmax()) { ADMB_ARRAY_BOUNDS_ERROR("Index out of bounds", "i3_array& i4_array::operator [] (int i)", indexmin(), indexmax(), i); } return t[i]; }
/** * Description not yet available. * \param */ d3_array& d5_array::operator ( ) (int i, int j) { if (i < indexmin() || i > indexmax()) { ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds", "d3_array& d5_array::operator[](int i, int j)", indexmin(), indexmax(), i); } return elem(i)(j); }
/** * Description not yet available. * \param */ d4_array& d5_array::operator ( ) (int i) { if (i < indexmin() || i > indexmax()) { ADMB_ARRAY_BOUNDS_ERROR("index out of bounds", "d4_array& d5_array::operator()(int i)", indexmin(), indexmax(), i); } //return t[i]; return elem(i); }
/** * Description not yet available. * \param */ dvar5_array& dvar6_array::operator ( ) (int i) { if (i < indexmin() || i > indexmax()) { ADMB_ARRAY_BOUNDS_ERROR("index out of bounds", "dvar5_array& dvar6_array::operator()(int i)", indexmin(), indexmax(), i); } return t[i]; }
/** * Description not yet available. * \param */ dvar_vector& dvar6_array::operator ( ) (int i,int j,int k,int l,int m) { if (i < indexmin() || i > indexmax()) { ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds", "dvar_vector& dvar6_array::operator()(int i, int j, int k, int l, int m)", indexmin(), indexmax(), i); } return elem(i)(j,k,l,m); }
/** * Description not yet available. * \param */ dvar3_array& dvar6_array::operator ( ) (int i,int j,int k) { if (i < indexmin() || i > indexmax()) { ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds", "dvar5_array& dvar6_array::operator()(int i, int j, int k)", indexmin(), indexmax(), i); } return elem(i)(j,k); }
/** * Description not yet available. * \param */ prevariable dvar6_array::operator ( ) (int i,int j,int k,int l,int m, int n) { if (i < indexmin() || i > indexmax()) { ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds", "prevariable dvar6_array::operator()(int i, int j, int k, int l, int m, int n)", indexmin(), indexmax(), i); } return elem(i)(j,k,l,m,n); }
/** Return a matrix from d3_array indexed at i. \param i int */ dmatrix& d3_array::operator()(int i) { if (i < slicemin()) { ADMB_ARRAY_BOUNDS_ERROR("matrix bound exceeded -- row index too low", "dmatrix& d3_array::operator()(int i)", indexmin(), indexmax(), i); } if (i > slicemax()) { ADMB_ARRAY_BOUNDS_ERROR("matrix bound exceeded -- row index too high", "dmatrix& d3_array::operator()(int i)", indexmin(), indexmax(), i); } return t[i]; }
void param_init_bounded_matrix::allocate(int rmin,int rmax, int cmin,int cmax, double _minb,double _maxb,int _phase_start,const char * s) { minb=_minb; maxb=_maxb; dvar_matrix::allocate(rmin,rmax,cmin,cmax); model_name_tag::allocate(s); if (allocated(*this)) { initial_params::allocate(_phase_start); for (int i=indexmin();i<=indexmax();i++) { if (allocated((*this)(i))) { if (ad_comm::global_bparfile) { *(ad_comm::global_bparfile) >> (*this)(i); } else if (ad_comm::global_parfile) { *(ad_comm::global_parfile) >> (*this)(i); } else { if ((!initial_value_flag) || initial_value <=minb || initial_value >= maxb) { //cerr << "Initial value out of bounds -- using halfway value\n."; initial_value=(minb+maxb)/2.; } (*this)(i)=(initial_value); } }
/** * Description not yet available. * \param */ void dvector::fill_randcau(const random_number_generator& rng) { for (int i=indexmin(); i<=indexmax(); i++) { (*this)(i)=randcau(rng); } }
/** Allocate variable array with dimensions [hsl to hsu] x [sl to sh] x [nrl to nrh] x [ncl to nch] x [l5 to u5]. \param hsl lower dvar5_array index \param hsu upper dvar5_array index \param sl lower dvar4_array index \param su upper dvar4_array index \param nrl lower dvar3_array row index \param nrh upper dvar3_array row index \param ncl lower matrix row index \param nch upper matrix row index \param l5 lower matrix column index \param u5 upper matrix column index */ void dvar5_array::allocate( const ad_integer& hsl, const ad_integer& hsu, const index_type& sl, const index_type& sh, const index_type& nrl, const index_type& nrh, const index_type& ncl, const index_type& nch, const index_type& l5, const index_type& u5) { if ((shape = new vector_shape(hsl, hsu)) == 0) { cerr << " Error: dvar5_array unable to allocate memory in " << __FILE__ << ':' << __LINE__ << '\n'; ad_exit(1); } if ((t = new dvar4_array[size()]) == 0) { cerr << " Error: dvar5_array unable to allocate memory in " << __FILE__ << ':' << __LINE__ << '\n'; ad_exit(1); } t -= indexmin(); int il=hsl; int iu=hsu; for (int i = il; i <= iu; ++i) { t[i].allocate( ad_integer(sl(i)), ad_integer(sh(i)), nrl(i), nrh(i), ncl(i), nch(i), l5(i),u5(i)); } }
/// Deallocate dvar_vector memory. void dvar_vector::deallocate() { if (shape) { #ifdef DIAG cout << " Deallocating dvar_vector with ptr_address\n " << &ptr << " pointing at " << (ptr+indexmin()) << "\n"; #endif if (shape->ncopies > 0) { --(shape->ncopies); } else { va = (double_and_int*) shape->trueptr; *(arr_link**) va = link_ptr; arr_free(va); delete shape; } allocate(); } #ifdef DEBUG else { cerr << "Warning -- Unable to deallocate an unallocated dvar_vector.\n"; } #endif }
/** * Description not yet available. * \param */ void i4_array::allocate(int hsl,int hsu) { int ss=hsu-hsl+1; if (ss>0) { if ( (t = new i3_array[ss]) == 0) { cerr << " Error allocating memory in i4_array contructor\n"; ad_exit(21); } if ( (shape=new vector_shapex(hsl,hsu,t)) == 0) { cerr << " Error allocating memory in i4_array contructor\n"; ad_exit(21); } t -= indexmin(); for (int i=hsl; i<=hsu; i++) { (*this)(i).allocate(); } } else { t=0; shape=0; } }
/** * Description not yet available. * \param */ void d4_array::operator /= (double d) { for (int i=indexmin();i<=indexmax();i++) { (*this)(i)/=d; } }
/** * Description not yet available. * \param */ void i4_array::allocate(const ad_integer& hsl,const ad_integer& hsu, const index_type& sl,const index_type& sh,const index_type& nrl, const index_type& nrh,const index_type& ncl,const index_type& nch) { int ss=hsu-hsl+1; if (ss>0) { if ( (t = new i3_array[ss]) == 0) { cerr << " Error allocating memory in i4_array contructor\n"; ad_exit(21); } if ( (shape=new vector_shapex(hsl,hsu,t)) == 0) { cerr << " Error allocating memory in i4_array contructor\n"; ad_exit(21); } t -= indexmin(); for (int i=hsl; i<=hsu; i++) { (*this)(i).allocate(ad_integer(sl(i)),ad_integer(sh(i)),nrl(i),nrh(i), ncl(i),nch(i)); } } else { t=0; shape=0; } }
/** Allocate vector of integers with dimension [ncl to nch]. \param ncl lower vector index \param nch upper vector index */ void ivector::allocate(int ncl,int nch) { if (ncl > nch) { allocate(); } else { if ((v = new int[static_cast<unsigned int>(nch - ncl + 1)]) == 0 ) { cerr << " Error: ivector unable to allocate memory in " << __FILE__ << ':' << __LINE__ << '\n'; ad_exit(1); } if ((shape = new vector_shapex(ncl, nch, v)) == NULL) { cerr << " Error: ivector unable to allocate memory in " << __FILE__ << ':' << __LINE__ << '\n'; ad_exit(1); } index_min = ncl; index_max = nch; v -= indexmin(); #ifdef SAFE_INITIALIZE for (int i = ncl; i <= nch; ++i) { v[i] = 0; } #endif } }
/** Allocate vector of empty i3_array with dimension [hsl to hsu]. \param hsl lower vector index \param hsu upper vector index */ void i4_array::allocate(int hsl, int hsu) { unsigned int ss = static_cast<unsigned int>(hsu < hsl ? 0 : hsu - hsl + 1); if (ss > 0) { if ((t = new i3_array[ss]) == 0) { cerr << " Error: i4_array unable to allocate memory in " << __FILE__ << ':' << __LINE__ << '\n'; ad_exit(1); } if ((shape=new vector_shapex(hsl, hsu, t)) == 0) { cerr << " Error: i4_array unable to allocate memory in " << __FILE__ << ':' << __LINE__ << '\n'; ad_exit(1); } t -= indexmin(); for (int i = hsl; i <= hsu; ++i) { (*this)(i).allocate(); } } else { t = 0; shape = 0; } }
/** Allocate dvar5_array with dimensions [hsl to hsu] x [sl to sh] x [nrl to nrh] x [ncl to nch] x [l5 to u5]. */ void dvar5_array::allocate( int hsl, int hsu, int sl, int sh, int nrl, int nrh, int ncl, int nch, int l5, int u5) { if ((shape = new vector_shape(hsl, hsu)) == 0) { cerr << " Error: dvar5_array unable to allocate memory in " << __FILE__ << ':' << __LINE__ << '\n'; ad_exit(1); } if ((t = new dvar4_array[size()]) == 0) { cerr << " Error: dvar5_array unable to allocate memory in " << __FILE__ << ':' << __LINE__ << '\n'; ad_exit(1); } t -= indexmin(); for (int i = hsl; i <= hsu; ++i) { (*this)(i).allocate(sl, sh, nrl, nrh, ncl, nch, l5, u5); } }
/** * Description not yet available. * \param */ dvar_vector::dvar_vector(const predvar_vector& pdv) { #ifndef OPT_LIB if (pdv.ub<pdv.lb) { cerr << "lower index greater than upper index in dvar_vector::" " dvar-vector(const predvar_vector&) " << endl; ad_exit (1); } if ((pdv.lb<pdv.p->indexmin()) || (pdv.lb>pdv.p->indexmax())) { cerr << "lower index out of bounds in dvar_vector::" " operator(int lb,int ub) " << endl; ad_exit (1); } if ((pdv.ub<pdv.p->indexmin()) || (pdv.ub>pdv.p->indexmax())) { cerr << " upper index out of bounds in dvar_vector::" " operator(int lb,int ub) " << endl; ad_exit (1); } #endif index_min=pdv.lb; index_max=pdv.ub; shape=pdv.p->shape; link_ptr=pdv.p->link_ptr; (shape->ncopies)++; va = pdv.p->va; #ifdef DIAG cout << " Making copy for dvar_vector with ptr_address\n " << &va << " pointing at " << (va+indexmin()) << "\n"; #endif }
/** * Description not yet available. * \param */ void dvar6_array::allocate(const ad_integer& hsl,const ad_integer& hsu, const index_type& sl,const index_type& sh,const index_type& nrl, const index_type& nrh,const index_type& ncl,const index_type& nch, const index_type& l5,const index_type& u5, const index_type& l6,const index_type& u6) { if ( (shape=new vector_shape (hsl,hsu)) == 0) { cerr << " Error allocating memory in dvar5_array contructor\n"; } int ss=size(); if ( (t = new dvar5_array[ss]) == 0) { cerr << " Error allocating memory in dvar5_array contructor\n"; ad_exit(21); } t -= indexmin(); int il=hsl; int iu=hsu; for (int i=il; i<=iu; i++) { t[i].allocate(ad_integer(sl(i)),ad_integer(sh(i)),nrl(i),nrh(i), ncl(i),nch(i),l5(i),u5(i),l6(i),u6(i)); } }
/** * Description not yet available. * \param */ dvector::dvector(const ivector& u) { allocate(u.indexmin(),u.indexmax()); for ( int i=indexmin(); i<=indexmax(); i++) { elem(i)=u.elem(i); } }
/** * Description not yet available. * \param */ ivector& ivector::shift(int min) { v += indexmin()-min; index_max=index_max+min-index_min; index_min=min; shape->shift(min); return *this; }
/** * Description not yet available. * \param */ dvar_vector& dvar_vector::shift(int min) { va += indexmin()-min; index_max=index_max-index_min+min; index_min=min; shape->shift(min); return *this; }
/** * Multiply elements of a vector by a constant. * \param x constant for multiplication. */ dvector& dvector::operator*=(const double x) { for (int i=indexmin(); i<=indexmax(); i++) { elem(i)*=x; } return(*this); }
/** Reallocate size of array. \param percent change */ void dvector::reallocate(double s) { if (::allocated(*this)) { int oldmin=indexmin(); int oldmax=indexmax(); dvector tmp(indexmin(),indexmax()); tmp=(*this); deallocate(); allocate(indexmin(),int(s*indexmax())); #ifndef OPT_LIB initialize(); #endif int max = oldmax < indexmax() ? oldmax : indexmax(); (*this)(oldmin, max) = tmp(oldmin, max); } }