o
    8Vaf                     @   s   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	 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 d dlmZ ddlmZ ddlmZmZmZmZ edZG dd deZdS )    )S)Basic)Tuple)Lambda)
fuzzy_boolEq)Dummy)_sympify)And
as_Boolean)sift)SymPyDeprecationWarning   )Contains)SetEmptySetUnion	FiniteSetZconditionsetc                   @   sr   e Zd ZdZejfddZedd Zedd Z	edd Z
edd	 Zed
d Zdd Zdd Zdd ZdS )ConditionSeta  
    Set of elements which satisfies a given condition.

    {x | condition(x) is True for x in S}

    Examples
    ========

    >>> from sympy import Symbol, S, ConditionSet, pi, Eq, sin, Interval
    >>> from sympy.abc import x, y, z

    >>> sin_sols = ConditionSet(x, Eq(sin(x), 0), Interval(0, 2*pi))
    >>> 2*pi in sin_sols
    True
    >>> pi/2 in sin_sols
    False
    >>> 3*pi in sin_sols
    False
    >>> 5 in ConditionSet(x, x**2 > 4, S.Reals)
    True

    If the value is not in the base set, the result is false:

    >>> 5 in ConditionSet(x, x**2 > 4, Interval(2, 4))
    False

    Notes
    =====

    Symbols with assumptions should be avoided or else the
    condition may evaluate without consideration of the set:

    >>> n = Symbol('n', negative=True)
    >>> cond = (n > 0); cond
    False
    >>> ConditionSet(n, cond, S.Integers)
    EmptySet

    Only free symbols can be changed by using `subs`:

    >>> c = ConditionSet(x, x < 1, {x, z})
    >>> c.subs(x, y)
    ConditionSet(x, x < 1, {y, z})

    To check if ``pi`` is in ``c`` use:

    >>> pi in c
    False

    If no base set is specified, the universal set is implied:

    >>> ConditionSet(x, x < 1).base_set
    UniversalSet

    Only symbols or symbol-like expressions can be used:

    >>> ConditionSet(x + 1, x + 1 < 1, S.Integers)
    Traceback (most recent call last):
    ...
    ValueError: non-symbol dummy not recognized in condition

    When the base set is a ConditionSet, the symbols will be
    unified if possible with preference for the outermost symbols:

    >>> ConditionSet(x, x < y, ConditionSet(z, z + y < 2, S.Integers))
    ConditionSet(x, (x < y) & (x + y < 2), Integers)

    c                    s  ddl m} ddlm}m} t|g}||r|dt|}t|ts.td| ttt	rT}dd D }	t
|	 td|d	d
dd  ttju r_|S tju rgtjS t|trn|S |D ]}
t|
dds~td|
 qp|tju rtd|f d }t|t	rt|fdd}|d  rt	|d  }t	|d   }nt	|d  S t| r$|j\}}} fdd}t||f\}}||kr|dtt|g||g}|krt
||}n1|jj@ sdd | D }t
|||}nj|j@ s$|t
|||}ttrCjd krCtjd trCjd |S t ! |}|d u rR|S t"||S )Nr   )BadSignatureError)flattenhas_dupszDuplicate symbols detectedz'base set should be a Set object, not %sc                 s   s    | ]}t |d V  qdS )r   Nr   ).0Zlhs r   9/usr/lib/python3/dist-packages/sympy/sets/conditionset.py	<genexpr>h   s    z'ConditionSet.__new__.<locals>.<genexpr>zUsing {} for conditioniD  z1.5z{} for condition)ZfeatureZissueZdeprecated_since_versionZ
useinstead	_diff_wrtFz`%s` is not symbol-likez sym `%s` is not in base_set `%s`c                    s   t  | S N)r   subs)_)	conditionsymr   r   <lambda>   s    z&ConditionSet.__new__.<locals>.<lambda>Tc                    s    | t td jS Nr   )r   adummyZas_dummyr"   )s)clsr   r   sig   s   z!ConditionSet.__new__.<locals>.sigz"sym does not match sym of base setc                 S   s   i | ]\}}||qS r   r   )r   kvr   r   r   
<dictcomp>       z(ConditionSet.__new__.<locals>.<dictcomp>r   )#sympy.core.functionr   sympy.utilities.iterablesr   r   r
   
isinstancer   	TypeErrorr   r   r   formatwarnr   r   truefalser   getattr
ValueErrorcontainsr   argsmapdictzipfree_symbolsitemsZxreplacer   Z	intersectr   __new__r   )r'   r"   r!   base_setr   r   r   ZflatZcondition_origZtempiZknowZsiftedr&   cbr(   ZsaZsbZrepsZrvr   )r'   r!   r"   r   r>   Y   s   








zConditionSet.__new__c                 C   
   | j d S r$   r8   selfr   r   r   r#         
 zConditionSet.<lambda>c                 C   rC   )Nr   rD   rE   r   r   r   r#      rG   c                 C   rC   )N   rD   rE   r   r   r   r#      rG   c                 C   s   | j j| jj }|| jjB S r   )r!   r<   r"   r?   )rF   Z	cond_symsr   r   r   r<      s   zConditionSet.free_symbolsc                 C   s   ddl m} || jgS )Nr   )r   )r.   r   r"   )rF   r   r   r   r   bound_symbols   s   zConditionSet.bound_symbolsc                    s    fdd  | j |stjS t|| j}|tju rtjS t| j f| j}z||}W n ty;   t|| dd Y S w t||S )Nc                    s`   dd | |fD }| d}|dkrdS |dkrdS t| t|ko/t fddt| |D S )	Nc                 S   s   g | ]}t |tqS r   )r/   r   )r   r@   r   r   r   
<listcomp>   r,   z:ConditionSet._contains.<locals>.ok_sig.<locals>.<listcomp>Tr   Fr   c                 3   s    | ]
\}} ||V  qd S r   r   )r   r@   jok_sigr   r   r      s    
z9ConditionSet._contains.<locals>.ok_sig.<locals>.<genexpr>)countlenallr;   )arB   ZtuplesrA   rL   r   r   rM      s   
z&ConditionSet._contains.<locals>.ok_sigF)Zevaluate)	r"   r   r4   r   r?   r   r!   r0   r   )rF   otherZ	base_condZlamdaZlambda_condr   rL   r   	_contains   s   	

zConditionSet._containsc                 C   s>   t | j| j}t| jtr|| }n||}t|| j|S r   )r   r"   r!   r/   r   r   r?   r7   )rF   rR   fr   r   r   as_relational   s
   
zConditionSet.as_relationalc           	      C   s   | j \}}}||t}|t}|||}||kr*|s#|||}| |||S |r-nt|ddr:|||}n	 | |||S )Nr   F)r8   r   r%   Zhasfuncr5   )	rF   oldnewr"   ZcondbaseZdsymZinsymZnewbaser   r   r   
_eval_subs   s   
zConditionSet._eval_subsN)__name__
__module____qualname____doc__r   ZUniversalSetr>   propertyr"   r!   r?   r<   rI   rS   rU   rZ   r   r   r   r   r      s    DQ

r   N) Zsympyr   Zsympy.core.basicr   Zsympy.core.containersr   r-   r   Zsympy.core.logicr   Zsympy.core.relationalr   Zsympy.core.symbolr	   Zsympy.core.sympifyr
   Zsympy.logic.boolalgr   r   r.   r   Zsympy.utilities.exceptionsr   r7   r   Zsetsr   r   r   r   r%   r   r   r   r   r   <module>   s    