
    MZdJ                     r   d dl mZ d dlmZ d dlmZ d dlmZm	Z	 d dl
mZmZ 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mZ d d
lmZ d dlmZ d dlmZm Z  d dl!m"Z"  G d de      Z# G d de#      Z$ e"e$e      d        Z% G d de#      Z& e"e&e      d        Z% G d de      Z' e"e'e      d        Z%y)    )Tuple)Basic)Expr)AddS)get_integer_partPrecisionExhausted)Function)fuzzy_or)Integer)GtLtGeLe
Relationalis_eq)Symbol)_sympify)imre)dispatchc                   T    e Zd ZU dZee   ed<   ed        Zed        Z	d Z
d Zd Zy)	RoundFunctionz+Abstract base class for rounding functions.argsc                    | j                  |      }||S |j                  s|j                  du r|S |j                  st        j
                  |z  j                  rMt        |      }|j                  t        j
                        s | |      t        j
                  z  S  | |d      S t        j                  x}x}}t        j                  |      }|D ]U  }|j                  s!|j                  rt        |      j                  r||z  }6|j                  t              r||z  }Q||z  }W |s|s|S |r|rM|j                  r)|j                  s5t        j
                  |z  j                  s|j                  rd|j                  rX	 t        || j                  i d      \  }	}|t        |	      t        |      t        j
                  z  z   z  }t        j                  }||z  }|s|S |j                  st        j
                  |z  j                  r'| | t        |      d      t        j
                  z  z   S t%        |t&        t(        f      r||z   S | | |d      z   S # t         t"        f$ r Y w xY w)NFevaluateT)return_ints)_eval_number
is_integer	is_finiteis_imaginaryr   ImaginaryUnitis_realr   hasZeror   	make_argsr   r   _dirr   r	   NotImplementedError
isinstancefloorceiling)
clsargviipartnpartsparttermstrs
             E/usr/lib/python3/dist-packages/sympy/functions/elementary/integers.pyevalzRoundFunction.eval   s   S!=H>>S]]e3J 3<<3A55)1vaoo--sU++ !"&& c" 	A||2a53C3C
v

	 L MMu11aooe6K5T5T""u}}'388RT;1gaj&@@@ 	LAOOE$9#B#B3r%y59!//IIIw/05= 3uu555 '(;< s   AI I-,I-c                     t               N)r)   r-   r.   s     r7   r   zRoundFunction._eval_numberQ   s    !##    c                 4    | j                   d   j                  S Nr   )r   r!   selfs    r7   _eval_is_finitezRoundFunction._eval_is_finiteU   s    yy|%%%r<   c                 4    | j                   d   j                  S r>   r   r$   r?   s    r7   _eval_is_realzRoundFunction._eval_is_realX       yy|###r<   c                 4    | j                   d   j                  S r>   rC   r?   s    r7   _eval_is_integerzRoundFunction._eval_is_integer[   rE   r<   N)__name__
__module____qualname____doc__tTupler   __annotations__classmethodr8   r   rA   rD   rG    r<   r7   r   r      sE    5
,36 36j $ $&$$r<   r   c                   d    e Zd ZdZdZed        ZddZddZd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zy)r+   a  
    Floor is a univariate function which returns the largest integer
    value not greater than its argument. This implementation
    generalizes floor to complex numbers by taking the floor of the
    real and imaginary parts separately.

    Examples
    ========

    >>> from sympy import floor, E, I, S, Float, Rational
    >>> floor(17)
    17
    >>> floor(Rational(23, 10))
    2
    >>> floor(2*E)
    5
    >>> floor(-Float(0.567))
    -1
    >>> floor(-I/2)
    -I
    >>> floor(S(5)/2 + 5*I/2)
    2 + 2*I

    See Also
    ========

    sympy.functions.elementary.integers.ceiling

    References
    ==========

    .. [1] "Concrete mathematics" by Graham, pp. 87
    .. [2] https://mathworld.wolfram.com/FloorFunction.html

    c                     |j                   r|j                         S t        d || fD              r|S |j                  r|j	                  t
              d   S y )Nc              3   V   K   | ]!  }t         t        fD ]  }t        ||        # y wr:   r+   r,   r*   .0r0   js      r7   	<genexpr>z%floor._eval_number.<locals>.<genexpr>   :      @ug.>@)* Aq! @! @   ')r   )	is_Numberr+   anyis_NumberSymbolapproximation_intervalr   r;   s     r7   r   zfloor._eval_number   s\    ==99; @t@ @J--g6q99 r<   Nc                    ddl m} | j                  d   }|j                  |d      }| j                  |d      }|t        j
                  u st        ||      r6|j                  |dt        |      j                  rdnd      }t        |      }|j                  r-||k(  r&|j                  ||      }|j                  r|dz
  S |S |S |j                  |||      S 	Nr   AccumBounds-+dircdir   logxrh   )!sympy.calculus.accumulationboundsrb   r   subsr   NaNr*   limitr   is_negativer+   r!   rf   as_leading_term	r@   xrk   rh   rb   r.   arg0r6   ndirs	            r7   _eval_as_leading_termzfloor._eval_as_leading_term   s    Aiilxx1~IIaO155=Jt[999Qbh.B.Bs9LDdA>>qywwqtw, $ 0 0q1u7a7""14d";;r<   c                    | j                   d   }|j                  |d      }| j                  |d      }|t        j                  u r6|j	                  |dt        |      j                  rdnd      }t        |      }|j                  r>ddl	m
} ddlm}	 |j                  ||||      }
|dk  r |	d|df      n |dd      }|
|z   S ||k(  r-|j                  ||dk7  r|nd	      }|j                  r|dz
  S |S |S )
