U
    9%e                     @   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SetKindZ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kr|S tjkrtjS |tjkrtjS |D ]}t|ddstd| q|tjk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 k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   V/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/sympy/sets/conditionset.py	<genexpr>e   s     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)Zdeprecated_since_versionZ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>       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>   s      z(ConditionSet.__new__.<locals>.<dictcomp>r   r   )r   r   r   r   
isinstancer   	TypeErrorr   r   r   r   r   truefalseZEmptySetgetattr
ValueErrorcontainsr   argsmapdictzipfree_symbolsitemsZxreplacer   Z	intersectr   __new__r   )r*   r$   r#   base_setZflatZcondition_origtempiZknowZsiftedr)   cbr+   sasbZrepsrvr   )r*   r#   r$   r   r<   X   s    






 




zConditionSet.__new__c                 C   s
   | j d S r'   r6   selfr   r   r   r%      r&   zConditionSet.<lambda>c                 C   s
   | j d S )Nr   rE   rF   r   r   r   r%      r&   c                 C   s
   | j d S )N   rE   rF   r   r   r   r%      r&   c                 C   s   | j j| jj }|| jjB S r    )r#   r:   r$   r=   )rG   Z	cond_symsr   r   r   r:      s    zConditionSet.free_symbolsc                 C   s   t | jgS r    )r   r$   rF   r   r   r   bound_symbols   s    zConditionSet.bound_symbolsc                    s    fdd  | j |stjS t|| j}|tjkr:tjS t| j f| j}z||}W n" tk
rx   t|| dd Y S X 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   )r/   r   )r   r?   r   r   r   
<listcomp>   s     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allr9   )arA   Ztuplesr@   rL   r   r   rM      s    
z&ConditionSet._contains.<locals>.ok_sigF)evaluate)	r$   r   r2   r   r=   r   r#   r0   r   )rG   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=   r5   )rG   rS   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)r6   r!   r(   hasfuncr3   )	rG   oldnewr$   ZcondbaseZdsymZinsymZnewbaser   r   r   
_eval_subs   s    
zConditionSet._eval_subsc                 C   s   t | jjS r    )r   r$   kindrF   r   r   r   _kind   s    zConditionSet._kindN)__name__
__module____qualname____doc__r   ZUniversalSetr<   propertyr$   r#   r=   r:   rI   rT   rV   r\   r^   r   r   r   r   r      s   DY

r   N)#Zsympy.core.singletonr   Zsympy.core.basicr   Zsympy.core.containersr   Zsympy.core.functionr   r   Zsympy.core.logicr   Zsympy.core.relationalr	   Zsympy.core.symbolr
   Zsympy.core.sympifyr   Zsympy.logic.boolalgr   r   Zsympy.utilities.iterablesr   r   r   Zsympy.utilities.exceptionsr   r5   r   Zsetsr   r   r   r   r(   r   r   r   r   r   <module>   s   