a
     ¬<bL  ã                   @   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 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Ú	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.

    .. 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 r®tjS |tju r¾tjS |D ]}t|ddƒsÂt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ržtdƒ‚tttˆgƒt|
gƒƒƒ}|
ˆkrÒtˆ|ƒ‰|}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   úg/Users/vegardjervell/Documents/master/model/venv/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)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>Ž   r   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>¡   r   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*   ÚsaZsbZrepsÚrvr   )r)   r#   r$   r   r;   X   s†    

ÿ
ú
öò


ÿ




zConditionSet.__new__c                 C   s
   | j d S r&   ©r5   ©Úselfr   r   r   r%   ±   r   zConditionSet.<lambda>c                 C   s
   | j d S )Nr   rC   rD   r   r   r   r%   ²   r   c                 C   s
   | j d S )Né   rC   rD   r   r   r   r%   ³   r   c                 C   s   | j j| jj }|| jjB S r    )r#   r9   r$   r<   )rE   Z	cond_symsr   r   r   r9   µ   s    zConditionSet.free_symbolsc                 C   s   t | jgƒS r    )r   r$   rD   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   )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Úallr8   )Úar@   Ztuplesr?   rJ   r   r   rK   ¿   s    
ÿz&ConditionSet._contains.<locals>.ok_sigF)Úevaluate)	r$   r   r1   r   r<   r   r#   r/   r   )rE   ÚotherZ	base_condZlamdaZlambda_condr   rJ   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<   r4   )rE   rQ   Ú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)r5   r!   r'   ZhasÚfuncr2   )	rE   Ú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<   r9   rG   rR   rT   rY   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   r4   r   Zsetsr   r   r   r'   r   r   r   r   r   Ú<module>   s   