| 
									
										
										
										
											2014-09-30 18:30:15 +08:00
										 |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * GTSAM Copyright 2010, Georgia Tech Research Corporation,  | 
					
						
							|  |  |  |  * Atlanta, Georgia 30332-0415 | 
					
						
							|  |  |  |  * All Rights Reserved | 
					
						
							|  |  |  |  * Authors: Frank Dellaert, et al. (see THANKS for the full author list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * See LICENSE for the license information | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * @file Expression.h | 
					
						
							|  |  |  |  * @date September 18, 2014 | 
					
						
							|  |  |  |  * @author Frank Dellaert | 
					
						
							|  |  |  |  * @author Paul Furgale | 
					
						
							|  |  |  |  * @brief Expressions for Block Automatic Differentiation | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-30 18:34:03 +08:00
										 |  |  | #include <gtsam_unstable/nonlinear/Expression.h>
 | 
					
						
							| 
									
										
										
										
											2014-09-30 18:30:15 +08:00
										 |  |  | #include <gtsam/nonlinear/NonlinearFactor.h>
 | 
					
						
							| 
									
										
										
										
											2014-10-02 17:01:39 +08:00
										 |  |  | #include <gtsam/base/Testable.h>
 | 
					
						
							| 
									
										
										
										
											2014-09-30 18:30:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * BAD Factor that supports arbitrary expressions via AD | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | template<class T> | 
					
						
							| 
									
										
										
										
											2014-10-02 17:01:39 +08:00
										 |  |  | class BADFactor: public NoiseModelFactor { | 
					
						
							| 
									
										
										
										
											2014-09-30 18:30:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const T measurement_; | 
					
						
							|  |  |  |   const Expression<T> expression_; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// Constructor
 | 
					
						
							| 
									
										
										
										
											2014-10-02 17:01:39 +08:00
										 |  |  |   BADFactor(const SharedNoiseModel& noiseModel, //
 | 
					
						
							|  |  |  |       const T& measurement, const Expression<T>& expression) : | 
					
						
							|  |  |  |       NoiseModelFactor(noiseModel, expression.keys()), //
 | 
					
						
							| 
									
										
										
										
											2014-09-30 18:30:15 +08:00
										 |  |  |       measurement_(measurement), expression_(expression) { | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-02 17:01:39 +08:00
										 |  |  |   /**
 | 
					
						
							|  |  |  |    * Error function *without* the NoiseModel, \f$ z-h(x) \f$. | 
					
						
							|  |  |  |    * We override this method to provide | 
					
						
							|  |  |  |    * both the function evaluation and its derivative(s) in H. | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   virtual Vector unwhitenedError(const Values& x, | 
					
						
							|  |  |  |       boost::optional<std::vector<Matrix>&> H = boost::none) const { | 
					
						
							|  |  |  |     if (H) { | 
					
						
							| 
									
										
										
										
											2014-10-02 19:30:16 +08:00
										 |  |  |       assert(H->size()==size()); | 
					
						
							| 
									
										
										
										
											2014-10-02 17:01:39 +08:00
										 |  |  |       typedef std::map<Key, Matrix> MapType; | 
					
						
							|  |  |  |       MapType terms; | 
					
						
							| 
									
										
										
										
											2014-10-03 16:25:02 +08:00
										 |  |  |       Augmented<T> augmented = expression_.augmented(x); | 
					
						
							|  |  |  |       // copy terms to H, which is pre-allocated to correct size
 | 
					
						
							|  |  |  |       // TODO apply move semantics
 | 
					
						
							| 
									
										
										
										
											2014-10-02 19:30:16 +08:00
										 |  |  |       size_t j = 0; | 
					
						
							| 
									
										
										
										
											2014-10-03 16:25:02 +08:00
										 |  |  |       MapType::const_iterator it = augmented.jacobians().begin(); | 
					
						
							|  |  |  |       for (; it != augmented.jacobians().end(); ++it) | 
					
						
							|  |  |  |         (*H)[j++] = it->second; | 
					
						
							|  |  |  |       return measurement_.localCoordinates(augmented.value()); | 
					
						
							| 
									
										
										
										
											2014-10-02 17:01:39 +08:00
										 |  |  |     } else { | 
					
						
							|  |  |  |       const T& value = expression_.value(x); | 
					
						
							|  |  |  |       return measurement_.localCoordinates(value); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-09-30 18:30:15 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | // BADFactor
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 |