Audacity 3.2.0
MathTests.cpp
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*!********************************************************************
3
4 Audacity: A Digital Audio Editor
5
6 MathTests.cpp
7
8 Matthieu Hodgkinson
9
10**********************************************************************/
11#include "LinearFit.h"
12
13#include <catch2/catch.hpp>
14
15TEST_CASE("LinearFit")
16{
17 SECTION("exact fit, no weights")
18 {
19 const std::vector<double> x { 1.0, 2.0, 3.0, 4.0, 5.0 };
20 const std::vector<double> y { 2.0, 3.0, 4.0, 5.0, 6.0 };
21 const auto result = LinearFit(x, y);
22 REQUIRE(result.first == 1.0);
23 REQUIRE(result.second == 1.0);
24 }
25
26 SECTION("inexact fit, corrected by zero-weight")
27 {
28 const std::vector<double> x { 1.0, 2.0, 3.0, 4.0, 5.0 };
29 const std::vector<double> y { 2.0, 3.0, 4.0, 5.0, 7.0 };
30 const std::vector<double> weights { 1.0, 1.0, 1.0, 1.0, 0.0 };
31 const auto result = LinearFit(x, y, weights);
32 REQUIRE(result.first == 1.0);
33 REQUIRE(result.second == 1.0);
34 }
35
36 SECTION("exact fit, random weights")
37 {
38 const std::vector<double> x { 1.0, 2.0, 3.0, 4.0, 5.0 };
39 const std::vector<double> y { 2.0, 3.0, 4.0, 5.0, 6.0 };
40 const std::vector<double> weights { 1.0, 2.0, 1.0, 3.0, 1.0 };
41 const auto result = LinearFit(x, y, weights);
42 REQUIRE(result.first == 1.0);
43 REQUIRE(result.second == 1.0);
44 }
45
46 SECTION("exact fit with int x and y input vectors")
47 {
48 const std::vector<int> x { 1, 2, 3, 4, 5 };
49 const std::vector<int> y { 2, 3, 4, 5, 6 };
50 const std::vector<double> weights { 1.0, 1.0, 1.0, 1.0, 1.0 };
51 const auto result = LinearFit(x, y, weights);
52 REQUIRE(result.first == 1.0);
53 REQUIRE(result.second == 1.0);
54 }
55
56 SECTION("exact fit, with non-unit gradient")
57 {
58 const std::vector<double> x { 1.0, 2.0, 3.0, 4.0, 5.0 };
59 const std::vector<double> y { 2.0, 4.0, 6.0, 8.0, 10.0 };
60 const std::vector<double> weights { 1.0, 1.0, 1.0, 1.0, 0.0 };
61 const auto result = LinearFit(x, y, weights);
62 REQUIRE(result.first == 2.0);
63 REQUIRE(result.second == 0.0);
64 }
65}
std::pair< double, double > LinearFit(const std::vector< X > &x, const std::vector< Y > &y, std::vector< W > w={})
Linear least-square fit of a set of points y located at x, with optional weights w.
Definition: LinearFit.h:26
TEST_CASE("LinearFit")
Definition: MathTests.cpp:15