o
    oh                    @   s  d Z ddlmZ ddlmZmZmZmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ ddlmZmZ dd	lmZmZ dd
lmZmZ ddlmZ ddlmZ ddlmZmZ ddl m!Z! ddl"m#Z#m$Z$m%Z% ddl&m'Z'm(Z( ddl)m*Z* dd Z+eG dd deZ,G dd de,Z-G dd de-edZ.G dd de-edZ/e. Z0e/ Z1e0e_0e1e_1dd e#e2< G dd  d ee,Z3G d!d" d"ee3Z4G d#d$ d$ee3Z5G d%d& d&e3Z6G d'd( d(e3Z7G d)d* d*e3Z8G d+d, d,e3Z9G d-d. d.e3Z:G d/d0 d0e3Z;G d1d2 d2e3Z<G d3d4 d4e3Z=G d5d6 d6e3Z>d7d8 Z?d9d: Z@d;d< ZAd=d> ZBd?d@ ZCdAdB ZDddDdEZEddFdGZFddIdJZGddKdLZHdMdN ZIddOdPZJdQdR ZKdSdT ZLdUdV ZMdWdX ZNdYdZ ZOd[d\ ZPd]d^ ZQe(ZRdd_d`ZSdadb ZTdcdd ZUdedf ZVdgdh ZWdidj ZXdkdl ZYdmdn ZZdodp Z[dqdr Z\ddtduZ]dvdw Z^ddxdyZ_dzd{ Z`d|d} Zad~d Zbdd Zcdd Zddd ZedddZfdd Zgdd Zhdd Zi	s	sdddZjdd Zkdd Zledd Zmedd Znedd Zoedd Zpdd Zqe4e5e7e8e9e6e:e=fZrdd ZsdsS )z"
Boolean algebra module for SymPy
    )defaultdict)chaincombinationsproductpermutations)Add)Basic)cacheit)Tuple)sympify_method_argssympify_return)Application
Derivative)BooleanKind
NumberKind)Number)	LatticeOp)	SingletonS)ordered)_sympy_converter_sympifysympify)siftibin)
filldedentc                 C   sf   ddl m} | dkrtS | dkrtS t| |r&| j}|du r | S |r$tS tS t| tr-| S td|  )a  Like ``bool``, return the Boolean value of an expression, e,
    which can be any instance of :py:class:`~.Boolean` or ``bool``.

    Examples
    ========

    >>> from sympy import true, false, nan
    >>> from sympy.logic.boolalg import as_Boolean
    >>> from sympy.abc import x
    >>> as_Boolean(0) is false
    True
    >>> as_Boolean(1) is true
    True
    >>> as_Boolean(x)
    x
    >>> as_Boolean(2)
    Traceback (most recent call last):
    ...
    TypeError: expecting bool or Boolean, not `2`.
    >>> as_Boolean(nan)
    Traceback (most recent call last):
    ...
    TypeError: expecting bool or Boolean, not `nan`.

    r   )SymbolTFNz$expecting bool or Boolean, not `%s`.)sympy.core.symbolr   truefalse
isinstanceis_zeroBoolean	TypeError)er   z r&   g/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/logic/boolalg.py
as_Boolean   s   

r(   c                   @   s   e Zd ZdZdZeZedgedd Z	e	Z
edgedd ZeZdd	 Zedged
d Zedgedd ZeZeZedgedd ZeZdd ZdddZdd Zedd Zdd ZdS )r"   zDA Boolean object is an object for which logic operations make sense.r&   )otherr"   c                 C   
   t | |S NAndselfr)   r&   r&   r'   __and__H      
zBoolean.__and__c                 C   r*   r+   )Orr.   r&   r&   r'   __or__N   r1   zBoolean.__or__c                 C   s   t | S )zOverloading for ~Notr/   r&   r&   r'   
__invert__T   s   zBoolean.__invert__c                 C   r*   r+   Impliesr.   r&   r&   r'   
__rshift__X   r1   zBoolean.__rshift__c                 C   s
   t || S r+   r8   r.   r&   r&   r'   
__lshift__\   r1   zBoolean.__lshift__c                 C   r*   r+   )Xorr.   r&   r&   r'   __xor__c   r1   zBoolean.__xor__c                 C   sX   ddl m} ddlm} | |s||rtd|  | ko+|tt| | S )a  
        Returns ``True`` if the given formulas have the same truth table.
        For two formulas to be equal they must have the same literals.

        Examples
        ========

        >>> from sympy.abc import A, B, C
        >>> from sympy import And, Or, Not
        >>> (A >> B).equals(~B >> ~A)
        True
        >>> Not(And(A, B, C)).equals(And(Not(A), Not(B), Not(C)))
        False
        >>> Not(And(A, Not(A))).equals(Or(B, Not(B)))
        False

        r   )satisfiable
Relationalzhandling of relationals)	sympy.logic.inferencer>   sympy.core.relationalr@   hasNotImplementedErroratomsr5   
Equivalent)r/   r)   r>   r@   r&   r&   r'   equalsi   s   zBoolean.equalsTc                 C      | S r+   r&   r/   simplifyr&   r&   r'   to_nnf      zBoolean.to_nnfc           	      C   s   ddl m} ddlm} | j}t|dkrb| }|jtu r^i }| 	|D ]%}|||dvrJ|
 }|tjtjtjfv rD||||< q%ttdq%| |}|j| jkrZ| S |
 S | 
 S td)a  
        Rewrites Boolean expression in terms of real sets.

        Examples
        ========

        >>> from sympy import Symbol, Eq, Or, And
        >>> x = Symbol('x', real=True)
        >>> Eq(x, 0).as_set()
        {0}
        >>> (x > 0).as_set()
        Interval.open(0, oo)
        >>> And(-2 < x, x < 2).as_set()
        Interval.open(-2, 2)
        >>> Or(x < -2, 2 < x).as_set()
        Union(Interval.open(-oo, -2), Interval.open(2, oo))

        r   )periodicityr?      )r   Nz
                            as_set is not implemented for relationals
                            with periodic solutions
                            zGSorry, as_set has not yet been implemented for multivariate expressions)sympy.calculus.utilrM   rB   r@   free_symbolslenpopkindr   rE   _eval_as_setr   EmptySetUniversalSetRealsas_relationalrD   r   subsfuncas_set)	r/   rM   r@   freexrepsrsnewr&   r&   r'   r[      s*   

	zBoolean.as_setc                    s.   ddl m m t j fdd| jD  S )Nr   EqNec                    s,   g | ]}|j s|jst| fr|jqS r&   )
is_Boolean	is_Symbolr    binary_symbols.0irb   r&   r'   
<listcomp>   s    z*Boolean.binary_symbols.<locals>.<listcomp>)rB   rc   rd   setunionargsr6   r&   rb   r'   rg      s   zBoolean.binary_symbolsc                 C   s2   ddl m} || |}|du rtS |du rtS d S )Nr   )askTF)sympy.assumptionsro   r   r   )r/   assumptionsro   retr&   r&   r'   _eval_refine   s   
zBoolean._eval_refineNT)__name__
__module____qualname____doc__	__slots__r   rS   r   NotImplementedr0   __rand__r3   __ror__r7   r:   r;   __rrshift____rlshift__r=   __rxor__rG   rK   r[   propertyrg   rs   r&   r&   r&   r'   r"   @   s4    










1
r"   c                   @   s   e Zd ZdZdZdZdZdd Zdd Ze	dd	 Z
dddZeZeZeZeZeZeZeZeZeZeZeZeZeZdd ZeZeZeZdd Zd
S )BooleanAtomzQ
    Base class of :py:class:`~.BooleanTrue` and :py:class:`~.BooleanFalse`.
    T   c                 O   rH   r+   r&   r/   akwr&   r&   r'   rJ         zBooleanAtom.simplifyc                 O   rH   r+   r&   r   r&   r&   r'   expand   r   zBooleanAtom.expandc                 C   rH   r+   r&   r6   r&   r&   r'   	canonical   rL   zBooleanAtom.canonicalNc                 C   s   t d)Nz(BooleanAtom not allowed in this context.)r#   r.   r&   r&   r'   _noop      zBooleanAtom._noopc                 C      t tdNz
            A Boolean argument can only be used in
            Eq and Ne; all other relationals expect
            real expressions.
        r#   r   r.   r&   r&   r'   __lt__      zBooleanAtom.__lt__c                 K   rH   r+   r&   )r/   kwargsr&   r&   r'   _eval_simplify   r   zBooleanAtom._eval_simplifyr+   )ru   rv   rw   rx   re   is_Atom_op_priorityrJ   r   r   r   r   __add____radd____sub____rsub____mul____rmul____pow____rpow____truediv____rtruediv____mod____rmod___eval_powerr   __le____gt____ge__r   r&   r&   r&   r'   r      s8    

r   c                       D   e Zd ZdZdd Zdd Z fddZedd	 Zd
d Z	  Z
S )BooleanTruea  
    SymPy version of ``True``, a singleton that can be accessed via ``S.true``.

    This is the SymPy version of ``True``, for use in the logic module. The
    primary advantage of using ``true`` instead of ``True`` is that shorthand Boolean
    operations like ``~`` and ``>>`` will work as expected on this class, whereas with
    True they act bitwise on 1. Functions in the logic module will return this
    class when they evaluate to true.

    Notes
    =====

    There is liable to be some confusion as to when ``True`` should
    be used and when ``S.true`` should be used in various contexts
    throughout SymPy. An important thing to remember is that
    ``sympify(True)`` returns ``S.true``. This means that for the most
    part, you can just use ``True`` and it will automatically be converted
    to ``S.true`` when necessary, similar to how you can generally use 1
    instead of ``S.One``.

    The rule of thumb is:

    "If the boolean in question can be replaced by an arbitrary symbolic
    ``Boolean``, like ``Or(x, y)`` or ``x > 1``, use ``S.true``.
    Otherwise, use ``True``"

    In other words, use ``S.true`` only on those contexts where the
    boolean is being used as a symbolic representation of truth.
    For example, if the object ends up in the ``.args`` of any expression,
    then it must necessarily be ``S.true`` instead of ``True``, as
    elements of ``.args`` must be ``Basic``. On the other hand,
    ``==`` is not a symbolic operation in SymPy, since it always returns
    ``True`` or ``False``, and does so in terms of structural equality
    rather than mathematical, so it should return ``True``. The assumptions
    system should use ``True`` and ``False``. Aside from not satisfying
    the above rule of thumb, the assumptions system uses a three-valued logic
    (``True``, ``False``, ``None``), whereas ``S.true`` and ``S.false``
    represent a two-valued logic. When in doubt, use ``True``.

    "``S.true == True is True``."

    While "``S.true is True``" is ``False``, "``S.true == True``"
    is ``True``, so if there is any doubt over whether a function or
    expression will return ``S.true`` or ``True``, just use ``==``
    instead of ``is`` to do the comparison, and it will work in either
    case.  Finally, for boolean flags, it's better to just use ``if x``
    instead of ``if x is True``. To quote PEP 8:

    Do not compare boolean values to ``True`` or ``False``
    using ``==``.

    * Yes:   ``if greeting:``
    * No:    ``if greeting == True:``
    * Worse: ``if greeting is True:``

    Examples
    ========

    >>> from sympy import sympify, true, false, Or
    >>> sympify(True)
    True
    >>> _ is True, _ is true
    (False, True)

    >>> Or(true, false)
    True
    >>> _ is true
    True

    Python operators give a boolean result for true but a
    bitwise result for True

    >>> ~true, ~True  # doctest: +SKIP
    (False, -2)
    >>> true >> true, True >> True
    (True, 0)

    See Also
    ========

    sympy.logic.boolalg.BooleanFalse

    c                 C      dS NTr&   r6   r&   r&   r'   __bool__Q  r   zBooleanTrue.__bool__c                 C      t dS r   hashr6   r&   r&   r'   __hash__T  r   zBooleanTrue.__hash__c                    s$   |du rdS |du rdS t  |S NTFsuper__eq__r.   	__class__r&   r'   r   W  
   zBooleanTrue.__eq__c                 C      t S r+   )r   r6   r&   r&   r'   negated^  rL   zBooleanTrue.negatedc                 C      t jS )z
        Rewrite logic operators and relationals in terms of real sets.

        Examples
        ========

        >>> from sympy import true
        >>> true.as_set()
        UniversalSet

        )r   rV   r6   r&   r&   r'   r[   b  s   zBooleanTrue.as_setru   rv   rw   rx   r   r   r   r   r   r[   __classcell__r&   r&   r   r'   r      s    S
r   )	metaclassc                       r   )BooleanFalsea  
    SymPy version of ``False``, a singleton that can be accessed via ``S.false``.

    This is the SymPy version of ``False``, for use in the logic module. The
    primary advantage of using ``false`` instead of ``False`` is that shorthand
    Boolean operations like ``~`` and ``>>`` will work as expected on this class,
    whereas with ``False`` they act bitwise on 0. Functions in the logic module
    will return this class when they evaluate to false.

    Notes
    ======

    See the notes section in :py:class:`sympy.logic.boolalg.BooleanTrue`

    Examples
    ========

    >>> from sympy import sympify, true, false, Or
    >>> sympify(False)
    False
    >>> _ is False, _ is false
    (False, True)

    >>> Or(true, false)
    True
    >>> _ is true
    True

    Python operators give a boolean result for false but a
    bitwise result for False

    >>> ~false, ~False  # doctest: +SKIP
    (True, -1)
    >>> false >> false, False >> False
    (True, 0)

    See Also
    ========

    sympy.logic.boolalg.BooleanTrue

    c                 C   r   NFr&   r6   r&   r&   r'   r     r   zBooleanFalse.__bool__c                 C   r   r   r   r6   r&   r&   r'   r     r   zBooleanFalse.__hash__c                    s$   |du rdS |du rdS t  |S r   r   r.   r   r&   r'   r     r   zBooleanFalse.__eq__c                 C   r   r+   )r   r6   r&   r&   r'   r     rL   zBooleanFalse.negatedc                 C   r   )z
        Rewrite logic operators and relationals in terms of real sets.

        Examples
        ========

        >>> from sympy import false
        >>> false.as_set()
        EmptySet
        )r   rU   r6   r&   r&   r'   r[     s   zBooleanFalse.as_setr   r&   r&   r   r'   r   q  s    *
r   c                 C   s   | rt S tS r+   )r   r   r]   r&   r&   r'   <lambda>      r   c                   @   s   e Zd ZdZdZdd Zdd Zdd ZeZeZ	eZ
ed	d
 ZdddZdddZedd Zedd Zdd Zdd ZdS )BooleanFunctionzBoolean function is a function that lives in a boolean space
    It is used as base class for :py:class:`~.And`, :py:class:`~.Or`,
    :py:class:`~.Not`, etc.
    Tc                    sD   t | }t|ts|jdi  S |j fdd|jD  }t|S )Nc                       g | ]
}|j d i  qS r&   rJ   ri   r   r   r&   r'   rk         z2BooleanFunction._eval_simplify.<locals>.<listcomp>r&   )simplify_univariater    r   rJ   rZ   rn   simplify_logic)r/   r   rvr&   r   r'   r     s
   
zBooleanFunction._eval_simplifyc                 K   s   ddl m} || fi |S )Nr   r   )sympy.simplify.simplifyrJ   )r/   r   rJ   r&   r&   r'   rJ     s   zBooleanFunction.simplifyc                 C   r   r   r   r.   r&   r&   r'   r     r   zBooleanFunction.__lt__c                 G   s   dd |D S )Nc                 S      g | ]}t |qS r&   )r(   rh   r&   r&   r'   rk         z=BooleanFunction.binary_check_and_simplify.<locals>.<listcomp>r&   )r/   rn   r&   r&   r'   binary_check_and_simplify  s   z)BooleanFunction.binary_check_and_simplifyc                 C      | j | jd|iS )NrJ   )_to_nnfrn   rI   r&   r&   r'   rK        zBooleanFunction.to_nnfc                 C   r   Ndeep)_to_anfrn   r/   r   r&   r&   r'   to_anf  r   zBooleanFunction.to_anfc                 O   s   | dd}t }|D ]6}t|s||}|r<t|| r!|j}n|f}|D ]}t||v r5| j    S || q&q|| q| | S )NrJ   T)	getrl   
is_literalrK   r    rn   r5   zeroadd)clsrn   r   rJ   argsetargr   r&   r&   r'   r     s    

zBooleanFunction._to_nnfc                 O   sT   | dd}g }|D ]}|rt|rt|tr|j|d}|| q
| |ddiS )Nr   Tr   remove_trueF)r   r   r    r5   r   append)r   rn   r   r   new_argsr   r&   r&   r'   r     s   zBooleanFunction._to_anfc                 O   s"   | dd t| g|R i |S )NevaluateT)
setdefaultr   )r/   symbolsrq   r&   r&   r'   diff  s   zBooleanFunction.diffc                 C   sZ   || j v r#ddlm} ddlm} |d|| |d| |dfdS || jv r*d S tjS )Nr   )rc   	PiecewiserN   )rN   T)	rg   rB   rc   $sympy.functions.elementary.piecewiser   rY   rP   r   Zero)r/   r]   rc   r   r&   r&   r'   _eval_derivative  s   

z BooleanFunction._eval_derivativeNrt   )ru   rv   rw   rx   re   r   rJ   r   r   r   r   classmethodr   rK   r   r   r   r   r   r&   r&   r&   r'   r     s&    




r   c                       sZ   e Zd ZdZeZeZdZe	dd Z
dd Z fddZd	d
 Zdd ZdddZ  ZS )r-   aS  
    Logical AND function.

    It evaluates its arguments in order, returning false immediately
    when an argument is false and true if they are all true.

    Examples
    ========

    >>> from sympy.abc import x, y
    >>> from sympy import And
    >>> x & y
    x & y

    Notes
    =====

    The ``&`` operator is provided as a convenience, but note that its use
    here is different from its normal use in Python, which is bitwise
    and. Hence, ``And(a, b)`` and ``a & b`` will produce different results if
    ``a`` and ``b`` are integers.

    >>> And(x, y).subs(x, 1)
    y

    Nc                 C   sr   t j| }t|t}g }t }t|D ]"}|jr1|j}||v r!q|j	j|v r,t
g  S || || q|S r+   )r   r   r   _new_args_filterr-   rl   r   is_Relationalr   r   r   r   r   r   rn   newargsrelr]   cr&   r&   r'   r   @  s   


zAnd._new_args_filterc              	   C      g }d }| j D ]+}z|||}W n ty    |d u r|}Y qw |dkr)t  S |dkr2|| q|d ur=||| t|trWt|j }||rWt|| }|	| | j
| S )NFT)rn   rY   r#   r   r   r    r-   rl   issubsetr   rZ   r/   oldra   rn   badrj   old_setr&   r&   r'   
_eval_subsR  .   






zAnd._eval_subsc              	      s  ddl m m ddlm} t jdi |}t|ts|S t	|j
fdddd\}}|s0|S t	| fdddd\}}|d	 }|rB|d
 }	i i }
t	tdd |D dd }
g }g }d|
v r|
dD ]z\}}| }|j|ks{||jjv r|vrz.|t|j|j dd|\}}||| | }|||	|| kr|}n|| W qhW n	 ty   Y nw |v r||||  qh|j|kr||jjvr|j|< || qh|| qhtt}|
D ]}|
| D ]\}}|}|j}|t| ||f qq|}
d|
v sc|
D ]}|dd |
| D  qfdd|D }fdd|D }|jdd || | D |  }t }t }t|||t|dS )Nr   )Equalityr@   )linear_coeffsc                    
   t |  S r+   r    rj   r?   r&   r'   r   x     
 z$And._eval_simplify.<locals>.<lambda>Tbinaryc                    r   r+   r   r   )r   r&   r'   r   |  r   measureratioc                 S   s   g | ]}|j |fqS r&   )rP   rh   r&   r&   r'   rk     s    
z&And._eval_simplify.<locals>.<listcomp>c                 S   s   t | d S Nr   )rQ   r   r&   r&   r'   r     r   rN   Fr   c                 S   s   g | ]\}}|qS r&   r&   )ri   fr$   r&   r&   r'   rk     r   c                       g | ]}|  qS r&   rY   ri   eir^   r&   r'   rk         c                    r  r&   r  r  r	  r&   r'   rk     r
  c                 S      g | ]}|j qS r&   r   rh   r&   r&   r'   rk         )threeterm_patternsr&   )rB   r   r@   sympy.solvers.solvesetr   r   r   r    r-   r   rn   r   rR   lhsrhsrP   r   rZ   r   
ValueErrorrY   r   listxreplacerQ   extend_simplify_patterns_and_simplify_patterns_and3"_apply_patternbased_simplificationr   )r/   r   r   r   RelnonReleqsr)   r   r  sifted	nonlineqsr\   r$   r]   mbenewresiftedkr  patternsr  r   )r   r@   r^   r'   r   n  s   






 zAnd._eval_simplifyc                 C       ddl m} |dd | jD  S )Nr   )Intersectionc                 S      g | ]}|  qS r&   r[   ri   r   r&   r&   r'   rk     r   z$And._eval_as_set.<locals>.<listcomp>)sympy.sets.setsr%  rn   )r/   r%  r&   r&   r'   rT        zAnd._eval_as_setc                 O      t dd | jD  S )Nc                 S   r   r&   r4   r(  r&   r&   r'   rk     r   z,And._eval_rewrite_as_Nor.<locals>.<listcomp>)Norrn   r/   rn   r   r&   r&   r'   _eval_rewrite_as_Nor     zAnd._eval_rewrite_as_NorTc                 C   s"   |rt j| jd|i}t|S | S r   )r-   r   rn   distribute_xor_over_and)r/   r   resultr&   r&   r'   r     s   z
And.to_anfrt   )ru   rv   rw   rx   r   r   r   identitynargsr   r   r   r   rT   r.  r   r   r&   r&   r   r'   r-      s    
Dr-   c                       sV   e Zd ZdZeZeZedd Z	dd Z
dd Zdd	 Z fd
dZdddZ  ZS )r2   aM  
    Logical OR function

    It evaluates its arguments in order, returning true immediately
    when an  argument is true, and false if they are all false.

    Examples
    ========

    >>> from sympy.abc import x, y
    >>> from sympy import Or
    >>> x | y
    x | y

    Notes
    =====

    The ``|`` operator is provided as a convenience, but note that its use
    here is different from its normal use in Python, which is bitwise
    or. Hence, ``Or(a, b)`` and ``a | b`` will return different things if
    ``a`` and ``b`` are integers.

    >>> Or(x, y).subs(x, 0)
    y

    c                    sz   g }g }t j| }|D ]+}|jr1|j}||v rq|jj t fdd|D r,tg  S || || qt	|t
S )Nc                 3   s    | ]}| kV  qd S r+   r&   ri   r_   ncr&   r'   	<genexpr>      z&Or._new_args_filter.<locals>.<genexpr>)r   r   r   r   r   anyr   r   r   r   r2   r   r&   r5  r'   r     s   


zOr._new_args_filterc              	   C   r   r   )rn   rY   r#   r   r   r    r2   rl   r   r   rZ   r   r&   r&   r'   r     r   zOr._eval_subsc                 C   r$  )Nr   )Unionc                 S   r&  r&   r'  r(  r&   r&   r'   rk     r   z#Or._eval_as_set.<locals>.<listcomp>)r)  r:  rn   )r/   r:  r&   r&   r'   rT     r*  zOr._eval_as_setc                 O   r+  )Nc                 S   r   r&   r4   r(  r&   r&   r'   rk     r   z,Or._eval_rewrite_as_Nand.<locals>.<listcomp>)Nandrn   r-  r&   r&   r'   _eval_rewrite_as_Nand  r/  zOr._eval_rewrite_as_Nandc                    s   ddl m}m}m  ||}|r' fdd|D }|jdi |S t jdi |}t|t	s7|S t
 }t|||d tS )Nr   )LeGerc   c                    s&   i | ]}|  |j|j|jqS r&   )rZ   r  r  strictrh   rc   r/   r&   r'   
<dictcomp>  s
    z%Or._eval_simplify.<locals>.<dictcomp>r   r&   )rB   r=  r>  rc   rE   r  r   r   r    r2   _simplify_patterns_orr  r   )r/   r   r=  r>  leger^   r   r#  r   r@  r'   r     s   
zOr._eval_simplifyTc                    sb   t dtjd }fdd|D }t|}dd |D } fdd|D }tt|ddiS )NrN   c                 3   s    | ]	}t  j|V  qd S r+   )r   rn   )ri   jr6   r&   r'   r7  #      zOr.to_anf.<locals>.<genexpr>c                 s   s    | ]}t | V  qd S r+   r,   r(  r&   r&   r'   r7  %  r8  c                 3   s$    | ]} rt | d n|V  qdS )r   N)r   ri   r]   r   r&   r'   r7  &     " r   F)rangerQ   rn   r   from_iterabler<   r  )r/   r   rn   r&   )r   r/   r'   r   !  s   
z	Or.to_anfrt   )ru   rv   rw   rx   r   r   r   r2  r   r   r   rT   r<  r   r   r   r&   r&   r   r'   r2     s    
r2   c                   @   s<   e Zd ZdZdZedd Zdd ZdddZdd	d
Z	dS )r5   a  
    Logical Not function (negation)


    Returns ``true`` if the statement is ``false`` or ``False``.
    Returns ``false`` if the statement is ``true`` or ``True``.

    Examples
    ========

    >>> from sympy import Not, And, Or
    >>> from sympy.abc import x, A, B
    >>> Not(True)
    False
    >>> Not(False)
    True
    >>> Not(And(True, False))
    True
    >>> Not(Or(True, False))
    False
    >>> Not(And(And(True, x), Or(x, False)))
    ~x
    >>> ~x
    ~x
    >>> Not(And(Or(A, B), Or(~A, ~B)))
    ~((A | B) & (~A | ~B))

    Notes
    =====

    - The ``~`` operator is provided as a convenience, but note that its use
      here is different from its normal use in Python, which is bitwise
      not. In particular, ``~a`` and ``Not(a)`` will be different if ``a`` is
      an integer. Furthermore, since bools in Python subclass from ``int``,
      ``~True`` is the same as ``~1`` which is ``-2``, which has a boolean
      value of True.  To avoid this issue, use the SymPy boolean types
      ``true`` and ``false``.

    - As of Python 3.12, the bitwise not operator ``~`` used on a
      Python ``bool`` is deprecated and will emit a warning.

    >>> from sympy import true
    >>> ~True  # doctest: +SKIP
    -2
    >>> ~true
    False

    Tc                 C   s>   t |ts	|dv r|rtS tS |jr|jd S |jr|jS d S )NTFr   )r    r   r   r   is_Notrn   r   r   )r   r   r&   r&   r'   eval^  s   
zNot.evalc                 C   s   | j d  tjS )z
        Rewrite logic operators and relationals in terms of real sets.

        Examples
        ========

        >>> from sympy import Not, Symbol
        >>> x = Symbol('x')
        >>> Not(x > 0).as_set()
        Interval(-oo, 0)
        r   )rn   r[   
complementr   rW   r6   r&   r&   r'   rT   h  s   zNot._eval_as_setc                    sb  t | r| S | jd }|j|j}}|tkr#tjdd |D d|iS |tkr4tjdd |D d|iS |tkrF|\}}tj|t||dS |tkr[tjt| tdd |D  |dS |t	krg }t
dt|d d	D ]}t||D ]  fd
d|D }	|t|	  qrqktj|d|iS |tkr|\}}}
tjt|t|
tt|t||dS td| )Nr   c                 S   r   r&   r4   r(  r&   r&   r'   rk     r   zNot.to_nnf.<locals>.<listcomp>rJ   c                 S   r   r&   r4   r(  r&   r&   r'   rk     r   r   c                 S   r   r&   r4   r(  r&   r&   r'   rk     r   rN      c                        g | ]}| v rt |n|qS r&   r4   ri   r`   negr&   r'   rk          z!Illegal operator %s in expression)r   rn   rZ   r-   r2   r   r9   r5   rF   r<   rH  rQ   r   r   ITEr  )r/   rJ   exprrZ   rn   r   r  r1  rj   clauser   r&   rQ  r'   rK   v  s6   

(z
Not.to_nnfc                 C   s   t jt| jd |dS )Nr   r   )r<   r   r   rn   r   r&   r&   r'   r     s   z
Not.to_anfNrt   )
ru   rv   rw   rx   rK  r   rL  rT   rK   r   r&   r&   r&   r'   r5   *  s    1
	
$r5   c                       s`   e Zd ZdZdd fdd
Zeedd Zddd	Zd
d Z	dd Z
dd Zdd Z  ZS )r<   ac  
    Logical XOR (exclusive OR) function.


    Returns True if an odd number of the arguments are True and the rest are
    False.

    Returns False if an even number of the arguments are True and the rest are
    False.

    Examples
    ========

    >>> from sympy.logic.boolalg import Xor
    >>> from sympy import symbols
    >>> x, y = symbols('x y')
    >>> Xor(True, False)
    True
    >>> Xor(True, True)
    False
    >>> Xor(True, False, True, True, False)
    True
    >>> Xor(True, False, True, False)
    False
    >>> x ^ y
    x ^ y

    Notes
    =====

    The ``^`` operator is provided as a convenience, but note that its use
    here is different from its normal use in Python, which is bitwise xor. In
    particular, ``a ^ b`` and ``Xor(a, b)`` will be different if ``a`` and
    ``b`` are integers.

    >>> Xor(x, y).subs(y, 0)
    x

    T)r   c                   s  t  }t j| g|R i |}|jD ]:}t|ts|dv r$|r#t}nqt|tr>|jD ]}||v r7|	|n|
| q,q||v rH|	| q|
| qdd |D }d}	g }
t|D ]4\}\}}}t|d t|D ]}|| d d \}}||kr|	 }	 n||kr nqmq]|
||f q]|	rt|v r|	tn|
t |
D ]\}}|	| |	| qt|dkrtS t|dkr| S d|v r|r|	d tt| S tt||_t||_|S )	NrJ  c                 S   s"   g | ]}|j r||j|jjfqS r&   )r   r   r   r4  r&   r&   r'   rk     s    zXor.__new__.<locals>.<listcomp>FrN   rN  r   T)rl   r   __new___argsr    r   r   r<   rn   remover   	enumeraterH  rQ   r   r   rR   r5   tupler   	frozenset_argset)r   r   rn   r   r   objr   r   r   oddrY  rj   r_   r   r6  rD  rjcjr  r   r&   r'   rW    s^   





zXor.__new__c                 C      t t| jS r+   r[  r   r]  r6   r&   r&   r'   rn        zXor.argsc                    sd   g }t dt| jd dD ]}t| j|D ]  fdd| jD }|t|  qqtj|d|iS )Nr   rN   rN  c                    rO  r&   r4   rP  rQ  r&   r'   rk     rS  zXor.to_nnf.<locals>.<listcomp>rJ   )rH  rQ   rn   r   r   r2   r-   r   )r/   rJ   rn   rj   rV  r&   rQ  r'   rK     s   z
Xor.to_nnfc                    $    j }t fddtt|D  S )Nc                       g | ]}t | jqS r&   )_convert_to_varsSOPrn   rF  r6   r&   r'   rk         z+Xor._eval_rewrite_as_Or.<locals>.<listcomp>)rn   r2   _get_odd_parity_termsrQ   r/   rn   r   r   r&   r6   r'   _eval_rewrite_as_Or     
zXor._eval_rewrite_as_Orc                    re  )Nc                    rf  r&   )_convert_to_varsPOSrn   rF  r6   r&   r'   rk     rh  z,Xor._eval_rewrite_as_And.<locals>.<listcomp>)rn   r-   _get_even_parity_termsrQ   rj  r&   r6   r'   _eval_rewrite_as_And
  rl  zXor._eval_rewrite_as_Andc                    sH   | j  fdd| jD  }| }t|ts|S t }t|| d d S )Nc                    r   r   r   r   r   r&   r'   rk     r   z&Xor._eval_simplify.<locals>.<listcomp>r   )rZ   rn   r   r    r<   _simplify_patterns_xorr  )r/   r   r   r#  r&   r   r'   r     s   
zXor._eval_simplifyc                 C   sJ   t |tr!t|j}|| jr#t| j| }|| | j| S d S d S r+   )r    r<   rl   rn   r   r   rZ   )r/   r   ra   r   rn   r&   r&   r'   r     s   



zXor._eval_subsrt   )ru   rv   rw   rx   rW  r   r	   rn   rK   rk  ro  r   r   r   r&   r&   r   r'   r<     s    '2
r<   c                   @      e Zd ZdZedd ZdS )r;  a  
    Logical NAND function.

    It evaluates its arguments in order, giving True immediately if any
    of them are False, and False if they are all True.

    Returns True if any of the arguments are False
    Returns False if all arguments are True

    Examples
    ========

    >>> from sympy.logic.boolalg import Nand
    >>> from sympy import symbols
    >>> x, y = symbols('x y')
    >>> Nand(False, True)
    True
    >>> Nand(True, True)
    False
    >>> Nand(x, y)
    ~(x & y)

    c                 G      t t| S r+   )r5   r-   r   rn   r&   r&   r'   rL  >     z	Nand.evalNru   rv   rw   rx   r   rL  r&   r&   r&   r'   r;  &  s    r;  c                   @   rq  )r,  a*  
    Logical NOR function.

    It evaluates its arguments in order, giving False immediately if any
    of them are True, and True if they are all False.

    Returns False if any argument is True
    Returns True if all arguments are False

    Examples
    ========

    >>> from sympy.logic.boolalg import Nor
    >>> from sympy import symbols
    >>> x, y = symbols('x y')

    >>> Nor(True, False)
    False
    >>> Nor(True, True)
    False
    >>> Nor(False, True)
    False
    >>> Nor(False, False)
    True
    >>> Nor(x, y)
    ~(x | y)

    c                 G   rr  r+   )r5   r2   rs  r&   r&   r'   rL  `  rt  zNor.evalNru  r&   r&   r&   r'   r,  C  s    r,  c                   @   rq  )Xnora  
    Logical XNOR function.

    Returns False if an odd number of the arguments are True and the rest are
    False.

    Returns True if an even number of the arguments are True and the rest are
    False.

    Examples
    ========

    >>> from sympy.logic.boolalg import Xnor
    >>> from sympy import symbols
    >>> x, y = symbols('x y')
    >>> Xnor(True, False)
    False
    >>> Xnor(True, True)
    True
    >>> Xnor(True, False, True, True, False)
    False
    >>> Xnor(True, False, True, False)
    True

    c                 G   rr  r+   )r5   r<   rs  r&   r&   r'   rL    rt  z	Xnor.evalNru  r&   r&   r&   r'   rv  e  s    rv  c                   @   s0   e Zd ZdZedd Zd
ddZd
ddZd	S )r9   a   
    Logical implication.

    A implies B is equivalent to if A then B. Mathematically, it is written
    as `A \Rightarrow B` and is equivalent to `\neg A \vee B` or ``~A | B``.

    Accepts two Boolean arguments; A and B.
    Returns False if A is True and B is False
    Returns True otherwise.

    Examples
    ========

    >>> from sympy.logic.boolalg import Implies
    >>> from sympy import symbols
    >>> x, y = symbols('x y')

    >>> Implies(True, False)
    False
    >>> Implies(False, False)
    True
    >>> Implies(True, True)
    True
    >>> Implies(False, True)
    True
    >>> x >> y
    Implies(x, y)
    >>> y << x
    Implies(x, y)

    Notes
    =====

    The ``>>`` and ``<<`` operators are provided as a convenience, but note
    that their use here is different from their normal use in Python, which is
    bit shifts. Hence, ``Implies(a, b)`` and ``a >> b`` will return different
    things if ``a`` and ``b`` are integers.  In particular, since Python
    considers ``True`` and ``False`` to be integers, ``True >> True`` will be
    the same as ``1 >> 1``, i.e., 0, which has a truth value of False.  To
    avoid this issue, use the SymPy objects ``true`` and ``false``.

    >>> from sympy import true, false
    >>> True >> False
    1
    >>> true >> false
    False

    c                 G   s   z#g }|D ]}t |ts|dv r|t| q|| q|\}}W n ty6   tdt|t|f w |dv s?|dv rFtt||S ||krLt	S |j
re|j
re|j|jkrZt	S |jj|jkrc|S d S tj| g|R  S )Nr   rN   z:%d operand(s) used for an Implies (pairs are required): %srJ  )r    r   r   boolr  rQ   strr2   r5   r   r   r   r   r   rW  )r   rn   r   r]   ABr&   r&   r'   rL    s2   zImplies.evalTc                 C   s   | j \}}tjt|||dS Nr   )rn   r2   r   r5   )r/   rJ   r   r  r&   r&   r'   rK     s   
zImplies.to_nnfc                 C   s"   | j \}}tjt|t|||dS )Nr   )rn   r<   r   r   r-   r/   r   r   r  r&   r&   r'   r     s   
zImplies.to_anfNrt   )ru   rv   rw   rx   r   rL  rK   r   r&   r&   r&   r'   r9     s    0

r9   c                       sD   e Zd ZdZ fddZeedd ZdddZdd	d
Z	  Z
S )rF   a  
    Equivalence relation.

    ``Equivalent(A, B)`` is True iff A and B are both True or both False.

    Returns True if all of the arguments are logically equivalent.
    Returns False otherwise.

    For two arguments, this is equivalent to :py:class:`~.Xnor`.

    Examples
    ========

    >>> from sympy.logic.boolalg import Equivalent, And
    >>> from sympy.abc import x
    >>> Equivalent(False, False, False)
    True
    >>> Equivalent(True, False, False)
    False
    >>> Equivalent(x, And(x, True))
    True

    c                    s  ddl m} dd |D }t|}|D ]}t|ts|dv r*|| |t| qg }|D ]}t||rA|||j	|j
