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