Discrete helpers/walsh perm

From Wikiversity
Jump to navigation Jump to search

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)
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