def mynumerator(x):
  if parent(x) == R:
    return x
  return numerator(x)

class fastfrac:
  def __init__(self,top,bot=1):
    if parent(top) == ZZ or parent(top) == R:
      self.top = R(top)
      self.bot = R(bot)
    elif top.__class__ == fastfrac:
      self.top = top.top
      self.bot = top.bot * bot
    else:
      self.top = R(numerator(top))
      self.bot = R(denominator(top)) * bot
  def reduce(self):
    return fastfrac(self.top / self.bot)
  def sreduce(self):
    return fastfrac(I.reduce(self.top),I.reduce(self.bot))
  def iszero(self):
    return self.top in I and not (self.bot in I)
  def isdoublingzero(self):
    return self.top in J and not (self.bot in J)
  def __add__(self,other):
    if parent(other) == ZZ:
      return fastfrac(self.top + self.bot * other,self.bot)
    if other.__class__ == fastfrac:
      return fastfrac(self.top * other.bot + self.bot * other.top,self.bot * other.bot)
    return NotImplemented
  def __sub__(self,other):
    if parent(other) == ZZ:
      return fastfrac(self.top - self.bot * other,self.bot)
    if other.__class__ == fastfrac:
      return fastfrac(self.top * other.bot - self.bot * other.top,self.bot * other.bot)
    return NotImplemented
  def __neg__(self):
    return fastfrac(-self.top,self.bot)
  def __mul__(self,other):
    if parent(other) == ZZ:
      return fastfrac(self.top * other,self.bot)
    if other.__class__ == fastfrac:
      return fastfrac(self.top * other.top,self.bot * other.bot)
    return NotImplemented
  def __rmul__(self,other):
    return self.__mul__(other)
  def __div__(self,other):
    if parent(other) == ZZ:
      return fastfrac(self.top,self.bot * other)
    if other.__class__ == fastfrac:
      return fastfrac(self.top * other.bot,self.bot * other.top)
    return NotImplemented
  __truediv__ = __div__
  def __pow__(self,other):
    if parent(other) == ZZ:
      return fastfrac(self.top ^ other,self.bot ^ other)
    return NotImplemented

def isidentity(x):
  return x.iszero()

def isdoublingidentity(x):
  return x.isdoublingzero()

R.<uc,ud,ux2,uy2,ux1,uy1,uX1,uY1,uZ1,uX2,uY2,uZ2> = PolynomialRing(QQ,12,order='invlex')
I = R.ideal([
  mynumerator((ux1^2+uy1^2)-(uc^2*(1+ud*ux1^2*uy1^2)))
, mynumerator((ux1)-(uX1/uZ1))
, mynumerator((uy1)-(uY1/uZ1))
, mynumerator((ux2^2+uy2^2)-(uc^2*(1+ud*ux2^2*uy2^2)))
, mynumerator((ux2)-(uX2/uZ2))
, mynumerator((uy2)-(uY2/uZ2))
, mynumerator((uZ1)-(1))
, mynumerator((uZ2)-(1))
])

J = I + R.ideal([0
, uX1-uX2
, uY1-uY2
, uZ1-uZ2
])

uc = fastfrac(uc)
ud = fastfrac(ud)
ux2 = fastfrac(ux2)
uy2 = fastfrac(uy2)
ux1 = fastfrac(ux1)
uy1 = fastfrac(uy1)
uX1 = fastfrac(uX1)
uY1 = fastfrac(uY1)
uZ1 = fastfrac(uZ1)
uX2 = fastfrac(uX2)
uY2 = fastfrac(uY2)
uZ2 = fastfrac(uZ2)


uC = ((uX1*uX2))
uD = ((uY1*uY2))
uE = ((ud*uC*uD))
uX3 = (((fastfrac(1)-uE)*((uX1+uY1)*(uX2+uY2)-uC-uD)))
uY3 = (((fastfrac(1)+uE)*(uD-uC)))
uZ3 = ((uc*(fastfrac(1)-uE^2)))

ux3 = (((ux1*uy2+uy1*ux2)/(uc*(fastfrac(1)+ud*ux1*ux2*uy1*uy2)))).reduce()
uy3 = (((uy1*uy2-ux1*ux2)/(uc*(fastfrac(1)-ud*ux1*ux2*uy1*uy2)))).reduce()

print(isidentity((ux3^2+uy3^2)-(uc^2*(fastfrac(1)+ud*ux3^2*uy3^2))))
print(isidentity((ux3)-(uX3/uZ3)))
print(isidentity((uy3)-(uY3/uZ3)))

unified = True
uX4 = uX3
uY4 = uY3
uZ4 = uZ3
ux4 = (((ux1*uy1+uy1*ux1)/(uc*(fastfrac(1)+ud*ux1*ux1*uy1*uy1)))).reduce()
uy4 = (((uy1*uy1-ux1*ux1)/(uc*(fastfrac(1)-ud*ux1*ux1*uy1*uy1)))).reduce()
if unified: unified = isdoublingidentity((ux4^2+uy4^2)-(uc^2*(fastfrac(1)+ud*ux4^2*uy4^2)))
if unified: unified = isdoublingidentity((ux4)-(uX4/uZ4))
if unified: unified = isdoublingidentity((uy4)-(uY4/uZ4))
if unified: print("Unified")

