-
Notifications
You must be signed in to change notification settings - Fork 1
/
PolarMatrix.py
77 lines (55 loc) · 1.38 KB
/
PolarMatrix.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-
import numpy as np
import copy as cp
m = 9
n = int(np.power(2,m))
T = np.array([[1, 0],
[1, 1]]
, dtype='int')
def recursivelyCalc_Iw(ep):
# BEC の相互情報量を計算
Iw = np.zeros((m+1,n))
Iw[0][0] = 1 - ep
for i in range(1,m+1):
tmp = int(np.power(2,i))
for j in range(tmp):
if j%2 == 0:
Iw[i][j] = np.power(Iw[i-1][j/2],2)
else:
Iw[i][j] = 2*Iw[i-1][int(j/2)] - np.power(Iw[i-1][int(j/2)],2)
return Iw[m]
def Bitrev(k):
# 置換行列 A_k2 の作成
k_2 = int(np.power(2, k))
A_k2 = np.zeros((k_2, k_2), dtype='int')
for i in range(k_2):
if i % 2 == 0:
tmp = int(i / 2)
A_k2[i][tmp] = 1
else:
tmp = int(i / 2 + k_2 / 2)
A_k2[i][tmp] = 1
return A_k2
def Dot_XOR(A, B): # ABの積(XOR)
out = np.dot(A, B)
for i in range(n):
if out[i] % 2 == 0:
out[i] = 0
else:
out[i] = 1
return out
def MakeGenMatrix():
Tn = cp.deepcopy(T)
Bn = np.eye(2, dtype='int')
for i in range(m - 1):
Tn = np.kron(T, Tn)
Bn = np.dot(Bitrev(i + 2), np.kron(np.eye(2), Bn))
Gen = np.dot(Bn, Tn) # 生成行列
return Gen
def main():
Gen = MakeGenMatrix()
Gen = Gen.astype(np.int64)
nameGen = 'GenerateMatrix_' + str(n) + '.txt'
np.savetxt(nameGen, Gen, fmt='%d')
if __name__ == '__main__':
main()