13#include <catch2/catch.hpp>
17 SECTION(
"exact fit, no weights")
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 };
22 REQUIRE(result.first == 1.0);
23 REQUIRE(result.second == 1.0);
26 SECTION(
"inexact fit, corrected by zero-weight")
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);
36 SECTION(
"exact fit, random weights")
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);
46 SECTION(
"exact fit with int x and y input vectors")
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);
56 SECTION(
"exact fit, with non-unit gradient")
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);
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.