1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
#include <iostream>
#include <set>
using namespace std;
//=========================================================
template <typename T> class Group
{
public:
set<T> S;
T (*op)( T, T );
void opTable();
bool isClosed();
bool isAssociative();
bool hasInverse();
bool hasIdentity( T &identity );
bool isCommutative();
};
//----------------------------
template <typename T> void Group<T>::opTable()
{
cout << "\t |\t";
for ( auto b : S ) cout << " " << b << '\t';
cout << "\n";
cout << "---\t---\t";
for ( auto b : S ) cout << "---" << '\t';
cout << "\n";
for ( auto a : S )
{
cout << a << "\t |\t";
for ( auto b : S ) cout << " " << op( a, b ) << '\t';
cout << '\n';
}
}
//----------------------------
template <typename T> bool Group<T>::isClosed()
{
for ( auto b : S )
{
for ( auto a : S )
{
if ( S.find( op( a, b ) ) == S.end() ) return false;
}
}
return true;
}
//----------------------------
template <typename T> bool Group<T>::isAssociative()
{
for ( auto c : S )
{
for ( auto b : S )
{
for ( auto a : S )
{
if ( op( a, op( b, c ) ) != op( op( a, b ), c ) ) return false;
}
}
}
return true;
}
//----------------------------
template <typename T> bool Group<T>::hasInverse()
{
T id;
if ( !hasIdentity( id ) ) return false; // pointless if no identity element
for ( auto b : S )
{
bool found = false;
for ( auto a : S )
{
if ( op( a, b ) == id && op( b, a ) == id )
{
found = true;
break;
}
}
if ( !found ) return false;
}
return true;
}
//----------------------------
template <typename T> bool Group<T>::hasIdentity( T &id )
{
id = T{};
auto a = *S.begin(); // find an identity for first element
bool found = false;
for ( auto b : S )
{
if ( op( b, a ) == a )
{
found = true;
id = b;
break;
}
}
if ( !found ) return false;
for ( auto b : S ) // identity must be unique
{
if ( op( id, b ) != b || op( b, id ) != b ) return false;
}
return true;
}
//----------------------------
template <typename T> bool Group<T>::isCommutative()
{
for ( auto b : S )
{
for ( auto a : S )
{
if ( op( a, b ) != op( b, a ) ) return false;
}
}
return true;
}
//=========================================================
// Some binary ops
int addModulo5 ( int a, int b ) { return ( a + b ) % 5; }
int add ( int a, int b ) { return a + b ; }
int timesmodulo5( int a, int b ) { return ( a * b ) % 5; }
int times ( int a, int b ) { return a * b ; }
//=========================================================
int main()
{
set<int> S = { 0, 1, 2, 3, 4 };
set<int> S0 = { 1, 2, 3, 4 };
// Group<int> G = { S, addModulo5 }; // *** abelian group ***
// Group<int> G = { S, add }; // *** not a group ***
// Group<int> G = { S , timesmodulo5 }; // *** not a group ***
Group<int> G = { S0, timesmodulo5 }; // *** abelian group ***
// Group<int> G = { S0, times }; // *** not a group ***
int id;
G.opTable();
bool Closed = G.isClosed();
bool Associative = G.isAssociative();
bool Inverse = G.hasInverse();
bool Neutral = G.hasIdentity( id );
bool Commutative = G.isCommutative();
cout << "G is " << ( Closed ? "" : "not " ) << "closed\n";
cout << "G is " << ( Associative ? "" : "not " ) << "associative\n";
cout << "G is " << ( Inverse ? "" : "not " ) << "invertible\n";
if ( Neutral )
{
cout << "G has an identity element " << id << '\n';
}
else
{
cout << "G has no identity element\n";
}
cout << "G is " << ( Commutative ? "" : "not " ) << "commutative\n";
if ( Closed && Associative && Inverse && Neutral )
{
cout << "G is " << ( Commutative ? "an abelian " : "a non-abelian " ) << "group\n";
}
else
{
cout << "G is not a group\n";
}
}
| |