ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
cmpdif11.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 
00013 #ifdef __TURBOC__
00014   #pragma hdrstop
00015   #include <iostream.h>
00016 #endif
00017 
00018 #ifdef __ZTC__
00019   #include <iostream.hpp>
00020 #endif
00021 
00022 #include <string.h>
00023 
00028 void banded_symmetric_dvar_matrix::save_dvar_matrix_value(void) const
00029 {
00030   // saves the size, address, and value information for a dvar_matrix
00031   int min=d.rowmin();
00032   int max=d.rowmax();
00033   for (int i=min;i<=max;i++)
00034   {
00035     d(i).save_dvar_vector_value();
00036     d(i).save_dvar_vector_position();
00037   }
00038 }
00039 
00044 void banded_lower_triangular_dvar_matrix::save_dvar_matrix_value(void) const
00045 {
00046   // saves the size, address, and value information for a dvar_matrix
00047   int min=d.rowmin();
00048   int max=d.rowmax();
00049   for (int i=min;i<=max;i++)
00050   {
00051     d(i).save_dvar_vector_value();
00052     d(i).save_dvar_vector_position();
00053   }
00054 }
00055 
00060 void banded_symmetric_dmatrix::save_dmatrix_value(void) const
00061 {
00062   // saves the size, address, and value information for a dvar_matrix
00063   int min=d.rowmin();
00064   int max=d.rowmax();
00065   for (int i=min;i<=max;i++)
00066   {
00067     d(i).save_dvector_value();
00068     d(i).save_dvector_position();
00069   }
00070 }
00071 
00076 banded_symmetric_dmatrix restore_banded_symmetric_dvar_matrix_value(
00077   const dvar_matrix_position& mpos)
00078 {
00079   // restores the size, address, and value information for a dvar_matrix
00080   banded_symmetric_dmatrix out((const dvar_matrix_position&)mpos);
00081   //int ierr;
00082   int min=out.rowmin();
00083   int max=out.rowmax();
00084   for (int i=max;i>=min;i--)
00085   {
00086     dvar_vector_position vpos=restore_dvar_vector_position();
00087     out.d(i)=restore_dvar_vector_value(vpos);
00088   }
00089   return out;
00090 }
00091 
00096 banded_lower_triangular_dmatrix
00097 restore_banded_lower_triangular_dvar_matrix_value(
00098   const dvar_matrix_position& mpos)
00099 {
00100   // restores the size, address, and value information for a dvar_matrix
00101   banded_lower_triangular_dmatrix out((const dvar_matrix_position&)mpos);
00102   //int ierr;
00103   int min=out.rowmin();
00104   int max=out.rowmax();
00105   for (int i=max;i>=min;i--)
00106   {
00107     dvar_vector_position vpos=restore_dvar_vector_position();
00108     out.d(i)=restore_dvar_vector_value(vpos);
00109   }
00110   return out;
00111 }
00112 
00117 void banded_symmetric_dvar_matrix::save_dvar_matrix_position(void) const
00118 {
00119   // saves the size and address information for a dvar_vector
00120   dvar_matrix_position tmp((*this).d,1);
00121   size_t wsize = sizeof(int);
00122   size_t wsize1 = sizeof(void*);
00123 
00124   int min=rowmin();
00125   int max=rowmax();
00126   for (int i=min;i<=max;i++)
00127   {
00128     gradient_structure::get_fp()->fwrite(&(tmp.lb(i)),wsize);
00129     gradient_structure::get_fp()->fwrite(&(tmp.ub(i)),wsize);
00130     gradient_structure::get_fp()->fwrite(&(tmp.ptr(i)),wsize1);
00131    }
00132   gradient_structure::get_fp()->fwrite(&(tmp.row_min),wsize);
00133   gradient_structure::get_fp()->fwrite(&(tmp.row_max),wsize);
00134 }
00135 
00140 void banded_lower_triangular_dvar_matrix::save_dvar_matrix_position(void) const
00141 {
00142   // saves the size and address information for a dvar_vector
00143   dvar_matrix_position tmp((*this).d,1);
00144   size_t wsize=sizeof(int);
00145   size_t wsize1=sizeof(void*);
00146 
00147   int min=rowmin();
00148   int max=rowmax();
00149   for (int i=min;i<=max;i++)
00150   {
00151     gradient_structure::get_fp()->fwrite(&(tmp.lb(i)),wsize);
00152     gradient_structure::get_fp()->fwrite(&(tmp.ub(i)),wsize);
00153     gradient_structure::get_fp()->fwrite(&(tmp.ptr(i)),wsize1);
00154    }
00155   gradient_structure::get_fp()->fwrite(&(tmp.row_min),wsize);
00156   gradient_structure::get_fp()->fwrite(&(tmp.row_max),wsize);
00157 }
00158 
00163 void banded_symmetric_dmatrix::save_dmatrix_derivatives(
00164   const dvar_matrix_position& pos) const
00165 {
00166   // puts the derivative values in a dvector into a dvar_vector's guts
00167   int min=rowmin();
00168   int max=rowmax();
00169   if (min!=pos.row_min||max!=pos.row_max)
00170   {
00171     cerr << "Incompatible array sizes in " <<
00172      "void dmatrix::save_dmatrix__derivatives(const dvar_matrix_position& pos)"
00173      << endl;
00174   }
00175   for (int i=min;i<=max;i++)
00176   {
00177     const dvector& xx=(*this)(i);
00178     dvector& x=(dvector&) xx;
00179     dvar_matrix_position& pos1=(dvar_matrix_position&)pos;
00180     //((const dvector&)x).save_dvector_derivatives(pos1(i));
00181     x.save_dvector_derivatives(pos1(i));
00182   }
00183 }
00184 
00189 void banded_lower_triangular_dmatrix::save_dmatrix_derivatives(
00190   const dvar_matrix_position& pos) const
00191 {
00192   // puts the derivative values in a dvector into a dvar_vector's guts
00193   int min=rowmin();
00194   int max=rowmax();
00195   if (min!=pos.row_min||max!=pos.row_max)
00196   {
00197     cerr << "Incompatible array sizes in " <<
00198     "void dmatrix::save_dmatrix__derivatives(const dvar_matrix_position& pos)"
00199     << endl;
00200   }
00201   for (int i=min;i<=max;i++)
00202   {
00203     dvector& x=(dvector&)d(i);
00204     dvar_matrix_position& pos1= (dvar_matrix_position&) pos;
00205     x.save_dvector_derivatives(pos1(i));
00206   }
00207 }
00208 
00213 banded_lower_triangular_dmatrix
00214   restore_banded_lower_triangular_dvar_matrix_derivatives
00215   (const dvar_matrix_position& _pos)
00216 {
00217   dvar_matrix_position& pos= (dvar_matrix_position&) _pos;
00218   banded_lower_triangular_dmatrix tmp(pos);
00219   for (int i=pos.row_max;i>=pos.row_min;i--)
00220   {
00221     tmp(i)=restore_dvar_vector_derivatives(pos(i));
00222   }
00223   return tmp;
00224 }
00225 
00230 banded_symmetric_dmatrix restore_bounded_symmetric_dvar_matrix_derivatives
00231   (const dvar_matrix_position& _pos)
00232 {
00233   dvar_matrix_position& pos= (dvar_matrix_position&) _pos;
00234   banded_symmetric_dmatrix tmp(pos);
00235   for (int i=pos.row_max;i>=pos.row_min;i--)
00236   {
00237     tmp(i)=restore_dvar_vector_derivatives(pos(i));
00238   }
00239   return tmp;
00240 }