a
    SG5d                     @   s   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 d d	lmZmZ d d
lmZmZ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BadSignatureError)
fuzzy_boolEq)Dummy)_sympify)And
as_Boolean)siftflattenhas_dups)sympy_deprecation_warning   )Contains)SetUnion	FiniteSetSetKindconditionsetc                   @   sz   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d ZdS )ConditionSeta  
    Set of elements which satisfies a given condition.

    .. math:: \{x \mid \textrm{condition}(x) = \texttt{True}, 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  t tg}t|r"tdt |}t|ts@td| t ttr}dd D }t| t	d| d ddd	d
 t
tju r|S tju rtjS |tju rtjS |D ]}t|ddstd| q|tju rtd|f d }t|trVt|fdd}	|	d  rJt|	d  }t|	d   }nt|	d  S t| r4|j\}
}} fdd}t||
f\}}||krtdtttgt|
g}|
krt||}nb|jj@ sdd | D }t|||}n,j|
j@ s4|t|||}ttrrjd krrtjd trrjd |S t |}|d u r|S t||S )NzDuplicate symbols detectedz'base set should be a Set object, not %sc                 s   s   | ]}t |d V  qdS )r   Nr   ).0lhs r   S/var/www/html/django/DPS/env/lib/python3.9/site-packages/sympy/sets/conditionset.py	<genexpr>e       z'ConditionSet.__new__.<locals>.<genexpr>zq
Using a set for the condition in ConditionSet is deprecated. Use a boolean
instead.

In this case, replace

    z

with

    
z1.5zdeprecated-conditionset-set)deprecated_since_versionactive_deprecations_target	_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>   r   z&ConditionSet.__new__.<locals>.<lambda>Tc                    s    | t td jS Nr   )r	   adummy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>   r   z(ConditionSet.__new__.<locals>.<dictcomp>r   r   )r   r   r   r   
isinstancer   	TypeErrorr   r   r   r   r   truefalseEmptySetgetattr
ValueErrorcontainsr   argsmapdictzipfree_symbolsitemsxreplacer   	intersectr   __new__r   )r.   r(   r'   base_setflatZcondition_origtempiZknowsiftedr-   cbr/   sasbrepsrvr   )r.   r'   r(   r   rC   X   s    











zConditionSet.__new__c                 C   s
   | j d S r*   r;   selfr   r   r   r)      r   zConditionSet.<lambda>c                 C   s
   | j d S )Nr   rO   rP   r   r   r   r)      r   c                 C   s
   | j d S )N   rO   rP   r   r   r   r)      r   c                 C   s   | j j| jj }|| jjB S r$   )r'   r?   r(   rD   )rQ   Z	cond_symsr   r   r   r?      s    zConditionSet.free_symbolsc                 C   s   t | jgS r$   )r   r(   rP   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yv   t|| dd Y S 0 t||S d S )Nc                    s`   dd | |fD }| d}|dkr(dS |dkr4dS t| t|ko^t fddt| |D S )	Nc                 S   s   g | ]}t |tqS r   )r3   r   )r   rG   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   rG   jok_sigr   r   r      s   z9ConditionSet._contains.<locals>.ok_sig.<locals>.<genexpr>)countlenallr>   )arJ   tuplesrI   rV   r   r   rW      s    
z&ConditionSet._contains.<locals>.ok_sigF)evaluate)	r(   r   r6   r   rD   r   r'   r4   r   )rQ   otherZ	base_condlamdaZlambda_condr   rV   r   	_contains   s    	
zConditionSet._containsc                 C   s>   t | j| j}t| jtr$|| }n||}t|| j|S r$   )r   r(   r'   r3   r   r   rD   r:   )rQ   r^   fr   r   r   as_relational   s
    
zConditionSet.as_relationalc           	      C   s   | j \}}}||t}|t}|||}||krT|sF|||}| |||S |rZnt|ddrt|||}n | |||S )Nr#   F)r;   r%   r+   hasfuncr8   )	rQ   oldnewr(   condbaseZdsymZinsymZnewbaser   r   r   
_eval_subs   s    
zConditionSet._eval_subsc                 C   s   t | jjS r$   )r   r(   kindrP   r   r   r   _kind   s    zConditionSet._kindN)__name__
__module____qualname____doc__r   UniversalSetrC   propertyr(   r'   rD   r?   rS   r`   rb   ri   rk   r   r   r   r   r      s   DY

r   N)#sympy.core.singletonr   sympy.core.basicr   sympy.core.containersr   sympy.core.functionr   r   sympy.core.logicr   sympy.core.relationalr	   sympy.core.symbolr
   sympy.core.sympifyr   sympy.logic.boolalgr   r   sympy.utilities.iterablesr   r   r   sympy.utilities.exceptionsr   r:   r   setsr   r   r   r   r+   r   r   r   r   r   <module>   s   