
#include <binopproperties.h>


uintc binopproperties::identityfind(bool & found) const
{
  uint k;
  for (uint i=0; i<N; ++i)
  {
    found=true;
    for (k=0; k<N; ++k)
    {
      if ((matik(i,k)==k) && (matik(k,i)==k))
        continue;

      found=false;
      k=N;
    }

    if (found)
      return i;
  }

  return 0;
}

bool const binopproperties::isClosed() const
{
  uint x;
  for (uint i=0; i<N; ++i)
  {
    for (uint k=0; k<N; ++k)
    {
      x = matik(i,k);
      if (x>=N)
        return false;
    }
  }

  return true;
}

boolc binopproperties::isCommutative() const
{
  for (uint i=0; i<N; ++i)
  {
    for (uint k=0; k<N/2; ++k)
    {      
      if (matik(i,k)!=matik(k,i))
        return false;
    }
  }

  return true;
}

boolc binopproperties::isGroup() const
{
  if (isClosed()==false)
    return false;

  if (isAssociative()==false)
    return false;

  bool found;
  uint id = identityfind(found);
  if (found==false)
    return false;

  for (uint i=0; i<N; ++i)
  {
    inverseright(found,i,id);
    if (found==false)
      return false;
  }

  for (uint i=0; i<N; ++i)
  {
    inverseleft(found,i,id);
    if (found==false)
      return false;
  }

  return true;
}

boolc binopproperties::isGroupAbelian() const
{
  if (isGroup()==false)
    return false;

  if (isCommutative()==false)
    return false;

  return true;
}

boolc binopproperties::isAssociative() const
{
  uint i;
  uint k;
  uint w;

  uint a1;
  uint a2;
  for ( i=0; i<N; ++i)
  {
    for (k=0; k<N; ++k)
    {
      for (w=0; w<N; ++w)
      {
        a2 = matik(k,w);
        a2 = matik(i,a2);

        a1 = matik(i,k);
        a1 = matik(a1,w);

        if (a2!=a1)
          return false;
      }
    }
  }

  return true;
}


      
uintc binopproperties::inverseright
(
  bool & found, 
  uintc i, 
  uintc e
) const
{
  found=true;
  for (uint w=0; w<N; ++w)
  {
    if (matik(i,w)==e)
      return w;
  }

  found=false;
  return 0;
}

uintc binopproperties::inverseleft
(
  bool & found, 
  uintc k, 
  uintc e
) const
{
  found=true;
  for (uint w=0; w<N; ++w)
  {
    if (matik(w,k)==e)
      return w;
  }

  found=false;
  return 0;
}