j	f q/g }t|D ]4\}	\}}
}t|	d t|D ]#}|| d d \}}||krnt    S ||
kr{|||f  nqXqH|D ]\}}|| || |d qt|dkrtS d|v r|d t| S d	|v r|d	 td
d |D  S t|}t | |}||_|S )Nr   r?   c                 S   r   r&   )r   r(  r&   r&   r'   rk     r   z&Equivalent.__new__.<locals>.<listcomp>rJ  rN   rN  TFc                 S   r   r&   r4   r(  r&   r&   r'   rk     r   )rB   r@   rl   r    r   discardr   rx  r   r   r   rZ  rH  rQ   r   rY  r   r-   r\  r   rW  r]  )r   rn   optionsr@   r   r]   r   r_   rY  rj   r   r6  rD  r`  ra  r   r  rX  r^  r   r&   r'   rW    sP   





zEquivalent.__new__c                 C   rb  r+   rc  r6   r&   r&   r'   rn     rd  zEquivalent.argsTc                 C   sh   g }t | j| jdd  D ]\}}|tt|| q|tt| jd | jd  tj|d|iS )NrN   r   rJ   )ziprn   r   r2   r5   r-   r   )r/   rJ   rn   r   r  r&   r&   r'   rK     s
    zEquivalent.to_nnfc                 C   s6   t | j }t dd | jD  }t|}tj|||dS )Nc                 S   s   g | ]
}t t|d dqS )Fr   )r   r5   r(  r&   r&   r'   rk   '  r   z%Equivalent.to_anf.<locals>.<listcomp>r   )r-   rn   r0  r<   r   r}  r&   r&   r'   r   %  s   
zEquivalent.to_anfrt   )ru   rv   rw   rx   rW  r   r	   rn   rK   r   r   r&   r&   r   r'   rF     s    )
rF   c                   @   s>   e Zd ZdZdd Zedd ZdddZd	d
 Zdd Z	dS )rT  aC  
    If-then-else clause.

    ``ITE(A, B, C)`` evaluates and returns the result of B if A is true
    else it returns the result of C. All args must be Booleans.

    From a logic gate perspective, ITE corresponds to a 2-to-1 multiplexer,
    where A is the select signal.

    Examples
    ========

    >>> from sympy.logic.boolalg import ITE, And, Xor, Or
    >>> from sympy.abc import x, y, z
    >>> ITE(True, False, True)
    False
    >>> ITE(Or(True, False), And(True, True), Xor(True, True))
    True
    >>> ITE(x, y, z)
    ITE(x, y, z)
    >>> ITE(True, x, y)
    x
    >>> ITE(False, x, y)
    y
    >>> ITE(x, y, y)
    y

    Trying to use non-Boolean args will generate a TypeError:

    >>> ITE(True, [], ())
    Traceback (most recent call last):
    ...
    TypeError: expecting bool, Boolean or ITE, not `[]`

    c                 O   s<  ddl m}m} t|dkrtd|\}}}t|||frutt||f\}}t j	dd ||fD  }tt|j
| dkrt|}	|jtu rJ|j}n!|jtu rS|j}n|jtu r^t|j}n|jtu rit|j}nt}t|	|rtt|}n
t|||\}}}d }
|dd	r| |||}
|
d u rtj| |||d
d}
|
S )Nr   rb      zexpecting exactly 3 argsc                 S   r  r&   )rg   rh   r&   r&   r'   rk   [  r  zITE.__new__.<locals>.<listcomp>rN   r   TFr  )rB   rc   rd   rQ   r  r    mapr(   rl   rm   rn   r  r   r  r   r5   r   r   r   rL  rW  )r   rn   r   rc   rd   r   r  r   bin_syms_ar   r&   r&   r'   rW  P  s<   






zITE.__new__c                 G   s  ddl m}m} |\}}}t|||frK|}t|jv r'|jtu r#|jn|j}nt|jv r<|jtu r6t	|jnt	|j}nd }|d urKt||rKt	|}|tu rQ|S |tu rW|S ||kr]|S |tu rg|tu rg|S |tu rs|tu rst	|S |||g|kr| |||ddS d S )Nr   rb   Fr  )
rB   rc   rd   r    r   rn   r  r  r   r5   )r   rn   rc   rd   r   r  r   r  r&   r&   r'   rL  v  s0   


 zITE.evalTc                 C   s,   | j \}}}tjtt||t|||dS r|  )rn   r-   r   r2   r5   )r/   rJ   r   r  r   r&   r&   r'   rK     s    z
ITE.to_nnfc                 C   s   |    S r+   )rK   r[   r6   r&   r&   r'   rT     r   zITE._eval_as_setc                 O   s*   ddl m} ||d |d f|d dfS )Nr   r   rN   rN  T)r   r   )r/   rn   r   r   r&   r&   r'   _eval_rewrite_as_Piecewise  s   zITE._eval_rewrite_as_PiecewiseNrt   )
ru   rv   rw   rx   rW  r   rL  rK   rT   r  r&   r&   r&   r'   rT  ,  s    #&

rT  c                   @   rq  )	Exclusivea  
    True if only one or no argument is true.

    ``Exclusive(A, B, C)`` is equivalent to ``~(A & B) & ~(A & C) & ~(B & C)``.

    For two arguments, this is equivalent to :py:class:`~.Xor`.

    Examples
    ========

    >>> from sympy.logic.boolalg import Exclusive
    >>> Exclusive(False, False, False)
    True
    >>> Exclusive(False, True, False)
    True
    >>> Exclusive(False, True, True)
    False

    c                 G   s4   g }t |dD ]\}}|tt|| qt| S )NrN  )r   r   r5   r-   )r   rn   and_argsr   r  r&   r&   r'   rL    s   zExclusive.evalNru  r&   r&   r&   r'   r    s    r  c                 C   
   t | S )a  Return a list of the conjuncts in ``expr``.

    Examples
    ========

    >>> from sympy.logic.boolalg import conjuncts
    >>> from sympy.abc import A, B
    >>> conjuncts(A & B)
    frozenset({A, B})
    >>> conjuncts(A | B)
    frozenset({A | B})

    )r-   	make_argsrU  r&   r&   r'   	conjuncts     
r  c                 C   r  )a  Return a list of the disjuncts in ``expr``.

    Examples
    ========

    >>> from sympy.logic.boolalg import disjuncts
    >>> from sympy.abc import A, B
    >>> disjuncts(A | B)
    frozenset({A, B})
    >>> disjuncts(A & B)
    frozenset({A & B})

    r2   r  r  r&   r&   r'   	disjuncts  r  r  c                 C      t | ttfS )ai  
    Given a sentence ``expr`` consisting of conjunctions and disjunctions
    of literals, return an equivalent sentence in CNF.

    Examples
    ========

    >>> from sympy.logic.boolalg import distribute_and_over_or, And, Or, Not
    >>> from sympy.abc import A, B, C
    >>> distribute_and_over_or(Or(A, And(Not(B), Not(C))))
    (A | ~B) & (A | ~C)

    )_distributer-   r2   r  r&   r&   r'   distribute_and_over_or  s   r  c                 C   r  )a  
    Given a sentence ``expr`` consisting of conjunctions and disjunctions
    of literals, return an equivalent sentence in DNF.

    Note that the output is NOT simplified.

    Examples
    ========

    >>> from sympy.logic.boolalg import distribute_or_over_and, And, Or, Not
    >>> from sympy.abc import A, B, C
    >>> distribute_or_over_and(And(Or(Not(A), B), C))
    (B & C) | (C & ~A)

    )r  r2   r-   r  r&   r&   r'   distribute_or_over_and     r  c                 C   r  )a  
    Given a sentence ``expr`` consisting of conjunction and
    exclusive disjunctions of literals, return an
    equivalent exclusive disjunction.

    Note that the output is NOT simplified.

    Examples
    ========

    >>> from sympy.logic.boolalg import distribute_xor_over_and, And, Xor, Not
    >>> from sympy.abc import A, B, C
    >>> distribute_xor_over_and(And(Xor(Not(A), B), C))
    (B & C) ^ (C & ~A)
    )r  r<   r-   r  r&   r&   r'   r0    r  r0  c                    s   t d d rGd jD ]}t |d r|  nqd S d  fddd jD  d tttfdd jD ddiS t d d rhd tttfd	dd jD ddiS d S )
zO
    Distributes ``info[1]`` over ``info[2]`` with respect to ``info[0]``.
    r   rN  rN   c                    s   g | ]}| ur|qS r&   r&   r   )conjr&   r'   rk   $      z_distribute.<locals>.<listcomp>c                    s(   g | ]} d  | d  d  fqS rN  rN   r&   ri   r   )inforestr&   r'   rk   &  s     r   Fc                    s   g | ]}| d   d fqS )rN   rN  r&   rF  )r  r&   r'   rk   *  s    )r    rn   r  r  r  )r  r   r&   )r  r  r  r'   r    s0    
r  Tc                 C   s    t | } t| r
| S | j|dS )a\  
    Converts expr to Algebraic Normal Form (ANF).

    ANF is a canonical normal form, which means that two
    equivalent formulas will convert to the same ANF.

    A logical expression is in ANF if it has the form

    .. math:: 1 \oplus a \oplus b \oplus ab \oplus abc

    i.e. it can be:
        - purely true,
        - purely false,
        - conjunction of variables,
        - exclusive disjunction.

    The exclusive disjunction can only contain true, variables
    or conjunction of variables. No negations are permitted.

    If ``deep`` is ``False``, arguments of the boolean
    expression are considered variables, i.e. only the
    top-level expression is converted to ANF.

    Examples
    ========
    >>> from sympy.logic.boolalg import And, Or, Not, Implies, Equivalent
    >>> from sympy.logic.boolalg import to_anf
    >>> from sympy.abc import A, B, C
    >>> to_anf(Not(A))
    A ^ True
    >>> to_anf(And(Or(A, B), Not(C)))
    A ^ B ^ (A & B) ^ (A & C) ^ (B & C) ^ (A & B & C)
    >>> to_anf(Implies(Not(A), Equivalent(B, C)), deep=False)
    True ^ ~A ^ (~A & (Equivalent(B, C)))

    r   )r   is_anfr   )rU  r   r&   r&   r'   r   1  s   %r   c                 C   s   t | |r| S | |S )aS  
    Converts ``expr`` to Negation Normal Form (NNF).

    A logical expression is in NNF if it
    contains only :py:class:`~.And`, :py:class:`~.Or` and :py:class:`~.Not`,
    and :py:class:`~.Not` is applied only to literals.
    If ``simplify`` is ``True``, the result contains no redundant clauses.

    Examples
    ========

    >>> from sympy.abc import A, B, C, D
    >>> from sympy.logic.boolalg import Not, Equivalent, to_nnf
    >>> to_nnf(Not((~A & ~B) | (C & D)))
    (A | B) & (~C | ~D)
    >>> to_nnf(Equivalent(A >> B, B >> A))
    (A | ~B | (A & ~B)) & (B | ~A | (B & ~A))

    )is_nnfrK   )rU  rJ   r&   r&   r'   rK   ]  s   

rK   Fc                 C   sj   t | } t| ts| S |r%|stt| dkrttdt| dd|dS t| r+| S t	| } t
| }|S )ao  
    Convert a propositional logical sentence ``expr`` to conjunctive normal
    form: ``((A | ~B | ...) & (B | C | ...) & ...)``.
    If ``simplify`` is ``True``, ``expr`` is evaluated to its simplest CNF
    form using the Quine-McCluskey algorithm; this may take a long
    time. If there are more than 8 variables the ``force`` flag must be set
    to ``True`` to simplify (default is ``False``).

    Examples
    ========

    >>> from sympy.logic.boolalg import to_cnf
    >>> from sympy.abc import A, B, D
    >>> to_cnf(~(A | B) | D)
    (D | ~A) & (D | ~B)
    >>> to_cnf((A | B) & (A | ~A), True)
    A | B

       
            To simplify a logical expression with more
            than 8 variables may take a long time and requires
            the use of `force=True`.cnfTforce)r   r    r   rQ   _find_predicatesr  r   r   is_cnfeliminate_implicationsr  )rU  rJ   r  resr&   r&   r'   to_cnfv  s   
r  c                 C   sf   t | } t| ts| S |r%|stt| dkrttdt| dd|dS t| r+| S t	| } t
| S )a  
    Convert a propositional logical sentence ``expr`` to disjunctive normal
    form: ``((A & ~B & ...) | (B & C & ...) | ...)``.
    If ``simplify`` is ``True``, ``expr`` is evaluated to its simplest DNF form using
    the Quine-McCluskey algorithm; this may take a long
    time. If there are more than 8 variables, the ``force`` flag must be set to
    ``True`` to simplify (default is ``False``).

    Examples
    ========

    >>> from sympy.logic.boolalg import to_dnf
    >>> from sympy.abc import A, B, C
    >>> to_dnf(B & (A | C))
    (A & B) | (B & C)
    >>> to_dnf((A & B) | (A & ~B) | (B & C) | (~B & C), True)
    A | C

    r  r  dnfTr  )r   r    r   rQ   r  r  r   r   is_dnfr  r  )rU  rJ   r  r&   r&   r'   to_dnf  s   
r  c                 C   s   t | } t| rt| tsdS t| tr"| jD ]}|js dS qdS t| trQ| jD ]$}t|tr?|jD ]	}|js=  dS q4q*t|rLt|trK dS q* dS dS dS )a  
    Checks if ``expr``  is in Algebraic Normal Form (ANF).

    A logical expression is in ANF if it has the form

    .. math:: 1 \oplus a \oplus b \oplus ab \oplus abc

    i.e. it is purely true, purely false, conjunction of
    variables or exclusive disjunction. The exclusive
    disjunction can only contain true, variables or
    conjunction of variables. No negations are permitted.

    Examples
    ========

    >>> from sympy.logic.boolalg import And, Not, Xor, true, is_anf
    >>> from sympy.abc import A, B, C
    >>> is_anf(true)
    True
    >>> is_anf(A)
    True
    >>> is_anf(And(A, B, C))
    True
    >>> is_anf(Xor(A, Not(B)))
    False

    TF)r   r   r    r5   r-   rn   rf   r<   )rU  r   r   r&   r&   r'   r    s0   






r  c                 C   s|   t | } t| r
dS | g}|r<| } | jttfv r4|r-| j}|D ]}t||v r, dS q!|| j nt| s:dS |sdS )a  
    Checks if ``expr`` is in Negation Normal Form (NNF).

    A logical expression is in NNF if it
    contains only :py:class:`~.And`, :py:class:`~.Or` and :py:class:`~.Not`,
    and :py:class:`~.Not` is applied only to literals.
    If ``simplified`` is ``True``, checks if result contains no redundant clauses.

    Examples
    ========

    >>> from sympy.abc import A, B, C
    >>> from sympy.logic.boolalg import Not, is_nnf
    >>> is_nnf(A & B | ~C)
    True
    >>> is_nnf((A | ~A) & (B | C))
    False
    >>> is_nnf((A | ~A) & (B | C), False)
    True
    >>> is_nnf(Not(A & B) | C)
    False
    >>> is_nnf((A >> B) & (B >> A))
    False

    TF)	r   r   rR   rZ   r-   r2   rn   r5   r  )rU  
simplifiedstackrn   r   r&   r&   r'   r     s$   r  c                 C      t | ttS )a(  
    Test whether or not an expression is in conjunctive normal form.

    Examples
    ========

    >>> from sympy.logic.boolalg import is_cnf
    >>> from sympy.abc import A, B, C
    >>> is_cnf(A | B | C)
    True
    >>> is_cnf(A & B & C)
    True
    >>> is_cnf((A & B) | C)
    False

    )_is_formr-   r2   r  r&   r&   r'   r  1  s   r  c                 C   r  )aM  
    Test whether or not an expression is in disjunctive normal form.

    Examples
    ========

    >>> from sympy.logic.boolalg import is_dnf
    >>> from sympy.abc import A, B, C
    >>> is_dnf(A | B | C)
    True
    >>> is_dnf(A & B & C)
    True
    >>> is_dnf((A & B) | C)
    True
    >>> is_dnf(A & (B | C))
    False

    )r  r2   r-   r  r&   r&   r'   r  E  s   r  c                 C   s   t | } t| |r|| n| g}|D ]-}t||r7t||r$||n|g}|D ]}t|du r5  dS q)qt|du r@ dS qdS )zE
    Test whether or not an expression is of the required form.

    FT)r   r    r  r   )rU  	function1	function2valslitvals2lr&   r&   r'   r  [  s   
r  c                 C   s   t | ddS )a  
    Change :py:class:`~.Implies` and :py:class:`~.Equivalent` into
    :py:class:`~.And`, :py:class:`~.Or`, and :py:class:`~.Not`.
    That is, return an expression that is equivalent to ``expr``, but has only
    ``&``, ``|``, and ``~`` as logical
    operators.

    Examples
    ========

    >>> from sympy.logic.boolalg import Implies, Equivalent,          eliminate_implications
    >>> from sympy.abc import A, B, C
    >>> eliminate_implications(Implies(A, B))
    B | ~A
    >>> eliminate_implications(Equivalent(A, B))
    (A | ~B) & (B | ~A)
    >>> eliminate_implications(Equivalent(A, B, C))
    (A | ~C) & (B | ~A) & (C | ~B)

    Fr   )rK   r  r&   r&   r'   r  o  s   r  c                    sl   ddl m  ttrtjd S dv st sjr dS tts4t fddjD r4dS dS )a{  
    Returns True if expr is a literal, else False.

    Examples
    ========

    >>> from sympy import Or, Q
    >>> from sympy.abc import A, B
    >>> from sympy.logic.boolalg import is_literal
    >>> is_literal(A)
    True
    >>> is_literal(~A)
    True
    >>> is_literal(Q.zero(A))
    True
    >>> is_literal(A + B)
    True
    >>> is_literal(Or(A, B))
    False

    r   )AppliedPredicaterJ  Tc                 3   s     | ]}t  p|jV  qd S r+   )r    r   r   r  rU  r&   r'   r7    s    
zis_literal.<locals>.<genexpr>F)	rp   r  r    r5   r   rn   r   r   allr  r&   r  r'   r     s   
r   c                    s8   t ttdtd dd   fdd| D S )a	  
    Takes clauses in CNF format and puts them into an integer representation.

    Examples
    ========

    >>> from sympy.logic.boolalg import to_int_repr
    >>> from sympy.abc import x, y
    >>> to_int_repr([x | y, y], [x, y]) == [{1, 2}, {2}]
    True

    rN   c                 S   s"   t | tr|| jd   S ||  S r  )r    r5   rn   )r   r   r&   r&   r'   append_symbol  s   
z"to_int_repr.<locals>.append_symbolc                    s&   g | ]} fd dt |D qS )c                    s   h | ]} |qS r&   r&   r(  r  r   r&   r'   	<setcomp>  r
  z)to_int_repr.<locals>.<listcomp>.<setcomp>r  r  r  r&   r'   rk     s    zto_int_repr.<locals>.<listcomp>)dictr  rH  rQ   )clausesr   r&   r  r'   to_int_repr  s
   r  c                 C   s   t dtttt| dS )a@  
    Return an integer corresponding to the base-2 digits given by *term*.

    Parameters
    ==========

    term : a string or list of ones and zeros

    Examples
    ========

    >>> from sympy.logic.boolalg import term_to_integer
    >>> term_to_integer([1, 0, 0])
    4
    >>> term_to_integer('100')
    4

     rN  )intjoinr  r  ry  )termr&   r&   r'   term_to_integer  s   r  c                 c   sz    dd |D }t | } t| tst| sdS tdt|d}|D ]}| tt||}|r7t	||fV  q!|V  q!dS )a  
    Return a generator of all possible configurations of the input variables,
    and the result of the boolean expression for those values.

    Parameters
    ==========

    expr : Boolean expression

    variables : list of variables

    input : bool (default ``True``)
        Indicates whether to return the input combinations.

    Examples
    ========

    >>> from sympy.logic.boolalg import truth_table
    >>> from sympy.abc import x,y
    >>> table = truth_table(x >> y, [x, y])
    >>> for t in table:
    ...     print('{0} -> {1}'.format(*t))
    [0, 0] -> True
    [0, 1] -> True
    [1, 0] -> False
    [1, 1] -> True

    >>> table = truth_table(x | y, [x, y])
    >>> list(table)
    [([0, 0], False), ([0, 1], True), ([1, 0], True), ([1, 1], True)]

    If ``input`` is ``False``, ``truth_table`` returns only a list of truth values.
    In this case, the corresponding input values of variables can be
    deduced from the index of a given output.

    >>> from sympy.utilities.iterables import ibin
    >>> vars = [y, x]
    >>> values = truth_table(x >> y, vars, input=False)
    >>> values = list(values)
    >>> values
    [True, False, True, True]

    >>> for i, value in enumerate(values):
    ...     print('{0} -> {1}'.format(list(zip(
    ...     vars, ibin(i, len(vars)))), value))
    [(y, 0), (x, 0)] -> True
    [(y, 0), (x, 1)] -> False
    [(y, 1), (x, 0)] -> True
    [(y, 1), (x, 1)] -> True

    c                 S   r   r&   )r   ri   vr&   r&   r'   rk     r   ztruth_table.<locals>.<listcomp>Nrw  repeat)
r   r    r   r   r   rQ   r  r  r  r  )rU  	variablesinputtabler  valuer&   r&   r'   truth_table  s   4r  c                 C   s:   d}t | D ]\}}||| kr|dkr|}q dS q|S )zm
    Checks if a pair of minterms differs by only one bit. If yes, returns
    index, else returns `-1`.
    r  )rZ  )minterm1minterm2indexr]   rj   r&   r&   r'   _check_pair#  s   r  c                        fddt | D }t| S )zh
    Converts a term in the expansion of a function from binary to its
    variable form (for SOP).
    c                    4   g | ]\}}|d kr|dkr | nt  | qS )r  rN   r4   ri   nvalr  r&   r'   rk   9      z'_convert_to_varsSOP.<locals>.<listcomp>)rZ  r-   )mintermr  tempr&   r  r'   rg  4     
rg  c                    r  )zh
    Converts a term in the expansion of a function from binary to its
    variable form (for POS).
    c                    r  )r  r   r4   r  r  r&   r'   rk   C  r  z'_convert_to_varsPOS.<locals>.<listcomp>)rZ  r2   )maxtermr  r  r&   r  r'   rm  >  r  rm  c                    s&    fddt | D }|stS t| S )z
    Converts a term in the expansion of a function from binary to its
    variable form (for ANF).

    Parameters
    ==========

    term : list of 1's and 0's (complementation pattern)
    variables : list of variables

    c                    s    g | ]\}}|d kr | qS )rN   r&   )ri   r  tr  r&   r'   rk   T  rS  z'_convert_to_varsANF.<locals>.<listcomp>)rZ  r   r-   )r  r  r  r&   r  r'   _convert_to_varsANFH  s   r  c                    $   dd  fddt d  D D S )zu
    Returns a list of lists, with all possible combinations of n zeros and ones
    with an odd number of ones.
    c                 S       g | ]}t |d  dkr|qS r  sumri   r$   r&   r&   r'   rk   a  rS  z)_get_odd_parity_terms.<locals>.<listcomp>c                       g | ]}t | qS r&   r   rh   r  r&   r'   rk   a  r
  rN  rH  r  r&   r  r'   ri  \     $ri  c                    r  )zv
    Returns a list of lists, with all possible combinations of n zeros and ones
    with an even number of ones.
    c                 S   r  )rN  r   r  r  r&   r&   r'   rk   i  rS  z*_get_even_parity_terms.<locals>.<listcomp>c                    r  r&   r  rh   r  r&   r'   rk   i  r
  rN  r  r  r&   r  r'   rn  d  r  rn  c                    s   sg S g }t tt }tt }t D ]\}}tdd |D }|| | qt d }t|D ]<}|| D ]5}	||d  D ],}
t |	  |
 }|dkrod ||	< ||
<  |	 dd }d||< ||vro|| qCq;q5|rxt|}|	 fdd	|D  |S )
z
    Reduces a set of minterms, if possible, to a simplified set of minterms
    with one less variable in the terms using QM method.
    c                 s   s    | ]	}|d krd V  qdS )rN   Nr&   ri   r  r&   r&   r'   r7  {  rE  z$_simplified_pairs.<locals>.<genexpr>r   rN   r  Nr  c                    s   g | ]
}|d ur | qS r+   r&   rh   termsr&   r'   rk     r   z%_simplified_pairs.<locals>.<listcomp>)
r  rH  rQ   r   rZ  r  r   r  _simplified_pairsr  )r  simplified_termstodotermdictr  r  onesr  r"  rj   rD  r  newtermr&   r  r'   r  l  s4   
r  c                    sj  |sg S t |}t fddt|D dg dg| }tD ]/\}}t|D ]&\}}tdd t||D rSd| |< |  d7  < ||  d7  < q-q%d}|rd}t|D ]R}	||	 r|	 t|D ]C}
|	|
kr||	 r||	 ||
 kr|
 	t	fd	dtD rd||
< d}t	D ]\}}|rd|
 |< |  d8  < qqnq`i }tD ] rC|v r|  nfd
