Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
22144 views
o

I��cj5�@s�dZddlZddlZddlmZddlmZddlmZddlm	Z
ddlmZddl
mZdd	lmZdd
lmZddlmZddlmZdZGd
d�dee�ZdS)aC
A boolean function class that represents a General Linear Equivalence Class
============================================================================

The ``boolean_function_general_linear_class`` module defines
the ``BooleanFunctionGeneralLinearClass`` class,
which is a subclass of BooleanFunctionImproved that represents
a general linear equivalence class of boolean functions.


AUTHORS:

- Paul Leopardi (2023-02-05): initial version

EXAMPLES:

::

    sage: from boolean_cayley_graphs.boolean_function_general_linear_class import (
    ....:     BooleanFunctionGeneralLinearClass)
    sage: bf = BooleanFunctionGeneralLinearClass([0,0,0,1])
    sage: type(bf)
    <class 'boolean_cayley_graphs.boolean_function_general_linear_class.BooleanFunctionGeneralLinearClass'>
    sage: bf.truth_table(format='int')
    (0, 0, 0, 1)
�N)�datetime)�BooleanFunction)�Matrix)�FiniteField)�Integer)�ZZ)�stdout)�BooleanFunctionImproved)�SaveablezUTF-8c@s\eZdZdZedd��Zedd��Zedd��Zdd	�Zd
d�Z	dd
�Z
dd�Zdd�ZdS)�!BooleanFunctionGeneralLinearClassae
    A subclass of BooleanFunctionImproved that represents
    a general linear equivalence class of boolean functions.

    The class inherits from BooleanFunctionImproved and 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_general_linear_class import (
        ....:     BooleanFunctionGeneralLinearClass)
        sage: bf1 = BooleanFunctionGeneralLinearClass([0,1,0,0])
        sage: type(bf1)
        <class 'boolean_cayley_graphs.boolean_function_general_linear_class.BooleanFunctionGeneralLinearClass'>
        sage: bf1.algebraic_normal_form()
        x0*x1 + x0
        sage: bf1.truth_table()
        (False, True, False, False)

    TESTS:

    ::

        sage: from boolean_cayley_graphs.boolean_function_general_linear_class import (
        ....:     BooleanFunctionGeneralLinearClass)
        sage: bf = BooleanFunctionGeneralLinearClass([0,1,0,0])
        sage: print(bf)
        Boolean function with 2 variables

        sage: from boolean_cayley_graphs.boolean_function_general_linear_class import (
        ....:     BooleanFunctionGeneralLinearClass)
        sage: bf = BooleanFunctionGeneralLinearClass([0,1,0,0])
        sage: latex(bf)
        \text{\texttt{Boolean{ }function{ }with{ }2{ }variables}}
   cC�|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_general_linear_class import (
            ....:     BooleanFunctionGeneralLinearClass)
            sage: bf2 = BooleanFunctionGeneralLinearClass([0,1,0,0])
            sage: bf2_tt_buffer = bf2.tt_buffer()
            sage: bf2_test = BooleanFunctionGeneralLinearClass.from_tt_buffer(2, bf2_tt_buffer)
            sage: bf2_test.algebraic_normal_form()
            x0*x1 + x0
            sage: bf2 == bf2_test
            True
            sage: bf3 = BooleanFunctionGeneralLinearClass([0,1,0,0]*2)
            sage: bf3.nvariables()
            3
            sage: bf3_tt_buffer = bf3.tt_buffer()
            sage: bf3_test = BooleanFunctionGeneralLinearClass.from_tt_buffer(3, bf3_tt_buffer)
            sage: bf3 == bf3_test
            True
        )r	�from_tt_buffer)�cls�dim�	tt_buffer�r�_/home/user/Boolean-Cayley-graphs/boolean_cayley_graphs/boolean_function_general_linear_class.pyr
