From b4ec8d3d1460b198976897cb3c66a6eb38abcdb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=B4=9A=E2=88=80W=E2=88=80=E1=B4=8E?= Date: Sat, 29 Jul 2017 02:42:42 -0700 Subject: [PATCH] differential std::clamp from c++17 with dlib:: prefix to clamp() (#734) --- dlib/control/mpc.h | 4 ++-- dlib/matrix/matrix_la.h | 2 +- dlib/optimization/optimization.h | 4 ++-- dlib/optimization/optimization_solve_qp_using_smo.h | 8 ++++---- dlib/test/matrix2.cpp | 12 ++++++------ 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/dlib/control/mpc.h b/dlib/control/mpc.h index 63e092ab4..48ef2b72d 100644 --- a/dlib/control/mpc.h +++ b/dlib/control/mpc.h @@ -339,8 +339,8 @@ namespace dlib for (unsigned long i = 0; i < horizon; ++i) { v_old[i] = v[i]; - v[i] = clamp(controls[i] - 1.0/lambda * df[i], lower, upper); - controls[i] = clamp(v[i] + (std::sqrt(lambda)-1)/(std::sqrt(lambda)+1)*(v[i]-v_old[i]), lower, upper); + v[i] = dlib::clamp(controls[i] - 1.0/lambda * df[i], lower, upper); + controls[i] = dlib::clamp(v[i] + (std::sqrt(lambda)-1)/(std::sqrt(lambda)+1)*(v[i]-v_old[i]), lower, upper); } } } diff --git a/dlib/matrix/matrix_la.h b/dlib/matrix/matrix_la.h index 1b4825199..bd19d6ab4 100644 --- a/dlib/matrix/matrix_la.h +++ b/dlib/matrix/matrix_la.h @@ -1646,7 +1646,7 @@ convergence: if (dot(delta, g) < 0) return p; else - return vector(p)+clamp(delta, -1, 1); + return vector(p)+dlib::clamp(delta, -1, 1); } // ---------------------------------------------------------------------------------------- diff --git a/dlib/optimization/optimization.h b/dlib/optimization/optimization.h index 1ffe036a5..561d64376 100644 --- a/dlib/optimization/optimization.h +++ b/dlib/optimization/optimization.h @@ -527,7 +527,7 @@ namespace dlib last_alpha = alpha; // Take the search step indicated by the above line search - x = clamp(x + alpha*s, x_lower, x_upper); + x = dlib::clamp(x + alpha*s, x_lower, x_upper); g = der(x); if (!is_finite(f_value)) @@ -658,7 +658,7 @@ namespace dlib last_alpha = alpha; // Take the search step indicated by the above line search - x = clamp(x + alpha*s, x_lower, x_upper); + x = dlib::clamp(x + alpha*s, x_lower, x_upper); g = -der(x); // Don't forget to negate the output from the line search since it is from the diff --git a/dlib/optimization/optimization_solve_qp_using_smo.h b/dlib/optimization/optimization_solve_qp_using_smo.h index a44875046..b9cc74df3 100644 --- a/dlib/optimization/optimization_solve_qp_using_smo.h +++ b/dlib/optimization/optimization_solve_qp_using_smo.h @@ -286,7 +286,7 @@ namespace dlib lambda = A*alpha; else lambda = A*alpha + d; - lambda = clamp(lambda, 0, max_lambda); + lambda = dlib::clamp(lambda, 0, max_lambda); // Compute f'(alpha) (i.e. the gradient of f(alpha) with respect to alpha) for the current alpha. matrix df = Q*alpha - b - trans(A)*lambda; @@ -333,7 +333,7 @@ namespace dlib lambda = A*alpha; else lambda = A*alpha + d; - lambda = clamp(lambda, 0, max_lambda); + lambda = dlib::clamp(lambda, 0, max_lambda); df = Q*alpha - b - trans(A)*lambda; if (trans(alpha)*df - C*min(df) < eps) @@ -375,7 +375,7 @@ namespace dlib lambda = A*alpha; else lambda = A*alpha + d; - lambda = clamp(lambda, 0, max_lambda); + lambda = dlib::clamp(lambda, 0, max_lambda); // Perform this form of the update every so often because doing so can help // avoid the buildup of numerical errors you get with the alternate update @@ -524,7 +524,7 @@ namespace dlib df = Q*alpha + b; // now take a projected gradient step using Nesterov's method. v = clamp(alpha - 1.0/lipschitz_bound * df, lower, upper); - alpha = clamp((1-gamma)*v + gamma*v_old, lower, upper); + alpha = dlib::clamp((1-gamma)*v + gamma*v_old, lower, upper); // check for convergence every 10 iterations diff --git a/dlib/test/matrix2.cpp b/dlib/test/matrix2.cpp index cee65e207..6cd5d4e93 100644 --- a/dlib/test/matrix2.cpp +++ b/dlib/test/matrix2.cpp @@ -379,23 +379,23 @@ namespace u = 2,2.2; out = 2, 2.2; - DLIB_TEST(equal(clamp(x, l, u) , out)); + DLIB_TEST(equal(dlib::clamp(x, l, u) , out)); out = 3, 2.2; - DLIB_TEST(!equal(clamp(x, l, u) , out)); + DLIB_TEST(!equal(dlib::clamp(x, l, u) , out)); out = 2, 4.2; - DLIB_TEST(!equal(clamp(x, l, u) , out)); + DLIB_TEST(!equal(dlib::clamp(x, l, u) , out)); x = 1.5, 1.5; out = x; - DLIB_TEST(equal(clamp(x, l, u) , out)); + DLIB_TEST(equal(dlib::clamp(x, l, u) , out)); x = 0.5, 1.5; out = 1, 1.5; - DLIB_TEST(equal(clamp(x, l, u) , out)); + DLIB_TEST(equal(dlib::clamp(x, l, u) , out)); x = 1.5, 0.5; out = 1.5, 1.0; - DLIB_TEST(equal(clamp(x, l, u) , out)); + DLIB_TEST(equal(dlib::clamp(x, l, u) , out)); }