o
    8VaC                     @   s  d dl mZ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 G dd de
ZG dd deZeeedd ZG dd deZeeedd ZG dd de
Z ee edd ZdS )    )BasicExpr)AddS)get_integer_partPrecisionExhausted)Function)fuzzy_or)Integer)GtLtGeLe
Relationalis_eq)Symbol)_sympify)dispatchc                   @   s4   e Zd ZdZedd Zdd Zdd Zdd	 Zd
S )RoundFunctionz&The base class for rounding functions.c              	   C   s  ddl m} | |}|d ur|S |js|jdu r|S |js$tj| jr;||}|	tjs5| |tj S | |ddS tj
 } }}t|}|D ] }	|	jsV|	jr[||	jr[||	7 }qI|		tre||	7 }qI||	7 }qI|sp|sp|S |r|r|jr|jstj| js|jr|jrzt|| ji dd\}
}|t|
t|tj  7 }tj
}W n ttfy   Y nw ||7 }|s|S |jstj| jr|| ||ddtj  S t|ttfr|| S || |dd S )Nr   )imFZevaluateT)Zreturn_ints)sympyr   _eval_number
is_integer	is_finiteis_imaginaryr   ImaginaryUnitis_realhasZeror   	make_argsr   r   _dirr
   r   NotImplementedError
isinstancefloorceiling)clsargr   viZipartZnpartZsparttermstr r-   E/usr/lib/python3/dist-packages/sympy/functions/elementary/integers.pyeval   sf   









zRoundFunction.evalc                 C      | j d jS Nr   )argsr   selfr-   r-   r.   _eval_is_finiteL      zRoundFunction._eval_is_finitec                 C   r0   r1   r2   r   r3   r-   r-   r.   _eval_is_realO   r6   zRoundFunction._eval_is_realc                 C   r0   r1   r7   r3   r-   r-   r.   _eval_is_integerR   r6   zRoundFunction._eval_is_integerN)	__name__
__module____qualname____doc__classmethodr/   r5   r8   r9   r-   r-   r-   r.   r      s    
6r   c                   @   t   e Zd ZdZdZedd ZdddZd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 ZdS )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] http://mathworld.wolfram.com/FloorFunction.html

    c                 C   B   |j r| S tdd || fD r|S |jr|td S d S )Nc                 s   (    | ]}t tfD ]}t||V  qqd S Nr$   r%   r#   .0r)   jr-   r-   r.   	<genexpr>       z%floor._eval_number.<locals>.<genexpr>r   )	is_Numberr$   anyis_NumberSymbolapproximation_intervalr
   r&   r'   r-   r-   r.   r   |      zfloor._eval_numberNr   c           	      C   s   | j d }||d}| |d}|jrG||krE|dkr5|j|dd}|j|dd}||kr4td|  n|j||d}|jrC|d S |S |S |j|||dS Nr   r@   cdir   z,Two sided limit of %s around 0does not exist)logxrR   r2   subsr   dir
ValueErroris_negativeZas_leading_term	r4   xrT   rR   r'   arg0r,   Zndirlndirr-   r-   r.   _eval_as_leading_term   "   
zfloor._eval_as_leading_termc                 C   s   | j d }||d}|jr6ddlm} ddlm} |||||}	|dkr-|d|dfn|dd}
|	|
 S | |d}||krV|j||dkrI|ndd}|j	rT|d S |S |S )Nr   AccumBoundsOrderrS   r@   rQ   
r2   rV   Zis_infiniteZsympy.calculus.utilra   Zsympy.series.orderrc   _eval_nseriesrW   rY   r4   r[   nrT   rR   r'   r\   ra   rc   sor,   r]   r-   r-   r.   re         
 zfloor._eval_nseriesc                 C   r0   r1   )r2   rY   r3   r-   r-   r.   _eval_is_negative   r6   zfloor._eval_is_negativec                 C   r0   r1   )r2   Zis_nonnegativer3   r-   r-   r.   _eval_is_nonnegative   r6   zfloor._eval_is_nonnegativec                 K      t |  S rC   r%   r4   r'   kwargsr-   r-   r.   _eval_rewrite_as_ceiling   r6   zfloor._eval_rewrite_as_ceilingc                 K      |t | S rC   fracro   r-   r-   r.   _eval_rewrite_as_frac   r6   zfloor._eval_rewrite_as_fracc                 C   s   t |}| jd jr%|jr| jd |d k S |jr%|jr%| jd t|k S | jd |kr2|jr2t jS |t ju r=| jr=t jS t	| |ddS Nr   rS   Fr   )