dt|D   |< tD ]ekrB rB  krB|v r| nfddt|D |< t fddt|D rBd< d}tD ]\}}|r@|  r@d| < ||  d8  < q$qq|sd}d}tD ] }||kr^}|}qO|dkr|dkrtD ]8\}}||krt|| D ]'\}}|r| | rd| |< d}||  d8  < |  d8  < q}qn|sZfddtD S )z
    After the truth table has been sufficiently simplified, use the prime
    implicant table method to recognize and eliminate redundant pairs,
    and return the essential arguments.
    c                    s   g | ]}d g  qS )r   r&   ri   r  )nl1r&   r'   rk     r
  z#_rem_redundancy.<locals>.<listcomp>r   c                 s   s$    | ]\}}|d kp||kV  qdS )r  Nr&   )ri   r  mtr&   r&   r'   r7    rG  z"_rem_redundancy.<locals>.<genexpr>rN   TFc                 3   s     | ]}|  | kV  qd S r+   r&   r  )rowrow2r&   r'   r7        c                       g | ]}|   qS r&   r&   rh   )coli	dommatrixr&   r'   rk     r  c                    r  r&   r&   rh   )col2ir  r&   r'   rk     r  c                 3   s     | ]} | | kV  qd S r+   r&   r  )colcol2r&   r'   r7    r  r  c                    s   g | ]
} | r| qS r&   r&   rh   )colcountl1r&   r'   rk     r   )rQ   rH  rZ  r  r  )r  r  ntermsrowcountprimeiprimetermir  anythingchangedrowirow2icolcachemaxterms
bestcolidxr`   r&   )
r  r  r  r  r  r  r  r  r  r  r'   _rem_redundancy  s   

 

&


Gr  c                    s   g }t |}| D ]i}t|tr|t|| qt|trQt|}| D ]}|| q%t	dt |dD ]}tt
||  | | fdd|D  q5qt|ttfrnt ||krftdj|d|t| qtd|S )Nrw  r  c                    s   g | ]} | qS r&   r&   r  dr&   r'   rk   
	  r   z%_input_to_binlist.<locals>.<listcomp>zTEach term must contain {bits} bits as there are
{bits} variables (or be an integer).)bitsz2A term list can only contain lists, ints or dicts.)rQ   r    r  r   r   r  r  keysrY  r   r  updater[  r  formatr#   )	inputlistr  binlistr  r  nonspecvarskeyr  r&   r	  r'   _input_to_binlist  s,   


r  Nc                 C   sX   |st S ttt| } t|| }t|pg | }|D ]}||v r%td| qt| ||S )a  
    The SOPform function uses simplified_pairs and a redundant group-
    eliminating algorithm to convert the list of all input combos that
    generate '1' (the minterms) into the smallest sum-of-products form.

    The variables must be given as the first argument.

    Return a logical :py:class:`~.Or` function (i.e., the "sum of products" or
    "SOP" form) that gives the desired outcome. If there are inputs that can
    be ignored, pass them as a list, too.

    The result will be one of the (perhaps many) functions that satisfy
    the conditions.

    Examples
    ========

    >>> from sympy.logic import SOPform
    >>> from sympy import symbols
    >>> w, x, y, z = symbols('w x y z')
    >>> minterms = [[0, 0, 0, 1], [0, 0, 1, 1],
    ...             [0, 1, 1, 1], [1, 0, 1, 1], [1, 1, 1, 1]]
    >>> dontcares = [[0, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 1]]
    >>> SOPform([w, x, y, z], minterms, dontcares)
    (y & z) | (~w & ~x)

    The terms can also be represented as integers:

    >>> minterms = [1, 3, 7, 11, 15]
    >>> dontcares = [0, 2, 5]
    >>> SOPform([w, x, y, z], minterms, dontcares)
    (y & z) | (~w & ~x)

    They can also be specified using dicts, which does not have to be fully
    specified:

    >>> minterms = [{w: 0, x: 1}, {y: 1, z: 1, x: 0}]
    >>> SOPform([w, x, y, z], minterms)
    (x & ~w) | (y & z & ~x)

    Or a combination:

    >>> minterms = [4, 7, 11, [1, 1, 1, 1]]
    >>> dontcares = [{w : 0, x : 0, y: 0}, 5]
    >>> SOPform([w, x, y, z], minterms, dontcares)
    (w & y & z) | (~w & ~y) | (x & z & ~w)

    See also
    ========

    POSform

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Quine-McCluskey_algorithm
    .. [2] https://en.wikipedia.org/wiki/Don%27t-care_term

    #%s in minterms is also in dontcares)r   r[  r  r   r  r  	_sop_form)r  minterms	dontcaresr
  r&   r&   r'   SOPform	  s   <
r  c                    s,   t || }t||}t fdd|D  S )Nc                    r  r&   )rg  rF  r  r&   r'   rk   e	  r
  z_sop_form.<locals>.<listcomp>)r  r  r2   )r  r  r  ra   	essentialr&   r  r'   r  b	  s   
r  c                    s   |st S ttt  t| }t|pg  }|D ]}||v r%td| qg }tdt dD ]}t|}||vrC||vrC|	| q0t
|| }t||}t fdd|D  S )a  
    The POSform function uses simplified_pairs and a redundant-group
    eliminating algorithm to convert the list of all input combinations
    that generate '1' (the minterms) into the smallest product-of-sums form.

    The variables must be given as the first argument.

    Return a logical :py:class:`~.And` function (i.e., the "product of sums"
    or "POS" form) that gives the desired outcome. If there are inputs that can
    be ignored, pass them as a list, too.

    The result will be one of the (perhaps many) functions that satisfy
    the conditions.

    Examples
    ========

    >>> from sympy.logic import POSform
    >>> from sympy import symbols
    >>> w, x, y, z = symbols('w x y z')
    >>> minterms = [[0, 0, 0, 1], [0, 0, 1, 1], [0, 1, 1, 1],
    ...             [1, 0, 1, 1], [1, 1, 1, 1]]
    >>> dontcares = [[0, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 1]]
    >>> POSform([w, x, y, z], minterms, dontcares)
    z & (y | ~w)

    The terms can also be represented as integers:

    >>> minterms = [1, 3, 7, 11, 15]
    >>> dontcares = [0, 2, 5]
    >>> POSform([w, x, y, z], minterms, dontcares)
    z & (y | ~w)

    They can also be specified using dicts, which does not have to be fully
    specified:

    >>> minterms = [{w: 0, x: 1}, {y: 1, z: 1, x: 0}]
    >>> POSform([w, x, y, z], minterms)
    (x | y) & (x | z) & (~w | ~x)

    Or a combination:

    >>> minterms = [4, 7, 11, [1, 1, 1, 1]]
    >>> dontcares = [{w : 0, x : 0, y: 0}, 5]
    >>> POSform([w, x, y, z], minterms, dontcares)
    (w | x) & (y | ~w) & (z | ~y)

    See also
    ========

    SOPform

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Quine-McCluskey_algorithm
    .. [2] https://en.wikipedia.org/wiki/Don%27t-care_term

    r  rw  r  c                    r  r&   )rm  rF  r  r&   r'   rk   	  r
  zPOSform.<locals>.<listcomp>)r   r[  r  r   r  r  r   rQ   r  r   r  r  r-   )r  r  r  r
  r  r  ra   r  r&   r  r'   POSformh	  s$   <


r  c                    s   t  }t |}|d| krtd||f ttt  t|}g }ttd|dD ]\}}|| dkr:|| q+t	 fdd|D dd	iS )
a  
    The ANFform function converts the list of truth values to
    Algebraic Normal Form (ANF).

    The variables must be given as the first argument.

    Return True, False, logical :py:class:`~.And` function (i.e., the
    "Zhegalkin monomial") or logical :py:class:`~.Xor` function (i.e.,
    the "Zhegalkin polynomial"). When True and False
    are represented by 1 and 0, respectively, then
    :py:class:`~.And` is multiplication and :py:class:`~.Xor` is addition.

    Formally a "Zhegalkin monomial" is the product (logical
    And) of a finite set of distinct variables, including
    the empty set whose product is denoted 1 (True).
    A "Zhegalkin polynomial" is the sum (logical Xor) of a
    set of Zhegalkin monomials, with the empty set denoted
    by 0 (False).

    Parameters
    ==========

    variables : list of variables
    truthvalues : list of 1's and 0's (result column of truth table)

    Examples
    ========
    >>> from sympy.logic.boolalg import ANFform
    >>> from sympy.abc import x, y
    >>> ANFform([x], [1, 0])
    x ^ True
    >>> ANFform([x, y], [0, 1, 1, 1])
    x ^ y ^ (x & y)

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Zhegalkin_polynomial

    rN  z8The number of truth values must be equal to 2^%d, got %drw  r  rN   c                    r  r&   )r  rF  r  r&   r'   rk   	  r
  zANFform.<locals>.<listcomp>r   F)
rQ   r  r[  r  r   
anf_coeffsrZ  r   r   r<   )r  truthvaluesn_varsn_valuescoeffsr  rj   r  r&   r  r'   ANFform	  s    *
r   c                 C   s   d t| }t|d }t| d| krtdt|  dd | D }t|D ]0}g }td|| d  D ]}||d|  ttdd |d|  |d| d    q6|}q(|d	 S )
a  
    Convert a list of truth values of some boolean expression
    to the list of coefficients of the polynomial mod 2 (exclusive
    disjunction) representing the boolean expression in ANF
    (i.e., the "Zhegalkin polynomial").

    There are `2^n` possible Zhegalkin monomials in `n` variables, since
    each monomial is fully specified by the presence or absence of
    each variable.

    We can enumerate all the monomials. For example, boolean
    function with four variables ``(a, b, c, d)`` can contain
    up to `2^4 = 16` monomials. The 13-th monomial is the
    product ``a & b & d``, because 13 in binary is 1, 1, 0, 1.

    A given monomial's presence or absence in a polynomial corresponds
    to that monomial's coefficient being 1 or 0 respectively.

    Examples
    ========
    >>> from sympy.logic.boolalg import anf_coeffs, bool_monomial, Xor
    >>> from sympy.abc import a, b, c
    >>> truthvalues = [0, 1, 1, 0, 0, 1, 0, 1]
    >>> coeffs = anf_coeffs(truthvalues)
    >>> coeffs
    [0, 1, 1, 0, 0, 0, 1, 0]
    >>> polynomial = Xor(*[
    ...     bool_monomial(k, [a, b, c])
    ...     for k, coeff in enumerate(coeffs) if coeff == 1
    ... ])
    >>> polynomial
    b ^ c ^ (a & b)

    z{:b}rN   rN  z9The number of truth values must be a power of two, got %dc                 S   s   g | ]}|gqS r&   r&   r  r&   r&   r'   rk   "
  r  zanf_coeffs.<locals>.<listcomp>c                 S   s   | |A S r+   r&   )r]   yr&   r&   r'   r   (
      zanf_coeffs.<locals>.<lambda>r   )r  rQ   r  rH  r   r  r  )r  r`   r  r  rj   tmprD  r&   r&   r'   r  	  s   $&r  c                 C   0   t | trt| t|} ttt|}t| |S )a  
    Return the k-th minterm.

    Minterms are numbered by a binary encoding of the complementation
    pattern of the variables. This convention assigns the value 1 to
    the direct form and 0 to the complemented form.

    Parameters
    ==========

    k : int or list of 1's and 0's (complementation pattern)
    variables : list of variables

    Examples
    ========

    >>> from sympy.logic.boolalg import bool_minterm
    >>> from sympy.abc import x, y, z
    >>> bool_minterm([1, 0, 1], [x, y, z])
    x & z & ~y
    >>> bool_minterm(6, [x, y, z])
    x & y & ~z

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Canonical_normal_form#Indexing_minterms

    )r    r  r   rQ   r[  r  r   rg  r"  r  r&   r&   r'   bool_minterm.
     

r&  c                 C   r$  )a  
    Return the k-th maxterm.

    Each maxterm is assigned an index based on the opposite
    conventional binary encoding used for minterms. The maxterm
    convention assigns the value 0 to the direct form and 1 to
    the complemented form.

    Parameters
    ==========

    k : int or list of 1's and 0's (complementation pattern)
    variables : list of variables

    Examples
    ========
    >>> from sympy.logic.boolalg import bool_maxterm
    >>> from sympy.abc import x, y, z
    >>> bool_maxterm([1, 0, 1], [x, y, z])
    y | ~x | ~z
    >>> bool_maxterm(6, [x, y, z])
    z | ~x | ~y

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Canonical_normal_form#Indexing_maxterms

    )r    r  r   rQ   r[  r  r   rm  r%  r&   r&   r'   bool_maxtermR
  r'  r(  c                 C   r$  )aR  
    Return the k-th monomial.

    Monomials are numbered by a binary encoding of the presence and
    absences of the variables. This convention assigns the value
    1 to the presence of variable and 0 to the absence of variable.

    Each boolean function can be uniquely represented by a
    Zhegalkin Polynomial (Algebraic Normal Form). The Zhegalkin
    Polynomial of the boolean function with `n` variables can contain
    up to `2^n` monomials. We can enumerate all the monomials.
    Each monomial is fully specified by the presence or absence
    of each variable.

    For example, boolean function with four variables ``(a, b, c, d)``
    can contain up to `2^4 = 16` monomials. The 13-th monomial is the
    product ``a & b & d``, because 13 in binary is 1, 1, 0, 1.

    Parameters
    ==========

    k : int or list of 1's and 0's
    variables : list of variables

    Examples
    ========
    >>> from sympy.logic.boolalg import bool_monomial
    >>> from sympy.abc import x, y, z
    >>> bool_monomial([1, 0, 1], [x, y, z])
    x & z
    >>> bool_monomial(6, [x, y, z])
    x & y

    )r    r  r   rQ   r[  r  r   r  r%  r&   r&   r'   bool_monomialv
  s   
#
r)  c                 C   s$   t | ts| hS t jtt| j S )zHelper to find logical predicates in BooleanFunctions.

    A logical predicate is defined here as anything within a BooleanFunction
    that is not a BooleanFunction itself.

    )r    r   rl   rm   r  r  rn   r  r&   r&   r'   r  
  s   
