Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 8800
1
#*****************************************************************************
2
# Copyright (C) 2016 Paul Leopardi [email protected]
3
#
4
# Distributed under the terms of the GNU General Public License (GPL)
5
# as published by the Free Software Foundation; either version 2 of
6
# the License, or (at your option) any later version.
7
# http://www.gnu.org/licenses/
8
#*****************************************************************************
9
10
import copy
11
import numpy as np
12
13
14
from boolean_cayley_graphs.bent_function import BentFunction
15
from boolean_cayley_graphs.bent_function_cayley_graph_classification import BentFunctionCayleyGraphClassification
16
from boolean_cayley_graphs.containers import BijectiveList
17
from sage.structure.sage_object import register_unpickle_override
18
19
import boolean_cayley_graphs.cayley_graph_controls as controls
20
21
load("bent_function_extended_affine_representative_polynomials.sage")
22
23
24
def save_boolean_dimension_cayley_graph_classifications(
25
dim,
26
start=1,
27
stop=None,
28
dir=None):
29
r"""
30
"""
31
verbose = controls.verbose
32
33
p = bent_function_extended_affine_representative_polynomials(dim)
34
c = [None]*len(p)
35
if stop is None:
36
stop = len(p)
37
for n in range(start, stop):
38
if verbose:
39
print('Function', n, ':')
40
f = BentFunction(p[n])
41
c[n] = BentFunctionCayleyGraphClassification.from_function(f)
42
name_n = 'p'+str(dim)+'_'+str(n)
43
c[n].save_mangled(name_n, dir=dir)
44
if verbose:
45
c[n].report()
46
return c
47
48
49
def load_boolean_dimension_cayley_graph_classifications(
50
dim,
51
start=1,
52
stop=None,
53
dir=None):
54
r"""
55
"""
56
verbose = controls.verbose
57
58
register_unpickle_override(
59
'bent_function_cayley_graph_classification',
60
'BentFunctionCayleyGraphClassification',
61
BentFunctionCayleyGraphClassification)
62
63
p = bent_function_extended_affine_representative_polynomials(dim)
64
c = [None]*len(p)
65
if stop is None:
66
stop = len(p)
67
for n in range(start, stop):
68
if verbose:
69
print('Function', n, ':')
70
name_n = 'p'+str(dim)+'_'+str(n)
71
c[n] = BentFunctionCayleyGraphClassification.load_mangled(
72
name_n,
73
dir=dir)
74
if verbose:
75
c[n].report()
76
return c
77
78
79
class BooleanDimensionCayleyGraphReclassification(SageObject):
80
r"""
81
"""
82
83
84
def __init__(self, cc):
85
r"""
86
"""
87
verbose = controls.verbose
88
89
self.dim = 0
90
cayley_graph_class_bijection = BijectiveList()
91
self.classification_list = copy.deepcopy(cc)
92
c = self.classification_list
93
self.reclassification_table = [None]*len(c)
94
r = self.reclassification_table
95
for n in range(len(c)):
96
if c[n] is not None:
97
if verbose:
98
print(n, ':')
99
if self.dim == 0:
100
p = c[n].algebraic_normal_form
101
bentf = BentFunction(p)
102
self.dim = bentf.nvariables()
103
104
cg_class_list = c[n].cayley_graph_class_list
105
cg_index_matrix = np.matrix(c[n].bent_cayley_graph_index_matrix)
106
dg_index_matrix = np.matrix(c[n].dual_cayley_graph_index_matrix)
107
cb_index_list = c[n].first_matrix_index_list()
108
r[n] = matrix(5, len(cg_class_list))
109
110
c_class_counts = np.histogram(cg_index_matrix,
111
list(range(len(cg_class_list) + 1)))[0]
112
d_class_counts = np.histogram(dg_index_matrix,
113
list(range(len(cg_class_list) + 1)))[0]
114
115
new_cg_index_matrix = cg_index_matrix.copy()
116
for i in range(len(cg_class_list)):
117
g = cg_class_list[i]
118
reclass_index = cayley_graph_class_bijection.index_append(g)
119
r[n][0, i] = reclass_index
120
cb_index = cb_index_list[i]
121
c_index = cb_index[0]
122
b_index = cb_index[1]
123
assert cg_index_matrix[c_index, b_index] == i
124
new_cg_index_matrix = np.where(cg_index_matrix == i,
125
reclass_index,
126
new_cg_index_matrix)
127
j = dg_index_matrix[c_index, b_index]
128
r[n][1, i] = i
129
r[n][2, i] = c_class_counts[i]
130
r[n][3, i] = j
131
r[n][4, i] = d_class_counts[j]
132
c[n].bent_cayley_graph_index_matrix = new_cg_index_matrix
133
134
new_dg_index_matrix = dg_index_matrix.copy()
135
for i in range(len(cg_class_list)):
136
reclass_index = r[n][0, i]
137
new_dg_index_matrix = np.where(dg_index_matrix == i,
138
reclass_index,
139
new_dg_index_matrix)
140
c[n].dual_cayley_graph_index_matrix = new_dg_index_matrix
141
142
if verbose:
143
print(r[n])
144
145
# Get the list part of the BijectiveList.
146
self.cayley_graph_class_list = cayley_graph_class_bijection.get_list()
147
148
149
def save_matrix_plots(self, prefix='re', cmap='gist_stern'):
150
r"""
151
"""
152
c = self.classification_list
153
for n in range(len(c)):
154
if c[n] is not None:
155
figure_name = prefix + str(self.dim) + '_' + str(n)
156
c[n].save_matrix_plots(figure_name, cmap=cmap)
157
158