
    wg>7                       d dl mZmZ d dlmZ d dlmZ d dlZd dlm	Z	m
Z
mZ d dlmZ d dlmZ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mZmZmZmZ d dl m!Z!m"Z" d dl#m$Z$ d dl%m&Z& d dl'm(Z(m)Z)m*Z* d dl+m,Z,m-Z- d dl.m/Z/ d dl0m1Z1m2Z2m3Z3m4Z4 d dl5m6Z6m7Z7m8Z8 d dl9m:Z:m;Z; d dl<m=Z=m>Z> d dl?m@Z@mAZAmBZBmCZCmDZDmEZE d dlFmGZG d dlHmIZI d dlJmKZKmLZLmMZMmNZNmOZO d dlPmQZQmRZR d dlSmTZTmUZU d dlVmWZW  ed de-j                  e-j                  e-j                  d e-j                  e-j                  e-j                  i      ZXe G d! d"ee             ZY G d# d$eY      ZZ G d% d&eY      Z[ G d' d(eYe$      Z\ G d) d*eYe$      Z] G d+ d,eY      Z^ G d- d.eYe,/      Z_ G d0 d1eYe,/      Z` G d2 d3eY      Zad4 e8eb<   d5 e8ec<    G d6 d7eY      Zd G d8 d9eY      Zed: Zfd; Zgd< Zhd= Zid> Zjd? Zkd@ ZldA ZmdB ZndC ZodD ZpdE Zq G dF dGe	      Zry)H    )AnyCallable)reduce)defaultdictN)KindUndefinedKind
NumberKind)Basic)Tuple	TupleKind)sympify_method_argssympify_return)
EvalfMixin)Expr)Lambda)	FuzzyBool
fuzzy_boolfuzzy_or	fuzzy_and	fuzzy_not)FloatInteger)	LatticeOp)global_parameters)EqNeis_lt)	SingletonS)ordered)symbolsSymbolDummyuniquely_named_symbol)_sympifysympify_sympy_converter)explog)MaxMin)AndOrNotXortruefalse)
deprecated)sympy_deprecation_warning)iproductsift
roundrobiniterablesubsets)	func_name
filldedent)mpimpf)prec_to_dpsc                       y N r@       T/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/sets/sets.py<lambda>rC   %   s    rA   TFc                      e Zd ZU dZdZdZdZdZdZdZ	dZ
dZdZeed<   dZeed<   dZeed<   dZdZeed<   dZeed	<   e ed
dd      d               Zed        Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!ed        Z"ed        Z#ed        Z$ed        Z%d Z&d Z'd Z(d  Z)d! Z*d" Z+d# Z,d$ Z-d% Z.d& Z/d' Z0d( Z1ed)        Z2ed*        Z3ed+        Z4ed,        Z5ed-        Z6ed.        Z7ed/        Z8ed0        Z9ed1        Z:d2 Z;d3 Z< e=d4ge>      d5        Z? e=d4ge>      d6        Z@ e=d4ge>      d7        ZA e=d4ge>      d8        ZB e=d4ge>      d9        ZC e=d:eDfge>      d;        ZE e=d4ge>      d<        ZFd= ZGy)>Seta  
    The base class for any kind of set.

    Explanation
    ===========

    This is not meant to be used directly as a container of items. It does not
    behave like the builtin ``set``; see :class:`FiniteSet` for that.

    Real intervals are represented by the :class:`Interval` class and unions of
    sets by the :class:`Union` class. The empty set is represented by the
    :class:`EmptySet` class and available as a singleton as ``S.EmptySet``.
    r@   FNis_Intersectionis_UniversalSetis_Complementis_emptyis_finite_set
        The is_EmptySet attribute of Set objects is deprecated.
        Use 's is S.EmptySet" or 's.is_empty' instead.
        1.5deprecated-is-emptysetdeprecated_since_versionactive_deprecations_targetc                      y r?   r@   selfs    rB   is_EmptySetzSet.is_EmptySetN        rA   c                     	 | j                   }|j                  sJ |j                         }|S # t        t        t
        t        f$ r t        j                  }Y |S w xY w)z?
        Return infimum (if possible) else S.Infinity.
        )	infis_comparableevalfNotImplementedErrorAttributeErrorAssertionError
ValueErrorr   Infinity)exprinfimums     rB   _infimum_keyzSet._infimum_keyZ   s\    
	!hhG((((mmoG  $
< 	!jjG	!s   *. )AAc                     t        | |      S )a  
        Returns the union of ``self`` and ``other``.

        Examples
        ========

        As a shortcut it is possible to use the ``+`` operator:

        >>> from sympy import Interval, FiniteSet
        >>> Interval(0, 1).union(Interval(2, 3))
        Union(Interval(0, 1), Interval(2, 3))
        >>> Interval(0, 1) + Interval(2, 3)
        Union(Interval(0, 1), Interval(2, 3))
        >>> Interval(1, 2, True, True) + FiniteSet(2, 3)
        Union({3}, Interval.Lopen(1, 2))

        Similarly it is possible to use the ``-`` operator for set differences:

        >>> Interval(0, 2) - Interval(0, 1)
        Interval.Lopen(1, 2)
        >>> Interval(1, 3) - FiniteSet(2)
        Union(Interval.Ropen(1, 2), Interval.Lopen(2, 3))

        )UnionrS   others     rB   unionz	Set.unionh   s    2 T5!!rA   c                     t        | |      S )a  
        Returns the intersection of 'self' and 'other'.

        Examples
        ========

        >>> from sympy import Interval

        >>> Interval(1, 3).intersect(Interval(1, 2))
        Interval(1, 2)

        >>> from sympy import imageset, Lambda, symbols, S
        >>> n, m = symbols('n m')
        >>> a = imageset(Lambda(n, 2*n), S.Integers)
        >>> a.intersect(imageset(Lambda(m, 2*m + 1), S.Integers))
        EmptySet

        Intersectionrd   s     rB   	intersectzSet.intersect   s    & D%((rA   c                 $    | j                  |      S )z/
        Alias for :meth:`intersect()`
        rj   rd   s     rB   intersectionzSet.intersection        ~~e$$rA   c                 F    | j                  |      t        j                  k(  S )a}  
        Returns True if ``self`` and ``other`` are disjoint.

        Examples
        ========

        >>> from sympy import Interval
        >>> Interval(0, 2).is_disjoint(Interval(1, 2))
        False
        >>> Interval(0, 2).is_disjoint(Interval(3, 4))
        True

        References
        ==========

        .. [1] https://en.wikipedia.org/wiki/Disjoint_sets
        )rj   r   EmptySetrd   s     rB   is_disjointzSet.is_disjoint   s    $ ~~e$

22rA   c                 $    | j                  |      S )z1
        Alias for :meth:`is_disjoint()`
        )rq   rd   s     rB   
isdisjointzSet.isdisjoint        &&rA   c                     t        ||       S )ae  
        The complement of 'self' w.r.t the given universe.

        Examples
        ========

        >>> from sympy import Interval, S
        >>> Interval(0, 1).complement(S.Reals)
        Union(Interval.open(-oo, 0), Interval.open(1, oo))

        >>> Interval(0, 1).complement(S.UniversalSet)
        Complement(UniversalSet, Interval(0, 1))

        
ComplementrS   universes     rB   
complementzSet.complement   s     (D))rA   c           	          t         t              rt        |t              rt         j                        t        |j                        k7  r|S g }t	        t         j                  |j                              }t        t        |            D ]-  fdt        |      D        }|j                  t        |        / t        | S t        |t              r@t         t        t        f      r)t        | j                  t        j                              S y t        |t              rt         fd|j                   D         S t        |t"              r2t#        |j                   d   t        |j                   d          d      S |t        j$                  u rt        j$                  S t        |t              rRt'        | fd      }t        t        |d    |d    rt#        t        |d      d            S t        j$                        S y )Nc              3   B   K   | ]  \  }\  }}|k7  r|n||z
    y wr?   r@   ).0isons       rB   	<genexpr>z"Set._complement.<locals>.<genexpr>   s(     OFQQ!V1,Os   c              3   (   K   | ]	  }|z
    y wr?   r@   )r}   r   rS   s     rB   r   z"Set._complement.<locals>.<genexpr>   s     81t88s   r      Fevaluatec                 8    t        j                  |             S r?   r   contains)xrS   s    rB   rC   z!Set._complement.<locals>.<lambda>   s    :dmmA6F+G rA   )
