| 
									
										
										
										
											2010-10-14 12:54:38 +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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  |  * @file    GaussianConditional.h | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |  * @brief   Conditional Gaussian Base class | 
					
						
							|  |  |  |  * @author  Christian Potthast | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // \callgraph
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | #include <list>
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | #include <boost/utility.hpp>
 | 
					
						
							|  |  |  | #include <boost/shared_ptr.hpp>
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | #include <boost/numeric/ublas/triangular.hpp>
 | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | #include <gtsam/base/types.h>
 | 
					
						
							| 
									
										
										
										
											2010-08-20 01:23:19 +08:00
										 |  |  | #include <gtsam/inference/Conditional.h>
 | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  | #include <gtsam/linear/VectorValues.h>
 | 
					
						
							| 
									
										
										
										
											2010-08-20 01:23:19 +08:00
										 |  |  | #include <gtsam/base/Matrix.h>
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | #include <gtsam/base/blockMatrices.h>
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | class GaussianFactor; | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * A conditional Gaussian functions as the node in a Bayes network | 
					
						
							|  |  |  |  * It has a set of parents y,z, etc. and implements a probability density on x. | 
					
						
							|  |  |  |  * The negative log-probability is given by || Rx - (d - Sy - Tz - ...)||^2 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | class GaussianConditional : public Conditional { | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |   typedef GaussianFactor FactorType; | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | 	typedef boost::shared_ptr<GaussianConditional> shared_ptr; | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	/** Store the conditional matrix as upper-triangular column-major */ | 
					
						
							|  |  |  | 	typedef boost::numeric::ublas::triangular_matrix<double, boost::numeric::ublas::upper, boost::numeric::ublas::column_major> matrix_type; | 
					
						
							|  |  |  | 	typedef VerticalBlockView<matrix_type> rsd_type; | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	typedef rsd_type::block_type r_type; | 
					
						
							|  |  |  |   typedef rsd_type::const_block_type const_r_type; | 
					
						
							|  |  |  |   typedef rsd_type::column_type d_type; | 
					
						
							|  |  |  |   typedef rsd_type::const_column_type const_d_type; | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | protected: | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	/** Store the conditional as one big upper-triangular wide matrix, arranged
 | 
					
						
							|  |  |  | 	 * as [ R S1 S2 ... d ].  Access these blocks using a VerticalBlockView. */ | 
					
						
							|  |  |  | 	matrix_type matrix_; | 
					
						
							|  |  |  | 	rsd_type rsd_; | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-10 22:44:53 +08:00
										 |  |  | 	/** vector of standard deviations */ | 
					
						
							|  |  |  | 	Vector sigmas_; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | public: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** default constructor needed for serialization */ | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	GaussianConditional(); | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/** constructor */ | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | 	GaussianConditional(Index key); | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/** constructor with no parents
 | 
					
						
							|  |  |  | 	 * |Rx-d| | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | 	GaussianConditional(Index key, const Vector& d, const Matrix& R, const Vector& sigmas); | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/** constructor with only one parent
 | 
					
						
							|  |  |  | 	 * |Rx+Sy-d| | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | 	GaussianConditional(Index key, const Vector& d, const Matrix& R, | 
					
						
							|  |  |  | 			Index name1, const Matrix& S, const Vector& sigmas); | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/** constructor with two parents
 | 
					
						
							|  |  |  | 	 * |Rx+Sy+Tz-d| | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | 	GaussianConditional(Index key, const Vector& d, const Matrix& R, | 
					
						
							|  |  |  | 			Index name1, const Matrix& S, Index name2, const Matrix& T, const Vector& sigmas); | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * constructor with number of arbitrary parents | 
					
						
							|  |  |  | 	 * |Rx+sum(Ai*xi)-d| | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | 	GaussianConditional(Index key, const Vector& d, | 
					
						
							|  |  |  | 			const Matrix& R, const std::list<std::pair<Index, Matrix> >& parents, const Vector& sigmas); | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * Constructor when matrices are already stored in a combined matrix, allows | 
					
						
							|  |  |  | 	 * for multiple frontal variables. | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	template<typename Iterator, class Matrix> | 
					
						
							| 
									
										
										
										
											2010-10-12 08:14:50 +08:00
										 |  |  | 	GaussianConditional(Iterator firstKey, Iterator lastKey, size_t nrFrontals, const VerticalBlockView<Matrix>& matrices, const Vector& sigmas); | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/** print */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | 	void print(const std::string& = "GaussianConditional") const; | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/** equals function */ | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	bool equals(const GaussianConditional &cg, double tol = 1e-9) const; | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //	/** permute the variables in the conditional */
 | 
					
						
							|  |  |  | //	void permuteWithInverse(const Permutation& inversePermutation);
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //  /** Permute the variables when only separator variables need to be permuted.
 | 
					
						
							|  |  |  | //   * Returns true if any reordered variables appeared in the separator and
 | 
					
						
							|  |  |  | //   * false if not.
 | 
					
						
							|  |  |  | //   */
 | 
					
						
							|  |  |  | //  bool permuteSeparatorWithInverse(const Permutation& inversePermutation);
 | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	/** dimension of multivariate variable */ | 
					
						
							|  |  |  | 	size_t dim() const { return rsd_.size1(); } | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | 	/** return stuff contained in GaussianConditional */ | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	rsd_type::const_column_type get_d() const { return rsd_.column(1+nrParents(), 0); } | 
					
						
							|  |  |  | 	rsd_type::const_block_type get_R() const { return rsd_(0); } | 
					
						
							| 
									
										
										
										
											2010-10-14 04:43:58 +08:00
										 |  |  | 	rsd_type::const_block_type get_S(const_iterator variable) const { return rsd_(variable - this->begin()); } | 
					
						
							| 
									
										
										
										
											2009-11-05 14:59:59 +08:00
										 |  |  | 	const Vector& get_sigmas() const {return sigmas_;} | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/**
 | 
					
						
							| 
									
										
										
										
											2009-11-05 14:59:59 +08:00
										 |  |  | 	 * solve a conditional Gaussian | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  | 	 * @param x values structure in which the parents values (y,z,...) are known | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 	 * @return solution x = R \ (d - Sy - Tz - ...) | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  | 	Vector solve(const VectorValues& x) const; | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |   /**
 | 
					
						
							|  |  |  |    * solve a conditional Gaussian | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  |    * @param x values structure in which the parents values (y,z,...) are known | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |    * @return solution x = R \ (d - Sy - Tz - ...) | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  |   Vector solve(const Permuted<VectorValues>& x) const; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							|  |  |  |   rsd_type::column_type get_d_() { return rsd_.column(1+nrParents(), 0); } | 
					
						
							|  |  |  |   rsd_type::block_type get_R_() { return rsd_(0); } | 
					
						
							| 
									
										
										
										
											2010-10-14 04:43:58 +08:00
										 |  |  |   rsd_type::block_type get_S_(iterator variable) { return rsd_(variable - this->begin()); } | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   friend class GaussianFactor; | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //	/** Serialization function */
 | 
					
						
							|  |  |  | //	friend class boost::serialization::access;
 | 
					
						
							|  |  |  | //	template<class Archive>
 | 
					
						
							|  |  |  | //	void serialize(Archive & ar, const unsigned int version) {
 | 
					
						
							|  |  |  | //		ar & boost::serialization::make_nvp("Conditional", boost::serialization::base_object<Conditional>(*this));
 | 
					
						
							|  |  |  | //		ar & BOOST_SERIALIZATION_NVP(R_);
 | 
					
						
							|  |  |  | //		ar & BOOST_SERIALIZATION_NVP(parents_);
 | 
					
						
							|  |  |  | //		ar & BOOST_SERIALIZATION_NVP(d_);
 | 
					
						
							|  |  |  | //		ar & BOOST_SERIALIZATION_NVP(sigmas_);
 | 
					
						
							|  |  |  | //	}
 | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-10-12 08:14:50 +08:00
										 |  |  | 	template<typename Iterator, class Matrix> | 
					
						
							|  |  |  | 	GaussianConditional::GaussianConditional(Iterator firstKey, Iterator lastKey, | 
					
						
							|  |  |  | 			size_t nrFrontals, const VerticalBlockView<Matrix>& matrices, | 
					
						
							|  |  |  | 			const Vector& sigmas) : | 
					
						
							|  |  |  | 		rsd_(matrix_), sigmas_(sigmas) { | 
					
						
							|  |  |  |   	nrFrontals_ = nrFrontals; | 
					
						
							| 
									
										
										
										
											2010-10-14 04:43:58 +08:00
										 |  |  | 		std::copy(firstKey, lastKey, back_inserter(keys_)); | 
					
						
							| 
									
										
										
										
											2010-10-12 08:14:50 +08:00
										 |  |  | 		rsd_.assignNoalias(matrices); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } |