ADMB Documentation  11.1.1015
 All Classes Files Functions Variables Typedefs Friends Defines
fvar_m14.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: fvar_m14.cpp 542 2012-07-10 21:04:06Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California 
00006  */
00011 // constructors, destructors and misc functions involving class prevariable 
00012 
00013 #include "fvar.hpp"
00014 
00015 #ifdef __TURBOC__
00016   #pragma hdrstop
00017   #include <iostream.h>
00018 #endif
00019 
00020 #ifdef __ZTC__
00021   #include <iostream.hpp>
00022 #endif
00023 
00024 
00025 /*
00026  dvar_matrix  operator*(const dvar_matrix& m1, const dvar_matrix& m2 )
00027  {
00028    if (m1.colmin() != m2.rowmin() || m1.colmax() != m2.rowmax())
00029    {
00030      cerr << " Incompatible array bounds in dmatrix  operator * (const dmatrix& x, const dmatrix& m)\n";
00031      ad_exit(21);
00032    }
00033    //dmatrix cm1=value(m1);
00034    //dmatrix cm2=value(m2);
00035    dmatrix tmp(m1.rowmin(),m1.rowmax(), m2.colmin(), m2.colmax());
00036    double sum;
00037    double ** temp_col=(double **) malloc(m2.rowsize()*sizeof(double*));
00038    temp_col-=m2.rowmin();
00039 
00040 
00041    for (int j=m2.colmin(); j<=m2.colmax(); j++)
00042    {
00043 
00044      for (int k=m2.rowmin(); k<=m2.rowmax(); k++)
00045      {
00046        temp_col[k] = (double*) &m2.elem_value(k,j);
00047      }
00048 
00049      for (int i=m1.rowmin(); i<=m1.rowmax(); i++)
00050      {
00051        sum=0.0;
00052        const dvar_vector& temp_row = m1(i);
00053        for (int k=m1.colmin(); k<=m1.colmax(); k++)
00054        {
00055           sum+=temp_row.elem_value(k) * (*temp_col[k]);
00056          // sum+=temp_row(k) * cm2(k,j);
00057        }
00058        tmp(i,j)=sum;
00059      }
00060    }
00061 
00062 
00063    temp_col+=m2.rowmin();
00064    free ((char*)temp_col);
00065    dvar_matrix vtmp=nograd_assign(tmp);
00066    save_identifier_string("TEST1");
00067    m1.save_dvar_matrix_value();
00068    m1.save_dvar_matrix_position();
00069    m2.save_dvar_matrix_value();
00070    m2.save_dvar_matrix_position();
00071    vtmp.save_dvar_matrix_position();
00072    save_identifier_string("TEST6");
00073    gradient_structure::GRAD_STACK1->
00074             set_gradient_stack(dmdm_prod);
00075    return vtmp;
00076  }
00077 */
00078 
00083  dvar_matrix operator*(const dvar_matrix& m1, const dvar_matrix& m2)
00084  {
00085    if (m1.colmin() != m2.rowmin() || m1.colmax() != m2.rowmax())
00086    {
00087      cerr << " Incompatible array bounds in dmatrix operator*(const dmatrix& x, const dmatrix& m)\n";
00088      ad_exit(21);
00089    }
00090    //dmatrix cm1=value(m1);
00091    //dmatrix cm2=value(m2);
00092    dmatrix tmp(m1.rowmin(),m1.rowmax(), m2.colmin(), m2.colmax());
00093    double sum;
00094 
00095 
00096    for (int j=m2.colmin(); j<=m2.colmax(); j++)
00097    {
00098 
00099      dvector m2col=column_value(m2,j);
00100      
00101      for (int i=m1.rowmin(); i<=m1.rowmax(); i++)
00102      {
00103        sum=value(m1(i))*m2col;
00104        tmp(i,j)=sum;
00105      }
00106    }
00107 
00108    dvar_matrix vtmp=nograd_assign(tmp);
00109    save_identifier_string("TEST1");
00110    m1.save_dvar_matrix_value();
00111    m1.save_dvar_matrix_position();
00112    m2.save_dvar_matrix_value();
00113    m2.save_dvar_matrix_position();
00114    vtmp.save_dvar_matrix_position();
00115    save_identifier_string("TEST6");
00116    gradient_structure::GRAD_STACK1->
00117             set_gradient_stack(dmdm_prod);
00118    return vtmp;
00119  }
00120 
00125 void dmdm_prod(void)
00126 {
00127   verify_identifier_string("TEST6");
00128   dvar_matrix_position vpos=restore_dvar_matrix_position();
00129   dmatrix dftmp=restore_dvar_matrix_derivatives(vpos);
00130   dvar_matrix_position m2pos=restore_dvar_matrix_position();
00131   dmatrix cm2=restore_dvar_matrix_value(m2pos);
00132   dvar_matrix_position m1pos=restore_dvar_matrix_position();
00133   dmatrix cm1=restore_dvar_matrix_value(m1pos);
00134   verify_identifier_string("TEST1");
00135   dmatrix dfm1(m1pos);
00136   dmatrix dfm2(m2pos);
00137   double dfsum;
00138   dfm1.initialize();
00139   dfm2.initialize();
00140   for (int j=cm2.colmin(); j<=cm2.colmax(); j++)
00141   {
00142     for (int i=cm1.rowmin(); i<=cm1.rowmax(); i++)
00143     {
00144       //tmp.elem(i,j)=sum;
00145       dfsum=dftmp.elem(i,j);
00146       for (int k=cm1.colmin(); k<=cm1.colmax(); k++)
00147       {
00148         //sum+=cm1(i,k) * cm2(k,j);
00149         dfm1.elem(i,k)+=dfsum * cm2.elem(k,j);
00150         dfm2.elem(k,j)+=dfsum * cm1.elem(i,k);
00151       }
00152     }
00153   }
00154   dfm1.save_dmatrix_derivatives(m1pos);
00155   dfm2.save_dmatrix_derivatives(m2pos);
00156   // cout << "leaving dmdm_prod"<<endl;
00157 }