ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
fvar_m22.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 void dmdv_prod(void);
00014 void cmdv_prod(void);
00015 
00020 dvar_vector operator*(const dvar_matrix& m, const dvar_vector& x)
00021 {
00022   RETURN_ARRAYS_INCREMENT();
00023 
00024   if (x.indexmin() != m.colmin() || x.indexmax() != m.colmax())
00025   {
00026      cerr << " Incompatible array bounds in "
00027      "dvar_vector operator*(const dvar_matrix& m, const dvar_vector& x)\n";
00028      ad_exit(21);
00029   }
00030 
00031   kkludge_object kkk;
00032   dvar_vector tmp(m.rowmin(),m.rowmax(),kkk);
00033   for (int i=m.rowmin(); i<=m.rowmax(); i++)
00034   {
00035     double sum = 0.0;
00036     for (int j=x.indexmin(); j<=x.indexmax(); j++)
00037     {
00038        //sum+=m[i][j]*x[j];
00039        sum+=(m.elem(i)).elem_value(j)*x.elem_value(j);
00040     }
00041     tmp.elem_value(i)=sum;
00042   }
00043   save_identifier_string("PLACE4");
00044   x.save_dvar_vector_value();
00045   x.save_dvar_vector_position();
00046   save_identifier_string("PLACE3");
00047   m.save_dvar_matrix_value();
00048   m.save_dvar_matrix_position();
00049   save_identifier_string("PLACE2");
00050   tmp.save_dvar_vector_position();
00051   save_identifier_string("PLACE1");
00052   gradient_structure::GRAD_STACK1->
00053       set_gradient_stack(dmdv_prod);
00054   RETURN_ARRAYS_DECREMENT();
00055   return(tmp);
00056 }
00057 
00062 void dmdv_prod(void)
00063 {
00064   verify_identifier_string("PLACE1");
00065   dvar_vector_position tmp_pos=restore_dvar_vector_position();
00066   verify_identifier_string("PLACE2");
00067   dvar_matrix_position m_pos=restore_dvar_matrix_position();
00068   dmatrix m=restore_dvar_matrix_value(m_pos);
00069   verify_identifier_string("PLACE3");
00070   dvar_vector_position x_pos=restore_dvar_vector_position();
00071   dvector x=restore_dvar_vector_value(x_pos);
00072   verify_identifier_string("PLACE4");
00073   dvector dftmp=restore_dvar_vector_derivatives(tmp_pos);
00074 
00075   dmatrix dfm(m_pos);
00076   dvector dfx(x_pos.indexmin(),x_pos.indexmax());
00077   dfm.initialize();
00078   dfx.initialize();
00079 
00080   for (int i=m.rowmax(); i>=m.rowmin(); i--)
00081   {
00082     //tmp.elem_value(i)=sum;
00083     double dfsum=dftmp.elem(i);
00084     for (int j=x.indexmax(); j>=x.indexmin(); j--)
00085     {
00086       //sum+=(m.elem(i)).elem_value(j)*x.elem_value(j);
00087       dfm.elem(i,j)+=dfsum*x.elem(j);
00088       dfx.elem(j)+=dfsum*m.elem(i,j);
00089     }
00090     //sum=0.0;
00091     dfsum=0.0;
00092   }
00093   dfx.save_dvector_derivatives(x_pos);
00094   dfm.save_dmatrix_derivatives(m_pos);
00095 }
00096 
00101 dvar_vector operator*(const dmatrix& m, const dvar_vector& x)
00102 {
00103   RETURN_ARRAYS_INCREMENT();
00104 
00105   if (x.indexmin() != m.colmin() || x.indexmax() != m.colmax())
00106   {
00107     cerr << " Incompatible array bounds in "
00108     "dvar_vector operator*(const dvar_matrix& m, const dvar_vector& x)\n";
00109     ad_exit(21);
00110   }
00111 
00112   kkludge_object kkk;
00113   dvar_vector tmp(m.rowmin(),m.rowmax(),kkk);
00114   for (int i=m.rowmin(); i<=m.rowmax(); i++)
00115   {
00116     double sum=0.0;
00117     for (int j=x.indexmin(); j<=x.indexmax(); j++)
00118     {
00119       //sum+=m[i][j]*x[j];
00120       sum+=(m.elem(i)).elem(j)*x.elem_value(j);
00121     }
00122     tmp.elem_value(i)=sum;
00123   }
00124   save_identifier_string("PLACE4");
00125   x.save_dvar_vector_value();
00126   x.save_dvar_vector_position();
00127   m.save_dmatrix_value();
00128   m.save_dmatrix_position();
00129   save_identifier_string("PLACE2");
00130   tmp.save_dvar_vector_position();
00131   save_identifier_string("PLACE1");
00132   gradient_structure::GRAD_STACK1->
00133       set_gradient_stack(cmdv_prod);
00134   RETURN_ARRAYS_DECREMENT();
00135   return tmp;
00136 }
00137 
00142 void cmdv_prod(void)
00143 {
00144   verify_identifier_string("PLACE1");
00145   dvar_vector_position tmp_pos=restore_dvar_vector_position();
00146   verify_identifier_string("PLACE2");
00147   dmatrix_position m_pos=restore_dmatrix_position();
00148   dmatrix m=restore_dmatrix_value(m_pos);
00149   dvar_vector_position x_pos=restore_dvar_vector_position();
00150   dvector x=restore_dvar_vector_value(x_pos);
00151   verify_identifier_string("PLACE4");
00152   dvector dftmp=restore_dvar_vector_derivatives(tmp_pos);
00153 
00154   dvector dfx(x_pos.indexmin(),x_pos.indexmax());
00155   dfx.initialize();
00156   for (int i=m.rowmax(); i>=m.rowmin(); i--)
00157   {
00158     // tmp.elem_value(i)=sum;
00159     double dfsum=dftmp.elem(i);
00160     for (int j=x.indexmax(); j>=x.indexmin(); j--)
00161     {
00162       //sum+=(m.elem(i)).elem(j)*x.elem_value(j);
00163       dfx.elem(j)+=dfsum*m.elem(i,j);
00164     }
00165     //sum=0.0;
00166     dfsum=0.0;
00167   }
00168   dfx.save_dvector_derivatives(x_pos);
00169 }