23#include <gtsam/config.h>
63 typedef std::shared_ptr<ExpressionFactor<T> > shared_ptr;
85 void print(
const std::string& s =
"",
120 return std::shared_ptr<JacobianFactor>();
124 if (noiseModel_ && noiseModel_->isConstrained()) {
125 noiseModel = std::static_pointer_cast<noiseModel::Constrained>(
126 noiseModel_)->unit();
130 std::shared_ptr<JacobianFactor> factor(
135 internal::JacobianMap jacobianMap(
keys_, Ab);
141 T value =
expression_.valueAndJacobianMap(x, jacobianMap);
148 Vector b = Ab(
size()).col(0);
149 noiseModel_->WhitenSystem(Ab.
matrix(), b);
156 gtsam::NonlinearFactor::shared_ptr
clone()
const override {
157 return std::static_pointer_cast<gtsam::NonlinearFactor>(
158 gtsam::NonlinearFactor::shared_ptr(
new This(*
this)));
174 throw std::invalid_argument(
"ExpressionFactor: no NoiseModel.");
175 if (noiseModel_->dim() != Dim)
176 throw std::invalid_argument(
177 "ExpressionFactor was created with a NoiseModel of incorrect dimension.");
190 std::map<Key, int> keyedDims;
199 throw std::runtime_error(
"ExpressionFactor::expression not provided: cannot deserialize.");
203#if GTSAM_ENABLE_BOOST_SERIALIZATION
205 template <
class Archive>
206 void save(Archive& ar,
const unsigned int )
const {
208 ar << boost::serialization::make_nvp(
"measured_", this->measured_);
213 template <
class Archive>
214 void load(Archive& ar,
const unsigned int ) {
216 ar >> boost::serialization::make_nvp(
"measured_", this->measured_);
221 BOOST_SERIALIZATION_SPLIT_MEMBER()
223 friend class boost::serialization::access;
243template <
typename T,
typename... Args>
246 static const std::size_t NARY_EXPRESSION_SIZE =
sizeof...(Args);
247 using ArrayNKeys = std::array<Key, NARY_EXPRESSION_SIZE>;
259 throw std::runtime_error(
260 "ExpressionFactorN::expression not provided: cannot deserialize.");
269 const T &measurement)
271 for (
const auto &key :
keys)
285#if GTSAM_ENABLE_BOOST_SERIALIZATION
286 friend class boost::serialization::access;
287 template <
class ARCHIVE>
288 void serialize(ARCHIVE &ar,
const unsigned int ) {
289 ar &boost::serialization::make_nvp(
291 boost::serialization::base_object<ExpressionFactor<T>>(*
this));
296template <
typename T,
typename... Args>
298 :
public Testable<ExpressionFactorN<T, Args...>> {};
Concept check for values that can be used in unit tests.
Expressions for Block Automatic Differentiation.
Non-linear factor base classes.
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
FastVector is a type alias to a std::vector with a custom memory allocator.
Definition FastVector.h:33
Global functions in a separate testing namespace.
Definition chartTesting.h:28
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition Key.cpp:30
void save(const Matrix &A, const string &s, const string &filename)
save a matrix to file, which can be loaded by matlab
Definition Matrix.cpp:154
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
std::vector< Matrix > * OptionalMatrixVecType
The OptionalMatrixVecType is a pointer to a vector of matrices.
Definition NonlinearFactor.h:63
noiseModel::Base::shared_ptr SharedNoiseModel
Aliases.
Definition NoiseModel.h:846
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
All noise models live in the noiseModel namespace.
Definition LossFunctions.cpp:33
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
A testable concept check that should be placed in applicable unit tests and in generic algorithms.
Definition Testable.h:59
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152
This class stores a dense matrix and allows it to be accessed as a collection of vertical blocks.
Definition VerticalBlockMatrix.h:47
const Matrix & matrix() const
Access to full matrix (including any portions excluded by rowStart(), rowEnd(), and firstBlock()).
Definition VerticalBlockMatrix.h:278
const KeyVector & keys() const
Access the factor's involved variable keys.
Definition Factor.h:143
KeyVector keys_
The keys involved in this factor.
Definition Factor.h:88
size_t size() const
Definition Factor.h:160
A Gaussian factor in the squared-error form.
Definition JacobianFactor.h:92
Factor that supports arbitrary expressions via AD.
Definition ExpressionFactor.h:46
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print relies on Testable traits being defined for T
Definition ExpressionFactor.h:85
gtsam::NonlinearFactor::shared_ptr clone() const override
Definition ExpressionFactor.h:156
virtual Expression< T > expression() const
Recreate expression from keys_ and measured_, used in load below.
Definition ExpressionFactor.h:198
Expression< T > expression_
the expression that is AD enabled
Definition ExpressionFactor.h:55
T measured_
the measurement to be compared with the expression
Definition ExpressionFactor.h:54
FastVector< int > dims_
dimensions of the Jacobian matrices
Definition ExpressionFactor.h:56
Vector unwhitenedError(const Values &x, OptionalMatrixVecType H=nullptr) const override
Error function without the NoiseModel, .
Definition ExpressionFactor.h:104
std::shared_ptr< GaussianFactor > linearize(const Values &x) const override
linearize to a GaussianFactor
Definition ExpressionFactor.h:117
const T & measured() const
return the measurement
Definition ExpressionFactor.h:82
~ExpressionFactor() override
Destructor.
Definition ExpressionFactor.h:79
bool equals(const NonlinearFactor &f, double tol) const override
equals relies on Testable traits being defined for T
Definition ExpressionFactor.h:92
ExpressionFactor(const SharedNoiseModel &noiseModel, const T &measurement)
Default constructor, for serialization.
Definition ExpressionFactor.h:166
ExpressionFactor(const SharedNoiseModel &noiseModel, const T &measurement, const Expression< T > &expression)
Constructor: creates a factor from a measurement and measurement function.
Definition ExpressionFactor.h:72
void initialize(const Expression< T > &expression)
Initialize with constructor arguments.
Definition ExpressionFactor.h:172
Expression class that supports automatic differentiation.
Definition Expression.h:49
N-ary variadic template for ExpressionFactor meant as a base class for N-ary factors.
Definition ExpressionFactor.h:244
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:258
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:268
Nonlinear factor base class.
Definition NonlinearFactor.h:70
virtual bool active(const Values &c) const
Checks whether a factor should be used based on a set of values.
Definition NonlinearFactor.h:143
A nonlinear sum-of-squares factor with a zero-mean noise model implementing the density Templated on...
Definition NonlinearFactor.h:208
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
Print.
Definition NonlinearFactor.cpp:82
virtual Vector unwhitenedError(const Values &x, OptionalMatrixVecType H=nullptr) const =0
Error function without the NoiseModel, .
bool equals(const NonlinearFactor &f, double tol=1e-9) const override
Check if two factors are equal.
Definition NonlinearFactor.cpp:90
NoiseModelFactor()
Default constructor for I/O only.
Definition NonlinearFactor.h:223
const SharedNoiseModel & noiseModel() const
access to the noise model
Definition NonlinearFactor.h:258
A non-templated config holding any types of Manifold-group elements.
Definition Values.h:65
noise model to the factor, and calculates the error by asking the user to implement the method