Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 19204
1
r"""
2
Tests for GF(2) linear algebra
3
==============================
4
5
The ``linear`` module defines functions that
6
test for linearity of functions defined on
7
GF(2) vector spaces.
8
9
AUTHORS:
10
11
- Paul Leopardi (2023-01-16): initial version
12
13
"""
14
#*****************************************************************************
15
# Copyright (C) 2016-2023 Paul Leopardi [email protected]
16
#
17
# Distributed under the terms of the GNU General Public License (GPL)
18
# as published by the Free Software Foundation; either version 2 of
19
# the License, or (at your option) any later version.
20
# http://www.gnu.org/licenses/
21
#*****************************************************************************
22
23
24
from sage.matrix.constructor import Matrix
25
from sage.rings.finite_rings.finite_field_constructor import FiniteField as GF
26
from sage.rings.integer import Integer
27
from boolean_cayley_graphs.integer_bits import base2
28
29
encoding = "UTF-8"
30
31
32
def is_linear(dim, perm, certificate=False):
33
r"""
34
Check if a permutation on `range(2**dim)` is linear on `GF(2)**dim`.
35
36
INPUT:
37
38
- ``dim`` -- the dimension of the GF(2) linear space.
39
- ``perm`` -- a function from `range(2**dim)` to iself, usually a permutation.
40
- ``certificate`` -- bool (default False). If true, return the GF(2) matrix
41
that corresponds to the permutation.
42
43
OUTPUT:
44
45
If ``certificate`` is false, a bool value.
46
If ``certificate`` is true, a tuple consisting of either (False, None)
47
or (True, M), where M is a GF(2) matrix that corresponds to the permutation.
48
49
EXAMPLES:
50
51
::
52
53
sage: from boolean_cayley_graphs.linear import is_linear
54
sage: dim = 2
55
sage: perm1 = lambda x: x*3 % 4
56
sage: is_linear(dim, perm1)
57
True
58
sage: is_linear(dim, perm1, certificate=True)
59
(
60
[1 0]
61
True, [1 1]
62
)
63
sage: perm2 = lambda x: (x+1) % 4
64
sage: is_linear(dim, perm2)
65
False
66
sage: is_linear(dim, perm2, certificate=True)
67
(False, None)
68
"""
69
v = 2**dim
70
71
# If perm(0) != 0 then perm cannot be linear.
72
if perm(0) != 0:
73
return (False, None) if certificate else False
74
75
# Check that perm is linear on each pair of basis vectors.
76
possibly_linear = True
77
for a in range(dim):
78
for b in range(a + 1, dim):
79
if perm(2**a) ^ perm(2**b) != perm((2**a) ^ (2**b)):
80
possibly_linear = False
81
break
82
if not possibly_linear:
83
break
84
if not possibly_linear:
85
return (False, None) if certificate else False
86
87
# Create the GF(2) matrix corresponding to perm, assuming linearity.
88
perm_matrix = Matrix(GF(2), [
89
base2(dim, Integer(perm(2**a)))
90
for a in range(dim)]).transpose()
91
# Check for each vector that the matrix gives the same result as perm.
92
vm = Matrix(GF(2), [
93
base2(dim, Integer(x))
94
for x in range(v)]).transpose()
95
linear = perm_matrix * vm == vm[:, [perm(x) for x in range(v)]]
96
if certificate:
97
return (True, perm_matrix) if linear else (False, None)
98
else:
99
return linear
100
101