`s'z0BooleanFunctionGeneralLinearClass.from_tt_buffercCr)a�
        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_general_linear_class import (
            ....:     BooleanFunctionGeneralLinearClass)
            sage: bf2 = BooleanFunctionGeneralLinearClass([0,1,0,0])
            sage: bf2_tt_hex = bf2.tt_hex()
            sage: bf2_test = BooleanFunctionGeneralLinearClass.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_general_linear_class import (
            ....:     BooleanFunctionGeneralLinearClass)
            sage: bf1 = BooleanFunctionGeneralLinearClass([0,1])
            sage: bf1_tt_hex = bf1.tt_hex()
            sage: bf1_test = BooleanFunctionGeneralLinearClass.from_tt_hex(1, bf1_tt_hex)
            sage: bf1_test.algebraic_normal_form()
            x
            sage: bf1 == bf1_test
            True
            sage: bf3 = BooleanFunctionGeneralLinearClass([0,1,0,0]*2)
            sage: bf3.nvariables()
            3
            sage: bf3_tt_hex = bf3.tt_hex()
            sage: bf3_test = BooleanFunctionGeneralLinearClass.from_tt_hex(3, bf3_tt_hex)
            sage: bf3 == bf3_test
            True
        )r	�from_tt_hex)rr�tt_hexrrrr�s4z-BooleanFunctionGeneralLinearClass.from_tt_hexcCs|t�|��S)a�
        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_general_linear_class import (
            ....:     BooleanFunctionGeneralLinearClass)
            sage: bf2 = BooleanFunctionGeneralLinearClass([1,0,1,1])
            sage: bf2_csv_name = tmp_filename(ext='.csv')
            sage: bf2.save_as_csv(bf2_csv_name)
            sage: bf2_test = BooleanFunctionGeneralLinearClass.from_csv(bf2_csv_name)
            sage: bf2 == bf2_test
            True
            sage: os.remove(bf2_csv_name)
            sage: bf3 = BooleanFunctionGeneralLinearClass([0,1,0,0]*2)
            sage: bf3_csv_name = tmp_filename(ext='.csv')
            sage: bf3.save_as_csv(bf3_csv_name)
            sage: bf3_test = BooleanFunctionGeneralLinearClass.from_csv(bf3_csv_name)
            sage: bf3 == bf3_test
            True
        )r	�from_csv)r�
csv_file_namerrrr�s$z*BooleanFunctionGeneralLinearClass.from_csvcCs
|�|�S)a�
        Test for equality between extended translation equivalence classes.

        WARNING:

        This test is for mathematical equivalence rather than strict equality.

        INPUT:

        - ``other`` - BooleanFunctionExtendedTranslateClassification: another equivalence class.

        OUTPUT:

        A Boolean value indicating whether ``self`` is equivalent to ``other``.

        EXAMPLES:

        ::

            sage: from boolean_cayley_graphs.boolean_function_improved import BooleanFunctionImproved
            sage: from boolean_cayley_graphs.boolean_function_extended_translate_classification import (
            ....:     BooleanFunctionExtendedTranslateClassification as BooleanFunctionETC)
            sage: R2.<x0,x1> = BooleanPolynomialRing(2)
            sage: p = x0*x1
            sage: f1 =BooleanFunctionImproved(p)
            sage: c1 = BooleanFunctionETC.from_function(f1)
            sage: f2 =BooleanFunctionImproved([0,0,0,1])
            sage: c2 = BooleanFunctionETC.from_function(f2)
            sage: print(c2.algebraic_normal_form)
            x0*x1
            sage: print(c1 == c2)
            True
        )�is_linear_equivalent)�self�otherrrr�__eq__�s
"z(BooleanFunctionGeneralLinearClass.__eq__cCst|�}t|�|�S)a�
        Return the complement Boolean function of `self`.

        INPUT:

        - ``self`` -- the current object.

        EXAMPLES:

        ::

            sage: from boolean_cayley_graphs.boolean_function_general_linear_class import BooleanFunctionGeneralLinearClass
            sage: bf0 = BooleanFunctionGeneralLinearClass([1,0,1,1])
            sage: bf1 = ~bf0
            sage: type(bf1)
            <class 'boolean_cayley_graphs.boolean_function_general_linear_class.BooleanFunctionGeneralLinearClass'>
            sage: bf1.algebraic_normal_form()
            x0*x1 + x0
            sage: bf1.truth_table()
            (False, True, False, False)
        �r	�type)r�bf_selfrrr�
__invert__
sz,BooleanFunctionGeneralLinearClass.__invert__cCst|�}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_general_linear_class import BooleanFunctionGeneralLinearClass
            sage: bf0 = BooleanFunctionGeneralLinearClass([1,0,1,0])
            sage: bf1 = BooleanFunctionGeneralLinearClass([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+BooleanFunctionGeneralLinearClass([0,1])
            Traceback (most recent call last):
            ...
            ValueError: the two Boolean functions must have the same number of variables
        r�rrrrrr�__add__'�#z)BooleanFunctionGeneralLinearClass.__add__cCst|�}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_general_linear_class import BooleanFunctionGeneralLinearClass
            sage: bf0 = BooleanFunctionGeneralLinearClass([1,0,1,0])
            sage: bf1 = BooleanFunctionGeneralLinearClass([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*BooleanFunctionGeneralLinearClass([0,1])
            Traceback (most recent call last):
            ...
            ValueError: the two Boolean functions must have the same number of variables
        rrrrr�__mul__Nr!z)BooleanFunctionGeneralLinearClass.__mul__cCst|�}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_general_linear_class import BooleanFunctionGeneralLinearClass
            sage: bf0 = BooleanFunctionGeneralLinearClass([1,0,1,0])
            sage: bf1 = BooleanFunctionGeneralLinearClass([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|BooleanFunctionGeneralLinearClass([0,1])
            Traceback (most recent call last):
            ...
            ValueError: the two Boolean functions must have the same number of variables
        rrrrr�__or__ur!z(BooleanFunctionGeneralLinearClass.__or__N)
�__name__�
__module__�__qualname__�__doc__�classmethodr
rrrrr r"r#rrrrr8s'
)
6
&%''r)r'�binascii�csvr�sage.crypto.boolean_functionr�sage.matrix.constructorr�0sage.rings.finite_rings.finite_field_constructorr�GF�sage.rings.integerr�sage.rings.integer_ringr�sysr�/boolean_cayley_graphs.boolean_function_improvedr	�boolean_cayley_graphs.saveabler
�+boolean_cayley_graphs.cayley_graph_controls�cayley_graph_controls�controls�encodingrrrrr�<module>s$