isinstance
ProductSetlensetslistziprange	enumerateappendrc   Interval	FiniteSetri   rz   r   Realsargsrw   rp   r5   )rS   re   overlapspairsr   siftedr   s   `     @rB   _complementzSet._complement   s   dJ'Juj,I499~UZZ0 HTYY

34E3u:& 3Oi>NO
D 123 (##x($9 56#E4??177+CDD 7 u%8UZZ899z*ejjmU5::a=$-GRWXXajj ::y)%!GHFVE]4$< 9vd|5teL1 1%&ZZ1 1 *rA   c                     t        | |      S )aW  
        Returns symmetric difference of ``self`` and ``other``.

        Examples
        ========

        >>> from sympy import Interval, S
        >>> Interval(1, 3).symmetric_difference(S.Reals)
        Union(Interval.open(-oo, 1), Interval.open(3, oo))
        >>> Interval(1, 10).symmetric_difference(S.Reals)
        Union(Interval.open(-oo, 1), Interval.open(10, oo))

        >>> from sympy import S, EmptySet
        >>> S.Reals.symmetric_difference(EmptySet)
        Reals

        References
        ==========
        .. [1] https://en.wikipedia.org/wiki/Symmetric_difference

        SymmetricDifferencerd   s     rB   symmetric_differencezSet.symmetric_difference   s    , #4//rA   c                 B    t        t        | |      t        ||             S r?   )rc   rw   rd   s     rB   _symmetric_differencezSet._symmetric_difference  s    Ze,j.EFFrA   c                     | j                   S )z
        The infimum of ``self``.

        Examples
        ========

        >>> from sympy import Interval, Union
        >>> Interval(0, 1).inf
        0
        >>> Union(Interval(0, 1), Interval(2, 3)).inf
        0

        )_infrR   s    rB   rW   zSet.inf       yyrA   c                     t        d| z        )Nz	(%s)._infrZ   rR   s    rB   r   zSet._inf      !+"455rA   c                     | j                   S )z
        The supremum of ``self``.

        Examples
        ========

        >>> from sympy import Interval, Union
        >>> Interval(0, 1).sup
        1
        >>> Union(Interval(0, 1), Interval(2, 3)).sup
        3

        )_suprR   s    rB   supzSet.sup  r   rA   c                     t        d| z        )Nz	(%s)._supr   rR   s    rB   r   zSet._sup.  r   rA   c                     ddl m} t        |d      }| j                  |      }t	        ||      r|S | ||| d      S t
        |   }||S |S )a   
        Returns a SymPy value indicating whether ``other`` is contained
        in ``self``: ``true`` if it is, ``false`` if it is not, else
        an unevaluated ``Contains`` expression (or, as in the case of
        ConditionSet and a union of FiniteSet/Intervals, an expression
        indicating the conditions for containment).

        Examples
        ========

        >>> from sympy import Interval, S
        >>> from sympy.abc import x

        >>> Interval(0, 1).contains(0.5)
        True

        As a shortcut it is possible to use the ``in`` operator, but that
        will raise an error unless an affirmative true or false is not
        obtained.

        >>> Interval(0, 1).contains(x)
        (0 <= x) & (x <= 1)
        >>> x in Interval(0, 1)
        Traceback (most recent call last):
        ...
        TypeError: did not evaluate to a bool: None

        The result of 'in' is a bool, not a SymPy value

        >>> 1 in Interval(0, 2)
        True
        >>> _ is S.true
        False
        r   )ContainsT)strictFr   )r   r   r&   	_containsr   tfn)rS   re   r   cbs        rB   r   zSet.contains2  s]    F 	'd+NN5!a"H9E4%88F9HrA   c                 D    t        t        |       j                   d      )a  Test if ``other`` is an element of the set ``self``.

        This is an internal method that is expected to be overridden by
        subclasses of ``Set`` and will be called by the public
        :func:`Set.contains` method or the :class:`Contains` expression.

        Parameters
        ==========

        other: Sympified :class:`Basic` instance
            The object whose membership in ``self`` is to be tested.

        Returns
        =======

        Symbolic :class:`Boolean` or ``None``.

        A return value of ``None`` indicates that it is unknown whether
        ``other`` is contained in ``self``. Returning ``None`` from here
        ensures that ``self.contains(other)`` or ``Contains(self, other)`` will
        return an unevaluated :class:`Contains` expression.

        If not ``None`` then the returned value is a :class:`Boolean` that is
        logically equivalent to the statement that ``other`` is an element of
        ``self``. Usually this would be either ``S.true`` or ``S.false`` but
        not always.
        z
._contains)rZ   type__name__rd   s     rB   r   zSet._containsb  s!    8 "T$Z%8%8$9"DEErA   c                 x   t        |t              st        d|z        | |k(  ry| j                  }|du ryt	        |      r|j                  ry| j
                  du r|j
                  ry| j                  |      }||S |j                  |       }||S ddlm	}  || |      }||S | j                  |      | k(  ryy)a'  
        Returns True if ``self`` is a subset of ``other``.

        Examples
        ========

        >>> from sympy import Interval
        >>> Interval(0, 0.5).is_subset(Interval(0, 1))
        True
        >>> Interval(0, 1).is_subset(Interval(0, 1, left_open=True))
        False

        Unknown argument '%s'TFNr   )is_subset_sets)r   rE   r]   rI   r   rJ   _eval_is_subset_eval_is_supersetsympy.sets.handlers.issubsetr   rj   )rS   re   rI   retr   s        rB   	is_subsetzSet.is_subset  s     %%4u<== 5===tx U^^&5+>+> ""5)?J%%d+?J 	@T5)?J >>% D( )rA   c                      yz;Returns a fuzzy bool for whether self is a subset of other.Nr@   rd   s     rB   r   zSet._eval_is_subset      rA   c                      yr   r@   rd   s     rB   r   zSet._eval_is_superset  r   rA   c                 $    | j                  |      S )z/
        Alias for :meth:`is_subset()`
        r   rd   s     rB   issubsetzSet.issubset  rn   rA   c                 n    t        |t              r| |k7  xr | j                  |      S t        d|z        )a,  
        Returns True if ``self`` is a proper subset of ``other``.

        Examples
        ========

        >>> from sympy import Interval
        >>> Interval(0, 0.5).is_proper_subset(Interval(0, 1))
        True
        >>> Interval(0, 1).is_proper_subset(Interval(0, 1))
        False

        r   r   rE   r   r]   rd   s     rB   is_proper_subsetzSet.is_proper_subset  s7     eS!5=:T^^E%::4u<==rA   c                 `    t        |t              r|j                  |       S t        d|z        )a-  
        Returns True if ``self`` is a superset of ``other``.

        Examples
        ========

        >>> from sympy import Interval
        >>> Interval(0, 0.5).is_superset(Interval(0, 1))
        False
        >>> Interval(0, 1).is_superset(Interval(0, 1, left_open=True))
        True

        r   r   rd   s     rB   is_supersetzSet.is_superset  s.     eS!??4((4u<==rA   c                 $    | j                  |      S )z1
        Alias for :meth:`is_superset()`
        )r   rd   s     rB   
issupersetzSet.issuperset  rt   rA   c                 n    t        |t              r| |k7  xr | j                  |      S t        d|z        )a2  
        Returns True if ``self`` is a proper superset of ``other``.

        Examples
        ========

        >>> from sympy import Interval
        >>> Interval(0, 1).is_proper_superset(Interval(0, 0.5))
        True
        >>> Interval(0, 1).is_proper_superset(Interval(0, 1))
        False

        r   )r   rE   r   r]   rd   s     rB   is_proper_supersetzSet.is_proper_superset  s9     eS!5=<T%5%5e%<<4u<==rA   c                     ddl m}  ||       S )Nr   PowerSet)powersetr   )rS   r   s     rB   _eval_powersetzSet._eval_powerset   s    &~rA   c                 "    | j                         S )a  
        Find the Power set of ``self``.

        Examples
        ========

        >>> from sympy import EmptySet, FiniteSet, Interval

        A power set of an empty set:

        >>> A = EmptySet
        >>> A.powerset()
        {EmptySet}

        A power set of a finite set:

        >>> A = FiniteSet(1, 2)
        >>> a, b, c = FiniteSet(1), FiniteSet(2), FiniteSet(1, 2)
        >>> A.powerset() == FiniteSet(a, b, c, EmptySet)
        True

        A power set of an interval:

        >>> Interval(1, 2).powerset()
        PowerSet(Interval(1, 2))

        References
        ==========

        .. [1] https://en.wikipedia.org/wiki/Power_set

        )r   rR   s    rB   r   zSet.powerset  s    B ""$$rA   c                     | j                   S )z
        The (Lebesgue) measure of ``self``.

        Examples
        ========

        >>> from sympy import Interval, Union
        >>> Interval(0, 1).measure
        1
        >>> Union(Interval(0, 1), Interval(2, 3)).measure
        2

        )_measurerR   s    rB   measurezSet.measure'  s     }}rA   c                 "    | j                         S )a	  
        The kind of a Set

        Explanation
        ===========

        Any :class:`Set` will have kind :class:`SetKind` which is
        parametrised by the kind of the elements of the set. For example
        most sets are sets of numbers and will have kind
        ``SetKind(NumberKind)``. If elements of sets are different in kind than
        their kind will ``SetKind(UndefinedKind)``. See
        :class:`sympy.core.kind.Kind` for an explanation of the kind system.

        Examples
        ========

        >>> from sympy import Interval, Matrix, FiniteSet, EmptySet, ProductSet, PowerSet

        >>> FiniteSet(Matrix([1, 2])).kind
        SetKind(MatrixKind(NumberKind))

        >>> Interval(1, 2).kind
        SetKind(NumberKind)

        >>> EmptySet.kind
        SetKind()

        A :class:`sympy.sets.powerset.PowerSet` is a set of sets:

        >>> PowerSet({1, 2, 3}).kind
        SetKind(SetKind(NumberKind))

        A :class:`ProductSet` represents the set of tuples of elements of
        other sets. Its kind is :class:`sympy.core.containers.TupleKind`
        parametrised by the kinds of the elements of those sets:

        >>> p = ProductSet(FiniteSet(1, 2), FiniteSet(3, 4))
        >>> list(p)
        [(1, 3), (2, 3), (1, 4), (2, 4)]
        >>> p.kind
        SetKind(TupleKind(NumberKind, NumberKind))

        When all elements of the set do not have same kind, the kind
        will be returned as ``SetKind(UndefinedKind)``:

        >>> FiniteSet(0, Matrix([1, 2])).kind
        SetKind(UndefinedKind)

        The kind of the elements of a set are given by the ``element_kind``
        attribute of ``SetKind``:

        >>> Interval(1, 2).kind.element_kind
        NumberKind

        See Also
        ========

        NumberKind
        sympy.core.kind.UndefinedKind
        sympy.core.containers.TupleKind
        MatrixKind
        sympy.matrices.expressions.sets.MatrixSet
        sympy.sets.conditionset.ConditionSet
        Rationals
        Naturals
        Integers
        sympy.sets.fancysets.ImageSet
        sympy.sets.fancysets.Range
        sympy.sets.fancysets.ComplexRegion
        sympy.sets.powerset.PowerSet
        sympy.sets.sets.ProductSet
        sympy.sets.sets.Interval
        sympy.sets.sets.Union
        sympy.sets.sets.Intersection
        sympy.sets.sets.Complement
        sympy.sets.sets.EmptySet
        sympy.sets.sets.UniversalSet
        sympy.sets.sets.FiniteSet
        sympy.sets.sets.SymmetricDifference
        sympy.sets.sets.DisjointUnion
        )_kindrR   s    rB   kindzSet.kind8  s    f zz|rA   c                     | j                   S )ag  
        The boundary or frontier of a set.

        Explanation
        ===========

        A point x is on the boundary of a set S if

        1.  x is in the closure of S.
            I.e. Every neighborhood of x contains a point in S.
        2.  x is not in the interior of S.
            I.e. There does not exist an open set centered on x contained
            entirely within S.

        There are the points on the outer rim of S.  If S is open then these
        points need not actually be contained within S.

        For example, the boundary of an interval is its start and end points.
        This is true regardless of whether or not the interval is open.

        Examples
        ========

        >>> from sympy import Interval
        >>> Interval(0, 1).boundary
        {0, 1}
        >>> Interval(0, 1, True, False).boundary
        {0, 1}
        )	_boundaryrR   s    rB   boundaryzSet.boundary  s    > ~~rA   c                 B    t        | | j                        j                  S )a  
        Property method to check whether a set is open.

        Explanation
        ===========

        A set is open if and only if it has an empty intersection with its
        boundary. In particular, a subset A of the reals is open if and only
        if each one of its points is contained in an open interval that is a
        subset of A.

        Examples
        ========
        >>> from sympy import S
        >>> S.Reals.is_open
        True
        >>> S.Rationals.is_open
        False
        )ri   r   rI   rR   s    rB   is_openzSet.is_open  s    * D$--0999rA   c                 8    | j                   j                  |       S )a  
        A property method to check whether a set is closed.

        Explanation
        ===========

        A set is closed if its complement is an open set. The closedness of a
        subset of the reals is determined with respect to R and its standard
        topology.

        Examples
        ========
        >>> from sympy import Interval
        >>> Interval(0, 1).is_closed
        True
        )r   r   rR   s    rB   	is_closedzSet.is_closed  s    $ }}&&t,,rA   c                      | | j                   z   S )aN  
        Property method which returns the closure of a set.
        The closure is defined as the union of the set itself and its
        boundary.

        Examples
        ========
        >>> from sympy import S, Interval
        >>> S.Reals.closure
        Reals
        >>> Interval(0, 1).closure
        Interval(0, 1)
        r   rR   s    rB   closurezSet.closure       dmm##rA   c                      | | j                   z
  S )ax  
        Property method which returns the interior of a set.
        The interior of a set S consists all points of S that do not
        belong to the boundary of S.

        Examples
        ========
        >>> from sympy import Interval
        >>> Interval(0, 1).interior
        Interval.open(0, 1)
        >>> Interval(0, 1).boundary.interior
        EmptySet
        r   rR   s    rB   interiorzSet.interior  r   rA   c                     t               r?   r   rR   s    rB   r   zSet._boundary      !##rA   c                     t        d| z        )Nz(%s)._measurer   rR   s    rB   r   zSet._measure  s    !/D"899rA   c                      t        t              S r?   SetKindr   rR   s    rB   r   z	Set._kind      }%%rA   c                     t        |      } | j                  | j                  D cg c]  }|j                  |       c} S c c}w Nr   )r=   funcr   rY   )rS   precdpsargs       rB   _eval_evalfzSet._eval_evalf  s:    $tyytyyA399s9+ABBAs   A)re   rE   c                 $    | j                  |      S r?   rf   rd   s     rB   __add__zSet.__add__
      zz%  rA   c                 $    | j                  |      S r?   r   rd   s     rB   __or__z
