OKlibrary  0.2.1.6
Algebra_Traits.hpp
Go to the documentation of this file.
00001 // Oliver Kullmann, 6.11.2004 (Swansea)
00002 /* Copyright 2004 - 2007 Oliver Kullmann
00003 This file is part of the OKlibrary. OKlibrary is free software; you can redistribute
00004 it and/or modify it under the terms of the GNU General Public License as published by
00005 the Free Software Foundation and included in this library; either version 3 of the
00006 License, or any later version. */
00007 
00008 #ifndef ALGEBRATRAITS_ggznn159Y
00009 
00010 #define ALGEBRATRAITS_ggznn159Y
00011 
00012 #include <cassert>
00013 
00014 namespace Algebra_Traits {
00015 
00016   template <typename Operator, typename Value>
00017   struct BinaryLinearCombination {
00018 
00019     typedef Operator operator_type;
00020     typedef Value value_type;
00021     const Operator x, y;
00022     const Value a, b, c;
00023 
00024     // TO IMPROVE: intelligent parameter passing
00025     BinaryLinearCombination(
00026           const operator_type& x,
00027           const operator_type& y,
00028           const value_type& a,
00029           const value_type& b) :
00030       x(x), y(y), a(a), b(b), c(x * a + y * b) {}
00031 
00032     BinaryLinearCombination(
00033           const operator_type& x,
00034           const operator_type& y,
00035           const value_type& a,
00036           const value_type& b,
00037           const value_type& c) :
00038       x(x), y(y), a(a), b(b), c(c) {
00039       assert(c == x * a + y * b);
00040     }
00041   };
00042 
00043   template <typename Operator, typename Value>
00044   inline bool operator ==(const BinaryLinearCombination<Operator, Value>& lhs, const BinaryLinearCombination<Operator, Value>& rhs) {
00045     return lhs.x == rhs.x and lhs.y == rhs.y and lhs.a == rhs.a and lhs.b == rhs.b and lhs.c == rhs.c;
00046   }
00047   template <typename Operator, typename Value>
00048   inline bool operator !=(const BinaryLinearCombination<Operator, Value>& lhs, const BinaryLinearCombination<Operator, Value>& rhs) {
00049     return not (lhs == rhs);
00050   }
00051 
00052 }
00053 
00054 namespace Algebra_Traits {
00055 
00056   template <typename MultiplicativeGroupoid>
00057   struct MultiplicativeGroupoid_traits {
00058     typedef MultiplicativeGroupoid value_type;
00059     static value_type identity();
00060     static bool invertible(value_type);
00061     static value_type inverse(value_type);
00062   };
00063 
00064   template <>
00065   struct MultiplicativeGroupoid_traits<int> {
00066     typedef int value_type;
00067     static value_type identity() { return value_type(1); }
00068     static bool invertible(const value_type x) { return x == 1 or x == -1; }
00069     static value_type inverse (const value_type x) {
00070       assert(invertible(x));
00071       return x;
00072     }
00073   };
00074 
00075 }
00076 
00077 
00078 #endif