Now just linearize

release/4.3a0
dellaert 2014-09-18 18:33:11 -04:00
parent b19132e004
commit a76c27d074
1 changed files with 35 additions and 13 deletions

View File

@ -20,6 +20,9 @@
#include <gtsam/geometry/Pose3.h> #include <gtsam/geometry/Pose3.h>
#include <gtsam/geometry/Cal3_S2.h> #include <gtsam/geometry/Cal3_S2.h>
#include <gtsam/inference/Key.h> #include <gtsam/inference/Key.h>
#include <gtsam/base/Testable.h>
#include <boost/make_shared.hpp>
#include <CppUnitLite/TestHarness.h> #include <CppUnitLite/TestHarness.h>
@ -66,7 +69,7 @@ Expression<Point2> operator -(const Expression<Point2>& p,
/// AD Factor /// AD Factor
template<class T> template<class T>
class BADFactor: NoiseModelFactor { class BADFactor: NonlinearFactor {
public: public:
@ -74,10 +77,22 @@ public:
BADFactor(const Expression<T>& t) { BADFactor(const Expression<T>& t) {
} }
Vector unwhitenedError(const Values& x, /**
boost::optional<std::vector<Matrix>&> H = boost::none) const { * Calculate the error of the factor
if (H) H->push_back(zeros(2,2)); * This is typically equal to log-likelihood, e.g. \f$ 0.5(h(x)-z)^2/sigma^2 \f$
return Vector(); */
double error(const Values& c) const {
return 0;
}
/// get the dimension of the factor (number of rows on linearization)
size_t dim() const {
return 0;
}
/// linearize to a GaussianFactor
boost::shared_ptr<GaussianFactor> linearize(const Values& c) const {
return boost::shared_ptr<JacobianFactor>(new JacobianFactor());
} }
}; };
@ -101,18 +116,25 @@ TEST(BAD, test) {
// Create factor // Create factor
BADFactor<Point2> f(e); BADFactor<Point2> f(e);
// evaluate, with derivatives // Create some values
Values values; Values values;
vector<Matrix> jacobians;
Vector actual = f.unwhitenedError(values, jacobians);
// Check value // Check value
Vector expected = (Vector(2) << 0, 0); EXPECT_DOUBLES_EQUAL(0, f.error(values), 1e-9);
EXPECT(assert_equal(expected, actual));
// Check derivatives // Check dimension
Matrix expectedHx = zeros(2,3); EXPECT_LONGS_EQUAL(0, f.dim());
EXPECT(assert_equal(expectedHx, jacobians[0]));
// Check linearization
JacobianFactor expected( //
1, (Matrix(2, 6) << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), //
2, (Matrix(2, 3) << 1, 2, 3, 4, 5, 6), //
3, (Matrix(2, 5) << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10), //
(Vector(2) << 1, 2));
boost::shared_ptr<GaussianFactor> gf = f.linearize(values);
boost::shared_ptr<JacobianFactor> jf = //
boost::dynamic_pointer_cast<JacobianFactor>(gf);
EXPECT( assert_equal(expected, *jf,1e-9));
} }
/* ************************************************************************* */ /* ************************************************************************* */