Template:Algebraic normal form/python
Appearance
Walsh permutations and Sierpiński triangles |
---|
This code uses the Python library discrete helpers.
from discretehelpers.walsh_perm import WalshPerm
from discretehelpers.binv import Binv
def anf_int_to_boolf(input_integer):
from discretehelpers.boolf import Boolf
input_indices = Binv(intval=input_integer).indices
if input_indices == set():
return Boolf(False)
result_boolf = Boolf(False)
for i in input_indices:
loop_indices = Binv(intval=i).indices
loop_boolf = Boolf(multi_and=loop_indices)
result_boolf = result_boolf ^ loop_boolf
return result_boolf
def create_sequence(arity):
sequence = []
for i in range(2 ** 2 ** arity):
boolf = anf_int_to_boolf(i)
binv = Binv(boolf.truth_table(arity))
sequence.append(binv.intval)
return sequence
sequence_0 = create_sequence(0)
# [0, 1]
sequence_1 = create_sequence(1)
# [0, 3, 2, 1]
sequence_2 = create_sequence(2)
# [0, 15, 10, 5, 12, 3, 6, 9, 8, 7, 2, 13, 4, 11, 14, 1]
sequence_3 = create_sequence(3)
# [0, 255, 170, 85 ... 84, 171, 254, 1]
sequence_4 = create_sequence(4)
# [0, 65535, 43690, 21845 ... 21844, 43691, 65534, 1]
wp_0 = WalshPerm(perm=sequence_0)
wp_1 = WalshPerm(perm=sequence_1)
wp_2 = WalshPerm(perm=sequence_2)
wp_3 = WalshPerm(perm=sequence_3)
wp_4 = WalshPerm(perm=sequence_4)
wp_0.matrix()
# array([[1]])
wp_1.matrix()
# array([[1, 0],
# [1, 1]])
wp_2.matrix()
# array([[1, 0, 0, 0],
# [1, 1, 0, 0],
# [1, 0, 1, 0],
# [1, 1, 1, 1]])
wp_3.matrix()
# array([[1, 0, 0, 0, 0, 0, 0, 0],
# [1, 1, 0, 0, 0, 0, 0, 0],
# [1, 0, 1, 0, 0, 0, 0, 0],
# [1, 1, 1, 1, 0, 0, 0, 0],
# [1, 0, 0, 0, 1, 0, 0, 0],
# [1, 1, 0, 0, 1, 1, 0, 0],
# [1, 0, 1, 0, 1, 0, 1, 0],
# [1, 1, 1, 1, 1, 1, 1, 1]])
wp_4.matr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