1 /*
2 Scan Tailor - Interactive post-processing tool for scanned pages.
3 Copyright (C) Joseph Artsimovich <joseph.artsimovich@gmail.com>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <boost/test/auto_unit_test.hpp>
20 #include <boost/test/floating_point_comparison.hpp>
21 #include <cmath>
22 #include <cstdlib>
23 #include "Function.h"
24
25 namespace adiff {
26 namespace tests {
27 BOOST_AUTO_TEST_SUITE(AutomaticDifferentiationTestSuite);
28
BOOST_AUTO_TEST_CASE(test1)29 BOOST_AUTO_TEST_CASE(test1) {
30 // F(x) = x^2 | x = 3
31
32 SparseMap<2> sparse_map(1);
33 sparse_map.markAllNonZero();
34
35 const Function<2> x(0, 3, sparse_map);
36 const Function<2> res(x * x);
37
38 const VecT<double> gradient(res.gradient(sparse_map));
39 const MatT<double> hessian(res.hessian(sparse_map));
40
41 // F = 9
42 // Fx = 2 * x = 6
43 // Fxx = 2
44
45 BOOST_REQUIRE_CLOSE(res.value, 9, 1e-06);
46 BOOST_REQUIRE_CLOSE(gradient[0], 6, 1e-06);
47 BOOST_REQUIRE_CLOSE(hessian(0, 0), 2, 1e-06);
48 }
49
BOOST_AUTO_TEST_CASE(test2)50 BOOST_AUTO_TEST_CASE(test2) {
51 // F(x, y) = x^2 | x = 3
52
53 SparseMap<2> sparse_map(2);
54 sparse_map.markAllNonZero();
55
56 const Function<2> x(0, 3, sparse_map);
57 const Function<2> res(x * x);
58
59 const VecT<double> gradient(res.gradient(sparse_map));
60 const MatT<double> hessian(res.hessian(sparse_map));
61
62 // F = 9
63 // Fx = 2 * x = 6
64 // Fy = 0
65 // Fxx = 2
66 // Fyy = 0
67 // Fxy = 0
68
69 BOOST_REQUIRE_CLOSE(res.value, 9, 1e-06);
70 BOOST_REQUIRE_CLOSE(gradient[0], 6, 1e-06);
71 BOOST_REQUIRE_CLOSE(gradient[1], 0, 1e-06);
72 BOOST_REQUIRE_CLOSE(hessian(0, 0), 2, 1e-06);
73 BOOST_REQUIRE_CLOSE(hessian(1, 0), 0, 1e-06);
74 BOOST_REQUIRE_CLOSE(hessian(0, 1), 0, 1e-06);
75 BOOST_REQUIRE_CLOSE(hessian(1, 1), 0, 1e-06);
76 }
77
BOOST_AUTO_TEST_CASE(test3)78 BOOST_AUTO_TEST_CASE(test3) {
79 // F(x, y) = x^3 * y^2 | x = 2, y = 3
80
81 SparseMap<2> sparse_map(2);
82 sparse_map.markAllNonZero();
83
84 const Function<2> x(0, 2, sparse_map);
85 const Function<2> y(1, 3, sparse_map);
86 const Function<2> xxx(x * x * x);
87 const Function<2> yy(y * y);
88 const Function<2> res(xxx * yy);
89
90 const VecT<double> gradient(res.gradient(sparse_map));
91 const MatT<double> hessian(res.hessian(sparse_map));
92
93 // F = 72
94 // Fx = 3 * x^2 * y^2 = 108
95 // Fy = 2 * y * x^3 = 48
96 // Fxx = 6 * x * y^2 = 108
97 // Fyy = 2 * x^3 = 16
98 // Fyx = 6 * y * x^2 = 72
99
100 BOOST_REQUIRE_CLOSE(res.value, 72, 1e-06);
101 BOOST_REQUIRE_CLOSE(gradient[0], 108, 1e-06);
102 BOOST_REQUIRE_CLOSE(gradient[1], 48, 1e-06);
103 BOOST_REQUIRE_CLOSE(hessian(0, 0), 108, 1e-06);
104 BOOST_REQUIRE_CLOSE(hessian(0, 1), 72, 1e-06);
105 BOOST_REQUIRE_CLOSE(hessian(1, 0), 72, 1e-06);
106 BOOST_REQUIRE_CLOSE(hessian(1, 1), 16, 1e-06);
107 }
108
109 BOOST_AUTO_TEST_SUITE_END();
110 } // namespace tests
111 } // namespace adiff