FixedVector.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* ----------------------------------------------------------------------------
  2. * GTSAM Copyright 2010, Georgia Tech Research Corporation,
  3. * Atlanta, Georgia 30332-0415
  4. * All Rights Reserved
  5. * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
  6. * See LICENSE for the license information
  7. * -------------------------------------------------------------------------- */
  8. /**
  9. * @file FixedVector.h
  10. * @brief Extension of boost's bounded_vector to allow for fixed size operation
  11. * @author Alex Cunningham
  12. */
  13. #pragma once
  14. #include <stdarg.h>
  15. #include <gtsam/base/Vector.h>
  16. namespace gtsam {
  17. /**
  18. * Fixed size vectors - compatible with boost vectors, but with compile-type
  19. * size checking.
  20. */
  21. template<size_t N>
  22. class FixedVector : public Eigen::Matrix<double, N, 1> {
  23. public:
  24. typedef Eigen::Matrix<double, N, 1> Base;
  25. /** default constructor */
  26. FixedVector() {}
  27. /** copy constructors */
  28. FixedVector(const FixedVector& v) : Base(v) {}
  29. /** Convert from a variable-size vector to a fixed size vector */
  30. FixedVector(const Vector& v) : Base(v) {}
  31. /** Initialize with a C-style array */
  32. FixedVector(const double* values) {
  33. std::copy(values, values+N, this->data());
  34. }
  35. /**
  36. * Create vector initialized to a constant value
  37. * @param value constant value
  38. */
  39. inline static FixedVector repeat(double value) {
  40. return FixedVector(Base::Constant(value));
  41. }
  42. /**
  43. * Create basis vector of
  44. * with a constant in spot i
  45. * @param i index of the one
  46. * @param value is the value to insert into the vector
  47. * @return delta vector
  48. */
  49. inline static FixedVector delta(size_t i, double value) {
  50. return FixedVector(Base::Unit(i) * value);
  51. }
  52. /**
  53. * Create basis vector,
  54. * with one in spot i
  55. * @param i index of the one
  56. * @return basis vector
  57. */
  58. inline static FixedVector basis(size_t i) { return FixedVector(Base::Unit(i)); }
  59. /**
  60. * Create zero vector
  61. */
  62. inline static FixedVector zero() { return FixedVector(Base::Zero());}
  63. /**
  64. * Create vector initialized to ones
  65. */
  66. inline static FixedVector ones() { return FixedVector(FixedVector::Ones());}
  67. static size_t dim() { return Base::max_size; }
  68. void print(const std::string& name="") const { gtsam::print(Vector(*this), name); }
  69. template<size_t M>
  70. bool equals(const FixedVector<M>& other, double tol=1e-9) const {
  71. return false;
  72. }
  73. bool equals(const FixedVector& other, double tol=1e-9) const {
  74. return equal_with_abs_tol(*this,other,tol);
  75. }
  76. };
  77. } // \namespace