/* ---------------------------------------------------------------------------- * 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 Cyclic.h * @brief Cyclic group, i.e., the integers modulo N * @author Frank Dellaert **/ #include #include namespace gtsam { /// Additive Group template class AdditiveGroup { }; /// Cyclic group of order N template class Cyclic : AdditiveGroup > { size_t i_; ///< we just use an unsigned int public: /// Constructor Cyclic(size_t i) : i_(i) { } /// Cast to size_t operator size_t() const { return i_; } /// Addition modulo N Cyclic operator+(const Cyclic& h) const { return (i_ + h.i_) % N; } /// Subtraction modulo N Cyclic operator-(const Cyclic& h) const { return (N + i_ - h.i_) % N; } /// Inverse Cyclic operator-() const { return (N - i_) % N; } /// print with optional string void print(const std::string& s = "") const { std::cout << s << i_ << std::endl; } /// equals with an tolerance, prints out message if unequal bool equals(const Cyclic& other, double tol = 1e-9) const { return other.i_ == i_; } }; namespace traits { /// Define Cyclic to be a model of the Group concept template struct structure_category > { typedef group_tag type; }; } // \namespace gtsam::traits namespace group { template AdditiveGroup compose(const AdditiveGroup&g, const AdditiveGroup& h) { return g + h; } template AdditiveGroup between(const AdditiveGroup&g, const AdditiveGroup& h) { return h - g; } template AdditiveGroup inverse(const AdditiveGroup&g) { return -g; } namespace traits { /// Define the trait that specifies Cyclic's identity element template struct identity > { static const Cyclic value; typedef Cyclic value_type; }; template const Cyclic identity >::value = Cyclic(0); /// Define the trait that asserts AdditiveGroup is an additive group template struct flavor > { typedef additive_tag type; }; } // \namespace gtsam::group::traits } // \namespace gtsam::group } // \namespace gtsam