diff --git a/gtsam_unstable/nonlinear/Expression-inl.h b/gtsam_unstable/nonlinear/Expression-inl.h index d5c5f1279..43d3071ce 100644 --- a/gtsam_unstable/nonlinear/Expression-inl.h +++ b/gtsam_unstable/nonlinear/Expression-inl.h @@ -48,7 +48,8 @@ namespace gtsam { template class Expression; -typedef std::map > JacobianMap; +//typedef std::map > JacobianMap; +typedef std::vector > > JacobianMap; //----------------------------------------------------------------------------- /** @@ -78,16 +79,28 @@ struct CallRecord { template void handleLeafCase(const Eigen::Matrix& dTdA, JacobianMap& jacobians, Key key) { - JacobianMap::iterator it = jacobians.find(key); - it->second.block(0, 0) += dTdA; // block makes HUGE difference + for(JacobianMap::iterator it = jacobians.begin(); it != jacobians.end(); ++it) + { + if((*it).first == key) + { + (*it).second.block(0, 0) += dTdA; // block makes HUGE difference + break; + } + } } /// Handle Leaf Case for Dynamic Matrix type (slower) template<> void handleLeafCase( const Eigen::Matrix& dTdA, JacobianMap& jacobians, Key key) { - JacobianMap::iterator it = jacobians.find(key); - it->second += dTdA; + for(JacobianMap::iterator it = jacobians.begin(); it != jacobians.end(); ++it) + { + if((*it).first == key) + { + (*it).second += dTdA; // block makes HUGE difference + break; + } + } } //----------------------------------------------------------------------------- diff --git a/gtsam_unstable/nonlinear/ExpressionFactor.h b/gtsam_unstable/nonlinear/ExpressionFactor.h index 84456c934..c9f3fae86 100644 --- a/gtsam_unstable/nonlinear/ExpressionFactor.h +++ b/gtsam_unstable/nonlinear/ExpressionFactor.h @@ -87,12 +87,13 @@ public: // Create and zero out blocks to be passed to expression_ JacobianMap blocks; + blocks.reserve(size()); for (DenseIndex i = 0; i < size(); i++) { Matrix& Hi = H->at(i); Hi.resize(Dim, dimensions_[i]); Hi.setZero(); // zero out Eigen::Block block = Hi.block(0, 0, Dim, dimensions_[i]); - blocks.insert(std::make_pair(keys_[i], block)); + blocks.push_back(std::make_pair(keys_[i], block)); } T value = expression_.value(x, blocks); @@ -121,8 +122,9 @@ public: // Create blocks into Ab_ to be passed to expression_ JacobianMap blocks; + blocks.reserve(size()); for (DenseIndex i = 0; i < size(); i++) - blocks.insert(std::make_pair(keys_[i], Ab(i))); + blocks.push_back(std::make_pair(keys_[i], Ab(i))); // Evaluate error to get Jacobians and RHS vector b T value = expression_.value(x, blocks); // <<< Reverse AD happens here !