Attempt at satisfying Group concept by deriving from base class. Needs to be fixed, also, test does not link :-(

release/4.3a0
dellaert 2014-12-07 13:24:59 +01:00
parent 022e930085
commit ef58a8a56a
2 changed files with 29 additions and 11 deletions

View File

@ -20,8 +20,15 @@
namespace gtsam { namespace gtsam {
/// Additive Group
template<typename Derived>
class AdditiveGroup {
};
/// Cyclic group of order N
template<size_t N> template<size_t N>
class Cyclic { class Cyclic : AdditiveGroup<Cyclic<N> > {
size_t i_; ///< we just use an unsigned int size_t i_; ///< we just use an unsigned int
public: public:
/// Constructor /// Constructor
@ -44,6 +51,16 @@ public:
Cyclic operator-() const { Cyclic operator-() const {
return (N - i_) % N; 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 { namespace traits {
@ -55,18 +72,18 @@ template<size_t N> struct structure_category<Cyclic<N> > {
namespace group { namespace group {
template<size_t N> template<typename G>
Cyclic<N> compose(const Cyclic<N>&g, const Cyclic<N>& h) { AdditiveGroup<G> compose(const AdditiveGroup<G>&g, const AdditiveGroup<G>& h) {
return g + h; return g + h;
} }
template<size_t N> template<typename G>
Cyclic<N> between(const Cyclic<N>&g, const Cyclic<N>& h) { AdditiveGroup<G> between(const AdditiveGroup<G>&g, const AdditiveGroup<G>& h) {
return h - g; return h - g;
} }
template<size_t N> template<typename G>
Cyclic<N> inverse(const Cyclic<N>&g) { AdditiveGroup<G> inverse(const AdditiveGroup<G>&g) {
return -g; return -g;
} }
@ -81,8 +98,8 @@ template<size_t N> struct identity<Cyclic<N> > {
template<size_t N> template<size_t N>
const Cyclic<N> identity<Cyclic<N> >::value = Cyclic<N>(0); const Cyclic<N> identity<Cyclic<N> >::value = Cyclic<N>(0);
/// Define the trait that asserts Cyclic is an additive group /// Define the trait that asserts AdditiveGroup is an additive group
template<size_t N> struct flavor<Cyclic<N> > { template<typename G> struct flavor<AdditiveGroup<G> > {
typedef additive_tag type; typedef additive_tag type;
}; };

View File

@ -26,7 +26,8 @@ typedef Cyclic<6> G; // Let's use the cyclic group of order 6
//****************************************************************************** //******************************************************************************
TEST(Cyclic, Concept) { TEST(Cyclic, Concept) {
BOOST_CONCEPT_ASSERT((IsGroup<G>)); // BOOST_CONCEPT_ASSERT((IsGroup<G>));
BOOST_CONCEPT_ASSERT((AdditiveGroup<G>));
EXPECT_LONGS_EQUAL(0, group::traits::identity<G>::value); EXPECT_LONGS_EQUAL(0, group::traits::identity<G>::value);
G g(2), h(3); G g(2), h(3);
// EXPECT(Group<G>().check_invariants(g,h)) // EXPECT(Group<G>().check_invariants(g,h))
@ -40,7 +41,7 @@ TEST(Cyclic, Constructor) {
//****************************************************************************** //******************************************************************************
TEST(Cyclic, Compose) { TEST(Cyclic, Compose) {
EXPECT_LONGS_EQUAL(0, group::compose(G(0),G(0))); EXPECT_LONGS_EQUAL(0, group::compose(G(0),G(0)));
EXPECT_LONGS_EQUAL(1, group::compose(G(0),G(1))); EXPECT_LONGS_EQUAL(1, group::compose(G(0),G(0)));
EXPECT_LONGS_EQUAL(2, group::compose(G(0),G(2))); EXPECT_LONGS_EQUAL(2, group::compose(G(0),G(2)));
EXPECT_LONGS_EQUAL(3, group::compose(G(0),G(3))); EXPECT_LONGS_EQUAL(3, group::compose(G(0),G(3)));
EXPECT_LONGS_EQUAL(4, group::compose(G(0),G(4))); EXPECT_LONGS_EQUAL(4, group::compose(G(0),G(4)));