1 /*
2 * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "rtc_tools/frame_analyzer/linear_least_squares.h"
12
13 #include <cstdint>
14
15 #include "test/gtest.h"
16
17 namespace webrtc {
18 namespace test {
19
TEST(LinearLeastSquares,ScalarIdentityOneObservation)20 TEST(LinearLeastSquares, ScalarIdentityOneObservation) {
21 IncrementalLinearLeastSquares lls;
22 lls.AddObservations({{1}}, {{1}});
23 EXPECT_EQ(std::vector<std::vector<double>>({{1.0}}), lls.GetBestSolution());
24 }
25
TEST(LinearLeastSquares,ScalarIdentityTwoObservationsOneCall)26 TEST(LinearLeastSquares, ScalarIdentityTwoObservationsOneCall) {
27 IncrementalLinearLeastSquares lls;
28 lls.AddObservations({{1, 2}}, {{1, 2}});
29 EXPECT_EQ(std::vector<std::vector<double>>({{1.0}}), lls.GetBestSolution());
30 }
31
TEST(LinearLeastSquares,ScalarIdentityTwoObservationsTwoCalls)32 TEST(LinearLeastSquares, ScalarIdentityTwoObservationsTwoCalls) {
33 IncrementalLinearLeastSquares lls;
34 lls.AddObservations({{1}}, {{1}});
35 lls.AddObservations({{2}}, {{2}});
36 EXPECT_EQ(std::vector<std::vector<double>>({{1.0}}), lls.GetBestSolution());
37 }
38
TEST(LinearLeastSquares,MatrixIdentityOneObservation)39 TEST(LinearLeastSquares, MatrixIdentityOneObservation) {
40 IncrementalLinearLeastSquares lls;
41 lls.AddObservations({{1, 2}, {3, 4}}, {{1, 2}, {3, 4}});
42 EXPECT_EQ(std::vector<std::vector<double>>({{1.0, 0.0}, {0.0, 1.0}}),
43 lls.GetBestSolution());
44 }
45
TEST(LinearLeastSquares,MatrixManyObservations)46 TEST(LinearLeastSquares, MatrixManyObservations) {
47 IncrementalLinearLeastSquares lls;
48 // Test that we can find the solution of the overspecified equation system:
49 // [1, 2] [1, 3] = [5, 11]
50 // [3, 4] [2, 4] [11, 25]
51 // [5, 6] [17, 39]
52 lls.AddObservations({{1}, {2}}, {{5}, {11}});
53 lls.AddObservations({{3}, {4}}, {{11}, {25}});
54 lls.AddObservations({{5}, {6}}, {{17}, {39}});
55
56 const std::vector<std::vector<double>> result = lls.GetBestSolution();
57 // We allow some numerical flexibility here.
58 EXPECT_DOUBLE_EQ(1.0, result[0][0]);
59 EXPECT_DOUBLE_EQ(2.0, result[0][1]);
60 EXPECT_DOUBLE_EQ(3.0, result[1][0]);
61 EXPECT_DOUBLE_EQ(4.0, result[1][1]);
62 }
63
TEST(LinearLeastSquares,MatrixVectorOneObservation)64 TEST(LinearLeastSquares, MatrixVectorOneObservation) {
65 IncrementalLinearLeastSquares lls;
66 // Test that we can find the solution of the overspecified equation system:
67 // [1, 2] [1] = [5]
68 // [3, 4] [2] [11]
69 // [5, 6] [17]
70 lls.AddObservations({{1, 3, 5}, {2, 4, 6}}, {{5, 11, 17}});
71
72 const std::vector<std::vector<double>> result = lls.GetBestSolution();
73 // We allow some numerical flexibility here.
74 EXPECT_DOUBLE_EQ(1.0, result[0][0]);
75 EXPECT_DOUBLE_EQ(2.0, result[0][1]);
76 }
77
TEST(LinearLeastSquares,LinearLeastSquaresNonPerfectSolution)78 TEST(LinearLeastSquares, LinearLeastSquaresNonPerfectSolution) {
79 IncrementalLinearLeastSquares lls;
80 // Test that we can find the non-perfect solution of the overspecified
81 // equation system:
82 // [1] [20] = [21]
83 // [2] [39]
84 // [3] [60]
85 // [2] [41]
86 // [1] [19]
87 lls.AddObservations({{1, 2, 3, 2, 1}}, {{21, 39, 60, 41, 19}});
88
89 EXPECT_DOUBLE_EQ(20.0, lls.GetBestSolution()[0][0]);
90 }
91
92 } // namespace test
93 } // namespace webrtc
94