Markus Moll | c9eca78 | 2012-07-25 11:34:59 +0200 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
| 2 | // Copyright 2012 Google Inc. All rights reserved. |
| 3 | // http://code.google.com/p/ceres-solver/ |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright notice, |
| 9 | // this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | // this list of conditions and the following disclaimer in the documentation |
| 12 | // and/or other materials provided with the distribution. |
| 13 | // * Neither the name of Google Inc. nor the names of its contributors may be |
| 14 | // used to endorse or promote products derived from this software without |
| 15 | // specific prior written permission. |
| 16 | // |
| 17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 21 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | // POSSIBILITY OF SUCH DAMAGE. |
| 28 | // |
| 29 | // Author: moll.markus@arcor.de (Markus Moll) |
Sameer Agarwal | e7295c2 | 2012-11-23 18:56:50 -0800 | [diff] [blame] | 30 | // sameeragarwal@google.com (Sameer Agarwal) |
Markus Moll | c9eca78 | 2012-07-25 11:34:59 +0200 | [diff] [blame] | 31 | |
Sameer Agarwal | e7295c2 | 2012-11-23 18:56:50 -0800 | [diff] [blame] | 32 | #include "ceres/polynomial.h" |
Markus Moll | c9eca78 | 2012-07-25 11:34:59 +0200 | [diff] [blame] | 33 | |
Markus Moll | c9eca78 | 2012-07-25 11:34:59 +0200 | [diff] [blame] | 34 | #include <cmath> |
| 35 | #include <cstddef> |
Sameer Agarwal | e7295c2 | 2012-11-23 18:56:50 -0800 | [diff] [blame] | 36 | #include <vector> |
| 37 | |
Markus Moll | c9eca78 | 2012-07-25 11:34:59 +0200 | [diff] [blame] | 38 | #include "Eigen/Dense" |
| 39 | #include "ceres/internal/port.h" |
Sameer Agarwal | 0beab86 | 2012-08-13 15:12:01 -0700 | [diff] [blame] | 40 | #include "glog/logging.h" |
Markus Moll | c9eca78 | 2012-07-25 11:34:59 +0200 | [diff] [blame] | 41 | |
| 42 | namespace ceres { |
| 43 | namespace internal { |
| 44 | namespace { |
| 45 | |
| 46 | // Balancing function as described by B. N. Parlett and C. Reinsch, |
| 47 | // "Balancing a Matrix for Calculation of Eigenvalues and Eigenvectors". |
| 48 | // In: Numerische Mathematik, Volume 13, Number 4 (1969), 293-304, |
| 49 | // Springer Berlin / Heidelberg. DOI: 10.1007/BF02165404 |
| 50 | void BalanceCompanionMatrix(Matrix* companion_matrix_ptr) { |
| 51 | CHECK_NOTNULL(companion_matrix_ptr); |
| 52 | Matrix& companion_matrix = *companion_matrix_ptr; |
| 53 | Matrix companion_matrix_offdiagonal = companion_matrix; |
| 54 | companion_matrix_offdiagonal.diagonal().setZero(); |
| 55 | |
| 56 | const int degree = companion_matrix.rows(); |
| 57 | |
| 58 | // gamma <= 1 controls how much a change in the scaling has to |
| 59 | // lower the 1-norm of the companion matrix to be accepted. |
| 60 | // |
| 61 | // gamma = 1 seems to lead to cycles (numerical issues?), so |
| 62 | // we set it slightly lower. |
| 63 | const double gamma = 0.9; |
| 64 | |
| 65 | // Greedily scale row/column pairs until there is no change. |
| 66 | bool scaling_has_changed; |
| 67 | do { |
| 68 | scaling_has_changed = false; |
| 69 | |
| 70 | for (int i = 0; i < degree; ++i) { |
| 71 | const double row_norm = companion_matrix_offdiagonal.row(i).lpNorm<1>(); |
| 72 | const double col_norm = companion_matrix_offdiagonal.col(i).lpNorm<1>(); |
| 73 | |
| 74 | // Decompose row_norm/col_norm into mantissa * 2^exponent, |
| 75 | // where 0.5 <= mantissa < 1. Discard mantissa (return value |
| 76 | // of frexp), as only the exponent is needed. |
| 77 | int exponent = 0; |
| 78 | std::frexp(row_norm / col_norm, &exponent); |
| 79 | exponent /= 2; |
| 80 | |
| 81 | if (exponent != 0) { |
| 82 | const double scaled_col_norm = std::ldexp(col_norm, exponent); |
| 83 | const double scaled_row_norm = std::ldexp(row_norm, -exponent); |
| 84 | if (scaled_col_norm + scaled_row_norm < gamma * (col_norm + row_norm)) { |
| 85 | // Accept the new scaling. (Multiplication by powers of 2 should not |
| 86 | // introduce rounding errors (ignoring non-normalized numbers and |
| 87 | // over- or underflow)) |
| 88 | scaling_has_changed = true; |
| 89 | companion_matrix_offdiagonal.row(i) *= std::ldexp(1.0, -exponent); |
| 90 | companion_matrix_offdiagonal.col(i) *= std::ldexp(1.0, exponent); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } while (scaling_has_changed); |
| 95 | |
| 96 | companion_matrix_offdiagonal.diagonal() = companion_matrix.diagonal(); |
| 97 | companion_matrix = companion_matrix_offdiagonal; |
| 98 | VLOG(3) << "Balanced companion matrix is\n" << companion_matrix; |
| 99 | } |
| 100 | |
| 101 | void BuildCompanionMatrix(const Vector& polynomial, |
| 102 | Matrix* companion_matrix_ptr) { |
| 103 | CHECK_NOTNULL(companion_matrix_ptr); |
| 104 | Matrix& companion_matrix = *companion_matrix_ptr; |
| 105 | |
| 106 | const int degree = polynomial.size() - 1; |
| 107 | |
| 108 | companion_matrix.resize(degree, degree); |
| 109 | companion_matrix.setZero(); |
| 110 | companion_matrix.diagonal(-1).setOnes(); |
Keir Mierle | ebcfdf4 | 2012-08-08 10:29:39 -0700 | [diff] [blame] | 111 | companion_matrix.col(degree - 1) = -polynomial.reverse().head(degree); |
Markus Moll | c9eca78 | 2012-07-25 11:34:59 +0200 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // Remove leading terms with zero coefficients. |
| 115 | Vector RemoveLeadingZeros(const Vector& polynomial_in) { |
| 116 | int i = 0; |
| 117 | while (i < (polynomial_in.size() - 1) && polynomial_in(i) == 0.0) { |
| 118 | ++i; |
| 119 | } |
| 120 | return polynomial_in.tail(polynomial_in.size() - i); |
| 121 | } |
| 122 | } // namespace |
| 123 | |
| 124 | bool FindPolynomialRoots(const Vector& polynomial_in, |
| 125 | Vector* real, |
| 126 | Vector* imaginary) { |
Markus Moll | c9eca78 | 2012-07-25 11:34:59 +0200 | [diff] [blame] | 127 | if (polynomial_in.size() == 0) { |
| 128 | LOG(ERROR) << "Invalid polynomial of size 0 passed to FindPolynomialRoots"; |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | Vector polynomial = RemoveLeadingZeros(polynomial_in); |
| 133 | const int degree = polynomial.size() - 1; |
| 134 | |
| 135 | // Is the polynomial constant? |
| 136 | if (degree == 0) { |
| 137 | LOG(WARNING) << "Trying to extract roots from a constant " |
| 138 | << "polynomial in FindPolynomialRoots"; |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | // Divide by leading term |
| 143 | const double leading_term = polynomial(0); |
| 144 | polynomial /= leading_term; |
| 145 | |
| 146 | // Separately handle linear polynomials. |
| 147 | if (degree == 1) { |
| 148 | if (real != NULL) { |
| 149 | real->resize(1); |
| 150 | (*real)(0) = -polynomial(1); |
| 151 | } |
| 152 | if (imaginary != NULL) { |
| 153 | imaginary->resize(1); |
| 154 | imaginary->setZero(); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // The degree is now known to be at least 2. |
| 159 | // Build and balance the companion matrix to the polynomial. |
| 160 | Matrix companion_matrix(degree, degree); |
| 161 | BuildCompanionMatrix(polynomial, &companion_matrix); |
| 162 | BalanceCompanionMatrix(&companion_matrix); |
| 163 | |
| 164 | // Find its (complex) eigenvalues. |
Petter Strandmark | ab8e2dc | 2012-09-10 08:46:22 -0700 | [diff] [blame] | 165 | Eigen::EigenSolver<Matrix> solver(companion_matrix, false); |
Markus Moll | c9eca78 | 2012-07-25 11:34:59 +0200 | [diff] [blame] | 166 | if (solver.info() != Eigen::Success) { |
| 167 | LOG(ERROR) << "Failed to extract eigenvalues from companion matrix."; |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | // Output roots |
| 172 | if (real != NULL) { |
| 173 | *real = solver.eigenvalues().real(); |
| 174 | } else { |
| 175 | LOG(WARNING) << "NULL pointer passed as real argument to " |
| 176 | << "FindPolynomialRoots. Real parts of the roots will not " |
| 177 | << "be returned."; |
| 178 | } |
| 179 | if (imaginary != NULL) { |
| 180 | *imaginary = solver.eigenvalues().imag(); |
| 181 | } |
| 182 | return true; |
| 183 | } |
| 184 | |
Sameer Agarwal | e7295c2 | 2012-11-23 18:56:50 -0800 | [diff] [blame] | 185 | Vector DifferentiatePolynomial(const Vector& polynomial) { |
| 186 | const int degree = polynomial.rows() - 1; |
| 187 | CHECK_GE(degree, 0); |
Sameer Agarwal | c89ea4b | 2013-01-09 16:09:35 -0800 | [diff] [blame] | 188 | |
| 189 | // Degree zero polynomials are constants, and their derivative does |
| 190 | // not result in a smaller degree polynomial, just a degree zero |
| 191 | // polynomial with value zero. |
| 192 | if (degree == 0) { |
| 193 | return Eigen::VectorXd::Zero(1); |
| 194 | } |
| 195 | |
Sameer Agarwal | e7295c2 | 2012-11-23 18:56:50 -0800 | [diff] [blame] | 196 | Vector derivative(degree); |
| 197 | for (int i = 0; i < degree; ++i) { |
| 198 | derivative(i) = (degree - i) * polynomial(i); |
| 199 | } |
| 200 | |
| 201 | return derivative; |
| 202 | } |
| 203 | |
| 204 | void MinimizePolynomial(const Vector& polynomial, |
| 205 | const double x_min, |
| 206 | const double x_max, |
| 207 | double* optimal_x, |
| 208 | double* optimal_value) { |
| 209 | // Find the minimum of the polynomial at the two ends. |
| 210 | // |
| 211 | // We start by inspecting the middle of the interval. Technically |
| 212 | // this is not needed, but we do this to make this code as close to |
| 213 | // the minFunc package as possible. |
| 214 | *optimal_x = (x_min + x_max) / 2.0; |
| 215 | *optimal_value = EvaluatePolynomial(polynomial, *optimal_x); |
| 216 | |
| 217 | const double x_min_value = EvaluatePolynomial(polynomial, x_min); |
| 218 | if (x_min_value < *optimal_value) { |
| 219 | *optimal_value = x_min_value; |
| 220 | *optimal_x = x_min; |
| 221 | } |
| 222 | |
| 223 | const double x_max_value = EvaluatePolynomial(polynomial, x_max); |
| 224 | if (x_max_value < *optimal_value) { |
| 225 | *optimal_value = x_max_value; |
| 226 | *optimal_x = x_max; |
| 227 | } |
| 228 | |
| 229 | // If the polynomial is linear or constant, we are done. |
| 230 | if (polynomial.rows() <= 2) { |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | const Vector derivative = DifferentiatePolynomial(polynomial); |
| 235 | Vector roots_real; |
| 236 | if (!FindPolynomialRoots(derivative, &roots_real, NULL)) { |
| 237 | LOG(WARNING) << "Unable to find the critical points of " |
| 238 | << "the interpolating polynomial."; |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | // This is a bit of an overkill, as some of the roots may actually |
| 243 | // have a complex part, but its simpler to just check these values. |
| 244 | for (int i = 0; i < roots_real.rows(); ++i) { |
| 245 | const double root = roots_real(i); |
| 246 | if ((root < x_min) || (root > x_max)) { |
| 247 | continue; |
| 248 | } |
| 249 | |
| 250 | const double value = EvaluatePolynomial(polynomial, root); |
| 251 | if (value < *optimal_value) { |
| 252 | *optimal_value = value; |
| 253 | *optimal_x = root; |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | Vector FindInterpolatingPolynomial(const vector<FunctionSample>& samples) { |
| 259 | const int num_samples = samples.size(); |
| 260 | int num_constraints = 0; |
| 261 | for (int i = 0; i < num_samples; ++i) { |
| 262 | if (samples[i].value_is_valid) { |
| 263 | ++num_constraints; |
| 264 | } |
| 265 | if (samples[i].gradient_is_valid) { |
| 266 | ++num_constraints; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | const int degree = num_constraints - 1; |
| 271 | Matrix lhs = Matrix::Zero(num_constraints, num_constraints); |
| 272 | Vector rhs = Vector::Zero(num_constraints); |
| 273 | |
| 274 | int row = 0; |
| 275 | for (int i = 0; i < num_samples; ++i) { |
| 276 | const FunctionSample& sample = samples[i]; |
| 277 | if (sample.value_is_valid) { |
Sameer Agarwal | e7295c2 | 2012-11-23 18:56:50 -0800 | [diff] [blame] | 278 | for (int j = 0; j <= degree; ++j) { |
| 279 | lhs(row, j) = pow(sample.x, degree - j); |
| 280 | } |
| 281 | rhs(row) = sample.value; |
| 282 | ++row; |
| 283 | } |
| 284 | |
| 285 | if (sample.gradient_is_valid) { |
| 286 | for (int j = 0; j < degree; ++j) { |
Sameer Agarwal | e7295c2 | 2012-11-23 18:56:50 -0800 | [diff] [blame] | 287 | lhs(row, j) = (degree - j) * pow(sample.x, degree - j - 1); |
| 288 | } |
| 289 | rhs(row) = sample.gradient; |
| 290 | ++row; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | return lhs.fullPivLu().solve(rhs); |
| 295 | } |
| 296 | |
| 297 | void MinimizeInterpolatingPolynomial(const vector<FunctionSample>& samples, |
| 298 | double x_min, |
| 299 | double x_max, |
| 300 | double* optimal_x, |
| 301 | double* optimal_value) { |
| 302 | const Vector polynomial = FindInterpolatingPolynomial(samples); |
| 303 | MinimizePolynomial(polynomial, x_min, x_max, optimal_x, optimal_value); |
| 304 | for (int i = 0; i < samples.size(); ++i) { |
| 305 | const FunctionSample& sample = samples[i]; |
| 306 | if ((sample.x < x_min) || (sample.x > x_max)) { |
| 307 | continue; |
| 308 | } |
| 309 | |
| 310 | const double value = EvaluatePolynomial(polynomial, sample.x); |
| 311 | if (value < *optimal_value) { |
| 312 | *optimal_x = sample.x; |
| 313 | *optimal_value = value; |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
Markus Moll | c9eca78 | 2012-07-25 11:34:59 +0200 | [diff] [blame] | 318 | } // namespace internal |
| 319 | } // namespace ceres |