
    wgp                         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"y)    )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y)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                     t              t        g      }t        |      rt        d      t        |      }t	        |t
              st        d|z        t              t	        t              r(}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      rt!        d|z         |j#                        t        j                  u rt        dd|d      d }t	        |t              r7t%        |fd      }	|	d    rt        |	d    }t        |	d     }nt        |	d    S t	        |       r|j&                  \  }
}} fd}t)        ||
f      \  }}||k7  rt        d      t+        t-        t        g      t        |
g                  }|
k(  rt        |      |}n|j.                  j.                  z  sA|j1                         D ci c]  \  }}||
 }}}t        |j3                  |            |}nGj.                  |
j.                  z  s.j3                  |      t        j3                  |      |      |}t	        t4              rMj&                  d   k(  r;t	        j&                  d   t
              rj&                  d   j7                  |      S t9        j:                   |      }||S t=        ||      S c c}}w )NzDuplicate symbols detectedz'base set should be a Set object, not %sc              3   4   K   | ]  }t        |d         yw)r   Nr	   ).0lhss     \/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/sets/conditionset.py	<genexpr>z'ConditionSet.__new__.<locals>.<genexpr>e   s     43BsAJ4s   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                 :    t        j                  |             S N)r   subs)_	conditionsyms    r    <lambda>z&ConditionSet.__new__.<locals>.<lambda>   s    Jy~~c1/E$F     Tc                 b     | t        t        d            j                         j                  S Nr   )r
   adummyas_dummyr,   )sclss    r    sigz!ConditionSet.__new__.<locals>.sig   s%    1bm,557;;;r.   z"sym does not match sym of base setr   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kvrvs   ```                 r    rF   zConditionSet.__new__X   s"   smu~D>#$@AAH%(C(9HDF FY'	i+&N4)4DT
I%   K  */+H" y)	O::qzz!::  	@A1k51 !:Q!>??	@ S!QWW,#xPQQh	*FHFd| &,/$fTl3 &,//h$mmGAq!<sAh'FBRx'(LMMGSENGQCL9:DCx	1-	^^c&6&66)-6A166	1::d+;<	++ann<ll4(	 2 24 8!<	 i*y~~a7H0H)..+S1 ~~a(228<<]]3Y9\r6uT26 7s   
Mc                      | j                   d   S r0   r>   selfs    r    r-   zConditionSet.<lambda>   s    		! r.   c                      | j                   d   S )Nr   rW   rX   s    r    r-   zConditionSet.<lambda>   s    diil r.   c                      | j                   d   S )N   rW   rX   s    r    r-   zConditionSet.<lambda>   s    TYYq\ r.   c                     | j                   j                  | j                  j                  z
  }|| j                  j                  z  S r(   )r+   rB   r,   rG   )rY   	cond_symss     r    rB   zConditionSet.free_symbols   s5    NN//$((2G2GG	4==5555r.   c                 .    t        | j                  g      S r(   )r   r,   rX   s    r    bound_symbolszConditionSet.bound_symbols   s    z""r.   c                 N   fd | j                   |      st        j                  S t        || j                        }|t        j                  u rt        j                  S t        | j                   f| j                        }	  ||      }t        ||      S # t        $ r Y y w xY w)Nc                     | |fD cg c]  }t        |t               }}|j                  d      }|dk(  ry|dk(  ryt        |       t        |      k(  xr t	        fdt        | |      D              S c c}w )NTr   Fr   c              3   6   K   | ]  \  }} ||        y wr(    )r   rK   jok_sigs      r    r!   z9ConditionSet._contains.<locals>.ok_sig.<locals>.<genexpr>   s       ,4!%Aq!,4s   )r6   r   countlenallrA   )arO   rK   tuplesrN   rf   s        r    rf   z&ConditionSet._contains.<locals>.ok_sig   s|    56F;qjE*;F;T"AAvAvq6SV# 4 ,4),Q,4 )4 4 <s   A5)	r,   r   r9   r   rG   r   r+   r   r7   )rY   other	base_condlamdalambda_condrf   s        @r    	_containszConditionSet._contains   s    	4 dhh&77N UDMM2	77N {DNN3	/,K y+..  		s   B 	B$#B$c                     t        | j                  | j                        }t        | j                  t              r || }n ||      }t        || j                  j                  |            S r(   )r   r,   r+   r6   r   r   rG   r=   )rY   rl   fs      r    as_relationalzConditionSet.as_relational   sR    488T^^,dhh&5	A%A1dmm,,U344r.   c                 b   | j                   \  }}}|j                  |t              }|j                  t              }|j                  ||      }||k7  r'|s|j                  ||      }| j	                  |||      S |rn!t        |dd      r|j                  ||      }n	 | j	                  |||      S )Nr%   F)r>   r)   r1   hasfuncr;   )	rY   oldnewr,   condbasedsyminsymnewbases	            r    
_eval_subszConditionSet._eval_subs   s    ))T4xxV$ ))C%d?yyc*99S$00S+u-99S#&DyydD))r.   c                 @    t        | j                  j                        S r(   )r   r,   kindrX   s    r    _kindzConditionSet._kind   s    txx}}%%r.   N)__name__
__module____qualname____doc__r   UniversalSetrF   propertyr,   r+   rG   rB   r`   rp   rs   r~   r   rd   r.   r    r   r      sv    CH /0nn W7r ,
-C23I12H6 6 # #/: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   r=   r   setsr   r   r   r   r1   r   rd   r.   r    <module>r      sL    " " ' 9 ' $ # ' / = = @  0 0 
~	c&3 c&r.   