diff --git a/SimoxUtility/tests/math/CMakeLists.txt b/SimoxUtility/tests/math/CMakeLists.txt index 0e9e6d7f60b91ca657eb84535062f981e080e27b..a3558c2192c3a02fd5dc2762b2929d833546f476 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 0000000000000000000000000000000000000000..fab80e952c987a730acc2bd292e52736646cd80c --- /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 0000000000000000000000000000000000000000..fe6d2013566166a99538de96f59ca41caa7d9f4e --- /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()