r   r2   r   r   	is_numberr%   trueInfinityr   r   r4   otherr-   r-   r.   __le__      zfloor.__le__c                 C   s   t |}| jd jr#|jr| jd |kS |jr#|jr#| jd t|kS | jd |kr0|jr0t jS |t ju r;| jr;t j	S t
| |ddS Nr   Fr   )r   r2   r   r   rw   r%   falseNegativeInfinityr   rx   r   rz   r-   r-   r.   __ge__      zfloor.__ge__c                 C   s   t |}| jd jr%|jr| jd |d kS |jr%|jr%| jd t|kS | jd |kr2|jr2t jS |t ju r=| jr=t j	S t
| |ddS rv   )r   r2   r   r   rw   r%   r   r   r   rx   r   rz   r-   r-   r.   __gt__   r}   zfloor.__gt__c                 C   s   t |}| jd jr#|jr| jd |k S |jr#|jr#| jd t|k S | jd |kr0|jr0t jS |t ju r;| jr;t j	S t
| |ddS r~   )r   r2   r   r   rw   r%   r   ry   r   rx   r   rz   r-   r-   r.   __lt__   r   zfloor.__lt__r1   r   )r:   r;   r<   r=   r!   r>   r   r^   re   rk   rl   rq   ru   r|   r   r   r   r-   r-   r-   r.   r$   V       #

	
r$   c                 C       t | t|pt | t|S rC   )r   rewriter%   rt   lhsrhsr-   r-   r.   _eval_is_eq   s   r   c                   @   r?   )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] http://mathworld.wolfram.com/CeilingFunction.html

    rS   c                 C   rA   )Nc                 s   rB   rC   rD   rE   r-   r-   r.   rH     rI   z'ceiling._eval_number.<locals>.<genexpr>rS   )rJ   r%   rK   rL   rM   r
   rN   r-   r-   r.   r     rO   zceiling._eval_numberNr   c           	      C   s   | j d }||d}| |d}|jrG||krE|dkr5|j|dd}|j|dd}||kr4td|  n|j||d}|jrA|S |d S |S |j|||dS rP   rU   rZ   r-   r-   r.   r^   $  r_   zceiling._eval_as_leading_termc                 C   s   | j d }||d}|jr6ddlm} ddlm} |||||}	|dkr-|d|dfn|dd}