Set.__or__  r   rA   c                 $    | j                  |      S r?   rl   rd   s     rB   __and__zSet.__and__  s    ~~e$$rA   c                     t        | |      S r?   )r   rd   s     rB   __mul__zSet.__mul__      $&&rA   c                     t        | |      S r?   r   rd   s     rB   __xor__zSet.__xor__  s    "4//rA   r(   c                 X    |j                   r|dk\  st        d|z        t        | g|z   S )Nr   z'%s: Exponent must be a positive Integer)
is_Integerr]   r   )rS   r(   s     rB   __pow__zSet.__pow__  s1    3!8FLMMD6#:&&rA   c                     t        | |      S r?   rv   rd   s     rB   __sub__zSet.__sub__$  r   rA   c                 p    t        |      }| j                  |      }t        |   }|t        d|z        |S )Nzdid not evaluate to a bool: %r)r%   r   r   	TypeError)rS   re   r   r   s       rB   __contains__zSet.__contains__(  s?    NN5!F9 <q@AArA   )Hr   
__module____qualname____doc__	__slots__	is_numberis_iterableis_intervalis_FiniteSetis_Intervalis_ProductSetis_UnionrF   r   __annotations__rG   rH   is_ComplexRegionrI   rJ   propertyr2   rT   staticmethodra   rf   rj   rm   rq   rs   rz   r   r   r   rW   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   NotImplementedr   r   r   r   r  r   r  r  r
  r@   rA   rB   rE   rE   ,   s    IIKKLKMH!%OY%!%OY%#M9#Hi#M9#	 "'#;   "6)*%3('*""1H00G    6 6    6 6.`F</b
%>&>('>&!%F    R Rh  @ : :, - -& $ $  $ $  $ $ : :&C %&7! 8! %&7! 8! %&7% 8% %&7' 8' %&70 80 eT]O^4' 5'
 %&7' 8'rA   rE   c                       e Zd ZdZdZd Zed        Zd Zd Z	d Z
ed        Zed	        Zd
 Zed        Zed        Zed        Zd Zd Zd Zy)r   a  
    Represents a Cartesian Product of Sets.

    Explanation
    ===========

    Returns a Cartesian product given several sets as either an iterable
    or individual arguments.

    Can use ``*`` operator on any sets for convenient shorthand.

    Examples
    ========

    >>> from sympy import Interval, FiniteSet, ProductSet
    >>> I = Interval(0, 5); S = FiniteSet(1, 2, 3)
    >>> ProductSet(I, S)
    ProductSet(Interval(0, 5), {1, 2, 3})

    >>> (2, 2) in ProductSet(I, S)
    True

    >>> Interval(0, 1) * Interval(0, 1) # The unit square
    ProductSet(Interval(0, 1), Interval(0, 1))

    >>> coin = FiniteSet('H', 'T')
    >>> set(coin**2)
    {(H, H), (H, T), (T, H), (T, T)}

    The Cartesian product is not commutative or associative e.g.:

    >>> I*S == S*I
    False
    >>> (I*I)*I == I*(I*I)
    False

    Notes
    =====

    - Passes most operations down to the argument sets

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Cartesian_product
    Tc                    t        |      dk(  rCt        |d         r5t        |d   t        t        f      st        ddd       t        |d         }|D cg c]  }t        |       }}t        d |D              st        d      t        |      dk(  rt        d	      S t        j                  |v rt        j                  S t        j                  | g|i |S c c}w )
Nr   r   zX
ProductSet(iterable) is deprecated. Use ProductSet(*iterable) instead.
                rL   zdeprecated-productset-iterablerN   c              3   <   K   | ]  }t        |t                y wr?   r   rE   r}   r   s     rB   r   z%ProductSet.__new__.<locals>.<genexpr>q  s     4!:a%4   z-Arguments to ProductSet should be of type Setr@   )r   r7   r   rE   setr3   tupler&   allr	  r   r   rp   r
   __new__)clsr   assumptionsr   s       rB   r$  zProductSet.__new__d  s    t9>htAw/
47SRUJ8W% */+K a>D$()q
))4t44KLL t9>R= ::::}}S747;77 *s   Cc                     | j                   S r?   r   rR   s    rB   r   zProductSet.sets}      yyrA   c                 >    fdt         | j                         S )Nc              3   v   K   | D ]-  }|j                   r |j                        E d {    *| / y 7 wr?   )r  r   )r   r   _flattens     rB   r,  z$ProductSet.flatten.<locals>._flatten  s7      ??'///G	/s   '979)r   r   )rS   r,  s    @rB   flattenzProductSet.flatten  s    	 8DII.//rA   c           	      &   |j                   ryt        |t              r!t        |      t        | j                        k7  rt
        j                  S t        t        | j                  |      D cg c]  \  }}|j                  |       c}} S c c}}w )a8  
        ``in`` operator for ProductSets.

        Examples
        ========

        >>> from sympy import Interval
        >>> (2, 3) in Interval(0, 5) * Interval(0, 5)
        True

        >>> (10, 10) in Interval(0, 5) * Interval(0, 5)
        False

        Passes operation on to constituent sets
        N)
	is_Symbolr   r   r   r   r   r1   r,   r   r   )rS   elementr   es       rB   r   zProductSet._contains  sf      '5)S\S^-K77Ns499g/FGtq!QZZ]GHHGs   -B
c           	      @   |D cg c]  }t        |       }}t        |      t        | j                        k7  st        d |D              st	        d      t        t        | j                  |      D cg c]  \  }}|j                  |       c}} S c c}w c c}}w )Nc              3   4   K   | ]  }|j                     y wr?   )r/  r}   r~   s     rB   r   z+ProductSet.as_relational.<locals>.<genexpr>  s      5. !5.   z/number of symbols must match the number of sets)r%   r   r   r#  r]   r,   r   as_relational)rS   r!   r   r~   s       rB   r6  zProductSet.as_relational  s    (/018A;00w<3tyy>) 5.%,5. 2.AC CC		74KLDAqQ__Q'LMM 1
 Ms   B5B
c                 L     t         fdt         j                        D         S )Nc              3   n   K   | ]+  \  }t        fd t        j                        D          - yw)c              3   d   K   | ]'  \  }}|k7  r||j                   z   n|j                    ) y wr?   r   )r}   jr   r~   s      rB   r   z1ProductSet._boundary.<locals>.<genexpr>.<genexpr>  s6      $B$(Aq 781fA

N!**$L $Bs   -0N)r   r   r   )r}   ar~   rS   s     @rB   r   z'ProductSet._boundary.<locals>.<genexpr>  s=      B$(Aq " $B,5dii,@$B C Bs   15)rc   r   r   rR   s   `rB   r   zProductSet._boundary  s*     B,5dii,@B C 	CrA   c                 :    t        d | j                  D              S )a  
        A property method which tests whether a set is iterable or not.
        Returns True if set is iterable, otherwise returns False.

        Examples
        ========

        >>> from sympy import FiniteSet, Interval
        >>> I = Interval(0, 1)
        >>> A = FiniteSet(1, 2, 3, 4, 5)
        >>> I.is_iterable
        False
        >>> A.is_iterable
        True

        c              3   4   K   | ]  }|j                     y wr?   r  r}   r!  s     rB   r   z)ProductSet.is_iterable.<locals>.<genexpr>       8s3??8r5  r#  r   rR   s    rB   r  zProductSet.is_iterable  s    $ 8dii888rA   c                 &    t        | j                   S )z
        A method which implements is_iterable property method.
        If self.is_iterable returns True (both constituent sets are iterable),
        then return the Cartesian Product. Otherwise, raise TypeError.
        )r4   r   rR   s    rB   __iter__zProductSet.__iter__  s     ##rA   c                 :    t        d | j                  D              S )Nc              3   4   K   | ]  }|j                     y wr?   rI   r  s     rB   r   z&ProductSet.is_empty.<locals>.<genexpr>  s     6q

6r5  )r   r   rR   s    rB   rI   zProductSet.is_empty  s    6DII666rA   c                 h    t        d | j                  D              }t        | j                  |g      S )Nc              3   4   K   | ]  }|j                     y wr?   rJ   r  s     rB   r   z+ProductSet.is_finite_set.<locals>.<genexpr>       B1qBr5  r   r   r   rI   rS   
all_finites     rB   rJ   zProductSet.is_finite_set  *    B		BB

344rA   c                 J    d}| j                   D ]  }||j                  z  } |S Nr   )r   r   )rS   r   r   s      rB   r   zProductSet._measure  s-     	!Aqyy G	!rA   c                 F    t        t        d | j                  D               S )Nc              3   H   K   | ]  }|j                   j                    y wr?   )r   element_kindr4  s     rB   r   z#ProductSet._kind.<locals>.<genexpr>  s     "J1166#6#6"Js    ")r   r   r   rR   s    rB   r   zProductSet._kind  s    y"J		"JKLLrA   c                 >    t        d d | j                  D              S )Nc                     | |z  S r?   r@   r;  r   s     rB   rC   z$ProductSet.__len__.<locals>.<lambda>  s
    1Q3 rA   c              3   2   K   | ]  }t        |        y wr?   )r   r  s     rB   r   z%ProductSet.__len__.<locals>.<genexpr>  s     (CAQ(C   )r   r   rR   s    rB   __len__zProductSet.__len__  s    &(C(CDDrA   c                 ,    t        | j                        S r?   rA  rR   s    rB   __bool__zProductSet.__bool__      499~rA   N)r   r  r  r  r  r$  r  r   r-  r   r6  r   r  rC  rI   rJ   r   r   rY  r[  r@   rA   rB   r   r   3  s    -\ M82  0I0N C C
 9 9&$ 7 7 5 5  MErA   r   c                   ^   e Zd ZdZdZddZed        Zed        Zed        Z	ed        Z
ed        Zed	        Zed
        Zed        Zed        Zed        Zed        Zed        Zed        Zd Zed        Zd Zd Zed        Zd ZddZd Zd Zed        Zed        Zd Z y) r   a  
    Represents a real interval as a Set.

    Usage:
        Returns an interval with end points ``start`` and ``end``.

        For ``left_open=True`` (default ``left_open`` is ``False``) the interval
        will be open on the left. Similarly, for ``right_open=True`` the interval
        will be open on the right.

    Examples
    ========

    >>> from sympy import Symbol, Interval
    >>> Interval(0, 1)
    Interval(0, 1)
    >>> Interval.Ropen(0, 1)
    Interval.Ropen(0, 1)
    >>> Interval.Ropen(0, 1)
    Interval.Ropen(0, 1)
    >>> Interval.Lopen(0, 1)
    Interval.Lopen(0, 1)
    >>> Interval.open(0, 1)
    Interval.open(0, 1)

    >>> a = Symbol('a', real=True)
    >>> Interval(0, a)
    Interval(0, a)

    Notes
    =====
    - Only real end points are supported
    - ``Interval(a, b)`` with $a > b$ will return the empty set
    - Use the ``evalf()`` method to turn an Interval into an mpmath
      ``mpi`` interval instance

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Interval_%28mathematics%29
    Tc           	      2   t        |      }t        |      }t        |      }t        |      }t        d ||fD              st        d|d|      t        t	        d ||||z
  fD                    rt        d      t        ||      rt        j                  S ||z
  j                  rt        j                  S ||k(  r|s|rt        j                  S ||k(  rC|sA|s?|t        j                  u s|t        j                  u rt        j                  S t        |      S |t        j                  u rt        }|t        j                  u rt        }|t        j                  k(  s|t        j                  k(  rt        j                  S t        j                  | ||||      S )Nc              3   l   K   | ],  }t        |t        t              t        t              f       . y wr?   )r   r   r0   r1   )r}   r;  s     rB   r   z#Interval.__new__.<locals>.<genexpr>  s,      . a$t*d5k!:; .s   24z>left_open and right_open can have only true/false values, got z and c              3   4   K   | ]  }|j                     y wr?   )is_extended_realr4  s     rB   r   z#Interval.__new__.<locals>.<genexpr>"  s     Saq11Sr5  z$Non-real intervals are not supported)r%   r#  rZ   r   r   r]   r   r   rp   is_negativer^   NegativeInfinityr   r0   r
   r$  )r%  startend	left_open
