#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <complex.h>
#include <string.h>

#include "../numeric/SU21-numeric.h"
#include "../numeric/SU21-numeric-base.c"

#define GOODPREFIX "C10p2/C10p2-0"

typedef struct {
  char Nam[MaxGenNameLength+1]; // Generator name
  char InvNam[MaxGenNameLength+1]; // Inverse generator name
  int Len; // ``Length'' of Generator
  Mtx_t Mtx; // matrix for generator
} Gen_t;

// $\sqrt{2}$ 
#define sq2 (1.414213562373095048801688724209)
// $U = ((1+\sqrt{2}) + \sqrt{-5+2\sqrt{2}}) / 2$
#define UU (1.207106781186547524400844362105+0.736812879103950296577504567300*I)
// WW = $2\cos(2\pi/9)$
#define WW (1.532088886237956070404785301111)
// WW2 = WW^2
#define WW2 (2.347296355333860697703433253539)

#define MaxNoGens 3
#define ActualNoGens 3
#define KSiz 1

void SetupGens(Elt_t GenElts[],int *NoGenElts_p);
void SetupK(Elt_t KElts[]);

int main(int argc,char *(argv[])) {
  // The generators as group elements
  Elt_t GenElts[2*ActualNoGens],KElts[KSiz];

  char *Prefix_p,FileNam[MAX_FILENAME_LEN]="../";
  int GenIx,GenEltIx,NoGenElts;
  size_t NamLen;
  Mtx_t MtxInv;

  FILE *GensFile;
  FILE *KFile;

  // Check that the prefix is right
  Prefix_p=getenv("PREFIX");
  assert(Prefix_p != NULL);
  assert(strncmp(Prefix_p,GOODPREFIX,50) == 0);

  // Open the output files
  {
    size_t len=strlen(Prefix_p);
    assert(3+len+5<MAX_FILENAME_LEN);
    strcpy(&FileNam[3],Prefix_p);
    strcpy(&FileNam[3]+len,"-Gens");
    printf("%s\n",FileNam);
    GensFile=fopen(FileNam,"w");
    assert(GensFile != NULL);
    strcpy(&FileNam[3]+len,"-K");
    printf("%s\n",FileNam);
    KFile=fopen(FileNam,"w");
    assert(KFile != NULL);
  }

  // Prepare the generators and write the generator file
  SetupGens(GenElts,&NoGenElts);
  fprintf(GensFile,"NoGenElts=%i\n",NoGenElts);
  for(GenEltIx=0;GenEltIx<NoGenElts;GenEltIx++)
    OutputElt(GensFile,GenElts[GenEltIx]);

  // Prepare K (the finite group) and write the K file
  SetupK(KElts);
  fprintf(KFile,"KSiz=%i\n",KSiz);
  for(GenEltIx=0;GenEltIx<KSiz;GenEltIx++)
    OutputElt(KFile,KElts[GenEltIx]);
}

