28 #include <boost/assign/std/vector.hpp> 29 #include <boost/assign/list_of.hpp> 30 #include <boost/range/iterator_range.hpp> 31 #include <boost/range/adaptor/map.hpp> 34 using namespace gtsam;
40 const vector<pair<Key, Matrix> > terms = list_of<pair<Key,Matrix> >
41 (make_pair(5, Matrix3::Identity()))
42 (make_pair(10, 2*Matrix3::Identity()))
43 (make_pair(15, 3*Matrix3::Identity()));
60 boost::make_iterator_range(terms.begin(), terms.begin()),
b);
71 boost::make_iterator_range(terms.begin(), terms.begin() + 1),
b, noise);
84 boost::make_iterator_range(terms.begin(), terms.begin() + 2),
b, noise);
86 terms[1].first, terms[1].second,
b, noise);
98 boost::make_iterator_range(terms.begin(), terms.begin() + 3),
b, noise);
100 terms[1].first, terms[1].second, terms[2].first, terms[2].second,
b, noise);
112 boost::make_iterator_range(terms.begin(), terms.begin() + 3),
b, noise);
113 map<Key,Matrix> mapTerms;
115 mapTerms.insert(terms[2]);
116 mapTerms.insert(terms[1]);
117 mapTerms.insert(terms[0]);
130 boost::make_iterator_range(terms.begin(), terms.begin() + 3),
b, noise);
132 blockMatrix(0) = terms[0].second;
133 blockMatrix(1) = terms[1].second;
134 blockMatrix(2) = terms[2].second;
136 JacobianFactor actual(terms | boost::adaptors::map_keys, blockMatrix, noise);
148 TEST(JabobianFactor, Hessian_conversion) {
150 1.57, 2.695, -1.1, -2.35,
151 2.695, 11.3125, -0.65, -10.225,
153 -2.35, -10.225, 0.5, 9.25).finished(),
154 (
Vector(4) << -7.885, -28.5175, 2.75, 25.675).finished(),
158 1.2530, 2.1508, -0.8779, -1.8755,
159 0, 2.5858, 0.4789, -2.3943).finished(),
166 TEST(JabobianFactor, Hessian_conversion2) {
177 TEST(JabobianFactor, Hessian_conversion3) {
180 0, 3, 2, 1).finished(),
219 Matrix A3(6,2); A3 << Matrix::Zero(4,2),
A33;
236 values.
insert(5, Vector::Constant(3, 1.0));
237 values.
insert(10, Vector::Constant(3, 0.5));
238 values.
insert(15, Vector::Constant(3, 1.0/3.0));
240 Vector expected_unwhitened(3); expected_unwhitened << 2.0, 1.0, 0.0;
244 Vector expected_whitened(3); expected_whitened << 4.0, 2.0, 0.0;
248 double expected_error = 0.5 * expected_whitened.squaredNorm();
249 double actual_error = factor.
error(values);
259 Matrix jacobianExpected(3, 9);
260 jacobianExpected << simple::terms[0].second, simple::terms[1].second, simple::terms[2].second;
262 Matrix augmentedJacobianExpected(3, 10);
263 augmentedJacobianExpected << jacobianExpected, rhsExpected;
265 Matrix augmentedHessianExpected =
266 augmentedJacobianExpected.transpose() * augmentedJacobianExpected;
284 expectDiagonal.
insert(5, Vector::Ones(3));
285 expectDiagonal.
insert(10, 4*Vector::Ones(3));
286 expectDiagonal.
insert(15, 9*Vector::Ones(3));
303 Matrix jacobianExpected(3, 9);
304 jacobianExpected << simple::terms[0].second, simple::terms[1].second, simple::terms[2].second;
306 Matrix augmentedJacobianExpected(3, 10);
307 augmentedJacobianExpected << jacobianExpected, rhsExpected;
309 Matrix augmentedHessianExpected =
310 augmentedJacobianExpected.transpose() * simple::noise->R().transpose()
311 * simple::noise->R() * augmentedJacobianExpected;
346 const double sigma = 0.1;
365 const double alpha = 0.5;
366 expectedX.
insert(1, - alpha * expectedE /sigma);
367 expectedX.
insert(2, alpha * expectedE /sigma);
405 0.0, 0.0, 1.0).finished();
412 0.0, 0.0, 2.0).finished();
416 0.0, 0.0, -2.0).finished();
423 0.0, 0.0, 3.0).finished();
428 gfg.
add(1, A01, b0, noiseModel::Diagonal::Sigmas(s0,
true));
429 gfg.
add(0, A10, 1, A11, b1, noiseModel::Diagonal::Sigmas(s1,
true));
430 gfg.
add(1, A21, b2, noiseModel::Diagonal::Sigmas(s2,
true));
432 Matrix zero3x3 = Matrix::Zero(3,3);
435 Vector9
b; b <<
b1, b0,
b2;
436 Vector9
sigmas; sigmas << s1, s0, s2;
438 JacobianFactor combinedFactor(0, A0, 1, A1, b, noiseModel::Diagonal::Sigmas(sigmas,
true));
439 GaussianFactorGraph::EliminationResult
expected = combinedFactor.
eliminate(list_of(0));
443 GaussianFactorGraph::EliminationResult actual =
EliminateQR(gfg, list_of(0));
483 vector<pair<Key, Matrix> > meas;
484 meas.push_back(make_pair(2, Ax2));
485 meas.push_back(make_pair(11, Al1x1));
486 JacobianFactor combined(meas, b2, noiseModel::Diagonal::Sigmas(sigmas));
489 pair<GaussianConditional::shared_ptr, JacobianFactor::shared_ptr>
493 double oldSigma = 0.0894427;
497 ).finished()/oldSigma;
499 -0.20, 0.00,-0.80, 0.00,
500 +0.00,-0.20,+0.00,-0.80
501 ).finished()/oldSigma;
508 double sigma = 0.2236;
511 1.00, 0.00, -1.00, 0.00,
512 0.00, 1.00, +0.00, -1.00
515 JacobianFactor expectedLF(11, Bl1x1, b1, noiseModel::Unit::Create(2));
524 4., 0., 1., 4., 1., 0., 3., 6., 8., 8., 1.,
525 9., 2., 0., 1., 6., 3., 9., 6., 6., 9., 4.,
526 5., 3., 7., 9., 5., 5., 9., 1., 3., 7., 0.,
527 5., 6., 5., 7., 9., 4., 0., 1., 1., 3., 5.,
528 0., 0., 4., 5., 6., 6., 7., 9., 4., 5., 4.,
529 0., 0., 9., 4., 8., 6., 2., 1., 4., 1., 6.,
530 0., 0., 6., 0., 4., 2., 4., 0., 1., 9., 6.,
531 0., 0., 6., 6., 4., 4., 5., 5., 5., 8., 6.,
532 0., 0., 0., 0., 8., 0., 9., 8., 2., 8., 0.,
533 0., 0., 0., 0., 0., 9., 4., 6., 3., 2., 0.,
534 0., 0., 0., 0., 1., 1., 9., 1., 5., 5., 3.,
535 0., 0., 0., 0., 1., 1., 3., 3., 2., 0., 5.,
536 0., 0., 0., 0., 0., 0., 0., 0., 2., 4., 6.,
537 0., 0., 0., 0., 0., 0., 0., 0., 6., 3., 4.).finished();
540 const SharedDiagonal sig_4D = noiseModel::Isotropic::Sigma(4, 0.5);
541 const SharedDiagonal sig_2D = noiseModel::Isotropic::Sigma(2, 0.5);
554 -12.1244, -5.1962, -5.2786, -8.6603, -10.5573, -5.9385, -11.3820, -7.2581, -8.7427, -13.4440, -5.3611,
555 0., 4.6904, 5.0254, 5.5432, 5.5737, 3.0153, -3.0153, -3.5635, -3.9290, -2.7412, 2.1625,
556 0., 0., -13.8160, -8.7166, -10.2245, -8.8666, -8.7632, -5.2544, -6.9192, -10.5537, -9.3250,
557 0., 0., 0., 6.5033, -1.1453, 1.3179, 2.5768, 5.5503, 3.6524, 1.3491, -2.5676,
558 0., 0., 0., 0., -9.6242, -2.1148, -9.3509, -10.5846, -3.5366, -6.8561, -3.2277,
559 0., 0., 0., 0., 0., 9.7887, 4.3551, 5.7572, 2.7876, 0.1611, 1.1769,
560 0., 0., 0., 0., 0., 0., -11.1139, -0.6521, -2.1943, -7.5529, -0.9081,
561 0., 0., 0., 0., 0., 0., 0., -4.6479, -1.9367, -6.5170, -3.7685,
562 0., 0., 0., 0., 0., 0., 0., 0., 8.2503, 3.3757, 6.8476,
563 0., 0., 0., 0., 0., 0., 0., 0., 0., -5.7095, -0.0090).finished();
567 noiseModel::Unit::Create(6));
570 GaussianFactorGraph::EliminationResult actual =
EliminateQR(factors, list_of(3)(5)(7));
591 pair<GaussianConditional::shared_ptr, JacobianFactor::shared_ptr>
595 EXPECT(actual.second->empty());
611 Matrix2 A1; A1 << 2,4, 2,1;
614 Matrix2 A2; A2 << 2,4, 2,4;
616 JacobianFactor lc(1, A1, 2, A2, b, noiseModel::Constrained::All(2));
619 pair<GaussianConditional::shared_ptr, JacobianFactor::shared_ptr>
625 Matrix Aempty = m.topRows(0);
626 Vector bempty = m.block(0,0,0,1);
627 JacobianFactor expectedLF(2, Aempty, bempty, noiseModel::Constrained::All(0));
631 Matrix2
R; R << 1,2, 0,1;
632 Matrix2
S; S << 1,2, 0,0;
647 Vector9
sigmas = Vector9::Ones();
651 GaussianFactorGraph::EliminationResult actual = factor.
eliminate(list_of(0));
654 expectedRd << -2.64575131, -2.64575131, -2.64575131, -2.64575131,
658 noiseModel::Unit::Create(3));
660 EXPECT(actual.second->empty());
Provides additional testing facilities for common data structures.
Vector unweighted_error(const VectorValues &c) const
std::pair< Matrix, Vector > jacobianUnweighted() const
Returns (dense) A,b pair associated with factor, does not bake in weights.
static int runAllTests(TestResult &result)
std::pair< boost::shared_ptr< GaussianConditional >, shared_ptr > eliminate(const Ordering &keys)
TEST(JacobianFactor, constructors_and_accessors)
static enum @843 ordering
#define DOUBLES_EQUAL(expected, actual, threshold)
static const double sigma
void diagonal(const MatrixType &m)
Rot2 R(Rot2::fromAngle(0.1))
const_iterator end() const
std::map< Key, Matrix > hessianBlockDiagonal() const override
Return the block diagonal of the Hessian for this factor.
std::pair< Matrix, Vector > jacobian() const override
Returns (dense) A,b pair associated with factor, bakes in the weights.
GaussianFactorGraph factors(list_of(factor1)(factor2)(factor3))
Pose3 x2(Rot3::Ypr(0.0, 0.0, 0.0), l2)
Matrix augmentedJacobian(const Ordering &ordering) const
JacobianFactor factor3(keyY, A32, keyZ, A33, b3, noiseModel::Isotropic::Sigma(2, sigma3))
VariableSlots describes the structure of a combined factor in terms of where each block comes from in...
void add(const GaussianFactor &factor)
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
constexpr int first(int i)
Implementation details for constexpr functions.
const constBVector getb() const
Vector error_vector(const VectorValues &c) const
const_iterator begin() const
#define EXPECT(condition)
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Array< double, 1, 3 > e(1./3., 0.5, 2.)
Matrix augmentedJacobian() const override
bool empty() const override
Conditional Gaussian Base class.
Linear Factor Graph where all factors are Gaussians.
constABlock getA(const_iterator variable) const
const SharedDiagonal & get_model() const
#define LONGS_EQUAL(expected, actual)
noiseModel::Diagonal::shared_ptr SharedDiagonal
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Matrix augmentedJacobianUnweighted() const
#define EXPECT_LONGS_EQUAL(expected, actual)
const KeyVector & keys() const
Access the factor's involved variable keys.
Matrix augmentedInformation() const override
void hessianDiagonal(double *d) const override
Raw memory access version of hessianDiagonal.
Matrix stack(size_t nrMatrices,...)
double error(const VectorValues &c) const override
A Gaussian factor using the canonical parameters (information form)
JacobianFactor factor2(keyX, A21, keyY, A22, b2, noiseModel::Isotropic::Sigma(2, sigma2))
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > Matrix
void transposeMultiplyAdd(double alpha, const Vector &e, VectorValues &x) const
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
VectorValues gradientAtZero() const override
A'*b for Jacobian.
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Eigen::Matrix< double, Eigen::Dynamic, 1 > Vector
iterator insert(const std::pair< Key, Vector > &key_value)
std::pair< GaussianConditional::shared_ptr, JacobianFactor::shared_ptr > EliminateQR(const GaussianFactorGraph &factors, const Ordering &keys)
std::uint64_t Key
Integer nonlinear key type.
Matrix information() const override
JacobianFactor factor1(keyX, A11, b1, noiseModel::Isotropic::Sigma(2, sigma1))