U
    9%e3,                     @   s   d Z ddlmZ ddlmZ ddlmZmZ ddlm	Z	 ddl
mZmZ ddlmZmZmZmZ dd	lmZ dd
lmZ ddlmZ G dd de	Zdd ee< dd ZG dd de	Zdd ee< G dd deZG dd deZdS )zModule for SymPy containers

    (SymPy objects that store other SymPy objects)

    The containers implemented in this module are subclassed to Basic.
    They are supposed to work seamlessly within the SymPy framework.
    )OrderedDict)
MutableSet)AnyCallable   )Basic)default_sort_keyordered)_sympifysympify_sympy_converterSympifyError)Kind)iterable)as_intc                       s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd ZeZ fddZ fddZdd Zdd Zdd Zdd Zeddd Zd&d"d#Zed$d% Z  ZS )'Tuplea  
    Wrapper around the builtin tuple object.

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

    The Tuple is a subclass of Basic, so that it works well in the
    SymPy framework.  The wrapped tuple is available as self.args, but
    you can also access elements or slices with [:] syntax.

    Parameters
    ==========

    sympify : bool
        If ``False``, ``sympify`` is not called on ``args``. This
        can be used for speedups for very large tuples where the
        elements are known to already be SymPy objects.

    Examples
    ========

    >>> from sympy import Tuple, symbols
    >>> a, b, c, d = symbols('a b c d')
    >>> Tuple(a, b, c)[1:]
    (b, c)
    >>> Tuple(a, b, c).subs(a, d)
    (d, b, c)

    c                 O   s.   | ddrdd |D }tj| f| }|S )Nr   Tc                 s   s   | ]}t |V  qd S N)r   .0arg r   T/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/sympy/core/containers.py	<genexpr>6   s     z Tuple.__new__.<locals>.<genexpr>)getr   __new__)clsargskwargsobjr   r   r   r   4   s    zTuple.__new__c                    s<   t |tr2|t }t fddt| D  S  j| S )Nc                 3   s   | ]} j | V  qd S r   r   )r   jselfr   r   r   =   s     z$Tuple.__getitem__.<locals>.<genexpr>)
isinstancesliceindiceslenr   ranger   )r"   ir%   r   r!   r   __getitem__:   s    
zTuple.__getitem__c                 C   s
   t | jS r   )r&   r   r!   r   r   r   __len__@   s    zTuple.__len__c                 C   s
   || j kS r   r   )r"   itemr   r   r   __contains__C   s    zTuple.__contains__c                 C   s
   t | jS r   )iterr   r!   r   r   r   __iter__F   s    zTuple.__iter__c                 C   s:   t |trt| j|j  S t |tr2t| j|  S tS d S r   r#   r   r   tupleNotImplementedr"   otherr   r   r   __add__I   s
    

zTuple.__add__c                 C   s:   t |trt|j| j  S t |tr2t|| j  S tS d S r   r/   r2   r   r   r   __radd__Q   s
    

zTuple.__radd__c                 C   sB   zt |}W n$ tk
r0   tdt| Y nX | j| j|  S )Nz3Can't multiply sequence by non-integer of type '%s')r   
ValueError	TypeErrortypefuncr   )r"   r3   nr   r   r   __mul__Y   s
    zTuple.__mul__c                    s    t |trt |S | j|kS r   )r#   r   super__eq__r   r2   	__class__r   r   r=   b   s    
zTuple.__eq__c                    s    t |trt |S | j|kS r   )r#   r   r<   __ne__r   r2   r>   r   r   r@   g   s    
zTuple.__ne__c                 C   s
   t | jS r   )hashr   r!   r   r   r   __hash__l   s    zTuple.__hash__c                    s   t  fdd| jD S )Nc                 3   s   | ]}|  V  qd S r   )
_to_mpmath)r   aprecr   r   r   p   s     z#Tuple._to_mpmath.<locals>.<genexpr>)r0   r   )r"   rF   r   rE   r   rC   o   s    zTuple._to_mpmathc                 C   s   t | j|jk S r   r
   r   r2   r   r   r   __lt__r   s    zTuple.__lt__c                 C   s   t | j|jkS r   rG   r2   r   r   r   __le__u   s    zTuple.__le__)returnc                 C   s   | j |S )z&Return number of occurrences of value.)r   count)r"   valuer   r   r   tuple_countz   s    zTuple.tuple_countNc                 C   sF   |dkr|dkr| j |S |dkr2| j ||S | j |||S dS )z2Searches and returns the first index of the value.N)r   index)r"   rL   startstopr   r   r   rN   ~   s
    zTuple.indexc                 C   s   t dd | jD  S )ao  
        The kind of a Tuple instance.

        The kind of a Tuple is always of :class:`TupleKind` but
        parametrised by the number of elements and the kind of each element.

        Examples
        ========

        >>> from sympy import Tuple, Matrix
        >>> Tuple(1, 2).kind
        TupleKind(NumberKind, NumberKind)
        >>> Tuple(Matrix([1, 2]), 1).kind
        TupleKind(MatrixKind(NumberKind), NumberKind)
        >>> Tuple(1, 2).kind.element_kind
        (NumberKind, NumberKind)

        See Also
        ========

        sympy.matrices.common.MatrixKind
        sympy.core.kind.NumberKind
        c                 s   s   | ]}|j V  qd S r   )kind)r   r(   r   r   r   r      s     zTuple.kind.<locals>.<genexpr>)	TupleKindr   r!   r   r   r   rQ      s    z