// Prepare the generating elements
//
void SetupGens(Elt_t GenElts[],int *NoGenElts_p) {
  // The form with respect to which the original, given matrices are
  // unitary:
  Mtx_t FF0=
    {{ -sq2+(1-sq2)*WW+WW2, 0, 0},
     { 0, (4-3*sq2)+sq2*WW+(-1+sq2)*WW2, 0},
     { 0, 0, (2+sq2)-WW-sq2*WW2}};

  // Matrix to conjugate them to be unitary with respect to
  // $\diag{1,1,-1}$.  The nonzero elements here are
  // obtained from the diagonal elements of _FF0_ by taking square
  // roots of absolute values.
  Mtx_t ConjMtxInv,ConjMtx={
    {0.546324809540912490675299134410,0,0},
    {0,1.701864324747346906543242431920,0},
    {0,0,1.198938557427753470429266518300}};

  // The generators
  Gen_t Gens[MaxNoGens]={
    {"A","a",20,
     {
       {1/9.0*(((-2*sq2-2)*UU+(2*sq2+3))*WW2+((-sq2-1)*UU+sq2)*WW+(7*sq2+7)*UU-7*sq2-12),
	1/9.0*(((4*sq2+3)*UU-3*sq2-11)*WW2+((5*sq2+6)*UU-9*sq2-10)*WW-5*sq2*UU+10),
	1/9.0*((UU+(2*sq2+1))*WW2+(-UU+(sq2+5))*WW+(-3*sq2-5)*UU+2*sq2+1)},
       {1/9.0*((-2*sq2*UU-sq2)*WW2+((-sq2+3)*UU-2*sq2)*WW+sq2*UU+5*sq2+6),
	1/9.0*(((sq2+1)*UU-sq2)*WW2+((-sq2-1)*UU+(sq2+3))*WW+(sq2+1)*UU-sq2-6),
	1/9.0*(((-5*sq2-6)*UU+(9*sq2+10))*WW2+((-sq2-3)*UU+(6*sq2-1))*WW+(13*sq2+18)*UU-24*sq2-32)},
       {1/9.0*(((3*sq2-2)*UU-3*sq2-2)*WW2+(-4*UU+(3*sq2+8))*WW+(-6*sq2+7)*UU-2),
	1/9.0*(((sq2-3)*UU+2*sq2)*WW2+((-sq2-3)*UU+sq2)*WW+(-5*sq2+6)*UU-sq2+6),
	1/9.0*(((sq2+1)*UU-sq2-3)*WW2+((2*sq2+2)*UU-2*sq2-3)*WW+(sq2+1)*UU-sq2)}
     }
    },

    {"B","b",20,
     {
       {1/18.0*(((-2*sq2-4)*UU-4)*WW2+((-4*sq2-8)*UU+(6*sq2+4))*WW+(4*sq2+8)*UU+8),
	1/18.0*(((-2*sq2+6)*UU-4*sq2-4)*WW2+(2*sq2*UU-8*sq2-2)*WW+(4*sq2-12)*UU+8*sq2-4),
	1/18.0*((4*sq2+4)*WW2+(2*sq2+8)*WW+-2*sq2-8)},
       {1/18.0*((-4*sq2-2)*UU*WW2+(-2*sq2+2)*UU*WW+(8*sq2+10)*UU),
	1/18.0*(((4*sq2+8)*UU-6*sq2-4)*WW2+((2*sq2+4)*UU-6*sq2-8)*WW+(-8*sq2-16)*UU+12*sq2+8),
	1/18.0*((-2*sq2*UU+(8*sq2+2))*WW2+((-4*sq2+6)*UU+(4*sq2-2))*WW+4*sq2*UU-16*sq2-16)},
       {1/18.0*(((2*sq2-6)*UU+(6*sq2-8))*WW2+(sq2*UU+(6*sq2-4))*WW+(-10*sq2+12)*UU-12*sq2+16),
	1/18.0*((2*sq2-2)*UU*WW2+(-2*sq2-4)*UU*WW+(-4*sq2+10)*UU),
	1/18.0*(((-2*sq2-4)*UU+(6*sq2+8))*WW2+((2*sq2+4)*UU+4)*WW+(4*sq2+8)*UU-12*sq2-16)}
     }
    },

    {"C","c",50,
     {
       {1/9.0*(((-3*sq2-4)*UU+(3*sq2+3))*WW2+((-3*sq2-5)*UU+(3*sq2+3))*WW+(9*sq2+11)*UU-9*sq2-9),
	1/9.0*(((3*sq2+4)*UU-sq2-4)*WW2+((6*sq2+5)*UU-2*sq2-5)*WW+(-3*sq2+1)*UU-4*sq2+5),
	1/9.0*(((4*sq2+5)*UU-8*sq2-9)*WW2+((2*sq2+4)*UU-4*sq2-12)*WW+(-8*sq2-13)*UU+13*sq2+21)},
       {1/9.0*(((2*sq2-2)*UU+(4*sq2+4))*WW2+((4*sq2-1)*UU-sq2-4)*WW+(-4*sq2-2)*UU-5*sq2-8),
	1/9.0*(((3*sq2+5)*UU-3*sq2-3)*WW2+UU*WW+(-3*sq2-7)*UU+3*sq2+3),
	1/9.0*(((-6*sq2-5)*UU+(2*sq2+5))*WW2+((-3*sq2-1)*UU+(sq2+1))*WW+(15*sq2+19)*UU-10*sq2-13)},
       {1/9.0*(((3*sq2+3)*UU-sq2-6)*WW2+((-3*sq2-6)*UU+(4*sq2+6))*WW-7*sq2+6),
	1/9.0*(((-4*sq2+1)*UU+(sq2+4))*WW2+((-2*sq2-1)*UU+(5*sq2+8))*WW+(8*sq2-8)*UU+sq2-8),
	1/9.0*(-UU*WW2+(((3*sq2+4)*UU-3*sq2-3)*WW+(3*sq2+5)*UU-3*sq2-3))}
     }
    }
  };


  int GenIx,GenEltIx;
  size_t NamLen;
  Mtx_t MtxInv;
  
  // Calculate the inverse of ConjMtx
  InvMtx(ConjMtxInv,ConjMtx);
  
  GenEltIx=0;
  for(GenIx=0;GenIx<ActualNoGens;GenIx++) {
    printf("Checking Generator %s\n",Gens[GenIx].Nam);
    // Check that the matrix is unitary with respect to FF0
    assert(CheckUnitary(Gens[GenIx].Mtx,FF0));
    // Calculate the conjugated matrix
    ConjugateMtx(GenElts[GenEltIx].Mtx,Gens[GenIx].Mtx,ConjMtx,ConjMtxInv);
    // and check that that is unitary relative to FF1
    assert(CheckUnitary(GenElts[GenEltIx].Mtx,FF1));
    InvMtx(MtxInv,GenElts[GenEltIx].Mtx);
    // Set up the generator name
    NamLen=strlen(Gens[GenIx].Nam);
    assert(NamLen>0 && NamLen<MaxGenNameLength);
    strcpy(GenElts[GenEltIx].Word,Gens[GenIx].Nam);
    // and ``length''
    GenElts[GenEltIx].Len=Gens[GenIx].Len;
    // and KIx and KStart
    GenElts[GenEltIx].KIx = -1;
    GenElts[GenEltIx].KStart = 0;
    
    FixUpElt(&GenElts[GenEltIx]);
    GenEltIx++;
    
    // Now do the inverse generator.  If the inverse name is empty, we
    // skip this.
    NamLen=strlen(Gens[GenIx].InvNam);
    if(NamLen>0) {
      // The matrix
      memcpy(&GenElts[GenEltIx].Mtx,MtxInv,sizeof(Mtx_t));
      assert(CheckUnitary(GenElts[GenEltIx].Mtx,FF1));
      // the inverse generator name
      assert(NamLen<MaxGenNameLength);
      strcpy(GenElts[GenEltIx].Word,Gens[GenIx].InvNam);
      // the length
      GenElts[GenEltIx].Len=Gens[GenIx].Len;
      // and KIx and KStart
      GenElts[GenEltIx].KIx = -1;
      GenElts[GenEltIx].KStart = 0;
      
      FixUpElt(&GenElts[GenEltIx]);
      GenEltIx++;
    }
  }
  *NoGenElts_p=GenEltIx;
}

  // Set up the (trivial) finite group, K
  //
  void SetupK(Elt_t KElts[]) {
    // The identity element
    Gen_t IGen=
      {"1","",0,{{1,0,0},{0,1,0},0,0,1}};
    Elt_t *KElt_p;
  
    KElt_p=&KElts[0];
  
    // Set up the matrix
    memcpy(KElt_p->Mtx,IGen.Mtx,sizeof(Mtx_t));
    // the generator name
    assert(strlen(IGen.Nam)<MaxGenNameLength);
    strcpy(KElt_p->Word,IGen.Nam);
    // the ``length''
    KElt_p->Len = IGen.Len;
    // and KIx and KStart
    KElt_p->KIx = 0;
    KElt_p->KStart = 0;
  
    FixUpElt(KElt_p);
  }
