ADMB Documentation  11.1.2536
 All Classes Files Functions Variables Typedefs Friends Defines
dmat.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: dmat.cpp 2419 2014-09-27 00:38:59Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #include "fvar.hpp"
00012 
00017  struct dvec_ptr_ptr
00018  {
00019    void ** m;
00020  };
00021 
00026  dmatrix::dmatrix( int nrl,  int nrh, int ncl,  int nch)
00027  {
00028    allocate(nrl,nrh,ncl,nch);
00029  }
00030 
00035  void dmatrix::allocate(int nrl,int nrh,int ncl,int nch)
00036  {
00037    if (nrh<nrl)
00038    {
00039      allocate();
00040      return;
00041    }
00042    index_min=nrl;
00043    index_max=nrh;
00044    if ( (m = new dvector [rowsize()]) == 0)
00045    {
00046      cerr << " Error allocating memory in dmatrix contructor\n";
00047      ad_exit(21);
00048    }
00049    if ( (shape = new mat_shapex(m))== 0)
00050    {
00051      cerr << " Error allocating memory in dmatrix contructor\n";
00052      ad_exit(21);
00053    }
00054    m -= rowmin();
00055    for (int i=rowmin(); i<=rowmax(); i++)
00056    {
00057      m[i].allocate(ncl,nch);
00058    }
00059  }
00060 
00065  dmatrix dmatrix::sub(int nrl,int nrh)
00066  {
00067    if (allocated(*this))
00068    {
00069      dmatrix 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  dmatrix::dmatrix(int nrl,int nrh)
00087  {
00088    allocate(nrl,nrh);
00089  }
00090 
00095  void dmatrix::allocate(int nrl,int nrh)
00096  {
00097    if (nrh<nrl)
00098    {
00099      allocate();
00100      return;
00101    }
00102    index_min=nrl;
00103    index_max=nrh;
00104    if ( (m = new dvector [rowsize()]) == 0)
00105    {
00106      cerr << " Error allocating memory in dmatrix contructor\n";
00107      ad_exit(21);
00108    }
00109    if ( (shape = new mat_shapex(m))== 0)
00110    {
00111      cerr << " Error allocating memory in dmatrix contructor\n";
00112      ad_exit(21);
00113    }
00114    m -= rowmin();
00115  }
00116 
00121  void dmatrix::allocate(ad_integer nrl,ad_integer nrh)
00122  {
00123    if (nrh<nrl)
00124    {
00125      allocate();
00126      return;
00127    }
00128    index_min=nrl;
00129    index_max=nrh;
00130    if ( (m = new dvector [rowsize()]) == 0)
00131    {
00132      cerr << " Error allocating memory in dmatrix contructor\n";
00133      ad_exit(21);
00134    }
00135    if ( (shape = new mat_shapex(m))== 0)
00136    {
00137      cerr << " Error allocating memory in dmatrix contructor\n";
00138      ad_exit(21);
00139    }
00140    m -= rowmin();
00141  }
00142 
00147 double min(const dmatrix& m)
00148 {
00149   double tmp=max(m(m.rowmin()));
00150   for (int i=m.rowmin()+1;i<=m.rowmax();i++)
00151   {
00152     double tmp1=min(m(i));
00153     if (tmp>tmp1) tmp=tmp1;
00154   }
00155   return tmp;
00156 }
00157 
00162 double max(const dmatrix& m)
00163 {
00164   double tmp=max(m(m.rowmin()));
00165   for (int i=m.rowmin()+1;i<=m.rowmax();i++)
00166   {
00167     double tmp1=max(m(i));
00168     if (tmp<tmp1) tmp=tmp1;
00169   }
00170   return tmp;
00171 }
00172 
00177 void dmatrix::allocate(const dmatrix& dm)
00178 {
00179   int nrl=dm.rowmin();
00180   int nrh=dm.rowmax();
00181   index_min=nrl;
00182   index_max=nrh;
00183 
00184   if ( (m = new dvector [rowsize()]) == 0)
00185   {
00186     cerr << " Error allocating memory in dmatrix contructor" << endl;
00187     ad_exit(21);
00188   }
00189 
00190   if ( (shape = new mat_shapex(m))== 0)
00191   {
00192     cerr << " Error allocating memory in dmatrix contructor" << endl;
00193     ad_exit(21);
00194   }
00195   m -= rowmin();
00196   for (int i=rowmin(); i<=rowmax(); i++)
00197   {
00198     m[i].allocate(dm(i));
00199   }
00200 }
00201 
00206 int ivector_check(const ivector& v, int l, int u)
00207   {
00208     if (v.indexmin()!=l||v.indexmax()!=u) return 1;
00209     return 0;
00210   }
00211 
00216  void dmatrix::allocate(int nrl,int nrh,const ivector& ncl,const ivector& nch)
00217  {
00218    if (nrh<nrl)
00219    {
00220      allocate();
00221      return;
00222    }
00223 
00224    if (nrl !=ncl.indexmin() || nrh !=ncl.indexmax() ||
00225      nrl !=nch.indexmin() || nrh !=nch.indexmax())
00226    {
00227      cerr << "Incompatible array bounds in "
00228      "dmatrix(int nrl,int nrh,const ivector& ncl,const ivector& nch)\n";
00229      ad_exit(1);
00230    }
00231    index_min=nrl;
00232    index_max=nrh;
00233    if ( (m = new dvector [rowsize()]) == 0)
00234    {
00235      cerr << " Error allocating memory in dmatrix contructor\n";
00236      ad_exit(21);
00237    }
00238    if ( (shape = new mat_shapex(m))== 0)
00239    {
00240      cerr << " Error allocating memory in dmatrix contructor\n";
00241      ad_exit(21);
00242    }
00243 
00244    m -= rowmin();
00245    for (int i=rowmin(); i<=rowmax(); i++)
00246    {
00247      m[i].allocate(ncl(i),nch(i));
00248    }
00249  }
00250 
00255  void dmatrix::allocate(int nrl,int nrh,int ncl,const ivector& nch)
00256  {
00257    if (nrh<nrl)
00258    {
00259      allocate();
00260      return;
00261    }
00262    if (nrl !=nch.indexmin() || nrh !=nch.indexmax())
00263    {
00264      cerr << "Incompatible array bounds in dmatrix(int nrl,int nrh,"
00265        "int ncl,const ivector& nch)\n";
00266      ad_exit(1);
00267    }
00268    index_min=nrl;
00269    index_max=nrh;
00270    if ( (m = new dvector [rowsize()]) == 0)
00271    {
00272      cerr << " Error allocating memory in dmatrix contructor\n";
00273      ad_exit(21);
00274    }
00275    if ( (shape = new mat_shapex(m))== 0)
00276    {
00277      cerr << " Error allocating memory in dmatrix contructor\n";
00278      ad_exit(21);
00279    }
00280    m -= rowmin();
00281    for (int i=rowmin(); i<=rowmax(); i++)
00282    {
00283      m[i].allocate(ncl,nch(i));
00284    }
00285  }
00286 
00291  void dmatrix::allocate(int nrl,int nrh,const ivector& ncl,int nch)
00292  {
00293    if (nrh<nrl)
00294    {
00295      allocate();
00296      return;
00297    }
00298    if (nrl !=ncl.indexmin() || nrh !=ncl.indexmax())
00299    {
00300      cerr << "Incompatible array bounds in dmatrix(int nrl,int nrh,"
00301        " int ncl,const ivector& nch)\n";
00302      ad_exit(1);
00303    }
00304    index_min=nrl;
00305    index_max=nrh;
00306    if ( (m = new dvector [rowsize()]) == 0)
00307    {
00308      cerr << " Error allocating memory in dmatrix contructor\n";
00309      ad_exit(21);
00310    }
00311    if ( (shape = new mat_shapex(m))== 0)
00312    {
00313      cerr << " Error allocating memory in dmatrix contructor\n";
00314      ad_exit(21);
00315    }
00316    m -= rowmin();
00317    for (int i=rowmin(); i<=rowmax(); i++)
00318    {
00319      m[i].allocate(ncl(i),nch);
00320    }
00321  }
00322 
00323 /*
00324  dmatrix::dmatrix(void)
00325  {
00326    #ifdef DIAG
00327      myheapcheck("Entering dmatrix(nrl,nrh,ncl,nch)" );
00328    #endif
00329 
00330    shape = NULL;
00331    m=NULL;
00332  }
00333 */
00334 
00339 dmatrix::dmatrix(int nrl, int nrh, const ivector& ncl, const ivector& nch)
00340  {
00341    allocate(nrl,nrh,ncl,nch);
00342  }
00343 
00348 dmatrix::dmatrix(int nrl, int nrh, int ncl, const ivector& nch)
00349  {
00350    allocate(nrl,nrh,ncl,nch);
00351  }
00352 
00357 dmatrix::dmatrix(const dmatrix& m2)
00358  {
00359    index_min=m2.index_min;
00360    index_max=m2.index_max;
00361    shape=m2.shape;
00362    if (shape)
00363    {
00364      (shape->ncopies)++;
00365    }
00366 #ifdef SAFE_ALL
00367    else
00368    {
00369      cerr << "Making a copy of an unallocated dmatrix"<<endl;
00370    }
00371 #endif
00372    m = m2.m;
00373  }
00374 
00379 void dmatrix::shallow_copy(const dmatrix& m2)
00380  {
00381    index_min=m2.index_min;
00382    index_max=m2.index_max;
00383    shape=m2.shape;
00384    if (shape)
00385    {
00386      (shape->ncopies)++;
00387    }
00388 #ifdef SAFE_ALL
00389    else
00390    {
00391      cerr << "Making a copy of an unallocated dmatrix"<<endl;
00392    }
00393 #endif
00394    m = m2.m;
00395  }
00396 
00401  dmatrix::~dmatrix()
00402  {
00403    deallocate();
00404  }
00405 
00410 dvector cube(const dvector& m)
00411    {
00412      dvector tmp;
00413      tmp.allocate(m);
00414      for (int i=tmp.indexmin();i<=tmp.indexmax();i++)
00415      {
00416        tmp(i)=cube(m(i));
00417      }
00418      return tmp;
00419    }
00420 
00425 dmatrix cube(const dmatrix& m)
00426    {
00427      dmatrix tmp;
00428      tmp.allocate(m);
00429      for (int i=tmp.rowmin();i<=tmp.rowmax();i++)
00430      {
00431        tmp(i)=cube(m(i));
00432      }
00433      return tmp;
00434    }
00435 
00440  void dmatrix::deallocate()
00441  {
00442 /*
00443    static int testflag=0;
00444    static int ycounter=0;
00445    if (testflag)
00446    {
00447      ycounter++;
00448      cout << " A " << ycounter << endl;
00449      test_the_pointer();
00450    }
00451 */
00452    if (shape)
00453    {
00454      if (shape->ncopies)
00455      {
00456        (shape->ncopies)--;
00457      }
00458      else
00459      {
00460        m= (dvector*) (shape->get_pointer());
00461        delete [] m;
00462        m=NULL;
00463        delete shape;
00464        shape=NULL;
00465      }
00466    }
00467 /*
00468    if (testflag)
00469    {
00470      cout << " B " << ycounter << endl;
00471      test_the_pointer();
00472    }
00473 */
00474 #ifdef SAFE_ALL
00475    else
00476    {
00477      cerr << "Warning -- trying to deallocate an unallocated dmatrix"<<endl;
00478    }
00479 #endif
00480  }