r  c                    sv  |dvrt dt| } |r/d}|dkrt| }n|dkr!t| }|r/tdd | jD r/| S dd	lm} |rS| |}dd
l	m
} tt||}	| tt||	} t| tsZ| S i }
i }ddlm} | |}|duryt|}||| |r| }|jr| }|||< ||
|< |j}||v rt||
|< || |s{| |
} |dur||
}t| }|st|dkr| |S |durt|}|| |st|dkrt| }d}tt|dd dd\}}|| }dd |D }t|| |}|durt|||  fdd|D }ng  t|dt|d  k}|dks)|du r2|r2t|| |S t || |S )ak  
    This function simplifies a boolean function to its simplified version
    in SOP or POS form. The return type is an :py:class:`~.Or` or
    :py:class:`~.And` object in SymPy.

    Parameters
    ==========

    expr : Boolean

    form : string (``'cnf'`` or ``'dnf'``) or ``None`` (default).
        If ``'cnf'`` or ``'dnf'``, the simplest expression in the corresponding
        normal form is returned; if ``None``, the answer is returned
        according to the form with fewest args (in CNF by default).

    deep : bool (default ``True``)
        Indicates whether to recursively simplify any
        non-boolean functions contained within the input.

    force : bool (default ``False``)
        As the simplifications require exponential time in the number
        of variables, there is by default a limit on expressions with
        8 variables. When the expression has more than 8 variables
        only symbolical simplification (controlled by ``deep``) is
        made. By setting ``force`` to ``True``, this limit is removed. Be
        aware that this can lead to very long simplification times.

    dontcare : Boolean
        Optimize expression under the assumption that inputs where this
        expression is true are don't care. This is useful in e.g. Piecewise
        conditions, where later conditions do not need to consider inputs that
        are converted by previous conditions. For example, if a previous
        condition is ``And(A, B)``, the simplification of expr can be made
        with don't cares for ``And(A, B)``.

    Examples
    ========

    >>> from sympy.logic import simplify_logic
    >>> from sympy.abc import x, y, z
    >>> b = (~x & ~y & ~z) | ( ~x & ~y & z)
    >>> simplify_logic(b)
    ~x & ~y
    >>> simplify_logic(x | y, dontcare=y)
    x

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Don%27t-care_term

    )Nr  r  zform can be cnf or dnf onlyFr  r  c                 s       | ]}t |V  qd S r+   )r   r   r&   r&   r'   r7  
  s    z!simplify_logic.<locals>.<genexpr>r   r?   r   )DummyNr  c                 S   s   | dv S )NrJ  r&   r   r&   r&   r'   r     r"  z simplify_logic.<locals>.<lambda>Tr   c                 S   s   g | ]
}|d kr
dndqS )TrN   r   r&   rh   r&   r&   r'   rk   !  r   z"simplify_logic.<locals>.<listcomp>c                    s   g | ]}| vr|qS r&   r&   r  dctruthtabler&   r'   rk   %  r  rN  rN   )!r  r   r  r  r  rn   rB   r@   rE   r   rJ   r[  r  r  r  r  r    r   r   r+  r  rR   r   r   r5   rY  r  rQ   r   r   _get_truthtabler  r  )rU  formr   r  dontcareform_okr@   r  rJ   r`   replundor+  varr
  nvardcvariablesr   r  
truthtablebigr&   r,  r'   r   
  s~   6









r   c                    sD   |    fdd fdd |g ggD }|g gkr g S |S )zN Return a list of all combinations leading to a True result for ``expr``.
    c                    sB   r    fdd| D }| fdd| D  |S | S )Nc                    s8   g | ]}|d  t ur|d   t id g|d  gqS rw  )r   r  rh   r  r&   r'   rk   5     8 z4_get_truthtable.<locals>._get_tt.<locals>.<listcomp>c                    s8   g | ]}|d  t ur|d   tidg|d  gqS rw  )r   r  r   rh   r9  r&   r'   rk   6  r:  )rR   r  )inputstab)_get_tt
_variablesr9  r'   r=  2  s   z _get_truthtable.<locals>._get_ttc                    s    g | ]}|d  r |d  qS rw  r&   )ri   r"  )constr&   r'   rk   9  rS  z#_get_truthtable.<locals>.<listcomp>)copy)r  rU  r?  r  r&   )r=  r>  r?  r'   r.  .  s   
r.  c           	      C   sL  | j }ttt|dd |D }| jD ]d}|jr$|| d  d7  < q|jr5||jd  d  d7  < qt|jtdd |jD f}|jD ]0}|jrc|| d  d7  < || d |  d7  < qG|jrt||jd  d	  d7  < qGt	d
qt
t}tt| D ]\}}ttdd |d  D |d< |t| | q|S )a  
    Assign a 5-item fingerprint to each symbol in the equation:
    [
    # of times it appeared as a Symbol;
    # of times it appeared as a Not(symbol);
    # of times it appeared as a Symbol in an And or Or;
    # of times it appeared as a Not(Symbol) in an And or Or;
    a sorted tuple of tuples, (i, j, k), where i is the number of arguments
    in an And or Or with which it appeared as a Symbol, and j is
    the number of arguments that were Not(Symbol); k is the number
    of times that (i, j) was seen.
    ]

    Examples
    ========

    >>> from sympy.logic.boolalg import _finger as finger
    >>> from sympy import And, Or, Not, Xor, to_cnf, symbols
    >>> from sympy.abc import a, b, x, y
    >>> eq = Or(And(Not(y), a), And(Not(y), b), And(x, y))
    >>> dict(finger(eq))
    {(0, 0, 1, 0, ((2, 0, 1),)): [x],
    (0, 0, 1, 0, ((2, 1, 1),)): [a, b],
    (0, 0, 1, 2, ((2, 0, 1),)): [y]}
    >>> dict(finger(x & ~y))
    {(0, 1, 0, 0, ()): [y], (1, 0, 0, 0, ()): [x]}

    In the following, the (5, 2, 6) means that there were 6 Or
    functions in which a symbol appeared as itself amongst 5 arguments in
    which there were also 2 negated symbols, e.g. ``(a0 | a1 | a2 | ~a3 | ~a4)``
    is counted once for a0, a1 and a2.

    >>> dict(finger(to_cnf(Xor(*symbols('a:5')))))
    {(0, 0, 8, 8, ((5, 0, 1), (5, 2, 6), (5, 4, 1))): [a0, a1, a2, a3, a4]}

    The equation must not have more than one level of nesting:

    >>> dict(finger(And(Or(x, y), y)))
    {(0, 0, 1, 0, ((2, 0, 1),)): [x], (1, 0, 1, 0, ((2, 0, 1),)): [y]}
    >>> dict(finger(And(Or(x, And(a, x)), y)))
    Traceback (most recent call last):
    ...
    NotImplementedError: unexpected level of nesting

    So y and x have unique fingerprints, but a and b do not.
    c                 S   s    g | ]}d gd t tg qS )r      )r   r  )ri   fir&   r&   r'   rk   p  rS  z_finger.<locals>.<listcomp>r   rN   c                 s   s    | ]}t |tV  qd S r+   )r    r5   )ri   air&   r&   r'   r7  w  s    z_finger.<locals>.<genexpr>rN  r  r  zunexpected level of nestingc                 S   s   g | ]	\}}||f qS r&   r&   )ri   rj   rD  r&   r&   r'   rk     s    )rP   r  r  r  rn   rf   rK  rQ   r  rD   r   r   iteritemsr[  sortedr   )	eqr  r
  r   orC  invr"  r  r&   r&   r'   _finger@  s*   /

"rJ  c                 C   s2   dd }t | }t |}|||}|r||fS |S )a  
    Return the simplified version of *bool1*, and the mapping of variables
    that makes the two expressions *bool1* and *bool2* represent the same
    logical behaviour for some correspondence between the variables
    of each.
    If more than one mappings of this sort exist, one of them
    is returned.

    For example, ``And(x, y)`` is logically equivalent to ``And(a, b)`` for
    the mapping ``{x: a, y: b}`` or ``{x: b, y: a}``.
    If no such mapping exists, return ``False``.

    Examples
    ========

    >>> from sympy import SOPform, bool_map, Or, And, Not, Xor
    >>> from sympy.abc import w, x, y, z, a, b, c, d
    >>> function1 = SOPform([x, z, y],[[1, 0, 1], [0, 0, 1]])
    >>> function2 = SOPform([a, b, c],[[1, 0, 1], [1, 0, 0]])
    >>> bool_map(function1, function2)
    (y & ~z, {y: a, z: b})

    The results are not necessarily unique, but they are canonical. Here,
    ``(w, z)`` could be ``(a, d)`` or ``(d, a)``:

    >>> eq =  Or(And(Not(y), w), And(Not(y), z), And(x, y))
    >>> eq2 = Or(And(Not(c), a), And(Not(c), d), And(b, c))
    >>> bool_map(eq, eq2)
    ((x & y) | (w & ~y) | (z & ~y), {w: a, x: b, y: c, z: d})
    >>> eq = And(Xor(a, b), c, And(c,d))
    >>> bool_map(eq, eq.subs(c, x))
    (c & d & (a | b) & (~a | ~b), {a: a, b: b, c: d, d: x})

    c                 S   s   | j |j krdS t| jt|jkrdS | jr| |iS t| }t|}t|t|kr-dS i }| D ]+}||vr< dS t|| t|| krK dS t|| D ]\}}|| | ||< qQq3|S )a:  Return the mapping that equates variables between two
        simplified boolean expressions if possible.

        By "simplified" we mean that a function has been denested
        and is either an And (or an Or) whose arguments are either
        symbols (x), negated symbols (Not(x)), or Or (or an And) whose
        arguments are only symbols or negated symbols. For example,
        ``And(x, Not(y), Or(w, Not(z)))``.

        Basic.match is not robust enough (see issue 4835) so this is
        a workaround that is valid for simplified boolean expressions
        NF)r   rQ   rn   rf   rJ  r  rZ  )r  r  f1f2	matchdictr"  rj   r]   r&   r&   r'   match  s(   zbool_map.<locals>.match)r   )bool1bool2rN  r   r  r  r&   r&   r'   bool_map  s   $)
rQ  c           	         s   ddl m m |du r|dur|}t| j fdddd\}}t|dkr(| S t|d	d dd\}}d
d |D }|rLt|dkrLt||| j|||}t||| j|||}| jfddt	|D | |  } | S )a  
    Replace patterns of Relational

    Parameters
    ==========

    rv : Expr
        Boolean expression

    patterns : tuple
        Tuple of tuples, with (pattern to simplify, simplified pattern) with
        two terms.

    measure : function
        Simplification measure.

    dominatingvalue : Boolean or ``None``
        The dominating value for the function of consideration.
        For example, for :py:class:`~.And` ``S.false`` is dominating.
        As soon as one expression is ``S.false`` in :py:class:`~.And`,
        the whole expression is ``S.false``.

    replacementvalue : Boolean or ``None``, optional
        The resulting value for the whole expression if one argument
        evaluates to ``dominatingvalue``.
        For example, for :py:class:`~.Nand` ``S.false`` is dominating, but
        in this case the resulting value is ``S.true``. Default is ``None``.
        If ``replacementvalue`` is ``None`` and ``dominatingvalue`` is not
        ``None``, ``replacementvalue = dominatingvalue``.

    threeterm_patterns : tuple, optional
        Tuple of tuples, with (pattern to simplify, simplified pattern) with
        three terms.

    r   r@   
_canonicalNc                    r   r+   r   r   r?   r&   r'   r     r   z4_apply_patternbased_simplification.<locals>.<lambda>Tr   rN   c                 S   s   t dd | jD  S )Nc                 s   s    | ]}|j d u V  qdS )FN)is_realrP  r&   r&   r'   r7    s    zG_apply_patternbased_simplification.<locals>.<lambda>.<locals>.<genexpr>)r9  rP   r   r&   r&   r'   r     s    
c                 S   r  r&   r  rh   r&   r&   r'   rk     r  z6_apply_patternbased_simplification.<locals>.<listcomp>r  c                    s   g | ]} |qS r&   r&   rh   )rS  r&   r'   rk     r   )
rB   r@   rS  r   rn   rQ   ,_apply_patternbased_threeterm_simplificationrZ   *_apply_patternbased_twoterm_simplificationr   )	r   r#  r   dominatingvaluereplacementvaluer  r  r  
nonRealRelr&   rR  r'   r    s4   '



r  c                    s  ddl m}m} ddlm mm d}|rt| dkrd} fdd| D } tt	| } fd	d| D }	g }
t
t|	dD ]n\\}}\}}|D ]c\}}g }t||D ]\}}t||}||}|rl|||f qU|r|D ];\}}||}||kr|g      S t|ts|||s|||j || }|dkr|
|||g|ff qqqJq@|
rt|
d
d dd}
|
d d }|\}}|  t|D ]}| |= q|du s|t|kr|j|kr|jD ]}| | qn| | d}|rt| dks| S )z- Apply pattern-based two-term simplification.r   MinMaxr>  Gt_InequalityTrN  Fc                    $   g | ]}t | fr|jn|qS r&   r    reversedr4  )r>  r^  r&   r'   rk   )     $ z>_apply_patternbased_twoterm_simplification.<locals>.<listcomp>c                    &   g | ]}t | r|fn||jfqS r&   ra  r4  r_  r&   r'   rk   -     & c                 S      | d S r  r&   pairr&   r&   r'   r   M  r"  z<_apply_patternbased_twoterm_simplification.<locals>.<lambda>r  reverserN   N)(sympy.functions.elementary.miscellaneousr[  r\  rB   r>  r^  r_  rQ   r  r   r   rZ  r   r
   rN  r   r  r    rT  rC   rn   rF  sortrb  r5   rZ   )r  r#  rZ   rW  rX  r   r[  r\  changedrtmpresultsrj   pirD  pjpatternsimpr  p1p2oldexprtmpresnp
costsavingreplacementidxnewrelr  r   r&   r]  r'   rV    s^   





9rV  c               	      s  ddl m}m} ddlm mm d}|rt| dkrd} fdd| D } tt	| } g }	fd	d| D }
t
t|
dD ]u\\}}\}}\}}|D ]g\}}g }t|||D ]\}}}t|||}||}|rt|||f q[|r|D ]<\}}||}||kr|g      S t|ts|||s|||j || }|dkr|	||||g|ff qyqOqB|	rt|	d
d dd}	|	d d }|\}}|  t|D ]}| |= q|du s|t|kr|j|kr|jD ]}| | qn| | d}|rt| dks| S )z/ Apply pattern-based three-term simplification.r   rZ  r=  Ltr_  Tr  Fc                    r`  r&   ra  r4  )r=  r  r&   r'   rk   m  rc  z@_apply_patternbased_threeterm_simplification.<locals>.<listcomp>c                    rd  r&   ra  r4  re  r&   r'   rk   s  rf  c                 S   rg  r  r&   rh  r&   r&   r'   r     r"  z>_apply_patternbased_threeterm_simplification.<locals>.<lambda>rj  rN   N)rl  r[  r\  rB   r=  r  r_  rQ   r  r   r   rZ  r   r
   rN  r   r  r    rT  rC   rn   rF  rm  rb  r5   rZ   ) r  r#  rZ   rW  rX  r   r[  r\  rn  rp  ro  rj   rq  rD  rr  r"  pkrs  rt  r  ru  rv  p3rw  rx  ry  rz  r{  r|  r}  r  r   r&   r~  r'   rU  b  s^   $




