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