Example #1
0
def main():
    operation = sys.argv[1]

    if operation == '-a':
        p, n, a = read('test.txt')
        les = LinearEquationSystem(a, p)
        test_accuracy(les)
    elif operation == '-g':
        gen_test(5, 9, 29, '-s' in sys.argv)
    elif operation == '-gauss':
        p, n, a = read('test.txt')
        les = LinearEquationSystem(a, p)
        sol, count = gaussian(les)
        if count == 0:
            x = sol([])
            print('Решение:', x)
            print('Ответ верный' if M.mul(les.a, M.t([x]), les.p) == les.b else 'Ответ неверный')
            return
        while True:
            t = input('Введите {} чисел в Z_{} через пробел или -1 для выхода\n'.format(count, les.p))
            if t == '-1':
                return
            x = sol(list(map(lambda el: int(el), t.split())))
            print('Решение:', x)
            print('Ответ верный' if M.mul(les.a, M.t([x]), les.p) == les.b else 'Ответ неверный')
    else:
        print('Некорректная операция')
Example #2
0
def wiedemann2(les: LinearEquationSystem):
    a, b, p, n = les.a, M.t(les.b)[0], les.p, les.n
    a_powers = [M.unit(n, n)]
    for i in range(1, 2 * n):
        a_powers.append(M.mul(a_powers[-1], a, p))
    aib = [M.mul_vec(ai, b, p) for ai in a_powers]
    k = 0
    gs = [[1]]
    uk1 = [0, 1] + ([0] * (n - 2))
    while Poly.deg(gs[k]) < n and k < n:
        seq = []
        for i in range(2 * n - Poly.deg(gs[k])):
            gab = M.mul_vec(fa(gs[k], a, p), aib[i], p)
            ugab = V.mul_sum(uk1, gab, p)
            seq.append(ugab)

        assert len(seq)
        f = berlekamp(seq, p)
        gs.append(Poly.mul(f, gs[k], p))
        k += 1
        uk1 = ([0] * k) + [1] + ([0] * (n - k - 1))

    print('k =', k)
    g = gs[-1]
    x = V.zero(n)
    for i in range(Poly.deg(g)):
        x = V.add(x, V.mul_scalar(aib[i], -g[i], p), p)
    return x
Example #3
0
def wiedemann1(les: LinearEquationSystem):
    a, b, p, n = les.a, M.t(les.b)[0], les.p, les.n
    for trial in range(MAX_TRIALS):
        bs = [b]
        ys = [[0] * n]
        ds = [0]
        k = 0

        while bs[k] != V.zero(n):
            u = [random.randint(0, p - 1) for _idx in range(n)]
            seq = []
            for i in range(2 * (n - ds[k])):
                ai = M.power(a, i, p)
                aib = M.mul_vec(ai, b, p)
                uaib = V.mul_sum(u, aib, p)
                seq.append(uaib)

            if not len(seq):
                break
            f = berlekamp(seq, p)
            if f is None:
                break

            ys.append(V.add(ys[k], M.mul_vec(f_tilda(f, a, p), b, p), p))
            bs.append(V.add(b, M.mul_vec(a, ys[k + 1], p), p))
            ds.append(ds[k] + len(f))
            k += 1
        if bs[k] != V.zero(n):
            print(trial, end='\r')
            continue
        return V.mul_scalar(ys[k], -1, p)
Example #4
0
def test_accuracy(les):
    for func, func_name in FUNCS:
        x = func(les)
        if x is None:
            print(func_name, ': ответ не найден')
        else:
            correct = M.mul(les.a, M.t([x]), les.p) == les.b
            print(func_name, ':', x, 'Ответ верный' if correct else 'Ответ неверный')