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

I��c�l�@sdZddlmZddlmZmZddlmZddlmZddl	m
Z
ddlmZddl
mZdd	lmZdd
lmZddlmZddlmZdd
lZdd
lZddlmZddlmZddlmZmZm Z ddl!m"Z"dd
l#m$Z%dd
l&Z&dd
l'Z(dZ)Gdd�dee"�Z*Gdd�de*�Z+d
S)a[
Classification of boolean functions within an extended translation class
========================================================================

The ``boolean_function_extended_translate_classification`` module defines:

 * the ``BooleanFunctionExtendedTranslateClassification`` class;
   which represents the classification of the general linear classes
   within the extended translation class of a boolean function; and
 * the ``BooleanFunctionExtendedTranslateClassPart`` class,
   which represents part of an extended translate classification.

AUTHORS:

- Paul Leopardi (2023-01-26): initial version

EXAMPLES:

::

    The classification of the boolean function defined by the polynomial x2 + x1*x2.

    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.<x1,x2> = BooleanPolynomialRing(2)
    sage: p = x2+x1*x2
    sage: f = BooleanFunctionImproved(p)
    sage: c = BooleanFunctionETC.from_function(f)
    sage: dict(sorted(c.__dict__.items()))
    {'algebraic_normal_form': x0*x1 + x1,
     'boolean_function_index_matrix': [0 2 1 3]
     [1 3 0 2]
     [2 0 3 1]
     [3 1 2 0],
     'boolean_function_list': [Boolean function with 2 variables,
      Boolean function with 2 variables,
      Boolean function with 2 variables,
      Boolean function with 2 variables],
     'general_linear_class_index_matrix': [0 0 1 0]
     [1 0 0 0]
     [0 0 0 1]
     [0 1 0 0],
     'general_linear_class_list': [Boolean function with 2 variables,
      Boolean function with 2 variables]}

REFERENCES:

The extended translation equivalence class and the general linear equivalence class
of a boolean function are defined by Leopardi [Leo2017]_.
�)�datetime)�array�argwhere)�log)�Graph)�matrix)�latex)�load)�matrix_plot)�Integer)�
SageObject)�stdoutN)�BooleanFunctionImproved)�!BooleanFunctionGeneralLinearClass)�
BijectiveList�List�ShelveBijectiveList)�Saveable�sagec@s<eZdZdZdd�Zdd�Ze			d
d	d
��Zdd�ZdS)�)BooleanFunctionExtendedTranslateClassParta�
    Partial classification of the general linear classes within the
    extended translation equivalence class of a boolean function.

    EXAMPLES:

    ::

        sage: from boolean_cayley_graphs.boolean_function_improved import BooleanFunctionImproved
        sage: from boolean_cayley_graphs.boolean_function_extended_translate_classification import (
        ....:     BooleanFunctionExtendedTranslateClassPart as BooleanFunctionETCPart)
        sage: R2.<x1,x2> = BooleanPolynomialRing(2)
        sage: p = x1+x2+x1*x2
        sage: f = BooleanFunctionImproved(p)
        sage: c1 = BooleanFunctionETCPart.from_function(f, c_stop=1)
        sage: print(c1)
        BooleanFunctionExtendedTranslateClassPart.from_function(BooleanFunctionImproved(x0*x1 + x0 + x1, c_start=0, c_stop=1))
        sage: latex(c1)
        \text{\texttt{BooleanFunctionExtendedTranslateClassPart.from{\char`\_}function(BooleanFunctionImproved(x0*x1{ }+{ }x0{ }+{ }x1,{ }c{\char`\_}start=0,{ }c{\char`\_}stop=1))}}

    cOs�z|d}|j|_|j|_|j|_|j|_|j|_|j|_WdS|�d�|_|�d�|_|�d�|_|�d�|_|�d�|_|�d�|_YdS)	a�
        Constructor from an object or from class attributes.

        INPUT:

        - ``algebraic_normal_form`` -- a polynomial of the type
          returned by ``BooleanFunction.algebraic_normal_form()``,
          representing the ``BooleanFunction`` whose classification this is.
        - ``boolean_function_index_matrix`` -- a ``Matrix` of integers,
          which are indices into ``boolean_function_list`` representing the
          distinct boolean functions.
        - ``boolean_function_list`` -- a list of boolean functions.
        - ``general_linear_class_index_matrix`` -- a ``Matrix` of integers,
          which are indices into ``general_linear_class_list`` representing the
          general linear equivalence classes.
        - ``general_linear_class_list`` -- a list of matrices representing the
          general linear equivalence classes.
        - ``c_start`` -- an integer representing the index of
          the first row of each matrix.

        OUTPUT:

        None.

        EFFECT:

        The current object ``self`` is initialized as follows.

        Each of
        - ``algebraic_normal_form``
        - ``boolean_function_index_matrix``
        - ``boolean_function_list``
        - ``general_linear_class_index_matrix``
        - ``general_linear_class_list``
        - ``c_start``
        is set to the corresponding input parameter.

        EXAMPLES:

        The partial classification of the boolean function defined by the polynomial
        :math:`x_1 + x_2 + x_1 x_2` is copied from `c1` to `c2`.

        ::

            sage: from boolean_cayley_graphs.boolean_function_improved import BooleanFunctionImproved
            sage: from boolean_cayley_graphs.boolean_function_extended_translate_classification import (
            ....:     BooleanFunctionExtendedTranslateClassPart as BooleanFunctionETCPart)
            sage: R2.<x1,x2> = BooleanPolynomialRing(2)
            sage: p = x1+x2+x1*x2
            sage: f = BooleanFunctionImproved(p)
            sage: c1 = BooleanFunctionETCPart.from_function(f, c_stop=1)
            sage: c2 = BooleanFunctionETCPart(c1)
            sage: print(c1 == c2)
            True
        r�algebraic_normal_form�boolean_function_index_matrix�boolean_function_list�!general_linear_class_index_matrix�general_linear_class_list�c_startN)rrrrrr�pop��self�args�kwargs�sobj�r"�l/home/user/Boolean-Cayley-graphs/boolean_cayley_graphs/boolean_function_extended_translate_classification.py�__init__ss68������z2BooleanFunctionExtendedTranslateClassPart.__init__cCsF|j|j��}t|�jdt|j�dt|j�dt|�dS)a%
        Sage string representation.

        INPUT:

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

        EXAMPLES:

        ::

            sage: from boolean_cayley_graphs.boolean_function_improved import BooleanFunctionImproved
            sage: from boolean_cayley_graphs.boolean_function_extended_translate_classification import (
            ....:     BooleanFunctionExtendedTranslateClassPart as BooleanFunctionETCPart)
            sage: R2.<x1,x2> = BooleanPolynomialRing(2)
            sage: p = x1+x2+x1*x2
            sage: f = BooleanFunctionImproved(p)
            sage: c1 = BooleanFunctionETCPart.from_function(f, c_stop=1)
            sage: print(c1)
            BooleanFunctionExtendedTranslateClassPart.from_function(BooleanFunctionImproved(x0*x1 + x0 + x1, c_start=0, c_stop=1))
        �'.from_function(BooleanFunctionImproved(z
, c_start=z	, c_stop=�)))rr�nrows�type�__name__�reprr)r�c_stopr"r"r#�_repr_�s"��������z0BooleanFunctionExtendedTranslateClassPart._repr_rNFcs�tj}tj}|��}d|}|dkr|}nt||�}|��}	|dkp'|dko'|}
|
r-t�nt�}||}t||�}
t||�}t	�}|�
��t|�D]]}|rbtt
��|dd�tt|����t��t||�D]:}|�||��t�fdd�t|�D��}t|�}|�|�}||
|||f<t|�}|�|�}|||||f<|r�	qg|��qI|��}|��|��|r�	|r�tt
���t��||	|
||||d�S)	aB
        Constructor from the ``BooleanFunction`` ``boolf``.

        INPUT:

        - ``boolf`` -- an object of class ``BooleanFunction``.
        - ``c_start`` -- integer (default: 0).
          The smallest value of `c` to use for extended translates.
        - ``c_stop`` -- integer (default: ``None``).
          One more than largest value of `c` to use for extended
          translates. ``None`` means use all remaining values.
        - ``limited_memory`` -- boolean (default: ``False``).
          A flag indicating whether the classification might be
          too large to fit into memory.

        OUTPUT:

        An object of class BooleanFunctionExtendedTranslateClassPart,
        initialized as follows.

        - ``algebraic_normal_form`` is set to ``boolf.algebraic_normal_form()``,
        - ``boolean_function_index_matrix`` -- a ``Matrix` of integers,
          which are indices into ``boolean_function_list`` representing the
          distinct boolean functions.
        - ``boolean_function_list`` -- a list of boolean functions.
        - ``general_linear_class_index_matrix`` -- a ``Matrix` of integers,
          which are indices into ``general_linear_class_list`` representing the
          general linear equivalence classes.
        - ``general_linear_class_list`` -- a list of matrices representing the
          general linear equivalence classes.
        - ``c_start`` is set to smallest value of `c` used for extended translates.

        Each entry ``boolean_function_index_matrix[c-c_start,b]`` corresponds to
        the boolean function
        :math:`x \mapsto \mathtt{boolf}(x+b) + \langle c, x \rangle + \mathtt{boolf}(b)`.
        This enumerates all of the extended translates of ``boolf`` having ``c``
        from ``c_start`` to but not including ``c_stop``.

        EXAMPLES:

        A partial classification of the boolean function defined by the polynomial
        :math:`x_1 + x_2 + x_1 x_2`.

        ::

            sage: from boolean_cayley_graphs.boolean_function_improved import BooleanFunctionImproved
            sage: from boolean_cayley_graphs.boolean_function_extended_translate_classification import (
            ....:     BooleanFunctionExtendedTranslateClassPart as BooleanFunctionETCPart)
            sage: R2.<x1,x2> = BooleanPolynomialRing(2)
            sage: p = x1+x2+x1*x2
            sage: f = BooleanFunctionImproved(p)
            sage: c1 = BooleanFunctionETCPart.from_function(f,c_start=2,c_stop=4)
            sage: dict(sorted(c1.__dict__.items()))
            {'algebraic_normal_form': x0*x1 + x0 + x1,
             'boolean_function_index_matrix': [0 2 1 3]
             [1 3 0 2],
             'boolean_function_list': [Boolean function with 2 variables,
              Boolean function with 2 variables,
              Boolean function with 2 variables,
              Boolean function with 2 variables],
             'c_start': 2,
             'general_linear_class_index_matrix': [0 1 0 0]
             [0 0 0 1],
             'general_linear_class_list': [Boolean function with 2 variables,
              Boolean function with 2 variables]}
        �N�� )�endc3s�|]}�|�VqdS)Nr")�.0�x��fr"r#�	<genexpr>Ls�zJBooleanFunctionExtendedTranslateClassPart.from_function.<locals>.<genexpr>)rrrrrr)�controls�checking�timing�
nvariables�minrrrrr�extended_translate�range�printr�now�len�get_listr
�flush�zero_translate�tupler�index_appendr�sync�
close_dict�remove_dict)�cls�boolfrr+�limited_memoryr7r8�dim�vrZ
use_shelveZboolean_function_bijection�c_lenrrr�b�c�ttZbf_ttZbf_tt_indexZbf_etcZ	glc_indexrr"r3r#�
from_function�shI
��



�
�z7BooleanFunctionExtendedTranslateClassPart.from_functioncCsT|durdS|j|jko)|j|jko)|j|jko)|j|jko)|j|jko)|j|jkS)a�
        Test for equality between partial classifications.

        WARNING:

        This test is for strict equality rather than mathematical equivalence.

        INPUT:

        - ``other`` - BooleanFunctionExtendedTranslateClassPart: another partial classification.

        OUTPUT:

        A Boolean value indicating whether ``self`` strictly equals ``other``.

        EXAMPLES:

        ::

            sage: from boolean_cayley_graphs.boolean_function_improved import BooleanFunctionImproved
            sage: from boolean_cayley_graphs.boolean_function_extended_translate_classification import (
            ....:     BooleanFunctionExtendedTranslateClassPart as BooleanFunctionETCPart)
            sage: R2.<x0,x1> = BooleanPolynomialRing(2)
            sage: p = x0*x1
            sage: f1 = BooleanFunctionImproved(p)
            sage: c1 = BooleanFunctionETCPart.from_function(f1, c_stop=1)
            sage: f2 = BooleanFunctionImproved([0,0,0,1])
            sage: c2 = BooleanFunctionETCPart.from_function(f2, c_stop=1)
            sage: print(c2.algebraic_normal_form)
            x0*x1
            sage: print(c1 == c2)
            True
        NF)rrrrrr�r�otherr"r"r#�__eq__os"
�
�
�
�
�z0BooleanFunctionExtendedTranslateClassPart.__eq__)rNF)	r)�
__module__�__qualname__�__doc__r$r,�classmethodrQrTr"r"r"r#r\sO"�rc@sFeZdZdZdZdZdZdd�Zdd�Ze				
ddd��Z
d
d�ZdS)�.BooleanFunctionExtendedTranslateClassificationa�
    Classification of the Cayley graphs within the
    extended translation equivalence class of a boolean function.

    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.<x1,x2> = BooleanPolynomialRing(2)
        sage: p = x1+x2+x1*x2
        sage: f = BooleanFunctionImproved(p)
        sage: c1 = BooleanFunctionETC.from_function(f)
        sage: print(c1)
        BooleanFunctionExtendedTranslateClassification.from_function(BooleanFunctionImproved(x0*x1 + x0 + x1))
        sage: latex(c1)
        \text{\texttt{BooleanFunctionExtendedTranslateClassification.from{\char`\_}function(BooleanFunctionImproved(x0*x1{ }+{ }x0{ }+{ }x1))}}
    z_bent_function.csvz_cg_class_list.csvz
