Boolean-Cayley-graphs / boolean_cayley_graphs / __pycache__ / boolean_function_improved.cpython-310.pyc
24897 viewso
I��crp � @ s� d Z ddlZddlZddlmZ ddlmZ ddlmZ ddlm Z
ddlmZ ddl
mZ dd lmZ dd
lmZ ddlmZ ddlmZmZ dd
lmZ ddlmZ ddlmZ dZG dd� dee�Z dS )a�
An improved Boolean function class
==================================
The ``boolean_function_improved`` module defines
the ``BooleanFunctionImproved`` class,
which is a subclass of BooleanFunction that adds extra methods.
One such method is ``cayley_graph``,
which returns the Cayley graph of the Boolean function.
AUTHORS:
- Paul Leopardi (2016-08-23): initial version
EXAMPLES:
::
sage: from boolean_cayley_graphs.boolean_function_improved import BooleanFunctionImproved
sage: bf = BooleanFunctionImproved([0,0,0,1])
sage: type(bf)
<class 'boolean_cayley_graphs.boolean_function_improved.BooleanFunctionImproved'>
sage: bf.truth_table(format='int')
(0, 0, 0, 1)
� N)�datetime)�BooleanFunction)�Matrix)�FiniteField)�Integer)�ZZ)�stdout)�boolean_cayley_graph)�boolean_linear_code��base2�inner)� is_linear)�SaveablezUTF-8c @ s� e Zd ZdZedd� �Zedd� �Zedd� �Zdd � Zd
d� Z dd
� Z
dd� Zdd� Zdd� Z
dd� Zd)dd�Zd*dd�Zd+dd�Zdd� Zd d!� Zd"d#� Zd$d%� Zd&d'� Zd(S ),�BooleanFunctionImproveda6
A subclass of BooleanFunction that adds extra methods.
The class inherits from BooleanFunction is initialized in the same way.
The class inherits from Saveable to obtain
load_mangled and save_mangled methods.
EXAMPLES:
::
sage: from boolean_cayley_graphs.boolean_function_improved import BooleanFunctionImproved
sage: bf1 = BooleanFunctionImproved([0,1,0,0])
sage: type(bf1)
<class 'boolean_cayley_graphs.boolean_function_improved.BooleanFunctionImproved'>
sage: bf1.algebraic_normal_form()
x0*x1 + x0
sage: bf1.truth_table()
(False, True, False, False)
TESTS:
::
sage: from sage.crypto.boolean_function import BooleanFunction
sage: bf = BooleanFunctionImproved([0,1,0,0])
sage: print(bf)
Boolean function with 2 variables
sage: from sage.crypto.boolean_function import BooleanFunction
sage: bf = BooleanFunctionImproved([0,1,0,0])
sage: latex(bf)
\text{\texttt{Boolean{ }function{ }with{ }2{ }variables}}
c C s t t�|�t�}| �||�S )a�
Constructor from the buffer tt_buffer.
The buffer tt_buffer is assumed to be the result of method tt_buffer(),
which returns a result of type buffer representing a truth table in hex.
INPUT:
- ``cls`` -- the class object.
- ``dim`` -- integer: the dimension of the Boolean function.
- ``tt_buffer`` -- buffer: the result of the method tt_buffer()
for the Boolean function.
EXAMPLES:
::
sage: from boolean_cayley_graphs.boolean_function_improved import BooleanFunctionImproved
sage: bf2 = BooleanFunctionImproved([0,1,0,0])
sage: bf2_tt_buffer = bf2.tt_buffer()
sage: bf2_test = BooleanFunctionImproved.from_tt_buffer(2, bf2_tt_buffer)
sage: bf2_test.algebraic_normal_form()
x0*x1 + x0
sage: bf2 == bf2_test
True
sage: bf3 = BooleanFunctionImproved([0,1,0,0]*2)
sage: bf3.nvariables()
3
sage: bf3_tt_buffer = bf3.tt_buffer()
sage: bf3_test = BooleanFunctionImproved.from_tt_buffer(3, bf3_tt_buffer)
sage: bf3 == bf3_test
True
)�str�binascii�b2a_hex�encoding�from_tt_hex)�cls�dim� tt_buffer�tt_hex� r �S/home/user/Boolean-Cayley-graphs/boolean_cayley_graphs/boolean_function_improved.py�from_tt_buffer_ s &z&BooleanFunctionImproved.from_tt_bufferc C s4 |dk rt |d�}d| }t||�}t|�S t|�S )a3
Constructor from the dimension dim, and the string tt_hex.
The string tt_hex is assumed to be the result of method tt_hex(), which returns
a string representing a truth table in hex.
INPUT:
- ``cls`` -- the class object.
- ``dim`` -- integer: the dimension of the Boolean function.
- ``tt_hex`` -- string: the result of the method tt_hex() for the Boolean function.
EXAMPLES:
::
sage: from boolean_cayley_graphs.boolean_function_improved import BooleanFunctionImproved
sage: bf2 = BooleanFunctionImproved([0,1,0,0])
sage: bf2_tt_hex = bf2.tt_hex()
sage: bf2_test = BooleanFunctionImproved.from_tt_hex(2, bf2_tt_hex)
sage: bf2_test.algebraic_normal_form()
x0*x1 + x0
sage: bf2 == bf2_test
True
TESTS:
::
sage: from boolean_cayley_graphs.boolean_function_improved import BooleanFunctionImproved
sage: bf1 = BooleanFunctionImproved([0,1])
sage: bf1_tt_hex = bf1.tt_hex()
sage: bf1_test = BooleanFunctionImproved.from_tt_hex(1, bf1_tt_hex)
sage: bf1_test.algebraic_normal_form()
x
sage: bf1 == bf1_test
True
sage: bf3 = BooleanFunctionImproved([0,1,0,0]*2)
sage: bf3.nvariables()
3
sage: bf3_tt_hex = bf3.tt_hex()
sage: bf3_test = BooleanFunctionImproved.from_tt_hex(3, bf3_tt_hex)
sage: bf3 == bf3_test
True
� � � )r r r )r r r Z
tt_integer�vZtt_bitsr r r r � s 2
z#BooleanFunctionImproved.from_tt_hexc C sV t |��}t�|�}t|�}t�t|d �|d �W d � S 1 s$w Y dS )a4
Constructor from a csv file.
The csv file is assumed to be produced by the method save_as_csv().
INPUT:
- ``cls`` -- the class object.
- ``csv_file_name`` -- string: the name of the csv file to read from.
EXAMPLES:
::
sage: import csv
sage: import os
sage: from boolean_cayley_graphs.boolean_function_improved import BooleanFunctionImproved
sage: bf2 = BooleanFunctionImproved([1,0,1,1])
sage: bf2_csv_name = tmp_filename(ext='.csv')
sage: bf2.save_as_csv(bf2_csv_name)
sage: bf2_test = BooleanFunctionImproved.from_csv(bf2_csv_name)
sage: bf2 == bf2_test
True
sage: os.remove(bf2_csv_name)
sage: bf3 = BooleanFunctionImproved([0,1,0,0]*2)
sage: bf3_csv_name = tmp_filename(ext='.csv')
sage: bf3.save_as_csv(bf3_csv_name)
sage: bf3_test = BooleanFunctionImproved.from_csv(bf3_csv_name)
sage: bf3 == bf3_test
True
�
nvariablesr N)�open�csv�
DictReader�nextr r �int)r Z
csv_file_nameZcsv_file�reader�rowr r r �from_csv� s
#
�$�z BooleanFunctionImproved.from_csvc C s t | �}t| �| �S )ag
Return the complement Boolean function of `self`.
INPUT:
- ``self`` -- the current object.
EXAMPLES:
::
sage: from boolean_cayley_graphs.boolean_function_improved import BooleanFunctionImproved
sage: bf0 = BooleanFunctionImproved([1,0,1,1])
sage: bf1 = ~bf0
sage: type(bf1)
<class 'boolean_cayley_graphs.boolean_function_improved.BooleanFunctionImproved'>
sage: bf1.algebraic_normal_form()
x0*x1 + x0
sage: bf1.truth_table()
(False, True, False, False)
�r �type)�self�bf_selfr r r �
__invert__� s z"BooleanFunctionImproved.__invert__c C s t | �}t| �|| �S )a�
Return the elementwise sum of `self`and `other` which must have the same number of variables.
INPUT:
- ``self`` -- the current object.
- ``other`` -- another Boolean function.
OUTPUT:
The elementwise sum of `self`and `other`
EXAMPLES:
::
sage: from boolean_cayley_graphs.boolean_function_improved import BooleanFunctionImproved
sage: bf0 = BooleanFunctionImproved([1,0,1,0])
sage: bf1 = BooleanFunctionImproved([1,1,0,0])
sage: (bf0+bf1).truth_table(format='int')
(0, 1, 1, 0)
sage: S = bf0.algebraic_normal_form() + bf1.algebraic_normal_form()
sage: (bf0+bf1).algebraic_normal_form() == S
True
TESTS:
::
sage: bf0+BooleanFunctionImproved([0,1])
Traceback (most recent call last):
...
ValueError: the two Boolean functions must have the same number of variables
r* �r, �otherr- r r r �__add__ � #zBooleanFunctionImproved.__add__c C s t | �}t| �|| �S )a�
Return the elementwise product of `self`and `other` which must have the same number of variables.
INPUT:
- ``self`` -- the current object.
- ``other`` -- another Boolean function.
OUTPUT:
The elementwise product of `self`and `other`
EXAMPLES:
::
sage: from boolean_cayley_graphs.boolean_function_improved import BooleanFunctionImproved
sage: bf0 = BooleanFunctionImproved([1,0,1,0])
sage: bf1 = BooleanFunctionImproved([1,1,0,0])
sage: (bf0*bf1).truth_table(format='int')
(1, 0, 0, 0)
sage: P = bf0.algebraic_normal_form() * bf1.algebraic_normal_form()
sage: (bf0*bf1).algebraic_normal_form() == P
True
TESTS:
::
sage: bf0*BooleanFunctionImproved([0,1])
Traceback (most recent call last):
...
ValueError: the two Boolean functions must have the same number of variables
r* r/ r r r �__mul__2 r2 zBooleanFunctionImproved.__mul__c C s t | �}t| �||B �S )a�
Return the concatenation of `self` and `other` which must have the same number of variables.
INPUT:
- ``self`` -- the current object.
- ``other`` -- another Boolean function.
OUTPUT:
The concatenation of `self`and `other`
EXAMPLES:
::
sage: from boolean_cayley_graphs.boolean_function_improved import BooleanFunctionImproved
sage: bf0 = BooleanFunctionImproved([1,0,1,0])
sage: bf1 = BooleanFunctionImproved([1,1,0,0])
sage: (bf0|bf1).truth_table(format='int')
(1, 0, 1, 0, 1, 1, 0, 0)
sage: C = bf0.truth_table() + bf1.truth_table()
sage: (bf0|bf1).truth_table(format='int') == C
True
TESTS:
::
sage: bf0|BooleanFunctionImproved([0,1])
Traceback (most recent call last):
...
ValueError: the two Boolean functions must have the same number of variables
r* r/ r r r �__or__Y r2 zBooleanFunctionImproved.__or__c C s t | �� �S )a�
Return the hash of ``self``.
This makes the class hashable.
INPUT:
- ``self`` -- the current object.
OUTPUT:
The hash of ``self``.
EXAMPLES::
sage: from boolean_cayley_graphs.boolean_function_improved import BooleanFunctionImproved
sage: bf1 = BooleanFunctionImproved([0,1,0,0])
sage: hash(bf1) == hash(bf1.tt_hex())
True
)�hashr �r, r r r �__hash__� s z BooleanFunctionImproved.__hash__c C � | � � }| �� }t||�S )a*
Return the Cayley graph of ``self``.
INPUT:
- ``self`` -- the current object.
OUTPUT:
The Cayley graph of ``self`` as an object of class ``Graph``.
EXAMPLES::
sage: from boolean_cayley_graphs.boolean_function_improved import BooleanFunctionImproved
sage: bf1 = BooleanFunctionImproved([0,1,0,0])
sage: g1 = bf1.cayley_graph()
sage: g1.adjacency_matrix()
[0 1 0 0]
[1 0 0 0]
[0 0 0 1]
[0 0 1 0]
)r! �extended_translater �r, r �fr r r �cayley_graph� s
z$BooleanFunctionImproved.cayley_graphc C s | d�r | � � S | � � S )a�
Return the extended Cayley graph of ``self``.
INPUT:
- ``self`` -- the current object.
OUTPUT:
The extended Cayley graph of ``self`` as an object of class ``Graph``.
This is the Cayley graph of ``self`` if ``self(0) == False``,
otherwise it is the Cayley graph of ``~self``.
EXAMPLES:
::
sage: from boolean_cayley_graphs.boolean_function_improved import BooleanFunctionImproved
sage: bf1 = BooleanFunctionImproved([0,1,0,0])
sage: g1 = bf1.extended_cayley_graph()
sage: g1.adjacency_matrix()
[0 1 0 0]
[1 0 0 0]
[0 0 0 1]
[0 0 1 0]
sage: bf2 = BooleanFunctionImproved([1,0,1,1])
sage: g2 = bf2.extended_cayley_graph()
sage: g2.adjacency_matrix()
[0 1 0 0]
[1 0 0 0]
[0 0 0 1]
[0 0 1 0]
r )r<