ADMB Documentation  11.1.2270
 All Classes Files Functions Variables Typedefs Friends Defines
cmpdif5.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: cmpdif5.cpp 1942 2014-04-28 22:22:45Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #include "fvar.hpp"
00012 
00013 
00014 //#undef TRACE
00015 //#define TRACE
00016 
00017 #ifdef __TURBOC__
00018   #pragma hdrstop
00019   #include <iostream.h>
00020 #endif
00021 
00022 #ifdef __ZTC__
00023   #include <iostream.hpp>
00024 #endif
00025 
00026 #include <string.h>
00027 
00032 void dmatrix::save_dmatrix_value(void) const
00033 {
00034   // saves the size, address, and value information for a dvar_matrix
00035   //int ierr;
00036   for (int i=rowmin();i<=rowmax();i++)
00037   {
00038     ((*this)(i).save_dvector_value());
00039     ((*this)(i).save_dvector_position());
00040   }
00041 }
00042 
00047 void d3_array::save_d3_array_value(void) const
00048 {
00049   // saves the size, address, and value information for a dvar_matrix
00050   //int ierr;
00051   for (int i=indexmin();i<=indexmax();i++)
00052   {
00053     ((*this)(i).save_dmatrix_value());
00054     ((*this)(i).save_dmatrix_position());
00055   }
00056 }
00057 
00062 dmatrix restore_dvar_matrix_value(const dvar_matrix_position& mpos)
00063 {
00064   // restores the size, address, and value information for a dvar_matrix
00065   dmatrix out((const dvar_matrix_position&)mpos);
00066   //int ierr;
00067   int min=out.rowmin();
00068   int max=out.rowmax();
00069   for (int i=max;i>=min;i--)
00070   {
00071     dvar_vector_position vpos=restore_dvar_vector_position();
00072     out(i)=restore_dvar_vector_value(vpos);
00073   }
00074   return out;
00075 }
00076 
00081 dmatrix restore_dmatrix_value(const dmatrix_position& mpos)
00082 {
00083   // restores the size, address, and value information for a dvar_matrix
00084   //  the size, address, and value information for a dvar_matrix
00085   dmatrix out((const dmatrix_position&) mpos);
00086   //int ierr;
00087   int min=out.rowmin();
00088   int max=out.rowmax();
00089   for (int i=max;i>=min;i--)
00090   {
00091     dvector_position vpos=restore_dvector_position();
00092     out(i)=restore_dvector_value(vpos);
00093   }
00094   return out;
00095 }
00096 
00101 d3_array restore_d3_array_value(const d3_array_position& mpos)
00102 {
00103   // restores the size, address, and value information for a dvar_matrix
00104   //  the size, address, and value information for a dvar_matrix
00105   d3_array out((const d3_array_position&) mpos);
00106   //int ierr;
00107   int min=out.indexmin();
00108   int max=out.indexmax();
00109   for (int i=max;i>=min;i--)
00110   {
00111     dmatrix_position vpos=restore_dmatrix_position();
00112     out(i)=restore_dmatrix_value(vpos);
00113   }
00114   return out;
00115 }
00116 
00121 dvector restore_dvar_vector_derivatives(const dvar_vector_position& tmp)
00122 {
00123   // puts the derivative values from a dvar_vector's guts into a dvector
00124   dvector tempvec(tmp.indexmin(),tmp.indexmax());
00125   double_and_int * va=tmp.va;
00126 
00127 #ifndef USE_ASSEMBLER
00128   for (int i=tmp.indexmin();i<=tmp.indexmax();i++)
00129   {
00130     tempvec(i)=va[i].xvalue();
00131     va[i].xvalue()=0.;
00132   }
00133 #else
00134      int min=tmp.indexmin();
00135      int n=tmp.max-min+1;
00136      dw_block_move(&(tempvec.elem(min)),&(va[min].xvalue()),n);
00137      dp_block_initialize(&(va[min].xvalue()),n);
00138 #endif
00139 
00140 //  _dp_vector_add
00141 //  _dp_vector_elem_div
00142 //  _dp_dotproduct
00143 //  _dp_vector_elem_prod
00144 //  _dp_scalar_product
00145 //  _dp_vector_sub
00146 //  _dw_block_move
00147 
00148   return tempvec;
00149 }
00150 
00155 dvector restore_dvar_vector_der_nozero(const dvar_vector_position& tmp)
00156 {
00157   // puts the derivative values from a dvar_vector's guts into a dvector
00158   dvector tempvec(tmp.min,tmp.max);
00159   double_and_int * va=tmp.va;
00160 
00161   #ifndef USE_ASSEMBLER
00162     for (int i=tmp.indexmin();i<=tmp.indexmax();i++)
00163     {
00164       tempvec(i)=va[i].xvalue();
00165     }
00166   #else
00167      int min=tmp.indexmin();
00168      int n=tmp.indexmax()-min+1;
00169      dw_block_move(&(tempvec.elem(min)),&(va[min].xvalue()),n);
00170   #endif
00171   return tempvec;
00172 }
00173 
00178 void dvector::save_dvector_derivatives(const dvar_vector_position& pos) const
00179 {
00180   // puts the derivative values in a dvector into a dvar_vector's guts
00181   int min=indexmin();
00182   int max=indexmax();
00183   if (min!=pos.indexmin() || max!=pos.indexmax())
00184   {
00185     cerr << "Incompatible array sizes in " <<
00186     "void dvector::save_dvector_derivatives(const dvar_vector_position& pos)"
00187     << endl;
00188   }
00189   double_and_int * ptr=pos.va;
00190 
00191   #ifndef USE_ASSEMBLER
00192     for (int i=min;i<=max;i++)
00193     {
00194       ptr[i].xvalue()+=(*this)(i);
00195     }
00196   #else
00197     int n=max-min+1;
00198     dp_vector_add(&(ptr[min].xvalue()),&(ptr[min].xvalue()),
00199       &(this->elem(min)),n);
00200   #endif
00201 }
00202 
00207 void dvector::save_dvector_derivatives_na(const dvar_vector_position& pos) const
00208 {
00209   // puts the derivative values in a dvector into a dvar_vector's guts
00210   int min=indexmin();
00211   int max=indexmax();
00212   if (min!=pos.indexmin() || max!=pos.indexmax())
00213   {
00214     cerr << "Incompatible array sizes in " <<
00215     "void dvector::save_dvector_derivatives_na(const dvar_vector_position& pos)"
00216     << endl;
00217   }
00218   double_and_int * ptr=pos.va;
00219 
00220 #ifndef USE_ASSEMBLER
00221   for (int i=min;i<=max;i++)
00222   {
00223     ptr[i].xvalue()=(*this)(i);
00224   }
00225 #else
00226      int n=max-min+1;
00227      dw_block_move(&(ptr[min].xvalue()),&(this->elem(min)),n);
00228 #endif
00229 }
00230 
00235 void save_dmatrix_derivatives(const dvar_matrix_position& _pos, const double x,
00236   const int& i, int& j)
00237 {
00238   dvar_matrix_position& pos= (dvar_matrix_position&) _pos;
00239   if ( i<pos.rowmin()||i>pos.rowmax() ||
00240      j<(pos.colmin())(i) || j>(pos.colmax())(i) )
00241   {
00242     cerr << "Error -- Index out of bounds in routine\n"
00243     " void save_dmatrix_derivatives(const dvar_matrix_position& pos"
00244     "  , const double& x, const int& i, const int& j)" << endl;
00245     ad_exit(1);
00246   }
00247   // puts the derivative values in a dvector into a dvar_vector's guts
00248   pos(i)(j)+=x;
00249 }
00250 
00255 void dmatrix::save_dmatrix_derivatives(const dvar_matrix_position& _pos) const
00256 {
00257   dvar_matrix_position& pos=(dvar_matrix_position&) _pos;
00258   // puts the derivative values in a dvector into a dvar_vector's guts
00259   int min=rowmin();
00260   int max=rowmax();
00261   if (min!=pos.row_min||max!=pos.row_max)
00262   {
00263     cerr << "Incompatible array sizes in " <<
00264     "void dmatrix::save_dmatrix__derivatives(const dvar_matrix_position& pos)"
00265     << endl;
00266   }
00267   for (int i=min;i<=max;i++)
00268   {
00269     if (allocated((*this)(i)))
00270       (*this)(i).save_dvector_derivatives(pos(i));
00271   }
00272 }
00273 
00278 void dmatrix::save_dmatrix_derivatives_na(const dvar_matrix_position& _pos)
00279   const
00280 {
00281   dvar_matrix_position& pos=(dvar_matrix_position&) _pos;
00282   // puts the derivative values in a dvector into a dvar_vector's guts
00283   int min=rowmin();
00284   int max=rowmax();
00285   if (min!=pos.row_min||max!=pos.row_max)
00286   {
00287     cerr << "Incompatible array sizes in " <<
00288     "void dmatrix::save_dmatrix__derivatives(const dvar_matrix_position& pos)"
00289     << endl;
00290   }
00291   for (int i=min;i<=max;i++)
00292   {
00293     if (allocated((*this)(i)))
00294       (*this)(i).save_dvector_derivatives_na(pos(i));
00295   }
00296 }
00297 
00302 void dvar_matrix::save_dvar_matrix_position(void) const
00303 {
00304   // saves the size and address information for a dvar_vector
00305   dvar_matrix_position tmp(*this,1);
00306   const int wsize=sizeof(int);
00307   const int wsize1=sizeof(void*);
00308 
00309   int min=rowmin();
00310   int max=rowmax();
00311   for (int i=min;i<=max;i++)
00312   {
00313     gradient_structure::get_fp()->fwrite(&(tmp.lb(i)),wsize);
00314     gradient_structure::get_fp()->fwrite(&(tmp.ub(i)),wsize);
00315     gradient_structure::get_fp()->fwrite(&(tmp.ptr(i)),wsize1);
00316    }
00317   gradient_structure::get_fp()->fwrite(&(tmp.row_min),wsize);
00318   gradient_structure::get_fp()->fwrite(&(tmp.row_max),wsize);
00319 }