fix linearRegression, fix doc, add unit test (it was untested since the change

making fitHyperplane no longer use it)
This commit is contained in:
Benoit Jacob 2009-05-05 16:50:58 +00:00
parent 5b364a68cb
commit 2b2f0c0220
2 changed files with 31 additions and 3 deletions

View File

@ -57,7 +57,7 @@
Vector3d coeffs; // will store the coefficients a, b, c Vector3d coeffs; // will store the coefficients a, b, c
linearRegression( linearRegression(
5, 5,
points, &points,
&coeffs, &coeffs,
1 // the coord to express as a function of 1 // the coord to express as a function of
// the other ones. 0 means x, 1 means y, 2 means z. // the other ones. 0 means x, 1 means y, 2 means z.
@ -108,14 +108,14 @@ void linearRegression(int numPoints,
result->resize(size); result->resize(size);
Matrix<Scalar, Dynamic, VectorType::SizeAtCompileTime, Matrix<Scalar, Dynamic, VectorType::SizeAtCompileTime,
Dynamic, VectorType::MaxSizeAtCompileTime, RowMajorBit> RowMajor, Dynamic, VectorType::MaxSizeAtCompileTime>
m(numPoints, size); m(numPoints, size);
if(funcOfOthers>0) if(funcOfOthers>0)
for(int i = 0; i < numPoints; ++i) for(int i = 0; i < numPoints; ++i)
m.row(i).start(funcOfOthers) = points[i]->start(funcOfOthers); m.row(i).start(funcOfOthers) = points[i]->start(funcOfOthers);
if(funcOfOthers<size-1) if(funcOfOthers<size-1)
for(int i = 0; i < numPoints; ++i) for(int i = 0; i < numPoints; ++i)
m.row(i).block(funcOfOthers, size-funcOfOthers-1) m.row(i).segment(funcOfOthers, size-funcOfOthers-1)
= points[i]->end(size-funcOfOthers-1); = points[i]->end(size-funcOfOthers-1);
for(int i = 0; i < numPoints; ++i) for(int i = 0; i < numPoints; ++i)
m.row(i).coeffRef(size-1) = Scalar(1); m.row(i).coeffRef(size-1) = Scalar(1);

View File

@ -62,6 +62,20 @@ void makeNoisyCohyperplanarPoints(int numPoints,
*(points[i]) += noiseAmplitude * VectorType::Random(size); *(points[i]) += noiseAmplitude * VectorType::Random(size);
} }
template<typename VectorType>
void check_linearRegression(int numPoints,
VectorType **points,
const VectorType& original,
typename VectorType::Scalar tolerance)
{
int size = points[0]->size();
assert(size==2);
VectorType result(size);
linearRegression(numPoints, points, &result, 1);
typename VectorType::Scalar error = (result - original).norm() / original.norm();
VERIFY(ei_abs(error) < ei_abs(tolerance));
}
template<typename VectorType, template<typename VectorType,
typename HyperplaneType> typename HyperplaneType>
void check_fitHyperplane(int numPoints, void check_fitHyperplane(int numPoints,
@ -81,6 +95,20 @@ void test_regression()
{ {
for(int i = 0; i < g_repeat; i++) for(int i = 0; i < g_repeat; i++)
{ {
{
Vector2f points2f [1000];
Vector2f *points2f_ptrs [1000];
for(int i = 0; i < 1000; i++) points2f_ptrs[i] = &(points2f[i]);
Vector2f coeffs2f;
Hyperplane<float,2> coeffs3f;
makeNoisyCohyperplanarPoints(1000, points2f_ptrs, &coeffs3f, 0.01f);
coeffs2f[0] = -coeffs3f.coeffs()[0]/coeffs3f.coeffs()[1];
coeffs2f[1] = -coeffs3f.coeffs()[2]/coeffs3f.coeffs()[1];
CALL_SUBTEST(check_linearRegression(10, points2f_ptrs, coeffs2f, 0.05f));
CALL_SUBTEST(check_linearRegression(100, points2f_ptrs, coeffs2f, 0.01f));
CALL_SUBTEST(check_linearRegression(1000, points2f_ptrs, coeffs2f, 0.002f));
}
{ {
Vector2f points2f [1000]; Vector2f points2f [1000];
Vector2f *points2f_ptrs [1000]; Vector2f *points2f_ptrs [1000];