9rU  c                  C   sF  ddl m}  ddlm}m}m}m}m}m} ddl	m
} ddlm}m}	 | d}
| d}| d}t||
|||
|tft|||
||
|tft||
||||
||
|ft|||
||
|||
|ft||
|||
|||
|ft||
|||
|||
|ft||
|||
|||
|ft||
|||
| t||
tj||tjft|||
|||
||
|	||ft|||
|||
t||k||
|||
|ft|||
|||
||
|	||ft||
|||
|||
|||ft||
|||
|t||k ||
|||
|ft||
|||
|||
|||ft||
||||
t|||||
|t||k tt||
|||
|ft|||
||
|t|||||
|t||k tt||
|||
|ft||
||||
t||k tt||
|||
|ft|||
||
|t||k tt||
|||
|ft||
||||
t||ktt||
|||
|ft|||
||
|t||ktt||
|||
|ft||
|||
|t|||||
|tft||
||| |
t|dk|||
|tft||
||| |
t|dk|||
|tff}|S )	z Two-term patterns for And.r   Wildrc   rd   r>  r^  r=  r  AbsrZ  r   r  r   )
sympy.corer  rB   rc   rd   r>  r^  r=  r  $sympy.functions.elementary.complexesr  rl  r[  r\  r
   r   r-   r   r   rT  )r  rc   rd   r>  r^  r=  r  r  r[  r\  r   r  r   _matchers_andr&   r&   r'   r    s@    0$0$$0$JJ6666,00!r  c                  C   s*  ddl m}  ddlm}m}m} | d}| d}| d}t|||||||||tft|||||||||tft|||||||||tft|||||||||t||||||ft|||||||||t||||||ft|||||||||t||||||ft|||||||||t||||||ft|||||||||t||||||ft|||||||||t||||||ft|||||||||t||||||ft|||||||||t||||||ft|||||||||t||||||ft|||||||||t||||||ft|||||||||t||||||ft|||||||||t||||||ft|||||||||t||||||ff}|S )z Three-term patterns for And.r   r  )rc   r>  r^  r   r  r   )	r  r  rB   rc   r>  r^  r
   r   r-   )r  rc   r>  r^  r   r  r   r  r&   r&   r'   r    s.      2222222222222r  c                  C   s  ddl m}  ddlm}m}m}m}m}m} ddl	m
} ddlm}m}	 | d}
| d}| d}t|||
||
|tft|||
||
|tft||
|||
|||
|ft||
|||
|||
|ft|||
||
|||
|ft|||
||
|||
|ft||
|||
|||
|ft||
|||
|t|||t||
|ft||
|||
|t|||||
|tft|||
|||
||
|||ft|||
|||
t||k|||
|||
ft|||
|||
||
|||ft||
|||
|||
|	||ft||
|||
|t||k||
|||
|ft||
|||
|||
|	||ft||
||||
t||ktt||
|||
|ft|||
||
|t||ktt||
|||
|ft||
||||
t||ktt||
|||
|ft|||
||
|t||ktt||
|||
|ft||
||||
t||ktt||
|||
|ft|||
||
|t||ktt||
|||
|ft|||
||
| t|dk|||
|tft|||
||
| t|dk|||
|tff}|S )	z Two-term patterns for Or.r   r  r  r  rZ  r   r  r   )r  r  rB   rc   rd   r>  r^  r=  r  r  r  rl  r[  r\  r
   r   rT  r2   )r  rc   rd   r>  r^  r=  r  r  r[  r\  r   r  r   _matchers_orr&   r&   r'   rB    s@    ,,$0$$0$66666600$rB  c                  C   sx  ddl m} m} ddlm} ddlm}m}m}m	}m
}m} |d}	|d}
|d}t||	|
||	|
||	|
ft||	|
||	|
||	|
ft||	|
||	|
||	|
ft||	|
||
|	||	|
ft||
|	||	|
||	|
ft||
|	||	|
||	|
ft||
|	|||	t||	| |
|||	||
|ft||
|	|||	t|
|kt||	|||	|
t||	|
||	|ft||
|	|||	t||	| |
|||	||
|ft||	|
||	|t||	||
|||	| |
|ft||	|
||	|t|
|k t||	|||	|
t||	|
||	|ft||	|
||	|t||	||
|||	| |
|ff}|S )z Two-term patterns for Xor.r   rZ  r  r  r   r  r   )rl  r[  r\  r  r  rB   rc   rd   r>  r^  r=  r  r
   r-   rT  )r[  r\  r  rc   rd   r>  r^  r=  r  r   r  r   _matchers_xorr&   r&   r'   rp  8  sH        rp  c                 C   s  ddl m} ddlm}m} t| ts| S | ||r| S | }|j}t	|dkr*|S |
 }|d|fddj|dd\}}|sA|S |sEtS g }	|D ]y\}
}}}|
tju rn|tju r\t}na|||dkri||k}nT||k }nO|||
dk}|||dk}|r|r|jr||
k}n3t|
|k||k}n)|rt|
|k||k }n|r|jr||
k}nt|
|k ||k}n	t|
|k ||k }|	| qIt|	 S )	zKreturn a simplified version of univariate boolean expression, else ``expr``r   r   rb   rN   Fr  T)	err_on_Eq)r   r   rB   rc   rd   r    r   rE   rP   rQ   rR   
_intervalsr   r   NegativeInfinityInfinityr   rY   is_infiniter-   r   r2   )rU  r   rc   rd   r   r\   r]   okrj   rn   r   r  _incl_aincl_br&   r&   r'   r   f  sR   







