ADMB Documentation  11.1.2497
 All Classes Files Functions Variables Typedefs Friends Defines
imat.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: imat.cpp 1713 2014-03-01 00:26:20Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #include "fvar.hpp"
00012 #ifdef __TURBOC__
00013   #pragma hdrstop
00014 #endif
00015 
00020 void imatrix::rowshift(int min )
00021 {
00022   m = m + rowmin() - min;
00023   index_max+=min-index_min;
00024   index_min=min;
00025 }
00026 
00031  imatrix::imatrix(int nrl,int nrh)
00032  {
00033    allocate(nrl,nrh);
00034  }
00035 
00040  imatrix imatrix::sub(int nrl,int nrh)
00041  {
00042    if (allocated(*this))
00043    {
00044      imatrix tmp(nrl,nrh);
00045      for (int i=nrl; i<=nrh; i++)
00046      {
00047        tmp[i].shallow_copy((*this)(i));
00048      }
00049      return tmp;
00050    }
00051    else
00052    {
00053      return *this;
00054    }
00055  }
00056 
00061  imatrix::imatrix(int nrl, int nrh, int ncl, int nch)
00062  {
00063    allocate(nrl,nrh,ncl,nch);
00064  }
00065 
00070  imatrix::imatrix(const ad_integer& nrl,const ad_integer& nrh,
00071    const index_type& ncl,const index_type& nch)
00072  {
00073    allocate(nrl,nrh,ncl,nch);
00074  }
00075 
00080 imatrix::imatrix(int nrl, int nrh, const ivector& iv)
00081  {
00082    allocate(nrl,nrh,iv);
00083  }
00084 
00089 void imatrix::allocate(int nrl, int nrh, const ivector& iv)
00090  {
00091    if (nrl>nrh)
00092    {
00093      allocate();
00094    }
00095    else
00096    {
00097      index_min=nrl;
00098      index_max=nrh;
00099 
00100      int rs=rowsize();
00101      if ( (m = new ivector [rs]) == 0)
00102      {
00103        cerr << " Error allocating memory in imatrix contructor\n";
00104        ad_exit(21);
00105      }
00106 
00107      if ( (shape = new mat_shapex(m))== 0)
00108      {
00109        cerr << " Error allocating memory in imatrix contructor\n";
00110        ad_exit(21);
00111      }
00112 
00113      m -= rowmin();
00114      for (int i=rowmin(); i<=rowmax(); i++)
00115      {
00116        m[i].index_min=iv.index_min;
00117        m[i].index_max=iv.index_max;
00118        m[i].shape=iv.shape;
00119        if (m[i].shape)
00120        {
00121          (m[i].shape->ncopies)++;
00122          m[i].v = iv.v;
00123        }
00124      }
00125    }
00126  }
00127 
00132  void imatrix::allocate(int nrl,int nrh,int ncl,int nch)
00133  {
00134    if (nrl>nrh)
00135    {
00136      allocate();
00137    }
00138    else
00139    {
00140      index_min=nrl;
00141      index_max=nrh;
00142      int rs=rowsize();
00143      if ( (m = new ivector [rs]) == 0)
00144      {
00145        cerr << " Error allocating memory in imatrix contructor\n";
00146        ad_exit(21);
00147      }
00148 
00149      if ( (shape = new mat_shapex(m))== 0)
00150      {
00151        cerr << " Error allocating memory in imatrix contructor\n";
00152        ad_exit(21);
00153      }
00154 
00155      m -= rowmin();
00156      for (int i=rowmin(); i<=rowmax(); i++)
00157      {
00158        m[i].allocate(ncl,nch);
00159      }
00160    }
00161  }
00162 
00167  void imatrix::allocate(void)  //default constructor
00168  {
00169    index_min=0;
00170    index_max=-1;
00171    m=NULL;
00172    shape=NULL;
00173  }
00174 
00179  void imatrix::allocate(int nrl,int nrh,const ivector& ncl,const ivector& nch)
00180  {
00181    if (nrl>nrh)
00182    {
00183      allocate();
00184    }
00185    else
00186    {
00187      index_min=nrl;
00188      index_max=nrh;
00189      int rs=rowsize();
00190      if ( (m = new ivector [rs]) == 0)
00191      {
00192        cerr << " Error allocating memory in imatrix contructor\n";
00193        ad_exit(21);
00194      }
00195      if ( (shape = new mat_shapex(m))== 0)
00196      {
00197        cerr << " Error allocating memory in imatrix contructor\n";
00198        ad_exit(21);
00199      }
00200      if (nrl !=ncl.indexmin() || nrh !=ncl.indexmax() ||
00201        nrl !=nch.indexmin() || nrh !=nch.indexmax())
00202      {
00203        cerr << "Incompatible array bounds in "
00204        "dmatrix(int nrl,int nrh, const ivector& ncl, const ivector& nch)\n";
00205        ad_exit(1);
00206      }
00207      m -= rowmin();
00208      for (int i=rowmin(); i<=rowmax(); i++)
00209      {
00210        m[i].allocate(ncl(i),nch(i));
00211      }
00212    }
00213  }
00214 
00219  void imatrix::allocate(int nrl, int nrh, int ncl, const ivector& nch)
00220  {
00221    if (nrl>nrh)
00222    {
00223      allocate();
00224    }
00225    else
00226    {
00227      index_min=nrl;
00228      index_max=nrh;
00229      int rs=rowsize();
00230      if ( (m = new ivector [rs]) == 0)
00231      {
00232        cerr << " Error allocating memory in imatrix contructor\n";
00233        ad_exit(21);
00234      }
00235      if (nrl !=nch.indexmin() || nrh !=nch.indexmax())
00236      {
00237        cerr << "Incompatible array bounds in "
00238        "imatrix::allocate(int nrl,int nrh,int ncl, const ivector& nch)\n";
00239        ad_exit(1);
00240      }
00241      if ( (shape = new mat_shapex(m))== 0)
00242      {
00243        cerr << " Error allocating memory in imatrix contructor\n";
00244        ad_exit(21);
00245      }
00246      m -= rowmin();
00247      for (int i=rowmin(); i<=rowmax(); i++)
00248      {
00249        m[i].allocate(ncl,nch(i));
00250      }
00251    }
00252  }
00253 
00258 imatrix::imatrix(const imatrix& m2)
00259  {
00260    index_min=m2.index_min;
00261    index_max=m2.index_max;
00262    if (m2.shape)
00263    {
00264      shape=m2.shape;
00265      (shape->ncopies)++;
00266      m = m2.m;
00267    }
00268    else
00269    {
00270      shape=NULL;
00271      m=NULL;
00272    }
00273  }
00274 
00279 void imatrix::shallow_copy(const imatrix& m2)
00280  {
00281    index_min=m2.index_min;
00282    index_max=m2.index_max;
00283    if (m2.shape)
00284    {
00285      shape=m2.shape;
00286      (shape->ncopies)++;
00287      m = m2.m;
00288    }
00289    else
00290    {
00291      shape=NULL;
00292      m=NULL;
00293    }
00294  }
00295 
00300 imatrix::imatrix(int nrl, int nrh, const ivector& ncl, const ivector& nch)
00301  {
00302    allocate(nrl,nrh,ncl,nch);
00303  }
00304 
00309 imatrix::imatrix(int nrl, int nrh, int ncl, const ivector& nch)
00310  {
00311    allocate(nrl,nrh,ncl,nch);
00312  }
00313 
00318  imatrix::imatrix(void)
00319  {
00320    allocate();
00321  }
00322 
00327  imatrix::~imatrix()
00328  {
00329    deallocate();
00330  }
00331 
00336  void imatrix::deallocate()
00337  {
00338    if (shape)
00339    {
00340      if (shape->ncopies)
00341      {
00342        (shape->ncopies)--;
00343      }
00344      else
00345      {
00346        m= (ivector*) (shape->get_pointer());
00347        delete [] m;
00348        m=NULL;
00349        delete shape;
00350        shape=NULL;
00351      }
00352    }
00353  }
00354 
00359  void imatrix::allocate(int nrl,int nrh)
00360  {
00361    if (nrl>nrh)
00362      allocate();
00363    else
00364    {
00365      index_min=nrl;
00366      index_max=nrh;
00367      int rs=rowsize();
00368      if ( (m = new ivector [rs]) == 0)
00369      {
00370        cerr << " Error allocating memory in imatrix contructor\n";
00371        ad_exit(21);
00372      }
00373      if ( (shape = new mat_shapex(m))== 0)
00374      {
00375        cerr << " Error allocating memory in imatrix contructor\n";
00376        ad_exit(21);
00377      }
00378      m -= indexmin();
00379    }
00380  }