right_opens        rB   r$  zInterval.__new__  s_   smY'	j)
 .,. .%#,j:; ;
 YSE3E	;RSSTCDD e::Ek&&::%<Y*::%<j

"eq/A/A&Azz!S>! A&&&I!**JAJJ#););";::}}S%iDDrA   c                      | j                   d   S )z
        The left end point of the interval.

        This property takes the same value as the ``inf`` property.

        Examples
        ========

        >>> from sympy import Interval
        >>> Interval(0, 1).start
        0

        r   _argsrR   s    rB   rd  zInterval.start<       zz!}rA   c                      | j                   d   S )z
        The right end point of the interval.

        This property takes the same value as the ``sup`` property.

        Examples
        ========

        >>> from sympy import Interval
        >>> Interval(0, 1).end
        1

        r   ri  rR   s    rB   re  zInterval.endM  rk  rA   c                      | j                   d   S )a  
        True if interval is left-open.

        Examples
        ========

        >>> from sympy import Interval
        >>> Interval(0, 1, left_open=True).left_open
        True
        >>> Interval(0, 1, left_open=False).left_open
        False

           ri  rR   s    rB   rf  zInterval.left_open^  rk  rA   c                      | j                   d   S )a  
        True if interval is right-open.

        Examples
        ========

        >>> from sympy import Interval
        >>> Interval(0, 1, right_open=True).right_open
        True
        >>> Interval(0, 1, right_open=False).right_open
        False

           ri  rR   s    rB   rg  zInterval.right_openo  rk  rA   c                      | ||dd      S )z.Return an interval including neither boundary.Tr@   r%  r;  r   s      rB   openzInterval.open  s     1at$$rA   c                      | ||dd      S )z3Return an interval not including the left boundary.TFr@   rr  s      rB   LopenzInterval.Lopen  s     1au%%rA   c                      | ||dd      S )z4Return an interval not including the right boundary.FTr@   rr  s      rB   RopenzInterval.Ropen  s     1a%%rA   c                     | j                   S r?   rd  rR   s    rB   r   zInterval._inf      zzrA   c                     | j                   S r?   re  rR   s    rB   r   zInterval._sup      xxrA   c                     | j                   S r?   ry  rR   s    rB   leftzInterval.left  rz  rA   c                     | j                   S r?   r|  rR   s    rB   rightzInterval.right  r}  rA   c                     | j                   s| j                  r$| j                  | j                  k\  }t	        |      S | j                  | j                  kD  }t	        |      S r?   )rf  rg  rd  re  r   )rS   conds     rB   rI   zInterval.is_empty  sM    >>T__::)D $ ::(D$rA   c                 .    | j                   j                  S r?   )r   is_zerorR   s    rB   rJ   zInterval.is_finite_set  s    ||###rA   c                    |t         j                  k(  rnt        t         j                  | j                  d| j
                         }t        | j                  t         j                  | j                   d      }t        ||      S t        |t              r,|j                  D cg c]  }|j                  s| }}|g k(  ry t        j                  | |      S c c}w NT)r   r   r   rc  rd  rf  re  r^   rg  rc   r   r   r   r  rE   r   )rS   re   r;  r   mnumss         rB   r   zInterval._complement  s    AGG++TZZ4>>13A1::4??/BDIAA;eY'$zz9!Q[[A9D9rztU++	 :s    C2Cc                     | j                   | j                  fD cg c]   }t        |      t        j                  k7  r|" }}t        | S c c}w r?   )rd  re  absr   r^   r   )rS   pfinite_pointss      rB   r   zInterval._boundary  sK    %)ZZ$: 2qFajj0  2 2-((2s   %A
c                    t        |t              r?|t        j                  u s-|j                  du s|j                  t        j                        rt        S | j                  t        j                  u r;| j                  t        j                  u r|j                  t        |j                     S t               }| j                  |      j                  ||      S )NF)r   r   r   NaNis_realhasComplexInfinityr1   rd  rc  re  r^   r   r#   r6  subs)rS   re   ds      rB   r   zInterval._contains  s    5$'5AEE>}}%13D3D)E ::+++AJJ0F}}(5==))G!!!$))!U33rA   c                     t        |      }| j                  r|| j                  k  }n|| j                  k  }| j                  r| j                  |k  }n| j                  |k  }t        ||      S )zARewrite an interval in terms of inequalities and logic operators.)r&   rg  re  rf  rd  r,   )rS   r   r  r  s       rB   r6  zInterval.as_relational  sZ    AJ??LEME>>::>D::?D4rA   c                 4    | j                   | j                  z
  S r?   )re  rd  rR   s    rB   r   zInterval._measure  s    xx$**$$rA   c                      t        t              S r?   )r   r	   rR   s    rB   r   zInterval._kind  s    z""rA   c                     t        t        | j                  j                  |            t        | j                  j                  |                  S r?   )r;   r<   rd  r   re  rS   r   s     rB   to_mpizInterval.to_mpi  s<    3tzz--d34$$T*+- 	-rA   c                     t        | j                  j                  |      | j                  j                  |      | j                  | j
                        S )N)rf  rg  )r   r  _evalfr  rf  rg  r  s     rB   r   zInterval._eval_evalf  s@    		((.

0A0A$0GnnB 	BrA   c                     | j                   j                  }|| j                  j                  z  }||j                   j                  z  }||j                  j                  z  }|S r?   )rd  rX   re  )rS   re   rX   s      rB   _is_comparablezInterval._is_comparable  sS    

00///222000rA   c                 n    | j                   t        j                  u xs | j                   t        d      k(  S )z;Return ``True`` if the left endpoint is negative infinity. z-inf)r  r   rc  r   rR   s    rB   is_left_unboundedzInterval.is_left_unbounded  s+     yyA...L$))uV}2LLrA   c                 n    | j                   t        j                  u xs | j                   t        d      k(  S )z<Return ``True`` if the right endpoint is positive infinity. z+inf)r  r   r^   r   rR   s    rB   is_right_unboundedzInterval.is_right_unbounded  s)     zzQZZ'F4::v+FFrA   c                 ~    t        |t              s-t        |t              rt        S t        |t              ry t        S y r?   )r   r   r   r1   rE   rd   s     rB   _eval_EqzInterval._eval_Eq  s2    %*%+E3'L +rA   N)FF)5   )!r   r  r  r  r  r$  r  rd  re  rf  rg  classmethodrs  ru  rw  r   r   r  r  rI   rJ   r   r   r   r6  r   r   r  r   r  r  r  r  r@   rA   rB   r   r     s   (R K&EP             % % & & & &             $ $, ) )
4  % %#-B M M G GrA   r   c                       e Zd ZdZdZed        Zed        Zd Zed        Z	d Z
ed        Zed	        Zed
        Zed        Zed        Zd Zed        Zd Zd Zd Zed        Zd Zy)rc   a  
    Represents a union of sets as a :class:`Set`.

    Examples
    ========

    >>> from sympy import Union, Interval
    >>> Union(Interval(1, 2), Interval(3, 4))
    Union(Interval(1, 2), Interval(3, 4))

    The Union constructor will always try to merge overlapping intervals,
    if possible. For example:

    >>> Union(Interval(1, 2), Interval(2, 3))
    Interval(1, 3)

    See Also
    ========

    Intersection

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Union_%28set_theory%29
    Tc                 "    t         j                  S r?   r   rp   rR   s    rB   identityzUnion.identity!      zzrA   c                 "    t         j                  S r?   r   UniversalSetrR   s    rB   zeroz
Union.zero%      ~~rA   c                 <   |j                  dt        j                        }t        |      }|r%t	        | j                  |            }t        |      S t	        t        |t        j                              }t        j                  | g| }t        |      |_        |S )Nr   )getr   r   r%   r   _new_args_filtersimplify_unionr    rE   ra   r
   r$  	frozenset_argset)r%  r   kwargsr   objs        rB   r$  zUnion.__new__)  s    ::j*;*D*DE ~ ,,T23D!$''GD#"2"234mmC'$'o
rA   c                     | j                   S r?   ri  rR   s    rB   r   z
Union.args:  rz  rA   c                 @    t        fd| j                  D              S )Nc              3   @   K   | ]  }|j                          y wr?   rz   )r}   r   ry   s     rB   r   z$Union._complement.<locals>.<genexpr>@  s     FqALL2F   )ri   r   rx   s    `rB   r   zUnion._complement>  s    FDIIFFFrA   c                 `    t        | j                  D cg c]  }|j                   c} S c c}w r?   )r+   r   rW   rS   r!  s     rB   r   z
Union._infB  %     		2SWW2332   +c                 `    t        | j                  D cg c]  }|j                   c} S c c}w r?   )r*   r   r   r  s     rB   r   z
Union._supH  r  r  c                 :    t        d | j                  D              S )Nc              3   4   K   | ]  }|j                     y wr?   rF  r?  s     rB   r   z!Union.is_empty.<locals>.<genexpr>P  s     ;#;r5  r   r   rR   s    rB   rI   zUnion.is_emptyN  s    ;;;;rA   c                 :    t        d | j                  D              S )Nc              3   4   K   | ]  }|j                     y wr?   rI  r?  s     rB   r   z&Union.is_finite_set.<locals>.<genexpr>T  s     @s**@r5  r  rR   s    rB   rJ   zUnion.is_finite_setR  s    @dii@@@rA   c                      j                   D cg c]  }t        |      |f }}d}d}|r||t        d |D              z  z  } fd|D        }|D cg c]  \  }}|j                  dk7  s||f }}}g }g }|D ]/  }	|	d   |v r|j	                  |	d          |j	                  |	       1 |}|dz  }|r|S c c}w c c}}w )Nr   r   c              3   :   K   | ]  \  }}|j                     y wr?   )r   )r}   sosinters      rB   r   z!Union._measure.<locals>.<genexpr>h  s     #Ijc5EMM#Is   c              3      K   | ];  \  }}j                   D ]'  }||vr!|t        |      z   |j                  |      f ) = y wr?   )r   r   rj   )r}   r  rm   newsetrS   s       rB   r   z!Union._measure.<locals>.<genexpr>l  sU      *)\		*6<S( 9V,,f.>.>|.LM *M *s   AA)r   r   sumr   r   )
rS   r   r   r   parityr  r  sos_list	sets_list_sets
   `         rB   r   zUnion._measureV  s     ,0995a1q!55v#ID#I IIIG*-1*D
 48NZS%5==A;MS%LNDN HI +7h&OODG,$$T*+ D bLF3 4 ; 6 Os   B9B>,B>c                     t        d | j                  D              s
t               S t        fdD              rd   S t        t              S )Nc              3   Z   K   | ]#  }|t         j                  us|j                   % y wr?   )r   rp   r   r}   r   s     rB   r   zUnion._kind.<locals>.<genexpr>  s     M3s!**7LchhM   ++c              3   .   K   | ]  }|d    k(    ywr   Nr@   r}   r~   kindss     rB   r   zUnion._kind.<locals>.<genexpr>       .1eAh.   r   )r"  r   r   r#  r   rS   r  s    @rB   r   zUnion._kind  sD    M$))MM9...8O=))rA   c           
      j      fd}t        t        |t        t         j                                     S )Nc                     j                   |    j                  }t        j                         D ]  \  }}|| k7  s||j                  z
  } |S )z8 The boundary of set i minus interior of all other sets )r   r   r   r   )r~   r   r:  r;  rS   s       rB   boundary_of_setz(Union._boundary.<locals>.boundary_of_set  sM    		!%%A!$)), '16AJJA' HrA   )rc   mapr   r   r   )rS   r  s   ` rB   r   zUnion._boundary  s)    	 c/5TYY+@ABBrA   c                 j    t        | j                  D cg c]  }|j                  |       c} S c c}w r?   )r-   r   r   )rS   re   r   s      rB   r   zUnion._contains  s(    tyy9!AJJu%9::9   0c                 @    t        fd| j                  D              S )Nc              3   @   K   | ]  }|j                          y wr?   r   )r}   r   re   s     rB   r   z"Union.is_subset.<locals>.<genexpr>  s     ?U+?r  r  rd   s    `rB   r   zUnion.is_subset      ?TYY???rA   c                 @   t        | j                        dk(  rt        d | j                  D              r| j                  \  }}|j                  |j                  k(  r|j
                  r|j                  rw|j                  r||j                  kD  n||j                  k\  }|j
                  r||j                  k  n||j                  k  }t        ||j                        }t        |||      S t        | j                  D cg c]  }|j                  |       c} S c c}w )z<Rewrite a Union in terms of equalities and logic operators. rn  c              3   <   K   | ]  }t        |t                y wr?   )r   r   r4  s     rB   r   z&Union.as_relational.<locals>.<genexpr>  s     ?Jq(+?r   )r   r   r#  r   rW   rg  rf  r   r,   r-   r6  )rS   symbolr;  r   mincondmaxcondnecondr~   s           rB   r6  zUnion.as_relational  s    		Na?TYY?? 99DAquu~!,,1;;,-KK&155.Vquu_,-LL&155.foFAEE*67G44TYY?AOOF+?@@?s   ?Dc                 :    t        d | j                  D              S )Nc              3   4   K   | ]  }|j                     y wr?   r>  r  s     rB   r   z$Union.is_iterable.<locals>.<genexpr>  r@  r5  r#  r   rR   s    rB   r  zUnion.is_iterable      8dii888rA   c                 4    t        d | j                  D         S )Nc              3   2   K   | ]  }t        |        y wr?   iterr  s     rB   r   z!Union.__iter__.<locals>.<genexpr>  s     ;#DI;rX  )r6   r   rR   s    rB   rC  zUnion.__iter__  s    ;;<<rA   N)r   r  r  r  r  r  r  r  r$  r   r   r   r   rI   rJ   r   r   r   r   r   r6  r  rC  r@   rA   rB   rc   rc     s    4 H   "  G 4 4
 4 4
 < < A A ) )V* C C;@A" 9 9=rA   rc   c                       e Zd ZdZdZed        Zed        ZdddZed        Z	ed	        Z
ed
        Zd Zed        Zed        Zd Zd Zed        Zd Zy)ri   a  
    Represents an intersection of sets as a :class:`Set`.

    Examples
    ========

    >>> from sympy import Intersection, Interval
    >>> Intersection(Interval(1, 3), Interval(2, 4))
    Interval(2, 3)

    We often use the .intersect method

    >>> Interval(1,3).intersect(Interval(2,4))
    Interval(2, 3)

    See Also
    ========

    Union

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Intersection_%28set_theory%29
    Tc                 "    t         j                  S r?   r  rR   s    rB   r  zIntersection.identity  r  rA   c                 "    t         j                  S r?   r  rR   s    rB   r  zIntersection.zero  r  rA   Nr   c          	      V   |t         j                  }t        t        t	        t        |                        }|r%t        | j                  |            }t        |      S t        t        |t        j                              }t        j                  | g| }t        |      |_        |S r?   )r   r   r   r    r!  r%   r  simplify_intersectionrE   ra   r
   r$  r  r  )r%  r   r   r  s       rB   r$  zIntersection.__new__  s    (11H GC/01 ,,T23D(..GD#"2"234mmC'$'o
rA   c                     | j                   S r?   ri  rR   s    rB   r   zIntersection.args  rz  rA   c                 :    t        d | j                  D              S )Nc              3   4   K   | ]  }|j                     y wr?   r>  r  s     rB   r   z+Intersection.is_iterable.<locals>.<genexpr>  r@  r5  )anyr   rR   s    rB   r  zIntersection.is_iterable  r  rA   c                 >    t        d | j                  D              ryy )Nc              3   4   K   | ]  }|j                     y wr?   rI  r  s     rB   r   z-Intersection.is_finite_set.<locals>.<genexpr>  s     ;#C%%;r5  T)r   r   rR   s    rB   rJ   zIntersection.is_finite_set  s    ;;; <rA   c                     t        d | j                  D              st        t              S t	        fdD              rd   S t               S )Nc              3   Z   K   | ]#  }|t         j                  us|j                   % y wr?   )r   r  r   r  s     rB   r   z%Intersection._kind.<locals>.<genexpr>  s     Q3s!..7PchhQr  c              3   .   K   | ]  }|d    k(    ywr  r@   r  s     rB   r   z%Intersection._kind.<locals>.<genexpr>  r  r  r   )r"  r   r   r   r#  r  s    @rB   r   zIntersection._kind  sD    Q$))QQ=))...8O9rA   c                     t               r?   r   rR   s    rB   r   zIntersection._inf  r   rA   c                     t               r?   r   rR   s    rB   r   zIntersection._sup  r   rA   c                 j    t        | j                  D cg c]  }|j                  |       c} S c c}w r?   )r,   r   r   )rS   re   r!  s      rB   r   zIntersection._contains	  s(    DII>SS\\%(>??>r  c              #     K   t        | j                  d       }d}|d   |d    z   }g g }}|D ]$  }d }	 t        |      }||j	                  |       & |j                  t               ||z   D ]<  }t        | j                        |hz
  }	t        |	ddi}
d}|D ]  }	 ||
v r|  |s< y  |s|st        d      t        d      y # t        $ r |j	                  |       Y w xY w# t        $ r d}Y \w xY ww)Nc                     | j                   S r?   r>  r   s    rB   rC   z'Intersection.__iter__.<locals>.<lambda>  s
    amm rA   FTkeyr   z)None of the constituent sets are iterableziThe computation had not completed because of the undecidable set membership is found in every candidates.)r5   r   r   r	  r   sortr!  ri   )rS   	sets_sift	completed
candidatesfinite_candidatesothers	candidatelengthr   
other_setsre   r   s               rB   rC  zIntersection.__iter__  sG    $;<		t_y6
$&6# 	4IF)Y !!((3	4 	3'"V+ 	ATYY1#-J *=u=EI &&Ez& 	  KLLKL L )  )i() ! & %I&sX   0C:C	C:AC:C)&C:+C:	C&#C:%C&&C:)C74C:6C77C:c                    t        | d d      \  }}|sy|D cg c]  }t        |       }}t        d |t                     t               }D ]F  t        fd| D              }|du r|j	                         |/|D ]  }|j                          H t        d |t                     }|D cg c]
  }t        |  }	}|r}|D ]t  t        fd|	D              }
|
du r|j	                         |
/t        |      D ]&  \  }}|v s|j                         t        | |	|<   ( |j                          n n|r}t        |      st               g}|r|D cg c]  }||z  	 }}|t               gk(  rt        j                  S |D cg c]
  }t        |  }}t        d	 |t                     fd
}|D cg c]  } ||      r| }}|rct        | }|t        j                  u rt        j                  S |j                  r|j                  |j                         n|j!                  |       t#        |      dk(  r|d   S t        |ddiS c c}w c c}w c c}w c c}w c c}w )z>Simplify intersection of one or more FiniteSets and other setsc                     | j                   S r?   )r  r	  s    rB   rC   z2Intersection._handle_finite_sets.<locals>.<lambda>7  s
    q~~ rA   TbinaryNc                     | |z  S r?   r@   rV  s     rB   rC   z2Intersection._handle_finite_sets.<locals>.<lambda>?  
    1q5 rA   c              3   @   K   | ]  }|j                          y wr?   r   r}   r   r1  s     rB   r   z3Intersection._handle_finite_sets.<locals>.<genexpr>E  s     :ajjm:r  c                     | |z  S r?   r@   rV  s     rB   rC   z2Intersection._handle_finite_sets.<locals>.<lambda>V  s
    !a% rA   c              3   @   K   | ]  }|j                          y wr?   r  r  s     rB   r   z3Intersection._handle_finite_sets.<locals>.<genexpr>]  s      C1A Cr  c                     | |z  S r?   r@   rV  s     rB   rC   z2Intersection._handle_finite_sets.<locals>.<lambda>  r  rA   c                 .     t         fdD              S )Nc              3   R   K   | ]  }t        j                  |               y wr?   r   )r}   r1  r   s     rB   r   zEIntersection._handle_finite_sets.<locals>.<lambda>.<locals>.<genexpr>  s     $U1Z

1%>$Us   $')r#  )r   all_elementss   `rB   rC   z2Intersection._handle_finite_sets.<locals>.<lambda>  s    $U$U!U rA   r   r   r   F)r5   r!  r   r   adddiscardr   r   remover#  r   rp   ri   rF   extendr   r   r   )r   fs_argsr  fsfs_setsdefiniteinallr   fs_elements
fs_symsetsinfsr   r   is_redundantr   restr#  r1  s                   @@rB   _handle_finite_setsz Intersection._handle_finite_sets2  sr   
 t%=dK  &--r3r7--0'35A 5 	!A:T::E}Q   !AIIaL!	!$ /#%@ .55im5
5    C
 CC4<LLO# )' 2 :16HHQK,5qMJqM	:
  &&q) ! ( 7|ugG /67rH}7G7sug::'./!	1// 0'35AU#;<?!;;(D qzz!zz!##DII&D!t9>7N666m .6 6< 8
 0 <s#   I5I!I"I'I,I,c                 j    t        | j                  D cg c]  }|j                  |       c} S c c}w )zBRewrite an Intersection in terms of equalities and logic operators)r,   r   r6  )rS   r  r!  s      rB   r6  zIntersection.as_relational  s*    $))D3S&&v.DEEDr  )r   r  r  r  rF   r  r  r  r$  r   r  rJ   r   r   r   r   rC  r  r2  r6  r@   rA   rB   ri   ri     s    2 O    '+ $   9 9   $ $ $ $@$LL a7 a7FFrA   ri   c                   d    e Zd ZdZdZddZed        Zd Zd Z	d Z
ed        Zed	        Zd
 Zy)rw   a  Represents the set difference or relative complement of a set with
    another set.

    $$A - B = \{x \in A \mid x \notin B\}$$


    Examples
    ========

    >>> from sympy import Complement, FiniteSet
    >>> Complement(FiniteSet(0, 1, 2), FiniteSet(1))
    {0, 2}

    See Also
    =========

    Intersection, Union

    References
    ==========

    .. [1] https://mathworld.wolfram.com/ComplementSet.html
    Tc                     t        t        ||f      \  }}|rt        j                  ||      S t	        j
                  | ||      S r?   )r  r%   rw   r   r
   r$  r%  r;  r   r   s       rB   r$  zComplement.__new__  s?    8aV$1$$Q**}}S!Q''rA   c                     |t         j                  k(  s j                  |      rt         j                  S t	        |t
              rt         fd|j                  D         S |j                         }||S t         |d      S )z2
        Simplify a :class:`Complement`.

        c              3   @   K   | ]  }|j                          y wr?   r  )r}   r   As     rB   r   z$Complement.reduce.<locals>.<genexpr>  s     !Ba!,,q/!Br  Fr   )
r   r  r   rp   r   rc   ri   r   r   rw   r9  Bresults   `  rB   r   zComplement.reduce  sl     !++a.::a!B166!BCCq!MaU33rA   c                     | j                   d   }| j                   d   }t        |j                  |      t        |j                  |                  S )Nr   r   )r   r,   r   r.   )rS   re   r9  r;  s       rB   r   zComplement._contains  s@    IIaLIIaL1::e$c!**U*;&<==rA   c                     | j                   \  }}|j                  |      }t        |j                  |            }t        ||      S )zGRewrite a complement in terms of equalities and logic
        operators)r   r6  r.   r,   rS   r  r9  r;  A_relB_rels         rB   r6  zComplement.as_relational  s?     yy1'AOOF+,5%  rA   c                 4    | j                   d   j                  S Nr   )r   r   rR   s    rB   r   zComplement._kind  s    yy|   rA   c                 8    | j                   d   j                  ryy )Nr   T)r   r  rR   s    rB   r  zComplement.is_iterable  s    99Q<## $rA   c                 h    | j                   \  }}|j                  }|du ry|du r|j                  ryy y )NTF)r   rJ   )rS   r9  r;  a_finites       rB   rJ   zComplement.is_finite_set  s<    yy1??t1?? $3rA   c              #   J   K   | j                   \  }}|D ]  }||vr|  y wr?   r(  )rS   r9  r;  r;  s       rB   rC  zComplement.__iter__  s1     yy1 	AzG		s   !#NT)r   r  r  r  rH   r$  r  r   r   r6  r   r  r  rJ   rC  r@   rA   rB   rw   rw     sc    0 M( 4 4">
!!    rA   rw   c                       e Zd ZdZdZdZdZe eddd      d               Z	ed        Z
d	 Zd
 Zd Zd Zd Zed        Zd Zd Zd Zy)rp   a  
    Represents the empty set. The empty set is available as a singleton
    as ``S.EmptySet``.

    Examples
    ========

    >>> from sympy import S, Interval
    >>> S.EmptySet
    EmptySet

    >>> Interval(1, 2).intersect(S.EmptySet)
    EmptySet

    See Also
    ========

    UniversalSet

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Empty_set
    TrK   rL   rM   rN   c                      yr  r@   rR   s    rB   rT   zEmptySet.is_EmptySet  rU   rA   c                      yrC  r@   rR   s    rB   r   zEmptySet._measure!      rA   c                     t         S r?   r1   rd   s     rB   r   zEmptySet._contains%      rA   c                     t         S r?   rN  rS   r  s     rB   r6  zEmptySet.as_relational(  rO  rA   c                      yrC  r@   rR   s    rB   rY  zEmptySet.__len__+  s    rA   c                     t        g       S r?   r  rR   s    rB   rC  zEmptySet.__iter__.  s    BxrA   c                     t        |       S r?   r   rR   s    rB   r   zEmptySet._eval_powerset1  s    rA   c                     | S r?   r@   rR   s    rB   r   zEmptySet._boundary4      rA   c                     |S r?   r@   rd   s     rB   r   zEmptySet._complement8      rA   c                     t               S r?   )r   rR   s    rB   r   zEmptySet._kind;  s
    yrA   c                     |S r?   r@   rd   s     rB   r   zEmptySet._symmetric_difference>  rY  rA   N)r   r  r  r  rI   rJ   r  r  r2   rT   r   r   r6  rY  rC  r   r   r   r   r   r@   rA   rB   rp   rp     s    0 HML	 "'#;     rA   rp   )	metaclassc                   Z    e Zd ZdZdZdZdZd Zd Ze	d        Z
d Zd Zd	 Ze	d
        Zy)r  a  
    Represents the set of all things.
    The universal set is available as a singleton as ``S.UniversalSet``.

    Examples
    ========

    >>> from sympy import S, Interval
    >>> S.UniversalSet
    UniversalSet

    >>> Interval(1, 2).intersect(S.UniversalSet)
    Interval(1, 2)

    See Also
    ========

    EmptySet

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Universal_set
    TFc                 "    t         j                  S r?   r  rd   s     rB   r   zUniversalSet._complement`  s    zzrA   c                     |S r?   r@   rd   s     rB   r   z"UniversalSet._symmetric_differencec  rY  rA   c                 "    t         j                  S r?   )r   r^   rR   s    rB   r   zUniversalSet._measuref  r  rA   c                      t        t              S r?   r   rR   s    rB   r   zUniversalSet._kindj  r   rA   c                     t         S r?   r0   rd   s     rB   r   zUniversalSet._containsm      rA   c                     t         S r?   rc  rQ  s     rB   r6  zUniversalSet.as_relationalp  rd  rA   c                 "    t         j                  S r?   r  rR   s    rB   r   zUniversalSet._boundarys  r  rA   N)r   r  r  r  rG   rI   rJ   r   r   r  r   r   r   r6  r   r@   rA   rB   r  r  B  sY    2 OHM  &  rA   r  c                       e Zd ZU dZdZdZdZdZd Zd Z	d Z
d Zd Zed	        Zed
        Zed        Zed        Zd Zd Zd Zd Zd Zd Zed        Zd Zd Zd Zd Zd Zd Z fdZe jB                  Z!e"e ge#f   e$d<    xZ%S )r   a  
    Represents a finite set of Sympy expressions.

    Examples
    ========

    >>> from sympy import FiniteSet, Symbol, Interval, Naturals0
    >>> FiniteSet(1, 2, 3, 4)
    {1, 2, 3, 4}
    >>> 3 in FiniteSet(1, 2, 3, 4)
    True
    >>> FiniteSet(1, (1, 2), Symbol('x'))
    {1, x, (1, 2)}
    >>> FiniteSet(Interval(1, 2), Naturals0, {1, 2})
    FiniteSet({1, 2}, Interval(1, 2), Naturals0)
    >>> members = [1, 2, 3, 4]
    >>> f = FiniteSet(*members)
    >>> f
    {1, 2, 3, 4}
    >>> f - FiniteSet(2)
    {1, 3, 4}
    >>> f + FiniteSet(2, 5)
    {1, 2, 3, 4, 5}

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Finite_set
    TFc                 X   |j                  dt        j                        }|r7t        t	        t
        |            }t        |      dk(  r)t        j                  S t        t	        t
        |            }i }t        t        t        |                  D ](  }|j                  r|||<   	 |||j                         <   * t        |j                               }t        t        |t         j"                              }t%        j&                  | g| }||_        |S # t        $ r |||<   Y w xY w)Nr   r   )r  r   r   r   r  r&   r   r   rp   reversedr    r/  as_dummyr	  r!  valuesrE   ra   r
   r$  	_args_set)r%  r   r  r   dargsr~   rl  r  s           rB   r$  zFiniteSet.__new__  s    ::j*;*D*DEGT*+D4yA~zz!GT*+D $wt}-. 	!A{{a!*+E!**,'	! '	GIs'7'789mmC'$'!
 ! ! E!H!s   (DD)(D)c                 ,    t        | j                        S r?   )r  r   rR   s    rB   rC  zFiniteSet.__iter__  s    DIIrA   c           	         t        |t              r\g g }}| j                  D ]M  }|j                  r|j                  r|j                  |       -|j                  dk(  r=|j                  |       O |t        j                  k(  r|g k7  r|j                          g }|t        t        j                  |d   dd      gz  }t        |d d |dd        D ]"  \  }}|j                  t        ||dd             $ |j                  t        |d   t        j                  dd             |g k7  rt        t        |ddit        | d      S t        |ddiS |g k(  r|rt        |t        | d      S |S t        |t              rg }| D ]S  }	t        |j!                  |	            }
|
t        j"                  us0|
t        j$                  usC|j                  |	       U t        | }|| k(  ry g }|D ]@  }	t        | j!                  |	            }
|
t        j"                  us0|j                  |	       B t        t        | |      S t&        j)                  | |      S )NFr   Tr  r   r   r   )r   r   r   r  r  r   r   r   r  rc  r   r^   rw   rc   r   r&   r   r0   r1   rE   r   )rS   re   r  symsr  	intervalsr;  r   unkr~   r   not_trues               rB   r   zFiniteSet._complement  s+   eX& R$DYY #;;199KKNYY%'KKN# DBJ			hq'9'947D$OPP	Sb	484 ADAq$$XaD$%?@A  $r(AJJd!KL2:%eY&G&G%t,u> > !)<e<<%eY-=NN Ly)C "ENN1-.AFF?q'7JJqM" S/Cd{H 'DMM!,-AFF?OOA&' i2C88tU++rA   c           
          || j                   v rt        j                  S t        | j                  D cg c]  }t        ||d       c} S c c}w )a  
        Tests whether an element, other, is in the set.

        Explanation
        ===========

        The actual test is for mathematical equality (as opposed to
        syntactical equality). In the worst case all elements of the
        set must be checked.

        Examples
        ========

        >>> from sympy import FiniteSet
        >>> 1 in FiniteSet(1, 2)
        True
        >>> 5 in FiniteSet(1, 2)
        False

        Tr   )rl  r   r0   r-   r   r   )rS   re   r1  s      rB   r   zFiniteSet._contains  sA    * DNN"66M TYYG1ed3GHHGs   Ac                 @    t        fd| j                  D              S )Nc              3   @   K   | ]  }j                  |        y wr?   )r   )r}   r1  re   s     rB   r   z,FiniteSet._eval_is_subset.<locals>.<genexpr>  s     ?+?r  r  rd   s    `rB   r   zFiniteSet._eval_is_subset  r  rA   c                     | S r?   r@   rR   s    rB   r   zFiniteSet._boundary
  rW  rA   c                     t        |  S r?   )r+   rR   s    rB   r   zFiniteSet._inf      DzrA   c                     t        |  S r?   )r*   rR   s    rB   r   zFiniteSet._sup  ry  rA   c                      yrC  r@   rR   s    rB   r   zFiniteSet.measure  rL  rA   c                       j                   s
t               S t         fd j                   D              r"t         j                   d   j                        S t        t              S )Nc              3   j   K   | ]*  }|j                   j                  d    j                   k(   , ywr  r   r   r}   r~   rS   s     rB   r   z"FiniteSet._kind.<locals>.<genexpr>  (     @499Q<,,,@   03r   r   r   r#  r   r   rR   s   `rB   r   zFiniteSet._kind  sH    yy9@dii@@499Q<,,--=))rA   c                 ,    t        | j                        S r?   )r   r   rR   s    rB   rY  zFiniteSet.__len__"  r\  rA   c           	      L    t        | D cg c]  }t        ||       c} S c c}w )z@Rewrite a FiniteSet in terms of equalities and logic operators. )r-   r   )rS   r  elems      rB   r6  zFiniteSet.as_relational%  s"    6Bvt$6776s   !c                 0    t        |       t        |      z
  S r?   )hashrd   s     rB   comparezFiniteSet.compare)  s    T