Nr   rc   rd   re   ra   Orderri   rQ   rg   )r   rm   r   rn   ro   r   rp   r+   is_infiniterl   rb   sympy.series.orderry   _eval_nseriesrf   r@   rs   nrk   rh   r.   rt   r6   rb   ry   soru   s                r7   r|   zfloor._eval_nseries   s    iilxx1~IIaO155=99Qbh.B.Bs9LDdAE0!!!Qd3A$%Fa!Q B0BAq5L197714194!7<D ,,1q53!3Hr<   c                 4    | j                   d   j                  S r>   )r   rp   r?   s    r7   _eval_is_negativezfloor._eval_is_negative       yy|'''r<   c                 4    | j                   d   j                  S r>   )r   is_nonnegativer?   s    r7   _eval_is_nonnegativezfloor._eval_is_nonnegative       yy|***r<   c                     t        |        S r:   r,   r@   r.   kwargss      r7   _eval_rewrite_as_ceilingzfloor._eval_rewrite_as_ceiling   s    ~r<   c                     |t        |      z
  S r:   fracr   s      r7   _eval_rewrite_as_fraczfloor._eval_rewrite_as_frac   s    T#Yr<   c                    t        |      }| j                  d   j                  rT|j                  r| j                  d   |dz   k  S |j                  r'|j                  r| j                  d   t        |      k  S | j                  d   |k(  r|j                  rt         j                  S |t         j                  u r| j                  rt         j                  S t        | |d      S Nr   ri   Fr   )
r   r   r$   r    	is_numberr,   trueInfinityr!   r   r@   others     r7   __le__zfloor.__le__   s    %99Q<yy|eai//5==yy|gen4499Q<5 U]]66MAJJ4>>66M$..r<   c                    t        |      }| j                  d   j                  rQ|j                  r| j                  d   |k\  S |j                  r'|j                  r| j                  d   t        |      k\  S | j                  d   |k(  r|j                  rt         j                  S |t         j                  u r| j                  rt         j                  S t        | |d      S Nr   Fr   )r   r   r$   r    r   r,   falseNegativeInfinityr!   r   r   r   s     r7   __ge__zfloor.__ge__   s    %99Q<yy|u,,5==yy|wu~5599Q<5 U]]77NA&&&4>>66M$..r<   c                    t        |      }| j                  d   j                  rT|j                  r| j                  d   |dz   k\  S |j                  r'|j                  r| j                  d   t        |      k\  S | j                  d   |k(  r|j                  rt         j                  S |t         j                  u r| j                  rt         j                  S t        | |d      S r   )r   r   r$   r    r   r,   r   r   r!   r   r   r   s     r7   __gt__zfloor.__gt__   s    %99Q<yy|uqy005==yy|wu~5599Q<5 U]]77NA&&&4>>66M$..r<   c                    t        |      }| j                  d   j                  rQ|j                  r| j                  d   |k  S |j                  r'|j                  r| j                  d   t        |      k  S | j                  d   |k(  r|j                  rt         j                  S |t         j                  u r| j                  rt         j                  S t        | |d      S r   )r   r   r$   r    r   r,   r   r   r!   r   r   r   s     r7   __lt__zfloor.__lt__   s    %99Q<yy|e++5==yy|gen4499Q<5 U]]77NAJJ4>>66M$..r<   r>   r   )rH   rI   rJ   rK   r(   rN   r   rv   r|   r   r   r   r   r   r   r   r   rO   r<   r7   r+   r+   _   sS    "F D: :< &(+////r<   r+   c                     t        | j                  t              |      xs t        | j                  t              |      S r:   )r   rewriter,   r   lhsrhss     r7   _eval_is_eqr      s2    
G$c
* %ckk$$%r<   c                   d    e Zd ZdZdZed        ZddZddZd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zy)r,   a  
    Ceiling is a univariate function which returns the smallest integer
    value not less than its argument. This implementation
    generalizes ceiling to complex numbers by taking the ceiling of the
    real and imaginary parts separately.

    Examples
    ========

    >>> from sympy import ceiling, E, I, S, Float, Rational
    >>> ceiling(17)
    17
    >>> ceiling(Rational(23, 10))
    3
    >>> ceiling(2*E)
    6
    >>> ceiling(-Float(0.567))
    0
    >>> ceiling(I/2)
    I
    >>> ceiling(S(5)/2 + 5*I/2)
    3 + 3*I

    See Also
    ========

    sympy.functions.elementary.integers.floor

    References
    ==========

    .. [1] "Concrete mathematics" by Graham, pp. 87
    .. [2] https://mathworld.wolfram.com/CeilingFunction.html

    ri   c                     |j                   r|j                         S t        d || fD              r|S |j                  r|j	                  t
              d   S y )Nc              3   V   K   | ]!  }t         t        fD ]  }t        ||        # y wr:   rT   rU   s      r7   rX   z'ceiling._eval_number.<locals>.<genexpr>'  rY   rZ   ri   )r[   r,   r\   r]   r^   r   r;   s     r7   r   zceiling._eval_number#  s\    ==;;=  @t@ @J--g6q99 r<   Nc                    ddl m} | j                  d   }|j                  |d      }| j                  |d      }|t        j
                  u st        ||      r6|j                  |dt        |      j                  rdnd      }t        |      }|j                  r-||k(  r&|j                  ||      }|j                  r|S |dz   S |S |j                  |||      S r`   )rl   rb   r   rm   r   rn   r*   ro   r   rp   r,   r!   rf   rq   rr   s	            r7   rv   zceiling._eval_as_leading_term-  s    Aiilxx1~IIaO155=Jt[999Qbh.B.Bs9LDA>>qywwqtw, ,,q7!a%7""14d";;r<   c                    | j                   d   }|j                  |d      }| j                  |d      }|t        j                  u r6|j	                  |dt        |      j                  rdnd      }t        |      }|j                  r>ddl	m
} ddlm}	 |j                  ||||      }
|dk  r |	d|df      n |dd      }|
|z   S ||k(  r-|j                  ||dk7  r|nd      }|j                  r|S |dz   S |S )	Nr   rc   rd   re   ra   rx   ri   rg   )r   rm   r   rn   ro   r   rp   r,   rz   rl   rb   r{   ry   r|   rf   r}   s                r7   r|   zceiling._eval_nseries=  s    iilxx1~IIaO155=99Qbh.B.Bs9LDAE0!!!Qd3A$%Fa!Q Aq0AAq5L197714194!7<D((13a!e3Hr<   c                     t        |        S r:   r+   r   s      r7   _eval_rewrite_as_floorzceiling._eval_rewrite_as_floorP  s    sd|r<   c                      |t        |       z   S r:   r   r   s      r7   r   zceiling._eval_rewrite_as_fracS  s    T3$Zr<   c                 4    | j                   d   j                  S r>   )r   is_positiver?   s    r7   _eval_is_positivezceiling._eval_is_positiveV  r   r<   c                 4    | j                   d   j                  S r>   )r   is_nonpositiver?   s    r7   _eval_is_nonpositivezceiling._eval_is_nonpositiveY  r   r<   c                    t        |      }| j                  d   j                  rT|j                  r| j                  d   |dz
  k  S |j                  r'|j                  r| j                  d   t        |      k  S | j                  d   |k(  r|j                  rt         j                  S |t         j                  u r| j                  rt         j                  S t        | |d      S r   )r   r   r$   r    r   r+   r   r   r!   r   r   r   s     r7   r   zceiling.__lt__\  s    %99Q<yy|uqy005==yy|uU|3399Q<5 U]]77NAJJ4>>66M$..r<   c                    t        |      }| j                  d   j                  rQ|j                  r| j                  d   |kD  S |j                  r'|j                  r| j                  d   t        |      kD  S | j                  d   |k(  r|j                  rt         j                  S |t         j                  u r| j                  rt         j                  S t        | |d      S r   )r   r   r$   r    r   r+   r   r   r!   r   r   r   s     r7   r   zceiling.__gt__j  s    %99Q<yy|e++5==yy|eEl2299Q<5 U]]77NA&&&4>>66M$..r<   c                    t        |      }| j                  d   j                  rT|j                  r| j                  d   |dz
  kD  S |j                  r'|j                  r| j                  d   t        |      kD  S | j                  d   |k(  r|j                  rt         j                  S |t         j                  u r| j                  rt         j                  S t        | |d      S r   )
r   r   r$   r    r   r+   r   r   r!   r   r   s     r7   r   zceiling.__ge__x  s    %99Q<yy|eai//5==yy|eEl2299Q<5 U]]66MA&&&4>>66M$..r<   c                    t        |      }| j                  d   j                  rQ|j                  r| j                  d   |k  S |j                  r'|j                  r| j                  d   t        |      k  S | j                  d   |k(  r|j                  rt         j                  S |t         j                  u r| j                  rt         j                  S t        | |d      S r   )r   r   r$   r    r   r+   r   r   r!   r   r   r   s     r7   r   zceiling.__le__  s    %99Q<yy|u,,5==yy|uU|3399Q<5 U]]77NAJJ4>>66M$..r<   r>   r   )rH   rI   rJ   rK   r(   rN   r   rv   r|   r   r   r   r   r   r   r   r   rO   r<   r7   r,   r,      sS    "F D: :< & (+////r<   r,   c                     t        | j                  t              |      xs t        | j                  t              |      S r:   )r   r   r+   r   r   s     r7   r   r     s-    U#S)IU3;;t3DS-IIr<   c                   ~    e Zd Z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 Zd Zd ZddZddZy)r   a  Represents the fractional part of x

    For real numbers it is defined [1]_ as

    .. math::
        x - \left\lfloor{x}\right\rfloor

    Examples
    ========

    >>> from sympy import Symbol, frac, Rational, floor, I
    >>> frac(Rational(4, 3))
    1/3
    >>> frac(-Rational(4, 3))
    2/3

    returns zero for integer arguments

    >>> n = Symbol('n', integer=True)
    >>> frac(n)
    0

    rewrite as floor

    >>> x = Symbol('x')
    >>> frac(x).rewrite(floor)
    x - floor(x)

    for complex arguments

    >>> r = Symbol('r', real=True)
    >>> t = Symbol('t', real=True)
    >>> frac(t + I*r)
    I*frac(r) + frac(t)

    See Also
    ========

    sympy.functions.elementary.integers.floor
    sympy.functions.elementary.integers.ceiling

    References
    ===========

    .. [1] https://en.wikipedia.org/wiki/Fractional_part
    .. [2] https://mathworld.wolfram.com/FractionalPart.html

    c                     ddl m  fd}t        j                  |      }t        j
                  t        j
                  }}|D ]f  }|j                  st        j                  |z  j                  r6t        |      }|j                  t        j                        s||z  }\||z  }b||z  }h  ||      } ||      }|t        j                  |z  z   S )Nr   ra   c                 b   | t         j                  t         j                  fv r	 dd      S | j                  rt         j                  S | j
                  rR| t         j                  u rt         j                  S | t         j                  u rt         j                  S | t        |       z
  S  | d      S r   )	r   r   r   r    r&   r   rn   ComplexInfinityr+   )r.   rb   r-   s    r7   _evalzfrac.eval.<locals>._eval  s    qzz1#5#566"1a((~~vv}}!%%<55LA---55Ls++sU++r<   )rl   rb   r   r'   r   r&   r"   r#   r$   r   r%   )	r-   r.   r   r4   realimagr5   r0   rb   s	   `       @r7   r8   z	frac.eval  s    A	, c"VVQVVd 
	A ~~!//!"3!<!<qEuuQ__-AIDAID	
	 T{T{aood***r<   c                     |t        |      z
  S r:   r   r   s      r7   r   zfrac._eval_rewrite_as_floor  s    U3Zr<   c                      |t        |       z   S r:   r   r   s      r7   r   zfrac._eval_rewrite_as_ceiling  s    WcT]""r<   c                      y)NTrO   r?   s    r7   rA   zfrac._eval_is_finite  s    r<   c                 4    | j                   d   j                  S r>   )r   is_extended_realr?   s    r7   rD   zfrac._eval_is_real  s    yy|,,,r<   c                 4    | j                   d   j                  S r>   )r   r"   r?   s    r7   _eval_is_imaginaryzfrac._eval_is_imaginary  s    yy|(((r<   c                 4    | j                   d   j                  S r>   )r   r    r?   s    r7   rG   zfrac._eval_is_integer  s    yy|&&&r<   c                 x    t        | j                  d   j                  | j                  d   j                  g      S r>   )r   r   is_zeror    r?   s    r7   _eval_is_zerozfrac._eval_is_zero  s.    1--tyy|/F/FGHHr<   c                      y)NFrO   r?   s    r7   r   zfrac._eval_is_negative  s    r<   c                     | j                   r=t        |      }|j                  rt        j                  S | j                  |      }|| S t        | |d      S NFr   )r   r   is_extended_nonpositiver   r   _value_one_or_morer   r@   r   ress      r7   r   zfrac.__ge__  sQ      UOE,,vv))%0Cx$..r<   c                     | j                   r=t        |      }| j                  |      }|| S |j                  rt        j
                  S t        | |d      S r   )r   r   r   is_extended_negativer   r   r   r   s      r7   r   zfrac.__gt__  sQ      UOE))%0Cx))vv$..r<   c                     | j                   r<t        |      }|j                  rt        j                  S | j                  |      }||S t        | |d      S r   )r   r   r   r   r   r   r   r   s      r7   r   zfrac.__le__  sO      UOE))ww))%0C
$..r<   c                     | j                   r<t        |      }|j                  rt        j                  S | j                  |      }||S t        | |d      S r   )r   r   r   r   r   r   r   r   s      r7   r   zfrac.__lt__+  sO      UOE,,ww))%0C
$..r<   c                     |j                   r]|j                  r'|dk\  }|r t        |t              st        j
                  S |j                  r|j                  rt        j
                  S y y y )Nri   )r   r   r*   r   r   r   r    r   r   s      r7   r   zfrac._value_one_or_more7  sW    !!qjz#z:66ME$5$5vv %6 "r<   Nc                    ddl m} | j                  d   }|j                  |d      }| j                  |d      }|j                  rT|j
                  rF|j                  ||      }|j                  rt        j                  S ||z
  j                  |||      S |S |t        j                  t        j                  t        j                  fv r	 |dd      S |j                  |||      S )Nr   ra   rg   rj   ri   )rl   rb   r   rm   r!   r   rf   rp   r   Onerq   r   r   r   rr   s	            r7   rv   zfrac._eval_as_leading_term@  s    Aiilxx1~IIaO>>yywwqtw,##55Ld
33ADt3LLa''Q5G5GHHq!$$""14d";;r<   c                    ddl m} | j                  d   }|j                  |d      }| j                  |d      }|j                  r2ddlm}	 |dk  r |d|df      }
|
S  |	dd       |||z  |df      z   }
|
S ||z
  j                  ||||      }|j                  rH|j                  ||      }||j                  rt        j                  z  }|S t        j                  z  }|S ||z  }|S )Nr   rx   ra   ri   rj   rg   )r{   ry   r   rm   rz   rl   rb   r|   r   rf   rp   r   r   r&   )r@   rs   r~   rk   rh   ry   r.   rt   r6   rb   r   r   ru   s                r7   r|   zfrac._eval_nseriesR  s    ,iilxx1~IIaOE$%Fa!Q AH 1<Aq0AE!Q$QRTUPVDW0WAH:,,Q4,HCyywwqtw, 0 0quu< J 78ff< J qJr<   r>   r   )rH   rI   rJ   rK   rN   r8   r   r   rA   rD   r   rG   r   r   r   r   r   r   r   rv   r|   rO   r<   r7   r   r     si    /` !+ !+F #-)'I
/
/
/
/<$r<   r   c                     | j                  t              |k(  s| j                  t              |k(  ry|j                  ry| j	                  |      }|yy )NTF)r   r+   r,   r   r   )r   r   r   s      r7   r   r   f  sP    Ec!	W		$


 
 
%C
 r<   N)(typingr   rL   sympy.core.basicr   sympy.core.exprr   
sympy.corer   r   sympy.core.evalfr   r	   sympy.core.functionr
   sympy.core.logicr   sympy.core.numbersr   sympy.core.relationalr   r   r   r   r   r   sympy.core.symbolr   sympy.core.sympifyr   $sympy.functions.elementary.complexesr   r   sympy.multipledispatchr   r   r+   r   r,   r   rO   r<   r7   <module>r      s    " "    A ( % & C C $ ' 7 +F$H F$RU/M U/p 
%% %
U/m U/p 
'5J JI8 IX 
$
 
r<   