dynamic_autodiff_cost_function_test.cc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 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: thadh@gmail.com (Thad Hughes)
  30. // mierle@gmail.com (Keir Mierle)
  31. // sameeragarwal@google.com (Sameer Agarwal)
  32. #include "ceres/dynamic_autodiff_cost_function.h"
  33. #include <cstddef>
  34. #include <memory>
  35. #include <vector>
  36. #include "gtest/gtest.h"
  37. namespace ceres::internal {
  38. // Takes 2 parameter blocks:
  39. // parameters[0] is size 10.
  40. // parameters[1] is size 5.
  41. // Emits 21 residuals:
  42. // A: i - parameters[0][i], for i in [0,10) -- this is 10 residuals
  43. // B: parameters[0][i] - i, for i in [0,10) -- this is another 10.
  44. // C: sum(parameters[0][i]^2 - 8*parameters[0][i]) + sum(parameters[1][i])
  45. class MyCostFunctor {
  46. public:
  47. template <typename T>
  48. bool operator()(T const* const* parameters, T* residuals) const {
  49. const T* params0 = parameters[0];
  50. int r = 0;
  51. for (int i = 0; i < 10; ++i) {
  52. residuals[r++] = T(i) - params0[i];
  53. residuals[r++] = params0[i] - T(i);
  54. }
  55. T c_residual(0.0);
  56. for (int i = 0; i < 10; ++i) {
  57. c_residual += pow(params0[i], 2) - T(8) * params0[i];
  58. }
  59. const T* params1 = parameters[1];
  60. for (int i = 0; i < 5; ++i) {
  61. c_residual += params1[i];
  62. }
  63. residuals[r++] = c_residual;
  64. return true;
  65. }
  66. };
  67. TEST(DynamicAutodiffCostFunctionTest, TestResiduals) {
  68. std::vector<double> param_block_0(10, 0.0);
  69. std::vector<double> param_block_1(5, 0.0);
  70. DynamicAutoDiffCostFunction<MyCostFunctor, 3> cost_function(
  71. new MyCostFunctor());
  72. cost_function.AddParameterBlock(param_block_0.size());
  73. cost_function.AddParameterBlock(param_block_1.size());
  74. cost_function.SetNumResiduals(21);
  75. // Test residual computation.
  76. std::vector<double> residuals(21, -100000);
  77. std::vector<double*> parameter_blocks(2);
  78. parameter_blocks[0] = &param_block_0[0];
  79. parameter_blocks[1] = &param_block_1[0];
  80. EXPECT_TRUE(
  81. cost_function.Evaluate(&parameter_blocks[0], residuals.data(), nullptr));
  82. for (int r = 0; r < 10; ++r) {
  83. EXPECT_EQ(1.0 * r, residuals.at(r * 2));
  84. EXPECT_EQ(-1.0 * r, residuals.at(r * 2 + 1));
  85. }
  86. EXPECT_EQ(0, residuals.at(20));
  87. }
  88. TEST(DynamicAutodiffCostFunctionTest, TestJacobian) {
  89. // Test the residual counting.
  90. std::vector<double> param_block_0(10, 0.0);
  91. for (int i = 0; i < 10; ++i) {
  92. param_block_0[i] = 2 * i;
  93. }
  94. std::vector<double> param_block_1(5, 0.0);
  95. DynamicAutoDiffCostFunction<MyCostFunctor, 3> cost_function(
  96. new MyCostFunctor());
  97. cost_function.AddParameterBlock(param_block_0.size());
  98. cost_function.AddParameterBlock(param_block_1.size());
  99. cost_function.SetNumResiduals(21);
  100. // Prepare the residuals.
  101. std::vector<double> residuals(21, -100000);
  102. // Prepare the parameters.
  103. std::vector<double*> parameter_blocks(2);
  104. parameter_blocks[0] = &param_block_0[0];
  105. parameter_blocks[1] = &param_block_1[0];
  106. // Prepare the jacobian.
  107. std::vector<std::vector<double>> jacobian_vect(2);
  108. jacobian_vect[0].resize(21 * 10, -100000);
  109. jacobian_vect[1].resize(21 * 5, -100000);
  110. std::vector<double*> jacobian;
  111. jacobian.push_back(jacobian_vect[0].data());
  112. jacobian.push_back(jacobian_vect[1].data());
  113. // Test jacobian computation.
  114. EXPECT_TRUE(cost_function.Evaluate(
  115. parameter_blocks.data(), residuals.data(), jacobian.data()));
  116. for (int r = 0; r < 10; ++r) {
  117. EXPECT_EQ(-1.0 * r, residuals.at(r * 2));
  118. EXPECT_EQ(+1.0 * r, residuals.at(r * 2 + 1));
  119. }
  120. EXPECT_EQ(420, residuals.at(20));
  121. for (int p = 0; p < 10; ++p) {
  122. // Check "A" Jacobian.
  123. EXPECT_EQ(-1.0, jacobian_vect[0][2 * p * 10 + p]);
  124. // Check "B" Jacobian.
  125. EXPECT_EQ(+1.0, jacobian_vect[0][(2 * p + 1) * 10 + p]);
  126. jacobian_vect[0][2 * p * 10 + p] = 0.0;
  127. jacobian_vect[0][(2 * p + 1) * 10 + p] = 0.0;
  128. }
  129. // Check "C" Jacobian for first parameter block.
  130. for (int p = 0; p < 10; ++p) {
  131. EXPECT_EQ(4 * p - 8, jacobian_vect[0][20 * 10 + p]);
  132. jacobian_vect[0][20 * 10 + p] = 0.0;
  133. }
  134. for (double entry : jacobian_vect[0]) {
  135. EXPECT_EQ(0.0, entry);
  136. }
  137. // Check "C" Jacobian for second parameter block.
  138. for (int p = 0; p < 5; ++p) {
  139. EXPECT_EQ(1.0, jacobian_vect[1][20 * 5 + p]);
  140. jacobian_vect[1][20 * 5 + p] = 0.0;
  141. }
  142. for (double entry : jacobian_vect[1]) {
  143. EXPECT_EQ(0.0, entry);
  144. }
  145. }
  146. TEST(DynamicAutodiffCostFunctionTest, JacobianWithFirstParameterBlockConstant) {
  147. // Test the residual counting.
  148. std::vector<double> param_block_0(10, 0.0);
  149. for (int i = 0; i < 10; ++i) {
  150. param_block_0[i] = 2 * i;
  151. }
  152. std::vector<double> param_block_1(5, 0.0);
  153. DynamicAutoDiffCostFunction<MyCostFunctor, 3> cost_function(
  154. new MyCostFunctor());
  155. cost_function.AddParameterBlock(param_block_0.size());
  156. cost_function.AddParameterBlock(param_block_1.size());
  157. cost_function.SetNumResiduals(21);
  158. // Prepare the residuals.
  159. std::vector<double> residuals(21, -100000);
  160. // Prepare the parameters.
  161. std::vector<double*> parameter_blocks(2);
  162. parameter_blocks[0] = &param_block_0[0];
  163. parameter_blocks[1] = &param_block_1[0];
  164. // Prepare the jacobian.
  165. std::vector<std::vector<double>> jacobian_vect(2);
  166. jacobian_vect[0].resize(21 * 10, -100000);
  167. jacobian_vect[1].resize(21 * 5, -100000);
  168. std::vector<double*> jacobian;
  169. jacobian.push_back(nullptr);
  170. jacobian.push_back(jacobian_vect[1].data());
  171. // Test jacobian computation.
  172. EXPECT_TRUE(cost_function.Evaluate(
  173. parameter_blocks.data(), residuals.data(), jacobian.data()));
  174. for (int r = 0; r < 10; ++r) {
  175. EXPECT_EQ(-1.0 * r, residuals.at(r * 2));
  176. EXPECT_EQ(+1.0 * r, residuals.at(r * 2 + 1));
  177. }
  178. EXPECT_EQ(420, residuals.at(20));
  179. // Check "C" Jacobian for second parameter block.
  180. for (int p = 0; p < 5; ++p) {
  181. EXPECT_EQ(1.0, jacobian_vect[1][20 * 5 + p]);
  182. jacobian_vect[1][20 * 5 + p] = 0.0;
  183. }
  184. for (double& i : jacobian_vect[1]) {
  185. EXPECT_EQ(0.0, i);
  186. }
  187. }
  188. TEST(DynamicAutodiffCostFunctionTest,
  189. JacobianWithSecondParameterBlockConstant) { // NOLINT
  190. // Test the residual counting.
  191. std::vector<double> param_block_0(10, 0.0);
  192. for (int i = 0; i < 10; ++i) {
  193. param_block_0[i] = 2 * i;
  194. }
  195. std::vector<double> param_block_1(5, 0.0);
  196. DynamicAutoDiffCostFunction<MyCostFunctor, 3> cost_function(
  197. new MyCostFunctor());
  198. cost_function.AddParameterBlock(param_block_0.size());
  199. cost_function.AddParameterBlock(param_block_1.size());
  200. cost_function.SetNumResiduals(21);
  201. // Prepare the residuals.
  202. std::vector<double> residuals(21, -100000);
  203. // Prepare the parameters.
  204. std::vector<double*> parameter_blocks(2);
  205. parameter_blocks[0] = &param_block_0[0];
  206. parameter_blocks[1] = &param_block_1[0];
  207. // Prepare the jacobian.
  208. std::vector<std::vector<double>> jacobian_vect(2);
  209. jacobian_vect[0].resize(21 * 10, -100000);
  210. jacobian_vect[1].resize(21 * 5, -100000);
  211. std::vector<double*> jacobian;
  212. jacobian.push_back(jacobian_vect[0].data());
  213. jacobian.push_back(nullptr);
  214. // Test jacobian computation.
  215. EXPECT_TRUE(cost_function.Evaluate(
  216. parameter_blocks.data(), residuals.data(), jacobian.data()));
  217. for (int r = 0; r < 10; ++r) {
  218. EXPECT_EQ(-1.0 * r, residuals.at(r * 2));
  219. EXPECT_EQ(+1.0 * r, residuals.at(r * 2 + 1));
  220. }
  221. EXPECT_EQ(420, residuals.at(20));
  222. for (int p = 0; p < 10; ++p) {
  223. // Check "A" Jacobian.
  224. EXPECT_EQ(-1.0, jacobian_vect[0][2 * p * 10 + p]);
  225. // Check "B" Jacobian.
  226. EXPECT_EQ(+1.0, jacobian_vect[0][(2 * p + 1) * 10 + p]);
  227. jacobian_vect[0][2 * p * 10 + p] = 0.0;
  228. jacobian_vect[0][(2 * p + 1) * 10 + p] = 0.0;
  229. }
  230. // Check "C" Jacobian for first parameter block.
  231. for (int p = 0; p < 10; ++p) {
  232. EXPECT_EQ(4 * p - 8, jacobian_vect[0][20 * 10 + p]);
  233. jacobian_vect[0][20 * 10 + p] = 0.0;
  234. }
  235. for (double& i : jacobian_vect[0]) {
  236. EXPECT_EQ(0.0, i);
  237. }
  238. }
  239. // Takes 3 parameter blocks:
  240. // parameters[0] (x) is size 1.
  241. // parameters[1] (y) is size 2.
  242. // parameters[2] (z) is size 3.
  243. // Emits 7 residuals:
  244. // A: x[0] (= sum_x)
  245. // B: y[0] + 2.0 * y[1] (= sum_y)
  246. // C: z[0] + 3.0 * z[1] + 6.0 * z[2] (= sum_z)
  247. // D: sum_x * sum_y
  248. // E: sum_y * sum_z
  249. // F: sum_x * sum_z
  250. // G: sum_x * sum_y * sum_z
  251. class MyThreeParameterCostFunctor {
  252. public:
  253. template <typename T>
  254. bool operator()(T const* const* parameters, T* residuals) const {
  255. const T* x = parameters[0];
  256. const T* y = parameters[1];
  257. const T* z = parameters[2];
  258. T sum_x = x[0];
  259. T sum_y = y[0] + 2.0 * y[1];
  260. T sum_z = z[0] + 3.0 * z[1] + 6.0 * z[2];
  261. residuals[0] = sum_x;
  262. residuals[1] = sum_y;
  263. residuals[2] = sum_z;
  264. residuals[3] = sum_x * sum_y;
  265. residuals[4] = sum_y * sum_z;
  266. residuals[5] = sum_x * sum_z;
  267. residuals[6] = sum_x * sum_y * sum_z;
  268. return true;
  269. }
  270. };
  271. class ThreeParameterCostFunctorTest : public ::testing::Test {
  272. protected:
  273. void SetUp() final {
  274. // Prepare the parameters.
  275. x_.resize(1);
  276. x_[0] = 0.0;
  277. y_.resize(2);
  278. y_[0] = 1.0;
  279. y_[1] = 3.0;
  280. z_.resize(3);
  281. z_[0] = 2.0;
  282. z_[1] = 4.0;
  283. z_[2] = 6.0;
  284. parameter_blocks_.resize(3);
  285. parameter_blocks_[0] = &x_[0];
  286. parameter_blocks_[1] = &y_[0];
  287. parameter_blocks_[2] = &z_[0];
  288. // Prepare the cost function.
  289. using DynamicMyThreeParameterCostFunction =
  290. DynamicAutoDiffCostFunction<MyThreeParameterCostFunctor, 3>;
  291. auto cost_function = std::make_unique<DynamicMyThreeParameterCostFunction>(
  292. new MyThreeParameterCostFunctor());
  293. cost_function->AddParameterBlock(1);
  294. cost_function->AddParameterBlock(2);
  295. cost_function->AddParameterBlock(3);
  296. cost_function->SetNumResiduals(7);
  297. cost_function_ = std::move(cost_function);
  298. // Setup jacobian data.
  299. jacobian_vect_.resize(3);
  300. jacobian_vect_[0].resize(7 * x_.size(), -100000);
  301. jacobian_vect_[1].resize(7 * y_.size(), -100000);
  302. jacobian_vect_[2].resize(7 * z_.size(), -100000);
  303. // Prepare the expected residuals.
  304. const double sum_x = x_[0];
  305. const double sum_y = y_[0] + 2.0 * y_[1];
  306. const double sum_z = z_[0] + 3.0 * z_[1] + 6.0 * z_[2];
  307. expected_residuals_.resize(7);
  308. expected_residuals_[0] = sum_x;
  309. expected_residuals_[1] = sum_y;
  310. expected_residuals_[2] = sum_z;
  311. expected_residuals_[3] = sum_x * sum_y;
  312. expected_residuals_[4] = sum_y * sum_z;
  313. expected_residuals_[5] = sum_x * sum_z;
  314. expected_residuals_[6] = sum_x * sum_y * sum_z;
  315. // Prepare the expected jacobian entries.
  316. expected_jacobian_x_.resize(7);
  317. expected_jacobian_x_[0] = 1.0;
  318. expected_jacobian_x_[1] = 0.0;
  319. expected_jacobian_x_[2] = 0.0;
  320. expected_jacobian_x_[3] = sum_y;
  321. expected_jacobian_x_[4] = 0.0;
  322. expected_jacobian_x_[5] = sum_z;
  323. expected_jacobian_x_[6] = sum_y * sum_z;
  324. expected_jacobian_y_.resize(14);
  325. expected_jacobian_y_[0] = 0.0;
  326. expected_jacobian_y_[1] = 0.0;
  327. expected_jacobian_y_[2] = 1.0;
  328. expected_jacobian_y_[3] = 2.0;
  329. expected_jacobian_y_[4] = 0.0;
  330. expected_jacobian_y_[5] = 0.0;
  331. expected_jacobian_y_[6] = sum_x;
  332. expected_jacobian_y_[7] = 2.0 * sum_x;
  333. expected_jacobian_y_[8] = sum_z;
  334. expected_jacobian_y_[9] = 2.0 * sum_z;
  335. expected_jacobian_y_[10] = 0.0;
  336. expected_jacobian_y_[11] = 0.0;
  337. expected_jacobian_y_[12] = sum_x * sum_z;
  338. expected_jacobian_y_[13] = 2.0 * sum_x * sum_z;
  339. expected_jacobian_z_.resize(21);
  340. expected_jacobian_z_[0] = 0.0;
  341. expected_jacobian_z_[1] = 0.0;
  342. expected_jacobian_z_[2] = 0.0;
  343. expected_jacobian_z_[3] = 0.0;
  344. expected_jacobian_z_[4] = 0.0;
  345. expected_jacobian_z_[5] = 0.0;
  346. expected_jacobian_z_[6] = 1.0;
  347. expected_jacobian_z_[7] = 3.0;
  348. expected_jacobian_z_[8] = 6.0;
  349. expected_jacobian_z_[9] = 0.0;
  350. expected_jacobian_z_[10] = 0.0;
  351. expected_jacobian_z_[11] = 0.0;
  352. expected_jacobian_z_[12] = sum_y;
  353. expected_jacobian_z_[13] = 3.0 * sum_y;
  354. expected_jacobian_z_[14] = 6.0 * sum_y;
  355. expected_jacobian_z_[15] = sum_x;
  356. expected_jacobian_z_[16] = 3.0 * sum_x;
  357. expected_jacobian_z_[17] = 6.0 * sum_x;
  358. expected_jacobian_z_[18] = sum_x * sum_y;
  359. expected_jacobian_z_[19] = 3.0 * sum_x * sum_y;
  360. expected_jacobian_z_[20] = 6.0 * sum_x * sum_y;
  361. }
  362. protected:
  363. std::vector<double> x_;
  364. std::vector<double> y_;
  365. std::vector<double> z_;
  366. std::vector<double*> parameter_blocks_;
  367. std::unique_ptr<CostFunction> cost_function_;
  368. std::vector<std::vector<double>> jacobian_vect_;
  369. std::vector<double> expected_residuals_;
  370. std::vector<double> expected_jacobian_x_;
  371. std::vector<double> expected_jacobian_y_;
  372. std::vector<double> expected_jacobian_z_;
  373. };
  374. TEST_F(ThreeParameterCostFunctorTest, TestThreeParameterResiduals) {
  375. std::vector<double> residuals(7, -100000);
  376. EXPECT_TRUE(cost_function_->Evaluate(
  377. parameter_blocks_.data(), residuals.data(), nullptr));
  378. for (int i = 0; i < 7; ++i) {
  379. EXPECT_EQ(expected_residuals_[i], residuals[i]);
  380. }
  381. }
  382. TEST_F(ThreeParameterCostFunctorTest, TestThreeParameterJacobian) {
  383. std::vector<double> residuals(7, -100000);
  384. std::vector<double*> jacobian;
  385. jacobian.push_back(jacobian_vect_[0].data());
  386. jacobian.push_back(jacobian_vect_[1].data());
  387. jacobian.push_back(jacobian_vect_[2].data());
  388. EXPECT_TRUE(cost_function_->Evaluate(
  389. parameter_blocks_.data(), residuals.data(), jacobian.data()));
  390. for (int i = 0; i < 7; ++i) {
  391. EXPECT_EQ(expected_residuals_[i], residuals[i]);
  392. }
  393. for (int i = 0; i < 7; ++i) {
  394. EXPECT_EQ(expected_jacobian_x_[i], jacobian[0][i]);
  395. }
  396. for (int i = 0; i < 14; ++i) {
  397. EXPECT_EQ(expected_jacobian_y_[i], jacobian[1][i]);
  398. }
  399. for (int i = 0; i < 21; ++i) {
  400. EXPECT_EQ(expected_jacobian_z_[i], jacobian[2][i]);
  401. }
  402. }
  403. TEST_F(ThreeParameterCostFunctorTest,
  404. ThreeParameterJacobianWithFirstAndLastParameterBlockConstant) {
  405. std::vector<double> residuals(7, -100000);
  406. std::vector<double*> jacobian;
  407. jacobian.push_back(nullptr);
  408. jacobian.push_back(jacobian_vect_[1].data());
  409. jacobian.push_back(nullptr);
  410. EXPECT_TRUE(cost_function_->Evaluate(
  411. parameter_blocks_.data(), residuals.data(), jacobian.data()));
  412. for (int i = 0; i < 7; ++i) {
  413. EXPECT_EQ(expected_residuals_[i], residuals[i]);
  414. }
  415. for (int i = 0; i < 14; ++i) {
  416. EXPECT_EQ(expected_jacobian_y_[i], jacobian[1][i]);
  417. }
  418. }
  419. TEST_F(ThreeParameterCostFunctorTest,
  420. ThreeParameterJacobianWithSecondParameterBlockConstant) {
  421. std::vector<double> residuals(7, -100000);
  422. std::vector<double*> jacobian;
  423. jacobian.push_back(jacobian_vect_[0].data());
  424. jacobian.push_back(nullptr);
  425. jacobian.push_back(jacobian_vect_[2].data());
  426. EXPECT_TRUE(cost_function_->Evaluate(
  427. parameter_blocks_.data(), residuals.data(), jacobian.data()));
  428. for (int i = 0; i < 7; ++i) {
  429. EXPECT_EQ(expected_residuals_[i], residuals[i]);
  430. }
  431. for (int i = 0; i < 7; ++i) {
  432. EXPECT_EQ(expected_jacobian_x_[i], jacobian[0][i]);
  433. }
  434. for (int i = 0; i < 21; ++i) {
  435. EXPECT_EQ(expected_jacobian_z_[i], jacobian[2][i]);
  436. }
  437. }
  438. // Takes 6 parameter blocks all of size 1:
  439. // x0, y0, y1, z0, z1, z2
  440. // Same 7 residuals as MyThreeParameterCostFunctor.
  441. // Naming convention for tests is (V)ariable and (C)onstant.
  442. class MySixParameterCostFunctor {
  443. public:
  444. template <typename T>
  445. bool operator()(T const* const* parameters, T* residuals) const {
  446. const T* x0 = parameters[0];
  447. const T* y0 = parameters[1];
  448. const T* y1 = parameters[2];
  449. const T* z0 = parameters[3];
  450. const T* z1 = parameters[4];
  451. const T* z2 = parameters[5];
  452. T sum_x = x0[0];
  453. T sum_y = y0[0] + 2.0 * y1[0];
  454. T sum_z = z0[0] + 3.0 * z1[0] + 6.0 * z2[0];
  455. residuals[0] = sum_x;
  456. residuals[1] = sum_y;
  457. residuals[2] = sum_z;
  458. residuals[3] = sum_x * sum_y;
  459. residuals[4] = sum_y * sum_z;
  460. residuals[5] = sum_x * sum_z;
  461. residuals[6] = sum_x * sum_y * sum_z;
  462. return true;
  463. }
  464. };
  465. class SixParameterCostFunctorTest : public ::testing::Test {
  466. protected:
  467. void SetUp() final {
  468. // Prepare the parameters.
  469. x0_ = 0.0;
  470. y0_ = 1.0;
  471. y1_ = 3.0;
  472. z0_ = 2.0;
  473. z1_ = 4.0;
  474. z2_ = 6.0;
  475. parameter_blocks_.resize(6);
  476. parameter_blocks_[0] = &x0_;
  477. parameter_blocks_[1] = &y0_;
  478. parameter_blocks_[2] = &y1_;
  479. parameter_blocks_[3] = &z0_;
  480. parameter_blocks_[4] = &z1_;
  481. parameter_blocks_[5] = &z2_;
  482. // Prepare the cost function.
  483. using DynamicMySixParameterCostFunction =
  484. DynamicAutoDiffCostFunction<MySixParameterCostFunctor, 3>;
  485. auto cost_function = std::make_unique<DynamicMySixParameterCostFunction>(
  486. new MySixParameterCostFunctor());
  487. for (int i = 0; i < 6; ++i) {
  488. cost_function->AddParameterBlock(1);
  489. }
  490. cost_function->SetNumResiduals(7);
  491. cost_function_ = std::move(cost_function);
  492. // Setup jacobian data.
  493. jacobian_vect_.resize(6);
  494. for (int i = 0; i < 6; ++i) {
  495. jacobian_vect_[i].resize(7, -100000);
  496. }
  497. // Prepare the expected residuals.
  498. const double sum_x = x0_;
  499. const double sum_y = y0_ + 2.0 * y1_;
  500. const double sum_z = z0_ + 3.0 * z1_ + 6.0 * z2_;
  501. expected_residuals_.resize(7);
  502. expected_residuals_[0] = sum_x;
  503. expected_residuals_[1] = sum_y;
  504. expected_residuals_[2] = sum_z;
  505. expected_residuals_[3] = sum_x * sum_y;
  506. expected_residuals_[4] = sum_y * sum_z;
  507. expected_residuals_[5] = sum_x * sum_z;
  508. expected_residuals_[6] = sum_x * sum_y * sum_z;
  509. // Prepare the expected jacobian entries.
  510. expected_jacobians_.resize(6);
  511. expected_jacobians_[0].resize(7);
  512. expected_jacobians_[0][0] = 1.0;
  513. expected_jacobians_[0][1] = 0.0;
  514. expected_jacobians_[0][2] = 0.0;
  515. expected_jacobians_[0][3] = sum_y;
  516. expected_jacobians_[0][4] = 0.0;
  517. expected_jacobians_[0][5] = sum_z;
  518. expected_jacobians_[0][6] = sum_y * sum_z;
  519. expected_jacobians_[1].resize(7);
  520. expected_jacobians_[1][0] = 0.0;
  521. expected_jacobians_[1][1] = 1.0;
  522. expected_jacobians_[1][2] = 0.0;
  523. expected_jacobians_[1][3] = sum_x;
  524. expected_jacobians_[1][4] = sum_z;
  525. expected_jacobians_[1][5] = 0.0;
  526. expected_jacobians_[1][6] = sum_x * sum_z;
  527. expected_jacobians_[2].resize(7);
  528. expected_jacobians_[2][0] = 0.0;
  529. expected_jacobians_[2][1] = 2.0;
  530. expected_jacobians_[2][2] = 0.0;
  531. expected_jacobians_[2][3] = 2.0 * sum_x;
  532. expected_jacobians_[2][4] = 2.0 * sum_z;
  533. expected_jacobians_[2][5] = 0.0;
  534. expected_jacobians_[2][6] = 2.0 * sum_x * sum_z;
  535. expected_jacobians_[3].resize(7);
  536. expected_jacobians_[3][0] = 0.0;
  537. expected_jacobians_[3][1] = 0.0;
  538. expected_jacobians_[3][2] = 1.0;
  539. expected_jacobians_[3][3] = 0.0;
  540. expected_jacobians_[3][4] = sum_y;
  541. expected_jacobians_[3][5] = sum_x;
  542. expected_jacobians_[3][6] = sum_x * sum_y;
  543. expected_jacobians_[4].resize(7);
  544. expected_jacobians_[4][0] = 0.0;
  545. expected_jacobians_[4][1] = 0.0;
  546. expected_jacobians_[4][2] = 3.0;
  547. expected_jacobians_[4][3] = 0.0;
  548. expected_jacobians_[4][4] = 3.0 * sum_y;
  549. expected_jacobians_[4][5] = 3.0 * sum_x;
  550. expected_jacobians_[4][6] = 3.0 * sum_x * sum_y;
  551. expected_jacobians_[5].resize(7);
  552. expected_jacobians_[5][0] = 0.0;
  553. expected_jacobians_[5][1] = 0.0;
  554. expected_jacobians_[5][2] = 6.0;
  555. expected_jacobians_[5][3] = 0.0;
  556. expected_jacobians_[5][4] = 6.0 * sum_y;
  557. expected_jacobians_[5][5] = 6.0 * sum_x;
  558. expected_jacobians_[5][6] = 6.0 * sum_x * sum_y;
  559. }
  560. protected:
  561. double x0_;
  562. double y0_;
  563. double y1_;
  564. double z0_;
  565. double z1_;
  566. double z2_;
  567. std::vector<double*> parameter_blocks_;
  568. std::unique_ptr<CostFunction> cost_function_;
  569. std::vector<std::vector<double>> jacobian_vect_;
  570. std::vector<double> expected_residuals_;
  571. std::vector<std::vector<double>> expected_jacobians_;
  572. };
  573. TEST_F(SixParameterCostFunctorTest, TestSixParameterResiduals) {
  574. std::vector<double> residuals(7, -100000);
  575. EXPECT_TRUE(cost_function_->Evaluate(
  576. parameter_blocks_.data(), residuals.data(), nullptr));
  577. for (int i = 0; i < 7; ++i) {
  578. EXPECT_EQ(expected_residuals_[i], residuals[i]);
  579. }
  580. }
  581. TEST_F(SixParameterCostFunctorTest, TestSixParameterJacobian) {
  582. std::vector<double> residuals(7, -100000);
  583. std::vector<double*> jacobian;
  584. jacobian.push_back(jacobian_vect_[0].data());
  585. jacobian.push_back(jacobian_vect_[1].data());
  586. jacobian.push_back(jacobian_vect_[2].data());
  587. jacobian.push_back(jacobian_vect_[3].data());
  588. jacobian.push_back(jacobian_vect_[4].data());
  589. jacobian.push_back(jacobian_vect_[5].data());
  590. EXPECT_TRUE(cost_function_->Evaluate(
  591. parameter_blocks_.data(), residuals.data(), jacobian.data()));
  592. for (int i = 0; i < 7; ++i) {
  593. EXPECT_EQ(expected_residuals_[i], residuals[i]);
  594. }
  595. for (int i = 0; i < 6; ++i) {
  596. for (int j = 0; j < 7; ++j) {
  597. EXPECT_EQ(expected_jacobians_[i][j], jacobian[i][j]);
  598. }
  599. }
  600. }
  601. TEST_F(SixParameterCostFunctorTest, TestSixParameterJacobianVVCVVC) {
  602. std::vector<double> residuals(7, -100000);
  603. std::vector<double*> jacobian;
  604. jacobian.push_back(jacobian_vect_[0].data());
  605. jacobian.push_back(jacobian_vect_[1].data());
  606. jacobian.push_back(nullptr);
  607. jacobian.push_back(jacobian_vect_[3].data());
  608. jacobian.push_back(jacobian_vect_[4].data());
  609. jacobian.push_back(nullptr);
  610. EXPECT_TRUE(cost_function_->Evaluate(
  611. parameter_blocks_.data(), residuals.data(), jacobian.data()));
  612. for (int i = 0; i < 7; ++i) {
  613. EXPECT_EQ(expected_residuals_[i], residuals[i]);
  614. }
  615. for (int i = 0; i < 6; ++i) {
  616. // Skip the constant variables.
  617. if (i == 2 || i == 5) {
  618. continue;
  619. }
  620. for (int j = 0; j < 7; ++j) {
  621. EXPECT_EQ(expected_jacobians_[i][j], jacobian[i][j]);
  622. }
  623. }
  624. }
  625. TEST_F(SixParameterCostFunctorTest, TestSixParameterJacobianVCCVCV) {
  626. std::vector<double> residuals(7, -100000);
  627. std::vector<double*> jacobian;
  628. jacobian.push_back(jacobian_vect_[0].data());
  629. jacobian.push_back(nullptr);
  630. jacobian.push_back(nullptr);
  631. jacobian.push_back(jacobian_vect_[3].data());
  632. jacobian.push_back(nullptr);
  633. jacobian.push_back(jacobian_vect_[5].data());
  634. EXPECT_TRUE(cost_function_->Evaluate(
  635. parameter_blocks_.data(), residuals.data(), jacobian.data()));
  636. for (int i = 0; i < 7; ++i) {
  637. EXPECT_EQ(expected_residuals_[i], residuals[i]);
  638. }
  639. for (int i = 0; i < 6; ++i) {
  640. // Skip the constant variables.
  641. if (i == 1 || i == 2 || i == 4) {
  642. continue;
  643. }
  644. for (int j = 0; j < 7; ++j) {
  645. EXPECT_EQ(expected_jacobians_[i][j], jacobian[i][j]);
  646. }
  647. }
  648. }
  649. class ValueError {
  650. public:
  651. explicit ValueError(double target_value) : target_value_(target_value) {}
  652. template <typename T>
  653. bool operator()(const T* value, T* residual) const {
  654. *residual = *value - T(target_value_);
  655. return true;
  656. }
  657. protected:
  658. double target_value_;
  659. };
  660. class DynamicValueError {
  661. public:
  662. explicit DynamicValueError(double target_value)
  663. : target_value_(target_value) {}
  664. template <typename T>
  665. bool operator()(T const* const* parameters, T* residual) const {
  666. residual[0] = T(target_value_) - parameters[0][0];
  667. return true;
  668. }
  669. protected:
  670. double target_value_;
  671. };
  672. TEST(DynamicAutoDiffCostFunction,
  673. EvaluateWithEmptyJacobiansArrayComputesResidual) {
  674. const double target_value = 1.0;
  675. double parameter = 0;
  676. ceres::DynamicAutoDiffCostFunction<DynamicValueError, 1> cost_function(
  677. new DynamicValueError(target_value));
  678. cost_function.AddParameterBlock(1);
  679. cost_function.SetNumResiduals(1);
  680. double* parameter_blocks[1] = {&parameter};
  681. double* jacobians[1] = {nullptr};
  682. double residual;
  683. EXPECT_TRUE(cost_function.Evaluate(parameter_blocks, &residual, jacobians));
  684. EXPECT_EQ(residual, target_value);
  685. }
  686. TEST(DynamicAutoDiffCostFunctionTest, DeductionTemplateCompilationTest) {
  687. // Ensure deduction guide to be working
  688. (void)DynamicAutoDiffCostFunction(new MyCostFunctor());
  689. }
  690. } // namespace ceres::internal