34 using namespace gtsam;
43 bool GaussianFactorGraph::equals(
const This& fg,
double tol)
const 45 return Base::equals(fg, tol);
53 keys.insert(factor->begin(), factor->end());
58 std::map<Key, size_t> GaussianFactorGraph::getKeyDimMap()
const {
59 map<Key, size_t> spec;
62 map<Key,size_t>::iterator it2 = spec.find(*it);
63 if ( it2 == spec.end() ) {
64 spec.emplace(*it, gf->getDim(it));
106 size_t& ncols)
const {
107 gttic_(GaussianFactorGraph_sparseJacobian);
109 typedef std::map<Key, size_t> KeySizeMap;
111 for (
const auto& factor : *
this) {
112 if (!static_cast<bool>(factor))
continue;
114 for (
auto it = factor->begin(); it != factor->end(); ++it) {
115 dims[*it] = factor->getDim(it);
121 KeySizeMap columnIndices = dims;
122 for (
const auto key : ordering) {
123 columnIndices[
key] = ncols;
129 entries.reserve(30 *
size());
131 for (
const auto& factor : *
this) {
132 if (!static_cast<bool>(factor))
continue;
136 boost::dynamic_pointer_cast<JacobianFactor>(factor));
137 if (!jacobianFactor) {
139 boost::dynamic_pointer_cast<HessianFactor>(factor));
143 throw std::invalid_argument(
144 "GaussianFactorGraph contains a factor that is neither a " 145 "JacobianFactor nor a HessianFactor.");
151 for (
auto key = whitened.begin();
key < whitened.end(); ++
key) {
154 size_t column_start = columnIndices[*
key];
155 for (
size_t i = 0;
i < (
size_t)whitenedA.rows();
i++)
156 for (
size_t j = 0;
j < (
size_t)whitenedA.cols();
j++) {
157 double s = whitenedA(
i,
j);
159 entries.emplace_back(nrows +
i, column_start +
j, s);
164 for (
size_t i = 0;
i < (
size_t)whitenedb.size();
i++) {
165 double s = whitenedb(
i);
166 if (
std::abs(s) > 1
e-12) entries.emplace_back(nrows +
i, ncols, s);
170 nrows += jacobianFactor->rows();
180 return sparseJacobian(
Ordering(this->
keys()), nrows, ncols);
184 Matrix GaussianFactorGraph::sparseJacobian_()
const {
185 gttic_(GaussianFactorGraph_sparseJacobian_matrix);
187 auto result = sparseJacobian();
193 const auto& entry =
result[k];
194 IJS(0, k) = double(std::get<0>(entry) + 1);
195 IJS(1, k) = double(std::get<1>(entry) + 1);
196 IJS(2, k) = std::get<2>(entry);
202 Matrix GaussianFactorGraph::augmentedJacobian(
210 Matrix GaussianFactorGraph::augmentedJacobian()
const {
217 pair<Matrix, Vector> GaussianFactorGraph::jacobian(
219 Matrix augmented = augmentedJacobian(ordering);
220 return make_pair(augmented.leftCols(augmented.cols() - 1),
221 augmented.col(augmented.cols() - 1));
225 pair<Matrix, Vector> GaussianFactorGraph::jacobian()
const {
226 Matrix augmented = augmentedJacobian();
227 return make_pair(augmented.leftCols(augmented.cols() - 1),
228 augmented.col(augmented.cols() - 1));
232 Matrix GaussianFactorGraph::augmentedHessian(
235 Scatter scatter(*
this, ordering);
241 Matrix GaussianFactorGraph::augmentedHessian()
const {
249 pair<Matrix, Vector> GaussianFactorGraph::hessian(
251 Matrix augmented = augmentedHessian(ordering);
252 size_t n = augmented.rows() - 1;
253 return make_pair(augmented.topLeftCorner(n, n), augmented.topRightCorner(n, 1));
257 pair<Matrix, Vector> GaussianFactorGraph::hessian()
const {
258 Matrix augmented = augmentedHessian();
259 size_t n = augmented.rows() - 1;
260 return make_pair(augmented.topLeftCorner(n, n), augmented.topRightCorner(n, 1));
268 factor->hessianDiagonalAdd(d);
275 map<Key,Matrix> GaussianFactorGraph::hessianBlockDiagonal()
const {
276 map<Key,Matrix> blocks;
278 if (!factor)
continue;
279 map<Key,Matrix> BD = factor->hessianBlockDiagonal();
280 map<Key,Matrix>::const_iterator it = BD.begin();
281 for (;it!=BD.end();++it) {
283 const Matrix& Bj = it->second;
287 blocks.emplace(j,Bj);
295 gttic(GaussianFactorGraph_optimize);
296 return BaseEliminateable::eliminateMultifrontal(
function)->optimize();
301 gttic(GaussianFactorGraph_optimize);
302 return BaseEliminateable::eliminateMultifrontal(ordering,
function)->optimize();
308 gttic(GaussianFactorGraph_optimizeDensely);
319 size_t n = augmented.rows() - 1;
320 auto AtA = augmented.topLeftCorner(n, n);
321 auto eta = augmented.topRightCorner(n, 1);
335 result = boost::make_shared<JacobianFactor>(*gf);
346 Vector e = Ai->error_vector(x0);
347 Ai->transposeMultiplyAdd(1.0, e, g);
357 if (!factor)
continue;
367 gttic(GaussianFactorGraph_optimizeGradientSearch);
369 gttic(Compute_Gradient);
372 double gradientSqNorm = grad.
dot(grad);
373 gttoc(Compute_Gradient);
380 gttic(Compute_minimizing_step_size);
382 double step = -gradientSqNorm /
dot(Rg, Rg);
383 gttoc(Compute_minimizing_step_size);
385 gttic(Compute_point);
388 gttoc(Compute_point);
398 e.push_back((*Ai) * x);
404 void GaussianFactorGraph::multiplyHessianAdd(
double alpha,
407 f->multiplyHessianAdd(alpha, x, y);
412 multiplyInPlace(x, e.begin());
416 void GaussianFactorGraph::multiplyInPlace(
const VectorValues&
x,
const Errors::iterator&
e)
const {
417 Errors::iterator ei =
e;
429 J::shared_ptr jacobian(boost::dynamic_pointer_cast<J>(factor));
430 if (jacobian && jacobian->get_model() && jacobian->get_model()->isConstrained()) {
439 void GaussianFactorGraph::transposeMultiplyAdd(
double alpha,
const Errors&
e,
442 Errors::const_iterator ei = e.begin();
445 Ai->transposeMultiplyAdd(alpha, *(ei++), x);
480 Errors::const_iterator ei = e.begin();
487 xi.first->second = Vector::Zero(Ai->getDim(
j));
488 xi.first->second += Ai->getA(
j).transpose() * *ei;
501 e.push_back(Ai->error_vector(x));
Vector3_ operator*(const Double_ &s, const Vector3_ &v)
def step(data, isam, result, truth, currPoseIndex)
static enum @843 ordering
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
VectorValues & addInPlace_(const VectorValues &c)
GaussianFactorGraph factors(list_of(factor1)(factor2)(factor3))
const Solve< LLT, Rhs > solve(const MatrixBase< Rhs > &b) const
IsDerived< DERIVEDFACTOR > push_back(boost::shared_ptr< DERIVEDFACTOR > factor)
Add a factor directly using a shared_ptr.
void g(const string &key, int i)
std::pair< iterator, bool > tryInsert(Key j, const Vector &value)
const SymmetricBlockMatrix & info() const
Return underlying information matrix.
Scalar EIGEN_BLAS_FUNC() dot(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
constABlock::ConstColXpr constBVector
Standard Cholesky decomposition (LL^T) of a matrix and associated features.
boost::shared_ptr< GaussianFactor > sharedFactor
Shared pointer to a factor.
boost::shared_ptr< This > shared_ptr
A shared_ptr to this class.
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Contains the HessianFactor class, a general quadratic factor.
Array< double, 1, 3 > e(1./3., 0.5, 2.)
Matrix augmentedJacobian() const override
Linear Factor Graph where all factors are Gaussians.
double dot(const VectorValues &v) const
JacobiRotation< float > J
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
bool hasConstraints(const GaussianFactorGraph &factors)
std::vector< std::tuple< int, int, double > > SparseTriplets
Expression of a fixed-size or dynamic-size block.
int optimize(const SfmData &db, const NonlinearFactorGraph &graph, const Values &initial, bool separateCalibration=false)
Eigen::SelfAdjointView< constBlock, Eigen::Upper > selfadjointView(DenseIndex I, DenseIndex J) const
Return the square sub-matrix that contains blocks(i:j, i:j).
boost::function< EliminationResult(const FactorGraphType &, const Ordering &)> Eliminate
The function type that does a single dense elimination step on a subgraph.
A Gaussian factor using the canonical parameters (information form)
KeyVector::const_iterator const_iterator
Const iterator over keys.
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
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
Gaussian Bayes Tree, the result of eliminating a GaussianJunctionTree.
std::uint64_t Key
Integer nonlinear key type.
Efficient incomplete Cholesky on rank-deficient matrices, todo: constrained Cholesky.