_matrices.csvcOs�z|d}|j|_|j|_|j|_|j|_|j|_WdS|�d�|_|�d�|_|�d�|_|�d�|_|�d�|_YdS)aO
        Constructor from an object or from class attributes.

        INPUT:

        - ``sobj`` -- BooleanFunctionExtendedTranslateClassification: object to copy.

        - ``algebraic_normal_form`` -- a polynomial of the type
          returned by ``BooleanFunction.algebraic_normal_form()``,
          representing the ``BooleanFunctionImproved`` whose classification this is.
        - ``boolean_function_index_matrix`` -- a ``Matrix` of integers,
          which are indices into ``boolean_function_list`` representing the
          distinct boolean functions.
        - ``boolean_function_list`` -- a list of boolean functions.
        - ``general_linear_class_index_matrix`` -- a ``Matrix` of integers,
          which are indices into ``general_linear_class_list`` representing the
          general linear equivalence classes.
        - ``general_linear_class_list`` -- a list of matrices representing the
          general linear equivalence classes.

        OUTPUT:

        None.

        EFFECT:

        The current object ``self`` is initialized as follows.

        Each of
        - ``algebraic_normal_form``
        - ``boolean_function_index_matrix``
        - ``boolean_function_list``
        - ``general_linear_class_index_matrix``
        - ``general_linear_class_list``
        is set to the corresponding input parameter.

        EXAMPLES:

        The classification of the boolean function defined by the polynomial
        :math:`x_1 + x_2 + x_1 x_2` is copied from `c1` to `c2`.

        ::

            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.<x1,x2> = BooleanPolynomialRing(2)
            sage: p = x1+x2+x1*x2
            sage: f = BooleanFunctionImproved(p)
            sage: c1 = BooleanFunctionETC.from_function(f)
            sage: c2 = BooleanFunctionETC(c1)
            sage: print(c1 == c2)
            True
        rrrrrrN)rrrrrrrr"r"r#r$�s.7�����z7BooleanFunctionExtendedTranslateClassification.__init__cCst|�jdt|j�dS)a
        Sage string representation.

        INPUT:

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

        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.<x1,x2> = BooleanPolynomialRing(2)
            sage: p = x1+x2+x1*x2
            sage: f = BooleanFunctionImproved(p)
            sage: c1 = BooleanFunctionETC.from_function(f)
            sage: print(c1)
            BooleanFunctionExtendedTranslateClassification.from_function(BooleanFunctionImproved(x0*x1 + x0 + x1))
        r%r&)r(r)r*r)rr"r"r#r,s����z5BooleanFunctionExtendedTranslateClassification._repr_TFcCstj||d�}||�S)a�
        Constructor from the ``BooleanFunctionImproved`` ``boolf``.

        INPUT:

        - ``boolf`` -- an object of class ``BooleanFunctionImproved``.
        - ``list_dual_graphs`` -- boolean. a flag indicating
          whether to list dual graphs.
        - ``limited_memory`` -- boolean. A flag indicating
          whether the classification might be too large
          to fit into memory. Default is False.

        OUTPUT:

        An object of class BooleanFunctionExtendedTranslateClassification,
        initialized as follows.

        - ``algebraic_normal_form`` is set to ``boolf.algebraic_normal_form()``,
        - ``boolean_function_index_matrix`` -- a ``Matrix` of integers,
          which are indices into ``boolean_function_list`` representing the
          distinct boolean functions.
        - ``boolean_function_list`` -- a list of boolean functions.
        - ``general_linear_class_index_matrix`` -- a ``Matrix` of integers,
          which are indices into ``general_linear_class_list`` representing the
          general linear equivalence classes.
        - ``general_linear_class_list`` -- a list of matrices representing the
          general linear equivalence classes.

        Each entry ``boolean_function_index_matrix[c,b]`` corresponds to
        the Cayley graph of the boolean function
        :math:`x \mapsto \mathtt{boolf}(x+b) + \langle c, x \rangle + \mathtt{boolf}(b)`.
        This enumerates all of the extended translates of ``boolf``.

        EXAMPLES:

        The classification of the boolean function defined by the polynomial
        :math:`x_1 + x_2 + x_1 x_2`.

        ::

            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.<x1,x2> = BooleanPolynomialRing(2)
            sage: p = x1+x2+x1*x2
            sage: f = BooleanFunctionImproved(p)
            sage: c3 = BooleanFunctionETC.from_function(f)
            sage: dict(sorted(c3.__dict__.items()))
            {'algebraic_normal_form': x0*x1 + x0 + x1,
             'boolean_function_index_matrix': [0 2 1 3]
             [1 3 0 2]
             [2 0 3 1]
             [3 1 2 0],
             'boolean_function_list': [Boolean function with 2 variables,
              Boolean function with 2 variables,
              Boolean function with 2 variables,
              Boolean function with 2 variables],
             'general_linear_class_index_matrix': [0 1 1 1]
             [1 1 0 1]
             [1 0 1 1]
             [1 1 1 0],
             'general_linear_class_list': [Boolean function with 2 variables,
              Boolean function with 2 variables]}

        TESTS:

        The classification of the boolean function defined by the polynomial
        :math:`x_1 + x_2 + x_1 x_2`, but with list_dual_graphs=False.

        ::

            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.<x1,x2> = BooleanPolynomialRing(2)
            sage: p = x1+x2+x1*x2
            sage: f = BooleanFunctionImproved(p)
            sage: c4 = BooleanFunctionETC.from_function(f,list_dual_graphs=False)
            sage: dict(sorted(c4.__dict__.items()))
            {'algebraic_normal_form': x0*x1 + x0 + x1,
             'boolean_function_index_matrix': [0 2 1 3]
             [1 3 0 2]
             [2 0 3 1]
             [3 1 2 0],
             'boolean_function_list': [Boolean function with 2 variables,
              Boolean function with 2 variables,
              Boolean function with 2 variables,
              Boolean function with 2 variables],
             'general_linear_class_index_matrix': [0 1 1 1]
             [1 1 0 1]
             [1 0 1 1]
             [1 1 1 0],
             'general_linear_class_list': [Boolean function with 2 variables,
              Boolean function with 2 variables]}
        )rJ)rrQ)rHrI�list_dual_graphsrJ�cpr"r"r#rQ s
