23#include <gtsam/config.h>
58 typedef boost::shared_ptr<ExpressionFactor<T> > shared_ptr;
80 void print(
const std::string& s =
"",
81 const KeyFormatter& keyFormatter = DefaultKeyFormatter)
const override {
100 boost::optional<std::vector<Matrix>&> H = boost::none)
const override {
115 return boost::shared_ptr<JacobianFactor>();
119 if (noiseModel_ && noiseModel_->isConstrained()) {
120 noiseModel = boost::static_pointer_cast<noiseModel::Constrained>(
121 noiseModel_)->unit();
125 boost::shared_ptr<JacobianFactor> factor(
130 internal::JacobianMap jacobianMap(
keys_, Ab);
136 T value =
expression_.valueAndJacobianMap(x, jacobianMap);
143 Vector b = Ab(
size()).col(0);
144 noiseModel_->WhitenSystem(Ab.
matrix(), b);
151 gtsam::NonlinearFactor::shared_ptr
clone()
const override {
152 return boost::static_pointer_cast<gtsam::NonlinearFactor>(
153 gtsam::NonlinearFactor::shared_ptr(
new This(*
this)));
169 throw std::invalid_argument(
"ExpressionFactor: no NoiseModel.");
170 if (noiseModel_->dim() != Dim)
171 throw std::invalid_argument(
172 "ExpressionFactor was created with a NoiseModel of incorrect dimension.");
185 std::map<Key, int> keyedDims;
194 throw std::runtime_error(
"ExpressionFactor::expression not provided: cannot deserialize.");
199 template <
class Archive>
200 void save(Archive& ar,
const unsigned int )
const {
202 ar << boost::serialization::make_nvp(
"measured_", this->measured_);
207 template <
class Archive>
208 void load(Archive& ar,
const unsigned int ) {
210 ar >> boost::serialization::make_nvp(
"measured_", this->measured_);
215 BOOST_SERIALIZATION_SPLIT_MEMBER()
217 friend class boost::serialization::access;
220 enum { NeedsToAlign = (
sizeof(T) % 16) == 0 };
241template <
typename T,
typename... Args>
244 static const std::size_t NARY_EXPRESSION_SIZE =
sizeof...(Args);
245 using ArrayNKeys = std::array<Key, NARY_EXPRESSION_SIZE>;
257 throw std::runtime_error(
258 "ExpressionFactorN::expression not provided: cannot deserialize.");
267 const T &measurement)
269 for (
const auto &key :
keys)
280 return expression(
keys);
283 friend class boost::serialization::access;
284 template <
class ARCHIVE>
285 void serialize(ARCHIVE &ar,
const unsigned int ) {
286 ar &boost::serialization::make_nvp(
288 boost::serialization::base_object<ExpressionFactor<T>>(*
this));
292template <
typename T,
typename... Args>
294 :
public Testable<ExpressionFactorN<T, Args...>> {};
298#if defined(GTSAM_ALLOW_DEPRECATED_SINCE_V41)
307template <
typename T,
typename A1,
typename A2>
311 ~ExpressionFactor2()
override {}
314 Vector evaluateError(
const A1 &a1,
const A2 &a2,
315 boost::optional<Matrix &> H1 = boost::none,
316 boost::optional<Matrix &> H2 = boost::none)
const {
318 values.insert(this->keys_[0], a1);
319 values.insert(this->keys_[1], a2);
320 std::vector<Matrix> H(2);
322 if (H1) (*H1) = H[0];
323 if (H2) (*H2) = H[1];
329 virtual Expression<T> expression(
Key key1,
Key key2)
const {
330 throw std::runtime_error(
331 "ExpressionFactor2::expression not provided: cannot deserialize.");
334 expression(
const typename ExpressionFactorN<T, A1, A2>::ArrayNKeys &keys)
336 return expression(keys[0], keys[1]);
341 ExpressionFactor2() {}
345 const T &measurement)
346 : ExpressionFactorN<T, A1, A2>({key1, key2}, noiseModel, measurement) {}
#define GTSAM_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
This marks a GTSAM object to require alignment.
Definition: types.h:286
Concept check for values that can be used in unit tests.
Expressions for Block Automatic Differentiation.
Non-linear factor base classes.
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
noiseModel::Base::shared_ptr SharedNoiseModel
Note, deliberately not in noiseModel namespace.
Definition: NoiseModel.h:736
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:69
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: Key.h:35
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
Definition: Testable.h:58
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:151
Definition: VerticalBlockMatrix.h:42
const Matrix & matrix() const
Access to full matrix (including any portions excluded by rowStart(), rowEnd(), and firstBlock())
Definition: VerticalBlockMatrix.h:187
This is the base class for all factor types.
Definition: Factor.h:56
const KeyVector & keys() const
Access the factor's involved variable keys.
Definition: Factor.h:125
KeyVector keys_
The keys involved in this factor.
Definition: Factor.h:73
size_t size() const
Definition: Factor.h:136
A Gaussian factor in the squared-error form.
Definition: JacobianFactor.h:91
Factor that supports arbitrary expressions via AD.
Definition: ExpressionFactor.h:44
Vector unwhitenedError(const Values &x, boost::optional< std::vector< Matrix > & > H=boost::none) const override
Error function without the NoiseModel, .
Definition: ExpressionFactor.h:99
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print relies on Testable traits being defined for T
Definition: ExpressionFactor.h:80
gtsam::NonlinearFactor::shared_ptr clone() const override
Definition: ExpressionFactor.h:151
virtual Expression< T > expression() const
Recreate expression from keys_ and measured_, used in load below.
Definition: ExpressionFactor.h:193
Expression< T > expression_
the expression that is AD enabled
Definition: ExpressionFactor.h:53
T measured_
the measurement to be compared with the expression
Definition: ExpressionFactor.h:52
FastVector< int > dims_
dimensions of the Jacobian matrices
Definition: ExpressionFactor.h:54
const T & measured() const
return the measurement
Definition: ExpressionFactor.h:77
boost::shared_ptr< GaussianFactor > linearize(const Values &x) const override
linearize to a GaussianFactor
Definition: ExpressionFactor.h:112
~ExpressionFactor() override
Destructor.
Definition: ExpressionFactor.h:74
bool equals(const NonlinearFactor &f, double tol) const override
equals relies on Testable traits being defined for T
Definition: ExpressionFactor.h:87
ExpressionFactor(const SharedNoiseModel &noiseModel, const T &measurement)
Default constructor, for serialization.
Definition: ExpressionFactor.h:161
ExpressionFactor(const SharedNoiseModel &noiseModel, const T &measurement, const Expression< T > &expression)
Constructor: creates a factor from a measurement and measurement function.
Definition: ExpressionFactor.h:67
void initialize(const Expression< T > &expression)
Initialize with constructor arguments.
Definition: ExpressionFactor.h:167
Expression class that supports automatic differentiation.
Definition: Expression.h:48
N-ary variadic template for ExpressionFactor meant as a base class for N-ary factors.
Definition: ExpressionFactor.h:242
virtual Expression< T > expression(const ArrayNKeys &keys) const
Recreate expression from given keys_ and measured_, used in load Needed to deserialize a derived fact...
Definition: ExpressionFactor.h:256
~ExpressionFactorN() override=default
Destructor.
ExpressionFactorN()=default
Default constructor, for serialization.
ExpressionFactorN(const ArrayNKeys &keys, const SharedNoiseModel &noiseModel, const T &measurement)
Constructor takes care of keys, but still need to call initialize.
Definition: ExpressionFactor.h:266
Nonlinear factor base class.
Definition: NonlinearFactor.h:43
virtual bool active(const Values &) const
Checks whether a factor should be used based on a set of values.
Definition: NonlinearFactor.h:106
A nonlinear sum-of-squares factor with a zero-mean noise model implementing the density Templated on...
Definition: NonlinearFactor.h:162
bool equals(const NonlinearFactor &f, double tol=1e-9) const override
Check if two factors are equal.
Definition: NonlinearFactor.cpp:71
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
Print.
Definition: NonlinearFactor.cpp:63
NoiseModelFactor()
Default constructor for I/O only.
Definition: NonlinearFactor.h:177
const SharedNoiseModel & noiseModel() const
access to the noise model
Definition: NonlinearFactor.h:211
A non-templated config holding any types of Manifold-group elements.
Definition: Values.h:63