Line data Source code
1 : #include <gmock/gmock.h>
2 : #include <gtest/gtest.h>
3 :
4 : #include <cfloat>
5 : #include "data.hpp"
6 : #include "matrix.hpp"
7 : #include "topline.hpp"
8 : #include "vectorf.hpp"
9 :
10 : using namespace testing;
11 : using std::vector;
12 :
13 : class TestThatTopLine : public Test
14 : {
15 : public:
16 : const int num_tc{30};
17 : double scale = 1.0;
18 : double rc = 0.0, order = 0.0;
19 : vector<double> tc;
20 : double epsilon{DBL_EPSILON};
21 :
22 4 : void SetUp() override { tc = vector<double>(num_tc); }
23 :
24 4 : void TearDown() override {}
25 : };
26 :
27 2 : TEST_F(TestThatTopLine, RequiresAtLeast10MoreThanTOPLINE_KSTARTCoefficients)
28 : {
29 1 : tc = vector<double>(TOPLINE_KSTART + 9);
30 2 : ASSERT_THROW(topline(tc, scale, rc, order), std::exception);
31 : }
32 :
33 2 : TEST_F(TestThatTopLine, SatisfiesStorageRequirementsForLinearLeastSquaresSystem)
34 : {
35 1 : int kstart{5};
36 1 : int m{num_tc - kstart - 1};
37 1 : int n{2};
38 1 : matrix<double> W(m, n);
39 1 : vectorf<double> b(m);
40 :
41 2 : ASSERT_THROW(constructLinearLeastSquaresSystem(tc, kstart, W, b),
42 : std::exception);
43 : }
44 :
45 2 : TEST_F(TestThatTopLine, CanConstructLinearLeastSquaresSystem)
46 : {
47 31 : for (int k{0}; k < num_tc; k++)
48 : {
49 30 : tc[ k ] = (double)(k + 1);
50 : }
51 :
52 1 : int kstart{5};
53 1 : int m{num_tc - kstart};
54 1 : int n{2};
55 2 : matrix<double> W(m, n);
56 2 : vectorf<double> b(m);
57 :
58 1 : constructLinearLeastSquaresSystem(tc, kstart, W, b);
59 :
60 26 : for (int k{1}; k <= m; k++)
61 : {
62 25 : EXPECT_THAT(W(k, 1), DoubleNear(1.0, epsilon));
63 25 : EXPECT_THAT(W(k, 2), DoubleNear((double)(kstart + k - 1), epsilon));
64 25 : EXPECT_THAT(
65 : b(k), DoubleNear(log10(fabs((double)((kstart + k - 1) + 1))), epsilon));
66 : }
67 1 : }
68 :
69 : // Problem from
70 : // https://tutorial.math.lamar.edu/Classes/CalcII/PowerSeries.aspx
71 : // Rc should be exactly 1/8.
72 : //
73 : // We want to fit the tail of the Taylor series, omit first
74 : // TOPLINE_KSTART terms from consideration.
75 2 : TEST_F(TestThatTopLine, WillComputeLeastSquarsSolution)
76 : {
77 31 : for (int k = 0; k < num_tc; k++)
78 : {
79 30 : tc[ k ] = pow(8, k + 1) / ((double)(k + 1));
80 : }
81 :
82 1 : EXPECT_THAT(topline(tc, scale, rc, order),
83 : DoubleNear(0.077610600696407323, epsilon));
84 1 : EXPECT_THAT(rc, DoubleNear(1.315873989428502e-01, epsilon));
85 1 : EXPECT_TRUE(std::isnan(order));
86 1 : }
|