34 class ExpressionFactor:
public NoiseModelFactor {
35 BOOST_CONCEPT_ASSERT((IsTestable<T>));
39 typedef ExpressionFactor<T> This;
40 static const int Dim = traits<T>::dimension;
48 typedef boost::shared_ptr<ExpressionFactor<T> > shared_ptr;
70 void print(
const std::string& s =
"",
71 const KeyFormatter& keyFormatter = DefaultKeyFormatter)
const {
90 boost::optional<std::vector<Matrix>&> H = boost::none)
const {
105 return boost::shared_ptr<JacobianFactor>();
109 if (noiseModel_ && noiseModel_->isConstrained()) {
110 noiseModel = boost::static_pointer_cast<noiseModel::Constrained>(
111 noiseModel_)->unit();
115 boost::shared_ptr<JacobianFactor> factor(
120 internal::JacobianMap jacobianMap(
keys_, Ab);
126 T value =
expression_.valueAndJacobianMap(x, jacobianMap);
133 Vector b = Ab(
size()).col(0);
134 noiseModel_->WhitenSystem(Ab.
matrix(), b);
141 virtual gtsam::NonlinearFactor::shared_ptr
clone()
const {
142 return boost::static_pointer_cast<gtsam::NonlinearFactor>(
143 gtsam::NonlinearFactor::shared_ptr(
new This(*
this)));
159 throw std::invalid_argument(
"ExpressionFactor: no NoiseModel.");
160 if (noiseModel_->dim() != Dim)
161 throw std::invalid_argument(
162 "ExpressionFactor was created with a NoiseModel of incorrect dimension.");
175 std::map<Key, int> keyedDims;
184 throw std::runtime_error(
"ExpressionFactor::expression not provided: cannot deserialize.");
189 template <
class Archive>
190 void save(Archive& ar,
const unsigned int )
const {
192 ar << boost::serialization::make_nvp(
"measured_", this->measured_);
197 template <
class Archive>
198 void load(Archive& ar,
const unsigned int ) {
200 ar >> boost::serialization::make_nvp(
"measured_", this->measured_);
205 BOOST_SERIALIZATION_SPLIT_MEMBER()
207 friend class boost::serialization::access;
210 enum { NeedsToAlign = (
sizeof(T) % 16) == 0 };
212 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
217 template <
typename T>
225 template <
typename T,
typename A1,
typename A2>
233 boost::optional<Matrix&> H1 = boost::none,
234 boost::optional<Matrix&> H2 = boost::none)
const {
238 std::vector<Matrix> H(2);
240 if (H1) (*H1) = H[0];
241 if (H2) (*H2) = H[1];
248 throw std::runtime_error(
"ExpressionFactor2::expression not provided: cannot deserialize.");
258 const T& measurement)
260 this->
keys_.push_back(key1);
261 this->
keys_.push_back(key2);
267 return expression(this->
keys_[0], this->
keys_[1]);
270 friend class boost::serialization::access;
271 template <
class ARCHIVE>
272 void serialize(ARCHIVE& ar,
const unsigned int ) {
273 ar& boost::serialization::make_nvp(
274 "ExpressionFactor", boost::serialization::base_object<ExpressionFactor<T> >(*
this));
void insert(Key j, const Value &val)
Add a variable with the given j, throws KeyAlreadyExists<J> if j is already present.
Definition: Values.cpp:133
This is the base class for all factor types.
Definition: Factor.h:54
A non-templated config holding any types of Manifold-group elements.
Definition: Values.h:70
FastVector< int > dims_
dimensions of the Jacobian matrices
Definition: ExpressionFactor.h:44
ExpressionFactor2()
Default constructor, for serialization.
Definition: ExpressionFactor.h:253
const T & measured() const
return the measurement
Definition: ExpressionFactor.h:67
virtual void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Print.
Definition: NonlinearFactor.cpp:63
virtual Expression< T > expression() const
Recreate expression from keys_ and measured_, used in load below.
Definition: ExpressionFactor.h:183
ExpressionFactor(const SharedNoiseModel &noiseModel, const T &measurement)
Default constructor, for serialization.
Definition: ExpressionFactor.h:151
ExpressionFactor(const SharedNoiseModel &noiseModel, const T &measurement, const Expression< T > &expression)
Constructor: creates a factor from a measurement and measurement function.
Definition: ExpressionFactor.h:57
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:57
virtual ~ExpressionFactor()
Destructor.
Definition: ExpressionFactor.h:64
bool equals(const NonlinearFactor &f, double tol) const
equals relies on Testable traits being defined for T
Definition: ExpressionFactor.h:77
virtual Expression< T > expression(Key key1, Key key2) const
Recreate expression from given keys_ and measured_, used in load Needed to deserialize a derived fact...
Definition: ExpressionFactor.h:247
virtual gtsam::NonlinearFactor::shared_ptr clone() const
Definition: ExpressionFactor.h:141
virtual boost::shared_ptr< GaussianFactor > linearize(const Values &x) const
linearize to a GaussianFactor
Definition: ExpressionFactor.h:102
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:150
ExpressionFactor2(Key key1, Key key2, const SharedNoiseModel &noiseModel, const T &measurement)
Constructor takes care of keys, but still need to call initialize.
Definition: ExpressionFactor.h:256
Binary specialization of ExpressionFactor meant as a base class for binary factors.
Definition: ExpressionFactor.h:226
Definition: VerticalBlockMatrix.h:41
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: Key.h:33
Nonlinear factor base class.
Definition: NonlinearFactor.h:50
const SharedNoiseModel & noiseModel() const
access to the noise model
Definition: NonlinearFactor.h:210
Vector evaluateError(const A1 &a1, const A2 &a2, boost::optional< Matrix & > H1=boost::none, boost::optional< Matrix & > H2=boost::none) const
Backwards compatible evaluateError, to make existing tests compile.
Definition: ExpressionFactor.h:232
size_t size() const
Definition: Factor.h:129
void initialize(const Expression< T > &expression)
Initialize with constructor arguments.
Definition: ExpressionFactor.h:157
virtual ~ExpressionFactor2()
Destructor.
Definition: ExpressionFactor.h:229
virtual Vector unwhitenedError(const Values &x, boost::optional< std::vector< Matrix > & > H=boost::none) const
Error function without the NoiseModel, .
Definition: ExpressionFactor.h:89
A nonlinear sum-of-squares factor with a zero-mean noise model implementing the density Templated on...
Definition: NonlinearFactor.h:161
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
Non-linear factor base classes.
Expression class that supports automatic differentiation.
Definition: Expression.h:49
A Gaussian factor in the squared-error form.
Definition: JacobianFactor.h:87
Factor that supports arbitrary expressions via AD.
Definition: Expression.h:38
virtual bool active(const Values &) const
Checks whether a factor should be used based on a set of values.
Definition: NonlinearFactor.h:113
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print relies on Testable traits being defined for T
Definition: ExpressionFactor.h:70
const Matrix & matrix() const
Access to full matrix (including any portions excluded by rowStart(), rowEnd(), and firstBlock())
Definition: VerticalBlockMatrix.h:187
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
Concept check for values that can be used in unit tests.
noiseModel::Base::shared_ptr SharedNoiseModel
Note, deliberately not in noiseModel namespace.
Definition: NoiseModel.h:1072
KeyVector keys_
The keys involved in this factor.
Definition: Factor.h:72
Expression< T > expression_
the expression that is AD enabled
Definition: ExpressionFactor.h:43
T measured_
the measurement to be compared with the expression
Definition: ExpressionFactor.h:42
virtual bool equals(const NonlinearFactor &f, double tol=1e-9) const
Check if two factors are equal.
Definition: NonlinearFactor.cpp:71
virtual double error(const Values &c) const
Calculate the error of the factor.
Definition: NonlinearFactor.cpp:97
NoiseModelFactor()
Default constructor for I/O only.
Definition: NonlinearFactor.h:176
Expressions for Block Automatic Differentiation.