Tuple.kind)NN)__name__
__module____qualname____doc__r   r)   r*   r,   r.   r4   r5   r;   __rmul__r=   r@   rB   rC   rH   rI   intrM   rN   propertyrQ   __classcell__r   r   r>   r   r      s(   
r   c                 C   s   t |  S r   r   )tupr   r   r   <lambda>       r]   c                    s    fdd}|S )a`  
    Decorator that converts any tuple in the function arguments into a Tuple.

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

    The motivation for this is to provide simple user interfaces.  The user can
    call a function with regular tuples in the argument, and the wrapper will
    convert them to Tuples before handing them to the function.

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

    >>> from sympy.core.containers import tuple_wrapper
    >>> def f(*args):
    ...    return args
    >>> g = tuple_wrapper(f)

    The decorated function g sees only the Tuple argument:

    >>> g(0, (1, 2), 3)
    (0, (1, 2), 3)

    c                     s<   g }| D ](}t |tr&|t|  q|| q ||S r   )r#   r0   appendr   )r   Zkw_argsZnewargsr   methodr   r   wrap_tuples   s    
z"tuple_wrapper.<locals>.wrap_tuplesr   )ra   rb   r   r`   r   tuple_wrapper   s    rc   c                       s   e Zd ZU dZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd ZdddZdd Zdd Zedd Z fddZejZeegef ed<   ZS )Dictax  
    Wrapper around the builtin dict object.

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

    The Dict is a subclass of Basic, so that it works well in the
    SymPy framework.  Because it is immutable, it may be included
    in sets, but its values must all be given at instantiation and
    cannot be changed afterwards.  Otherwise it behaves identically
    to the Python dict.

    Examples
    ========

    >>> from sympy import Dict, Symbol

    >>> D = Dict({1: 'one', 2: 'two'})
    >>> for key in D:
    ...    if key == 1:
    ...        print('%s %s' % (key, D[key]))
    1 one

    The args are sympified so the 1 and 2 are Integers and the values
    are Symbols. Queries automatically sympify args so the following work:

    >>> 1 in D
    True
    >>> D.has(Symbol('one')) # searches keys and values
    True
    >>> 'one' in D # not in the keys
    False
    >>> D[1]
    one

    c                 G   s   t |dkr6t|d ttfr6dd |d  D }n2t|r`tdd |D r`dd |D }ntdt|}t	j
| ft| }||_t||_|S )	Nr   r   c                 S   s   g | ]\}}t ||qS r   r[   r   kvr   r   r   
<listcomp>  s     z Dict.__new__.<locals>.<listcomp>c                 s   s   | ]}t |d kV  qdS )   N)r&   r   r   r   r   r     s     zDict.__new__.<locals>.<genexpr>c                 S   s   g | ]\}}t ||qS r   r[   re   r   r   r   rh     s     z<Pass Dict args as Dict((k1, v1), ...) or Dict({k1: v1, ...}))r&   r#   dictrd   itemsr   allr7   	frozensetr   r   r	   elements_dict)r   r   rk   rn   r   r   r   r   r      s    
zDict.__new__c                 C   s4   zt |}W n tk
r(   t|Y nX | j| S )zx.__getitem__(y) <==> x[y])r
   r   KeyErrorro   r"   keyr   r   r   r)     s
    zDict.__getitem__c                 C   s   t dd S )NzSymPy Dicts are Immutable)NotImplementedError)r"   rr   rL   r   r   r   __setitem__  s    zDict.__setitem__c                 C   s
   | j  S )zDReturns a set-like object providing a view on dict's items.
        )ro   rk   r!   r   r   r   rk     s    z
Dict.itemsc                 C   s
   | j  S )z$Returns the list of the dict's keys.)ro   keysr!   r   r   r   ru     s    z	Dict.keysc                 C   s
   | j  S )z&Returns the list of the dict's values.)ro   valuesr!   r   r   r   rv   !  s    zDict.valuesc                 C   s
   t | jS )zx.__iter__() <==> iter(x))r-   ro   r!   r   r   r   r.   %  s    zDict.__iter__c                 C   s
   | j  S )zx.__len__() <==> len(x))ro   r*   r!   r   r   r   r*   )  s    zDict.__len__Nc                 C   s4   zt |}W n tk
