ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
fvar_a46.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 // file fvar.cpp
00012 // constructors, destructors and misc functions involving class prevariable
00013 
00014 #include "fvar.hpp"
00015 
00016 #ifdef __TURBOC__
00017   #pragma hdrstop
00018   #include <iostream.h>
00019 #endif
00020 
00021 #ifdef __ZTC__
00022   #include <iostream.hpp>
00023 #endif
00024 
00025 
00026 void dvdv_elem_div(void);
00027 
00032 dvar_vector elem_div(const dvar_vector& v1, const dvar_vector& v2)
00033 {
00034   RETURN_ARRAYS_INCREMENT();
00035   if (v1.indexmin()!=v2.indexmin()||v1.indexmax()!=v2.indexmax())
00036   {
00037     cerr << "Incompatible bounds in "
00038     "dvar_vector elem_prod(const dvar_vector& v1, const dvar_vector& v2)"
00039     << endl;
00040     ad_exit(1);
00041   }
00042   dvar_vector tmp(v1.indexmin(),v1.indexmax());
00043   dvector tmp_inv(v1.indexmin(),v1.indexmax());
00044 
00045   for (int i=v1.indexmin();i<=v1.indexmax();i++)
00046   {
00047     tmp_inv.elem(i)=1./v2.elem_value(i);
00048     tmp.elem_value(i)=v1.elem_value(i)*tmp_inv.elem(i);
00049   }
00050 
00051   // The derivative list considerations
00052   save_identifier_string("bbbb");
00053   v1.save_dvar_vector_value();
00054   v1.save_dvar_vector_position();
00055   save_identifier_string("wwww");
00056   tmp_inv.save_dvector_value();
00057   tmp_inv.save_dvector_position();
00058   save_identifier_string("vvvv");
00059   v2.save_dvar_vector_position();
00060   save_identifier_string("uuuu");
00061   tmp.save_dvar_vector_position();
00062   save_identifier_string("aaaa");
00063   gradient_structure::GRAD_STACK1->set_gradient_stack(dvdv_elem_div);
00064   RETURN_ARRAYS_DECREMENT();
00065   return tmp;
00066 }
00067 
00072 void dvdv_elem_div(void)
00073 {
00074   // int ierr=fsetpos(gradient_structure::get_fp(),&filepos);
00075   verify_identifier_string("aaaa");
00076   dvar_vector_position tmppos=restore_dvar_vector_position();
00077   dvector dftmp=restore_dvar_vector_derivatives(tmppos);
00078   verify_identifier_string("uuuu");
00079   dvar_vector_position v2pos=restore_dvar_vector_position();
00080   verify_identifier_string("vvvv");
00081   dvector_position tmp_divpos=restore_dvector_position();
00082   dvector tmp_div=restore_dvector_value(tmp_divpos);
00083   verify_identifier_string("wwww");
00084   dvar_vector_position v1pos=restore_dvar_vector_position();
00085   dvector cv1=restore_dvar_vector_value(v1pos);
00086   verify_identifier_string("bbbb");
00087   dvector dfv1(cv1.indexmin(),cv1.indexmax());
00088   dvector dfv2(tmp_div.indexmin(),tmp_div.indexmax());
00089   for (int i=cv1.indexmin();i<=cv1.indexmax();i++)
00090   {
00091     //tmp+=cv1(i)*cv2(i);
00092     double& xx=tmp_div.elem(i);
00093     dfv1(i)=dftmp.elem(i)*xx;
00094     dfv2(i)=-dftmp.elem(i)*cv1.elem(i)*xx*xx;
00095   }
00096   dfv1.save_dvector_derivatives(v1pos);
00097   dfv2.save_dvector_derivatives(v2pos);
00098   //ierr=fsetpos(gradient_structure::get_fp(),&filepos);
00099 }
00100 
00101 void dvcv_elem_div(void);
00102 
00107 dvar_vector elem_div(const dvar_vector& v1, const dvector& v2)
00108 {
00109   RETURN_ARRAYS_INCREMENT();
00110   if (v1.indexmin()!=v2.indexmin()||v1.indexmax()!=v2.indexmax())
00111   {
00112     cerr << "Incompatible bounds in "
00113     "dvar_vector elem_prod(const dvar_vector& v1, const dvar_vector& v2)"
00114     << endl;
00115     ad_exit(1);
00116   }
00117   dvar_vector tmp(v1.indexmin(),v1.indexmax());
00118   dvector tmp_inv(v1.indexmin(),v1.indexmax());
00119 
00120   for (int i=v1.indexmin();i<=v1.indexmax();i++)
00121   {
00122     tmp_inv.elem(i)=1./v2.elem(i);
00123     tmp.elem_value(i)=v1.elem_value(i)*tmp_inv.elem(i);
00124   }
00125 
00126   // The derivative list considerations
00127   save_identifier_string("bbbb");
00128   v1.save_dvar_vector_value();
00129   v1.save_dvar_vector_position();
00130   save_identifier_string("wwww");
00131   tmp_inv.save_dvector_value();
00132   tmp_inv.save_dvector_position();
00133   save_identifier_string("vvvv");
00134   tmp.save_dvar_vector_position();
00135   save_identifier_string("aaaa");
00136   gradient_structure::GRAD_STACK1->set_gradient_stack(dvcv_elem_div);
00137   RETURN_ARRAYS_DECREMENT();
00138   return tmp;
00139 }
00140 
00145 void dvcv_elem_div(void)
00146 {
00147   // int ierr=fsetpos(gradient_structure::get_fp(),&filepos);
00148   verify_identifier_string("aaaa");
00149   dvar_vector_position tmppos=restore_dvar_vector_position();
00150   dvector dftmp=restore_dvar_vector_derivatives(tmppos);
00151   verify_identifier_string("vvvv");
00152   dvector_position tmp_divpos=restore_dvector_position();
00153   dvector tmp_div=restore_dvector_value(tmp_divpos);
00154   verify_identifier_string("wwww");
00155   dvar_vector_position v1pos=restore_dvar_vector_position();
00156   dvector cv1=restore_dvar_vector_value(v1pos);
00157   verify_identifier_string("bbbb");
00158   dvector dfv1(cv1.indexmin(),cv1.indexmax());
00159   for (int i=cv1.indexmin();i<=cv1.indexmax();i++)
00160   {
00161     dfv1(i)=dftmp.elem(i)*tmp_div.elem(i);
00162   }
00163   dfv1.save_dvector_derivatives(v1pos);
00164   //ierr=fsetpos(gradient_structure::get_fp(),&filepos);
00165 }
00166 
00167 void cvdv_elem_div(void);
00168 
00173 dvar_vector elem_div(const dvector& v1, const dvar_vector& v2)
00174 {
00175   RETURN_ARRAYS_INCREMENT();
00176   if (v1.indexmin()!=v2.indexmin()||v1.indexmax()!=v2.indexmax())
00177   {
00178     cerr << "Incompatible bounds in "
00179     "dvar_vector elem_prod(const dvar_vector& v1, const dvar_vector& v2)"
00180     << endl;
00181     ad_exit(1);
00182   }
00183   dvar_vector tmp(v1.indexmin(),v1.indexmax());
00184   dvector tmp_inv(v1.indexmin(),v1.indexmax());
00185 
00186   for (int i=v1.indexmin();i<=v1.indexmax();i++)
00187   {
00188     double x=1./v2.elem_value(i);
00189     tmp.elem_value(i)=v1.elem(i)*x;
00190     tmp_inv.elem(i)=-tmp.elem_value(i)*x;
00191   }
00192 
00193   // The derivative list considerations
00194   save_identifier_string("bbbb");
00195   tmp_inv.save_dvector_value();
00196   tmp_inv.save_dvector_position();
00197   save_identifier_string("vvvv");
00198   v2.save_dvar_vector_position();
00199   save_identifier_string("uuuu");
00200   tmp.save_dvar_vector_position();
00201   save_identifier_string("aaaa");
00202   gradient_structure::GRAD_STACK1->set_gradient_stack(cvdv_elem_div);
00203   RETURN_ARRAYS_DECREMENT();
00204   return tmp;
00205 }
00206 
00211 void cvdv_elem_div(void)
00212 {
00213   // int ierr=fsetpos(gradient_structure::get_fp(),&filepos);
00214   verify_identifier_string("aaaa");
00215   dvar_vector_position tmppos=restore_dvar_vector_position();
00216   dvector dftmp=restore_dvar_vector_derivatives(tmppos);
00217   verify_identifier_string("uuuu");
00218   dvar_vector_position v2pos=restore_dvar_vector_position();
00219   verify_identifier_string("vvvv");
00220   dvector_position tmp_divpos=restore_dvector_position();
00221   dvector tmp_div=restore_dvector_value(tmp_divpos);
00222   verify_identifier_string("bbbb");
00223   dvector dfv2(tmp_div.indexmin(),tmp_div.indexmax());
00224   for (int i=dfv2.indexmin();i<=dfv2.indexmax();i++)
00225   {
00226     //tmp+=cv1(i)*cv2(i);
00227     dfv2(i)=dftmp.elem(i)*tmp_div.elem(i);
00228   }
00229   dfv2.save_dvector_derivatives(v2pos);
00230   //ierr=fsetpos(gradient_structure::get_fp(),&filepos);
00231 }
00232