-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path05_binary_matrix_rank_test.py
63 lines (49 loc) · 1.7 KB
/
05_binary_matrix_rank_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import math
import copy
import gf2matrix
def test(input, n, M=32, Q=32):
N = int(math.floor(n/(M*Q))) #Number of blocks
if N < 38:
print(" Number of blocks must be greater than 37")
p = 0.0
return [0]*9
# Compute the reference probabilities for FM, FMM and remainder
r = M
product = 1.0
for i in range(r):
upper1 = (1.0 - (2.0**(i-Q)))
upper2 = (1.0 - (2.0**(i-M)))
lower = 1-(2.0**(i-r))
product = product * ((upper1*upper2)/lower)
FR_prob = product * (2.0**((r*(Q+M-r)) - (M*Q)))
r = M-1
product = 1.0
for i in range(r):
upper1 = (1.0 - (2.0**(i-Q)))
upper2 = (1.0 - (2.0**(i-M)))
lower = 1-(2.0**(i-r))
product = product * ((upper1*upper2)/lower)
FRM1_prob = product * (2.0**((r*(Q+M-r)) - (M*Q)))
LR_prob = 1.0 - (FR_prob + FRM1_prob)
FM = 0 # Number of full rank matrices
FMM = 0 # Number of rank -1 matrices
remainder = 0
for blknum in range(N):
block = [None] * (M*Q)
for i in range(M*Q):
block[i] = int(input[blknum*M*Q + i],2)
# Put in a matrix
matrix = gf2matrix.matrix_from_bits(M,Q,block,blknum)
rank = gf2matrix.rank(M,Q,matrix,blknum)
if rank == M: # count the result
FM += 1
elif rank == M-1:
FMM += 1
else:
remainder += 1
chisq = (((FM-(FR_prob*N))**2)/(FR_prob*N))
chisq += (((FMM-(FRM1_prob*N))**2)/(FRM1_prob*N))
chisq += (((remainder-(LR_prob*N))**2)/(LR_prob*N))
p = math.e **(-chisq/2.0)
success = (p >= 0.01)
return [n, M, Q, N, FM, FMM, chisq, p, success]