#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 "a7p2/a7p2-57"

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{-7}$ 
#define sqm7 (2.645751311064590590501615753639*I)
// Root of Z^3+3Z^2+3
#define ZZ (-3.279018786166593579491442605776)

#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=
    {{ 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/14.0*(-sqm7-7)*ZZ*ZZ+1/14.0*(-sqm7-21)*ZZ+1/14.0*(9*sqm7-7),
	0,
	0},
       {0,
	1/14.0*(-sqm7+7)*ZZ*ZZ+1/14.0*(-sqm7+21)*ZZ+1/14.0*(9*sqm7-7),
	0},
       {0,
	0,
	1/7.0*sqm7*ZZ*ZZ+1/7.0*sqm7*ZZ+1/14.0*(3*sqm7-7)}
     },
    },

    {"B","b",10,
     {
       {1/126.0*(5*sqm7+7)*ZZ*ZZ+2/21.0*sqm7*ZZ+1/21.0*(-4*sqm7-14),
	1/63.0*(-4*sqm7-14)*ZZ*ZZ+1/42.0*(-5*sqm7-21)*ZZ+1/42.0*(3*sqm7+7),
	1/1260.0*(17*sqm7-29)*ZZ*ZZ+1/105.0*(14*sqm7-17)*ZZ+1/210.0*(31*sqm7-65)},
       {1/315.0*(sqm7+53)*ZZ*ZZ+1/210.0*(-5*sqm7+201)*ZZ+1/105.0*(8*sqm7+2),
	1/63.0*(sqm7-7)*ZZ*ZZ+1/42.0*(3*sqm7-7)*ZZ+1/21.0*(-3*sqm7-7),
	1/126.0*(-5*sqm7+35)*ZZ*ZZ+1/21.0*(-2*sqm7+14)*ZZ+1/42.0*(sqm7-7)},
       {1/63.0*(13*sqm7-7)*ZZ*ZZ+1/21.0*(9*sqm7-7)*ZZ-4/21.0*sqm7,
	1/630.0*(-19*sqm7-77)*ZZ*ZZ+1/210.0*(-51*sqm7-133)*ZZ+1/70.0*(-3*sqm7-49),
	1/18.0*(-sqm7+1)*ZZ*ZZ+1/6.0*(-sqm7+1)*ZZ+1/6.0*(-sqm7-3)}
     },
    },

    {"C","c",10,
     {
       {1/63.0*(-sqm7-7)*ZZ*ZZ+1/21.0*(-5*sqm7-7)*ZZ+1/21.0*(3*sqm7+14),
	1/126.0*(11*sqm7-49)*ZZ*ZZ+1/42.0*(13*sqm7-49)*ZZ+1/21.0*(-6*sqm7-7),
	1/630.0*(41*sqm7-31)*ZZ*ZZ+1/105.0*(3*sqm7-25)*ZZ+1/35.0*(-6*sqm7-23)},
       {1/315.0*(-19*sqm7-151)*ZZ*ZZ+1/105.0*(7*sqm7-153)*ZZ+1/105.0*(25*sqm7-121),
	1/63.0*(-sqm7-7)*ZZ*ZZ+1/21.0*(2*sqm7-14)*ZZ+1/21.0*(10*sqm7+7),
	1/63.0*(-8*sqm7+7)*ZZ*ZZ+1/21.0*(-5*sqm7+14)*ZZ-4/21.0*sqm7},
       {1/63.0*(5*sqm7+35)*ZZ*ZZ+1/7.0*(-sqm7+7)*ZZ+1/21.0*(-22*sqm7-28),
	1/315.0*(-22*sqm7+182)*ZZ*ZZ+1/105.0*(-13*sqm7+203)*ZZ+1/105.0*(8*sqm7-98),
	1/63.0*(2*sqm7+14)*ZZ*ZZ+1/7.0*(sqm7+7)*ZZ+1/21.0*(8*sqm7+21)}
     }
    }
  };

  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 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);
}
