-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathp3a_unit_tests_search.cpp
More file actions
163 lines (153 loc) · 5.1 KB
/
p3a_unit_tests_search.cpp
File metadata and controls
163 lines (153 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <gtest/gtest.h>
#include "p3a_search.hpp"
inline void search_invert_linear()
{
auto const state_from_domain_value = [] P3A_HOST_DEVICE (double x) -> double {
return x;
};
auto const range_value_from_state = [] P3A_HOST_DEVICE (double x) -> double {
return x;
};
auto const derivative_value_from_state = [] P3A_HOST_DEVICE (double) -> double { return 1.0; };
double const desired_range_value = 0.3;
double const tolerance = 1.0e-6;
double const minimum_domain_value = 0.0;
double const maximum_domain_value = 1.0;
double domain_value;
auto const result = p3a::invert_differentiable_function(
state_from_domain_value,
range_value_from_state,
derivative_value_from_state,
desired_range_value,
tolerance,
minimum_domain_value,
maximum_domain_value,
domain_value);
EXPECT_EQ(result, p3a::search_errc::success);
EXPECT_FLOAT_EQ(domain_value, desired_range_value); // because linear
EXPECT_FLOAT_EQ(range_value_from_state(state_from_domain_value(domain_value)), desired_range_value);
}
TEST(search, invert_linear)
{
search_invert_linear();
}
inline void search_invert_cosine()
{
auto const state_from_domain_value = [] P3A_HOST_DEVICE (double x) -> double {
return x;
};
auto const range_value_from_state = [] P3A_HOST_DEVICE (double x) -> double {
auto const result = std::cos(x);
return result;
};
auto const derivative_value_from_state = [] P3A_HOST_DEVICE (double x) -> double {
auto const result = -std::sin(x);
return result;
};
double const desired_range_value = 0.3;
double const tolerance = 1.0e-6;
double const minimum_domain_value = 0.0;
double const maximum_domain_value = p3a::pi_value<double>();
double domain_value;
auto const result = p3a::invert_differentiable_function(
state_from_domain_value,
range_value_from_state,
derivative_value_from_state,
desired_range_value,
tolerance,
minimum_domain_value,
maximum_domain_value,
domain_value);
EXPECT_EQ(result, p3a::search_errc::success);
EXPECT_FLOAT_EQ(range_value_from_state(state_from_domain_value(domain_value)), desired_range_value);
}
// the point of this test is to have an input where the derivative
// of the function is zero at both endpoints of the subset of the
// domain being searched.
// In this case, Newton's method should not be enough by itself.
TEST(search, invert_cosine)
{
search_invert_cosine();
}
inline void search_invert_non_monotonic()
{
auto const state_from_domain_value = [] P3A_HOST_DEVICE (double x) -> double {
return x;
};
auto const range_value_from_state = [] P3A_HOST_DEVICE (double x) -> double {
auto const result = std::sin(x);
return result;
};
auto const derivative_value_from_state = [] P3A_HOST_DEVICE (double x) -> double {
auto const result = std::cos(x);
return result;
};
double const desired_range_value = 0.3;
double const tolerance = 1.0e-6;
double const minimum_domain_value = (1.0 / 4.0) * p3a::pi_value<double>();
double const maximum_domain_value = (7.0 / 4.0) * p3a::pi_value<double>();
double domain_value;
auto const result = p3a::invert_differentiable_function(
state_from_domain_value,
range_value_from_state,
derivative_value_from_state,
desired_range_value,
tolerance,
minimum_domain_value,
maximum_domain_value,
domain_value);
EXPECT_EQ(result, p3a::search_errc::success);
EXPECT_FLOAT_EQ(range_value_from_state(state_from_domain_value(domain_value)), desired_range_value);
}
// the point of this test is to have an input where the function
// is not monotonic in the subset of the domain specified.
// in fact, Newton's method starting at an endpoint will try to
// leave the subset of the domain in this case.
TEST(search, invert_non_monotonic)
{
search_invert_non_monotonic();
}
TEST(search, tabulated_interval)
{
int constexpr n = 4;
double const table[n] = {1.0, 2.0, 3.0, 4.0};
int i;
auto result = p3a::find_tabulated_interval(n, p3a::iterator_as_functor(table), 2.5, i);
EXPECT_EQ(result, p3a::search_errc::success);
EXPECT_EQ(i, 1);
result = p3a::find_tabulated_interval(n, p3a::iterator_as_functor(table), 1.0, i);
EXPECT_EQ(result, p3a::search_errc::success);
EXPECT_EQ(i, 0);
result = p3a::find_tabulated_interval(n, p3a::iterator_as_functor(table), 4.0, i);
EXPECT_EQ(result, p3a::search_errc::success);
EXPECT_EQ(i, 2);
}
void cos_no_deriv()
{
auto const function =
[] P3A_HOST_DEVICE (double x)
{
return std::sin(x);
};
double const domain_lower_bound = 0.0;
double const domain_upper_bound = 1.5;
double const desired_range_value = 0.9;
double domain_value;
double const tolerance = 1.0e-6;
int const maximum_iterations = 100;
auto const error = p3a::invert_function(
function,
desired_range_value,
domain_value,
domain_lower_bound,
domain_upper_bound,
tolerance,
maximum_iterations);
EXPECT_EQ(error, p3a::search_errc::success);
double const range_value = function(domain_value);
EXPECT_NEAR(range_value, desired_range_value, tolerance);
}
TEST(search, cos_no_deriv)
{
cos_no_deriv();
}