final v1.0 files
[weak_simulation_stab_extent.git] / supplement.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4
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 supplement(long stabStateIndices[], int T, double fractionSupplement)
17 {
18
19   int K = round(log(T)/log(2)); // T = 2^K
20
21   //printf("K=%d\n", K);
22
23   int treeDepth, levelMaskDepth, maskIndex;
24
25   unsigned long levelMask;
26
27   unsigned long long maskList[K];
28
29   unsigned long given = (unsigned long)stabStateIndices[0]; // the given bitstring
30
31   unsigned long bitstring;
32
33   int bitstringCounter = 1;  // bitstringCounter starts at 1 since 0 is the given bitstring
34   int bitstringCounterMax = floor(fractionSupplement*T);
35
36   int i;
37
38   if(bitstringCounter > bitstringCounterMax) return;
39
40   // first added bitstring is alpha ... alpha for alpha = 01
41   bitstring = 1;
42   for(i=1; i<T/2; i++) {
43     bitstring *= 4;
44     bitstring += 1;
45   }
46   //printf("%lu\n", bitstring);
47   stabStateIndices[bitstringCounter] = given^(bitstring);
48   bitstringCounter++;
49   if(bitstringCounter > bitstringCounterMax) return;
50
51   // second added bitstring is beta ... beta for beta = 10
52   bitstring = 2;
53   for(i=1; i<T/2; i++) {
54     bitstring *= 4;
55     bitstring += 2;
56   }
57   //printf("%lu\n", bitstring);
58   stabStateIndices[bitstringCounter] = given^bitstring;
59   bitstringCounter++;
60   if(bitstringCounter > bitstringCounterMax) return;
61
62   maskList[0] = round(pow(2,pow(2,K)))-1;
63
64   for(treeDepth=1; treeDepth<=K-1; treeDepth++) {
65
66     levelMask = round(pow(2,pow(2,K-treeDepth))-1);
67     // repeat this unit-cell mask to the other bits on levelMask
68     for(levelMaskDepth=2; levelMaskDepth<=round(pow(2,treeDepth-1)); levelMaskDepth++) {
69       levelMask += round(pow(2,pow(2,K-treeDepth+1)))*levelMask;
70     }
71
72     //printf("levelMask=");
73     //printBinary(levelMask, T);
74
75     bitstring = stabStateIndices[1] ^ levelMask;
76
77     for(maskIndex=0; maskIndex < (int)(pow(2,treeDepth)); maskIndex++) {
78       stabStateIndices[bitstringCounter] = bitstring;
79       for(i=0; i<treeDepth; i++){
80         // if ith binary digit of maskIndex is 1 then XOR with ith string in maskList
81         stabStateIndices[bitstringCounter] ^= ((int)(maskIndex/(pow(2,i)))%2)*maskList[i];
82       }
83       //printf("new bitstring=");
84       //printBinary(stabStateIndices[bitstringCounter], T);
85       bitstringCounter++;
86       if(bitstringCounter > bitstringCounterMax) return;
87     }
88
89     maskList[treeDepth] = levelMask;
90     
91   }
92     
93 }
94
95
96 int main(int argc, char *argv[])
97 {
98
99   int T = (int)pow(2,4);
100   long* stabStateIndices;
101   
102   stabStateIndices = calloc(T+1,sizeof(long));
103
104   stabStateIndices[0] = (int)(pow(2,T))-1;
105
106   supplement(&stabStateIndices[0], T, 1.0);
107
108   int i;
109   for(i=0; i<T+1; i++)
110     printBinary(stabStateIndices[i], T);
111   printf("\n");
112   
113   return 0;
114   
115 }