e�z<BooleanFunctionExtendedTranslateClassification.from_functioncCsH|durdS|j|jko#|j|jko#|j|jko#|j|jko#|j|jkS)ah
        Test for equality between classifications.

        WARNING:

        This test is for strict equality rather than mathematical equivalence.

        INPUT:

        - ``other`` - BooleanFunctionExtendedTranslateClassification: another classification.

        OUTPUT:

        A Boolean value indicating whether ``self`` strictly equals ``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
        NF)rrrrrrRr"r"r#rT�s"
�
�
�
�z5BooleanFunctionExtendedTranslateClassification.__eq__N)TF)r)rUrVrW�bent_function_csv_suffix�cg_class_list_csv_suffix�matrices_csv_suffixr$r,rXrQrTr"r"r"r#rY�sK�jrY),rWr�numpyrr�sage.functions.logr�sage.graphs.graphr�sage.matrix.constructorr�sage.misc.latexr�sage.misc.persistr	�sage.plot.matrix_plotr
�sage.rings.integerr�sage.structure.sage_objectr�sysr
�glob�np�/boolean_cayley_graphs.boolean_function_improvedrZ;boolean_cayley_graphs.boolean_function_general_linear_classr� boolean_cayley_graphs.containersrrr�boolean_cayley_graphs.saveabler�+boolean_cayley_graphs.cayley_graph_controls�cayley_graph_controlsr6�csv�os.path�os�default_algorithmrrYr"r"r"r#�<module>s4=B