// This program generates a ``dummy'' K file: 24cyclic-K, not associated
// to any actual~$\bar\Gamma$.

#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 "24cyclic/24cyclic"



// Z24, $\zeta_{24}=e^{\pi i/12}$
#define Z24  (0.9659258262890682867497432+0.2588190451025207623488988*I)

#define KSiz 24

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 KElts[KSiz];

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

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

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

  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+3<MAX_FILENAME_LEN);
    strcpy(&FileNam[3],Prefix_p);
    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 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]);
}

// Set up the 288-element finite group, K
//
void SetupK(Elt_t KElts[],
	    Mtx_t FF0,Mtx_t ConjMtx,Mtx_t ConjMtxInv) {

  int Ix1;
  Elt_t *KElt_p;
  Mtx_t KMtx,UPowMtx;
  // $K$ is generated by U
  Mtx_t U={{Z24,0,0},{0,CONJ(Z24),0},{0,0,1}};

  // K names
  char KString[24][5]=
    {"","U","U^2","U^3","U^4","U^5","U^6","U^7",
     "U^8","U^9","U^10","U^11","U^12","U^13","U^14","U^15",
     "U^16","U^17","U^18","U^19","U^20","U^21","U^22","U^23"};


  memcpy(UPowMtx,Id3,sizeof(Mtx_t));

  KElt_p=KElts;

  // Set up elements of the form:
  //
  //   U^m for $0\leq m<24$
  //
  for(Ix1=0;Ix1<24;Ix1++) {
    // The matrix
    memcpy(KMtx,UPowMtx,sizeof(Mtx_t));
    MultMtx(UPowMtx,U,KMtx);
    assert(CheckUnitary(KMtx,FF0));
    ConjugateMtx(KElt_p->Mtx,KMtx,ConjMtx,ConjMtxInv);
    assert(CheckPU(KElt_p->Mtx,FF1,epsSmall));
    // the generator name
    strncpy(KElt_p->Word,KString[Ix1],5);
    if(Ix1 == 0)
      KElt_p->Len = 0;
    else 
      KElt_p->Len = 2;
    // KIx and KStart
    KElt_p->KIx = KElt_p->KStart = (KElt_p-KElts);

    FixUpElt(KElt_p);
    KElt_p++;
    }
}
