blob: 2bf4009164f64a31f7945cb5c740b4d412ccbeef [file] [log] [blame]
Keir Mierle8ebb0732012-04-30 23:09:08 -07001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2010, 2011, 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: keir@google.com (Keir Mierle)
30//
31// Utility functions useful for testing.
32
33#include <cmath>
34#include <glog/logging.h>
35#include "gtest/gtest.h"
36#include "ceres/stringprintf.h"
37
38namespace ceres {
39namespace internal {
40
41bool ExpectClose(double x, double y, double max_abs_relative_difference) {
42 double absolute_difference = fabs(x - y);
43 double relative_difference = absolute_difference / std::max(fabs(x), fabs(y));
44 if (x == 0 || y == 0) {
45 // If x or y is exactly zero, then relative difference doesn't have any
46 // meaning. Take the absolute difference instead.
47 relative_difference = absolute_difference;
48 }
49 if (relative_difference > max_abs_relative_difference) {
50 VLOG(1) << StringPrintf("x=%17g y=%17g abs=%17g rel=%17g",
51 x, y, absolute_difference, relative_difference);
52 }
53
54 EXPECT_NEAR(relative_difference, 0.0, max_abs_relative_difference);
55 return relative_difference <= max_abs_relative_difference;
56}
57
58void ExpectArraysCloseUptoScale(int n,
59 const double* p,
60 const double* q,
61 double tol) {
62 CHECK_GT(n, 0);
63 CHECK(p);
64 CHECK(q);
65
66 double p_max = 0;
67 double q_max = 0;
68 int p_i = 0;
69 int q_i = 0;
70
71 for (int i = 0; i < n; ++i) {
72 if (std::abs(p[i]) > p_max) {
73 p_max = std::abs(p[i]);
74 p_i = i;
75 }
76 if (std::abs(q[i]) > q_max) {
77 q_max = std::abs(q[i]);
78 q_i = i;
79 }
80 }
81
82 // If both arrays are all zeros, they are equal up to scale, but
83 // for testing purposes, that's more likely to be an error than
84 // a desired result.
85 CHECK_NE(p_max, 0.0);
86 CHECK_NE(q_max, 0.0);
87
88 for (int i = 0; i < n; ++i) {
89 double p_norm = p[i] / p[p_i];
90 double q_norm = q[i] / q[q_i];
91
92 EXPECT_NEAR(p_norm, q_norm, tol) << "i=" << i;
93 }
94}
95
96void ExpectArraysClose(int n,
97 const double* p,
98 const double* q,
99 double tol) {
100 CHECK_GT(n, 0);
101 CHECK(p);
102 CHECK(q);
103
104 for (int i = 0; i < n; ++i) {
105 EXPECT_NEAR(p[i], q[i], tol) << "i=" << i;
106 }
107}
108
109} // namespace internal
110} // namespace ceres