final v1.0 files
[weak_simulation_stab_extent.git] / supplement2.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <string.h>
5
6 void printBinary(unsigned long number, int T) {
7
8   int i;
9   
10   for(i=0; i<T; i++)
11     printf("%d", (int)(number/pow(2,i))%2);
12   printf("\n");
13   
14 }
15
16 void supplement2(long stabStateIndices[], int T, double fractionSupplement)
17 {
18
19   int bitstringCounterMax = floor(fractionSupplement*(2*T-1));
20   if(bitstringCounterMax < 1)
21     return;
22
23   int K = round(log(T)/log(2)); // T = 2^K
24
25   //printf("K=%d\n", K);
26
27   unsigned long given = (unsigned long)stabStateIndices[0]; // the given bitstring
28
29   //printf("given bitstring =");
30   //printBinary(stabStateIndices[0], T);
31
32   unsigned long long precurserBitstrings[2*T-1];
33   unsigned long long tmpBitstrings[2*T-1];
34
35   int precurserBitstringCounter = 0;
36
37   int i, j, k;
38
39   // first added bitstring is alpha = 10
40   precurserBitstrings[0] = 2;
41   //printf("added precurser=");
42   //printBinary(precurserBitstrings[0], T);
43   // second added bitstring is beta = 01
44   precurserBitstrings[1] = 1;
45   //printf("added precurser=");
46   //printBinary(precurserBitstrings[1], T);
47   // third added bitstring is gamma = 11
48   precurserBitstrings[2] = 0;
49   //printf("added precurser=");
50   //printBinary(precurserBitstrings[2], T);
51   precurserBitstringCounter+=3;
52
53   long mask;
54   for(i=1; i<=K-1; i++) {
55     mask = 0;
56     for(j=0;j<pow(2,i-1);j++)
57       mask += 3*pow(2,2*j+pow(2,i));
58     //printf("mask=");
59     //printBinary(mask, T);
60     for(j=0; j<precurserBitstringCounter; j++) {
61       tmpBitstrings[j] = precurserBitstrings[j]+pow(2,pow(2,i))*precurserBitstrings[j];
62       //printf("added precurser=");
63       //printBinary(tmpBitstrings[j], T);
64       tmpBitstrings[j+precurserBitstringCounter] = precurserBitstrings[j]+((long)(pow(2,pow(2,i))*(precurserBitstrings[j]))^mask);
65       //printf("!!added precurser=");
66       //printBinary(tmpBitstrings[j+precurserBitstringCounter], T);
67     }
68     tmpBitstrings[2*precurserBitstringCounter] = (long)((tmpBitstrings[2])^((long)(mask/pow(2,pow(2,i)))));
69     //printf("!added precurser=");
70     //printBinary(tmpBitstrings[2*precurserBitstringCounter], T);
71     precurserBitstringCounter = pow(2,i+2)-1;
72     memcpy(precurserBitstrings,tmpBitstrings,precurserBitstringCounter*sizeof(unsigned long));
73   }
74
75   int bitstringCounter = 1;  // bitstringCounter starts at 1 since 0 is the given bitstring
76
77   mask = mask+mask/pow(2,pow(2,K-1));
78   for(i=0; i<2*T-1; i++) {
79     stabStateIndices[bitstringCounter] = given^(precurserBitstrings[i]^((long)mask));
80     //stabStateIndices[bitstringCounter] = given^(precurserBitstrings[i]);
81     //printf("new bitstring=");
82     //printBinary(stabStateIndices[bitstringCounter], T);
83     bitstringCounter++;
84     if(bitstringCounter > bitstringCounterMax) return;
85   }
86
87 }
88
89
90 int main(int argc, char *argv[])
91 {
92
93   int T = (int)pow(2,4);
94   long* stabStateIndices;
95   
96   stabStateIndices = calloc(2*T+1,sizeof(long));
97
98   stabStateIndices[0] = (int)(pow(2,T))-1;
99
100   printBinary(stabStateIndices[0],T);
101   printf("!!\n");
102
103   supplement2(&stabStateIndices[0], T, 1.0);
104
105   int i;
106   for(i=0; i<2*T; i++)
107     printBinary(stabStateIndices[i], T);
108   printf("\n");
109
110   free(stabStateIndices);
111   
112   return 0;
113   
114 }