T%[()rA   c                 n    t        |      }t        | D cg c]  }|j                  |       c} S c c}w r   )r=   r   rY   )rS   r   r   r  s       rB   r   zFiniteSet._eval_evalf,  s0    $>4:::,>??>s   2c           
      R    ddl m} t        | D cg c]  } ||fi | c} S c c}w )Nr   )simplify)sympy.simplifyr  r   )rS   r  r  r  s       rB   _eval_simplifyzFiniteSet._eval_simplify0  s(    +E8D3F3EFFEs   $c                     | j                   S r?   r(  rR   s    rB   _sorted_argszFiniteSet._sorted_args4  r)  rA   c                      | j                   t        | j                        D cg c]  } | j                   |  c} S c c}w r?   )r   r8   r   )rS   r   s     rB   r   zFiniteSet._eval_powerset8  s4    tyy'$))2DEQ9499a=EFFEs   >c                     ddl m} d } |t        |             syd t        fd|D              syt	        |t              }t        |j                        D ]  }t        | }||vs y  ||      S )z1Rewriting method for a finite set to a power set.r   r   c                 .    t        | xr	 | | dz
  z         S rP  )boolr   s    rB   rC   z5FiniteSet._eval_rewrite_as_PowerSet.<locals>.<lambda>?  s    4 5!q1u+o6 rA   Nc                 >    t        | t              xr | j                  S r?   )r   rE   r  )r   s    rB   rC   z5FiniteSet._eval_rewrite_as_PowerSet.<locals>.<lambda>C  s    jc2Gs7G7G rA   c              3   .   K   | ]  } |        y wr?   r@   )r}   r   fs_tests     rB   r   z6FiniteSet._eval_rewrite_as_PowerSet.<locals>.<genexpr>D  s     0C73<0r  r
  )r   r   r   r#  maxr8   r   r   )	rS   r   r  r   is2powbiggestr   arg_setr  s	           @rB   _eval_rewrite_as_PowerSetz#FiniteSet._eval_rewrite_as_PowerSet;  su    &6c$i G0400d$7<<( 	CoGd"	   rA   c                 r    t        |t              st        dt        |      z        |j	                  |       S Nz!Invalid comparison of set with %sr   rE   r	  r9   r   rd   s     rB   __ge__zFiniteSet.__ge__N  s1    %%?)EBRRSSt$$rA   c                 r    t        |t              st        dt        |      z        | j	                  |      S r  )r   rE   r	  r9   r   rd   s     rB   __gt__zFiniteSet.__gt__S  s3    %%?)EBRRSS&&u--rA   c                 r    t        |t              st        dt        |      z        | j	                  |      S r  r  rd   s     rB   __le__zFiniteSet.__le__X  s1    %%?)EBRRSS~~e$$rA   c                 r    t        |t              st        dt        |      z        | j	                  |      S r  )r   rE   r	  r9   r   rd   s     rB   __lt__zFiniteSet.__lt__]  s3    %%?)EBRRSS$$U++rA   c                 l    t        |t        t        f      r| j                  |k(  S t        |   |      S r?   )r   r!  r  rl  super__eq__)rS   re   	__class__s     rB   r  zFiniteSet.__eq__b  s0    ec9-.>>U**w~e$$rA   __hash__)&r   r  r  r  r  r  rI   rJ   r$  rC  r   r   r   r  r   r   r   r   r   rY  r6  r  r   r  r  r   r  r  r  r  r  r  r
   r  r   r   r  __classcell__r  s   @rB   r   r   x  s    : LKHM8/,bI8@        *8*@G  G!&%
.
%
,
%
 ).Hx%6rA   r   c                     t        |  S r?   rU  r	  s    rB   rC   rC   i  s
    )Q- rA   c                     t        |  S r?   rU  r	  s    rB   rC   rC   j  s
    	1 rA   c                   H    e Zd ZdZdZd	dZed        Zd Ze	d        Z
d Zy)
r   a  Represents the set of elements which are in either of the
    sets and not in their intersection.

    Examples
    ========

    >>> from sympy import SymmetricDifference, FiniteSet
    >>> SymmetricDifference(FiniteSet(1, 2, 3), FiniteSet(3, 4, 5))
    {1, 2, 4, 5}

    See Also
    ========

    Complement, Union

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Symmetric_difference
    Tc                 `    |rt         j                  ||      S t        j                  | ||      S r?   )r   r   r
   r$  r6  s       rB   r$  zSymmetricDifference.__new__  s+    &--a33}}S!Q''rA   c                 H    |j                  |       }||S t        | |d      S )NFr   )r   r   r:  s      rB   r   zSymmetricDifference.reduce  s,    ((+M&q!e<<rA   c                 |    | j                   \  }}|j                  |      }|j                  |      }t        ||      S )zQRewrite a symmetric_difference in terms of equalities and
        logic operators)r   r6  r/   r?  s         rB   r6  z!SymmetricDifference.as_relational  s:     yy1''5%  rA   c                 >    t        d | j                  D              ryy )Nc              3   4   K   | ]  }|j                     y wr?   r>  r  s     rB   r   z2SymmetricDifference.is_iterable.<locals>.<genexpr>  s     43s4r5  Tr  rR   s    rB   r  zSymmetricDifference.is_iterable  s    4$))44 5rA   c              #      K   | j                   }t        d |D         }|D ]"  }d}|D ]  }||v s|dz  } |dz  dk(  s| $ y w)Nc              3   2   K   | ]  }t        |        y wr?   r  r  s     rB   r   z/SymmetricDifference.__iter__.<locals>.<genexpr>  s     73T#Y7rX  r   r   rn  )r   r6   )rS   r   rf   itemcountr   s         rB   rC  zSymmetricDifference.__iter__  sd     yy7$78 	DE 19QJE qyA~
	s   ,AAANrH  )r   r  r  r  is_SymmetricDifferencer$  r  r   r6  r  r  rC  r@   rA   rB   r   r   m  sF    * "( = =!  rA   r   c                   t    e Zd ZdZd Zed        Zed        Zed        Zed        Z	d Z
d Zd	 Zd
 Zd Zy)DisjointUniona   Represents the disjoint union (also known as the external disjoint union)
    of a finite number of sets.

    Examples
    ========

    >>> from sympy import DisjointUnion, FiniteSet, Interval, Union, Symbol
    >>> A = FiniteSet(1, 2, 3)
    >>> B = Interval(0, 5)
    >>> DisjointUnion(A, B)
    DisjointUnion({1, 2, 3}, Interval(0, 5))
    >>> DisjointUnion(A, B).rewrite(Union)
    Union(ProductSet({1, 2, 3}, {0}), ProductSet(Interval(0, 5), {1}))
    >>> C = FiniteSet(Symbol('x'), Symbol('y'), Symbol('z'))
    >>> DisjointUnion(C, C)
    DisjointUnion({x, y, z}, {x, y, z})
    >>> DisjointUnion(C, C).rewrite(Union)
    ProductSet({x, y, z}, {0, 1})

    References
    ==========

    https://en.wikipedia.org/wiki/Disjoint_union
    c                     g }|D ]1  }t        |t              r|j                  |       %t        d|z         t	        j
                  | g| }|S )NzQInvalid input: '%s', input args                     to DisjointUnion must be Sets)r   rE   r   r	  r
   r$  )r%  r   dj_collectionset_ir  s        rB   r$  zDisjointUnion.__new__  sb     	<E%%$$U+ !35:!; < <		< mmC0-0
rA   c                     | j                   S r?   r(  rR   s    rB   r   zDisjointUnion.sets  r)  rA   c                 :    t        d | j                  D              S )Nc              3   4   K   | ]  }|j                     y wr?   rF  r  s     rB   r   z)DisjointUnion.is_empty.<locals>.<genexpr>  s     77r5  )r   r   rR   s    rB   rI   zDisjointUnion.is_empty  s    7TYY777rA   c                 h    t        d | j                  D              }t        | j                  |g      S )Nc              3   4   K   | ]  }|j                     y wr?   rI  r  s     rB   r   z.DisjointUnion.is_finite_set.<locals>.<genexpr>  rJ  r5  rK  rL  s     rB   rJ   zDisjointUnion.is_finite_set  rN  rA   c                     | j                   ryd}| j                  D ]  }|j                   r|xr |j                  }! |S )NFT)rI   r   r  )rS   	iter_flagr  s      rB   r  zDisjointUnion.is_iterable  sC    ==	YY 	<E>>%;%*;*;		< rA   c                     t         j                  }d}|D ]9  }t        |t              st	        |t        |            }t        ||      }|dz   }; |S )z
        Rewrites the disjoint union as the union of (``set`` x {``i``})
        where ``set`` is the element in ``sets`` at index = ``i``
        r   r   )r   rp   r   rE   r   r   rc   )rS   r   r  dj_unionindexr  crosss          rB   _eval_rewrite_as_Unionz$DisjointUnion._eval_rewrite_as_Union  sX     :: 	"E%%"5)E*:; 51			"
 rA   c                 J   t        |t              rt        |      dk7  rt        j                  S |d   j
                  st        j                  S |d   t        | j                        k\  s|d   dk  rt        j                  S | j                  |d      j                  |d         S )a  
        ``in`` operator for DisjointUnion

        Examples
        ========

        >>> from sympy import Interval, DisjointUnion
        >>> D = DisjointUnion(Interval(0, 1), Interval(0, 2))
        >>> (0.5, 0) in D
        True
        >>> (0.5, 1) in D
        True
        >>> (1.5, 0) in D
        False
        >>> (1.5, 1) in D
        True

        Passes operation on to constituent sets
        rn  r   r   )r   r   r   r   r1   r  r   r   )rS   r0  s     rB   r   zDisjointUnion._contains  s    ( '5)S\Q->77Nqz$$77N1:TYY'71:>77Nyy$..wqz::rA   c                       j                   s
t               S t         fd j                   D              r j                   d   j                  S t        t              S )Nc              3   j   K   | ]*  }|j                   j                  d    j                   k(   , ywr  r~  r  s     rB   r   z&DisjointUnion._kind.<locals>.<genexpr>	  r  r  r   r  rR   s   `rB   r   zDisjointUnion._kind	  sC    yy9@dii@@99Q<$$$=))rA   c           	          | j                   rUg }t        | j                        D ]*  \  }}|j                  t	        |t        |      h             , t        t        |       S t        d| z        )Nz'%s' is not iterable.)	r  r   r   r   r4   r   r  r6   r]   )rS   itersr~   r   s       rB   rC  zDisjointUnion.__iter__$	  sh    E!$)), 81Xa'!*678 
E*++4t;<<rA   c                 |    | j                   r#d}| j                  D ]  }|t        |      z  } |S t        d| z        )a1  
        Returns the length of the disjoint union, i.e., the number of elements in the set.

        Examples
        ========

        >>> from sympy import FiniteSet, DisjointUnion, EmptySet
        >>> D1 = DisjointUnion(FiniteSet(1, 2, 3, 4), EmptySet, FiniteSet(3, 4, 5))
        >>> len(D1)
        7
        >>> D2 = DisjointUnion(FiniteSet(3, 5, 7), EmptySet, FiniteSet(3, 5, 7))
        >>> len(D2)
        6
        >>> D3 = DisjointUnion(EmptySet, EmptySet)
        >>> len(D3)
        0

        Adds up the lengths of the constituent sets.
        r   z'%s' is not a finite set.)rJ   r   r   r]   )rS   sizer!  s      rB   rY  zDisjointUnion.__len__/	  sI    * Dyy !C !K84?@@rA   N)r   r  r  r  r$  r  r   rI   rJ   r  r  r   r   rC  rY  r@   rA   rB   r  r    sv    2	   8 8 5 5  ;>*	=ArA   r  c            	      <   ddl m} ddlm} t	        |       dk  rt        dt	        |       z        t        | d   t        t        f      r&t	        |       dkD  rt        | d   | d         }| dd }n
