From 00621521206e507df05d2470cb55fd54595ea610 Mon Sep 17 00:00:00 2001 From: dellaert Date: Sat, 23 May 2015 22:00:39 -0700 Subject: [PATCH] prototyping direct sum --- gtsam/geometry/tests/testCyclic.cpp | 90 ++++++++++++++++++++++++++++- 1 file changed, 89 insertions(+), 1 deletion(-) diff --git a/gtsam/geometry/tests/testCyclic.cpp b/gtsam/geometry/tests/testCyclic.cpp index a15d7e2c2..eaacea5c3 100644 --- a/gtsam/geometry/tests/testCyclic.cpp +++ b/gtsam/geometry/tests/testCyclic.cpp @@ -58,18 +58,106 @@ TEST(Cyclic, Between) { } //****************************************************************************** -TEST(Cyclic, Ivnverse) { +TEST(Cyclic, Inverse) { EXPECT_LONGS_EQUAL(0, traits::Inverse(G(0))); EXPECT_LONGS_EQUAL(2, traits::Inverse(G(1))); EXPECT_LONGS_EQUAL(1, traits::Inverse(G(2))); } +//****************************************************************************** +TEST(Cyclic, Negation) { + EXPECT_LONGS_EQUAL(0, -G(0)); + EXPECT_LONGS_EQUAL(2, -G(1)); + EXPECT_LONGS_EQUAL(1, -G(2)); +} + +//****************************************************************************** +TEST(Cyclic, Negation2) { + typedef Cyclic<2> Z2; + EXPECT_LONGS_EQUAL(0, -Z2(0)); + EXPECT_LONGS_EQUAL(1, -Z2(1)); +} + //****************************************************************************** TEST(Cyclic , Invariants) { G g(2), h(1); check_group_invariants(g,h); } +//****************************************************************************** +namespace gtsam { + +template +class DirectSum: public std::pair { + BOOST_CONCEPT_ASSERT((IsGroup)); // TODO(frank): check additive + BOOST_CONCEPT_ASSERT((IsGroup)); // TODO(frank): check additive + BOOST_CONCEPT_ASSERT((IsTestable)); + BOOST_CONCEPT_ASSERT((IsTestable)); + + const G& g() const { return this->first; } + const H& h() const { return this->second;} + +public: + // Construct from two subgroup elements + DirectSum(const G& g, const H& h):std::pair(g,h) {} + + /// Default constructor yields identity + DirectSum():std::pair(G::Identity(),H::Identity()) { + } + + /// Identity element + static DirectSum Identity() { + return DirectSum(); + } + /// Addition + DirectSum operator+(const DirectSum& other) const { + return DirectSum(g()+other.g(), h()+other.h()); + } + /// Subtraction + DirectSum operator-(const DirectSum& other) const { + return DirectSum(g()-other.g(), h()-other.h()); + } + /// Inverse + DirectSum operator-() const { + return DirectSum(- g(), - h()); + } + /// print with optional string + void print(const std::string& s = "") const { + std::cout << s << "(\n"; + traits::Print(g()); + std::cout << ",\n"; + traits::Print(h()); + std::cout << ")" << std::endl; + } + /// equals with an tolerance, prints out message if unequal + bool equals(const DirectSum& other, double tol = 1e-9) const { + return *this == other; // uses std::pair operator + } +}; + +/// Define direct sums to be a model of the Additive Group concept +template +struct traits > : internal::AdditiveGroupTraits >, // + Testable > { +}; + +} // namespace gtsam + +TEST(Cyclic , DirectSum) { + // The Direct sum of Cyclic<2> and Cyclic<2> is *not* Cyclic<4>, but the + // smallest non-cyclic group called the Klein four-group: + typedef DirectSum, Cyclic<2> > K; + BOOST_CONCEPT_ASSERT((IsGroup)); + BOOST_CONCEPT_ASSERT((IsTestable)); + + K g(0, 1), h(1, 1); + EXPECT(assert_equal(K(0, 1), - g)); + EXPECT(assert_equal(K(), g + g)); + EXPECT(assert_equal(K(1, 0), g + h)); + EXPECT(assert_equal(K(1, 0), g - h)); + check_group_invariants(g, h); +} + //****************************************************************************** int main() { TestResult tr;