| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file    Vector.h | 
					
						
							|  |  |  |  * @brief   typedef and functions to augment Boost's ublas::vector<double> | 
					
						
							|  |  |  |  * @author  Kai Ni | 
					
						
							|  |  |  |  * @author  Frank Dellaert | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // \callgraph
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-18 14:11:19 +08:00
										 |  |  | #include <list>
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | #include <boost/numeric/ublas/vector.hpp>
 | 
					
						
							| 
									
										
										
										
											2010-02-16 07:53:16 +08:00
										 |  |  | #include <boost/numeric/ublas/vector_proxy.hpp>
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | #include <boost/random/linear_congruential.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Vector is a *global* typedef
 | 
					
						
							|  |  |  | // wrap-matlab does this typedef as well
 | 
					
						
							|  |  |  | #if ! defined (MEX_H)
 | 
					
						
							|  |  |  | typedef boost::numeric::ublas::vector<double> Vector; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2010-02-16 07:53:16 +08:00
										 |  |  | typedef boost::numeric::ublas::vector_range<Vector> SubVector; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-03 03:37:54 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * An auxiliary function to printf for Win32 compatibility, added by Kai | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | void odprintf(const char *format, ...); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  *  constructor with size and initial data, row order ! | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | Vector Vector_( size_t m, const double* const data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  *  nice constructor, dangerous as number of arguments must be exactly right | 
					
						
							|  |  |  |  *  and you have to pass doubles !!! always use 0.0 never 0 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | Vector Vector_(size_t m, ...); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Create vector initialized to a constant value | 
					
						
							|  |  |  |  * @param size | 
					
						
							|  |  |  |  * @param constant value | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | Vector repeat(size_t n, double value); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-11 22:42:09 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Create basis vector of dimension n, | 
					
						
							|  |  |  |  * with a constant in spot i | 
					
						
							|  |  |  |  * @param n is the size of the vector | 
					
						
							|  |  |  |  * @param index of the one | 
					
						
							|  |  |  |  * @param value is the value to insert into the vector | 
					
						
							|  |  |  |  * @return delta vector | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | Vector delta(size_t n, size_t i, double value); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Create basis vector of dimension n, | 
					
						
							|  |  |  |  * with one in spot i | 
					
						
							|  |  |  |  * @param n is the size of the vector | 
					
						
							|  |  |  |  * @param index of the one | 
					
						
							|  |  |  |  * @return basis vector | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | inline Vector basis(size_t n, size_t i) { return delta(n, i, 1.0); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Create zero vector | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  |  * @param size | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | inline Vector zero(size_t n) { return repeat(n,0.0);} | 
					
						
							| 
									
										
										
										
											2009-10-27 22:21:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Create vector initialized to ones | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  |  * @param size | 
					
						
							| 
									
										
										
										
											2009-10-27 22:21:22 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | inline Vector ones(size_t n) { return repeat(n,1.0);} | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 	 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * check if all zero | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | bool zero(const Vector& v); | 
					
						
							| 
									
										
										
										
											2009-12-28 20:37:34 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * dimensionality == size | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2010-01-10 14:35:16 +08:00
										 |  |  | inline size_t dim(const Vector& v) { return v.size(); } | 
					
						
							| 
									
										
										
										
											2010-01-14 13:58:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * print with optional string | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | void print(const Vector& v, const std::string& s = ""); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * operator==() | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | bool operator==(const Vector& vec1,const Vector& vec2); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-01 01:36:34 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Greater than or equal to operation | 
					
						
							|  |  |  |  * returns true if all elements in v1 | 
					
						
							|  |  |  |  * are greater than corresponding elements in v2 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | bool greaterThanOrEqual(const Vector& v1, const Vector& v2); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * VecA == VecB up to tolerance | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | bool equal_with_abs_tol(const Vector& vec1, const Vector& vec2, double tol=1e-9); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-10 14:35:16 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Override of equal in Lie.h | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | inline bool equal(const Vector& vec1, const Vector& vec2, double tol) { | 
					
						
							|  |  |  |   return equal_with_abs_tol(vec1, vec2, tol); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Override of equal in Lie.h | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | inline bool equal(const Vector& vec1, const Vector& vec2) { | 
					
						
							|  |  |  |   return equal_with_abs_tol(vec1, vec2); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Same, prints if error | 
					
						
							|  |  |  |  * @param vec1 Vector | 
					
						
							|  |  |  |  * @param vec2 Vector | 
					
						
							|  |  |  |  * @param tol 1e-9 | 
					
						
							|  |  |  |  * @return bool | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | bool assert_equal(const Vector& vec1, const Vector& vec2, double tol=1e-9); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * extract subvector, slice semantics, i.e. range = [i1,i2[ excluding i2 | 
					
						
							|  |  |  |  * @param v Vector | 
					
						
							|  |  |  |  * @param i1 first row index | 
					
						
							|  |  |  |  * @param i2 last  row index + 1 | 
					
						
							|  |  |  |  * @return subvector v(i1:i2) | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | Vector sub(const Vector &v, size_t i1, size_t i2); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-01 01:24:01 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Inserts a subvector into a vector IN PLACE | 
					
						
							|  |  |  |  * @param big is the vector to be changed | 
					
						
							|  |  |  |  * @param small is the vector to insert | 
					
						
							|  |  |  |  * @param i is the index where the subvector should be inserted | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | void subInsert(Vector& big, const Vector& small, size_t i); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-13 14:14:55 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * elementwise multiplication | 
					
						
							|  |  |  |  * @param a first vector | 
					
						
							|  |  |  |  * @param b second vector | 
					
						
							|  |  |  |  * @return vector [a(i)*b(i)] | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | Vector emul(const Vector &a, const Vector &b); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * elementwise division | 
					
						
							|  |  |  |  * @param a first vector | 
					
						
							|  |  |  |  * @param b second vector | 
					
						
							|  |  |  |  * @return vector [a(i)/b(i)] | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | Vector ediv(const Vector &a, const Vector &b); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-31 18:29:25 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * elementwise division, but 0/0 = 0, not inf | 
					
						
							|  |  |  |  * @param a first vector | 
					
						
							|  |  |  |  * @param b second vector | 
					
						
							|  |  |  |  * @return vector [a(i)/b(i)] | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | Vector ediv_(const Vector &a, const Vector &b); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-13 14:14:55 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * sum vector elements | 
					
						
							|  |  |  |  * @param a vector | 
					
						
							|  |  |  |  * @return sum_i a(i) | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | double sum(const Vector &a); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-16 14:25:11 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * elementwise reciprocal of vector elements | 
					
						
							|  |  |  |  * @param a vector | 
					
						
							|  |  |  |  * @return [1/a(i)] | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | Vector reciprocal(const Vector &a); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-18 01:47:23 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * elementwise sqrt of vector elements | 
					
						
							|  |  |  |  * @param a vector | 
					
						
							|  |  |  |  * @return [sqrt(a(i))] | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | Vector esqrt(const Vector& v); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-21 15:38:37 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * absolut values of vector elements | 
					
						
							|  |  |  |  * @param a vector | 
					
						
							|  |  |  |  * @return [abs(a(i))] | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | Vector abs(const Vector& v); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-16 13:08:29 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * return the max element of a vector | 
					
						
							|  |  |  |  * @param a vector | 
					
						
							|  |  |  |  * @return max(a) | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | double max(const Vector &a); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-12 05:37:40 +08:00
										 |  |  | /** Dot product */ | 
					
						
							| 
									
										
										
										
											2010-01-30 10:04:37 +08:00
										 |  |  | double dot(const Vector &a, const Vector& b); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-30 12:01:49 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * BLAS Level 1 scal: x <- alpha*x | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | void scal(double alpha, Vector& x); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-30 10:04:37 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * BLAS Level 1 axpy: y <- alpha*x + y | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | void axpy(double alpha, const Vector& x, Vector& y); | 
					
						
							| 
									
										
										
										
											2010-02-16 07:53:16 +08:00
										 |  |  | void axpy(double alpha, const Vector& x, SubVector& y); | 
					
						
							| 
									
										
										
										
											2009-12-12 05:37:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-17 02:39:39 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Divide every element of a Vector into a scalar | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | Vector operator/(double s, const Vector& v); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * house(x,j) computes HouseHolder vector v and scaling factor beta | 
					
						
							|  |  |  |  *  from x, such that the corresponding Householder reflection zeroes out | 
					
						
							|  |  |  |  *  all but x.(j), j is base 0. Golub & Van Loan p 210. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2010-01-22 02:51:59 +08:00
										 |  |  | std::pair<double,Vector> house(const Vector &x); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-27 22:21:22 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Weighted Householder solution vector, | 
					
						
							|  |  |  |  * a.k.a., the pseudoinverse of the column | 
					
						
							| 
									
										
										
										
											2009-11-10 12:36:07 +08:00
										 |  |  |  * NOTE: if any sigmas are zero (indicating a constraint) | 
					
						
							|  |  |  |  * the pseudoinverse will be a selection vector, and the | 
					
						
							| 
									
										
										
										
											2010-01-16 14:25:11 +08:00
										 |  |  |  * variance will be zero | 
					
						
							| 
									
										
										
										
											2009-10-27 22:21:22 +08:00
										 |  |  |  * @param v is the first column of the matrix to solve | 
					
						
							| 
									
										
										
										
											2010-01-16 14:25:11 +08:00
										 |  |  |  * @param weights is a vector of weights/precisions where w=1/(s*s) | 
					
						
							|  |  |  |  * @return a pair of the pseudoinverse of v and the associated precision/weight | 
					
						
							| 
									
										
										
										
											2009-10-27 22:21:22 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2010-01-16 14:25:11 +08:00
										 |  |  | std::pair<Vector, double> | 
					
						
							|  |  |  | weightedPseudoinverse(const Vector& v, const Vector& weights); | 
					
						
							| 
									
										
										
										
											2009-10-29 20:52:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-16 12:57:58 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Fast version *no error checking* ! | 
					
						
							| 
									
										
										
										
											2010-01-16 14:25:11 +08:00
										 |  |  |  * Pass in initialized vector pseudo of size(weights) or will crash ! | 
					
						
							| 
									
										
										
										
											2010-01-16 12:57:58 +08:00
										 |  |  |  * @return the precision, pseudoinverse in third argument | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2010-01-16 14:25:11 +08:00
										 |  |  | double weightedPseudoinverse(const Vector& a, const Vector& weights, Vector& pseudo); | 
					
						
							| 
									
										
										
										
											2010-01-16 12:57:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * concatenate Vectors | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2009-12-18 14:11:19 +08:00
										 |  |  | Vector concatVectors(const std::list<Vector>& vs); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-18 14:11:19 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * concatenate Vectors | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | Vector concatVectors(size_t nrVectors, ...); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * random vector | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | Vector rand_vector_norm(size_t dim, double mean = 0, double sigma = 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } // namespace gtsam
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static boost::minstd_rand generator(42u); | 
					
						
							|  |  |  | 
 |