polynomial_test.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2015 Google Inc. All rights reserved.
  3. // http://ceres-solver.org/
  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)
  30. // sameeragarwal@google.com (Sameer Agarwal)
  31. #include "ceres/polynomial.h"
  32. #include <algorithm>
  33. #include <cmath>
  34. #include <cstddef>
  35. #include <limits>
  36. #include <vector>
  37. #include "ceres/function_sample.h"
  38. #include "ceres/test_util.h"
  39. #include "gtest/gtest.h"
  40. namespace ceres::internal {
  41. namespace {
  42. // For IEEE-754 doubles, machine precision is about 2e-16.
  43. const double kEpsilon = 1e-13;
  44. const double kEpsilonLoose = 1e-9;
  45. // Return the constant polynomial p(x) = 1.23.
  46. Vector ConstantPolynomial(double value) {
  47. Vector poly(1);
  48. poly(0) = value;
  49. return poly;
  50. }
  51. // Return the polynomial p(x) = poly(x) * (x - root).
  52. Vector AddRealRoot(const Vector& poly, double root) {
  53. Vector poly2(poly.size() + 1);
  54. poly2.setZero();
  55. poly2.head(poly.size()) += poly;
  56. poly2.tail(poly.size()) -= root * poly;
  57. return poly2;
  58. }
  59. // Return the polynomial
  60. // p(x) = poly(x) * (x - real - imag*i) * (x - real + imag*i).
  61. Vector AddComplexRootPair(const Vector& poly, double real, double imag) {
  62. Vector poly2(poly.size() + 2);
  63. poly2.setZero();
  64. // Multiply poly by x^2 - 2real + abs(real,imag)^2
  65. poly2.head(poly.size()) += poly;
  66. poly2.segment(1, poly.size()) -= 2 * real * poly;
  67. poly2.tail(poly.size()) += (real * real + imag * imag) * poly;
  68. return poly2;
  69. }
  70. // Sort the entries in a vector.
  71. // Needed because the roots are not returned in sorted order.
  72. Vector SortVector(const Vector& in) {
  73. Vector out(in);
  74. std::sort(out.data(), out.data() + out.size());
  75. return out;
  76. }
  77. // Run a test with the polynomial defined by the N real roots in roots_real.
  78. // If use_real is false, nullptr is passed as the real argument to
  79. // FindPolynomialRoots. If use_imaginary is false, nullptr is passed as the
  80. // imaginary argument to FindPolynomialRoots.
  81. template <int N>
  82. void RunPolynomialTestRealRoots(const double (&real_roots)[N],
  83. bool use_real,
  84. bool use_imaginary,
  85. double epsilon) {
  86. Vector real;
  87. Vector imaginary;
  88. Vector poly = ConstantPolynomial(1.23);
  89. for (int i = 0; i < N; ++i) {
  90. poly = AddRealRoot(poly, real_roots[i]);
  91. }
  92. Vector* const real_ptr = use_real ? &real : nullptr;
  93. Vector* const imaginary_ptr = use_imaginary ? &imaginary : nullptr;
  94. bool success = FindPolynomialRoots(poly, real_ptr, imaginary_ptr);
  95. EXPECT_EQ(success, true);
  96. if (use_real) {
  97. EXPECT_EQ(real.size(), N);
  98. real = SortVector(real);
  99. ExpectArraysClose(N, real.data(), real_roots, epsilon);
  100. }
  101. if (use_imaginary) {
  102. EXPECT_EQ(imaginary.size(), N);
  103. const Vector zeros = Vector::Zero(N);
  104. ExpectArraysClose(N, imaginary.data(), zeros.data(), epsilon);
  105. }
  106. }
  107. } // namespace
  108. TEST(Polynomial, InvalidPolynomialOfZeroLengthIsRejected) {
  109. // Vector poly(0) is an ambiguous constructor call, so
  110. // use the constructor with explicit column count.
  111. Vector poly(0, 1);
  112. Vector real;
  113. Vector imag;
  114. bool success = FindPolynomialRoots(poly, &real, &imag);
  115. EXPECT_EQ(success, false);
  116. }
  117. TEST(Polynomial, ConstantPolynomialReturnsNoRoots) {
  118. Vector poly = ConstantPolynomial(1.23);
  119. Vector real;
  120. Vector imag;
  121. bool success = FindPolynomialRoots(poly, &real, &imag);
  122. EXPECT_EQ(success, true);
  123. EXPECT_EQ(real.size(), 0);
  124. EXPECT_EQ(imag.size(), 0);
  125. }
  126. TEST(Polynomial, LinearPolynomialWithPositiveRootWorks) {
  127. const double roots[1] = {42.42};
  128. RunPolynomialTestRealRoots(roots, true, true, kEpsilon);
  129. }
  130. TEST(Polynomial, LinearPolynomialWithNegativeRootWorks) {
  131. const double roots[1] = {-42.42};
  132. RunPolynomialTestRealRoots(roots, true, true, kEpsilon);
  133. }
  134. TEST(Polynomial, QuadraticPolynomialWithPositiveRootsWorks) {
  135. const double roots[2] = {1.0, 42.42};
  136. RunPolynomialTestRealRoots(roots, true, true, kEpsilon);
  137. }
  138. TEST(Polynomial, QuadraticPolynomialWithOneNegativeRootWorks) {
  139. const double roots[2] = {-42.42, 1.0};
  140. RunPolynomialTestRealRoots(roots, true, true, kEpsilon);
  141. }
  142. TEST(Polynomial, QuadraticPolynomialWithTwoNegativeRootsWorks) {
  143. const double roots[2] = {-42.42, -1.0};
  144. RunPolynomialTestRealRoots(roots, true, true, kEpsilon);
  145. }
  146. TEST(Polynomial, QuadraticPolynomialWithCloseRootsWorks) {
  147. const double roots[2] = {42.42, 42.43};
  148. RunPolynomialTestRealRoots(roots, true, false, kEpsilonLoose);
  149. }
  150. TEST(Polynomial, QuadraticPolynomialWithComplexRootsWorks) {
  151. Vector real;
  152. Vector imag;
  153. Vector poly = ConstantPolynomial(1.23);
  154. poly = AddComplexRootPair(poly, 42.42, 4.2);
  155. bool success = FindPolynomialRoots(poly, &real, &imag);
  156. EXPECT_EQ(success, true);
  157. EXPECT_EQ(real.size(), 2);
  158. EXPECT_EQ(imag.size(), 2);
  159. ExpectClose(real(0), 42.42, kEpsilon);
  160. ExpectClose(real(1), 42.42, kEpsilon);
  161. ExpectClose(std::abs(imag(0)), 4.2, kEpsilon);
  162. ExpectClose(std::abs(imag(1)), 4.2, kEpsilon);
  163. ExpectClose(std::abs(imag(0) + imag(1)), 0.0, kEpsilon);
  164. }
  165. TEST(Polynomial, QuarticPolynomialWorks) {
  166. const double roots[4] = {1.23e-4, 1.23e-1, 1.23e+2, 1.23e+5};
  167. RunPolynomialTestRealRoots(roots, true, true, kEpsilon);
  168. }
  169. TEST(Polynomial, QuarticPolynomialWithTwoClustersOfCloseRootsWorks) {
  170. const double roots[4] = {1.23e-1, 2.46e-1, 1.23e+5, 2.46e+5};
  171. RunPolynomialTestRealRoots(roots, true, true, kEpsilonLoose);
  172. }
  173. TEST(Polynomial, QuarticPolynomialWithTwoZeroRootsWorks) {
  174. const double roots[4] = {-42.42, 0.0, 0.0, 42.42};
  175. RunPolynomialTestRealRoots(roots, true, true, 2 * kEpsilonLoose);
  176. }
  177. TEST(Polynomial, QuarticMonomialWorks) {
  178. const double roots[4] = {0.0, 0.0, 0.0, 0.0};
  179. RunPolynomialTestRealRoots(roots, true, true, kEpsilon);
  180. }
  181. TEST(Polynomial, NullPointerAsImaginaryPartWorks) {
  182. const double roots[4] = {1.23e-4, 1.23e-1, 1.23e+2, 1.23e+5};
  183. RunPolynomialTestRealRoots(roots, true, false, kEpsilon);
  184. }
  185. TEST(Polynomial, NullPointerAsRealPartWorks) {
  186. const double roots[4] = {1.23e-4, 1.23e-1, 1.23e+2, 1.23e+5};
  187. RunPolynomialTestRealRoots(roots, false, true, kEpsilon);
  188. }
  189. TEST(Polynomial, BothOutputArgumentsNullWorks) {
  190. const double roots[4] = {1.23e-4, 1.23e-1, 1.23e+2, 1.23e+5};
  191. RunPolynomialTestRealRoots(roots, false, false, kEpsilon);
  192. }
  193. TEST(Polynomial, DifferentiateConstantPolynomial) {
  194. // p(x) = 1;
  195. Vector polynomial(1);
  196. polynomial(0) = 1.0;
  197. const Vector derivative = DifferentiatePolynomial(polynomial);
  198. EXPECT_EQ(derivative.rows(), 1);
  199. EXPECT_EQ(derivative(0), 0);
  200. }
  201. TEST(Polynomial, DifferentiateQuadraticPolynomial) {
  202. // p(x) = x^2 + 2x + 3;
  203. Vector polynomial(3);
  204. polynomial(0) = 1.0;
  205. polynomial(1) = 2.0;
  206. polynomial(2) = 3.0;
  207. const Vector derivative = DifferentiatePolynomial(polynomial);
  208. EXPECT_EQ(derivative.rows(), 2);
  209. EXPECT_EQ(derivative(0), 2.0);
  210. EXPECT_EQ(derivative(1), 2.0);
  211. }
  212. TEST(Polynomial, MinimizeConstantPolynomial) {
  213. // p(x) = 1;
  214. Vector polynomial(1);
  215. polynomial(0) = 1.0;
  216. double optimal_x = 0.0;
  217. double optimal_value = 0.0;
  218. double min_x = 0.0;
  219. double max_x = 1.0;
  220. MinimizePolynomial(polynomial, min_x, max_x, &optimal_x, &optimal_value);
  221. EXPECT_EQ(optimal_value, 1.0);
  222. EXPECT_LE(optimal_x, max_x);
  223. EXPECT_GE(optimal_x, min_x);
  224. }
  225. TEST(Polynomial, MinimizeLinearPolynomial) {
  226. // p(x) = x - 2
  227. Vector polynomial(2);
  228. polynomial(0) = 1.0;
  229. polynomial(1) = 2.0;
  230. double optimal_x = 0.0;
  231. double optimal_value = 0.0;
  232. double min_x = 0.0;
  233. double max_x = 1.0;
  234. MinimizePolynomial(polynomial, min_x, max_x, &optimal_x, &optimal_value);
  235. EXPECT_EQ(optimal_x, 0.0);
  236. EXPECT_EQ(optimal_value, 2.0);
  237. }
  238. TEST(Polynomial, MinimizeQuadraticPolynomial) {
  239. // p(x) = x^2 - 3 x + 2
  240. // min_x = 3/2
  241. // min_value = -1/4;
  242. Vector polynomial(3);
  243. polynomial(0) = 1.0;
  244. polynomial(1) = -3.0;
  245. polynomial(2) = 2.0;
  246. double optimal_x = 0.0;
  247. double optimal_value = 0.0;
  248. double min_x = -2.0;
  249. double max_x = 2.0;
  250. MinimizePolynomial(polynomial, min_x, max_x, &optimal_x, &optimal_value);
  251. EXPECT_EQ(optimal_x, 3.0 / 2.0);
  252. EXPECT_EQ(optimal_value, -1.0 / 4.0);
  253. min_x = -2.0;
  254. max_x = 1.0;
  255. MinimizePolynomial(polynomial, min_x, max_x, &optimal_x, &optimal_value);
  256. EXPECT_EQ(optimal_x, 1.0);
  257. EXPECT_EQ(optimal_value, 0.0);
  258. min_x = 2.0;
  259. max_x = 3.0;
  260. MinimizePolynomial(polynomial, min_x, max_x, &optimal_x, &optimal_value);
  261. EXPECT_EQ(optimal_x, 2.0);
  262. EXPECT_EQ(optimal_value, 0.0);
  263. }
  264. TEST(Polymomial, ConstantInterpolatingPolynomial) {
  265. // p(x) = 1.0
  266. Vector true_polynomial(1);
  267. true_polynomial << 1.0;
  268. std::vector<FunctionSample> samples;
  269. FunctionSample sample;
  270. sample.x = 1.0;
  271. sample.value = 1.0;
  272. sample.value_is_valid = true;
  273. samples.push_back(sample);
  274. const Vector polynomial = FindInterpolatingPolynomial(samples);
  275. EXPECT_NEAR((true_polynomial - polynomial).norm(), 0.0, 1e-15);
  276. }
  277. TEST(Polynomial, LinearInterpolatingPolynomial) {
  278. // p(x) = 2x - 1
  279. Vector true_polynomial(2);
  280. true_polynomial << 2.0, -1.0;
  281. std::vector<FunctionSample> samples;
  282. FunctionSample sample;
  283. sample.x = 1.0;
  284. sample.value = 1.0;
  285. sample.value_is_valid = true;
  286. sample.gradient = 2.0;
  287. sample.gradient_is_valid = true;
  288. samples.push_back(sample);
  289. const Vector polynomial = FindInterpolatingPolynomial(samples);
  290. EXPECT_NEAR((true_polynomial - polynomial).norm(), 0.0, 1e-15);
  291. }
  292. TEST(Polynomial, QuadraticInterpolatingPolynomial) {
  293. // p(x) = 2x^2 + 3x + 2
  294. Vector true_polynomial(3);
  295. true_polynomial << 2.0, 3.0, 2.0;
  296. std::vector<FunctionSample> samples;
  297. {
  298. FunctionSample sample;
  299. sample.x = 1.0;
  300. sample.value = 7.0;
  301. sample.value_is_valid = true;
  302. sample.gradient = 7.0;
  303. sample.gradient_is_valid = true;
  304. samples.push_back(sample);
  305. }
  306. {
  307. FunctionSample sample;
  308. sample.x = -3.0;
  309. sample.value = 11.0;
  310. sample.value_is_valid = true;
  311. samples.push_back(sample);
  312. }
  313. Vector polynomial = FindInterpolatingPolynomial(samples);
  314. EXPECT_NEAR((true_polynomial - polynomial).norm(), 0.0, 1e-15);
  315. }
  316. TEST(Polynomial, DeficientCubicInterpolatingPolynomial) {
  317. // p(x) = 2x^2 + 3x + 2
  318. Vector true_polynomial(4);
  319. true_polynomial << 0.0, 2.0, 3.0, 2.0;
  320. std::vector<FunctionSample> samples;
  321. {
  322. FunctionSample sample;
  323. sample.x = 1.0;
  324. sample.value = 7.0;
  325. sample.value_is_valid = true;
  326. sample.gradient = 7.0;
  327. sample.gradient_is_valid = true;
  328. samples.push_back(sample);
  329. }
  330. {
  331. FunctionSample sample;
  332. sample.x = -3.0;
  333. sample.value = 11.0;
  334. sample.value_is_valid = true;
  335. sample.gradient = -9;
  336. sample.gradient_is_valid = true;
  337. samples.push_back(sample);
  338. }
  339. const Vector polynomial = FindInterpolatingPolynomial(samples);
  340. EXPECT_NEAR((true_polynomial - polynomial).norm(), 0.0, 1e-14);
  341. }
  342. TEST(Polynomial, CubicInterpolatingPolynomialFromValues) {
  343. // p(x) = x^3 + 2x^2 + 3x + 2
  344. Vector true_polynomial(4);
  345. true_polynomial << 1.0, 2.0, 3.0, 2.0;
  346. std::vector<FunctionSample> samples;
  347. {
  348. FunctionSample sample;
  349. sample.x = 1.0;
  350. sample.value = EvaluatePolynomial(true_polynomial, sample.x);
  351. sample.value_is_valid = true;
  352. samples.push_back(sample);
  353. }
  354. {
  355. FunctionSample sample;
  356. sample.x = -3.0;
  357. sample.value = EvaluatePolynomial(true_polynomial, sample.x);
  358. sample.value_is_valid = true;
  359. samples.push_back(sample);
  360. }
  361. {
  362. FunctionSample sample;
  363. sample.x = 2.0;
  364. sample.value = EvaluatePolynomial(true_polynomial, sample.x);
  365. sample.value_is_valid = true;
  366. samples.push_back(sample);
  367. }
  368. {
  369. FunctionSample sample;
  370. sample.x = 0.0;
  371. sample.value = EvaluatePolynomial(true_polynomial, sample.x);
  372. sample.value_is_valid = true;
  373. samples.push_back(sample);
  374. }
  375. const Vector polynomial = FindInterpolatingPolynomial(samples);
  376. EXPECT_NEAR((true_polynomial - polynomial).norm(), 0.0, 1e-14);
  377. }
  378. TEST(Polynomial, CubicInterpolatingPolynomialFromValuesAndOneGradient) {
  379. // p(x) = x^3 + 2x^2 + 3x + 2
  380. Vector true_polynomial(4);
  381. true_polynomial << 1.0, 2.0, 3.0, 2.0;
  382. Vector true_gradient_polynomial = DifferentiatePolynomial(true_polynomial);
  383. std::vector<FunctionSample> samples;
  384. {
  385. FunctionSample sample;
  386. sample.x = 1.0;
  387. sample.value = EvaluatePolynomial(true_polynomial, sample.x);
  388. sample.value_is_valid = true;
  389. samples.push_back(sample);
  390. }
  391. {
  392. FunctionSample sample;
  393. sample.x = -3.0;
  394. sample.value = EvaluatePolynomial(true_polynomial, sample.x);
  395. sample.value_is_valid = true;
  396. samples.push_back(sample);
  397. }
  398. {
  399. FunctionSample sample;
  400. sample.x = 2.0;
  401. sample.value = EvaluatePolynomial(true_polynomial, sample.x);
  402. sample.value_is_valid = true;
  403. sample.gradient = EvaluatePolynomial(true_gradient_polynomial, sample.x);
  404. sample.gradient_is_valid = true;
  405. samples.push_back(sample);
  406. }
  407. const Vector polynomial = FindInterpolatingPolynomial(samples);
  408. EXPECT_NEAR((true_polynomial - polynomial).norm(), 0.0, 1e-14);
  409. }
  410. TEST(Polynomial, CubicInterpolatingPolynomialFromValuesAndGradients) {
  411. // p(x) = x^3 + 2x^2 + 3x + 2
  412. Vector true_polynomial(4);
  413. true_polynomial << 1.0, 2.0, 3.0, 2.0;
  414. Vector true_gradient_polynomial = DifferentiatePolynomial(true_polynomial);
  415. std::vector<FunctionSample> samples;
  416. {
  417. FunctionSample sample;
  418. sample.x = -3.0;
  419. sample.value = EvaluatePolynomial(true_polynomial, sample.x);
  420. sample.value_is_valid = true;
  421. sample.gradient = EvaluatePolynomial(true_gradient_polynomial, sample.x);
  422. sample.gradient_is_valid = true;
  423. samples.push_back(sample);
  424. }
  425. {
  426. FunctionSample sample;
  427. sample.x = 2.0;
  428. sample.value = EvaluatePolynomial(true_polynomial, sample.x);
  429. sample.value_is_valid = true;
  430. sample.gradient = EvaluatePolynomial(true_gradient_polynomial, sample.x);
  431. sample.gradient_is_valid = true;
  432. samples.push_back(sample);
  433. }
  434. const Vector polynomial = FindInterpolatingPolynomial(samples);
  435. EXPECT_NEAR((true_polynomial - polynomial).norm(), 0.0, 1e-14);
  436. }
  437. } // namespace ceres::internal