ADMB Documentation  11.1.2192
 All Classes Files Functions Variables Typedefs Friends Defines
cmpdif11.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: cmpdif11.cpp 1863 2014-04-08 00:13:37Z johnoel $
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   const int wsize=sizeof(int);
00122   const int 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   const int wsize=sizeof(int);
00145   const int 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 #if defined(_MSC_VER)
00204     dvector& x=(dvector&)(*this)(i);
00205 #else
00206     const dvector& cx=(const dvector&)(*this)(i);
00207     dvector& x=(dvector&)(cx);
00208     //dvector& x=(dvector&)(*this)(i);
00209 #endif
00210     dvar_matrix_position& pos1= (dvar_matrix_position&) pos;
00211     x.save_dvector_derivatives(pos1(i));
00212   }
00213 }
00214 
00219 banded_lower_triangular_dmatrix
00220   restore_banded_lower_triangular_dvar_matrix_derivatives
00221   (const dvar_matrix_position& _pos)
00222 {
00223   dvar_matrix_position& pos= (dvar_matrix_position&) _pos;
00224   banded_lower_triangular_dmatrix tmp(pos);
00225   for (int i=pos.row_max;i>=pos.row_min;i--)
00226   {
00227     tmp(i)=restore_dvar_vector_derivatives(pos(i));
00228   }
00229   return tmp;
00230 }
00231 
00236 banded_symmetric_dmatrix restore_bounded_symmetric_dvar_matrix_derivatives
00237   (const dvar_matrix_position& _pos)
00238 {
00239   dvar_matrix_position& pos= (dvar_matrix_position&) _pos;
00240   banded_symmetric_dmatrix tmp(pos);
00241   for (int i=pos.row_max;i>=pos.row_min;i--)
00242   {
00243     tmp(i)=restore_dvar_vector_derivatives(pos(i));
00244   }
00245   return tmp;
00246 }