aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--Eigen/src/LeastSquares/LeastSquares.h6
-rw-r--r--test/regression.cpp28
2 files changed, 31 insertions, 3 deletions
diff --git a/Eigen/src/LeastSquares/LeastSquares.h b/Eigen/src/LeastSquares/LeastSquares.h
index cd6ec3cf4..f73fa9ff2 100644
--- a/Eigen/src/LeastSquares/LeastSquares.h
+++ b/Eigen/src/LeastSquares/LeastSquares.h
@@ -57,7 +57,7 @@
Vector3d coeffs; // will store the coefficients a, b, c
linearRegression(
5,
- points,
+ &points,
&coeffs,
1 // the coord to express as a function of
// the other ones. 0 means x, 1 means y, 2 means z.
@@ -108,14 +108,14 @@ void linearRegression(int numPoints,
result->resize(size);
Matrix<Scalar, Dynamic, VectorType::SizeAtCompileTime,
- Dynamic, VectorType::MaxSizeAtCompileTime, RowMajorBit>
+ RowMajor, Dynamic, VectorType::MaxSizeAtCompileTime>
m(numPoints, size);
if(funcOfOthers>0)
for(int i = 0; i < numPoints; ++i)
m.row(i).start(funcOfOthers) = points[i]->start(funcOfOthers);
if(funcOfOthers<size-1)
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);
for(int i = 0; i < numPoints; ++i)
m.row(i).coeffRef(size-1) = Scalar(1);
diff --git a/test/regression.cpp b/test/regression.cpp
index e551bd402..534ad3d1c 100644
--- a/test/regression.cpp
+++ b/test/regression.cpp
@@ -62,6 +62,20 @@ void makeNoisyCohyperplanarPoints(int numPoints,
*(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,
typename HyperplaneType>
void check_fitHyperplane(int numPoints,
@@ -85,6 +99,20 @@ void test_regression()
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_ptrs [1000];
+ for(int i = 0; i < 1000; i++) points2f_ptrs[i] = &(points2f[i]);
Hyperplane<float,2> coeffs3f;
makeNoisyCohyperplanarPoints(1000, points2f_ptrs, &coeffs3f, 0.01f);
CALL_SUBTEST(check_fitHyperplane(10, points2f_ptrs, coeffs3f, 0.05f));