Newer
Older
{
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));
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
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
}
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