Discrete helpers/walsh perm

From testwiki
Jump to navigation Jump to search

Template:Dh-link A Walsh permutation is a periodic permutation (Perm) that is derived from an invertible binary matrix.

import numpy as np

from discretehelpers.perm import Perm
from discretehelpers.walsh_perm import WalshPerm


vector   = [3, 7, 1]
vector_6 = [3, 7, 1, 8, 16, 32]

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

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

sequence = [0, 3, 7, 4, 1, 2, 6, 5]

perm_finite   = Perm(sequence)
perm_periodic = Perm(sequence, perilen=8)

wp = WalshPerm(vector)

assert wp == WalshPerm(vector=vector_6)
assert wp == WalshPerm(matrix=matrix)
assert wp == WalshPerm(matrix=matrix_4)
assert wp == WalshPerm(perm=sequence)
assert wp == WalshPerm(perm=perm_finite)
assert wp == WalshPerm(perm=perm_periodic)

assert wp != perm_finite
assert wp == perm_periodic

assert wp.vector() == wp.vector(3) == (3, 7, 1)
assert                wp.vector(4) == (3, 7, 1, 8)

assert  np.array_equal(wp.matrix() , matrix  )
assert  np.array_equal(wp.matrix(4), matrix_4)

Template:Collapsible START

assert wp.inverse == WalshPerm([4, 5, 3])

assert wp.inverse.sequence() == [0, 4, 5, 1, 3, 7, 6, 2]

assert wp * wp.inverse == wp.inverse * wp == WalshPerm() == Perm()  # identity permutation

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

assert np.array_equal(wp.inverse.matrix(), inverse_matrix)


real_matrix_product = np.array([[1, 2, 2],
                                [0, 1, 2],
                                [0, 0, 1]])

assert np.array_equal(
    np.dot(matrix, inverse_matrix),
    real_matrix_product
)

assert np.array_equal(
    real_matrix_product % 2,
    np.eye(3)  # identity matrix
)

Template:Collapsible END

Template:Collapsible START

wp.mapping == {1: 3, 2: 7, 3: 4, 4: 1, 5: 2, 7: 5}
wp.moved == {1, 2, 3, 4, 5, 7}

wp.perilen == 8

wp.order == 3

wp.cycles == wp.cycles_dynamic() == [[1, 3, 4], [2, 7, 5]]
wp.cycles_dynamic(16) == wp.cycles_dynamic(degree=4) == [[1, 3, 4], [2, 7, 5], [9, 11, 12], [10, 15, 13]]

assert wp.sequence() == wp.sequence(8)  == wp.sequence(degree=3) == sequence
assert                  wp.sequence(16) == wp.sequence(degree=4) == sequence + [_ + 8 for _ in sequence]

wp.apply_on_vector(['0', '1', '2', '3', '4', '5', '6', '7']) == ['0', '4', '5', '1', '3', '7', '6', '2']

The attribute length is not inherited. Template:Collapsible END

Template:Collapsible START

The beige edges connect complementary colors. This is pattern 2. (For degree 3 the possible values are in 1...7.)
assert wp.complement_pattern(3) == 2   #  7 - 5
assert wp.complement_pattern(4) == 10  # 15 - 5

assert wp.complement_distance == 5

The complement patterns differ by degree. But the distance from the highest element 2degree1 is always the same Template:Collapsible END

assert wp.degree == 3

assert wp.transpose == WalshPerm([7, 3, 2])
assert wp.transpose_vector() == (7, 3, 2)

assert wp.determinant == 1
assert WalshPerm([7, 11, 13, 14]).determinant == -3