|	|
 S | |d}||krV|j||dkrI|ndd}|j	rR|S |d S |S )Nr   r`   rb   rS   rQ   rd   rf   r-   r-   r.   re   7  rj   zceiling._eval_nseriesc                 K   rm   rC   r$   ro   r-   r-   r.   _eval_rewrite_as_floorG  r6   zceiling._eval_rewrite_as_floorc                 K      |t |  S rC   rs   ro   r-   r-   r.   ru   J     zceiling._eval_rewrite_as_fracc                 C   r0   r1   )r2   is_positiver3   r-   r-   r.   _eval_is_positiveM  r6   zceiling._eval_is_positivec                 C   r0   r1   )r2   Zis_nonpositiver3   r-   r-   r.   _eval_is_nonpositiveP  r6   zceiling._eval_is_nonpositivec                 C   s   t |}| jd jr%|jr| jd |d kS |jr%|jr%| jd t|kS | jd |kr2|jr2t jS |t ju r=| jr=t j	S t
| |ddS rv   )r   r2   r   r   rw   r$   r   ry   r   rx   r   rz   r-   r-   r.   r   S  r}   zceiling.__lt__c                 C   s   t |}| jd jr#|jr| jd |kS |jr#|jr#| jd t|kS | jd |kr0|jr0t jS |t ju r;| jr;t j	S t
| |ddS r~   )r   r2   r   r   rw   r$   r   r   r   rx   r   rz   r-   r-   r.   r   a  r   zceiling.__gt__c                 C   s   t |}| jd jr%|jr| jd |d kS |jr%|jr%| jd t|kS | jd |kr2|jr2t jS |t ju r=| jr=t jS t	| |ddS rv   )
r   r2   r   r   rw   r$   rx   r   r   r   rz   r-   r-   r.   r   o  r}   zceiling.__ge__c                 C   s   t |}| jd jr#|jr| jd |kS |jr#|jr#| jd t|kS | jd |kr0|jr0t jS |t ju r;| jr;t j	S t
| |ddS r~   )r   r2   r   r   rw   r$   r   ry   r   rx   r   rz   r-   r-   r.   r|   }  r   zceiling.__le__r1   r   )r:   r;   r<   r=   r!   r>   r   r^   re   r   ru   r   r   r   r   r   r|   r-   r-   r-   r.   r%      r   r%   c                 C   r   rC   )r   r   r$   rt   r   r-   r-   r.   r     s    c                   @   s   e Zd ZdZe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 Zdd Zdd Zdd Zdd Zdd ZdS )rt   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] http://mathworld.wolfram.com/FractionalPart.html

    c           	         s   ddl m m}  fdd}t|}tjtj}}|D ]#}|js(tj| j	r<||}|
tjs7||7 }q||7 }q||7 }q||}||}|tj|  S )Nr   )ra   r   c                    sh   | t ju s
| t ju r ddS | jrt jS | jr.| t ju r t jS | t ju r(t jS | t|  S | ddS rv   )	r   ry   r   r   r   rw   ZNaNZComplexInfinityr$   )r'   ra   r&   r-   r.   _eval  s   


zfrac.eval.<locals>._eval)r   ra   r   r   r    r   r   r   r   r   r   )	r&   r'   r   r   r*   realimagr+   r)   r-   r   r.   r/     s   



z	frac.evalc                 K   rr   rC   r   ro   r-   r-   r.   r     r6   zfrac._eval_rewrite_as_floorc                 K   r   rC   rn   ro   r-   r-   r.   rq     r   zfrac._eval_rewrite_as_ceilingc                 C      dS )NTr-   r3   r-   r-   r.   r5        zfrac._eval_is_finitec                 C   r0   r1   )r2   is_extended_realr3   r-   r-   r.   r8     r6   zfrac._eval_is_realc                 C   r0   r1   )r2   r   r3   r-   r-   r.   _eval_is_imaginary  r6   zfrac._eval_is_imaginaryc                 C   r0   r1   )r2   r   r3   r-   r-   r.   r9     r6   zfrac._eval_is_integerc                 C   s   t | jd j| jd jgS r1   )r	   r2   Zis_zeror   r3   r-   r-   r.   _eval_is_zero  s   zfrac._eval_is_zeroc                 C   r   )NFr-   r3   r-   r-   r.   rk     r   zfrac._eval_is_negativec                 C   s@   | j rt|}|jrtjS | |}|d ur| S t| |ddS NFr   )r   r   is_extended_nonpositiver   rx   _value_one_or_morer   r4   r{   resr-   r-   r.   r     s   
zfrac.__ge__c                 C   s@   | j rt|}| |}|d ur| S |jrtjS t| |ddS r   )r   r   r   is_extended_negativer   rx   r   r   r-   r-   r.   r   
  s   
zfrac.__gt__c                 C   >   | j rt|}|jrtjS | |}|d ur|S t| |ddS r   )r   r   r   r   r   r   r   r   r-   r-   r.   r|        
zfrac.__le__c                 C   r   r   )r   r   r   r   r   r   r   r   r-   r-   r.   r   "  r   zfrac.__lt__c                 C   sF   |j r|jr|dk}|rt|tstjS |jr|jr!tjS d S d S d S )NrS   )r   rw   r#   r   r   rx   r   r   r   r-   r-   r.   r   .  s   zfrac._value_one_or_moreN)r:   r;   r<   r=   r>   r/   r   rq   r5   r8   r   r9   r   rk   r   r   r|   r   r   r-   r-   r-   r.   rt     s"    0
#rt   c                 C   sD   |  t|ks|  t|krdS |jrdS | |}|d ur dS d S )NTF)r   r$   r%   r   r   )r   r   r   r-   r-   r.   r   8  s   
N)!r   r   r   Z
sympy.corer   r   Zsympy.core.evalfr   r   Zsympy.core.functionr   Zsympy.core.logicr	   Zsympy.core.numbersr
   Zsympy.core.relationalr   r   r   r   r   r   Zsympy.core.symbolr   Zsympy.core.sympifyr   Zsympy.multipledispatchr   r   r$   r   r%   rt   r-   r-   r-   r.   <module>   s.     D 
 
 (