From 8ebe59443c69a65ad99e35e5286d3df09a76cbca Mon Sep 17 00:00:00 2001
From: Rainer Kartmann <rainer.kartmann@kit.edu>
Date: Thu, 5 May 2022 08:56:57 +0200
Subject: [PATCH] Add test

---
 SimoxUtility/tests/math/CMakeLists.txt        |  2 +-
 .../tests/math/regression/CMakeLists.txt      |  1 +
 .../tests/math/regression/linear3d.cpp        | 75 +++++++++++++++++++
 3 files changed, 77 insertions(+), 1 deletion(-)
 create mode 100644 SimoxUtility/tests/math/regression/CMakeLists.txt
 create mode 100644 SimoxUtility/tests/math/regression/linear3d.cpp

diff --git a/SimoxUtility/tests/math/CMakeLists.txt b/SimoxUtility/tests/math/CMakeLists.txt
index 0e9e6d7f6..a3558c219 100644
--- a/SimoxUtility/tests/math/CMakeLists.txt
+++ b/SimoxUtility/tests/math/CMakeLists.txt
@@ -8,4 +8,4 @@ ADD_SU_TEST( SoftMinMax )
 ADD_SU_TEST( statistics )
 
 ADD_SUBDIRECTORY(pose)
-
+ADD_SUBDIRECTORY(regression)
diff --git a/SimoxUtility/tests/math/regression/CMakeLists.txt b/SimoxUtility/tests/math/regression/CMakeLists.txt
new file mode 100644
index 000000000..fab80e952
--- /dev/null
+++ b/SimoxUtility/tests/math/regression/CMakeLists.txt
@@ -0,0 +1 @@
+ADD_SU_TEST( linear3d )
diff --git a/SimoxUtility/tests/math/regression/linear3d.cpp b/SimoxUtility/tests/math/regression/linear3d.cpp
new file mode 100644
index 000000000..fe6d20135
--- /dev/null
+++ b/SimoxUtility/tests/math/regression/linear3d.cpp
@@ -0,0 +1,75 @@
+/**
+* @package    SimoxUtility
+* @author     Raphael Grimm
+* @copyright  2019 Raphael Grimm
+*/
+
+#define BOOST_TEST_MODULE SimoxUtility_RegressionTest
+
+#include <random>
+#include <iostream>
+
+#include <boost/test/included/unit_test.hpp>
+
+#include <SimoxUtility/math/regression/linear3d.h>
+
+
+
+namespace SimoxMathRegressionTest
+{
+
+struct Fixture
+{
+    const double prec = 1e-10;
+
+    std::function<Eigen::Vector3d(double)> f = [](double x)
+    {
+        Eigen::Vector3d y;
+        for (Eigen::Index i = 0; i < y.rows(); ++i)
+        {
+            y(i) = - (1 + i) + (2 * i) * x;
+        }
+        return y;
+    };
+    const std::vector<double> xs
+    {
+        -1, 0, 2
+    };
+    const std::vector<Eigen::Vector3d> ys
+    {
+        f(xs[0]), f(xs[1]), f(xs[2])
+    };
+
+};
+}
+
+
+BOOST_FIXTURE_TEST_SUITE(SimoxMathRegressionTest, Fixture)
+
+
+BOOST_AUTO_TEST_CASE(test_linear_regression_3d_fit_and_predict)
+{
+    using simox::math::LinearRegression3D;
+
+    // Fit
+
+    const LinearRegression3D regression = LinearRegression3D::Fit(xs, ys);
+
+    BOOST_CHECK_CLOSE(regression.coefficients(0, 0), - (1 + 0), prec);
+    BOOST_CHECK_CLOSE(regression.coefficients(1, 0), - (1 + 1), prec);
+    BOOST_CHECK_CLOSE(regression.coefficients(2, 0), - (1 + 2), prec);
+
+    BOOST_CHECK_CLOSE(regression.coefficients(0, 1), (2 * 0), prec);
+    BOOST_CHECK_CLOSE(regression.coefficients(1, 1), (2 * 1), prec);
+    BOOST_CHECK_CLOSE(regression.coefficients(2, 1), (2 * 2), prec);
+
+
+    // Predict
+
+    BOOST_CHECK_LE((regression.predict(xs[0]) - ys[0]).norm(), prec);
+    BOOST_CHECK_LE((regression.predict(xs[1]) - ys[1]).norm(), prec);
+    BOOST_CHECK_LE((regression.predict(xs[2]) - ys[2]).norm(), prec);
+}
+
+
+BOOST_AUTO_TEST_SUITE_END()
-- 
GitLab