00001
00002
00003
00004
00005
00006
00011 #include "fvar.hpp"
00012 #include "admb_messages.h"
00013
00018 d4_array::d4_array(int nrl,int nrh)
00019 {
00020 allocate(nrl,nrh);
00021 }
00022
00027 four_array_shape::four_array_shape(int hsl,int hsu)
00028
00029 {
00030 hslice_min=hsl;
00031 hslice_max=hsu;
00032
00033
00034
00035
00036
00037
00038 ncopies=0;
00039 }
00040
00045 double sum(const d4_array& m)
00046 {
00047 double tmp=0.;
00048 for (int i=m.indexmin();i<=m.indexmax();i++)
00049 {
00050 tmp+=sum(m.elem(i));
00051 }
00052 return tmp;
00053 }
00054
00059 d4_array d4_array::sub(int nrl,int nrh)
00060 {
00061 if (allocated(*this))
00062 {
00063 d4_array tmp(nrl,nrh);
00064 for (int i=nrl; i<=nrh; i++)
00065 {
00066 tmp[i].shallow_copy((*this)(i));
00067 }
00068 return tmp;
00069 }
00070 else
00071 {
00072 return *this;
00073 }
00074 }
00075
00080 d4_array::d4_array(const d4_array& m2)
00081 {
00082 if (m2.shape)
00083 {
00084 shape=m2.shape;
00085 (shape->ncopies)++;
00086 t = m2.t;
00087 }
00088 else
00089 {
00090 shape=NULL;
00091 t=NULL;
00092 }
00093 }
00094
00099 void d4_array::shallow_copy(const d4_array& m2)
00100 {
00101 if (m2.shape)
00102 {
00103 shape=m2.shape;
00104 (shape->ncopies)++;
00105 t = m2.t;
00106 }
00107 else
00108 {
00109 shape=NULL;
00110 t=NULL;
00111 }
00112 }
00113
00118 void d4_array::deallocate()
00119 {
00120 if (shape)
00121 {
00122 if (shape->ncopies)
00123 {
00124 (shape->ncopies)--;
00125 }
00126 else
00127 {
00128 t += hslicemin();
00129 delete [] t;
00130 delete shape;
00131 }
00132 }
00133 #if defined(SAFE_ALL)
00134 else
00135 {
00136 cerr << "Warning -- trying to deallocate an unallocated d4_array"<<endl;
00137 }
00138 #endif
00139 }
00140
00144 d4_array::~d4_array()
00145 {
00146 deallocate();
00147 }
00148
00153 d4_array& d4_array::operator=(const d4_array& m)
00154 {
00155 int mmin=hslicemin();
00156 int mmax=hslicemax();
00157 if (mmin!=m.hslicemin() || mmax!=m.hslicemax())
00158 {
00159 cerr << "Incompatible bounds in"
00160 " d4_array& d4_array:: operator = (const d4_array& m)"
00161 << endl;
00162 ad_exit(1);
00163 }
00164 for (int i=mmin; i<=mmax; i++)
00165 {
00166 (*this)(i)=m(i);
00167 }
00168 return *this;
00169 }
00170
00175 void d4_array::allocate(const d4_array& m1)
00176 {
00177 if ( (shape=new four_array_shape(m1.hslicemin(),m1.hslicemax()))
00178 == 0)
00179 {
00180 cerr << " Error allocating memory in dvar4_array contructor" << endl;
00181 }
00182 int ss=hslicesize();
00183 if ( (t = new d3_array[ss]) == 0)
00184 {
00185 cerr << " Error allocating memory in dvar4_array contructor" << endl;
00186 ad_exit(21);
00187 }
00188 t -= hslicemin();
00189 for (int i=hslicemin(); i<=hslicemax(); i++)
00190 {
00191 t[i].allocate(m1[i]);
00192 }
00193 }
00194
00195 #ifndef OPT_LIB
00196
00201 d3_array& d4_array::operator () (int i)
00202 {
00203 if (i < hslicemin() || i > hslicemax())
00204 {
00205 ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00206 "d3_array& d4_array::operator() (int i)", hslicemin(), hslicemax(), i);
00207 }
00208 return t[i];
00209 }
00210
00215 d3_array& d4_array::operator [] (int i)
00216 {
00217 if (i < hslicemin() || i > hslicemax())
00218 {
00219 ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00220 "d3_array& d4_array::operator[] (int i)",
00221 hslicemin(), hslicemax(), i);
00222 }
00223 return t[i];
00224 }
00225
00230 dmatrix& d4_array::operator ( ) (int i ,int j)
00231 {
00232 if (i < hslicemin() || i > hslicemax())
00233 {
00234 ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00235 "dmatrix& d4_array::operator() (int i, int j)",
00236 hslicemin(), hslicemax(), i);
00237 }
00238 return ((*this)(i))(j);
00239 }
00240
00245 dvector& d4_array::operator ( ) (int i,int j,int k)
00246 {
00247 if (i < hslicemin() || i > hslicemax())
00248 {
00249 ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00250 "dvector& d4_array::operator() (int i, int j, int k)",
00251 hslicemin(), hslicemax(), i);
00252 }
00253 return (((*this)(i,j))(k));
00254 }
00255
00260 double& d4_array::operator ( ) (int i,int j,int k,int l)
00261 {
00262 if (i < hslicemin() || i > hslicemax())
00263 {
00264 ADMB_ARRAY_BOUNDS_ERROR("hslice index out of bounds",
00265 "double& d4_array::operator() (int i, int j, int k, int l)",
00266 hslicemin(), hslicemax(), i);
00267 }
00268 return ( ((*this)(i,j,k))(l));
00269 }
00270
00275 const d3_array& d4_array::operator()(int i) const
00276 {
00277 if (i<hslicemin()||i>hslicemax())
00278 { cerr << "Error hslice index out of bounds in\n"
00279 "d3_array& d4_array::operator ( )" << endl;
00280 ad_exit(1);
00281 }
00282 return t[i];
00283 }
00284
00289 const d3_array& d4_array::operator[](int i) const
00290 {
00291 if (i<hslicemin()||i>hslicemax())
00292 { cerr << "Error hslice index out of bounds in\n"
00293 "d3_array& d4_array::operator ( )" << endl;
00294 ad_exit(1);
00295 }
00296 return t[i];
00297 }
00298
00303 const dmatrix& d4_array::operator()(int i, int j) const
00304 {
00305 if (i<hslicemin()||i>hslicemax())
00306 { cerr << "Error hslice index out of bounds in\n"
00307 "dmatrix& d4_array::operator ( )" << endl;
00308 ad_exit(1);
00309 }
00310 return ((*this)(i))(j);
00311 }
00312
00317 const dvector& d4_array::operator()(int i, int j, int k) const
00318 {
00319 if (i<hslicemin()||i>hslicemax())
00320 { cerr << "Error hslice index out of bounds in\n"
00321 "dvector& d4_array::operator ( )" << endl;
00322 ad_exit(1);
00323 }
00324 return (((*this)(i,j))(k));
00325 }
00326
00331 const double& d4_array::operator()(int i, int j, int k, int l) const
00332 {
00333 if (i<hslicemin()||i>hslicemax())
00334 { cerr << "Error hslice index out of bounds in\n"
00335 "double& d4_array::operator ( )" << endl;
00336 ad_exit(1);
00337 }
00338 return ( ((*this)(i,j,k))(l));
00339 }
00340
00341 #endif
00342
00347 void d4_array::allocate(int hsl,int hsu,int sl,int sh,int nrl,
00348 int nrh,int ncl,int nch)
00349 {
00350 if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00351 {
00352 cerr << " Error allocating memory in d3_array contructor\n";
00353 ad_exit(21);
00354 }
00355 int ss=hslicesize();
00356 if ( (t = new d3_array[ss]) == 0)
00357 {
00358 cerr << " Error allocating memory in d3_array contructor\n";
00359 ad_exit(21);
00360 }
00361 t -= hslicemin();
00362 for (int i=hsl; i<=hsu; i++)
00363 {
00364 (*this)(i).allocate(sl,sh,nrl,nrh,ncl,nch);
00365 }
00366 }
00367
00372 void d4_array::allocate(int hsl,int hsu,int sl,int sh,int nrl,
00373 int nrh, const ivector& ncl, const ivector& nch)
00374 {
00375 if ( (shape=new four_array_shape (hsl,hsu)) == 0)
00376 {
00377 cerr << " Error allocating memory in d4_array contructor\n";
00378 }
00379
00380 int ss=hslicesize();
00381 if ( (t = new d3_array[ss]) == 0)
00382 {
00383 cerr << " Error allocating memory in d3_array contructor\n";
00384 ad_exit(21);
00385 }
00386 t -= hslicemin();
00387 for (int i=hsl; i<=hsu; i++)
00388 {
00389 (*this)(i).allocate(sl,sh,nrl,nrh,ncl,nch);
00390 }
00391 }
00392
00397 void d4_array::allocate(int hsl, int hsu, int sl, int sh, const ivector& nrl,
00398 const ivector& nrh, const ivector& ncl, const ivector& nch)
00399 {
00400 if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00401 {
00402 cerr << " Error allocating memory in d4_array contructor\n";
00403 }
00404
00405 int ss=hslicesize();
00406 if ( (t = new d3_array[ss]) == 0)
00407 {
00408 cerr << " Error allocating memory in d3_array contructor\n";
00409 ad_exit(21);
00410 }
00411 t -= hslicemin();
00412 for (int i=hsl; i<=hsu; i++)
00413 {
00414 (*this)(i).allocate(sl,sh,nrl(i),nrh(i),ncl(i),nch(i));
00415 }
00416 }
00417
00422 d4_array::d4_array(int hsl, int hsu, int sl, const ivector& sh,
00423 int nrl, const imatrix& nrh, int ncl, int nch)
00424 {
00425 allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
00426 }
00427
00432 void d4_array::allocate(int hsl, int hsu, int sl, const ivector& sh,
00433 int nrl, const imatrix& nrh, int ncl, int nch)
00434 {
00435
00436
00437 if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00438 {
00439 cerr << " Error allocating memory in d4_array contructor\n";
00440 }
00441
00442 int ss=hslicesize();
00443 if ( (t = new d3_array[ss]) == 0)
00444 {
00445 cerr << " Error allocating memory in d3_array contructor\n";
00446 ad_exit(21);
00447 }
00448 t -= hslicemin();
00449 for (int i=hsl; i<=hsu; i++)
00450 {
00451 (*this)(i).allocate(sl,sh(i),nrl,nrh(i),ncl,nch);
00452 }
00453 }
00454
00459 d4_array::d4_array(int hsl,int hsu,int sl,int sh,int nrl,
00460 int nrh,int ncl,int nch)
00461 {
00462 allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
00463 }
00464
00469 d4_array::d4_array(int hsl,int hsu, int sl,int sh,ivector nrl,ivector nrh,
00470 ivector ncl,ivector nch)
00471 {
00472 allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
00473 }
00474
00479 void d4_array::initialize()
00480 {
00481 if (!(!(*this)))
00482 {
00483 for (int i=hslicemin();i<=hslicemax();i++)
00484 {
00485 elem(i).initialize();
00486 }
00487 }
00488 }
00489
00494 d4_array::d4_array(int hsl, int hsu, int sl, const ivector& sh, int nrl,
00495 const imatrix& nrh, int ncl, const i3_array& nch)
00496 {
00497 allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
00498 }
00499
00504 void d4_array::allocate(int hsl, int hsu, int sl, const ivector& sh, int nrl,
00505 const imatrix& nrh, int ncl, const i3_array& nch)
00506 {
00507
00508
00509
00510
00511
00512 if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00513 {
00514 cerr << " Error allocating memory in d4_array contructor\n";
00515 }
00516 int ss=hslicesize();
00517 if ( (t = new d3_array[ss]) == 0)
00518 {
00519 cerr << " Error allocating memory in d3_array contructor\n";
00520 ad_exit(21);
00521 }
00522 t -= hslicemin();
00523 for (int i=hsl; i<=hsu; i++)
00524 {
00525 (*this)(i).allocate(sl,sh(i),nrl,nrh(i),ncl,nch(i));
00526 }
00527 }
00528
00533 d4_array::d4_array(int hsl,int hsu,const index_type& sl,
00534 const index_type& sh,const index_type& nrl,const index_type& nrh,
00535 const index_type& ncl,const index_type& nch)
00536 {
00537 allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
00538 }
00539
00544 void d4_array::allocate(ad_integer hsl,ad_integer hsu,const index_type& sl,
00545 const index_type& sh,const index_type& nrl,const index_type& nrh,
00546 const index_type& ncl,const index_type& nch)
00547 {
00548 if (hsl>hsu)
00549 {
00550 allocate();
00551 return;
00552 }
00553 int ss=hsu-hsl+1;
00554 if ( (t = new d3_array[ss]) == 0)
00555 {
00556 cerr << " Error allocating memory in d3_array contructor\n";
00557 ad_exit(21);
00558 }
00559 if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00560 {
00561 cerr << " Error allocating memory in d3_array contructor\n";
00562 ad_exit(21);
00563 }
00564 t -= int(hsl);
00565 for (int i=hsl; i<=hsu; i++)
00566 {
00567 const index_type& rsl=sl[i];
00568 const index_type& rsh=sh[i];
00569 const index_type& rnrl=nrl[i];
00570 const index_type& rnrh=nrh[i];
00571 const index_type& rncl=ncl[i];
00572 const index_type& rnch=nch[i];
00573
00574 const ad_integer& aa=ad_integer(rsl);
00575 const ad_integer& bb=ad_integer(rsh);
00576
00577 (*this)(i).allocate(aa,bb,rnrl,rnrh,rncl,rnch);
00578
00579
00580 }
00581 }
00582
00587 void d4_array::allocate(int hsl,int hsu,const index_type& sl,
00588 const index_type& sh,const index_type& nrl,const index_type& nrh,
00589 const index_type& ncl,const index_type& nch)
00590 {
00591 if (hsl>hsu)
00592 {
00593 allocate();
00594 return;
00595 }
00596 int ss=hsu-hsl+1;
00597 if ( (t = new d3_array[ss]) == 0)
00598 {
00599 cerr << " Error allocating memory in d3_array contructor\n";
00600 ad_exit(21);
00601 }
00602 if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00603 {
00604 cerr << " Error allocating memory in d3_array contructor\n";
00605 ad_exit(21);
00606 }
00607 t -= int(hsl);
00608 for (int i=hsl; i<=hsu; i++)
00609 {
00610 const index_type& rsl=sl[i];
00611 const index_type& rsh=sh[i];
00612 const index_type& rnrl=nrl[i];
00613 const index_type& rnrh=nrh[i];
00614 const index_type& rncl=ncl[i];
00615 const index_type& rnch=nch[i];
00616
00617 const ad_integer& aa=ad_integer(rsl);
00618 const ad_integer& bb=ad_integer(rsh);
00619
00620 (*this)(i).allocate(aa,bb,rnrl,rnrh,rncl,rnch);
00621
00622
00623 }
00624 }
00625
00630 void d4_array::allocate(ad_integer hsl,ad_integer hsu,const index_type& sl,
00631 const index_type& sh,const index_type& nrl,const index_type& nrh)
00632 {
00633 if (hsl>hsu)
00634 {
00635 allocate();
00636 return;
00637 }
00638 int ss=hsu-hsl+1;
00639 if ( (t = new d3_array[ss]) == 0)
00640 {
00641 cerr << " Error allocating memory in d3_array contructor\n";
00642 ad_exit(21);
00643 }
00644 if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00645 {
00646 cerr << " Error allocating memory in d3_array contructor\n";
00647 ad_exit(21);
00648 }
00649 t -= int(hsl);
00650 for (int i=hsl; i<=hsu; i++)
00651 {
00652 const index_type& rsl=sl[i];
00653 const index_type& rsh=sh[i];
00654 const index_type& rnrl=nrl[i];
00655 const index_type& rnrh=nrh[i];
00656 const ad_integer& aa=ad_integer(rsl);
00657 const ad_integer& bb=ad_integer(rsh);
00658 (*this)(i).allocate(aa,bb,rnrl,rnrh);
00659 }
00660 }
00661
00666 void d4_array::allocate(ad_integer hsl,ad_integer hsu,const index_type& sl,
00667 const index_type& sh)
00668 {
00669 if (hsl>hsu)
00670 {
00671 allocate();
00672 return;
00673 }
00674 int ss=hsu-hsl+1;
00675 if ( (t = new d3_array[ss]) == 0)
00676 {
00677 cerr << " Error allocating memory in d3_array contructor\n";
00678 ad_exit(21);
00679 }
00680 if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00681 {
00682 cerr << " Error allocating memory in d3_array contructor\n";
00683 ad_exit(21);
00684 }
00685 t -= int(hsl);
00686 for (int i=hsl; i<=hsu; i++)
00687 {
00688 const index_type& rsl=sl[i];
00689 const index_type& rsh=sh[i];
00690 const ad_integer& aa=ad_integer(rsl);
00691 const ad_integer& bb=ad_integer(rsh);
00692 (*this)(i).allocate(aa,bb);
00693 }
00694 }
00695
00700 void d4_array::allocate(ad_integer hsl,ad_integer hsu)
00701 {
00702 if (hsl>hsu)
00703 {
00704 allocate();
00705 return;
00706 }
00707
00708 int ss=hsu-hsl+1;
00709 if ((t = new d3_array[ss]) == 0)
00710 {
00711 cerr << " Error allocating memory in d3_array contructor\n";
00712 ad_exit(21);
00713 }
00714 if ( (shape=new four_array_shape(hsl,hsu)) == 0)
00715 {
00716 cerr << " Error allocating memory in d3_array contructor\n";
00717 ad_exit(21);
00718 }
00719 t -= int(hsl);
00720 for (int i=hsl; i<=hsu; i++)
00721 {
00722 (*this)(i).allocate();
00723 }
00724 }