r   c                 C   s>   t | ts	tdt | trt| jtdd | jD  S dS )a  
    Return the total number of inputs for the logic gates realizing the
    Boolean expression.

    Returns
    =======

    int
        Number of gate inputs

    Note
    ====

    Not all Boolean functions count as gate here, only those that are
    considered to be standard gates. These are: :py:class:`~.And`,
    :py:class:`~.Or`, :py:class:`~.Xor`, :py:class:`~.Not`, and
    :py:class:`~.ITE` (multiplexer). :py:class:`~.Nand`, :py:class:`~.Nor`,
    and :py:class:`~.Xnor` will be evaluated to ``Not(And())`` etc.

    Examples
    ========

    >>> from sympy.logic import And, Or, Nand, Not, gateinputcount
    >>> from sympy.abc import x, y, z
    >>> expr = And(x, y)
    >>> gateinputcount(expr)
    2
    >>> gateinputcount(Or(expr, z))
    4

    Note that ``Nand`` is automatically evaluated to ``Not(And())`` so

    >>> gateinputcount(Nand(x, y, z))
    4
    >>> gateinputcount(Not(And(x, y, z)))
    4

    Although this can be avoided by using ``evaluate=False``

    >>> gateinputcount(Nand(x, y, z, evaluate=False))
    3

    Also note that a comparison will count as a Boolean variable:

    >>> gateinputcount(And(x > z, y >= 2))
    2

    As will a symbol:
    >>> gateinputcount(x)
    0

    zExpression must be Booleanc                 s   r*  r+   )gateinputcountrF  r&   r&   r'   r7    r8  z!gateinputcount.<locals>.<genexpr>r   )r    r"   r#   BooleanGatesrQ   rn   r  r  r&   r&   r'   r    s
   
5
r  rt   )FFr+   )NTFN)NN)trx   collectionsr   	itertoolsr   r   r   r   sympy.core.addr   sympy.core.basicr   sympy.core.cacher	   sympy.core.containersr
   sympy.core.decoratorsr   r   sympy.core.functionr   r   sympy.core.kindr   r   sympy.core.numbersr   sympy.core.operationsr   sympy.core.singletonr   r   sympy.core.sortingr   sympy.core.sympifyr   r   r   sympy.utilities.iterablesr   r   sympy.utilities.miscr   r(   r"   r   r   r   r   r   rx  r   r-   r2   r5   r<   r;  r,  rv  r9   rF   rT  r  r  r  r  r  r0  r  r   rK   r  r  r  r  r  r  r  r  r   r  r  integer_to_termr  r  rg  rm  r  ri  rn  r  r  r  r  r  r  r   r  r&  r(  r)  r  r   r.  rJ  rQ  r  rV  rU  r  r  rB  rp  r   r  r  r&   r&   r&   r'   <module>   s    ) 	4tJY !jt 	"TTu

,

*(
81"
D

+f
K
Q>7$$)
 GW
BDD
1
*
4
-4