ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
fvar_mat.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 "param_init_bounded_number_matrix.h"
00013 
00018  dvar_matrix::dvar_matrix(int nrl,int nrh,int ncl,int nch)
00019  {
00020    allocate(nrl,nrh,ncl,nch);
00021 #ifndef OPT_LIB
00022    initialize();
00023 #endif
00024  }
00025 
00030  dvar_matrix::dvar_matrix(int nrl,int nrh,kkludge_object kk)
00031  {
00032    allocate(nrl,nrh);
00033 #ifndef OPT_LIB
00034    initialize();
00035 #endif
00036  }
00037 
00042  dvar_matrix::dvar_matrix(int nrl,int nrh)
00043  {
00044    allocate(nrl,nrh);
00045 #ifndef OPT_LIB
00046    initialize();
00047 #endif
00048  }
00049 
00054  dvar_matrix::dvar_matrix(const param_init_bounded_number_matrix& pibnm)
00055  {
00056    int indexmin = pibnm.indexmin();
00057    int indexmax = pibnm.indexmax();
00058    allocate(indexmin, indexmax);
00059 
00060 #ifndef OPT_LIB
00061    initialize();
00062 #endif
00063 
00064    for (int i = indexmin; i <= indexmax; i++)
00065    {
00066      dvar_vector v(pibnm(i));
00067      this->operator()(i) = v;
00068    }
00069  }
00070 
00075  dvar_matrix dvar_matrix::sub(int nrl,int nrh)
00076  {
00077    if (allocated(*this))
00078    {
00079      dvar_matrix tmp(nrl,nrh);
00080      for (int i=nrl; i<=nrh; i++)
00081      {
00082        tmp[i].shallow_copy((*this)(i));
00083      }
00084      return tmp;
00085    }
00086    else
00087    {
00088      return *this;
00089    }
00090  }
00091 
00096  void dvar_matrix::allocate(int nrl,int nrh)
00097  {
00098    if (nrl>nrh)
00099      allocate();
00100    else
00101    {
00102      index_min=nrl;
00103      index_max=nrh;
00104      int rs=rowsize();
00105      if ( (m = new dvar_vector [rs]) == 0)
00106      {
00107        cerr << " Error allocating memory in dvar_matrix contructor\n";
00108        ad_exit(21);
00109      }
00110      if ( (shape=new mat_shapex(m)) == 0)
00111      {
00112        cerr << " Error allocating memory in dvar_matrix contructor\n";
00113      }
00114      m -= rowmin();
00115    }
00116  }
00117 
00122  void dvar_matrix::allocate(int nrl,int nrh,int ncl,int nch)
00123  {
00124    if (nrl>nrh)
00125      allocate();
00126    else
00127    {
00128      index_min=nrl;
00129      index_max=nrh;
00130      int rs=rowsize();
00131      if ( (m = new dvar_vector [rs]) == 0)
00132      {
00133        cerr << " Error allocating memory in dvar_matrix contructor\n";
00134        ad_exit(21);
00135      }
00136      if ( (shape=new mat_shapex(m)) == 0)
00137      {
00138        cerr << " Error allocating memory in dvar_matrix contructor\n";
00139      }
00140      m -= rowmin();
00141      for (int i=nrl; i<=nrh; i++)
00142      {
00143        m[i].allocate(ncl,nch);
00144      }
00145    }
00146  }
00147 
00152  void dvar_matrix::allocate(ad_integer nrl,ad_integer nrh)
00153  {
00154    if (nrl>nrh)
00155      allocate();
00156    else
00157    {
00158      index_min=nrl;
00159      index_max=nrh;
00160      if ( (m = new dvar_vector [rowsize()]) == 0)
00161      {
00162        cerr << " Error allocating memory in dvar_matrix contructor\n";
00163        ad_exit(21);
00164      }
00165      if ( (shape=new mat_shapex(m)) == 0)
00166      {
00167        cerr << " Error allocating memory in dvar_matrix contructor\n";
00168      }
00169      m -= rowmin();
00170    }
00171  }
00172 
00177  void dvar_matrix::allocate(const dmatrix& m1)
00178  {
00179    if (m1.shape)
00180    {
00181      int nrl=m1.rowmin();
00182      int nrh=m1.rowmax();
00183      index_min=nrl;
00184      index_max=nrh;
00185      int rs=rowsize();
00186      if ( (m = new dvar_vector [rs]) == 0)
00187      {
00188        cerr << " Error allocating memory in dvar_matrix contructor\n";
00189        ad_exit(21);
00190      }
00191      if ( (shape=new mat_shapex(m)) == 0)
00192      {
00193        cerr << " Error allocating memory in dvar_matrix contructor\n";
00194      }
00195      m -= rowmin();
00196      for (int i=nrl; i<=nrh; i++)
00197      {
00198        m[i].allocate(m1(i));
00199      }
00200    }
00201    else
00202    {
00203      //cerr << "Warning -- trying to make a dvar_matrix copy of an "
00204       // " unallocated dmatrix" << endl;
00205      allocate();
00206    }
00207  }
00208 
00213 void dvar_matrix::allocate(const dvar_matrix& m1)
00214  {
00215    if (m1.shape)
00216    {
00217      int nrl=m1.rowmin();
00218      int nrh=m1.rowmax();
00219      index_min=nrl;
00220      index_max=nrh;
00221      int rs=rowsize();
00222      //if ( (m = new dvar_vector [rowsize()]) == 0)
00223      if ( (m = new dvar_vector [rs]) == 0)
00224      {
00225        cerr << " Error allocating memory in dvar_matrix contructor"<<endl;
00226       ad_exit(21);
00227      }
00228      if ( (shape=new mat_shapex(m)) == 0)
00229      {
00230        cerr << " Error allocating memory in dvar_matrix contructor"<<endl;
00231      }
00232      m -= rowmin();
00233      for (int i=nrl; i<=nrh; i++)
00234      {
00235        m[i].allocate(m1(i));
00236      }
00237    }
00238    else
00239    {
00240      //cerr << "Warning -- trying to make a dvar_matrix copy of an "
00241      //  "unallocated dvar_matrix" << endl;
00242      allocate();
00243    }
00244  }
00245 
00250 dvar_matrix::dvar_matrix(int nrl, int nrh, const ivector& ncl,
00251   const ivector& nch)
00252  {
00253    allocate(nrl,nrh,ncl,nch);
00254 #ifndef OPT_LIB
00255    initialize();
00256 #endif
00257  }
00258 
00263 void dvar_matrix::allocate(int nrl, int nrh, const ivector& ncl,
00264   const ivector& nch)
00265  {
00266    if (nrl>nrh)
00267      allocate();
00268    else
00269    {
00270      if (nrl !=ncl.indexmin() || nrh !=ncl.indexmax() ||
00271        nrl !=nch.indexmin() || nrh !=nch.indexmax())
00272      {
00273        cerr << "Incompatible array bounds in "
00274        "dvar_matrix(int nrl, int nrh, const ivector& ncl, const ivector& nch)"
00275             << endl ;
00276        ad_exit(1);
00277      }
00278      index_min=nrl;
00279      index_max=nrh;
00280 
00281      int rs=rowsize();
00282      if ( (m = new dvar_vector [rs]) == 0)
00283      {
00284        cerr << " Error allocating memory in dvar_matrix contructor"<<endl;
00285        ad_exit(21);
00286      }
00287      if ( (shape=new mat_shapex(m)) == 0)
00288      {
00289        cerr << " Error allocating memory in dvar_matrix contructor"<<endl;
00290      }
00291 
00292      m -= rowmin();
00293 
00294      for (int i=nrl; i<=nrh; i++)
00295      {
00296        m[i].allocate(ncl[i],nch[i]);
00297      }
00298    }
00299  }
00300 
00305 dvar_matrix::dvar_matrix(int nrl, int nrh, int ncl, const ivector& nch)
00306  {
00307    allocate(nrl,nrh,ncl,nch);
00308 #ifndef OPT_LIB
00309    initialize();
00310 #endif
00311  }
00312 
00317 void dvar_matrix::allocate(int nrl, int nrh, int ncl, const ivector& nch)
00318  {
00319    if (nrl>nrh)
00320      allocate();
00321    else
00322    {
00323      if (nrl !=nch.indexmin() || nrh !=nch.indexmax())
00324      {
00325        cerr << "Incompatible array bounds in "
00326        "dvar_matrix(int nrl, int nrh, const int& ncl, const ivector& nch)"
00327        << endl;
00328        ad_exit(1);
00329      }
00330      index_min=nrl;
00331      index_max=nrh;
00332 
00333      int rs=rowsize();
00334      if ( (m = new dvar_vector [rs]) == 0)
00335      {
00336        cerr << " Error allocating memory in dvar_matrix contructor"<<endl;
00337        ad_exit(21);
00338      }
00339 
00340      if ( (shape=new mat_shapex(m)) == 0)
00341      {
00342        cerr << " Error allocating memory in dvar_matrix contructor"<<endl;
00343      }
00344      m -= rowmin();
00345 
00346      for (int i=nrl; i<=nrh; i++)
00347      {
00348        m[i].allocate(ncl,nch[i]);
00349      }
00350    }
00351  }
00352 
00357 void dvar_matrix::allocate(int nrl, int nrh, const ivector& ncl, int nch)
00358  {
00359    if (nrl>nrh)
00360      allocate();
00361    else
00362    {
00363      if (nrl !=ncl.indexmin() || nrh !=ncl.indexmax())
00364      {
00365        cerr << "Incompatible array bounds in "
00366        "dvar_matrix(int nrl, int nrh, const ivector& ncl,int nch)"
00367        << endl;
00368        ad_exit(1);
00369      }
00370      index_min=nrl;
00371      index_max=nrh;
00372 
00373      int rs=rowsize();
00374      if ( (m = new dvar_vector [rs]) == 0)
00375      {
00376        cerr << " Error allocating memory in dvar_matrix contructor"<<endl;
00377        ad_exit(21);
00378      }
00379 
00380      if ( (shape=new mat_shapex(m)) == 0)
00381      {
00382        cerr << " Error allocating memory in dvar_matrix contructor"<<endl;
00383      }
00384      m -= rowmin();
00385 
00386      for (int i=nrl; i<=nrh; i++)
00387      {
00388        m[i].allocate(ncl[i],nch);
00389      }
00390    }
00391  }
00392 
00397 dvar_matrix::dvar_matrix(const dvar_matrix& m2)
00398  {
00399    if (!(m2))
00400    {
00401      //cerr << "Making a copy of an unallocated dvar_matrix" << endl;
00402      index_min=0;
00403      index_max=-1;
00404      shape=NULL;
00405      m=NULL;
00406    }
00407    else
00408    {
00409      index_min=m2.index_min;
00410      index_max=m2.index_max;
00411      shape=m2.shape;
00412      (shape->ncopies)++;
00413      m = m2.m;
00414    }
00415  }
00416 
00421  void dvar_matrix::shallow_copy(const dvar_matrix& m2)
00422  {
00423    if (!(m2))
00424    {
00425      //cerr << "Making a copy of an unallocated dvar_matrix" << endl;
00426      index_min=0;
00427      index_max=-1;
00428      shape=NULL;
00429      m=NULL;
00430    }
00431    else
00432    {
00433      index_min=m2.index_min;
00434      index_max=m2.index_max;
00435      shape=m2.shape;
00436      (shape->ncopies)++;
00437      m = m2.m;
00438    }
00439  }
00440 
00445  void dvar_matrix::allocate(void)
00446  {
00447    index_min=1;
00448    index_max=0;
00449    shape=NULL;
00450    m = NULL;
00451  }
00452 
00457  dvar_matrix::dvar_matrix(void)
00458  {
00459    allocate();
00460  }
00461 
00466 dvar_matrix::dvar_matrix(const dmatrix& m2)
00467  {
00468    index_min=m2.index_min;
00469    index_max=m2.index_max;
00470    int rs=rowsize();
00471    if ( (m = new dvar_vector [rs]) == 0)
00472    {
00473      cerr << " Error allocating memory in dvar_matrix contructor"<<endl;
00474      ad_exit(21);
00475    }
00476    if ( (shape =new mat_shapex(m)) == 0)
00477    {
00478      cerr << " Error allocating memory in dvar_matrix contructor"<<endl;
00479    }
00480 
00481    m -= rowmin();
00482    for (int i=rowmin(); i<=rowmax(); i++)
00483    {
00484      m[i].allocate(m2(i).indexmin(),m2(i).indexmax());
00485      elem(i)=m2.elem(i);
00486    }
00487  }
00488 
00493  dvar_matrix::~dvar_matrix()
00494  {
00495    if (shape)
00496    {
00497      if (shape->ncopies)
00498      {
00499        (shape->ncopies)--;
00500      }
00501      else
00502      {
00503        deallocate();
00504      }
00505    }
00506  }
00507 
00512  void dvar_matrix::deallocate()
00513  {
00514    if (shape)
00515    {
00516      m=(dvar_vector*)(shape->get_pointer());
00517      delete [] m;
00518      delete shape;
00519      m=NULL;
00520      shape=NULL;
00521    }
00522    else
00523    {
00524      //cerr << "Warning -- trying to delete an unallocated dvar_matrix"<<endl;
00525    }
00526  }
00527 
00532 dvar_matrix& dvar_matrix::operator=(const dvar_matrix& m1)
00533  {
00534    if (!allocated(*this))
00535    {
00536      shallow_copy(m1);
00537    }
00538    else
00539    {
00540      if (rowmin() != m1.rowmin() || rowmax() != m1.rowmax())
00541      {
00542        cerr << " Incompatible array bounds in dvar_matrix& operator = "
00543          "(const dvar_matrix&)\n";
00544        ad_exit(21);
00545      }
00546 
00547      if (m != m1.m)            // check for condition that both matrices
00548      {                         // don't point to the same object
00549        for (int i=rowmin(); i<=rowmax(); i++)
00550        {
00551          (*this)[i]= m1[i];
00552        }
00553      }
00554    }
00555    return(*this);
00556  }
00557 
00562 dvar_matrix& dvar_matrix::operator=(const dmatrix& m1)
00563  {
00564    if (rowmin() != m1.rowmin() || rowmax() != m1.rowmax() ||
00565      colmin() != m1.colmin() || colmax() != m1.colmax() )
00566    {
00567      cerr << " Incompatible array bounds in "
00568      "dvar_matrix& operator = (const dvar_vector&)\n";
00569      ad_exit(21);
00570    }
00571 
00572    for (int i=rowmin(); i<=rowmax(); i++)
00573    {
00574      (*this)[i]= m1[i];
00575    }
00576    return(*this);
00577  }
00578 
00583 void copy_status(const ostream& _s, const dvar_matrix& m1)
00584   {
00585     ostream& s= (ostream&) _s;
00586     s << " matrix_copy flags \n";
00587 
00588     for (int i=m1.rowmin(); i<=m1.rowmax(); i++)
00589     {
00590       copy_status(s,m1[i]);
00591     }
00592     s <<"\n";
00593   }