#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 "a7p2N/a7p2N-7-KK"

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;

// $\exp(2\pi i/7)$, a seventh root of unity
#define ZZ (0.6234898018587335305300629+0.7818314824680298087044108*I)
// and its powers, the other seventh roots of unity
#define ZZ2 (ZZ*ZZ)
#define ZZ3 (ZZ*ZZ2)
#define ZZ4 (ZZ*ZZ3)
#define ZZ5 (ZZ*ZZ4)
#define ZZ6 (ZZ*ZZ5)

#define MaxNoGens 2
#define ActualNoGens 2
#define KSiz 7

void SetupGens(Elt_t GenElts[],int *NoGenElts_p,
	       Mtx_t FF0,Mtx_t ConjMtx,Mtx_t ConjMtxInv);
void SetupK(Elt_t KElts[],
	       Mtx_t FF0,Mtx_t ConjMtx,Mtx_t ConjMtxInv);

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;

  // The form with respect to which the original, given matrices are
  // unitary:
  Mtx_t FF0=
    {{ ZZ+1/ZZ, 0, 0},
     { 0, ZZ2+1/ZZ2, 0},
     { 0, 0, ZZ4+1/ZZ4}};

  // Matrix to conjugate them to be unitary with respect to
  // $\diag{1,1,-1}$
  Mtx_t ConjMtxInv, ConjMtx={
    {0, 0, SQRT(-CREAL(ZZ4+1/ZZ4))},
    {0, SQRT(-CREAL(ZZ2+1/ZZ2)), 0},
    {SQRT(CREAL(ZZ+1/ZZ)), 0, 0}};

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

  // Calculate the inverse of ConjMtx
  InvMtx(ConjMtxInv,ConjMtx);
  
  // Prepare the generators and write the generator file
  SetupGens(GenElts,&NoGenElts,FF0,ConjMtx,ConjMtxInv);
  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,FF0,ConjMtx,ConjMtxInv);
  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,
	       Mtx_t FF0,Mtx_t ConjMtx,Mtx_t ConjMtxInv) {

  // The generators
  Gen_t Gens[MaxNoGens]={

    {"B","b",20,
     {
       {1/7.0*(-4*ZZ5-8*ZZ4-5*ZZ3-2*ZZ2+ZZ-3),
	1/7.0*(-6*ZZ5+2*ZZ4+3*ZZ3-3*ZZ2-2*ZZ+6),
	1/7.0*(5*ZZ5+3*ZZ4+ZZ3+6*ZZ2+4*ZZ-5)},
       {1/7.0*(ZZ5+2*ZZ4+3*ZZ3-3*ZZ2-2*ZZ-8),
	1/7.0*(5*ZZ5+3*ZZ4+ZZ3+6*ZZ2-3*ZZ+2),
	1/7.0*(-3*ZZ5-6*ZZ4-9*ZZ3-5*ZZ2-ZZ+3)},
       {1/14.0*(10*ZZ5+13*ZZ4+2*ZZ3+19*ZZ2+ZZ+18),
	1/7.0*(-3*ZZ5-6*ZZ4-2*ZZ3-5*ZZ2-ZZ-11),
	1/7.0*(-ZZ5+5*ZZ4+4*ZZ3-4*ZZ2+2*ZZ+1)}
     }
    },

    {"C","c",20,
     {
       {1/7.0*(2*ZZ5+4*ZZ4+6*ZZ3+ZZ2-4*ZZ-2),
	1/7.0*(10*ZZ5+13*ZZ4+2*ZZ3+5*ZZ2+15*ZZ+11),
	1/7.0*(8*ZZ5+16*ZZ4+10*ZZ3+4*ZZ2+12*ZZ+6)},
       {1/7.0*(-4*ZZ5-ZZ4+9*ZZ3+5*ZZ2+8*ZZ-3),
	1/7.0*(-6*ZZ5-5*ZZ4-4*ZZ3-10*ZZ2-2*ZZ-8),
	1/7.0*(-2*ZZ5+3*ZZ4+8*ZZ3+13*ZZ2+11*ZZ+9)},
       {1/7.0*(-13*ZZ5+2*ZZ4-11*ZZ3-3*ZZ2-2*ZZ-8),
	1/7.0*(-9*ZZ5-4*ZZ4-13*ZZ3-ZZ2-10*ZZ-12),
	1/7.0*(4*ZZ5-6*ZZ4-2*ZZ3+2*ZZ2-ZZ-4)}
     }
    }
  };

  int GenIx,GenEltIx;
  size_t NamLen;
  Mtx_t MtxInv;

  GenEltIx=0;
  for(GenIx=0;GenIx<ActualNoGens;GenIx++) {
    // Check that the matrix is unitary with respect to FF0
    // printf("Checking GenIx=%i\n",GenIx);
    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 7-element finite group, K
//
void SetupK(Elt_t KElts[],
	       Mtx_t FF0,Mtx_t ConjMtx,Mtx_t ConjMtxInv) {
  int Ix,RIx,CIx;
  Elt_t *KElt_p;
  Mtx_t tmpMtx,KMtx;
  // Powers of
  //
  //   Z = \zeta_7 = e^{2\pi i / 7}
  COMPLEX_t Zeta7Pow[7]={1,ZZ,ZZ2,ZZ3,ZZ4,ZZ5,ZZ6};

  // Exponent strings
  char ExpString[7][3]=
    {"??","","^2","^3","^4","^5","^6"};

  KElt_p=KElts;

  // Set up elements of the form:
  //
  //        \zeta_7^{Ix}       0                0
  //          0          \zeta_7^{2 Ix}         0
  //          0                0          \zeta_7^{4 Ix}
  //
  //   = Z^{Ix}

  memset(KMtx,0,sizeof(Mtx_t));

  for(Ix=0;Ix<7;Ix++) {
    // Set up the matrix,
    KMtx[0][0]=Zeta7Pow[Ix];
    KMtx[1][1]=Zeta7Pow[(2*Ix) % 7];
    KMtx[2][2]=Zeta7Pow[(4*Ix) % 7];
    assert(CheckUnitary(KMtx,FF0));
    // calculate the conjugated matrix,
    ConjugateMtx(KElt_p->Mtx,KMtx,ConjMtx,ConjMtxInv);
    // and check that that is unitary relative to FF1.
    assert(CheckUnitary(KElt_p->Mtx,FF1));

    // Set up the word,
    if(Ix == 0)
      strcpy(KElt_p->Word,"");
    else {
      strcpy(KElt_p->Word,"Z");
      strcpy((KElt_p->Word)+1,ExpString[Ix]);
    }
    // the ``length'',
    if(Ix == 0)
      KElt_p->Len = 0;
    else
      KElt_p->Len = 2;
    // and KIx and KStart.
    KElt_p->KIx = KElt_p->KStart =(KElt_p-KElts);

    FixUpElt(KElt_p);
    KElt_p++;
  }
}
