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 00035 dvariable operator*(const dvar_vector& v1, const dvar_vector& v2) 00036 { 00037 RETURN_ARRAYS_INCREMENT(); 00038 if (v1.indexmin()!=v2.indexmin()||v1.indexmax()!=v2.indexmax()) 00039 { 00040 cerr << "Incompatible bounds in " 00041 "prevariable operator * (const dvar_vector& v1, const dvar_vector& v2)" 00042 << endl; 00043 ad_exit(1); 00044 } 00045 double tmp=0; 00046 00047 #ifndef USE_ASSEMBLER 00048 int mmin=v1.indexmin(); 00049 int mmax=v1.indexmax(); 00050 #ifdef OPT_LIB 00051 double * pt1=&v1.elem_value(mmin); 00052 double * pt1m=&v1.elem_value(mmax); 00053 double * pt2=&v2.elem_value(mmin); 00054 do 00055 { 00056 tmp+= *pt1++ * *pt2++; 00057 } 00058 while (pt1<=pt1m); 00059 #else 00060 for (int i=mmin;i<=mmax;i++) 00061 { 00062 tmp+=v1.elem_value(i)*v2.elem_value(i); 00063 } 00064 #endif 00065 #else 00066 int mmin=v1.indexmin(); 00067 int n=v1.indexmax()-mmin+1; 00068 dp_dotproduct(&tmp,&(v1.elem_value(mmin)),&(v2.elem_value(mmin)),n); 00069 #endif 00070 00071 dvariable vtmp=nograd_assign(tmp); 00072 00073 // The derivative list considerations 00074 save_identifier_string("bbbb"); 00075 v1.save_dvar_vector_value(); 00076 v1.save_dvar_vector_position(); 00077 v2.save_dvar_vector_value(); 00078 v2.save_dvar_vector_position(); 00079 vtmp.save_prevariable_position(); 00080 save_identifier_string("aaaa"); 00081 gradient_structure::GRAD_STACK1-> 00082 set_gradient_stack(dvdv_dot); 00083 RETURN_ARRAYS_DECREMENT(); 00084 return vtmp; 00085 } 00086 00087 /* 00088 * Description not yet available. 00089 * \param 00090 */ 00091 void dvdv_dot(void) 00092 { 00093 verify_identifier_string("aaaa"); 00094 double dftmp=restore_prevariable_derivative(); 00095 dvar_vector_position v2pos=restore_dvar_vector_position(); 00096 dvector cv2=restore_dvar_vector_value(v2pos); 00097 dvar_vector_position v1pos=restore_dvar_vector_position(); 00098 dvector cv1=restore_dvar_vector_value(v1pos); 00099 verify_identifier_string("bbbb"); 00100 dvector dfv1(cv1.indexmin(),cv1.indexmax()); 00101 dvector dfv2(cv2.indexmin(),cv2.indexmax()); 00102 #ifdef OPT_LIB 00103 double * pdf1=&dfv1(cv1.indexmin()); 00104 double * pdf1m=&dfv1(cv1.indexmax()); 00105 double * pdf2=&dfv2(cv1.indexmin()); 00106 double * pc1=&cv1(cv1.indexmin()); 00107 double * pc2=&cv2(cv1.indexmin()); 00108 do 00109 { 00110 *pdf1++ = dftmp * *pc2++; 00111 *pdf2++ = dftmp * *pc1++; 00112 } 00113 while (pdf1<=pdf1m); 00114 #else 00115 for (int i=cv1.indexmin();i<=cv1.indexmax();i++) 00116 { 00117 //tmp+=cv1(i)*cv2(i); 00118 dfv1(i)=dftmp*cv2.elem(i); 00119 dfv2(i)=dftmp*cv1.elem(i); 00120 } 00121 #endif 00122 dfv1.save_dvector_derivatives(v1pos); 00123 dfv2.save_dvector_derivatives(v2pos); 00124 } 00125 00126 void X_dv_sum(void); 00127 00133 dvariable sum(const dvar_vector& v1) 00134 { 00135 if (allocated(v1)) 00136 { 00137 dvector cv1=value(v1); 00138 double tmp=0; 00139 for (int i=cv1.indexmin();i<=cv1.indexmax();i++) 00140 { 00141 tmp+=cv1.elem(i); 00142 } 00143 00144 dvariable vtmp=nograd_assign(tmp); 00145 00146 // The derivative list considerations 00147 save_identifier_string("bbbb"); 00148 v1.save_dvar_vector_position(); 00149 vtmp.save_prevariable_position(); 00150 save_identifier_string("aaaa"); 00151 gradient_structure::GRAD_STACK1-> 00152 set_gradient_stack(X_dv_sum); 00153 return vtmp; 00154 } 00155 else 00156 { 00157 dvariable vtmp=0.0; 00158 return vtmp; 00159 } 00160 } 00161 00162 /* 00163 * Description not yet available. 00164 * 00165 */ 00166 void X_dv_sum(void) 00167 { 00168 verify_identifier_string("aaaa"); 00169 double dftmp=restore_prevariable_derivative(); 00170 dvar_vector_position v1pos=restore_dvar_vector_position(); 00171 verify_identifier_string("bbbb"); 00172 dvector dfv1(v1pos.indexmin(),v1pos.indexmax()); 00173 for (int i=dfv1.indexmin();i<=dfv1.indexmax();i++) 00174 { 00175 //tmp+=cv1(i)*cv2(i); 00176 dfv1(i)=dftmp; 00177 } 00178 dfv1.save_dvector_derivatives(v1pos); 00179 } 00180 00187 dvariable sum(const dvar_matrix& m) 00188 { 00189 RETURN_ARRAYS_INCREMENT(); 00190 dvariable tmp=0.; 00191 for (int i=m.rowmin();i<=m.rowmax();i++) 00192 { 00193 tmp+=sum(m.elem(i)); 00194 } 00195 RETURN_ARRAYS_DECREMENT(); 00196 return tmp; 00197 }
Generated on Tue Mar 8 2016 19:51:33 for ADMB Documentation by 1.8.0 |