/** * Description not yet available. * \param */ dvar_matrix operator*(const dvar_matrix& m1, const dvar_matrix& m2) { if (m1.colmin() != m2.rowmin() || m1.colmax() != m2.rowmax()) { cerr << " Incompatible array bounds in " "dmatrix operator*(const dmatrix& x, const dmatrix& m)\n"; ad_exit(21); } //dmatrix cm1=value(m1); //dmatrix cm2=value(m2); dmatrix tmp(m1.rowmin(),m1.rowmax(), m2.colmin(), m2.colmax()); double sum; for (int j=m2.colmin(); j<=m2.colmax(); j++) { dvector m2col=column_value(m2,j); for (int i=m1.rowmin(); i<=m1.rowmax(); i++) { sum=value(m1(i))*m2col; tmp(i,j)=sum; } } dvar_matrix vtmp=nograd_assign(tmp); save_identifier_string("TEST1"); m1.save_dvar_matrix_value(); m1.save_dvar_matrix_position(); m2.save_dvar_matrix_value(); m2.save_dvar_matrix_position(); vtmp.save_dvar_matrix_position(); save_identifier_string("TEST6"); gradient_structure::GRAD_STACK1-> set_gradient_stack(dmdm_prod); return vtmp; }
/** * Description not yet available. * \param */ dvar_vector operator*(const dvector& x, const dvar_matrix& m) { RETURN_ARRAYS_INCREMENT(); if (x.indexmin() != m.rowmin() || x.indexmax() != m.rowmax()) { cerr << " Incompatible array bounds in " "dvar_vector operator*(const dvector& x, const dvar_matrix& m)\n"; ad_exit(21); } dvar_vector tmp(m.colmin(),m.colmax()); dvariable sum; for (int j=m.colmin(); j<=m.colmax(); j++) { sum=0.0; for (int i=x.indexmin(); i<=x.indexmax(); i++) { sum+=x.elem(i)*m.elem(i,j); } tmp[j]=sum; } RETURN_ARRAYS_DECREMENT(); return(tmp); }
/** * Description not yet available. * \param */ dvar_matrix trans(const dvar_matrix& m1) { int rmin=m1.indexmin(); int rmax=m1.indexmax(); int cmin=m1.colmin(); int cmax=m1.colmax(); dvar_matrix t1(cmin,cmax,rmin,rmax); for (int i=rmin; i<=rmax; i++) { for (int j=cmin; j<=cmax; j++) { t1.elem_value(j,i)=m1.elem_value(i,j); } } save_identifier_string("uu"); m1.save_dvar_matrix_position(); t1.save_dvar_matrix_position(); save_identifier_string("vv"); gradient_structure::GRAD_STACK1-> set_gradient_stack(dfmattrans); return (t1); }
/** * Description not yet available. * \param */ dvar_vector operator*(const dvar_matrix& m, const dvector& x) { RETURN_ARRAYS_INCREMENT(); if (x.indexmin() != m.colmin() || x.indexmax() != m.colmax()) { cerr << " Incompatible array bounds in " "dvar_vector operator * (const dvar_matrix& m, const dvar_vector& x)\n"; ad_exit(21); } kkludge_object kkk; dvar_vector tmp(m.rowmin(),m.rowmax(),kkk); double sum; for (int i=m.rowmin(); i<=m.rowmax(); i++) { sum=0.0; const dvar_vector& tt=m.elem(i); for (int j=x.indexmin(); j<=x.indexmax(); j++) { //sum+=m[i][j]*x[j]; sum+=tt.elem_value(j)*x.elem(j); } tmp.elem_value(i)=sum; } save_identifier_string("PL4"); x.save_dvector_value(); x.save_dvector_position(); m.save_dvar_matrix_position(); tmp.save_dvar_vector_position(); save_identifier_string("PLX"); gradient_structure::GRAD_STACK1-> set_gradient_stack(dmcv_prod); RETURN_ARRAYS_DECREMENT(); return(tmp); }
/** LU decomposition back susbstitution alogrithm for variable object. \param a A dmatrix containing LU decomposition of input matrix. \f$a\f$. \param indx Permutation vector from ludcmp. \param b A dvector containing the RHS, \f$b\f$ of the linear equation \f$A\cdot X = B\f$, to be solved, and containing on return the solution vector \f$X\f$. \n\n The implementation of this algorithm was inspired by "Numerical Recipes in C", 2nd edition, Press, Teukolsky, Vetterling, Flannery, chapter 2 */ void lubksb(dvar_matrix a, const ivector& indx,dvar_vector b) { int i,ii=0,ip,j,iiflag=0; dvariable sum; int lb=a.colmin(); int ub=a.colmax(); for (i=lb;i<=ub;i++) { ip=indx(i); sum=b(ip); b(ip)=b(i); if (iiflag) { for (j=ii;j<=i-1;j++) { sum -= a.elem(i,j)*b.elem(j); } } else if (!ISZERO(value(sum))) { ii=i; iiflag=1; } b(i)=sum; } for (i=ub;i>=lb;i--) { sum=b(i); for (j=i+1;j<=ub;j++) { // !!! remove to show bug sum -= a.elem(i,j)*b.elem(j); } // !!! remove to show bug b.elem(i)=sum/a.elem(i,i); } }
/** * Description not yet available. * \param */ dvar_matrix operator*(const dvar_matrix& m1, const dmatrix& cm2) { if (m1.colmin() != cm2.rowmin() || m1.colmax() != cm2.rowmax()) { cerr << " Incompatible array bounds in " "dmatrix operator*(const dvar_matrix& x, const dmatrix& m)\n"; ad_exit(21); } dmatrix cm1=value(m1); //dmatrix cm2=value(m2); dmatrix tmp(m1.rowmin(),m1.rowmax(), cm2.colmin(), cm2.colmax()); #ifdef OPT_LIB const size_t rowsize = (size_t)cm2.rowsize(); #else const int _rowsize = cm2.rowsize(); assert(_rowsize > 0); const size_t rowsize = (size_t)_rowsize; #endif try { double* temp_col = new double[rowsize]; temp_col-=cm2.rowmin(); for (int j=cm2.colmin(); j<=cm2.colmax(); j++) { for (int k=cm2.rowmin(); k<=cm2.rowmax(); k++) { temp_col[k] = cm2.elem(k,j); } for (int i=cm1.rowmin(); i<=cm1.rowmax(); i++) { double sum=0.0; dvector& temp_row = cm1(i); for (int k=cm1.colmin(); k<=cm1.colmax(); k++) { sum+=temp_row(k) * (temp_col[k]); // sum+=temp_row(k) * cm2(k,j); } tmp(i,j)=sum; } } temp_col+=cm2.rowmin(); delete [] temp_col; temp_col = 0; } catch (std::bad_alloc& e) { cerr << "Error[" << __FILE__ << ':' << __LINE__ << "]: Unable to allocate array.\n"; //ad_exit(21); throw e; } dvar_matrix vtmp=nograd_assign(tmp); save_identifier_string("TEST1"); //m1.save_dvar_matrix_value(); m1.save_dvar_matrix_position(); cm2.save_dmatrix_value(); cm2.save_dmatrix_position(); vtmp.save_dvar_matrix_position(); save_identifier_string("TEST6"); gradient_structure::GRAD_STACK1-> set_gradient_stack(dmcm_prod); return vtmp; }