Newer
Older
p_m->first = l1;
p_m->second = cf1;
p_l.first = l2;
p_l.second = cf2;
return(p_l);
}
// Utility routines that checks for convergence based on various criteria
// Based on zero (neglible) gradient
bool zero_grad_conv(const ColumnVector& par,
const ColumnVector& grad,
double cf,
double gtol)
{
double test = 0.0; // test will be largest relative component of gradient
for (int i=0; i<par.Nrows(); i++) {

Jesper Andersson
committed
test = std::max(test,std::abs(grad.element(i))*std::max(std::abs(par.element(i)),1.0));

Jesper Andersson
committed
test /= std::max(cf,1.0); // Protect against near-zero values for cost-function
return(test < gtol);
}
// Based on zero (neglible) decrease in cost-function
bool zero_cf_diff_conv(double cfo,
double cfn,
double cftol)
{

Jesper Andersson
committed
return(2.0*std::abs(cfo-cfn) <= cftol*(std::abs(cfo)+std::abs(cfn)+MISCMATHS::EPS));
}
// Based on zero (neglible) step in parameter space
bool zero_par_step_conv(const ColumnVector& par,
const ColumnVector& step,
double ptol)
{
double test = 0.0;
for (int i=0; i<par.Nrows(); i++) {

Jesper Andersson
committed
test = std::max(test,std::abs(step.element(i))/std::max(std::abs(par.element(i)),1.0));
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
}
return(test < ptol);
}
// Utility routines that allow the user to check accuracy of their own grad and hess functions
pair<ColumnVector,ColumnVector> check_grad(const ColumnVector& par,
const NonlinCF& cfo)
{
pair<ColumnVector,ColumnVector> rv;
rv.first = cfo.NonlinCF::grad(par);
rv.second = cfo.grad(par);
return(rv);
}
pair<boost::shared_ptr<BFMatrix>,boost::shared_ptr<BFMatrix> > check_hess(const ColumnVector& par,
const NonlinCF& cfo)
{
pair<boost::shared_ptr<BFMatrix>,boost::shared_ptr<BFMatrix> > rv;
rv.first = cfo.NonlinCF::hess(par);
rv.second = cfo.hess(par);
return(rv);
}
void print_newmat(const NEWMAT::GeneralMatrix& m,
std::string fname)
{
if (!fname.length()) {
cout << endl << m << endl;
}
else {
try {
std::ofstream fout(fname.c_str());
fout << setprecision(10) << m;
}
catch(...) {
std::string errmsg("print_newmat: Failed to write to file " + fname);
throw NonlinException(errmsg);
}
}
}
} // End namespace MISCMATHS