Commit c75bbc7d authored by Davis King's avatar Davis King

Added a version of poly_min_extrap() that uses a 2nd degree model.

parent aaeb52ba
...@@ -193,6 +193,24 @@ namespace dlib ...@@ -193,6 +193,24 @@ namespace dlib
return put_in_range(0,1,x); return put_in_range(0,1,x);
} }
// ----------------------------------------------------------------------------------------
inline double poly_min_extrap (
double f0,
double d0,
double f1
)
{
const double temp = 2*(f1 - f0 - d0);
if (std::abs(temp) <= d0*std::numeric_limits<double>::epsilon())
return 0.5;
const double alpha = -d0/temp;
// now make sure the minimum is within the allowed range of (0,1)
return put_in_range(0,1,alpha);
}
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
inline double lagrange_poly_min_extrap ( inline double lagrange_poly_min_extrap (
......
...@@ -103,6 +103,22 @@ namespace dlib ...@@ -103,6 +103,22 @@ namespace dlib
- returns the point in the range [0,1] that minimizes the polynomial c(x) - returns the point in the range [0,1] that minimizes the polynomial c(x)
!*/ !*/
// ----------------------------------------------------------------------------------------
inline double poly_min_extrap (
double f0,
double d0,
double f1
);
/*!
ensures
- let c(x) be a 2nd degree polynomial such that:
- c(0) == f0
- c(1) == f1
- derivative of c(x) at x==0 is d0
- returns the point in the range [0,1] that minimizes the polynomial c(x)
!*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
inline double lagrange_poly_min_extrap ( inline double lagrange_poly_min_extrap (
......
...@@ -917,6 +917,21 @@ namespace ...@@ -917,6 +917,21 @@ namespace
} }
void test_poly_min_extract_2nd()
{
double off;
off = 0.0; DLIB_TEST(std::abs( poly_min_extrap(off*off, -2*off, (1-off)*(1-off)) - off) < 1e-13);
off = 0.1; DLIB_TEST(std::abs( poly_min_extrap(off*off, -2*off, (1-off)*(1-off)) - off) < 1e-13);
off = 0.2; DLIB_TEST(std::abs( poly_min_extrap(off*off, -2*off, (1-off)*(1-off)) - off) < 1e-13);
off = 0.3; DLIB_TEST(std::abs( poly_min_extrap(off*off, -2*off, (1-off)*(1-off)) - off) < 1e-13);
off = 0.4; DLIB_TEST(std::abs( poly_min_extrap(off*off, -2*off, (1-off)*(1-off)) - off) < 1e-13);
off = 0.5; DLIB_TEST(std::abs( poly_min_extrap(off*off, -2*off, (1-off)*(1-off)) - off) < 1e-13);
off = 0.6; DLIB_TEST(std::abs( poly_min_extrap(off*off, -2*off, (1-off)*(1-off)) - off) < 1e-13);
off = 0.8; DLIB_TEST(std::abs( poly_min_extrap(off*off, -2*off, (1-off)*(1-off)) - off) < 1e-13);
off = 0.9; DLIB_TEST(std::abs( poly_min_extrap(off*off, -2*off, (1-off)*(1-off)) - off) < 1e-13);
off = 1.0; DLIB_TEST(std::abs( poly_min_extrap(off*off, -2*off, (1-off)*(1-off)) - off) < 1e-13);
}
class optimization_tester : public tester class optimization_tester : public tester
{ {
...@@ -930,6 +945,7 @@ namespace ...@@ -930,6 +945,7 @@ namespace
void perform_test ( void perform_test (
) )
{ {
test_poly_min_extract_2nd();
optimization_test(); optimization_test();
} }
} a; } a;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment