Skip to content

Commit

Permalink
one side derivatives
Browse files Browse the repository at this point in the history
  • Loading branch information
Huangzizhou committed Jun 7, 2024
1 parent 4438ac2 commit e7e41ad
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/polysolve/nonlinear/Solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ namespace polysolve::nonlinear
void Solver::verify_gradient(Problem &objFunc, const TVector &x, const TVector &grad)
{
bool match = false;
double J = objFunc(x);

switch (gradient_fd_strategy)
{
Expand All @@ -598,15 +599,17 @@ namespace polysolve::nonlinear
double J1 = objFunc(x1);

double fd = (J2 - J1) / 2 / gradient_fd_eps;
double fd_right = (J2 - J) / gradient_fd_eps;
double fd_left = (J - J1) / gradient_fd_eps;
double analytic = direc.dot(grad);

match = abs(fd - analytic) < 1e-8 || abs(fd - analytic) < 1e-4 * abs(analytic);

// Log error in either case to make it more visible in the logs.
if (match)
m_logger.debug("step size: {}, finite difference: {}, derivative: {}", gradient_fd_eps, fd, analytic);
m_logger.debug("step size: {}, finite difference: {} {} {}, derivative: {}", gradient_fd_eps, fd, fd_left, fd_right, analytic);
else
m_logger.error("step size: {}, finite difference: {}, derivative: {}", gradient_fd_eps, fd, analytic);
m_logger.error("step size: {}, finite difference: {} {} {}, derivative: {}", gradient_fd_eps, fd, fd_left, fd_right, analytic);
}
break;
case FiniteDiffStrategy::FULL_FINITE_DIFF:
Expand Down

0 comments on commit e7e41ad

Please sign in to comment.