#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 "a15p2/a15p2-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{-15}$ 
#define sqm15 (3.87298334620741688517926539978239961*I)
// Root of Z^3+3Z^2+1
#define ZZ ()

#define MaxNoGens 2
#define ActualNoGens 2
#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=
    {{ 2, 0, 0},
     { 0, 0, 1},
     { 0, 1, 0}};

  // Matrix to conjugate them to be unitary with respect to
  // $\diag{1,1,-1}$
  Mtx_t ConjMtx={{2,0,0},{0,1,1},{0,1,-1}},ConjMtxInv;

  // The generators
  Gen_t Gens[MaxNoGens]={
    {"A","a",10,
     {
       {1/30.0*(-11*sqm15-135)*ZZ*ZZ+1/30.0*(39*sqm15+265)*ZZ+1/30.0*(17*sqm15+265),
	1/15.0*(28*sqm15-40)*ZZ*ZZ+1/15.0*(-42*sqm15+10)*ZZ+1/15.0*(-51*sqm15+145),
	1/30.0*(9*sqm15+85)*ZZ*ZZ+1/30.0*(19*sqm15-185)*ZZ+1/30.0*(-23*sqm15-185)},
       {1/15.0*(7*sqm15+55)*ZZ*ZZ+1/15.0*(-48*sqm15-20)*ZZ+1/15.0*(-19*sqm15-125),
	1/30.0*(-7*sqm15+135)*ZZ*ZZ+1/30.0*(33*sqm15-275)*ZZ+1/30.0*(9*sqm15-275),
	1/15.0*(-4*sqm15+20)*ZZ*ZZ+1/15.0*(16*sqm15+100)*ZZ+1/15.0*(13*sqm15+25)},
       {1/15.0*(-48*sqm15+40)*ZZ*ZZ+1/15.0*(52*sqm15-220)*ZZ+1/15.0*(106*sqm15+50),
	1/15.0*(-16*sqm15-140)*ZZ*ZZ+1/15.0*(29*sqm15+205)*ZZ+1/15.0*(27*sqm15+265),
	3/5.0*sqm15*ZZ*ZZ+1/15.0*(-36*sqm15+5)*ZZ+1/30.0*(-41*sqm15-5)}
     },
    },
    {"B","b",10,
     {
       {1/30.0*(-sqm15+15)*ZZ*ZZ+1/15.0*(2*sqm15+10)*ZZ+1/30.0*(-3*sqm15-35),
	1/3.0*ZZ*ZZ+1/6.0*(-sqm15-1)*ZZ-1/3.0,
	1/3.0*ZZ*ZZ+1/12.0*(sqm15-7)*ZZ-2/3.0},
       {1/30.0*(sqm15+5)*ZZ*ZZ+1/30.0*(-9*sqm15-5)*ZZ+1/15.0*(-sqm15-5),
	-4/15.0*sqm15*ZZ*ZZ+1/30.0*(7*sqm15-25)*ZZ+1/30.0*(11*sqm15-5),
	1/15.0*(-sqm15-10)*ZZ*ZZ+1/30.0*(3*sqm15+25)*ZZ+1/15.0*(2*sqm15+25)},
       {1/15.0*(2*sqm15+10)*ZZ*ZZ+1/15.0*(2*sqm15-20)*ZZ+1/15.0*(-4*sqm15-10),
	1/30.0*(-sqm15-25)*ZZ*ZZ+1/15.0*(2*sqm15+20)*ZZ+1/15.0*(sqm15+25),
	1/10.0*(3*sqm15-5)*ZZ*ZZ+1/30.0*(-11*sqm15+5)*ZZ+1/30.0*(-23*sqm15+25)}
     }
    }
  };

  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++) {
    // 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
    GenElts[GenEltIx].KIx = -1;
    
    FixUpGenElt(&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
      GenElts[GenEltIx].KIx = -1;
      FixUpGenElt(&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 the index in K
  KElt_p->KIx = 0;
  FixUpGenElt(KElt_p);
}
