ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
cmpdif6.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 dmatrix::save_dmatrix_position(void) const
00029 {
00030   // saves the size and address information for a dvar_vector
00031   dmatrix_position tmp(*this);
00032   size_t wsize=sizeof(int);
00033   size_t wsize1=sizeof(void*);
00034 
00035   int min=rowmin();
00036   int max=rowmax();
00037   for (int i=min;i<=max;i++)
00038   {
00039     gradient_structure::get_fp()->fwrite(&(tmp.lb(i)),wsize);
00040     gradient_structure::get_fp()->fwrite(&(tmp.ub(i)),wsize);
00041     gradient_structure::get_fp()->fwrite(&(tmp.ptr(i)),wsize1);
00042    }
00043   gradient_structure::get_fp()->fwrite(&(tmp.row_min),wsize);
00044   gradient_structure::get_fp()->fwrite(&(tmp.row_max),wsize);
00045 }
00046 
00051 void d3_array::save_d3_array_position() const
00052 {
00053   // saves the size and address information for a dvar_vector
00054   int mmin=indexmin();
00055   int mmax=indexmax();
00056   size_t wsize = sizeof(int);
00057 /*
00058   dmatrix_position tmp(*this);
00059   const int wsize=sizeof(int);
00060   const int wsize1=sizeof(void*);
00061 
00062   for (int i=min;i<=max;i++)
00063   {
00064     gradient_structure::get_fp()->fwrite(&(tmp.lb(i)),wsize);
00065     gradient_structure::get_fp()->fwrite(&(tmp.ub(i)),wsize);
00066     gradient_structure::get_fp()->fwrite(&(tmp.ptr(i)),wsize1);
00067    }
00068 */
00069   gradient_structure::get_fp()->fwrite(&(mmin),wsize);
00070   gradient_structure::get_fp()->fwrite(&(mmax),wsize);
00071 }
00072 
00077 d3_array_position restore_d3_array_position(void)
00078 {
00079   // saves the size and address information for a dvar_vector
00080   int mmin;
00081   int mmax;
00082   gradient_structure::get_fp()->fread(&mmax,sizeof(int));
00083   gradient_structure::get_fp()->fread(&mmin,sizeof(int));
00084   d3_array_position tmp(mmin,mmax);
00085   return tmp;
00086 }
00087 
00092 dvar_matrix_position restore_dvar_matrix_position(void)
00093 {
00094   // reads back the size and address information for a dvar_matrix
00095   // restores the size, address, and value information for a dvar_vector
00096   int min;
00097   int max;
00098   gradient_structure::get_fp()->fread(&max,sizeof(int));
00099   gradient_structure::get_fp()->fread(&min,sizeof(int));
00100   dvar_matrix_position tmp(min,max);
00101   // cout << "tmp.ptr= " << tmp.ptr ;
00102   for (int i=max;i>=min;i--)
00103   {
00104     gradient_structure::get_fp()->fread(&(tmp.ptr(i)),sizeof(void*));
00105     gradient_structure::get_fp()->fread(&(tmp.ub(i)),sizeof(int));
00106     gradient_structure::get_fp()->fread(&(tmp.lb(i)),sizeof(int));
00107   }
00108   return tmp;
00109 }
00110 
00115 dmatrix_position restore_dmatrix_position(void)
00116 {
00117   // reads back the size and address information for a dvar_matrix
00118   // restores the size, address, and value information for a dvar_vector
00119   int min;
00120   int max;
00121   gradient_structure::get_fp()->fread(&max,sizeof(int));
00122   gradient_structure::get_fp()->fread(&min,sizeof(int));
00123   dmatrix_position tmp(min,max);
00124   // cout << "tmp.ptr= " << tmp.ptr ;
00125   for (int i=max;i>=min;i--)
00126   {
00127     gradient_structure::get_fp()->fread(&(tmp.ptr(i)),sizeof(void*));
00128     gradient_structure::get_fp()->fread(&(tmp.ub(i)),sizeof(int));
00129     gradient_structure::get_fp()->fread(&(tmp.lb(i)),sizeof(int));
00130   }
00131   return tmp;
00132 }
00133 
00138 dmatrix restore_dvar_matrix_derivatives(const dvar_matrix_position& _pos)
00139 {
00140   dvar_matrix_position& pos= (dvar_matrix_position&) _pos;
00141   dmatrix tmp(pos);
00142   for (int i=pos.row_max;i>=pos.row_min;i--)
00143   {
00144     tmp(i)=restore_dvar_vector_derivatives(pos(i));
00145   }
00146   return tmp;
00147 }
00148 
00153 dmatrix restore_dvar_matrix_der_nozero(const dvar_matrix_position& _pos)
00154 {
00155   dvar_matrix_position& pos= (dvar_matrix_position&) _pos;
00156   dmatrix tmp(pos);
00157   for (int i=pos.row_max;i>=pos.row_min;i--)
00158   {
00159     tmp(i)=restore_dvar_vector_der_nozero(pos(i));
00160   }
00161   return tmp;
00162 }
00163 
00168 dvector restore_dvar_matrix_derivative_row(const dvar_matrix_position& _pos,
00169   const int& ii)
00170 {
00171   dvar_matrix_position& pos= (dvar_matrix_position&) _pos;
00172   dvector tmp=restore_dvar_vector_derivatives(pos(ii));
00173   return tmp;
00174 }
00175 
00180 dvector restore_dvar_matrix_derivative_column(const dvar_matrix_position& _pos,
00181   const int& ii)
00182 {
00183   dvar_matrix_position& pos= (dvar_matrix_position&) _pos;
00184   dvector tmpvec(pos.rowmin(),pos.rowmax());
00185   int min=tmpvec.indexmin();
00186   int max=tmpvec.indexmax();
00187   for (int i=min;i<=max;i++)
00188   {
00189     tmpvec(i)=((pos(i)).va)[ii].xvalue();
00190     ((pos(i)).va)[ii].xvalue()=0.;
00191   }
00192   return tmpvec;
00193 }
00194 
00199 dvar_vector nograd_assign(dvector tmp)
00200 {
00201   kkludge_object kg;
00202   int min=tmp.indexmin();
00203   int max=tmp.indexmax();
00204   dvar_vector out(min,max,kg); // dvar_vector constructor that
00205                                // doesn't do any initialization
00206   for (int i=min;i<=max;i++)
00207   {
00208     value(out(i))=tmp(i);
00209   }
00210   return out;
00211 }
00212 
00217 dvar_matrix nograd_assign(const dmatrix& m)
00218 {
00219   // cout << "Entering nograd assign"<<endl;
00220   //kkludge_object kg;
00221   int nrl=m.rowmin();
00222   int nrh=m.rowmax();
00223   ivector ncl(nrl,nrh);
00224   ivector nch(nrl,nrh);
00225   int i;
00226   for (i=nrl;i<=nrh;i++)
00227   {
00228     ncl(i)=m(i).indexmin();
00229     nch(i)=m(i).indexmax();
00230   }
00231   dvar_matrix out(nrl,nrh,ncl,nch); // dvar_matrix constructor that
00232                                        // doesn't do any initialization
00233   for (i=nrl;i<=nrh;i++)
00234   {
00235     for (int j=ncl(i);j<=nch(i);j++)
00236     {
00237       value(out(i,j))=m(i,j);
00238     }
00239     // out(i)=nograd_assign(m(i));
00240   }
00241   // cout << "Leaving nograd assign"<<endl;
00242   return out;
00243 }
00244 
00249 dvar_matrix nograd_assign_trans(const dmatrix& m)
00250 {
00251   // cout << "Entering nograd assign"<<endl;
00252   //kkludge_object kg;
00253   int nrl=m.rowmin();
00254   int nrh=m.rowmax();
00255   ivector ncl(nrl,nrh);
00256   ivector nch(nrl,nrh);
00257   int i;
00258   for (i=nrl;i<=nrh;i++)
00259   {
00260     ncl(i)=m(i).indexmin();
00261     nch(i)=m(i).indexmax();
00262   }
00263   dvar_matrix out(nrl,nrh,ncl,nch); // dvar_matrix constructor that
00264                                        // doesn't do any initialization
00265   for (i=nrl;i<=nrh;i++)
00266   {
00267     for (int j=ncl(i);j<=nch(i);j++)
00268     {
00269       value(out(j,i))=m(i,j);
00270     }
00271     // out(i)=nograd_assign(m(i));
00272   }
00273   // cout << "Leaving nograd assign"<<endl;
00274   return out;
00275 }
00276 
00281 void nograd_assign_column(const dvar_matrix& m, const dvector& v, const int& ii)
00282 {
00283   // cout << "Entering nograd assign"<<endl;
00284   //kkludge_object kg;
00285   if (ii<m.colmin()||ii>m.colmax()  ||
00286    (v.indexmin()!=m.rowmin()) ||
00287    (v.indexmax()!=m.rowmax()) )
00288   {
00289     cerr << "Error -- Index out of bounds in\n"
00290       "void nograd_assign(const dvar_matrix& m,const dvector& v, const int& ii)"
00291       << endl;
00292     ad_exit(1);
00293   }
00294   int min=v.indexmin();
00295   int max=v.indexmax();
00296   for (int j=min;j<=max;j++)
00297   {
00298     value(m(j,ii))=v(j);
00299   }
00300     // out(i)=nograd_assign(m(i));
00301 }
00302 
00307   d3_array_position::d3_array_position(int mmin,int mmax) :
00308     min(mmin), max(mmax)
00309   {}