ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
cmpdif4.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 dvector::save_dvector_position(void) const
00029 {
00030   // saves the size and address information for a dvar_vector
00031   size_t wsize=sizeof(dvector_position);
00032   dvector_position tmp(*this);
00033   //int num_rec;
00034   gradient_structure::get_fp()->fwrite(&tmp,wsize);
00035 }
00036 
00041 ivector_position restore_ivector_position(void)
00042 {
00043   // reads back the size and address information for a ivector
00044   // Back up the stream and read the number of bytes written in the
00045   // ``write function'' corresponding to this ``read function''
00046   ivector_position tmp;
00047   //int ierr;
00048   gradient_structure::get_fp()->fread(&tmp,sizeof(ivector_position));
00049   return tmp;
00050 }
00051 
00056 dvar_vector_position restore_dvar_vector_position(void)
00057 {
00058   // reads back the size and address information for a dvar_vector
00059   // Back up the stream and read the number of bytes written in the
00060   // ``write function'' corresponding to this ``read function''
00061   dvar_vector_position tmp;
00062   gradient_structure::get_fp()->fread(&tmp,sizeof(dvar_vector_position));
00063   return tmp;
00064 }
00065 
00070 dvector_position restore_dvector_position(void)
00071 {
00072   // reads back the size and address information for a dvar_vector
00073   // Back up the stream and read the number of bytes written in the
00074   // ``write function'' corresponding to this ``read function''
00075   dvector_position tmp;
00076   gradient_structure::get_fp()->fread(&tmp,sizeof(dvector_position));
00077   return tmp;
00078 }
00079 
00083 void dvar_vector::save_dvar_vector_value(void) const
00084 {
00085   //int ierr=save_dvar_vector_position();
00086   //const unsigned wsize=sizeof(double);
00087   //int num_rec;
00088   int min=indexmin();
00089   int max=indexmax();
00090   for (int i=min;i<=max;i++)
00091   {
00092     //double tmp=value((*this)(i));
00093     //gradient_structure::get_fp()->fwrite(&tmp,wsize);
00094     gradient_structure::get_fp()->fwrite(this->elem_value(i));
00095   }
00096 }
00097 
00101 void dvector::save_dvector_value(void) const
00102 {
00103   // int ierr=save_dvector_position();
00104   //int wsize=sizeof(double);
00105   //int num_rec;
00106   int min=indexmin();
00107   int max=indexmax();
00108   for (int i=min;i<=max;i++)
00109   {
00110     double tmp=(*this)(i);
00111     gradient_structure::get_fp()->fwrite(tmp);
00112   }
00113 }
00114 
00118 void ivector::save_ivector_value(void) const
00119 {
00120   // int ierr=save_ivector_position();
00121   size_t wsize=sizeof(int);
00122   int min=indexmin();
00123   int max=indexmax();
00124   for (int i=min;i<=max;i++)
00125   {
00126     int tmp=(*this)(i);
00127     gradient_structure::get_fp()->fwrite(&tmp,size_t(wsize));
00128   }
00129 }
00130 
00139 dvector restore_dvector_value(const dvector_position& tmp)
00140 {
00141   // restores the size, address, and value information for a dvar_vector
00142   dvector temp_vec(tmp.indexmin(),tmp.indexmax());
00143   for (int i=tmp.indexmax();i>=tmp.indexmin();i--)
00144   {
00145     double ttmp = 0.0;
00146     gradient_structure::get_fp()->fread(ttmp);
00147     temp_vec(i)=ttmp;
00148   }
00149   return temp_vec;
00150 }
00151 
00156 ivector restore_ivector_value(const ivector_position& tmp)
00157 {
00158   // restores the size, address, and value information for a ivector
00159   // Back up the stream and read the number of bytes written in the
00160   // ``write function'' corresponding to this ``read function''
00161   ivector temp_vec(tmp.indexmin(),tmp.indexmax());
00162   for (int i=tmp.indexmax();i>=tmp.indexmin();i--)
00163   {
00164     int n = 0;
00165     gradient_structure::get_fp()->fread(&n, sizeof(int));
00166     temp_vec(i) = n;
00167   }
00168   return temp_vec;
00169   // Back up the stream again for the next function
00170 }
00171 
00178 dvector restore_dvar_vector_value(const dvar_vector_position& tmp)
00179 {
00180   dvector temp_vec(tmp.indexmin(),tmp.indexmax());
00181   for (int i=tmp.indexmax();i>=tmp.indexmin();i--)
00182   {
00183     double ttmp = 0.0;
00184     //gradient_structure::get_fp()->fread(&ttmp,sizeof(double));
00185     gradient_structure::get_fp()->fread(ttmp);
00186     temp_vec(i)=ttmp;
00187   }
00188   return temp_vec;
00189 }
00193 void dvar_matrix::save_dvar_matrix_value(void) const
00194 {
00195   int min=rowmin();
00196   int max=rowmax();
00197   for (int i=min;i<=max;i++)
00198   {
00199     ((*this)(i).save_dvar_vector_value());
00200     ((*this)(i).save_dvar_vector_position());
00201   }
00202 }