代解决的代码4

import numpy as np
import sympy
from scipy.optimize import fsolve
from sympy import *
import math

H = np.array(
            [[0, 0, 0, 1, 1, 1, 1],
             [0, 1, 1, 0, 0, 1, 1],
             [1, 0, 1, 0, 1, 0, 1]])

def func(X):
    '''
    求校验矩阵H
    '''

    H = np.array(
        [[0, 0, 0, 1, 1, 1, 1],
         [0, 1, 1, 0, 0, 1, 1],
         [1, 0, 1, 0, 1, 0, 1]])

    G1 = np.eye(4)

    G2 = [[X[0], X[1], X[2]]
        , [X[3], X[4], X[5]]
        , [X[6], X[7], X[8]]
        , [X[9], X[10], X[11]]]
    G = np.concatenate((G1, G2), axis=1)
    #print(G.shape)

    GH_T = np.dot(G, np.transpose(H))
    m, n = GH_T.shape[0], GH_T.shape[1]
    for i in range(m):
        for j in range(n):
            GH_T[i][j] %= 2
    GH_T = np.array(GH_T)
    GH_T = GH_T.reshape(1,12)
    #print(GH_T)

    return GH_T

def shuzu(j,len):
    C = np.zeros((len,))
    for k in range(0,len):
        C[k] = int(j[k])
        #print(C)
    return C

C = np.zeros((12,))
for i in range(0,pow(2,12)):
    D = np.zeros((1,12))
    i = bin(i)
    j = i[2:]
    j = j.zfill(12)
    C = shuzu(j,12)
    G = func(C)
    if (G== D).all():
        print(C)
        X = C


G1 = np.eye(4)
G2 = [[X[0], X[1], X[2]]
        , [X[3], X[4], X[5]]
        , [X[6], X[7], X[8]]
        , [X[9], X[10], X[11]]]
G = np.concatenate((G1, G2), axis=1)
print(G)

X = [0,1,0,1,0,1,0,1]
X1 = X[0:4]
X2 = X[4:8]
C1 = np.dot(X1,G)
i = C1.shape[0]
for j in range(i):
    C1[j] %= 2
C2 = np.dot(X2,G)
i = C2.shape[0]
for j in range(i):
    C2[j] %= 2
print(C1)
print(C2)

def func2(e):
    H = np.array(
        [[0, 0, 0, 1, 1, 1, 1],
         [0, 1, 1, 0, 0, 1, 1],
         [1, 0, 1, 0, 1, 0, 1]])
    s_ = np.dot(e,np.transpose(H))
    k = s_.shape[0]
    for i in range(k):
        s_ %= 2
    return s_


r = [0,1,0,1,1,1,0]
H = np.array(
        [[0, 0, 0, 1, 1, 1, 1],
         [0, 1, 1, 0, 0, 1, 1],
         [1, 0, 1, 0, 1, 0, 1]])
s = np.dot(r,np.transpose(H))
i = s.shape[0]
for j in range(i):
    s[j] %= 2
print(s)


e = np.zeros((7,))
e_dataset = []
for i in range(0,pow(2,7)):
    i = bin(i)
    j = i[2:]
    j = j.zfill(7)
    e = shuzu(j,7)
    s_ = func2(e)
    #print(s_)
    if (s_ == s).all():
        #print(e)
        e_dataset.append(e)

def siranguji(e_dataset):
    p_mat = np.array([[6/7,1/7]
                  ,[1/7,6/7]])
    P_ = []
    length = len(e_dataset)
    for i in range(0, length):
        P = 1
        for j in range(0,7):
            if e_dataset[i][j] == 1:
                P = P * p_mat[0][1]
            elif e_dataset[i][j] == 1:
                P = P * p_mat[0][0]
            P_.append(P)

    return P_

P_ = siranguji(e_dataset)
index = np.argmax(P_)
r_ = np.zeros([7])
r = np.array(r)

e_ = np.array(e_dataset[index])

for j in range(0,7):
    e_[j] = int(e_[j])

print(r)
print(e_)
for j in range(0,7):
    r_  = np.bitwise_xor(r,e_)
print(r_)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值