Skip to content

Commit

Permalink
Clean up code:
Browse files Browse the repository at this point in the history
Create another gradcalc function which is a wrapper to call value and gradcalc.
  • Loading branch information
johnoel committed Mar 12, 2014
1 parent a3ce061 commit 6ffe1a0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/linad99/fvar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3485,6 +3485,7 @@ int count_factor(const ivector & v);

//void gradcalc( int , double *);
void gradcalc(int nvar, const dvector & g);
double gradcalc(int nvar, const dvector& g, dvariable& f);
void slave_gradcalc(void);

/**
Expand Down
23 changes: 19 additions & 4 deletions src/linad99/sgradclc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@
#endif

/**
Compute the gradient from the data stored in the global \ref gradient_structure.
\param nvar Number of variables in the gradient.
\param _g Vector from 1 to nvar. On return contains the gradient.
*/
Compute the gradient from the data stored in the global \ref gradient_structure.
\param nvar Number of variables in the gradient.
\param _g Vector from 1 to nvar. On return contains the gradient.
*/
void gradcalc(int nvar, const dvector& _g)
{
if (nvar!=0)
Expand Down Expand Up @@ -227,6 +228,20 @@ void gradcalc(int nvar, const dvector& _g)
gradient_structure::restore_variables();
}
}
/**
Compute the gradient from the data stored in the global \ref gradient_structure.
\param nvar Number of variables in the gradient.
\param _g Vector from 1 to nvar. On return contains the gradient.
\param f objective function
\returns likelihood value
*/
double gradcalc(int nvar, const dvector& _g, dvariable& f)
{
double v = value(f);
gradcalc(nvar, _g);
return v;
}
/**
*/
void gradient_structure::save_arrays()
Expand Down
27 changes: 8 additions & 19 deletions src/nh99/mod_hess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ void function_minimizer::hess_routine_noparallel(void)
//if (adjm_ptr) set_labels_for_hess(nvar);
independent_variables x(1,nvar);
initial_params::xinit(x); // get the initial values into the x vector
double f;
double delta=1.e-5;
dvector g1(1,nvar);
dvector g2(1,nvar);
Expand All @@ -95,8 +94,7 @@ void function_minimizer::hess_routine_noparallel(void)
*objective_function_value::pobjfun=0.0;
pre_userfunction();
vf+=*objective_function_value::pobjfun;
f=value(vf);
gradcalc(nvar,g1);
gradcalc(nvar, g1, vf);
}
double sdelta1;
double sdelta2;
Expand All @@ -114,8 +112,7 @@ void function_minimizer::hess_routine_noparallel(void)
*objective_function_value::pobjfun=0.0;
pre_userfunction();
vf+=*objective_function_value::pobjfun;
double f = value(vf);
gradcalc(nvar,g1);
gradcalc(nvar, g1, vf);

sdelta2=x(i)-delta;
useless(sdelta2);
Expand All @@ -126,8 +123,7 @@ void function_minimizer::hess_routine_noparallel(void)
*objective_function_value::pobjfun=0.0;
pre_userfunction();
vf+=*objective_function_value::pobjfun;
f=value(vf);
gradcalc(nvar,g2);
gradcalc(nvar, g2, vf);
x(i)=xsave;
hess1=(g1-g2)/(sdelta1-sdelta2);

Expand All @@ -140,8 +136,7 @@ void function_minimizer::hess_routine_noparallel(void)
*objective_function_value::pobjfun=0.0;
pre_userfunction();
vf+=*objective_function_value::pobjfun;
f=value(vf);
gradcalc(nvar,g1);
gradcalc(nvar, g1, vf);

x(i)=xsave-eps*delta;
sdelta2=x(i)-eps*delta;
Expand All @@ -153,8 +148,7 @@ void function_minimizer::hess_routine_noparallel(void)
*objective_function_value::pobjfun=0.0;
pre_userfunction();
vf+=*objective_function_value::pobjfun;
f=value(vf);
gradcalc(nvar,g2);
gradcalc(nvar, g2, vf);
x(i)=xsave;

vf=initial_params::reset(dvar_vector(x));
Expand All @@ -178,7 +172,6 @@ void function_minimizer::hess_routine_and_constraint(int iprof,
int nvar=initial_params::nvarcalc(); // get the number of active parameters
independent_variables x(1,nvar);
initial_params::xinit(x); // get the initial values into the x vector
double f;
double delta=1.e-6;
dvector g1(1,nvar);
dvector g2(1,nvar);
Expand All @@ -203,8 +196,7 @@ void function_minimizer::hess_routine_and_constraint(int iprof,
pre_userfunction();
vf+=*objective_function_value::pobjfun;
vf-=lambda*likeprof_params::likeprofptr[iprof]->variable();
f=value(vf);
gradcalc(nvar,g1);
gradcalc(nvar, g1, vf);
}
double sdelta1;
double sdelta2;
Expand All @@ -213,7 +205,6 @@ void function_minimizer::hess_routine_and_constraint(int iprof,
{
hess_calcreport(i,nvar);

double f=0.0;
double xsave=x(i);
sdelta1=x(i)+delta;
useless(sdelta1);
Expand All @@ -225,8 +216,7 @@ void function_minimizer::hess_routine_and_constraint(int iprof,
pre_userfunction();
vf+=*objective_function_value::pobjfun;
vf-=lambda*likeprof_params::likeprofptr[iprof]->variable();
f=value(vf);
gradcalc(nvar,g1);
gradcalc(nvar, g1, vf);

sdelta2=x(i)-delta;
useless(sdelta2);
Expand All @@ -238,8 +228,7 @@ void function_minimizer::hess_routine_and_constraint(int iprof,
pre_userfunction();
vf+=*objective_function_value::pobjfun;
vf-=lambda*likeprof_params::likeprofptr[iprof]->variable();
f=value(vf);
gradcalc(nvar,g2);
gradcalc(nvar, g2, vf);
x(i)=xsave;
hess1=(g1-g2)/(sdelta1-sdelta2);
/*
Expand Down

0 comments on commit 6ffe1a0

Please sign in to comment.