| d   }| dd }t        |t              rnt        |      rt        |di       }|rbt	        |      dk7  rt        t        d	            |j                  d   }|dk(  rd
}nHt        d|dz         D cg c]  }t        d|z         }}nt!        j"                  |      j$                  }t'         ||D cg c]  }t)                c}       t        fd|D              }	t        |	 ||	       }n t+        t        dt-        |      z              t/        d |D              r&|D cg c]  }t-        |       }
}t        d|
z        t	        |      dk(  r|d   }	  |||      }|t*        |s|S 	 t        ||      r|j                  \  }}|j0                  d   |j2                  k(  r|S t        ||      rt	        |j4                  j0                        dk(  rt	        |j0                        dk(  rv|j4                  j0                  d   }|j0                  d   }t7        t        ||j2                  j9                  ||j4                  j2                              g|j:                   S ||S  ||g| S c c}w c c}w c c}w # t*        $ r  |||      }Y &w xY w)a  
    Return an image of the set under transformation ``f``.

    Explanation
    ===========

    If this function cannot compute the image, it returns an
    unevaluated ImageSet object.

    .. math::
        \{ f(x) \mid x \in \mathrm{self} \}

    Examples
    ========

    >>> from sympy import S, Interval, imageset, sin, Lambda
    >>> from sympy.abc import x

    >>> imageset(x, 2*x, Interval(0, 2))
    Interval(0, 4)

    >>> imageset(lambda x: 2*x, Interval(0, 2))
    Interval(0, 4)

    >>> imageset(Lambda(x, sin(x)), Interval(-2, 1))
    ImageSet(Lambda(x, sin(x)), Interval(-2, 1))

    >>> imageset(sin, Interval(-2, 1))
    ImageSet(Lambda(x, sin(x)), Interval(-2, 1))
    >>> imageset(lambda y: x + y, Interval(-2, 1))
    ImageSet(Lambda(y, x + y), Interval(-2, 1))

    Expressions applied to the set of Integers are simplified
    to show as few negatives as possible and linear expressions
    are converted to a canonical form. If this is not desirable
    then the unevaluated ImageSet should be used.

    >>> imageset(x, -2*x + 5, S.Integers)
    ImageSet(Lambda(x, 2*x + 1), Integers)

    See Also
    ========

    sympy.sets.fancysets.ImageSet

    r   ImageSet)set_functionrn  z)imageset expects at least 2 args, got: %sr   Nnargsz
                    This function can take more than 1 arg
                    but the potentially complicated set input
                    has not been analyzed at this point to
                    know its dimensions. TODO
                    r   zx%ic              3   H   K   | ]  }t        t        |              y wr?   )r$   r"   )r}   r~   dexprs     rB   r   zimageset.<locals>.<genexpr>	  s'      *"# *1Iu *s   "zN
            expecting lambda, Lambda, or FunctionClass,
            not '%s'.c              3   >   K   | ]  }t        |t                 y wr?   r  r  s     rB   r   zimageset.<locals>.<genexpr>	  s     
4az!S!!
4s   z.arguments after mapping should be sets, not %s)	fancysetsr  setexprr  r   r]   r   r"   r"  r   callablegetattrrZ   r:   r   r   inspect	signature
parametersr%   r#   r	  r9   r  	variablesr_   lamdaimagesetr  	base_sets)r   r  r  fset_listr  Nr   r~   varnamer!  rr   yr  s                  @rB   r  r  M	  s   ^ $%
4y1}Ds4yPQQ$q'FE?+D	A47DG$8G8!V	!7B'5zQ)* 6 +   

1AAv05aQ@1VEAI&@@!!!$//Aa0UW012 *'(* *33 
 $&q\$* + , 	, 
48
44&./	!//<tCE 	E 8}qk	!Q$Ay  a"VVFAs;;q>QVV#Jc8$
 399&&'1,Q[[1AQ1FII''*KKN1affkk!SYY^^<=O@CO O =HA!!!c A 1 0  	!C A	!s$   2K64K;'L L LLc                     | t         t        fv ryt        d      } | |      j                  |      }|dkD  dk(  s|dk  dk(  ryy)zk
    Checks whether function ``func`` is invertible when the domain is
    restricted to set ``setv``.
    Tur   N)r(   r)   r#   diff)r   setvr  fdiffs       rB   is_function_invertible_in_setr  	  sL     Szc
AGLLOE 		duqyT1rA   c                 X   ddl m} | st        j                  S | D ]  }t	        |t
              rt        d       | D cg c]  }|j                  s| }}t        |      dkD  r2d |D        }t        | }|g| D cg c]  }|j                  r| c}z   } t        |       } d}|rS| D ]K  }d}| |hz
  D ]8  }	 |||	      }
|
t	        |
t              s|
h}
| ||	hz
  j                  |
      } n |sI|}  n |rSt        |       dk(  r| j                         S t        | ddiS c c}w c c}w )	aZ  
    Simplify a :class:`Union` using known rules.

    Explanation
    ===========

    We first start with global rules like 'Merge all FiniteSets'

    Then we iterate through all pairs and ask the constituent sets if they
    can simplify themselves with any other constituent.  This process depends
    on ``union_sets(a, b)`` functions.
    r   )
union_sets Input args to Union must be Setsr   c              3   .   K   | ]  }|D ]  }|   y wr?   r@   )r}   r!  r   s      rB   r   z!simplify_union.<locals>.<genexpr>	  s     33s3!Q3Q3s   TFr   )sympy.sets.handlers.unionr  r   rp   r   rE   r	  r  r   r   r!  rf   poprc   )r   r  r   r   finite_setsr;  
finite_setnew_argsr   tnew_sets              rB   r  r  	  sW    5 zz @#s#>??@
 #5ann15K5
;!3+3]
|$EQannqEE t9DH
 	AHQCZ $Q* &%gs3#*) $1v44W=H 	   4yA~xxzd+U++; 6 Fs   D"D"<D'D'c                 b  	 | st         j                  S | D ]  }t        |t              rt	        d       t         j
                  | v rt         j
                  S t        j                  |       }||S | D ]d  }|j                  st        |       |hz
  }t        |      dkD  r%t        | 	t        	fd|j                  D         c S t        |j                   c S  | D ]S  }|j                  s| j                  |       | |j                  d   gz   }t        t        | |j                  d         c S  ddlm} t        |       } d}|rA| D ]9  }d}| |hz
  D ]&  } |||      }|| ||hz
  j%                  |h      } n |s7|}  n |rAt        |       dk(  r| j'                         S t        | ddiS )	aI  
    Simplify an intersection using known rules.

    Explanation
    ===========

    We first start with global rules like
    'if any empty sets return empty set' and 'distribute any unions'

    Then we iterate through all pairs and ask the constituent sets if they
    can simplify themselves with any other constituent
    r  r   c              3   6   K   | ]  }t        |        y wr?   rh   )r}   r   re   s     rB   r   z(simplify_intersection.<locals>.<genexpr>=
  s     JC|C7Js   r   )intersection_setsTFr   )r   r  r   rE   r	  rp   ri   r2  r  r!  r   rc   r   rH   r&  rw    sympy.sets.handlers.intersectionr  rf   r  )
r   r   rvr   r  r  r  r  r  re   s
            @rB   r  r  
  s    ~~ @#s#>??@
 	zzTzz 
	)	)$	/B	~	  &::TaSJ:"$j1J166JKKaff~%&  D??KKN+JlJ7CC	D C t9DH
 	AHQCZ +Aq1 & $1v44gY?H 	  4yA~xxzT2E22rA   c                 $   t        ||gd d      \  }}t        |      dk(  r,t        |d   D cg c]  }|d   D ]  } | ||        c}} S t        |      dk(  r)|d   D cg c]  }t        | |d   ||       }}t	        | S y c c}}w c c}w )Nc                 "    t        | t              S r?   )r   r   r	  s    rB   rC   z%_handle_finite_sets.<locals>.<lambda>g
  s    Jq),D rA   Tr  rn  r   r   )r5   r   r   _apply_operationrc   )	opr   r  commutativer(  re   r~   r:  r   s	            rB   r2  r2  e
  s    1a&"DTRNGU
7|qWQZLLA2a8L8LMM	W	HOPQ
S1 U1Xq+>SSd| MSs   B
$Bc           	         ddl m} t        d      }t        | |||      }|	 | ||      }||r	 | ||      }|t	        d      \  }}t        |t              r:t        |t              s* |t        | | ||            |      j                         }|S t        |t              s:t        |t              r* |t        | | ||            |      j                         }|S  |t        ||f | ||            ||      }|S )Nr   r  r  zx y)	r  r  r#   r2  r!   r   rE   r   doit)	r  r   r  r  r  r  out_x_ys	            rB   r  r  q
  s    #c
A
b!Q
4C
{Ah
{{Ah
{BajC&86!R1X.2779C
 J	 As#
1c(:6!R1X.2779C J 62r(Br2J7A>CJrA   c                 ,    ddl m} t        || |d      S )Nr   )_set_addTr  )sympy.sets.handlers.addr  r  )r   r  r  s      rB   set_addr  
      0Ha==rA   c                 ,    ddl m} t        || |d      S )Nr   )_set_subFr  )r  r  r  )r   r  r  s      rB   set_subr  
      0Ha>>rA   c                 ,    ddl m} t        || |d      S )Nr   )_set_mulTr  )sympy.sets.handlers.mulr  r  )r   r  r  s      rB   set_mulr  
  r  rA   c                 ,    ddl m} t        || |d      S )Nr   )_set_divFr  )r  r  r  )r   r  r  s      rB   set_divr  
  r  rA   c                 ,    ddl m} t        || |d      S )Nr   )_set_powFr  )sympy.sets.handlers.powerr  r  )r   r  r  s      rB   set_powr   
  s    2Ha>>rA   c                      ddl m}  || |      S )Nr   )_set_function)sympy.sets.handlers.functionsr"  )r  r   r"  s      rB   r  r  
  s    ;ArA   c                   *     e Zd ZdZd fd	Zd Z xZS )r   a#  
    SetKind is kind for all Sets

    Every instance of Set will have kind ``SetKind`` parametrised by the kind
    of the elements of the ``Set``. The kind of the elements might be
    ``NumberKind``, or ``TupleKind`` or something else. When not all elements
    have the same kind then the kind of the elements will be given as
    ``UndefinedKind``.

    Parameters
    ==========

    element_kind: Kind (optional)
        The kind of the elements of the set. In a well defined set all elements
        will have the same kind. Otherwise the kind should
        :class:`sympy.core.kind.UndefinedKind`. The ``element_kind`` argument is optional but
        should only be omitted in the case of ``EmptySet`` whose kind is simply
        ``SetKind()``

    Examples
    ========

    >>> from sympy import Interval
    >>> Interval(1, 2).kind
    SetKind(NumberKind)
    >>> Interval(1,2).kind.element_kind
    NumberKind

    See Also
    ========

    sympy.core.kind.NumberKind
    sympy.matrices.kind.MatrixKind
    sympy.core.containers.TupleKind
    c                 6    t         |   | |      }||_        |S r?   )r  r$  rS  )r%  rS  r  r  s      rB   r$  zSetKind.__new__
  s     goc<0'
rA   c                 :    | j                   syd| j                   z  S )Nz	SetKind()zSetKind(%s))rS  rR   s    rB   __repr__zSetKind.__repr__
  s       4#4#444rA   r?   )r   r  r  r  r$  r'  r  r  s   @rB   r   r   
  s    "F
5rA   r   )stypingr   r   	functoolsr   collectionsr   r  sympy.core.kindr   r   r	   sympy.core.basicr
   sympy.core.containersr   r   sympy.core.decoratorsr   r   sympy.core.evalfr   sympy.core.exprr   sympy.core.functionr   sympy.core.logicr   r   r   r   r   sympy.core.numbersr   r   sympy.core.operationsr   sympy.core.parametersr   sympy.core.relationalr   r   r   sympy.core.singletonr   r   sympy.core.sortingr    sympy.core.symbolr!   r"   r#   r$   sympy.core.sympifyr%   r&   r'   &sympy.functions.elementary.exponentialr(   r)   (sympy.functions.elementary.miscellaneousr*   r+   sympy.logic.boolalgr,   r-   r.   r/   r0   r1   sympy.utilities.decoratorr2   sympy.utilities.exceptionsr3   sympy.utilities.iterablesr4   r5   r6   r7   r8   sympy.utilities.miscr9   r:   mpmathr;   r<   mpmath.libmp.libmpfr=   r   rE   r   r   rc   ri   rw   rp   r  r   r!  r  r   r  r  r  r  r  r2  r  r  r  r  r  r   r  r   r@   rA   rB   <module>rD     s      #  ; ; " 2 E '   &  - + 3 / / - & K K B B ; = > > 0 @0 0 6  + ,!&&FFAFF	177GGQWW	!  C% C CLr rjYs Yxn=C n=bcF3	 cFLZ ZzGsi GT33) 3lo7 o7b 0  5  A# AJXAC XAv}"@$5,pK3\	*>
?
>
?
?

-5d -5rA   