
    Ed                         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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                       e Zd ZdZej        fdZ ed           Z ed           Z	 ed           Z
ed             Zed             Zd Zd	 Zd
 Z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                 P    t                    t          g          }t          |          rt          d          t          |          }t	          |t
                    st          d|z            t                    t	          t                    r0}d D             }t          | t          d| d ddd	           t                    t          j        u r|S t          j        u rt          j        S |t          j        u rt          j        S |D ]%}t          |d
d          st!          d|z            &|                              t          j        u rt          dd|d          d }t	          |t                    rJt%          |fd          }	|	d          rt          |	d          }t          |	d           }nt          |	d          S t	          |           r(|j        \  }
}} fd}t)          ||
f          \  }}||k    rt          d          t+          t-          t          g          t          |
g                              }|
k    rt          |          |}n|j        j        z  sDd |                                D             }t          |                    |                    |}nIj        |
j        z  s:                    |          t                              |          |          |}t	          t4                    rQj        d         k    r@t	          j        d         t
                    r j        d                             |          S t9          j         |          }||nt=          ||          S )NzDuplicate symbols detectedz'base set should be a Set object, not %sc              3   6   K   | ]}t          |d           V  dS )r   Nr	   ).0lhss     7lib/python3.11/site-packages/sympy/sets/conditionset.py	<genexpr>z'ConditionSet.__new__.<locals>.<genexpr>e   s*      443BsAJJ444444    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 `z` is not in base_set ``c                 J    t                              |                     S N)r   subs)_	conditionsyms    r    <lambda>z&ConditionSet.__new__.<locals>.<lambda>   s    Jy~~c1/E/E$F$F r"   Tc                 p     | t          t          d                                                    j        S Nr   )r
   adummyas_dummyr-   )sclss    r    sigz!ConditionSet.__new__.<locals>.sig   s,    s1bmm,,5577;;r"   z"sym does not match sym of base setc                     i | ]\  }}||	S  r7   )r   kvs      r    
<dictcomp>z(ConditionSet.__new__.<locals>.<dictcomp>   s    666A1666r"   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   )r4   r-   r,   base_setflatcondition_origtempiknowsiftedr3   cbr5   sasbrepsrvs   ```               r    rK   zConditionSet.__new__X   s   smmu~~D>> 	B#$@AAAH%%(C(( 	F9HDF F FY''	i++ 	&N44)444DT
I%      */+H   " y))	 	O 	:qz! 	:  	@ 	@A1k511 @ !:Q!>???@ S!!QW, 	R)###xxxPQQQh	** 	0FFFFFH HFd| 0 &,/$fTl3 &,//h$$ 	mGAq!< < < < <sAh''FBRx N'(LMMMGSENNGQCLL99::DCx 
	1--	^c&66 66666	1::d+;+;<<	+an< ll4((	 2 24 8 8!<<	 i** 	=y~a7H0H 	=).+S11 = ~a(228<<<]3Y996rruT26r"   c                     | j         d         S r0   rC   selfs    r    r.   zConditionSet.<lambda>   s    	! r"   c                     | j         d         S )Nr   rZ   r[   s    r    r.   zConditionSet.<lambda>   s    dil r"   c                     | j         d         S )N   rZ   r[   s    r    r.   zConditionSet.<lambda>   s    TYq\ r"   c                 R    | j         j        | j        j        z
  }|| j        j        z  S r)   )r,   rG   r-   rL   )r\   	cond_symss     r    rG   zConditionSet.free_symbols   s&    N/$(2GG	4=555r"   c                 ,    t          | j        g          S r)   )r   r-   r[   s    r    bound_symbolszConditionSet.bound_symbols   s    z"""r"   c                 X   fd | j         |          st          j        S t          || j                  }|t          j        u rt          j        S t          | j         f| j                  }	  ||          }t          ||          S # t          $ r t          || d          cY S w xY w)Nc                     d | |fD             }|                     d          }|dk    rdS |dk    rdS t          |           t          |          k    o(t          fdt          | |          D                       S )Nc                 8    g | ]}t          |t                    S r7   )r;   r   )r   rP   s     r    
<listcomp>z:ConditionSet._contains.<locals>.ok_sig.<locals>.<listcomp>   s"    ;;;qjE**;;;r"   Tr   Fr   c              3   6   K   | ]\  }} ||          V  d S r)   r7   )r   rP   jok_sigs      r    r!   z9ConditionSet._contains.<locals>.ok_sig.<locals>.<genexpr>   sB       ,4 ,4!%Aq!,4 ,4 ,4 ,4 ,4 ,4r"   )countlenallrF   )arT   tuplesrS   rj   s       r    rj   z&ConditionSet._contains.<locals>.ok_sig   s    ;;QF;;;FT""AAv uAv tq66SVV# 4 ,4 ,4 ,4 ,4),Q,4 ,4 ,4 )4 )4 4r"   F)evaluate)	r-   r   r>   r   rL   r   r,   r   r<   )r\   other	base_condlamdalambda_condrj   s        @r    	_containszConditionSet._contains   s    	4 	4 	4 	4 	4 vdh&& 	7N UDM22	 	7N {DN33	/%,,K y+...  	9 	9 	9E4%888888	9s   /B
 
B)(B)c                     t          | j        | j                  }t          | j        t                    r || }n ||          }t          || j                            |                    S r)   )r   r-   r,   r;   r   r   rL   rB   )r\   rq   fs      r    as_relationalzConditionSet.as_relational   sc    48T^,,dh&& 	5	AA%A1dm,,U33444r"   c                    | j         \  }}}|                    |t                    }|                    t                    }|                    ||          }||k    r/|s|                    ||          }|                     |||          S |rn)t          |dd          r|                    ||          }n	 |                     |||          S )Nr&   F)rC   r*   r1   hasfuncr@   )	r\   oldnewr-   condbasedsyminsymnewbases	            r    
_eval_subszConditionSet._eval_subs   s    )T4xxV$$  ))C%%d? 	1 +yyc**99S$000 	S+u-- 	99S#&&DDyydD)))r"   c                 4    t          | j        j                  S r)   )r   r-   kindr[   s    r    _kindzConditionSet._kind   s    tx}%%%r"   N)__name__
__module____qualname____doc__r   UniversalSetrK   propertyr-   r,   rL   rG   rc   ru   rx   r   r   r7   r"   r    r   r      s        C CH /0n W7 W7 W7 W7r (,,
-
-C2233Ix1122H6 6 X6 # # X#/ / /:5 5 5* * *$& & & & &r"   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   rB   r   setsr   r   r   r   r1   r   r7   r"   r    <module>r      sh   " " " " " " " " " " " " ' ' ' ' ' ' 9 9 9 9 9 9 9 9 ' ' ' ' ' ' $ $ $ $ $ $ # # # # # # ' ' ' ' ' ' / / / / / / / / = = = = = = = = = = @ @ @ @ @ @       0 0 0 0 0 0 0 0 0 0 0 0 
~		c& c& c& c& c&3 c& c& c& c& c&r"   