comparison tests/TestMathUtilities.cpp @ 194:26daede606a8

Add L^p norms, doc, tests
author Chris Cannam
date Wed, 07 Oct 2015 11:14:16 +0100
parents 0fdbb93e92b7
children
comparison
equal deleted inserted replaced
193:ca658c7215a9 194:26daede606a8
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 2
3 #include "maths/MathUtilities.h" 3 #include "maths/MathUtilities.h"
4 4
5 #include <cmath> 5 #include <cmath>
6 #include <iostream>
6 7
7 #define BOOST_TEST_DYN_LINK 8 #define BOOST_TEST_DYN_LINK
8 #define BOOST_TEST_MAIN 9 #define BOOST_TEST_MAIN
9 10
10 #include <boost/test/unit_test.hpp> 11 #include <boost/test/unit_test.hpp>
12
13 using namespace std;
11 14
12 BOOST_AUTO_TEST_SUITE(TestMathUtilities) 15 BOOST_AUTO_TEST_SUITE(TestMathUtilities)
13 16
14 BOOST_AUTO_TEST_CASE(round) 17 BOOST_AUTO_TEST_CASE(round)
15 { 18 {
32 BOOST_CHECK_EQUAL(MathUtilities::mean(0, 0), 0); 35 BOOST_CHECK_EQUAL(MathUtilities::mean(0, 0), 0);
33 double d0[] = { 0, 4, 3, -1 }; 36 double d0[] = { 0, 4, 3, -1 };
34 BOOST_CHECK_EQUAL(MathUtilities::mean(d0, 4), 1.5); 37 BOOST_CHECK_EQUAL(MathUtilities::mean(d0, 4), 1.5);
35 double d1[] = { -2.6 }; 38 double d1[] = { -2.6 };
36 BOOST_CHECK_EQUAL(MathUtilities::mean(d1, 1), -2.6); 39 BOOST_CHECK_EQUAL(MathUtilities::mean(d1, 1), -2.6);
37 std::vector<double> v; 40 vector<double> v;
38 v.push_back(0); 41 v.push_back(0);
39 v.push_back(4); 42 v.push_back(4);
40 v.push_back(3); 43 v.push_back(3);
41 v.push_back(-1); 44 v.push_back(-1);
42 BOOST_CHECK_EQUAL(MathUtilities::mean(v, 0, 4), 1.5); 45 BOOST_CHECK_EQUAL(MathUtilities::mean(v, 0, 4), 1.5);
146 BOOST_CHECK_EQUAL(MathUtilities::gcd(27, 18), 9); 149 BOOST_CHECK_EQUAL(MathUtilities::gcd(27, 18), 9);
147 BOOST_CHECK_EQUAL(MathUtilities::gcd(18, 36), 18); 150 BOOST_CHECK_EQUAL(MathUtilities::gcd(18, 36), 18);
148 BOOST_CHECK_EQUAL(MathUtilities::gcd(37, 18), 1); 151 BOOST_CHECK_EQUAL(MathUtilities::gcd(37, 18), 1);
149 } 152 }
150 153
154 BOOST_AUTO_TEST_CASE(getAlphaNorm1)
155 {
156 vector<double> in { -1, 1.5, 3, 5 };
157 double out = MathUtilities::getAlphaNorm(in, 1);
158 double expected = 2.6250;
159 double thresh = 1e-4;
160 BOOST_CHECK_SMALL(out - expected, thresh);
161 }
162
163 BOOST_AUTO_TEST_CASE(getAlphaNorm2)
164 {
165 vector<double> in { -1, 1.5, 3, 5 };
166 double out = MathUtilities::getAlphaNorm(in, 2);
167 double expected = 3.0516;
168 double thresh = 1e-4;
169 BOOST_CHECK_SMALL(out - expected, thresh);
170 }
171
172 BOOST_AUTO_TEST_CASE(getL1Norm)
173 {
174 vector<double> in { -1, 1.5, 3, 5 };
175 double out = MathUtilities::getLpNorm(in, 1);
176 // L1 norm is the sum of magnitudes
177 double expected = 10.5;
178 double thresh = 1e-5;
179 BOOST_CHECK_SMALL(out - expected, thresh);
180 }
181
182 BOOST_AUTO_TEST_CASE(getL2Norm)
183 {
184 vector<double> in { -1, 1.5, 3, 5 };
185 double out = MathUtilities::getLpNorm(in, 2);
186 // L2 norm is the sqrt of sum of squared magnitudes
187 double expected = sqrt(37.25);
188 double thresh = 1e-5;
189 BOOST_CHECK_SMALL(out - expected, thresh);
190 }
191
192 BOOST_AUTO_TEST_CASE(getL3Norm)
193 {
194 vector<double> in { -1, 1.5, 3, 5 };
195 double out = MathUtilities::getLpNorm(in, 3);
196 double expected = 5.3875;
197 double thresh = 1e-4;
198 BOOST_CHECK_SMALL(out - expected, thresh);
199 }
200
201 BOOST_AUTO_TEST_CASE(normaliseL1)
202 {
203 vector<double> in { -1, 1.5, 3, 5 };
204 vector<double> expected { -0.095238, 0.142857, 0.285714, 0.476190 };
205 vector<double> out = MathUtilities::normaliseLp(in, 1);
206 double thresh = 1e-5;
207 for (int i = 0; i < int(out.size()); ++i) {
208 BOOST_CHECK_SMALL(out[i] - expected[i], thresh);
209 }
210 out = MathUtilities::normaliseLp({ 0, 0, 0, 0 }, 1);
211 for (int i = 0; i < int(out.size()); ++i) {
212 BOOST_CHECK_EQUAL(out[i], 0.25);
213 }
214 }
215
216 BOOST_AUTO_TEST_CASE(normaliseL2)
217 {
218 vector<double> in { -1, 1.5, 3, 5 };
219 vector<double> expected { -0.16385, 0.24577, 0.49154, 0.81923 };
220 vector<double> out = MathUtilities::normaliseLp(in, 2);
221 double thresh = 1e-5;
222 for (int i = 0; i < int(out.size()); ++i) {
223 BOOST_CHECK_SMALL(out[i] - expected[i], thresh);
224 }
225 out = MathUtilities::normaliseLp({ 0, 0, 0, 0 }, 2);
226 for (int i = 0; i < int(out.size()); ++i) {
227 BOOST_CHECK_EQUAL(out[i], 0.5);
228 }
229 }
230
231 BOOST_AUTO_TEST_CASE(normaliseL3)
232 {
233 vector<double> in { -1, 1.5, 3, 5 };
234 vector<double> expected { -0.18561, 0.27842, 0.55684, 0.92807 };
235 vector<double> out = MathUtilities::normaliseLp(in, 3);
236 double thresh = 1e-5;
237 for (int i = 0; i < int(out.size()); ++i) {
238 BOOST_CHECK_SMALL(out[i] - expected[i], thresh);
239 }
240 out = MathUtilities::normaliseLp({ 0, 0, 0, 0 }, 3);
241 for (int i = 0; i < int(out.size()); ++i) {
242 BOOST_CHECK_SMALL(out[i] - 0.62996, 1e-5);
243 }
244 }
245
151 BOOST_AUTO_TEST_SUITE_END() 246 BOOST_AUTO_TEST_SUITE_END()
152 247
153 248