finitegroup.h

00001 #ifndef GM_GROUP_H 00002 #define GM_GROUP_H 00003 00004 // A binary operator abstract base class 00005 template<class T> 00006 class BinaryOp 00007 { 00008 BinaryOp(); 00009 public: 00010 static T binary(T left, T right) = 0; 00011 }; 00012 00013 // The Addition operator mod something 00014 template<class T, unsigned int Modulus> 00015 class PlusOp: public BinaryOp<T> 00016 { 00017 PlusOp(); 00018 public: 00019 00020 static T binary(T left, T right) 00021 { 00022 return (left + right) % Modulus; 00023 } 00024 }; 00025 00026 template<class T> 00027 class PlusOp: public BinaryOp<T> 00028 { 00029 PlusOp(); 00030 public: 00031 00032 static T binary(T left, T right) 00033 { 00034 return left + right; 00035 } 00036 }; 00037 00038 template<class T, unsigned int Modulus> 00039 class MultOp: public BinaryOp<T> 00040 { 00041 MultOp(); 00042 public: 00043 00044 static T binary(T left, T right) 00045 { 00046 return (left * right) % Modulus; 00047 } 00048 }; 00049 00050 template<class T> 00051 class MultOp: public BinaryOp<T> 00052 { 00053 MultOp(); 00054 public: 00055 00056 static T binary(T left, T right) 00057 { 00058 return left * right; 00059 } 00060 }; 00061 00062 00063 00064 template<class T, class O> 00065 class FiniteGroup 00066 { 00067 public: 00068 FiniteGroup(const vector<T>& elements); 00069 00070 // harder 00071 bool isGroup() 00072 { 00073 // what must be: 00074 // closed under binary star operator 00075 // must exist an element 'e' that satisfies: x*e = x; (identity) 00076 // must exist a unique inverse 00077 00078 return true; 00079 } 00080 00081 // easy 00082 virtual bool isGenerator(T element) const = 0; 00083 00084 // easy 00085 virtual vector<T> findGenerators() const = 0; 00086 00087 // trivial 00088 virtual unsigned int order() { return m_elements.size(); } 00089 00090 // ?? harder 00091 virtual unsigned int order(T element) = 0; 00092 00094 virtual friend T operator *(T element1, T element2) 00095 { 00096 return O::binary(element1, element2); 00097 } 00098 00100 static int gcd(int p, int q) { return 1; } 00101 00102 // trivial 00103 bool isElement(T element) 00104 { 00105 for (unsigned int i = 0; i < m_elements.size(); i++) 00106 if (m_elements[i] == T) 00107 return true; 00108 return false; 00109 } 00110 00111 protected: 00112 vector<T> m_elements; 00113 }; 00114 00115 template <class O> 00116 class IntegerGroup: public FiniteGroup<unsigned int, O> 00117 { 00118 public: 00119 IntegerGroup(const vector<unsigned int>& elements): FiniteGroup(elements) {} 00120 00121 virtual bool isGroup() const { return true; } 00122 00123 virtual bool isGenerator(unsigned int element) const 00124 { 00125 if (isElement(element) && (gcd(element, order()) == 1) 00126 return true; 00127 return false; 00128 } 00129 00130 virtual vector<unsigned int> findGenerators() const 00131 { 00132 vector<unsigned int> generators; 00133 00134 for (unsigned int i = 0; i < order(); i++) 00135 { 00136 if (gcd(m_elements[i], order()) == 1) 00137 generators.push_back(m_elements[i]); 00138 } 00139 return generators; 00140 00141 } 00142 }; 00143 00144 #endif

Generated on Tue Oct 5 14:41:47 2004 for GNU Messenger by doxygen 1.3.8