Skip to content
Snippets Groups Projects
Commit 8ebe5944 authored by Rainer Kartmann's avatar Rainer Kartmann
Browse files

Add test

parent 4c177a00
No related branches found
No related tags found
No related merge requests found
......@@ -8,4 +8,4 @@ ADD_SU_TEST( SoftMinMax )
ADD_SU_TEST( statistics )
ADD_SUBDIRECTORY(pose)
ADD_SUBDIRECTORY(regression)
ADD_SU_TEST( linear3d )
/**
* @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()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment