ADMB Documentation  11.4.2891
 All Classes Files Functions Variables Typedefs Friends Defines
cmpdif5.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 <string.h>
00013 #include <cassert>
00014 
00019 void dmatrix::save_dmatrix_value(void) const
00020 {
00021   // saves the size, address, and value information for a dvar_matrix
00022   //int ierr;
00023   for (int i=rowmin();i<=rowmax();i++)
00024   {
00025     ((*this)(i).save_dvector_value());
00026     ((*this)(i).save_dvector_position());
00027   }
00028 }
00029 
00034 void d3_array::save_d3_array_value(void) const
00035 {
00036   // saves the size, address, and value information for a dvar_matrix
00037   //int ierr;
00038   for (int i=indexmin();i<=indexmax();i++)
00039   {
00040     ((*this)(i).save_dmatrix_value());
00041     ((*this)(i).save_dmatrix_position());
00042   }
00043 }
00044 
00049 dmatrix restore_dvar_matrix_value(const dvar_matrix_position& mpos)
00050 {
00051   // restores the size, address, and value information for a dvar_matrix
00052   dmatrix out((const dvar_matrix_position&)mpos);
00053   //int ierr;
00054   int min=out.rowmin();
00055   int max=out.rowmax();
00056   for (int i=max;i>=min;i--)
00057   {
00058     dvar_vector_position vpos=restore_dvar_vector_position();
00059     out(i)=restore_dvar_vector_value(vpos);
00060   }
00061   return out;
00062 }
00063 
00068 dmatrix restore_dmatrix_value(const dmatrix_position& mpos)
00069 {
00070   // restores the size, address, and value information for a dvar_matrix
00071   //  the size, address, and value information for a dvar_matrix
00072   dmatrix out((const dmatrix_position&) mpos);
00073   //int ierr;
00074   int min=out.rowmin();
00075   int max=out.rowmax();
00076   for (int i=max;i>=min;i--)
00077   {
00078     dvector_position vpos=restore_dvector_position();
00079     out(i)=restore_dvector_value(vpos);
00080   }
00081   return out;
00082 }
00083 
00088 d3_array restore_d3_array_value(const d3_array_position& mpos)
00089 {
00090   // restores the size, address, and value information for a dvar_matrix
00091   //  the size, address, and value information for a dvar_matrix
00092   d3_array out((const d3_array_position&) mpos);
00093   //int ierr;
00094   int min=out.indexmin();
00095   int max=out.indexmax();
00096   for (int i=max;i>=min;i--)
00097   {
00098     dmatrix_position vpos=restore_dmatrix_position();
00099     out(i)=restore_dmatrix_value(vpos);
00100   }
00101   return out;
00102 }
00103 
00108 dvector restore_dvar_vector_derivatives(const dvar_vector_position& tmp)
00109 {
00110   // puts the derivative values from a dvar_vector's guts into a dvector
00111   dvector tempvec(tmp.indexmin(),tmp.indexmax());
00112   double_and_int * va=tmp.va;
00113 
00114 #ifndef USE_ASSEMBLER
00115   for (int i=tmp.indexmin();i<=tmp.indexmax();i++)
00116   {
00117     tempvec(i)=va[i].xvalue();
00118     va[i].xvalue()=0.;
00119   }
00120 #else
00121      int min=tmp.indexmin();
00122      int n=tmp.max-min+1;
00123      dw_block_move(&(tempvec.elem(min)),&(va[min].xvalue()),n);
00124      dp_block_initialize(&(va[min].xvalue()),n);
00125 #endif
00126 
00127 //  _dp_vector_add
00128 //  _dp_vector_elem_div
00129 //  _dp_dotproduct
00130 //  _dp_vector_elem_prod
00131 //  _dp_scalar_product
00132 //  _dp_vector_sub
00133 //  _dw_block_move
00134 
00135   return tempvec;
00136 }
00137 
00142 dvector restore_dvar_vector_der_nozero(const dvar_vector_position& tmp)
00143 {
00144   // puts the derivative values from a dvar_vector's guts into a dvector
00145   dvector tempvec(tmp.min,tmp.max);
00146   double_and_int * va=tmp.va;
00147 
00148   #ifndef USE_ASSEMBLER
00149     for (int i=tmp.indexmin();i<=tmp.indexmax();i++)
00150     {
00151       tempvec(i)=va[i].xvalue();
00152     }
00153   #else
00154      int min=tmp.indexmin();
00155      int n=tmp.indexmax()-min+1;
00156      dw_block_move(&(tempvec.elem(min)),&(va[min].xvalue()),n);
00157   #endif
00158   return tempvec;
00159 }
00160 
00164 void dvector::save_dvector_derivatives(const dvar_vector_position& pos) const
00165 {
00166   const int min = indexmin();
00167   const int max = indexmax();
00168 
00169   //Check for incompatible array sizes
00170   assert(min == pos.indexmin() && max == pos.indexmax());
00171 
00172 #ifdef USE_ASSEMBLER
00173   double_and_int* ptr = pos.va;
00174   int n=max-min+1;
00175   dp_vector_add(&(ptr[min].xvalue()), &(ptr[min].xvalue()),
00176     &(this->elem(min)), n);
00177 #else
00178   double_and_int* dest = &pos.va[min];
00179   double* source = &v[min];
00180   for (int i = min; i <= max; ++i)
00181   {
00182     dest->x += *source;
00183     ++source;
00184     ++dest;
00185   }
00186 #endif
00187 }
00188 
00193 void dvector::save_dvector_derivatives_na(const dvar_vector_position& pos) const
00194 {
00195   // puts the derivative values in a dvector into a dvar_vector's guts
00196   int min=indexmin();
00197   int max=indexmax();
00198   if (min!=pos.indexmin() || max!=pos.indexmax())
00199   {
00200     cerr << "Incompatible array sizes in " <<
00201     "void dvector::save_dvector_derivatives_na(const dvar_vector_position& pos)"
00202     << endl;
00203   }
00204   double_and_int * ptr=pos.va;
00205 
00206 #ifndef USE_ASSEMBLER
00207   for (int i=min;i<=max;i++)
00208   {
00209     ptr[i].xvalue()=(*this)(i);
00210   }
00211 #else
00212      int n=max-min+1;
00213      dw_block_move(&(ptr[min].xvalue()),&(this->elem(min)),n);
00214 #endif
00215 }
00216 
00221 void save_dmatrix_derivatives(const dvar_matrix_position& _pos, const double x,
00222   const int& i, int& j)
00223 {
00224   dvar_matrix_position& pos= (dvar_matrix_position&) _pos;
00225   if ( i<pos.rowmin()||i>pos.rowmax() ||
00226      j<(pos.colmin())(i) || j>(pos.colmax())(i) )
00227   {
00228     cerr << "Error -- Index out of bounds in routine\n"
00229     " void save_dmatrix_derivatives(const dvar_matrix_position& pos"
00230     "  , const double& x, const int& i, const int& j)" << endl;
00231     ad_exit(1);
00232   }
00233   // puts the derivative values in a dvector into a dvar_vector's guts
00234   pos(i)(j)+=x;
00235 }
00236 
00241 void dmatrix::save_dmatrix_derivatives(const dvar_matrix_position& _pos) const
00242 {
00243   dvar_matrix_position& pos=(dvar_matrix_position&) _pos;
00244   // puts the derivative values in a dvector into a dvar_vector's guts
00245   int min=rowmin();
00246   int max=rowmax();
00247   if (min!=pos.row_min||max!=pos.row_max)
00248   {
00249     cerr << "Incompatible array sizes in " <<
00250     "void dmatrix::save_dmatrix__derivatives(const dvar_matrix_position& pos)"
00251     << endl;
00252   }
00253   for (int i=min;i<=max;i++)
00254   {
00255     if (allocated((*this)(i)))
00256       (*this)(i).save_dvector_derivatives(pos(i));
00257   }
00258 }
00259 
00264 void dmatrix::save_dmatrix_derivatives_na(const dvar_matrix_position& _pos)
00265   const
00266 {
00267   dvar_matrix_position& pos=(dvar_matrix_position&) _pos;
00268   // puts the derivative values in a dvector into a dvar_vector's guts
00269   int min=rowmin();
00270   int max=rowmax();
00271   if (min!=pos.row_min||max!=pos.row_max)
00272   {
00273     cerr << "Incompatible array sizes in " <<
00274     "void dmatrix::save_dmatrix__derivatives(const dvar_matrix_position& pos)"
00275     << endl;
00276   }
00277   for (int i=min;i<=max;i++)
00278   {
00279     if (allocated((*this)(i)))
00280       (*this)(i).save_dvector_derivatives_na(pos(i));
00281   }
00282 }
00283 
00288 void dvar_matrix::save_dvar_matrix_position(void) const
00289 {
00290   // saves the size and address information for a dvar_vector
00291   dvar_matrix_position tmp(*this,1);
00292   size_t wsize=sizeof(int);
00293   size_t wsize1=sizeof(void*);
00294 
00295   int min=rowmin();
00296   int max=rowmax();
00297   for (int i=min;i<=max;i++)
00298   {
00299     gradient_structure::get_fp()->fwrite(&(tmp.lb(i)),wsize);
00300     gradient_structure::get_fp()->fwrite(&(tmp.ub(i)),wsize);
00301     gradient_structure::get_fp()->fwrite(&(tmp.ptr(i)),wsize1);
00302    }
00303   gradient_structure::get_fp()->fwrite(&(tmp.row_min),wsize);
00304   gradient_structure::get_fp()->fwrite(&(tmp.row_max),wsize);
00305 }