c@348
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
c@348
|
2
|
c@348
|
3 #include "maths/MathUtilities.h"
|
c@348
|
4
|
c@348
|
5 #include <cmath>
|
c@418
|
6 #include <iostream>
|
c@348
|
7
|
c@348
|
8 #define BOOST_TEST_DYN_LINK
|
c@348
|
9 #define BOOST_TEST_MAIN
|
c@348
|
10
|
c@348
|
11 #include <boost/test/unit_test.hpp>
|
c@348
|
12
|
c@418
|
13 using namespace std;
|
c@418
|
14
|
c@348
|
15 BOOST_AUTO_TEST_SUITE(TestMathUtilities)
|
c@348
|
16
|
c@348
|
17 BOOST_AUTO_TEST_CASE(round)
|
c@348
|
18 {
|
c@348
|
19 BOOST_CHECK_EQUAL(MathUtilities::round(0.5), 1.0);
|
c@348
|
20 BOOST_CHECK_EQUAL(MathUtilities::round(0.49), 0.0);
|
c@348
|
21 BOOST_CHECK_EQUAL(MathUtilities::round(0.99), 1.0);
|
c@348
|
22 BOOST_CHECK_EQUAL(MathUtilities::round(0.01), 0.0);
|
c@348
|
23 BOOST_CHECK_EQUAL(MathUtilities::round(0.0), 0.0);
|
c@348
|
24 BOOST_CHECK_EQUAL(MathUtilities::round(100.0), 100.0);
|
c@348
|
25 BOOST_CHECK_EQUAL(MathUtilities::round(-0.2), 0.0);
|
c@348
|
26 BOOST_CHECK_EQUAL(MathUtilities::round(-0.5), -1.0);
|
c@348
|
27 BOOST_CHECK_EQUAL(MathUtilities::round(-0.99), -1.0);
|
c@348
|
28 BOOST_CHECK_EQUAL(MathUtilities::round(-1.0), -1.0);
|
c@348
|
29 BOOST_CHECK_EQUAL(MathUtilities::round(-1.1), -1.0);
|
c@348
|
30 BOOST_CHECK_EQUAL(MathUtilities::round(-1.5), -2.0);
|
c@348
|
31 }
|
c@348
|
32
|
c@348
|
33 BOOST_AUTO_TEST_CASE(mean)
|
c@348
|
34 {
|
c@348
|
35 BOOST_CHECK_EQUAL(MathUtilities::mean(0, 0), 0);
|
c@348
|
36 double d0[] = { 0, 4, 3, -1 };
|
c@348
|
37 BOOST_CHECK_EQUAL(MathUtilities::mean(d0, 4), 1.5);
|
c@348
|
38 double d1[] = { -2.6 };
|
c@348
|
39 BOOST_CHECK_EQUAL(MathUtilities::mean(d1, 1), -2.6);
|
c@418
|
40 vector<double> v;
|
c@348
|
41 v.push_back(0);
|
c@348
|
42 v.push_back(4);
|
c@348
|
43 v.push_back(3);
|
c@348
|
44 v.push_back(-1);
|
c@348
|
45 BOOST_CHECK_EQUAL(MathUtilities::mean(v, 0, 4), 1.5);
|
c@348
|
46 BOOST_CHECK_EQUAL(MathUtilities::mean(v, 1, 2), 3.5);
|
c@348
|
47 BOOST_CHECK_EQUAL(MathUtilities::mean(v, 3, 1), -1);
|
c@348
|
48 BOOST_CHECK_EQUAL(MathUtilities::mean(v, 3, 0), 0);
|
c@348
|
49 }
|
c@348
|
50
|
c@348
|
51 BOOST_AUTO_TEST_CASE(sum)
|
c@348
|
52 {
|
c@348
|
53 BOOST_CHECK_EQUAL(MathUtilities::sum(0, 0), 0);
|
c@348
|
54 double d0[] = { 0, 4, 3, -1 };
|
c@348
|
55 BOOST_CHECK_EQUAL(MathUtilities::sum(d0, 4), 6);
|
c@348
|
56 double d1[] = { -2.6 };
|
c@348
|
57 BOOST_CHECK_EQUAL(MathUtilities::sum(d1, 1), -2.6);
|
c@348
|
58 }
|
c@348
|
59
|
c@348
|
60 BOOST_AUTO_TEST_CASE(median)
|
c@348
|
61 {
|
c@348
|
62 BOOST_CHECK_EQUAL(MathUtilities::median(0, 0), 0);
|
c@348
|
63 double d0[] = { 0, 4, 3, -1 };
|
c@348
|
64 BOOST_CHECK_EQUAL(MathUtilities::median(d0, 4), 1.5);
|
c@348
|
65 double d1[] = { 0, 4, 3, -1, -1 };
|
c@348
|
66 BOOST_CHECK_EQUAL(MathUtilities::median(d1, 5), 0);
|
c@348
|
67 double d2[] = { 1.0, -2.0 };
|
c@348
|
68 BOOST_CHECK_EQUAL(MathUtilities::median(d2, 2), -0.5);
|
c@348
|
69 double d3[] = { -2.6 };
|
c@348
|
70 BOOST_CHECK_EQUAL(MathUtilities::median(d3, 1), -2.6);
|
c@348
|
71 }
|
c@348
|
72
|
c@348
|
73 BOOST_AUTO_TEST_CASE(princarg)
|
c@348
|
74 {
|
c@348
|
75 BOOST_CHECK_EQUAL(MathUtilities::princarg(M_PI), M_PI);
|
c@348
|
76 BOOST_CHECK_EQUAL(MathUtilities::princarg(-M_PI), M_PI);
|
c@348
|
77 BOOST_CHECK_EQUAL(MathUtilities::princarg(2 * M_PI), 0.0);
|
c@348
|
78 BOOST_CHECK_EQUAL(MathUtilities::princarg(5 * M_PI), M_PI);
|
c@348
|
79 BOOST_CHECK_EQUAL(MathUtilities::princarg(1.0), 1.0);
|
c@348
|
80 BOOST_CHECK_EQUAL(MathUtilities::princarg(-1.0), -1.0);
|
c@348
|
81 BOOST_CHECK_EQUAL(MathUtilities::princarg(-10.0), -10.0 + 4 * M_PI);
|
c@348
|
82 }
|
c@348
|
83
|
c@348
|
84 BOOST_AUTO_TEST_CASE(isPowerOfTwo)
|
c@348
|
85 {
|
c@348
|
86 BOOST_CHECK_EQUAL(MathUtilities::isPowerOfTwo(0), false);
|
c@348
|
87 BOOST_CHECK_EQUAL(MathUtilities::isPowerOfTwo(1), true);
|
c@348
|
88 BOOST_CHECK_EQUAL(MathUtilities::isPowerOfTwo(-2), false);
|
c@348
|
89 BOOST_CHECK_EQUAL(MathUtilities::isPowerOfTwo(2), true);
|
c@348
|
90 BOOST_CHECK_EQUAL(MathUtilities::isPowerOfTwo(3), false);
|
c@348
|
91 BOOST_CHECK_EQUAL(MathUtilities::isPowerOfTwo(12), false);
|
c@348
|
92 BOOST_CHECK_EQUAL(MathUtilities::isPowerOfTwo(16), true);
|
c@348
|
93 }
|
c@348
|
94
|
c@348
|
95 BOOST_AUTO_TEST_CASE(nextPowerOfTwo)
|
c@348
|
96 {
|
c@348
|
97 BOOST_CHECK_EQUAL(MathUtilities::nextPowerOfTwo(0), 1);
|
c@348
|
98 BOOST_CHECK_EQUAL(MathUtilities::nextPowerOfTwo(1), 1);
|
c@348
|
99 BOOST_CHECK_EQUAL(MathUtilities::nextPowerOfTwo(-2), 1);
|
c@348
|
100 BOOST_CHECK_EQUAL(MathUtilities::nextPowerOfTwo(2), 2);
|
c@348
|
101 BOOST_CHECK_EQUAL(MathUtilities::nextPowerOfTwo(3), 4);
|
c@348
|
102 BOOST_CHECK_EQUAL(MathUtilities::nextPowerOfTwo(12), 16);
|
c@348
|
103 BOOST_CHECK_EQUAL(MathUtilities::nextPowerOfTwo(16), 16);
|
c@348
|
104 }
|
c@348
|
105
|
c@348
|
106 BOOST_AUTO_TEST_CASE(previousPowerOfTwo)
|
c@348
|
107 {
|
c@348
|
108 BOOST_CHECK_EQUAL(MathUtilities::previousPowerOfTwo(0), 1);
|
c@348
|
109 BOOST_CHECK_EQUAL(MathUtilities::previousPowerOfTwo(1), 1);
|
c@348
|
110 BOOST_CHECK_EQUAL(MathUtilities::previousPowerOfTwo(-2), 1);
|
c@348
|
111 BOOST_CHECK_EQUAL(MathUtilities::previousPowerOfTwo(2), 2);
|
c@348
|
112 BOOST_CHECK_EQUAL(MathUtilities::previousPowerOfTwo(3), 2);
|
c@348
|
113 BOOST_CHECK_EQUAL(MathUtilities::previousPowerOfTwo(12), 8);
|
c@348
|
114 BOOST_CHECK_EQUAL(MathUtilities::previousPowerOfTwo(16), 16);
|
c@348
|
115 }
|
c@348
|
116
|
c@348
|
117 BOOST_AUTO_TEST_CASE(nearestPowerOfTwo)
|
c@348
|
118 {
|
c@348
|
119 BOOST_CHECK_EQUAL(MathUtilities::nearestPowerOfTwo(0), 1);
|
c@348
|
120 BOOST_CHECK_EQUAL(MathUtilities::nearestPowerOfTwo(1), 1);
|
c@348
|
121 BOOST_CHECK_EQUAL(MathUtilities::nearestPowerOfTwo(-2), 1);
|
c@348
|
122 BOOST_CHECK_EQUAL(MathUtilities::nearestPowerOfTwo(2), 2);
|
c@348
|
123 BOOST_CHECK_EQUAL(MathUtilities::nearestPowerOfTwo(3), 4);
|
c@348
|
124 BOOST_CHECK_EQUAL(MathUtilities::nearestPowerOfTwo(11), 8);
|
c@348
|
125 BOOST_CHECK_EQUAL(MathUtilities::nearestPowerOfTwo(12), 16);
|
c@348
|
126 BOOST_CHECK_EQUAL(MathUtilities::nearestPowerOfTwo(16), 16);
|
c@348
|
127 }
|
c@348
|
128
|
c@348
|
129 BOOST_AUTO_TEST_CASE(factorial)
|
c@348
|
130 {
|
c@360
|
131 BOOST_CHECK_EQUAL(MathUtilities::factorial(-10), 0.0);
|
c@360
|
132 BOOST_CHECK_EQUAL(MathUtilities::factorial(0), 1.0);
|
c@360
|
133 BOOST_CHECK_EQUAL(MathUtilities::factorial(1), 1.0);
|
c@360
|
134 BOOST_CHECK_EQUAL(MathUtilities::factorial(2), 2.0);
|
c@360
|
135 BOOST_CHECK_EQUAL(MathUtilities::factorial(3), 6.0);
|
c@360
|
136 BOOST_CHECK_EQUAL(MathUtilities::factorial(4), 24.0);
|
c@360
|
137
|
c@360
|
138 // Too big for an int, hence double return value from factorial
|
c@360
|
139 BOOST_CHECK_EQUAL(MathUtilities::factorial(20), 2432902008176640000.0);
|
c@348
|
140 }
|
c@348
|
141
|
c@350
|
142 BOOST_AUTO_TEST_CASE(gcd)
|
c@350
|
143 {
|
c@350
|
144 BOOST_CHECK_EQUAL(MathUtilities::gcd(1, 1), 1);
|
c@350
|
145 BOOST_CHECK_EQUAL(MathUtilities::gcd(2, 1), 1);
|
c@350
|
146 BOOST_CHECK_EQUAL(MathUtilities::gcd(2, 3), 1);
|
c@350
|
147 BOOST_CHECK_EQUAL(MathUtilities::gcd(4, 2), 2);
|
c@350
|
148 BOOST_CHECK_EQUAL(MathUtilities::gcd(18, 24), 6);
|
c@350
|
149 BOOST_CHECK_EQUAL(MathUtilities::gcd(27, 18), 9);
|
c@350
|
150 BOOST_CHECK_EQUAL(MathUtilities::gcd(18, 36), 18);
|
c@350
|
151 BOOST_CHECK_EQUAL(MathUtilities::gcd(37, 18), 1);
|
c@350
|
152 }
|
c@350
|
153
|
c@418
|
154 BOOST_AUTO_TEST_CASE(getAlphaNorm1)
|
c@418
|
155 {
|
c@418
|
156 vector<double> in { -1, 1.5, 3, 5 };
|
c@418
|
157 double out = MathUtilities::getAlphaNorm(in, 1);
|
c@418
|
158 double expected = 2.6250;
|
c@418
|
159 double thresh = 1e-4;
|
c@418
|
160 BOOST_CHECK_SMALL(out - expected, thresh);
|
c@418
|
161 }
|
c@418
|
162
|
c@418
|
163 BOOST_AUTO_TEST_CASE(getAlphaNorm2)
|
c@418
|
164 {
|
c@418
|
165 vector<double> in { -1, 1.5, 3, 5 };
|
c@418
|
166 double out = MathUtilities::getAlphaNorm(in, 2);
|
c@418
|
167 double expected = 3.0516;
|
c@418
|
168 double thresh = 1e-4;
|
c@418
|
169 BOOST_CHECK_SMALL(out - expected, thresh);
|
c@418
|
170 }
|
c@418
|
171
|
c@418
|
172 BOOST_AUTO_TEST_CASE(getL1Norm)
|
c@418
|
173 {
|
c@418
|
174 vector<double> in { -1, 1.5, 3, 5 };
|
c@418
|
175 double out = MathUtilities::getLpNorm(in, 1);
|
c@418
|
176 // L1 norm is the sum of magnitudes
|
c@418
|
177 double expected = 10.5;
|
c@418
|
178 double thresh = 1e-5;
|
c@418
|
179 BOOST_CHECK_SMALL(out - expected, thresh);
|
c@418
|
180 }
|
c@418
|
181
|
c@418
|
182 BOOST_AUTO_TEST_CASE(getL2Norm)
|
c@418
|
183 {
|
c@418
|
184 vector<double> in { -1, 1.5, 3, 5 };
|
c@418
|
185 double out = MathUtilities::getLpNorm(in, 2);
|
c@418
|
186 // L2 norm is the sqrt of sum of squared magnitudes
|
c@418
|
187 double expected = sqrt(37.25);
|
c@418
|
188 double thresh = 1e-5;
|
c@418
|
189 BOOST_CHECK_SMALL(out - expected, thresh);
|
c@418
|
190 }
|
c@418
|
191
|
c@418
|
192 BOOST_AUTO_TEST_CASE(getL3Norm)
|
c@418
|
193 {
|
c@418
|
194 vector<double> in { -1, 1.5, 3, 5 };
|
c@418
|
195 double out = MathUtilities::getLpNorm(in, 3);
|
c@418
|
196 double expected = 5.3875;
|
c@418
|
197 double thresh = 1e-4;
|
c@418
|
198 BOOST_CHECK_SMALL(out - expected, thresh);
|
c@418
|
199 }
|
c@418
|
200
|
c@418
|
201 BOOST_AUTO_TEST_CASE(normaliseL1)
|
c@418
|
202 {
|
c@418
|
203 vector<double> in { -1, 1.5, 3, 5 };
|
c@418
|
204 vector<double> expected { -0.095238, 0.142857, 0.285714, 0.476190 };
|
c@418
|
205 vector<double> out = MathUtilities::normaliseLp(in, 1);
|
c@418
|
206 double thresh = 1e-5;
|
c@418
|
207 for (int i = 0; i < int(out.size()); ++i) {
|
c@418
|
208 BOOST_CHECK_SMALL(out[i] - expected[i], thresh);
|
c@418
|
209 }
|
c@418
|
210 out = MathUtilities::normaliseLp({ 0, 0, 0, 0 }, 1);
|
c@418
|
211 for (int i = 0; i < int(out.size()); ++i) {
|
c@418
|
212 BOOST_CHECK_EQUAL(out[i], 0.25);
|
c@418
|
213 }
|
c@418
|
214 }
|
c@418
|
215
|
c@418
|
216 BOOST_AUTO_TEST_CASE(normaliseL2)
|
c@418
|
217 {
|
c@418
|
218 vector<double> in { -1, 1.5, 3, 5 };
|
c@418
|
219 vector<double> expected { -0.16385, 0.24577, 0.49154, 0.81923 };
|
c@418
|
220 vector<double> out = MathUtilities::normaliseLp(in, 2);
|
c@418
|
221 double thresh = 1e-5;
|
c@418
|
222 for (int i = 0; i < int(out.size()); ++i) {
|
c@418
|
223 BOOST_CHECK_SMALL(out[i] - expected[i], thresh);
|
c@418
|
224 }
|
c@418
|
225 out = MathUtilities::normaliseLp({ 0, 0, 0, 0 }, 2);
|
c@418
|
226 for (int i = 0; i < int(out.size()); ++i) {
|
c@418
|
227 BOOST_CHECK_EQUAL(out[i], 0.5);
|
c@418
|
228 }
|
c@418
|
229 }
|
c@418
|
230
|
c@418
|
231 BOOST_AUTO_TEST_CASE(normaliseL3)
|
c@418
|
232 {
|
c@418
|
233 vector<double> in { -1, 1.5, 3, 5 };
|
c@418
|
234 vector<double> expected { -0.18561, 0.27842, 0.55684, 0.92807 };
|
c@418
|
235 vector<double> out = MathUtilities::normaliseLp(in, 3);
|
c@418
|
236 double thresh = 1e-5;
|
c@418
|
237 for (int i = 0; i < int(out.size()); ++i) {
|
c@418
|
238 BOOST_CHECK_SMALL(out[i] - expected[i], thresh);
|
c@418
|
239 }
|
c@418
|
240 out = MathUtilities::normaliseLp({ 0, 0, 0, 0 }, 3);
|
c@418
|
241 for (int i = 0; i < int(out.size()); ++i) {
|
c@418
|
242 BOOST_CHECK_SMALL(out[i] - 0.62996, 1e-5);
|
c@418
|
243 }
|
c@418
|
244 }
|
c@418
|
245
|
c@348
|
246 BOOST_AUTO_TEST_SUITE_END()
|
c@348
|
247
|
c@348
|
248
|