Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 2 | // Copyright 2023 Google Inc. All rights reserved. |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 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: keir@google.com (Keir Mierle) |
| 30 | // |
| 31 | // Utility functions useful for testing. |
| 32 | |
| 33 | #include "ceres/test_util.h" |
| 34 | |
| 35 | #include <algorithm> |
| 36 | #include <cmath> |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 37 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 38 | #include "ceres/file.h" |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 39 | #include "ceres/internal/port.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 40 | #include "ceres/stringprintf.h" |
| 41 | #include "ceres/types.h" |
| 42 | #include "gflags/gflags.h" |
| 43 | #include "glog/logging.h" |
| 44 | #include "gtest/gtest.h" |
| 45 | |
| 46 | DECLARE_string(test_srcdir); |
| 47 | |
| 48 | // This macro is used to inject additional path information specific |
| 49 | // to the build system. |
| 50 | |
| 51 | #ifndef CERES_TEST_SRCDIR_SUFFIX |
| 52 | #define CERES_TEST_SRCDIR_SUFFIX "" |
| 53 | #endif |
| 54 | |
| 55 | namespace ceres { |
| 56 | namespace internal { |
| 57 | |
| 58 | bool ExpectClose(double x, double y, double max_abs_relative_difference) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 59 | if (std::isinf(x) && std::isinf(y)) { |
| 60 | EXPECT_EQ(std::signbit(x), std::signbit(y)); |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | if (std::isnan(x) && std::isnan(y)) { |
| 65 | return true; |
| 66 | } |
| 67 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 68 | double absolute_difference = fabs(x - y); |
| 69 | double relative_difference = absolute_difference / std::max(fabs(x), fabs(y)); |
| 70 | if (x == 0 || y == 0) { |
| 71 | // If x or y is exactly zero, then relative difference doesn't have any |
| 72 | // meaning. Take the absolute difference instead. |
| 73 | relative_difference = absolute_difference; |
| 74 | } |
| 75 | if (relative_difference > max_abs_relative_difference) { |
| 76 | VLOG(1) << StringPrintf("x=%17g y=%17g abs=%17g rel=%17g", |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 77 | x, |
| 78 | y, |
| 79 | absolute_difference, |
| 80 | relative_difference); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | EXPECT_NEAR(relative_difference, 0.0, max_abs_relative_difference); |
| 84 | return relative_difference <= max_abs_relative_difference; |
| 85 | } |
| 86 | |
| 87 | void ExpectArraysCloseUptoScale(int n, |
| 88 | const double* p, |
| 89 | const double* q, |
| 90 | double tol) { |
| 91 | CHECK_GT(n, 0); |
| 92 | CHECK(p); |
| 93 | CHECK(q); |
| 94 | |
| 95 | double p_max = 0; |
| 96 | double q_max = 0; |
| 97 | int p_i = 0; |
| 98 | int q_i = 0; |
| 99 | |
| 100 | for (int i = 0; i < n; ++i) { |
| 101 | if (std::abs(p[i]) > p_max) { |
| 102 | p_max = std::abs(p[i]); |
| 103 | p_i = i; |
| 104 | } |
| 105 | if (std::abs(q[i]) > q_max) { |
| 106 | q_max = std::abs(q[i]); |
| 107 | q_i = i; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // If both arrays are all zeros, they are equal up to scale, but |
| 112 | // for testing purposes, that's more likely to be an error than |
| 113 | // a desired result. |
| 114 | CHECK_NE(p_max, 0.0); |
| 115 | CHECK_NE(q_max, 0.0); |
| 116 | |
| 117 | for (int i = 0; i < n; ++i) { |
| 118 | double p_norm = p[i] / p[p_i]; |
| 119 | double q_norm = q[i] / q[q_i]; |
| 120 | |
| 121 | EXPECT_NEAR(p_norm, q_norm, tol) << "i=" << i; |
| 122 | } |
| 123 | } |
| 124 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 125 | void ExpectArraysClose(int n, const double* p, const double* q, double tol) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 126 | CHECK_GT(n, 0); |
| 127 | CHECK(p); |
| 128 | CHECK(q); |
| 129 | |
| 130 | for (int i = 0; i < n; ++i) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 131 | EXPECT_TRUE(ExpectClose(p[i], q[i], tol)) << "p[" << i << "]" << p[i] << " " |
| 132 | << "q[" << i << "]" << q[i] << " " |
| 133 | << "tol: " << tol; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | std::string TestFileAbsolutePath(const std::string& filename) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 138 | return JoinPath(CERES_GET_FLAG(FLAGS_test_srcdir) + CERES_TEST_SRCDIR_SUFFIX, |
| 139 | filename); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | std::string ToString(const Solver::Options& options) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 143 | return StringPrintf("(%s, %s, %s, %s, %d)", |
| 144 | LinearSolverTypeToString(options.linear_solver_type), |
| 145 | SparseLinearAlgebraLibraryTypeToString( |
| 146 | options.sparse_linear_algebra_library_type), |
| 147 | options.linear_solver_ordering ? "USER" : "AUTOMATIC", |
| 148 | PreconditionerTypeToString(options.preconditioner_type), |
| 149 | options.num_threads); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | } // namespace internal |
| 153 | } // namespace ceres |