r$   | Y S X | j||S )z:Returns the value for key if the key is in the dictionary.)r
   r   ro   r   )r"   rr   defaultr   r   r   r   -  s
    
zDict.getc                 C   s.   zt |}W n tk
r"   Y dS X || jkS )z6D.__contains__(k) -> True if D has a key k, else FalseF)r
   r   ro   rq   r   r   r   r,   5  s
    zDict.__contains__c                 C   s   t | j|jk S r   rG   r2   r   r   r   rH   =  s    zDict.__lt__c                 C   s   t t| jtdS )N)rr   )r0   sortedr   r   r!   r   r   r   _sorted_args@  s    zDict._sorted_argsc                    s"   t |tr| t|kS t |S r   )r#   rj   rd   r<   r=   r2   r>   r   r   r=   D  s    
zDict.__eq__rB   )N)rS   rT   rU   rV   r   r)   rt   rk   ru   rv   r.   r*   r   r,   rH   rY   ry   r=   r   rB   r   r   __annotations__rZ   r   r   r>   r   rd      s    
%	

rd   c                 C   s   t |   S r   )rd   rk   )dr   r   r   r]   L  r^   c                   @   sh   e Zd ZdddZdd Zdd Zdd	 Zd
d ZdddZdd Z	dd Z
dd Zdd Zdd ZdS )
OrderedSetNc                 C   s&   |rt dd |D | _nt  | _d S )Nc                 s   s   | ]}|d fV  qd S r   r   )r   r+   r   r   r   r   Q  s     z&OrderedSet.__init__.<locals>.<genexpr>)r   map)r"   r   r   r   r   __init__O  s    zOrderedSet.__init__c                 C   s
   t | jS r   )r&   r}   r!   r   r   r   r*   U  s    zOrderedSet.__len__c                 C   s
   || j kS r   r}   rq   r   r   r   r,   X  s    zOrderedSet.__contains__c                 C   s   d | j |< d S r   r   rq   r   r   r   add[  s    zOrderedSet.addc                 C   s   | j | d S r   )r}   poprq   r   r   r   discard^  s    zOrderedSet.discardTc                 C   s   | j j|dd S )N)lastr   )r}   popitem)r"   r   r   r   r   r   a  s    zOrderedSet.popc                 c   s   | j  E d H  d S r   )r}   ru   r!   r   r   r   r.   d  s    zOrderedSet.__iter__c                 C   s.   | j sd| jjf S d| jjt| j  f S )Nz%s()z%s(%r))r}   r?   rS   listru   r!   r   r   r   __repr__g  s    zOrderedSet.__repr__c                    s   |   fdd| D S )Nc                    s   g | ]}| kr|qS r   r   r   valr3   r   r   rh   m  s      z+OrderedSet.intersection.<locals>.<listcomp>r>   r2   r   r   r   intersectionl  s    zOrderedSet.intersectionc                    s   |   fdd| D S )Nc                    s   g | ]}| kr|qS r   r   r   r   r   r   rh   p  s      z)OrderedSet.difference.<locals>.<listcomp>r>   r2   r   r   r   
differenceo  s    zOrderedSet.differencec                 C   s   |D ]}|  | qd S r   )r   )r"   r   r   r   r   r   updater  s    zOrderedSet.update)N)T)rS   rT   rU   r~   r*   r,   r   r   r   r.   r   r   r   r   r   r   r   r   r|   N  s   

r|   c                       s(   e Zd ZdZ fddZdd Z  ZS )rR   ad  
    TupleKind is a subclass of Kind, which is used to define Kind of ``Tuple``.

    Parameters of TupleKind will be kinds of all the arguments in Tuples, for
    example

    Parameters
    ==========

    args : tuple(element_kind)
       element_kind is kind of element.
       args is tuple of kinds of element

    Examples
    ========

    >>> from sympy import Tuple
    >>> Tuple(1, 2).kind
    TupleKind(NumberKind, NumberKind)
    >>> Tuple(1, 2).kind.element_kind
    (NumberKind, NumberKind)

    See Also
    ========

    sympy.core.kind.NumberKind
    MatrixKind
    sympy.sets.sets.SetKind
    c                    s   t  j| f| }||_|S r   )r<   r   element_kind)r   r   r   r>   r   r   r     s    zTupleKind.__new__c                 C   s   d | jS )NzTupleKind{})formatr   r!   r   r   r   r     s    zTupleKind.__repr__)rS   rT   rU   rV   r   r   rZ   r   r   r>   r   rR   v  s   rR   N)rV   collectionsr   collections.abcr   typingr   r   basicr   Zsortingr   r	   r   r
   r   r   Zsympy.core.kindr   Zsympy.utilities.iterablesr   Zsympy.utilities.miscr   r   r0   rc   rd   rj   r|   rR   r   r   r   r   <module>   s"    $s(