U
    9%e                    @  s"  d dl mZ d dlZd dlZd dlZd dlZd dlZd dl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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! ddl"m#Z# ddl$m%Z% d dl&m'Z'm(Z(m)Z) d dl*m+Z+ d dl,Z,d dl-m.Z/ d dl-m0Z0m1Z2 d dl3m4Z4 d dl-m5Z5m6Z6m7Z7m8Z8 d dl9m:Z: d dl;m<Z=m>Z?m@ZAmBZBmCZDmEZEmFZF d dlGmHZHmIZImJZJ ddlKmLZL eMdZNdxddZOdd ZPddiZQdydd ZRd!d" ZSd#d$ ZTeUd%ZVd&d' ZWe
d(d)d* ZXejYZZd+d, Z[d-d. Z\d/d0 Z]d1d2 Z^G d3d4 d4eZ_G d5d6 d6e_Z`e` eea< eejb< e`ZcG d7d8 d8e_ZdG d9d: d:edZeeeeef< G d;d< d<eZgG d=d> d>edZhG d?d@ d@eeZiG dAdB dBeiedCZjG dDdE dEeiedCZkG dFdG dGeiedCZlG dHdI dIehedCZmG dJdK dKe_edCZnejnZoG dLdM dMe_edCZpG dNdO dOe_edCZqejqZre+eqedPdQ ZsG dRdS dSeedCZtejtZuG dTdU dUeZvG dVdW dWevedCZwejwZxG dXdY dYevedCZyejyZzG dZd[ d[evedCZ{G d\d] d]evedCZ|G d^d_ d_evedCZ}G d`da daevedCZ~G dbdc dceedCZejZddde Ze+ee_dfdQ Zsdgdh Zeeej< e(rdidj Zdkdl Zeeee)d< eeee)dd< dmdn Zeeee,jdd< dodp Zeee:< dqdr Zeee< ddslmZmZ ddtlmZ ek e_ddulmZ ej e_dvdw Ze  ejqejnejpejtfZdS )z    )annotationsN)	lru_cache   )Tuple)SympifyError_sympy_convertersympify_convert_numpy_types_sympify_is_numpy_instance)S	Singleton)Basic)Expr
AtomicExpr)pure_complex)cacheitclear_cache)
_sympifyit)	fuzzy_not)
NumberKind)
SYMPY_INTSHAS_GMPYgmpy)dispatch)bitcountround_nearestMPZ)mpf_powmpf_pimpf_e	phi_fixed)	mpnumeric)finffninffnanfzero
_normalizeprec_to_dpsdps_to_prec)as_intdebug
filldedent)global_parameters   c                 C  s  t |tr*t| ddstdt| |kS | s8||  } }| s@dS |s| | }}|dkrht|t|kS |dkrt|t| }}tdd ||fD std|t}|t}|s|s||kS td	D ]^}t|dd}|s|r|	t
td
d |D }t|dd} q.q|| }}|| }}qt|}	|	sj|rj|	t
tdd |D }t|dd}	|r|	r|d s|	d rtdd t||	D S dt
t|jt|d|j }tt|| | dkS t| | }
t| }|r|dkr|
| |kS |
|kS dS )a  Return a bool indicating whether the error between z1 and z2
    is $\le$ ``tol``.

    Examples
    ========

    If ``tol`` is ``None`` then ``True`` will be returned if
    :math:`|z1 - z2|\times 10^p \le 5` where $p$ is minimum value of the
    decimal precision of each value.

    >>> from sympy import comp, pi
    >>> pi4 = pi.n(4); pi4
    3.142
    >>> comp(_, 3.142)
    True
    >>> comp(pi4, 3.141)
    False
    >>> comp(pi4, 3.143)
    False

    A comparison of strings will be made
    if ``z1`` is a Number and ``z2`` is a string or ``tol`` is ''.

    >>> comp(pi4, 3.1415)
    True
    >>> comp(pi4, 3.1415, '')
    False

    When ``tol`` is provided and $z2$ is non-zero and
    :math:`|z1| > 1` the error is normalized by :math:`|z1|`:

    >>> abs(pi4 - 3.14)/pi4
    0.000509791731426756
    >>> comp(pi4, 3.14, .001)  # difference less than 0.1%
    True
    >>> comp(pi4, 3.14, .0005)  # difference less than 0.1%
    False

    When :math:`|z1| \le 1` the absolute error is used:

    >>> 1/pi4
    0.3183
    >>> abs(1/pi4 - 0.3183)/(1/pi4)
    3.07371499106316e-5
    >>> abs(1/pi4 - 0.3183)
    9.78393554684764e-6
    >>> comp(1/pi4, 0.3183, 1e-5)
    True

    To see if the absolute error between ``z1`` and ``z2`` is less
    than or equal to ``tol``, call this as ``comp(z1 - z2, 0, tol)``
    or ``comp(z1 - z2, tol=tol)``:

    >>> abs(pi4 - 3.14)
    0.00160156249999988
    >>> comp(pi4 - 3.14, 0, .002)
    True
    >>> comp(pi4 - 3.14, 0, .001)
    False
    T)Zor_realz$when z2 is a str z1 must be a Number Nc                 s  s   | ]}|j V  qd S N)	is_number.0i r6   Q/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/sympy/core/numbers.py	<genexpr>s   s     zcomp.<locals>.<genexpr>zexpecting 2 numbersr/   c                 S  s   g | ]
}|j qS r6   _precr3   r6   r6   r7   
<listcomp>   s     zcomp.<locals>.<listcomp>c                 S  s   g | ]
}|j qS r6   r9   r3   r6   r6   r7   r;      s     r   c                 s  s   | ]\}}t ||V  qd S r1   )comp)r4   r5   jr6   r6   r7   r8      s     
   r:      )
isinstancestrr   
ValueErrorr   allZatomsFloatrangenr)   minzipr:   getattrintabs)Zz1Zz2ZtolabfaZfb_cacbdiffZaz1r6   r6   r7   r<   (   sT    =






 r<   c                 C  sB   | \}}}}|s|st S | S ddlm} t||||||t}|S )ae  Return the mpf tuple normalized appropriately for the indicated
    precision after doing a check to see if zero should be returned or
    not when the mantissa is 0. ``mpf_normlize`` always assumes that this
    is zero, but it may not be since the mantissa for mpf's values "+inf",
    "-inf" and "nan" have a mantissa of zero, too.

    Note: this is not intended to validate a given mpf tuple, so sending
    mpf tuples that were not created by mpmath may produce bad results. This
    is only a wrapper to ``mpf_normalize`` which provides the check for non-
    zero mpfs that have a 0 for the mantissa.
    r   r   )r'   mpmath.libmp.backendr   mpf_normalizernd)mpfprecsignmanexptbcr   rvr6   r6   r7   mpf_norm   s    r]   divideFc                 C  s   t d | krt  | t d< dS )z
    Should SymPy raise an exception on 0/0 or return a nan?

    divide == True .... raise an exception
    divide == False ... return nan
    r^   N)_errdictr   )r^   r6   r6   r7   seterr   s    r`   c                 C  sd   t | dt| j\}}}}ddg|d  | } |dk rDd|  }nd}| d| 9 } t| t|fS )N_mpf_r   r/   r   )rI   mpmathrV   ra   rJ   )pZneg_powrY   rZ   rO   qr6   r6   r7   _as_integer_ratio   s    rf   c                 C  s~   |   std|  |  \}}}t|}|dkr@tt| }n6d| }tdd tt|D }t	|| d|  }||fS )z3Convert an ordinary decimal instance to a Rational.zdec must be finite, got %s.r   rb   c                 S  s   g | ]\}}|d |  qS )r>   r6   )r4   r5   Zdir6   r6   r7   r;      s     z-_decimal_to_Rational_prec.<locals>.<listcomp>r>   )
	is_finite	TypeErroras_tuplelenIntegerrJ   sum	enumeratereversedRational)decsderW   r\   r6   r6   r7   _decimal_to_Rational_prec   s    rt   z[-+]?((\d*\.\d+)|(\d+\.?))c                 C  s   t t| S )z5Return True if n starts like a floating point number.)bool	_floatpatmatchfr6   r6   r7   _literal_float   s    rz   i   c                  G  s   t | dk rtdt |  dd | D }d|kr6dS | }trh|D ]}|rZt||n|}qFt|S |D ]}t||}ql|S )a  Computes nonnegative integer greatest common divisor.

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

    The algorithm is based on the well known Euclid's algorithm [1]_. To
    improve speed, ``igcd()`` has its own caching mechanism.

    Examples
    ========

    >>> from sympy import igcd
    >>> igcd(2, 4)
    2
    >>> igcd(5, 10, 15)
    5

    References
    ==========

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

    r/   z,igcd() takes at least 2 arguments (%s given)c                 S  s   g | ]}t t|qS r6   )rK   r+   r3   r6   r6   r7   r;     s     zigcd.<locals>.<listcomp>r   )rj   rh   popr   r   gcdr+   math)argsZ	args_temprL   rM   r6   r6   r7   igcd   s    
r   c                 C  s  t t| t t| } }| |k r,||  } }dtjj }|  |kr|dkr|  | }t| |? t||?  }}d\}}}}	|| dkrqh|| ||  }
||
|  ||
|	   }}|| dk r̐qh|| }}||	||
|  |f\}}}}	||	 dkrqh|| ||	  }
||
|  ||
|   }}|| dk r@qh|| }}||	|||
|	  f\}}}}	q|dkr|| |  } }q8||  ||  ||  |	|   } }q8|r|| |  } }q| S )a  Computes greatest common divisor of two integers.

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

    Euclid's algorithm for the computation of the greatest
    common divisor ``gcd(a, b)``  of two (positive) integers
    $a$ and $b$ is based on the division identity
       $$ a = q \times b + r$$,
    where the quotient  $q$  and the remainder  $r$  are integers
    and  $0 \le r < b$. Then each common divisor of  $a$  and  $b$
    divides  $r$, and it follows that  ``gcd(a, b) == gcd(b, r)``.
    The algorithm works by constructing the sequence
    r0, r1, r2, ..., where  r0 = a, r1 = b,  and each  rn
    is the remainder from the division of the two preceding
    elements.

    In Python, ``q = a // b``  and  ``r = a % b``  are obtained by the
    floor division and the remainder operations, respectively.
    These are the most expensive arithmetic operations, especially
    for large  a  and  b.

    Lehmer's algorithm [1]_ is based on the observation that the quotients
    ``qn = r(n-1) // rn``  are in general small integers even
    when  a  and  b  are very large. Hence the quotients can be
    usually determined from a relatively small number of most
    significant bits.

    The efficiency of the algorithm is further enhanced by not
    computing each long remainder in Euclid's sequence. The remainders
    are linear combinations of  a  and  b  with integer coefficients
    derived from the quotients. The coefficients can be computed
    as far as the quotients can be determined from the chosen
    most significant parts of  a  and  b. Only then a new pair of
    consecutive remainders is computed and the algorithm starts
    anew with this pair.

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Lehmer%27s_GCD_algorithm

    r/   r   r   r   r   r   )rK   r+   sysint_infobits_per_digit
bit_lengthrJ   )rL   rM   nbitsrF   xyABCDre   Zx_qyZB_qDZA_qCr6   r6   r7   igcd_lehmer  s>    ,



$r   c                  G  sX   t | dk rtdt |  d| kr(dS | d }| dd D ]}|t|| | }q<|S )zComputes integer least common multiple.

    Examples
    ========

    >>> from sympy import ilcm
    >>> ilcm(5, 10)
    10
    >>> ilcm(7, 3)
    21
    >>> ilcm(5, 10, 15)
    30

    r/   z,ilcm() takes at least 2 arguments (%s given)r   r   N)rj   rh   r   )r~   rL   rM   r6   r6   r7   ilcm  s    
r   c           
      C  s   | s|sdS | s&d|t | t |fS |s@| t |  dt | fS | dk rV|  d } }nd}|dk rp| d }}nd}d\}}}}|r| | | |  }}	||||	|  ||	|  ||f\} }}}}}q|| || | fS )a0  Returns x, y, g such that g = x*a + y*b = gcd(a, b).

    Examples
    ========

    >>> from sympy.core.numbers import igcdex
    >>> igcdex(2, 3)
    (-1, 1, 1)
    >>> igcdex(10, 12)
    (-1, 1, 2)

    >>> x, y, g = igcdex(100, 2004)
    >>> x, y, g
    (-20, 1, 4)
    >>> x*100 + y*2004
    4

    )r   r   r   r   rb   r   r   )rK   )
rL   rM   Zx_signZy_signr   r   rrq   cre   r6   r6   r7   igcdex  s"    .r   c                 C  s   d}zFt | t | } }|dkrH|dkrHt| |\}}}|dkrH|| }W np tk
r   t| t| } }| jrx|jsttd|dk}|tjtj	fkrtd| n|rd|  }Y nX |dkrtd| |f |S )a  
    Return the number $c$ such that, $a \times c = 1 \pmod{m}$
    where $c$ has the same sign as $m$. If no such value exists,
    a ValueError is raised.

    Examples
    ========

    >>> from sympy import mod_inverse, S

    Suppose we wish to find multiplicative inverse $x$ of
    3 modulo 11. This is the same as finding $x$ such
    that $3x = 1 \pmod{11}$. One value of x that satisfies
    this congruence is 4. Because $3 \times 4 = 12$ and $12 = 1 \pmod{11}$.
    This is the value returned by ``mod_inverse``:

    >>> mod_inverse(3, 11)
    4
    >>> mod_inverse(-3, 11)
    7

    When there is a common factor between the numerators of
    `a` and `m` the inverse does not exist:

    >>> mod_inverse(2, 4)
    Traceback (most recent call last):
    ...
    ValueError: inverse of 2 mod 4 does not exist

    >>> mod_inverse(S(2)/7, S(5)/2)
    7/2

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Modular_multiplicative_inverse
    .. [2] https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm
    Nr   rb   z
                Expected numbers for arguments; symbolic `mod_inverse`
                is not implemented
                but symbolic expressions can be handled with the
                similar function,
                sympy.polys.polytools.invertz*m > 1 did not evaluate; try to simplify %sz%inverse of %s (mod %s) does not exist)
r+   r   rB   r   r2   rh   r-   r   truefalse)rL   mr   r   rO   gbigr6   r6   r7   mod_inverse  s&    'r   c                      s  e Zd ZdZdZdZdZdZdZe	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d  Zd!d" Zd#d$ Zed%d& ZedSd(d)Zed*e d+d, Z!ed*e d-d. Z"ed*e d/d0 Z#ed*e d1d2 Z$d3d4 Z%d5d6 Z&d7d8 Z'd9d: Z(d;d< Z)d=d> Z* fd?d@Z+dAdB Z,ddCdDdEZ-dFdG Z.dTdIdJZ/dUdKdLZ0dMdN Z1dOdP Z2dQdR Z3  Z4S )VNumbera  Represents atomic numbers in SymPy.

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

    Floating point numbers are represented by the Float class.
    Rational numbers (of any size) are represented by the Rational class.
    Integer numbers (of any size) are represented by the Integer class.
    Float and Rational are subclasses of Number; Integer is a subclass
    of Rational.

    For example, ``2/3`` is represented as ``Rational(2, 3)`` which is
    a different object from the floating point number obtained with
    Python division ``2/3``. Even for numbers that are exactly
    represented in binary, there is a difference between how two forms,
    such as ``Rational(1, 2)`` and ``Float(0.5)``, are used in SymPy.
    The rational form is to be preferred in symbolic computations.

    Other kinds of numbers, such as algebraic numbers ``sqrt(2)`` or
    complex numbers ``3 + 4*I``, are not instances of Number class as
    they are not atomic.

    See Also
    ========

    Float, Integer, Rational
    Tr6   rb   c                 G  s   t |dkr|d }t|tr"|S t|tr4t|S t|trRt |dkrRt| S t|ttj	t
jfrnt|S t|tr| }|dkrtjS |dkrtjS |dkrtjS |dkrtjS t|}t|tr|S td| d	}t|t|j d S )
Nr   r   r/   naninf+inf-infz$String "%s" does not denote a Numberz<expected str|int|long|float|Decimal|Number object but got %r)rj   r@   r   r   rk   tuplero   floatrc   rV   decimalDecimalrD   rA   lowerr   NaNInfinityNegativeInfinityr   rB   rh   type__name__)clsobj_objvalmsgr6   r6   r7   __new__S  s4    



zNumber.__new__c                 C  s
   t | jS r1   )ru   is_extended_negativeselfr6   r6   r7   could_extract_minus_signq  s    zNumber.could_extract_minus_signc                 O  s4   ddl m} t|ddr"t| |S || |f||S )Nr   )invertr2   T)sympy.polys.polytoolsr   rI   r   )r   otherZgensr~   r   r6   r6   r7   r   t  s    
zNumber.invertc                 C  s   ddl m} z.t|}| js*tj| |fkr8tjtjfW S W n tk
rR   t Y S X |s`td| j	r~|j	r~t
t| j|j S t|tr| t| }n| | }|jr|dkrt|n
t|d }| ||  }n(| r|| ||krdnd}|r|n| }t
||S )Nr   )rX   zmodulo by zeror   rb   )$sympy.functions.elementary.complexesrX   r   is_infiniter   r   rh   NotImplementedZeroDivisionError
is_Integerr   divmodrd   r@   rD   ro   rg   rJ   )r   r   rX   ratwr   r6   r6   r7   
__divmod__z  s(    

zNumber.__divmod__c                 C  s0   zt |}W n tk
r$   t Y S X t|| S r1   )r   rh   r   r   r   r   r6   r6   r7   __rdivmod__  s
    
zNumber.__rdivmod__c                 C  s   t d| jj dS )z7Evaluation of mpf tuple accurate to at least prec bits.z%s needs ._as_mpf_val() methodNNotImplementedError	__class__r   r   rW   r6   r6   r7   _as_mpf_val  s    zNumber._as_mpf_valc                 C  s   t | ||S r1   rD   _newr   r   r6   r6   r7   _eval_evalf  s    zNumber._eval_evalfc                 C  s   t || j}| ||fS r1   )maxr:   r   r   r6   r6   r7   
_as_mpf_op  s    zNumber._as_mpf_opc                 C  s   t | dS )N5   )mlibZto_floatr   r   r6   r6   r7   	__float__  s    zNumber.__float__c                 C  s   t d| jj d S )Nz%s needs .floor() methodr   r   r6   r6   r7   floor  s    zNumber.floorc                 C  s   t d| jj d S )Nz%s needs .ceiling() methodr   r   r6   r6   r7   ceiling  s    zNumber.ceilingc                 C  s   |   S r1   r   r   r6   r6   r7   	__floor__  s    zNumber.__floor__c                 C  s   |   S r1   r   r   r6   r6   r7   __ceil__  s    zNumber.__ceil__c                 C  s   | S r1   r6   r   r6   r6   r7   _eval_conjugate  s    zNumber._eval_conjugatec                 G  s   ddl m} |tjf| S )Nr   )Order)Zsympy.series.orderr   r   One)r   symbolsr   r6   r6   r7   _eval_order  s    zNumber._eval_orderc                 C  s   ||  kr| S | S r1   r6   r   oldnewr6   r6   r7   
_eval_subs  s    
zNumber._eval_subsc                 C  s   dS )N)r   r   r   r6   r   r6   r6   r7   	class_key  s    zNumber.class_keyNc                 C  s   |   dd| fS )N)r   r6   r6   )r   )r   orderr6   r6   r7   sort_key  s    zNumber.sort_keyr   c                 C  sL   t |tr@tjr@|tjkr tjS |tjkr0tjS |tjkr@tjS t	| |S r1   )
r@   r   r.   evaluater   r   r   r   r   __add__r   r6   r6   r7   r     s    


zNumber.__add__c                 C  sL   t |tr@tjr@|tjkr tjS |tjkr0tjS |tjkr@tjS t	| |S r1   )
r@   r   r.   r   r   r   r   r   r   __sub__r   r6   r6   r7   r     s    


zNumber.__sub__c                 C  s   t |trttjrt|tjkr tjS |tjkrJ| jr6tjS | jrBtjS tj	S q|tj	kr| jr`tjS | jrltj	S tjS nt |t
rtS t| |S r1   )r@   r   r.   r   r   r   r   is_zerois_positiver   r   r   r   __mul__r   r6   r6   r7   r     s$    



zNumber.__mul__c                 C  sB   t |tr6tjr6|tjkr tjS |tjtjfkr6tjS t	
| |S r1   )r@   r   r.   r   r   r   r   r   Zeror   __truediv__r   r6   r6   r7   r     s    
zNumber.__truediv__c                 C  s   t d| jj d S )Nz%s needs .__eq__() methodr   r   r6   r6   r7   __eq__  s    zNumber.__eq__c                 C  s   t d| jj d S )Nz%s needs .__ne__() methodr   r   r6   r6   r7   __ne__  s    zNumber.__ne__c                 C  sF   zt |}W n$ tk
r0   td| |f Y nX td| jj d S )NzInvalid comparison %s < %sz%s needs .__lt__() methodr
   r   rh   r   r   r   r   r6   r6   r7   __lt__
  s    zNumber.__lt__c                 C  sF   zt |}W n$ tk
r0   td| |f Y nX td| jj d S )NzInvalid comparison %s <= %sz%s needs .__le__() methodr   r   r6   r6   r7   __le__  s    zNumber.__le__c                 C  s@   zt |}W n$ tk
r0   td| |f Y nX t || S )NzInvalid comparison %s > %s)r
   r   rh   r   r   r6   r6   r7   __gt__  s
    zNumber.__gt__c                 C  s@   zt |}W n$ tk
r0   td| |f Y nX t || S )NzInvalid comparison %s >= %s)r
   r   rh   r   r   r6   r6   r7   __ge__!  s
    zNumber.__ge__c                   s
   t   S r1   super__hash__r   r   r6   r7   r   (  s    zNumber.__hash__c                 O  s   dS )NTr6   )r   Zwrtflagsr6   r6   r7   is_constant+  s    zNumber.is_constant)rationalc                O  s2   | j s
|s| dfS | jr&tj|  ffS tj| ffS Nr6   )is_Rationalis_negativer   NegativeOner   )r   r   depskwargsr6   r6   r7   as_coeff_mul.  s
    
zNumber.as_coeff_mulc                 G  s   | j r| dfS tj| ffS r   )r   r   r   )r   r   r6   r6   r7   as_coeff_add6  s    zNumber.as_coeff_addFc                 C  s,   |r| j stj| fS | r"| tjfS tj| fS z1Efficiently extract the coefficient of a product.)r   r   r   r   r   r6   r6   r7   as_coeff_Mul<  s    

zNumber.as_coeff_Mulc                 C  s   |s| t jfS t j| fS z3Efficiently extract the coefficient of a summation.r   r   r   r6   r6   r7   as_coeff_AddB  s    
zNumber.as_coeff_Addc                 C  s   ddl m} || |S )z#Compute GCD of `self` and `other`. r   )r|   )r   r|   )r   r   r|   r6   r6   r7   r|   H  s    z
Number.gcdc                 C  s   ddl m} || |S )z#Compute LCM of `self` and `other`. r   )lcm)r   r  )r   r   r  r6   r6   r7   r  M  s    z
Number.lcmc                 C  s   ddl m} || |S )z1Compute GCD and cofactors of `self` and `other`. r   )	cofactors)r   r  )r   r   r  r6   r6   r7   r  R  s    zNumber.cofactors)N)F)F)5r   
__module____qualname____doc__is_commutativer2   	is_Number	__slots__r:   r   kindr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   classmethodr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r|   r  r  __classcell__r6   r6   r   r7   r   ,  sd   








r   c                      s  e Zd ZU dZdZded< dZdZdZdZ	dZ
dZdYddZedZd	d
Zdd Zdd Zdd Zdd Zdd Zd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/d0 Z"e#d1e$d2d3 Z%e#d1e$d4d5 Z&e#d1e$d6d7 Z'e#d1e$d8d9 Z(e#d1e$d:d; Z)e#d1e$d<d= Z*d>d? Z+d@dA Z,dBdC Z-dDdE Z.dFdG Z/dHdI Z0dJdK Z1dLdM Z2dNdO Z3dPdQ Z4 fdRdSZ5d[dUdVZ6dWdX Z7  Z8S )\rD   a]  Represent a floating-point number of arbitrary precision.

    Examples
    ========

    >>> from sympy import Float
    >>> Float(3.5)
    3.50000000000000
    >>> Float(3)
    3.00000000000000

    Creating Floats from strings (and Python ``int`` and ``long``
    types) will give a minimum precision of 15 digits, but the
    precision will automatically increase to capture all digits
    entered.

    >>> Float(1)
    1.00000000000000
    >>> Float(10**20)
    100000000000000000000.
    >>> Float('1e20')
    100000000000000000000.

    However, *floating-point* numbers (Python ``float`` types) retain
    only 15 digits of precision:

    >>> Float(1e20)
    1.00000000000000e+20
    >>> Float(1.23456789123456789)
    1.23456789123457

    It may be preferable to enter high-precision decimal numbers
    as strings:

    >>> Float('1.23456789123456789')
    1.23456789123456789

    The desired number of digits can also be specified:

    >>> Float('1e-3', 3)
    0.00100
    >>> Float(100, 4)
    100.0

    Float can automatically count significant figures if a null string
    is sent for the precision; spaces or underscores are also allowed. (Auto-
    counting is only allowed for strings, ints and longs).

    >>> Float('123 456 789.123_456', '')
    123456789.123456
    >>> Float('12e-3', '')
    0.012
    >>> Float(3, '')
    3.

    If a number is written in scientific notation, only the digits before the
    exponent are considered significant if a decimal appears, otherwise the
    "e" signifies only how to move the decimal:

    >>> Float('60.e2', '')  # 2 digits significant
    6.0e+3
    >>> Float('60e2', '')  # 4 digits significant
    6000.
    >>> Float('600e-2', '')  # 3 digits significant
    6.00

    Notes
    =====

    Floats are inexact by their nature unless their value is a binary-exact
    value.

    >>> approx, exact = Float(.1, 1), Float(.125, 1)

    For calculation purposes, evalf needs to be able to change the precision
    but this will not increase the accuracy of the inexact value. The
    following is the most accurate 5-digit approximation of a value of 0.1
    that had only 1 digit of precision:

    >>> approx.evalf(5)
    0.099609

    By contrast, 0.125 is exact in binary (as it is in base 10) and so it
    can be passed to Float or evalf to obtain an arbitrary precision with
    matching accuracy:

    >>> Float(exact, 5)
    0.12500
    >>> exact.evalf(20)
    0.12500000000000000000

    Trying to make a high-precision Float from a float is not disallowed,
    but one must keep in mind that the *underlying float* (not the apparent
    decimal value) is being obtained with high precision. For example, 0.3
    does not have a finite binary representation. The closest rational is
    the fraction 5404319552844595/2**54. So if you try to obtain a Float of
    0.3 to 20 digits of precision you will not see the same thing as 0.3
    followed by 19 zeros:

    >>> Float(0.3, 20)
    0.29999999999999998890

    If you want a 20-digit value of the decimal 0.3 (not the floating point
    approximation of 0.3) you should send the 0.3 as a string. The underlying
    representation is still binary but a higher precision than Python's float
    is used:

    >>> Float('0.3', 20)
    0.30000000000000000000

    Although you can increase the precision of an existing Float using Float
    it will not increase the accuracy -- the underlying value is not changed:

    >>> def show(f): # binary rep of Float
    ...     from sympy import Mul, Pow
    ...     s, m, e, b = f._mpf_
    ...     v = Mul(int(m), Pow(2, int(e), evaluate=False), evaluate=False)
    ...     print('%s at prec=%s' % (v, f._prec))
    ...
    >>> t = Float('0.3', 3)
    >>> show(t)
    4915/2**14 at prec=13
    >>> show(Float(t, 20)) # higher prec, not higher accuracy
    4915/2**14 at prec=70
    >>> show(Float(t, 2)) # lower prec
    307/2**10 at prec=10

    The same thing happens when evalf is used on a Float:

    >>> show(t.evalf(20))
    4915/2**14 at prec=70
    >>> show(t.evalf(2))
    307/2**10 at prec=10

    Finally, Floats can be instantiated with an mpf tuple (n, c, p) to
    produce the number (-1)**n*c*2**p:

    >>> n, c, p = 1, 5, 0
    >>> (-1)**n*c*2**p
    -5
    >>> Float((1, 5, 0))
    -5.00000000000000

    An actual mpf tuple also contains the number of bits in c as the last
    element of the tuple:

    >>> _._mpf_
    (1, 5, 0, 3)

    This is not needed for instantiation and is not the same thing as the
    precision. The mpf tuple and the precision are two separate quantities
    that Float tracks.

    In SymPy, a Float is a number that can be computed with arbitrary
    precision. Although floating point 'inf' and 'nan' are not such
    numbers, Float can create these numbers:

    >>> Float('-inf')
    -oo
    >>> _.is_Float
    False

    Zero in Float only has a single value. Values are not separate for
    positive and negative zeroes.
    ra   r:   ztuple[int, int, int, int]ra   NTc              	   C  sH  |d k	r|d k	rt dt|tr|dd }|drRt|dkrRd| }nD|drzt|dkrzd	|dd   }n|d
krtjS |dkrtj	S  nt|t
r|dkrd}nt|t
r|t
dkrtjS t|t
r|t
dkrtj	S t|t
rt|rtjS t|ttfr"t|}nt|tjkr2|S |tj	krB|S |tjkrR|S t|rft|}n0t|tjr|d kr|d kr|jj}|j}|d krJ|d krJd}t|tr|S t|trt|rzt|}W n tjk
r   Y nNX d|k}t|\}}|jr6|r6t|tt|d}td|}t |}n|dkr^|d ksr|d kr|dkrt|tst dd }t|rzt|}W n tjk
r   Y nHX d|k}t|\}}|jr|rt|tt|d}t |}d}|d krt d| |d ks,|dkr4t |}t!|}t|t
rZt"#||t$}nt|trxt"%||t$}nt|tjr|& rt"%t||t$}n@|' rtjS |( r|dkrtjS tj	S t dt| nPt|t)rt|dkrt|d tr~t*|}|d +dr>|d d d |d< |d drb|d dd  |d< t,|d d|d< t)|}n~t|dkrt-||S t.|d dk|d dkt.dd |D fst d|f t-|d |d |d t/|d f|S n:z|0|}W n* t1t2fk
r6   tj||dj}Y nX | j-||dd S )!Nz=Both decimal and binary precision supplied. Supply only one.  r0   .r   0z-.r/   z-0.)r   r   r   r   r      -z^The null string can only be used when the number to Float is passed as a string or an integer.Tzstring-float not recognized: %szunexpected decimal value %s)      Lrb   0x   r  )r   r   c                 s  s   | ]}t |ttfkV  qd S r1   )r   rJ   r3   r6   r6   r7   r8     s     z Float.__new__.<locals>.<genexpr>zmalformed mpf: %s)rW   F)zero)3rB   r@   rA   replacer   
startswithrj   r   r   r   r   r}   isnanr   r   rk   r   r	   rc   rV   contextrW   ra   rD   rz   r   r   InvalidOperationrt   r   r   lstripr*   rJ   r   
from_floatrU   Zfrom_strrg   is_nanr   r   listendswithr   r   rC   r   r   r   AttributeError)r   numZdps	precisionNumZisintokra   r6   r6   r7   r     s    









(








zFloat.__new__c                 C  s\   |r|t krtjS |tkr tjS |tkr.tjS |tkr<tjS t	
| }t|||_||_|S r1   )r'   r   r   _mpf_nanr   _mpf_infr   	_mpf_ninfr   r   r   r]   ra   r:   )r   ra   r:   r  r   r6   r6   r7   r     s    
z
Float._newc                 C  s   t | jfd| jifS )Nr%  )r   Zto_pickablera   r:   r   r6   r6   r7   __getnewargs_ex__  s    zFloat.__getnewargs_ex__c                 C  s   | j | jfS r1   r  r   r6   r6   r7   _hashable_content  s    zFloat._hashable_contentc                 C  s   t ttt| j| jS r1   )rk   rJ   r   to_intZ	mpf_floorra   r:   r   r6   r6   r7   r     s    zFloat.floorc                 C  s   t ttt| j| jS r1   )rk   rJ   r   r-  Zmpf_ceilra   r:   r   r6   r6   r7   r     s    zFloat.ceilingc                 C  s   |   S r1   r   r   r6   r6   r7   r     s    zFloat.__floor__c                 C  s   |   S r1   r   r   r6   r6   r7   r     s    zFloat.__ceil__c                 C  s   t | jS r1   )rc   rV   ra   r   r6   r6   r7   r$    s    z	Float.numc                 C  s0   t | j|}|| jkr,| j|kr,t| j| |S r1   )r]   ra   r:   r,   r   rW   r\   r6   r6   r7   r     s    zFloat._as_mpf_valc                 C  s   | j t|| jfS r1   )ra   r   r:   r   r6   r6   r7   r     s    zFloat._as_mpf_opc                 C  s   | j ttfkrdS dS )NFTra   r)  r*  r   r6   r6   r7   _eval_is_finite  s    zFloat._eval_is_finitec                 C  s   | j ttfkrdS dS NTFr/  r   r6   r6   r7   _eval_is_infinite  s    zFloat._eval_is_infinitec                 C  s
   | j tkS r1   ra   r'   r   r6   r6   r7   _eval_is_integer  s    zFloat._eval_is_integerc                 C  s   | j ttfkrdS | jdk S NFr   ra   r*  r)  r$  r   r6   r6   r7   _eval_is_negative  s    zFloat._eval_is_negativec                 C  s   | j ttfkrdS | jdkS r5  r6  r   r6   r6   r7   _eval_is_positive  s    zFloat._eval_is_positivec                 C  s&   | j tkrdS | j tkrdS | jdk S NTFr   r6  r   r6   r6   r7   _eval_is_extended_negative  s
    

z Float._eval_is_extended_negativec                 C  s&   | j tkrdS | j tkrdS | jdkS r9  )ra   r)  r*  r$  r   r6   r6   r7   _eval_is_extended_positive  s
    

z Float._eval_is_extended_positivec                 C  s
   | j tkS r1   r3  r   r6   r6   r7   _eval_is_zero  s    zFloat._eval_is_zeroc                 C  s
   | j tkS r1   r3  r   r6   r6   r7   __bool__  s    zFloat.__bool__c                 C  s   | s| S t t| j| jS r1   )rD   r   r   Zmpf_negra   r:   r   r6   r6   r7   __neg__  s    zFloat.__neg__r   c                 C  sF   t |tr:tjr:|| j\}}tt	| j
||t|S t| |S r1   )r@   r   r.   r   r   r:   rD   r   r   Zmpf_addra   rU   r   r   r   rhsrW   r6   r6   r7   r      s    zFloat.__add__c                 C  sF   t |tr:tjr:|| j\}}tt	| j
||t|S t| |S r1   )r@   r   r.   r   r   r:   rD   r   r   Zmpf_subra   rU   r   r?  r6   r6   r7   r     s    zFloat.__sub__c                 C  sF   t |tr:tjr:|| j\}}tt	| j
||t|S t| |S r1   )r@   r   r.   r   r   r:   rD   r   r   mpf_mulra   rU   r   r?  r6   r6   r7   r     s    zFloat.__mul__c                 C  sN   t |trB|dkrBtjrB|| j\}}tt	| j
||t|S t| |S Nr   )r@   r   r.   r   r   r:   rD   r   r   Zmpf_divra   rU   r   r?  r6   r6   r7   r     s    zFloat.__truediv__c                 C  s   t |tr4|jdkr4tjr4ttt| || jdS t |trntjrn| | }|t|krntdt	| j|jdS t |t
rtjr|| j\}}tt| j||t|S t
| |S )Nr   r%  r   )r@   ro   re   r.   r   rD   __mod__r:   rJ   r   r   r   r   r   mpf_modra   rU   )r   r   r   r@  rW   r6   r6   r7   rD    s    zFloat.__mod__c                 C  s`   t |trtjr|| S t |trTtjrT|| j\}}tt	
|| j|t|S t| |S r1   )r@   rD   r.   r   rD  r   r   r:   r   r   rE  ra   rU   __rmod__r?  r6   r6   r7   rF  +  s    
zFloat.__rmod__c              	   C  s"  t | dr |jr| S |jr tjS t|trt|trX| j}t	
t| j|j|t|S t|tr|jdkr|jd r| jrttj|dd|  | S || j\}}| j}zt|||t}t	
||W S  tjk
r   t|tf|tf|t\}}t	
||t	
||tj   Y S X dS )z
        expt is symbolic object but not equal to 0, 1

        (-p)**r -> exp(r*log(-p)) -> exp(r*(log(p) + I*Pi)) ->
                  -> p**r*(sin(Pi*r) + cos(Pi*r)*I)
        r   r   r/   Fr   N)equal_valuedis_extended_positiver   r   ComplexInfinityr@   r   rk   r:   rD   r   r   Zmpf_pow_intra   rd   rU   ro   re   r   Powr   _eval_powerr   r   ZComplexResultZmpc_powr'   ImaginaryUnit)r   rZ   rW   Zmpfselfr   reZimr6   r6   r7   rL  4  sN    

 
   
zFloat._eval_powerc                 C  s   t t| j| jS r1   )rD   r   r   Zmpf_absra   r:   r   r6   r6   r7   __abs__T  s    zFloat.__abs__c                 C  s   | j tkrdS tt| j S rB  )ra   r'   rJ   r   r-  r   r6   r6   r7   __int__W  s    
zFloat.__int__c                 C  s   ddl m} zt|}W n tk
r0   t Y S X t||r@dS |jrZ|jrPdS || S |j	rl| j
|j
kS |jr||| S |jr|| j}tt| j
|S | s| S dS )Nr   )BooleanF)Zsympy.logic.boolalgrQ  r
   r   r   r@   is_NumberSymbolis_irrationalr   is_Floatra   r   r  r   r:   ru   r   Zmpf_eq)r   r   rQ  ompfr6   r6   r7   r   \  s*    



zFloat.__eq__c                 C  s
   | |k S r1   r6   r   r6   r6   r7   r   z  s    zFloat.__ne__c                 C  s   zt |}W n tk
r$   t Y S X |jr`t| jt|j}t|j	}t t
|||S |jr|t t
|| j|jS |jr|tjtjfkr|t| j}|jdkr|jrt t
|| j|| jS d S Nr   )r
   r   r   r   r   rA  ra   from_intre   rd   ru   rT  is_comparabler   r   r   evalfr)   r:   r  r   )r   r   opZsmpfrU  r6   r6   r7   _Frel}  s,    

 
zFloat._Frelc                 C  s:   t |tr|| S | |tj}|d kr6t| |S |S r1   )r@   NumberSymbolr   r[  r   Zmpf_gtr   r   r   r   r\   r6   r6   r7   r     s    

zFloat.__gt__c                 C  s:   t |tr|| S | |tj}|d kr6t| |S |S r1   )r@   r\  r   r[  r   Zmpf_ger   r   r]  r6   r6   r7   r     s    

zFloat.__ge__c                 C  s:   t |tr|| S | |tj}|d kr6t| |S |S r1   )r@   r\  r   r[  r   Zmpf_ltr   r   r]  r6   r6   r7   r     s    

zFloat.__lt__c                 C  s:   t |tr|| S | |tj}|d kr6t| |S |S r1   )r@   r\  r   r[  r   Zmpf_ler   r   r]  r6   r6   r7   r     s    

zFloat.__le__c                   s
   t   S r1   r   r   r   r6   r7   r     s    zFloat.__hash__1e-15c                 C  s   t | | t|k S r1   )rK   rD   )r   r   epsilonr6   r6   r7   
epsilon_eq  s    zFloat.epsilon_eqc                 C  s   t tt| |S r1   )formatr   r   rA   )r   format_specr6   r6   r7   
__format__  s    zFloat.__format__)NN)T)r^  )9r   r  r  r  r	  __annotations__is_rationalrS  r2   is_realis_extended_realrT  r   r  r   r+  r,  r   r   r   r   propertyr$  r   r   r0  r2  r4  r7  r8  r:  r;  r<  r=  r>  r   r   r   r   r   r   rD  rF  rL  rO  rP  r   r   r[  r   r   r   r   r   r`  rc  r  r6   r6   r   r7   rD   X  sv   
 &
 






 
rD   c                      s  e Zd ZU dZdZdZdZdZdZde	d< de	d< dZ
ed]d	d
Zd^ddZdd Zdd Zdd Zdd Zdd Zededd ZeZededd Zededd Zededd  ZeZeded!d" Zeded#d$ Zeded%d& Zeded'd( Zd)d* Zd+d, Z d-d. Z!d/d0 Z"d1d2 Z#d3d4 Z$d5d6 Z%d7d8 Z&d9d: Z'd;d< Z(d=d> Z)d?d@ Z*dAdB Z+dCdD Z,dEdF Z-dGdH Z. fdIdJZ/d_dKdLZ0e1dMdN Z2e1dOdP Z3ededQdR Z4ededSdT Z5dUdV Z6d`dWdXZ7dadYdZZ8dbd[d\Z9  Z:S )cro   a  Represents rational numbers (p/q) of any size.

    Examples
    ========

    >>> from sympy import Rational, nsimplify, S, pi
    >>> Rational(1, 2)
    1/2

    Rational is unprejudiced in accepting input. If a float is passed, the
    underlying value of the binary representation will be returned:

    >>> Rational(.5)
    1/2
    >>> Rational(.2)
    3602879701896397/18014398509481984

    If the simpler representation of the float is desired then consider
    limiting the denominator to the desired value or convert the float to
    a string (which is roughly equivalent to limiting the denominator to
    10**12):

    >>> Rational(str(.2))
    1/5
    >>> Rational(.2).limit_denominator(10**12)
    1/5

    An arbitrarily precise Rational is obtained when a string literal is
    passed:

    >>> Rational("1.23")
    123/100
    >>> Rational('1e-2')
    1/100
    >>> Rational(".1")
    1/10
    >>> Rational('1e-2/3.2')
    1/320

    The conversion of other types of strings can be handled by
    the sympify() function, and conversion of floats to expressions
    or simple fractions can be handled with nsimplify:

    >>> S('.[3]')  # repeating digits in brackets
    1/3
    >>> S('3**2/10')  # general expressions
    9/10
    >>> nsimplify(.3)  # numbers that have a simple form
    3/10

    But if the input does not reduce to a literal Rational, an error will
    be raised:

    >>> Rational(pi)
    Traceback (most recent call last):
    ...
    TypeError: invalid input: pi


    Low-level
    ---------

    Access numerator and denominator as .p and .q:

    >>> r = Rational(3, 4)
    >>> r
    3/4
    >>> r.p
    3
    >>> r.q
    4

    Note that p and q return integers (not SymPy Integers) so some care
    is needed when using them in expressions:

    >>> r.p/r.q
    0.75

    If an unevaluated Rational is desired, ``gcd=1`` can be passed and
    this will keep common divisors of the numerator and denominator
    from being eliminated. It is not possible, however, to leave a
    negative value in the denominator.

    >>> Rational(2, 4, gcd=1)
    2/4
    >>> Rational(2, -4, gcd=1).q
    4

    See Also
    ========
    sympy.core.sympify.sympify, sympy.simplify.simplify.nsimplify
    TFrd   re   rJ   rd   re   Nc           	   	   C  sP  |d kr&t |tr|S t |tr$nt |ttfr>tt| S t |tspzt|}W n tt	fk
rl   Y nX n|
ddkrtd| |dd}|dd}t|dkr|\}}t|}t|}|| }zt|}W n tk
r   Y nX t|j|jdS t |tstd| d}d}d}t |tsPt|}||j9 }|j}nt|}t |tst|}||j9 }||j9 }n|t|9 }|}|dkr|dkrtd rtd	ntjS tjS |dk r| }| }|stt||}|dkr
|| }|| }|dkrt|S |dkr6|dkr6tjS t | }||_||_|S )
N/r   zinvalid input: %sr  r0   r/   r   r^   zIndeterminate 0/0)!r@   ro   r   r   rD   rf   rA   r   r   SyntaxErrorcountrh   r  rsplitrj   	fractionsFractionrB   	numeratordenominatorre   rd   rJ   r_   r   r   rJ  r   rK   rk   Halfr   r   )	r   rd   re   r|   ZpqfpZfqQr   r6   r6   r7   r   4  s    















zRational.__new__@B c                 C  s(   t | j| j}t|t t|S )a4  Closest Rational to self with denominator at most max_denominator.

        Examples
        ========

        >>> from sympy import Rational
        >>> Rational('3.141592653589793').limit_denominator(10)
        22/7
        >>> Rational('3.141592653589793').limit_denominator(100)
        311/99

        )rn  ro  rd   re   ro   limit_denominatorrJ   )r   max_denominatorry   r6   r6   r7   rv    s    zRational.limit_denominatorc                 C  s   | j | jfS r1   ri  r   r6   r6   r7   __getnewargs__  s    zRational.__getnewargs__c                 C  s   | j | jfS r1   ri  r   r6   r6   r7   r,    s    zRational._hashable_contentc                 C  s
   | j dkS rB  rd   r   r6   r6   r7   r8    s    zRational._eval_is_positivec                 C  s
   | j dkS rB  ry  r   r6   r6   r7   r<    s    zRational._eval_is_zeroc                 C  s   t | j | jS r1   )ro   rd   re   r   r6   r6   r7   r>    s    zRational.__neg__r   c                 C  s   t jrzt|tr,t| j| j|j  | jdS t|tr\t| j|j | j|j  | j|j S t|trn||  S t	| |S t	| |S rV  )
r.   r   r@   rk   ro   rd   re   rD   r   r   r   r6   r6   r7   r     s    

&
zRational.__add__c                 C  s   t jr|t|tr,t| j| j|j  | jdS t|tr\t| j|j | j|j  | j|j S t|trp| |  S t	| |S t	| |S rV  )
r.   r   r@   rk   ro   rd   re   rD   r   r   r   r6   r6   r7   r     s    

&

zRational.__sub__c                 C  s   t jr|t|tr,t| j|j | j | jdS t|tr\t| j|j | j|j  | j|j S t|trp|  | S t	| |S t	| |S rV  )
r.   r   r@   rk   ro   re   rd   rD   r   __rsub__r   r6   r6   r7   rz    s    

&

zRational.__rsub__c                 C  s   t jrt|tr0t| j|j | jt|j| jS t|trnt| j|j | j|j t| j|jt| j|j S t|tr||  S t	
| |S t	
| |S r1   )r.   r   r@   rk   ro   rd   re   r   rD   r   r   r   r6   r6   r7   r     s    
 
4
zRational.__mul__c                 C  s   t jrt|trJ| jr(|jtjkr(tjS t| j| j	|j t
| j|jS n`t|trt| j|j	 | j	|j t
| j|jt
| j	|j	 S t|tr| d|  S t| |S t| |S rV  )r.   r   r@   rk   rd   r   r   rJ  ro   re   r   rD   r   r   r   r6   r6   r7   r     s    
"
4
zRational.__truediv__c                 C  s   t jrt|tr0t|j| j | jt| j|jS t|trnt|j| j |j| j t| j|jt| j|j S t|tr|d|   S t	
| |S t	
| |S rV  )r.   r   r@   rk   ro   rd   re   r   rD   r   __rtruediv__r   r6   r6   r7   r{    s    
 
4
zRational.__rtruediv__c                 C  s   t jrt|trR| j|j |j| j  }t| j|j ||j | j  | j|j S t|trtt| t||jdS t	| |S t	| |S )NrC  )
r.   r   r@   ro   rd   re   rD   rD  r:   r   )r   r   rF   r6   r6   r7   rD    s    
*
zRational.__mod__c                 C  s"   t |trt|| S t| |S r1   )r@   ro   rD  r   rF  r   r6   r6   r7   rF    s    
zRational.__rmod__c                 C  s  t |trt |tr&| |j| S |jr| }|tjkrJt| j	| j
S | jrntj| t| j	| j
 |  S t| j	| j
| S |tjkr| j
| j	krtjS | j
| j	 k rtjtjtj  S tjS t |trt| j
|j
 | j	|j
 dS t |tr|j
|j	 }|r|d7 }||j	 |j
 }t||j	}| j
dkrdt| j
| t| j	|  td| j	| d S t| j	| td| j	| d S |j	|j
 }t||j	}| j
dkrt| j
| t| j	|  td| j	d S t| j	| td| j	d S | jr|jr|  | S d S rV  )r@   r   rD   r   r:   r   r   r   ro   re   rd   r   r   r   rM  r   rk   is_even)r   rZ   neZintpartZremfracpartZratfracpartr6   r6   r7   rL    sF    



. *
zRational._eval_powerc                 C  s   t | j| j|tS r1   )r   from_rationalrd   re   rU   r   r6   r6   r7   r   5  s    zRational._as_mpf_valc                 C  s   t t| j| j||S r1   )rc   make_mpfr   r~  rd   re   r   rW   rU   r6   r6   r7   _mpmath_8  s    zRational._mpmath_c                 C  s   t t| j| jS r1   )ro   rK   rd   re   r   r6   r6   r7   rO  ;  s    zRational.__abs__c                 C  s2   | j | j }}|dk r&t| |  S t|| S rB  )rd   re   rJ   )r   rd   re   r6   r6   r7   rP  >  s    zRational.__int__c                 C  s   t | j| j S r1   rk   rd   re   r   r6   r6   r7   r   D  s    zRational.floorc                 C  s   t | j | j  S r1   r  r   r6   r6   r7   r   G  s    zRational.ceilingc                 C  s   |   S r1   r   r   r6   r6   r7   r   J  s    zRational.__floor__c                 C  s   |   S r1   r   r   r6   r6   r7   r   M  s    zRational.__ceil__c                 C  sN  zt |}W n tk
r$   t Y S X t|ts4dS | s>| S |jrX|jrNdS || S |jrv| j	|j	kot| j
|j
kS |jrJ| j
| j
d @ rdS |jd d \}}}|r| }|s| jr| jrdS || j	kS ddlm} |dkr| jsdS | j	o| j	|  o|| j	| d|dfkS | jr(dS || j	koH|| j
d| dfkS dS )NFr   r  )integer_logr   r/   T)r
   r   r   r@   r   rR  rS  r   r   rd   re   rT  ra   r   r|  powerr  )r   r   rq   r   tr  r6   r6   r7   r   P  sF    




"zRational.__eq__c                 C  s
   | |k S r1   r6   r   r6   r6   r7   r     s    zRational.__ne__c                 C  s   zt |}W n tk
r$   t Y S X |jrd }| | }}|jrLt||}nD|jr^t||}n2|jrt|j	|j
 t|j
|j	  }}t||}|r||S |jr|jrt|j	|j
| fS d S r1   )r
   r   r   r  rR  rI   rT  r   rk   rd   re   r2   rg  )r   r   attrrZ  rq   or6   r6   r7   _Rrel  s$    

"
zRational._Rrelc                 C  s6   |  |d}|d kr| |f}nt|ts,|S tj| S )Nr   )r  r@   r   r   r   r]  r6   r6   r7   r     s    

zRational.__gt__c                 C  s6   |  |d}|d kr| |f}nt|ts,|S tj| S )Nr   )r  r@   r   r   r   r]  r6   r6   r7   r     s    

zRational.__ge__c                 C  s6   |  |d}|d kr| |f}nt|ts,|S tj| S )Nr   )r  r@   r   r   r   r]  r6   r6   r7   r     s    

zRational.__lt__c                 C  s6   |  |d}|d kr| |f}nt|ts,|S tj| S )Nr   )r  r@   r   r   r   r]  r6   r6   r7   r     s    

zRational.__le__c                   s
   t   S r1   r   r   r   r6   r7   r     s    zRational.__hash__c                 C  s$   ddl m} || |||||d S )zA wrapper to factorint which return factors of self that are
        smaller than limit (or cheap to compute). Special methods of
        factoring are disabled by default so that only trial division is used.
        r   )	factorrat)limit	use_trialuse_rhouse_pm1verbose)sympy.ntheory.factor_r  copy)r   r  r  r  r  r  visualr  r6   r6   r7   factors  s     zRational.factorsc                 C  s   | j S r1   ry  r   r6   r6   r7   rp    s    zRational.numeratorc                 C  s   | j S r1   )re   r   r6   r6   r7   rq    s    zRational.denominatorc                 C  sB   t |tr6|tjkr|S tt| j|jt| j|jS t	| |S r1   )
r@   ro   r   r   r   rd   r   re   r   r|   r   r6   r6   r7   r|     s    

zRational.gcdc                 C  s@   t |tr4t| jt| j|j |j t| j|jS t| |S r1   )r@   ro   rd   r   re   r   r  r   r6   r6   r7   r    s    
zRational.lcmc                 C  s   t | jt | jfS r1   r  r   r6   r6   r7   as_numer_denom  s    zRational.as_numer_denomc                 C  s*   | r | j r| tjfS |  tjfS tj| fS )a-  Return the tuple (R, self/R) where R is the positive Rational
        extracted from self.

        Examples
        ========

        >>> from sympy import S
        >>> (S(-3)/2).as_content_primitive()
        (3/2, -1)

        See docstring of Expr.as_content_primitive for more examples.
        )r   r   r   r   )r   radicalclearr6   r6   r7   as_content_primitive  s
    
zRational.as_content_primitivec                 C  s
   | t jfS r   r   r   r   r6   r6   r7   r     s    zRational.as_coeff_Mulc                 C  s
   | t jfS r   r   r   r6   r6   r7   r    s    zRational.as_coeff_Add)NN)ru  )NTFFFF)FT)F)F);r   r  r  r  rf  
is_integerre  r2   r	  rd  r   r   r   rv  rx  r,  r8  r<  r>  r   r   r   __radd__r   rz  r   __rmul__r   r{  rD  rF  rL  r   r  rO  rP  r   r   r   r   r   r   r  r   r   r   r   r   r  rh  rp  rq  r|   r  r  r  r   r  r  r6   r6   r   r7   ro     s   
\P








-/      



	


ro   c                      s  e Zd ZdZdZdZdZdZdZdd Z	d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%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Z d3d4 Z!d5d6 Z"d7d8 Z#d9d: Z$d;d< Z%d=d> Z&d?d@ Z' fdAdBZ(dCdD Z)dEdF Z*dGdH Z+e,dIe-dJdK Z.dLdM Z/dNdO Z0dPdQ Z1dRdS Z2dTdU Z3dVdW Z4dXdY Z5dZd[ Z6d\d] Z7d^d_ Z8d`da Z9dbdc Z:  Z;S )drk   aj  Represents integer numbers of any size.

    Examples
    ========

    >>> from sympy import Integer
    >>> Integer(3)
    3

    If a float or a rational is passed to Integer, the fractional part
    will be discarded; the effect is of rounding toward zero.

    >>> Integer(3.8)
    3
    >>> Integer(-3.8)
    -3

    A string is acceptable input if it can be parsed as an integer:

    >>> Integer("9" * 20)
    99999999999999999999

    It is rarely needed to explicitly instantiate an Integer, because
    Python integers are automatically converted to Integer when they
    are used in SymPy expressions.
    r   Tr6   c                 C  s   t | j|tS r1   )r   rW  rd   rU   r   r6   r6   r7   r   #  s    zInteger._as_mpf_valc                 C  s   t | |S r1   )rc   r  r   r  r6   r6   r7   r  &  s    zInteger._mpmath_c                 C  s   t |tr|dd}zt|}W n  tk
rB   td| Y nX |dkrRtjS |dkr`tjS |dkrntjS t	
| }||_|S )Nr  r0   z6Argument of Integer should be of numeric type, got %s.r   rb   r   )r@   rA   r  rJ   rh   r   r   r   r   r   r   rd   )r   r5   Zivalr   r6   r6   r7   r   )  s"    


zInteger.__new__c                 C  s   | j fS r1   ry  r   r6   r6   r7   rx  D  s    zInteger.__getnewargs__c                 C  s   | j S r1   ry  r   r6   r6   r7   rP  H  s    zInteger.__int__c                 C  s
   t | jS r1   rk   rd   r   r6   r6   r7   r   K  s    zInteger.floorc                 C  s
   t | jS r1   r  r   r6   r6   r7   r   N  s    zInteger.ceilingc                 C  s   |   S r1   r   r   r6   r6   r7   r   Q  s    zInteger.__floor__c                 C  s   |   S r1   r   r   r6   r6   r7   r   T  s    zInteger.__ceil__c                 C  s   t | j S r1   r  r   r6   r6   r7   r>  W  s    zInteger.__neg__c                 C  s   | j dkr| S t| j  S d S rB  )rd   rk   r   r6   r6   r7   rO  Z  s    
zInteger.__abs__c                 C  s2   t |tr"tjr"tt| j|j S t| |S d S r1   )	r@   rk   r.   r   r   r   rd   r   r   r   r6   r6   r7   r   `  s    zInteger.__divmod__c                 C  sz   t |tr tjr tt|| j S zt|}W n< tk
rh   d}t	|j
}t	| j
}t|||f Y nX t|| S d S )Nz7unsupported operand type(s) for divmod(): '%s' and '%s')r@   rJ   r.   r   r   r   rd   r   rh   r   r   r   )r   r   r   ZonameZsnamer6   r6   r7   r   f  s    

zInteger.__rdivmod__c                 C  sx   t jrjt|trt| j| S t|tr8t| j|j S t|tr^t| j|j |j |jdS t| |S t	| |S d S rV  )
r.   r   r@   rJ   rk   rd   ro   re   r   Addr   r6   r6   r7   r   t  s    


zInteger.__add__c                 C  s\   t jrPt|trt|| j S t|trDt|j| j|j  |jdS t| |S t| |S rV  )	r.   r   r@   rJ   rk   rd   ro   re   r  r   r6   r6   r7   r    s    

zInteger.__radd__c                 C  sv   t jrjt|trt| j| S t|tr8t| j|j S t|tr^t| j|j |j |jdS t| |S t| |S rV  )	r.   r   r@   rJ   rk   rd   ro   re   r   r   r6   r6   r7   r     s    


zInteger.__sub__c                 C  s\   t jrPt|trt|| j S t|trDt|j| j|j  |jdS t| |S t| |S rV  )	r.   r   r@   rJ   rk   rd   ro   re   rz  r   r6   r6   r7   rz    s    

zInteger.__rsub__c                 C  sz   t jrnt|trt| j| S t|tr8t| j|j S t|trbt| j|j |jt| j|jS t	| |S t	| |S r1   )
r.   r   r@   rJ   rk   rd   ro   re   r   r   r   r6   r6   r7   r     s    


 zInteger.__mul__c                 C  s`   t jrTt|trt|| j S t|trHt|j| j |jt| j|jS t	| |S t	| |S r1   )
r.   r   r@   rJ   rk   rd   ro   re   r   r  r   r6   r6   r7   r    s    

 zInteger.__rmul__c                 C  sP   t jrDt|trt| j| S t|tr8t| j|j S t| |S t| |S r1   )r.   r   r@   rJ   rk   rd   ro   rD  r   r6   r6   r7   rD    s    

zInteger.__mod__c                 C  sP   t jrDt|trt|| j S t|tr8t|j| j S t| |S t| |S r1   )r.   r   r@   rJ   rk   rd   ro   rF  r   r6   r6   r7   rF    s    

zInteger.__rmod__c                 C  s6   t |tr| j|kS t |tr*| j|jkS t| |S r1   )r@   rJ   rd   rk   ro   r   r   r6   r6   r7   r     s
    


zInteger.__eq__c                 C  s
   | |k S r1   r6   r   r6   r6   r7   r     s    zInteger.__ne__c                 C  sH   zt |}W n tk
r$   t Y S X |jr<t | j|jkS t| |S r1   )r
   r   r   r   rd   ro   r   r   r6   r6   r7   r     s    
zInteger.__gt__c                 C  sH   zt |}W n tk
r$   t Y S X |jr<t | j|jk S t| |S r1   )r
   r   r   r   rd   ro   r   r   r6   r6   r7   r     s    
zInteger.__lt__c                 C  sH   zt |}W n tk
r$   t Y S X |jr<t | j|jkS t| |S r1   )r
   r   r   r   rd   ro   r   r   r6   r6   r7   r     s    
zInteger.__ge__c                 C  sH   zt |}W n tk
r$   t Y S X |jr<t | j|jkS t| |S r1   )r
   r   r   r   rd   ro   r   r   r6   r6   r7   r     s    
zInteger.__le__c                 C  s
   t | jS r1   )hashrd   r   r6   r6   r7   r     s    zInteger.__hash__c                 C  s   | j S r1   ry  r   r6   r6   r7   	__index__  s    zInteger.__index__c                 C  s   t | jd S Nr/   )ru   rd   r   r6   r6   r7   _eval_is_odd  s    zInteger._eval_is_oddc                   s  ddl m} |tjkr:| jtjkr(tjS tjtjtj  S |tjkrVtd| dtj S t	|t
sv| jrv|jrv|  | S t	|trt |S t	|tsdS |tjkr| jrtjt|  | S |jr| }| jrtj| td|  d|  S td| jd| S tt| j|j\}}|rDt|t|j }| jr@|tj| 9 }|S tt| j}||}|dk	rv|d |d i}	nt|jdd}	d}
d}d}d}i }|	 D ]\}}||j9 }t||j\}}|dkr|
|| 9 }
|dkrt||j}|dkr|t|t|| |j| d9 }n|||< q| D ]4\}}|dkrH|}nt||}|dkr0 qfq0| D ]\}}||||  9 }qn||kr|
dkr|dkrd}n2|
| t|t||j }| jr|ttj|9 }|S )av  
        Tries to do some simplifications on self**expt

        Returns None if no further simplifications can be done.

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

        When exponent is a fraction (so we have for example a square root),
        we try to find a simpler representation by factoring the argument
        up to factors of 2**15, e.g.

          - sqrt(4) becomes 2
          - sqrt(-4) becomes 2*I
          - (2**(3+7)*3**(6+7))**Rational(1,7) becomes 6*18**(3/7)

        Further simplification would require a special call to factorint on
        the argument which is not done here for sake of speed.

        r   )perfect_powerr   NFi   )r  )r  r  r   r   rd   r   rM  r   ro   r@   r   r   r|  rD   r   rL  rr  rK  r   integer_nthrootrK   re   rk   rJ   r  itemsr   r   )r   rZ   r  r}  r   ZxexactresultZb_posrd   dictZout_intZout_radZsqr_intZsqr_gcdZsqr_dictprimeexponentZdiv_eZdiv_mr   exkvr   r6   r7   rL    sz    










"



zInteger._eval_powerc                 C  s   ddl m} || S )Nr   )isprime)Zsympy.ntheory.primetestr  )r   r  r6   r6   r7   _eval_is_primeh	  s    zInteger._eval_is_primec                 C  s   | dkrt | jS dS d S )Nr   F)r   is_primer   r6   r6   r7   _eval_is_compositem	  s    
zInteger._eval_is_compositec                 C  s
   | t jfS r1   r  r   r6   r6   r7   r  s	  s    zInteger.as_numer_denomr   c                 C  s4   t |tstS t |tr&t| j| S t| |d S rB  )r@   r   r   rk   rd   r   r   r6   r6   r7   __floordiv__v	  s
    

zInteger.__floordiv__c                 C  s   t t |j| j S r1   r  r   r6   r6   r7   __rfloordiv__~	  s    zInteger.__rfloordiv__c                 C  s,   t |tttjfr$t| jt|> S tS d S r1   r@   rJ   rk   numbersIntegralrd   r   r   r6   r6   r7   
__lshift__	  s    zInteger.__lshift__c                 C  s*   t |ttjfr"tt|| j> S tS d S r1   r@   rJ   r  r  rk   rd   r   r   r6   r6   r7   __rlshift__	  s    zInteger.__rlshift__c                 C  s,   t |tttjfr$t| jt|? S tS d S r1   r  r   r6   r6   r7   
__rshift__	  s    zInteger.__rshift__c                 C  s*   t |ttjfr"tt|| j? S tS d S r1   r  r   r6   r6   r7   __rrshift__	  s    zInteger.__rrshift__c                 C  s,   t |tttjfr$t| jt|@ S tS d S r1   r  r   r6   r6   r7   __and__	  s    zInteger.__and__c                 C  s*   t |ttjfr"tt|| j@ S tS d S r1   r  r   r6   r6   r7   __rand__	  s    zInteger.__rand__c                 C  s,   t |tttjfr$t| jt|A S tS d S r1   r  r   r6   r6   r7   __xor__	  s    zInteger.__xor__c                 C  s*   t |ttjfr"tt|| jA S tS d S r1   r  r   r6   r6   r7   __rxor__	  s    zInteger.__rxor__c                 C  s,   t |tttjfr$t| jt|B S tS d S r1   r  r   r6   r6   r7   __or__	  s    zInteger.__or__c                 C  s*   t |ttjfr"tt|| jB S tS d S r1   r  r   r6   r6   r7   __ror__	  s    zInteger.__ror__c                 C  s   t | j S r1   r  r   r6   r6   r7   
__invert__	  s    zInteger.__invert__)<r   r  r  r  re   r  r2   r   r	  r   r  r   r   rx  rP  r   r   r   r   r>  rO  r   r   r   r  r   rz  r   r  rD  rF  r   r   r   r   r   r   r   r  r  rL  r  r  r  r   r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r6   r6   r   r7   rk      sn   
									l

rk   c                      s   e Zd ZU dZdZdZdZdZeZ	e
 Zded< d'ddZ fd	d
Zd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edd Zdd  Zd*d!d"Zd#d$ Zd+d%d&Z  ZS ),AlgebraicNumbera4  
    Class for representing algebraic numbers in SymPy.

    Symbolically, an instance of this class represents an element
    $\alpha \in \mathbb{Q}(\theta) \hookrightarrow \mathbb{C}$. That is, the
    algebraic number $\alpha$ is represented as an element of a particular
    number field $\mathbb{Q}(\theta)$, with a particular embedding of this
    field into the complex numbers.

    Formally, the primitive element $\theta$ is given by two data points: (1)
    its minimal polynomial (which defines $\mathbb{Q}(\theta)$), and (2) a
    particular complex number that is a root of this polynomial (which defines
    the embedding $\mathbb{Q}(\theta) \hookrightarrow \mathbb{C}$). Finally,
    the algebraic number $\alpha$ which we represent is then given by the
    coefficients of a polynomial in $\theta$.
    )reprootaliasminpoly_own_minpolyTz
set[Basic]free_symbolsNc                 K  s  ddl m}m} ddlm} t|}d}d}	t|ttfr^|\}
}|
j	sddl
m} ||
}
n>|jr|j|j|j|jf\}
}}}	n|||ddd| }
}|
 }|dk	rt||s|t|d|}t| }n|| d|}t|  }n|d	dgd|}td	d}|dk	rJdd
lm} ||j|j|}||d|}t| }| |
 krh||
j}||f}|px|	}|dk	rd	dlm} t||s||}||f }tj| f| }||_||_||_|
|_d|_|S )a]  
        Construct a new algebraic number $\alpha$ belonging to a number field
        $k = \mathbb{Q}(\theta)$.

        There are four instance attributes to be determined:

        ===========  ============================================================================
        Attribute    Type/Meaning
        ===========  ============================================================================
        ``root``     :py:class:`~.Expr` for $\theta$ as a complex number
        ``minpoly``  :py:class:`~.Poly`, the minimal polynomial of $\theta$
        ``rep``      :py:class:`~sympy.polys.polyclasses.DMP` giving $\alpha$ as poly in $\theta$
        ``alias``    :py:class:`~.Symbol` for $\theta$, or ``None``
        ===========  ============================================================================

        See Parameters section for how they are determined.

        Parameters
        ==========

        expr : :py:class:`~.Expr`, or pair $(m, r)$
            There are three distinct modes of construction, depending on what
            is passed as *expr*.

            **(1)** *expr* is an :py:class:`~.AlgebraicNumber`:
            In this case we begin by copying all four instance attributes from
            *expr*. If *coeffs* were also given, we compose the two coeff
            polynomials (see below). If an *alias* was given, it overrides.

            **(2)** *expr* is any other type of :py:class:`~.Expr`:
            Then ``root`` will equal *expr*. Therefore it
            must express an algebraic quantity, and we will compute its
            ``minpoly``.

            **(3)** *expr* is an ordered pair $(m, r)$ giving the
            ``minpoly`` $m$, and a ``root`` $r$ thereof, which together
            define $\theta$. In this case $m$ may be either a univariate
            :py:class:`~.Poly` or any :py:class:`~.Expr` which represents the
            same, while $r$ must be some :py:class:`~.Expr` representing a
            complex number that is a root of $m$, including both explicit
            expressions in radicals, and instances of
            :py:class:`~.ComplexRootOf` or :py:class:`~.AlgebraicNumber`.

        coeffs : list, :py:class:`~.ANP`, None, optional (default=None)
            This defines ``rep``, giving the algebraic number $\alpha$ as a
            polynomial in $\theta$.

            If a list, the elements should be integers or rational numbers.
            If an :py:class:`~.ANP`, we take its coefficients (using its
            :py:meth:`~.ANP.to_list()` method). If ``None``, then the list of
            coefficients defaults to ``[1, 0]``, meaning that $\alpha = \theta$
            is the primitive element of the field.

            If *expr* was an :py:class:`~.AlgebraicNumber`, let $g(x)$ be its
            ``rep`` polynomial, and let $f(x)$ be the polynomial defined by
            *coeffs*. Then ``self.rep`` will represent the composition
            $(f \circ g)(x)$.

        alias : str, :py:class:`~.Symbol`, None, optional (default=None)
            This is a way to provide a name for the primitive element. We
            described several ways in which the *expr* argument can define the
            value of the primitive element, but none of these methods gave it
            a name. Here, for example, *alias* could be set as
            ``Symbol('theta')``, in order to make this symbol appear when
            $\alpha$ is printed, or rendered as a polynomial, using the
            :py:meth:`~.as_poly()` method.

        Examples
        ========

        Recall that we are constructing an algebraic number as a field element
        $\alpha \in \mathbb{Q}(\theta)$.

        >>> from sympy import AlgebraicNumber, sqrt, CRootOf, S
        >>> from sympy.abc import x

        Example (1): $\alpha = \theta = \sqrt{2}$

        >>> a1 = AlgebraicNumber(sqrt(2))
        >>> a1.minpoly_of_element().as_expr(x)
        x**2 - 2
        >>> a1.evalf(10)
        1.414213562

        Example (2): $\alpha = 3 \sqrt{2} - 5$, $\theta = \sqrt{2}$. We can
        either build on the last example:

        >>> a2 = AlgebraicNumber(a1, [3, -5])
        >>> a2.as_expr()
        -5 + 3*sqrt(2)

        or start from scratch:

        >>> a2 = AlgebraicNumber(sqrt(2), [3, -5])
        >>> a2.as_expr()
        -5 + 3*sqrt(2)

        Example (3): $\alpha = 6 \sqrt{2} - 11$, $\theta = \sqrt{2}$. Again we
        can build on the previous example, and we see that the coeff polys are
        composed:

        >>> a3 = AlgebraicNumber(a2, [2, -1])
        >>> a3.as_expr()
        -11 + 6*sqrt(2)

        reflecting the fact that $(2x - 1) \circ (3x - 5) = 6x - 11$.

        Example (4): $\alpha = \sqrt{2}$, $\theta = \sqrt{2} + \sqrt{3}$. The
        easiest way is to use the :py:func:`~.to_number_field()` function:

        >>> from sympy import to_number_field
        >>> a4 = to_number_field(sqrt(2), sqrt(2) + sqrt(3))
        >>> a4.minpoly_of_element().as_expr(x)
        x**2 - 2
        >>> a4.to_root()
        sqrt(2)
        >>> a4.primitive_element()
        sqrt(2) + sqrt(3)
        >>> a4.coeffs()
        [1/2, 0, -9/2, 0]

        but if you already knew the right coefficients, you could construct it
        directly:

        >>> a4 = AlgebraicNumber(sqrt(2) + sqrt(3), [S(1)/2, 0, S(-9)/2, 0])
        >>> a4.to_root()
        sqrt(2)
        >>> a4.primitive_element()
        sqrt(2) + sqrt(3)

        Example (5): Construct the Golden Ratio as an element of the 5th
        cyclotomic field, supposing we already know its coefficients. This time
        we introduce the alias $\zeta$ for the primitive element of the field:

        >>> from sympy import cyclotomic_poly
        >>> from sympy.abc import zeta
        >>> a5 = AlgebraicNumber(CRootOf(cyclotomic_poly(5), -1),
        ...                  [-1, -1, 0, 0], alias=zeta)
        >>> a5.as_poly().as_expr()
        -zeta**3 - zeta**2
        >>> a5.evalf()
        1.61803398874989

        (The index ``-1`` to ``CRootOf`` selects the complex root with the
        largest real and imaginary parts, which in this case is
        $\mathrm{e}^{2i\pi/5}$. See :py:class:`~.ComplexRootOf`.)

        Example (6): Building on the last example, construct the number
        $2 \phi \in \mathbb{Q}(\phi)$, where $\phi$ is the Golden Ratio:

        >>> from sympy.abc import phi
        >>> a6 = AlgebraicNumber(a5.to_root(), coeffs=[2, 0], alias=phi)
        >>> a6.as_poly().as_expr()
        2*phi
        >>> a6.primitive_element().evalf()
        1.61803398874989

        Note that we needed to use ``a5.to_root()``, since passing ``a5`` as
        the first argument would have constructed the number $2 \phi$ as an
        element of the field $\mathbb{Q}(\zeta)$:

        >>> a6_wrong = AlgebraicNumber(a5, coeffs=[2, 0])
        >>> a6_wrong.as_poly().as_expr()
        -2*zeta**3 - 2*zeta**2
        >>> a6_wrong.primitive_element().evalf()
        0.309016994374947 + 0.951056516295154*I

        r   )ANPDMP)minimal_polynomialNPolygenTZpolysr   )dup_compose)Symbol)Zsympy.polys.polyclassesr  r  Zsympy.polys.numberfieldsr  r   r@   r   r   Zis_Polyr   r  is_AlgebraicNumberr  r  r  r  getZ
get_domainZfrom_sympy_list	from_listZto_listZsympy.polys.densetoolsr  degreeremsymbolr  r   r   r  )r   exprcoeffsr  r~   r  r  r  Zrep0Zalias0r  r  r  domr  Zscoeffsr  r   Zsargsr  r   r6   r6   r7   r   	  sj     *
   






zAlgebraicNumber.__new__c                   s
   t   S r1   r   r   r   r6   r7   r   
  s    zAlgebraicNumber.__hash__c                 C  s   |   |S r1   )as_exprZ_evalfr   r6   r6   r7   r   
  s    zAlgebraicNumber._eval_evalfc                 C  s
   | j dk	S )z'Returns ``True`` if ``alias`` was set. N)r  r   r6   r6   r7   
is_aliased
  s    zAlgebraicNumber.is_aliasedc                 C  sb   ddl m}m} |dk	r&|| j|S | jdk	r@|| j| jS ddlm} || j|dS dS )z&Create a Poly instance from ``self``. r   )r  PurePolyNr   Dummyr   )r   r  r  r   r  r  r  r  )r   r   r  r  r  r6   r6   r7   as_poly
  s    
zAlgebraicNumber.as_polyc                 C  s   |  |p| j  S )z)Create a Basic expression from ``self``. )r  r  r  expand)r   r   r6   r6   r7   r  
  s    zAlgebraicNumber.as_exprc                   s    fdd j  D S )z7Returns all SymPy coefficients of an algebraic number. c                   s   g | ]} j j|qS r6   )r  r  Zto_sympy)r4   r   r   r6   r7   r;   
  s     z*AlgebraicNumber.coeffs.<locals>.<listcomp>r  Z
all_coeffsr   r6   r   r7   r  
  s    zAlgebraicNumber.coeffsc                 C  s
   | j  S )z8Returns all native coefficients of an algebraic number. r  r   r6   r6   r7   native_coeffs
  s    zAlgebraicNumber.native_coeffsc                 C  sv   ddl m} | j}| dkr"| S | | d  }|||j|  }|| }| | j }t||f| 	 S )z*Convert ``self`` to an algebraic integer. r   r  r   )
r   r  r  ZLCr  Zcomposer  r  r  r  )r   r  ry   coeffZpolyr  r  r6   r6   r7   to_algebraic_integer
  s    z$AlgebraicNumber.to_algebraic_integerc                   s~   ddl m  ddlm} |d |d  }} fdd| j D D ]6}|| j| jrB||||| j k rBt|  S qB| S )Nr   CRootOfr  measureratioc                   s   g | ]}|j  kr|qS r6   )func)r4   r   r  r6   r7   r;     s     
 z2AlgebraicNumber._eval_simplify.<locals>.<listcomp>)Zsympy.polys.rootoftoolsr  Zsympy.polysr  	all_rootsr  Z	is_Symbolr  )r   r   r  r  r  r   r6   r  r7   _eval_simplify  s    zAlgebraicNumber._eval_simplifyc                 C  s   t | j| jf|| jdS )a\  
        Form another element of the same number field.

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

        If we represent $\alpha \in \mathbb{Q}(\theta)$, form another element
        $\beta \in \mathbb{Q}(\theta)$ of the same number field.

        Parameters
        ==========

        coeffs : list, :py:class:`~.ANP`
            Like the *coeffs* arg to the class
            :py:meth:`constructor<.AlgebraicNumber.__new__>`, defines the
            new element as a polynomial in the primitive element.

            If a list, the elements should be integers or rational numbers.
            If an :py:class:`~.ANP`, we take its coefficients (using its
            :py:meth:`~.ANP.to_list()` method).

        Examples
        ========

        >>> from sympy import AlgebraicNumber, sqrt
        >>> a = AlgebraicNumber(sqrt(5), [-1, 1])
        >>> b = a.field_element([3, 2])
        >>> print(a)
        1 - sqrt(5)
        >>> print(b)
        2 + 3*sqrt(5)
        >>> print(b.primitive_element() == a.primitive_element())
        True

        See Also
        ========

        AlgebraicNumber
        )r  r  )r  r  r  r  )r   r  r6   r6   r7   field_element  s
    (
  zAlgebraicNumber.field_elementc                 C  s    |   }|ddgkp|| jgkS )z
        Say whether this algebraic number $\alpha \in \mathbb{Q}(\theta)$ is
        equal to the primitive element $\theta$ for its field.
        r   r   )r  r  )r   r   r6   r6   r7   is_primitive_element=  s    z$AlgebraicNumber.is_primitive_elementc                 C  s   | j r
| S | ddgS )z
        Get the primitive element $\theta$ for the number field
        $\mathbb{Q}(\theta)$ to which this algebraic number $\alpha$ belongs.

        Returns
        =======

        AlgebraicNumber

        r   r   )r  r  r   r6   r6   r7   primitive_elementG  s    z!AlgebraicNumber.primitive_elementc                 C  s*   | j r
| S |  }| j|d}t||fS )a  
        Convert ``self`` to an :py:class:`~.AlgebraicNumber` instance that is
        equal to its own primitive element.

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

        If we represent $\alpha \in \mathbb{Q}(\theta)$, $\alpha \neq \theta$,
        construct a new :py:class:`~.AlgebraicNumber` that represents
        $\alpha \in \mathbb{Q}(\alpha)$.

        Examples
        ========

        >>> from sympy import sqrt, to_number_field
        >>> from sympy.abc import x
        >>> a = to_number_field(sqrt(2), sqrt(2) + sqrt(3))

        The :py:class:`~.AlgebraicNumber` ``a`` represents the number
        $\sqrt{2}$ in the field $\mathbb{Q}(\sqrt{2} + \sqrt{3})$. Rendering
        ``a`` as a polynomial,

        >>> a.as_poly().as_expr(x)
        x**3/2 - 9*x/2

        reflects the fact that $\sqrt{2} = \theta^3/2 - 9 \theta/2$, where
        $\theta = \sqrt{2} + \sqrt{3}$.

        ``a`` is not equal to its own primitive element. Its minpoly

        >>> a.minpoly.as_poly().as_expr(x)
        x**4 - 10*x**2 + 1

        is that of $\theta$.

        Converting to a primitive element,

        >>> a_prim = a.to_primitive_element()
        >>> a_prim.minpoly.as_poly().as_expr(x)
        x**2 - 2

        we obtain an :py:class:`~.AlgebraicNumber` whose ``minpoly`` is that of
        the number itself.

        Parameters
        ==========

        radicals : boolean, optional (default=True)
            If ``True``, then we will try to return an
            :py:class:`~.AlgebraicNumber` whose ``root`` is an expression
            in radicals. If that is not possible (or if *radicals* is
            ``False``), ``root`` will be a :py:class:`~.ComplexRootOf`.

        Returns
        =======

        AlgebraicNumber

        See Also
        ========

        is_primitive_element

        radicals)r  minpoly_of_elementto_rootr  )r   r  r   r   r6   r6   r7   to_primitive_elementV  s
    Az$AlgebraicNumber.to_primitive_elementc                 C  sH   | j dkrB| jr| j| _ n(ddlm} |  }|| |dd| _ | j S )a{  
        Compute the minimal polynomial for this algebraic number.

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

        Recall that we represent an element $\alpha \in \mathbb{Q}(\theta)$.
        Our instance attribute ``self.minpoly`` is the minimal polynomial for
        our primitive element $\theta$. This method computes the minimal
        polynomial for $\alpha$.

        Nr   r  Tr  )r  r  r  Z sympy.polys.numberfields.minpolyr  r  )r   r  thetar6   r6   r7   r    s    

z"AlgebraicNumber.minpoly_of_elementc                 C  sn   | j rt| jts| jS |p"|  }|j|d}t|dkrD|d S |  }|D ]}|||rP|  S qPdS )a  
        Convert to an :py:class:`~.Expr` that is not an
        :py:class:`~.AlgebraicNumber`, specifically, either a
        :py:class:`~.ComplexRootOf`, or, optionally and where possible, an
        expression in radicals.

        Parameters
        ==========

        radicals : boolean, optional (default=True)
            If ``True``, then we will try to return the root as an expression
            in radicals. If that is not possible, we will return a
            :py:class:`~.ComplexRootOf`.

        minpoly : :py:class:`~.Poly`
            If the minimal polynomial for `self` has been pre-computed, it can
            be passed in order to save time.

        r  r   r   N)	r  r@   r  r  r  r  rj   r  Z	same_root)r   r  r  r   rootsr  rM   r6   r6   r7   r    s    zAlgebraicNumber.to_root)NN)N)N)T)TN)r   r  r  r  r	  r  is_algebraicr2   r   r
  setr  rd  r   r   r   rh  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r6   r6   r   r7   r  	  s4   

 i


+
	
Gr  c                   @  s   e Zd ZdZdZdd ZdS )RationalConstantz
    Abstract base class for rationals with specific behaviors

    Derived classes must define class attributes p and q and should probably all
    be singletons.
    r6   c                 C  s
   t | S r1   r   r   r   r6   r6   r7   r     s    zRationalConstant.__new__N)r   r  r  r  r	  r   r6   r6   r6   r7   r    s   r  c                   @  s   e Zd ZdZdd ZdS )IntegerConstantr6   c                 C  s
   t | S r1   r  r   r6   r6   r7   r     s    zIntegerConstant.__new__N)r   r  r  r	  r   r6   r6   r6   r7   r    s   r  c                   @  sh   e Zd ZdZdZdZdZdZdZdZ	dZ
dZdd Zed	d
 Zedd Zdd Zdd Zdd ZdS )r   a  The number zero.

    Zero is a singleton, and can be accessed by ``S.Zero``

    Examples
    ========

    >>> from sympy import S, Integer
    >>> Integer(0) is S.Zero
    True
    >>> 1/S.Zero
    zoo

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Zero
    r   r   FTr6   c                 C  s   dS r   r6   r   r6   r6   r7   rx    s    zZero.__getnewargs__c                   C  s   t jS r1   r   r6   r6   r6   r7   rO    s    zZero.__abs__c                   C  s   t jS r1   r   r6   r6   r6   r7   r>    s    zZero.__neg__c                 C  sd   |j r
| S |jrtjS |jdkr&tjS |jr2tjS | \}}|j	rNtj| S |tjk	r`| | S d S NF)
rI  r   r   rJ  rg  r   r   r   r   r   )r   rZ   r  termsr6   r6   r7   rL    s    


zZero._eval_powerc                 G  s   | S r1   r6   r   r   r6   r6   r7   r   #  s    zZero._eval_orderc                 C  s   dS r  r6   r   r6   r6   r7   r=  '  s    zZero.__bool__N)r   r  r  r  rd   re   r   r   r   r2   rX  r	  rx  staticmethodrO  r>  rL  r   r=  r6   r6   r6   r7   r     s"   

r   )	metaclassc                   @  sb   e Zd ZdZdZdZdZdZdZdd Z	e
dd Ze
d	d
 Zdd Zdd Ze
dddZdS )r   a  The number one.

    One is a singleton, and can be accessed by ``S.One``.

    Examples
    ========

    >>> from sympy import S, Integer
    >>> Integer(1) is S.One
    True

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/1_%28number%29
    Tr   r6   c                 C  s   dS r   r6   r   r6   r6   r7   rx  D  s    zOne.__getnewargs__c                   C  s   t jS r1   r  r6   r6   r6   r7   rO  G  s    zOne.__abs__c                   C  s   t jS r1   )r   r   r6   r6   r6   r7   r>  K  s    zOne.__neg__c                 C  s   | S r1   r6   r   rZ   r6   r6   r7   rL  O  s    zOne._eval_powerc                 G  s   d S r1   r6   r  r6   r6   r7   r   R  s    zOne._eval_orderNFc                 C  s   |r
t jS i S d S r1   r  )r  r  r  r  r  r  r6   r6   r7   r  U  s    zOne.factors)NTFFFF)r   r  r  r  r2   r   rd   re   r	  rx  r  rO  r>  rL  r   r  r6   r6   r6   r7   r   +  s$   

    r   c                   @  sH   e Zd ZdZdZdZdZdZdd Ze	dd	 Z
e	d
d Zdd ZdS )r   a]  The number negative one.

    NegativeOne is a singleton, and can be accessed by ``S.NegativeOne``.

    Examples
    ========

    >>> from sympy import S, Integer
    >>> Integer(-1) is S.NegativeOne
    True

    See Also
    ========

    One

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/%E2%88%921_%28number%29

    Trb   r   r6   c                 C  s   dS r   r6   r   r6   r6   r7   rx  |  s    zNegativeOne.__getnewargs__c                   C  s   t jS r1   r  r6   r6   r6   r7   rO    s    zNegativeOne.__abs__c                   C  s   t jS r1   r  r6   r6   r6   r7   r>    s    zNegativeOne.__neg__c                 C  s   |j rtjS |jrtjS t|trt|tr8td| S |tjkrHtjS |tj	tj
fkr^tjS |tjkrntjS t|tr|jdkrtjt|j S t|j|j\}}|r| | | t||j  S d S )Ng      r/   )is_oddr   r   r|  r   r@   r   rD   r   r   r   rr  rM  ro   re   rk   rd   r   )r   rZ   r5   r   r6   r6   r7   rL    s(    





zNegativeOne._eval_powerN)r   r  r  r  r2   rd   re   r	  rx  r  rO  r>  rL  r6   r6   r6   r7   r   ^  s   

r   c                   @  s4   e Zd ZdZdZdZdZdZdd Ze	dd	 Z
d
S )rr  a  The rational number 1/2.

    Half is a singleton, and can be accessed by ``S.Half``.

    Examples
    ========

    >>> from sympy import S, Rational
    >>> Rational(1, 2) is S.Half
    True

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/One_half
    Tr   r/   r6   c                 C  s   dS r   r6   r   r6   r6   r7   rx    s    zHalf.__getnewargs__c                   C  s   t jS r1   )r   rr  r6   r6   r6   r7   rO    s    zHalf.__abs__N)r   r  r  r  r2   rd   re   r	  rx  r  rO  r6   r6   r6   r7   rr    s   rr  c                      s@  e 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d	d
 Zd/ddZd0ddZededd ZeZededd Zededd Zededd ZeZededd Zdd Zdd Zdd  Zd!d" Z fd#d$Zd%d& Z d'd( Z!e"j#Z#e"j$Z$e"j%Z%e"j&Z&eded)d* Z'e'Z(d+d, Z)d-d. Z*  Z+S )1r   a  Positive infinite quantity.

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

    In real analysis the symbol `\infty` denotes an unbounded
    limit: `x\to\infty` means that `x` grows without bound.

    Infinity is often used not only to define a limit but as a value
    in the affinely extended real number system.  Points labeled `+\infty`
    and `-\infty` can be added to the topological space of the real numbers,
    producing the two-point compactification of the real numbers.  Adding
    algebraic properties to this gives us the extended real numbers.

    Infinity is a singleton, and can be accessed by ``S.Infinity``,
    or can be imported as ``oo``.

    Examples
    ========

    >>> from sympy import oo, exp, limit, Symbol
    >>> 1 + oo
    oo
    >>> 42/oo
    0
    >>> x = Symbol('x')
    >>> limit(exp(x), x, oo)
    oo

    See Also
    ========

    NegativeInfinity, NaN

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Infinity
    TFr6   c                 C  s
   t | S r1   r  r   r6   r6   r7   r     s    zInfinity.__new__c                 C  s   dS )Nz\inftyr6   r   printerr6   r6   r7   _latex  s    zInfinity._latexc                 C  s   | |kr|S d S r1   r6   r   r6   r6   r7   r     s    zInfinity._eval_subsNc                 C  s   t dS Nr   rD   r   r6   r6   r7   r     s    zInfinity._eval_evalfc                 K  s
   |  |S r1   r   r   rW   optionsr6   r6   r7   rY    s    zInfinity.evalfr   c                 C  s6   t |tr*tjr*|tjtjfkr&tjS | S t| |S r1   )r@   r   r.   r   r   r   r   r   r   r6   r6   r7   r     s
    zInfinity.__add__c                 C  s6   t |tr*tjr*|tjtjfkr&tjS | S t| |S r1   )r@   r   r.   r   r   r   r   r   r   r6   r6   r7   r     s
    zInfinity.__sub__c                 C  s   |   |S r1   r   r   r6   r6   r7   rz    s    zInfinity.__rsub__c                 C  sB   t |tr6tjr6|js |tjkr&tjS |jr0| S tjS t	| |S r1   )
r@   r   r.   r   r   r   r   rI  r   r   r   r6   r6   r7   r     s    zInfinity.__mul__c                 C  sP   t |trDtjrD|tjks.|tjks.|tjkr4tjS |jr>| S tjS t	| |S r1   
r@   r   r.   r   r   r   r   r   Zis_extended_nonnegativer   r   r6   r6   r7   r   "  s    
zInfinity.__truediv__c                 C  s   t jS r1   r   r   r   r6   r6   r7   rO  .  s    zInfinity.__abs__c                 C  s   t jS r1   )r   r   r   r6   r6   r7   r>  1  s    zInfinity.__neg__c                 C  s   |j rtjS |jrtjS |tjkr(tjS |tjkr8tjS |jdkr|jrddl	m
} ||}|jrhtjS |jrttjS |jrtjS | |  S dS )a  
        ``expt`` is symbolic object but not equal to 0 or 1.

        ================ ======= ==============================
        Expression       Result  Notes
        ================ ======= ==============================
        ``oo ** nan``    ``nan``
        ``oo ** -p``     ``0``   ``p`` is number, ``oo``
        ================ ======= ==============================

        See Also
        ========
        Pow
        NaN
        NegativeInfinity

        Fr   )rN  N)rI  r   r   r   r   r   rJ  rg  r2   r   rN  r   r   r   rY  )r   rZ   rN  Z	expt_realr6   r6   r7   rL  4  s$    

zInfinity._eval_powerc                 C  s   t jS r1   )r   r$   r   r6   r6   r7   r   Z  s    zInfinity._as_mpf_valc                   s
   t   S r1   r   r   r   r6   r7   r   ]  s    zInfinity.__hash__c                 C  s   |t jkp|tdkS r  r   r   r   r   r6   r6   r7   r   `  s    zInfinity.__eq__c                 C  s   |t jk	o|tdkS r  r  r   r6   r6   r7   r   c  s    zInfinity.__ne__c                 C  s   t |tstS tjS r1   r@   r   r   r   r   r   r6   r6   r7   rD  k  s    
zInfinity.__mod__c                 C  s   | S r1   r6   r   r6   r6   r7   r   s  s    zInfinity.floorc                 C  s   | S r1   r6   r   r6   r6   r7   r   v  s    zInfinity.ceiling)N)N),r   r  r  r  r  r2   
is_complexrg  r   rX  rI  r  r	  r   r  r   r   rY  r   r   r   r  r   rz  r   r  r   rO  r>  rL  r   r   r   r   r   r   r   r   r   rD  rF  r   r   r  r6   r6   r   r7   r     sV   (






&
r   c                      sH  e 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d	d
 Zd1ddZd2ddZededd ZeZededd Zededd Zededd ZeZededd Zdd Zdd Zdd  Zd!d" Z fd#d$Zd%d& Z d'd( Z!e"j#Z#e"j$Z$e"j%Z%e"j&Z&eded)d* Z'e'Z(d+d, Z)d-d. Z*d/d0 Z+  Z,S )3r   zNegative infinite quantity.

    NegativeInfinity is a singleton, and can be accessed
    by ``S.NegativeInfinity``.

    See Also
    ========

    Infinity
    TFr6   c                 C  s
   t | S r1   r  r   r6   r6   r7   r     s    zNegativeInfinity.__new__c                 C  s   dS )Nz-\inftyr6   r  r6   r6   r7   r    s    zNegativeInfinity._latexc                 C  s   | |kr|S d S r1   r6   r   r6   r6   r7   r     s    zNegativeInfinity._eval_subsNc                 C  s   t dS Nr   r  r   r6   r6   r7   r     s    zNegativeInfinity._eval_evalfc                 K  s
   |  |S r1   r  r  r6   r6   r7   rY    s    zNegativeInfinity.evalfr   c                 C  s6   t |tr*tjr*|tjtjfkr&tjS | S t| |S r1   )r@   r   r.   r   r   r   r   r   r   r6   r6   r7   r     s
    zNegativeInfinity.__add__c                 C  s6   t |tr*tjr*|tjtjfkr&tjS | S t| |S r1   )r@   r   r.   r   r   r   r   r   r   r6   r6   r7   r     s
    zNegativeInfinity.__sub__c                 C  s   |   |S r1   r
  r   r6   r6   r7   rz    s    zNegativeInfinity.__rsub__c                 C  sB   t |tr6tjr6|js |tjkr&tjS |jr0| S tjS t	| |S r1   )
r@   r   r.   r   r   r   r   rI  r   r   r   r6   r6   r7   r     s    zNegativeInfinity.__mul__c                 C  sP   t |trDtjrD|tjks.|tjks.|tjkr4tjS |jr>| S tjS t	| |S r1   r  r   r6   r6   r7   r     s    
zNegativeInfinity.__truediv__c                 C  s   t jS r1   r  r   r6   r6   r7   rO    s    zNegativeInfinity.__abs__c                 C  s   t jS r1   r  r   r6   r6   r7   r>    s    zNegativeInfinity.__neg__c                 C  s   |j r|tjks$|tjks$|tjkr*tjS t|trL|jrL|jrFtjS tjS tj| }tj	| }|dkrr|j
rr|S |tjkr|j
r|jstjS || S dS )a^  
        ``expt`` is symbolic object but not equal to 0 or 1.

        ================ ======= ==============================
        Expression       Result  Notes
        ================ ======= ==============================
        ``(-oo) ** nan`` ``nan``
        ``(-oo) ** oo``  ``nan``
        ``(-oo) ** -oo`` ``nan``
        ``(-oo) ** e``   ``oo``  ``e`` is positive even integer
        ``(-oo) ** o``   ``-oo`` ``o`` is positive odd integer
        ================ ======= ==============================

        See Also
        ========

        Infinity
        Pow
        NaN

        r   N)r2   r   r   r   r   r@   rk   rI  r  r   rg   rJ  r   )r   rZ   Zinf_partZs_partr6   r6   r7   rL    s,    



zNegativeInfinity._eval_powerc                 C  s   t jS r1   )r   r%   r   r6   r6   r7   r      s    zNegativeInfinity._as_mpf_valc                   s
   t   S r1   r   r   r   r6   r7   r     s    zNegativeInfinity.__hash__c                 C  s   |t jkp|tdkS r  r   r   r   r   r6   r6   r7   r     s    zNegativeInfinity.__eq__c                 C  s   |t jk	o|tdkS r  r  r   r6   r6   r7   r   	  s    zNegativeInfinity.__ne__c                 C  s   t |tstS tjS r1   r  r   r6   r6   r7   rD    s    
zNegativeInfinity.__mod__c                 C  s   | S r1   r6   r   r6   r6   r7   r     s    zNegativeInfinity.floorc                 C  s   | S r1   r6   r   r6   r6   r7   r     s    zNegativeInfinity.ceilingc                 C  s   t jdt jdiS rV  )r   r   r   r   r6   r6   r7   as_powers_dict  s    zNegativeInfinity.as_powers_dict)N)N)-r   r  r  r  rg  r  r  r   rX  r   r2   r  r	  r   r  r   r   rY  r   r   r   r  r   rz  r   r  r   rO  r>  rL  r   r   r   r   r   r   r   r   r   rD  rF  r   r   r  r  r6   r6   r   r7   r   |  sX   






+
r   c                      s   e 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ZdZdZdZdd Zdd	 Zd
d Zededd Zededd Zededd Zededd Zdd Zdd Zdd Z fddZdd Z dd  Z!e"j#Z#e"j$Z$e"j%Z%e"j&Z&  Z'S )!r   a.  
    Not a Number.

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

    This serves as a place holder for numeric values that are indeterminate.
    Most operations on NaN, produce another NaN.  Most indeterminate forms,
    such as ``0/0`` or ``oo - oo` produce NaN.  Two exceptions are ``0**0``
    and ``oo**0``, which all produce ``1`` (this is consistent with Python's
    float).

    NaN is loosely related to floating point nan, which is defined in the
    IEEE 754 floating point standard, and corresponds to the Python
    ``float('nan')``.  Differences are noted below.

    NaN is mathematically not equal to anything else, even NaN itself.  This
    explains the initially counter-intuitive results with ``Eq`` and ``==`` in
    the examples below.

    NaN is not comparable so inequalities raise a TypeError.  This is in
    contrast with floating point nan where all inequalities are false.

    NaN is a singleton, and can be accessed by ``S.NaN``, or can be imported
    as ``nan``.

    Examples
    ========

    >>> from sympy import nan, S, oo, Eq
    >>> nan is S.NaN
    True
    >>> oo - oo
    nan
    >>> nan + 1
    nan
    >>> Eq(nan, nan)   # mathematical equality
    False
    >>> nan == nan     # structural equality
    True

    References
    ==========

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

    TNFr6   c                 C  s
   t | S r1   r  r   r6   r6   r7   r   d  s    zNaN.__new__c                 C  s   dS )Nz
\text{NaN}r6   r  r6   r6   r7   r  g  s    z
NaN._latexc                 C  s   | S r1   r6   r   r6   r6   r7   r>  j  s    zNaN.__neg__r   c                 C  s   | S r1   r6   r   r6   r6   r7   r   m  s    zNaN.__add__c                 C  s   | S r1   r6   r   r6   r6   r7   r   q  s    zNaN.__sub__c                 C  s   | S r1   r6   r   r6   r6   r7   r   u  s    zNaN.__mul__c                 C  s   | S r1   r6   r   r6   r6   r7   r   y  s    zNaN.__truediv__c                 C  s   | S r1   r6   r   r6   r6   r7   r   }  s    z	NaN.floorc                 C  s   | S r1   r6   r   r6   r6   r7   r     s    zNaN.ceilingc                 C  s   t S r1   )r(  r   r6   r6   r7   r     s    zNaN._as_mpf_valc                   s
   t   S r1   r   r   r   r6   r7   r     s    zNaN.__hash__c                 C  s
   |t jkS r1   r   r   r   r6   r6   r7   r     s    z
NaN.__eq__c                 C  s
   |t jk	S r1   r  r   r6   r6   r7   r     s    z
NaN.__ne__)(r   r  r  r  r  rg  rf  re  r  is_transcendentalr  rX  rg   r   r  r   r   r2   r	  r   r  r>  r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r6   r6   r   r7   r   #  sJ   /



r   c                 C  s   dS r  r6   )rL   rM   r6   r6   r7   _eval_is_eq  s    r  c                   @  sp   e Zd ZdZdZdZdZdZdZdZ	e
ZdZdd Zdd Zed	d
 Zdd Zdd Zedd Zdd ZdS )rJ  a  Complex infinity.

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

    In complex analysis the symbol `\tilde\infty`, called "complex
    infinity", represents a quantity with infinite magnitude, but
    undetermined complex phase.

    ComplexInfinity is a singleton, and can be accessed by
    ``S.ComplexInfinity``, or can be imported as ``zoo``.

    Examples
    ========

    >>> from sympy import zoo
    >>> zoo + 42
    zoo
    >>> 42/zoo
    0
    >>> zoo + zoo
    nan
    >>> zoo*zoo
    zoo

    See Also
    ========

    Infinity
    TFr6   c                 C  s
   t | S r1   r  r   r6   r6   r7   r     s    zComplexInfinity.__new__c                 C  s   dS )Nz\tilde{\infty}r6   r  r6   r6   r7   r    s    zComplexInfinity._latexc                   C  s   t jS r1   r  r6   r6   r6   r7   rO    s    zComplexInfinity.__abs__c                 C  s   | S r1   r6   r   r6   r6   r7   r     s    zComplexInfinity.floorc                 C  s   | S r1   r6   r   r6   r6   r7   r     s    zComplexInfinity.ceilingc                   C  s   t jS r1   )r   rJ  r6   r6   r6   r7   r>    s    zComplexInfinity.__neg__c                 C  s<   |t jkrt jS t|tr8|jr&t jS |jr2t jS t jS d S r1   )r   rJ  r   r@   r   r   r   r   r   r6   r6   r7   rL    s    

zComplexInfinity._eval_powerN)r   r  r  r  r  r   r2   r  r  rg  r   r
  r	  r   r  r  rO  r   r   r>  rL  r6   r6   r6   r7   rJ    s$   

rJ  c                      st   e Zd ZdZdZdZdZdZe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 fddZ  ZS )r\  Tr6   c                 C  s
   t | S r1   r  r   r6   r6   r7   r     s    zNumberSymbol.__new__c                 C  s   dS )z Return an interval with number_cls endpoints
        that contains the value of NumberSymbol.
        If not implemented, then return None.
        Nr6   r   Z
number_clsr6   r6   r7   approximation  s    zNumberSymbol.approximationc                 C  s   t | ||S r1   r   r   r6   r6   r7   r     s    zNumberSymbol._eval_evalfc                 C  sF   zt |}W n tk
r$   t Y S X | |kr2dS |jrB| jrBdS dS r1  )r
   r   r   r  rS  r   r6   r6   r7   r     s    
zNumberSymbol.__eq__c                 C  s
   | |k S r1   r6   r   r6   r6   r7   r     s    zNumberSymbol.__ne__c                 C  s   | |krt jS t| |S r1   )r   r   r   r   r   r6   r6   r7   r     s    zNumberSymbol.__le__c                 C  s   | |krt jS t| |S r1   )r   r   r   r   r   r6   r6   r7   r     s    zNumberSymbol.__ge__c                 C  s   t d S r1   )r   r   r6   r6   r7   rP    s    zNumberSymbol.__int__c                   s
   t   S r1   r   r   r   r6   r7   r   "  s    zNumberSymbol.__hash__)r   r  r  r  rg   r2   r	  rR  r   r
  r   r  r   r   r   r   r   rP  r   r  r6   r6   r   r7   r\    s   r\  c                   @  s|   e Zd ZdZdZdZdZdZdZdZ	dZ
dZd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S )Exp1a:  The `e` constant.

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

    The transcendental number `e = 2.718281828\ldots` is the base of the
    natural logarithm and of the exponential function, `e = \exp(1)`.
    Sometimes called Euler's number or Napier's constant.

    Exp1 is a singleton, and can be accessed by ``S.Exp1``,
    or can be imported as ``E``.

    Examples
    ========

    >>> from sympy import exp, log, E
    >>> E is exp(1)
    True
    >>> log(E)
    1

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/E_%28mathematical_constant%29
    TFr6   c                 C  s   dS )Nrs   r6   r  r6   r6   r7   r  L  s    zExp1._latexc                   C  s   t jS r1   )r   r  r6   r6   r6   r7   rO  O  s    zExp1.__abs__c                 C  s   dS r  r6   r   r6   r6   r7   rP  S  s    zExp1.__int__c                 C  s   t |S r1   )r!   r   r6   r6   r7   r   V  s    zExp1._as_mpf_valc                 C  s(   t |trtdtdfS t |tr$d S )Nr/   r  
issubclassrk   ro   r  r6   r6   r7   approximation_intervalY  s    

zExp1.approximation_intervalc                 C  s(   t jr| |S ddlm} ||S d S )Nr   )exp)r.   Z
exp_is_pow_eval_power_exp_is_pow&sympy.functions.elementary.exponentialr  )r   rZ   r  r6   r6   r7   rL  _  s    
zExp1._eval_powerc                 C  sb  |j r"|tkrtS |t kr"tjS ddlm} t||rB|jd S |jst	t }||| fkrdt
S |tt	 }| rd| jr|jrtjS |jrtjS |tj jrt	 S |tj jrt	S n@|j r|d }|dkr|d8 }||k rtj|tj tj  S | \}}|tt fkrd S |gd  }}t|D ]J}	t|	|rb|d krZ|	jd }n d S n|	jrv||	 n d S q4|r|t|  S d S |jrNg }
g }d}|jD ]t}|tjkr|| q| | }t|tr|j| kr|j|kr||j d}n
|| n
|
| q|
s2|r^t|
 t| t| dd S n|j r^| S d S )Nr   )logr/   r   FTrG  )!r  oor   r   r  r  r@   r~   Zis_AddIr   r  pir  r|  r   r  r   rr  r   r  PirM  r   MulZ	make_argsrX  appendrK  baser  r  Z	is_Matrix)r   argr  ZIoor  Zncoeffr  r  Zlog_termtermoutaddZ
argchangedrL   Znewar6   r6   r7   r  f  sz    








zExp1._eval_power_exp_is_powc                 K  s*   ddl m} |ttjd  t|t  S )Nr   )sinr/   )(sympy.functions.elementary.trigonometricr+  r!  r   r#  )r   r   r+  r6   r6   r7   _eval_rewrite_as_sin  s    zExp1._eval_rewrite_as_sinc                 K  s*   ddl m} |tt|ttjd    S )Nr   )cosr/   )r,  r.  r!  r   r#  )r   r   r.  r6   r6   r7   _eval_rewrite_as_cos  s    zExp1._eval_rewrite_as_cosN)r   r  r  r  rf  r   r   rS  r2   r  r  r	  r  r  rO  rP  r   r  rL  r  r-  r/  r6   r6   r6   r7   r  &  s&   
Pr  c                   @  s\   e Zd ZdZdZdZdZdZdZdZ	dZ
dZdd Zedd Zd	d
 Zdd Zdd ZdS )r#  a  The `\pi` constant.

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

    The transcendental number `\pi = 3.141592654\ldots` represents the ratio
    of a circle's circumference to its diameter, the area of the unit circle,
    the half-period of trigonometric functions, and many other things
    in mathematics.

    Pi is a singleton, and can be accessed by ``S.Pi``, or can
    be imported as ``pi``.

    Examples
    ========

    >>> from sympy import S, pi, oo, sin, exp, integrate, Symbol
    >>> S.Pi
    pi
    >>> pi > 3
    True
    >>> pi.is_irrational
    True
    >>> x = Symbol('x')
    >>> sin(x + 2*pi)
    sin(x)
    >>> integrate(exp(-x**2), (x, -oo, oo))
    sqrt(pi)

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Pi
    TFr6   c                 C  s   dS )Nz\pir6   r  r6   r6   r7   r    s    z	Pi._latexc                   C  s   t jS r1   )r   r#  r6   r6   r6   r7   rO    s    z
Pi.__abs__c                 C  s   dS )Nr  r6   r   r6   r6   r7   rP    s    z
Pi.__int__c                 C  s   t |S r1   )r    r   r6   r6   r7   r     s    zPi._as_mpf_valc                 C  s@   t |trtdtdfS t |tr<tdddtdddfS d S )Nr  r     G   r         r  r  r6   r6   r7   r    s    

zPi.approximation_intervalN)r   r  r  r  rf  r   r   rS  r2   r  r  r	  r  r  rO  rP  r   r  r6   r6   r6   r7   r#    s   #
r#  c                   @  s\   e 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d	d
 Zdd Zdd ZeZdS )GoldenRatioa  The golden ratio, `\phi`.

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

    `\phi = \frac{1 + \sqrt{5}}{2}` is an algebraic number.  Two quantities
    are in the golden ratio if their ratio is the same as the ratio of
    their sum to the larger of the two quantities, i.e. their maximum.

    GoldenRatio is a singleton, and can be accessed by ``S.GoldenRatio``.

    Examples
    ========

    >>> from sympy import S
    >>> S.GoldenRatio > 1
    True
    >>> S.GoldenRatio.expand(func=True)
    1/2 + sqrt(5)/2
    >>> S.GoldenRatio.is_irrational
    True

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Golden_ratio
    TFr6   c                 C  s   dS )Nz\phir6   r  r6   r6   r7   r  ,  s    zGoldenRatio._latexc                 C  s   dS rV  r6   r   r6   r6   r7   rP  /  s    zGoldenRatio.__int__c                 C  s$   t t|d | d }t||S Nr>   )r   from_man_expr"   r]   r.  r6   r6   r7   r   2  s    zGoldenRatio._as_mpf_valc                 K  s    ddl m} tjtj|d  S )Nr   )sqrtr?   )(sympy.functions.elementary.miscellaneousr7  r   rr  )r   hintsr7  r6   r6   r7   _eval_expand_func7  s    zGoldenRatio._eval_expand_funcc                 C  s&   t |trtjtdfS t |tr"d S r  r  rk   r   r   ro   r  r6   r6   r7   r  ;  s    

z"GoldenRatio.approximation_intervalN)r   r  r  r  rf  r   r   rS  r2   r  r  r	  r  rP  r   r:  r  _eval_rewrite_as_sqrtr6   r6   r6   r7   r4    s   r4  c                   @  s\   e 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d	d
 Zdd Zdd ZeZdS )TribonacciConstanta  The tribonacci constant.

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

    The tribonacci numbers are like the Fibonacci numbers, but instead
    of starting with two predetermined terms, the sequence starts with
    three predetermined terms and each term afterwards is the sum of the
    preceding three terms.

    The tribonacci constant is the ratio toward which adjacent tribonacci
    numbers tend. It is a root of the polynomial `x^3 - x^2 - x - 1 = 0`,
    and also satisfies the equation `x + x^{-3} = 2`.

    TribonacciConstant is a singleton, and can be accessed
    by ``S.TribonacciConstant``.

    Examples
    ========

    >>> from sympy import S
    >>> S.TribonacciConstant > 1
    True
    >>> S.TribonacciConstant.expand(func=True)
    1/3 + (19 - 3*sqrt(33))**(1/3)/3 + (3*sqrt(33) + 19)**(1/3)/3
    >>> S.TribonacciConstant.is_irrational
    True
    >>> S.TribonacciConstant.n(20)
    1.8392867552141611326

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Generalizations_of_Fibonacci_numbers#Tribonacci_numbers
    TFr6   c                 C  s   dS )Nz\text{TribonacciConstant}r6   r  r6   r6   r7   r  s  s    zTribonacciConstant._latexc                 C  s   dS rV  r6   r   r6   r6   r7   rP  v  s    zTribonacciConstant.__int__c                 C  s"   | j dd|d }t||dS )NT)functionr  rC  )r:  r   rD   r.  r6   r6   r7   r   y  s    zTribonacciConstant._eval_evalfc                 K  s@   ddl m}m} d|dd|d   |dd|d   d S )Nr   )cbrtr7  r      r  !   )r8  r?  r7  )r   r9  r?  r7  r6   r6   r7   r:  }  s    z$TribonacciConstant._eval_expand_funcc                 C  s&   t |trtjtdfS t |tr"d S r  r;  r  r6   r6   r7   r    s    

z)TribonacciConstant.approximation_intervalN)r   r  r  r  rf  r   r   rS  r2   r  r  r	  r  rP  r   r:  r  r<  r6   r6   r6   r7   r=  D  s   $r=  c                   @  sH   e Zd ZdZdZdZdZdZdZdZ	dd Z
dd	 Zd
d Zdd ZdS )
EulerGammaa  The Euler-Mascheroni constant.

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

    `\gamma = 0.5772157\ldots` (also called Euler's constant) is a mathematical
    constant recurring in analysis and number theory.  It is defined as the
    limiting difference between the harmonic series and the
    natural logarithm:

    .. math:: \gamma = \lim\limits_{n\to\infty}
              \left(\sum\limits_{k=1}^n\frac{1}{k} - \ln n\right)

    EulerGamma is a singleton, and can be accessed by ``S.EulerGamma``.

    Examples
    ========

    >>> from sympy import S
    >>> S.EulerGamma.is_irrational
    >>> S.EulerGamma > 0
    True
    >>> S.EulerGamma > 1
    False

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Euler%E2%80%93Mascheroni_constant
    TFNr6   c                 C  s   dS )Nz\gammar6   r  r6   r6   r7   r    s    zEulerGamma._latexc                 C  s   dS rB  r6   r   r6   r6   r7   rP    s    zEulerGamma.__int__c                 C  s,   t j|d }t || d }t||S r5  )r   ZlibhyperZeuler_fixedr6  r]   r   rW   r  r\   r6   r6   r7   r     s    zEulerGamma._as_mpf_valc                 C  s6   t |trtjtjfS t |tr2tjtdddfS d S )Nr  r?   r   )r  rk   r   r   r   ro   rr  r  r6   r6   r7   r    s    

z!EulerGamma.approximation_interval)r   r  r  r  rf  r   r   rS  r2   r	  r  rP  r   r  r6   r6   r6   r7   rB    s   rB  c                   @  sR   e Zd ZdZdZdZdZdZdZdZ	dd Z
dd	 Zd
d ZdddZdd ZdS )Catalana  Catalan's constant.

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

    $G = 0.91596559\ldots$ is given by the infinite series

    .. math:: G = \sum_{k=0}^{\infty} \frac{(-1)^k}{(2k+1)^2}

    Catalan is a singleton, and can be accessed by ``S.Catalan``.

    Examples
    ========

    >>> from sympy import S
    >>> S.Catalan.is_irrational
    >>> S.Catalan > 0
    True
    >>> S.Catalan > 1
    False

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Catalan%27s_constant
    TFNr6   c                 C  s   dS rB  r6   r   r6   r6   r7   rP    s    zCatalan.__int__c                 C  s*   t |d }t || d }t||S r5  )r   Zcatalan_fixedr6  r]   rC  r6   r6   r7   r     s    zCatalan._as_mpf_valc                 C  s6   t |trtjtjfS t |tr2tdddtjfS d S )N	   r>   r   )r  rk   r   r   r   ro   r  r6   r6   r7   r    s    

zCatalan.approximation_intervalc                 C  sb   |d k	s|d k	r| S ddl m} ddlm} |dddd}|tj| d| d d  |dtjfS )	Nr   r  r   )Sumr  T)integerZnonnegativer/   )r  r  Zsympy.concrete.summationsrF  r   r   r   )r   Zk_symr   r  rF  r  r6   r6   r7   _eval_rewrite_as_Sum  s    zCatalan._eval_rewrite_as_Sumc                 C  s   dS )NGr6   r  r6   r6   r7   r     s    zCatalan._latex)NN)r   r  r  r  rf  r   r   rS  r2   r	  rP  r   r  rH  r  r6   r6   r6   r7   rD    s   
rD  c                   @  sp   e Zd ZdZdZdZdZdZdZdZ	e
ZdZdd Zedd Zd	d
 Zdd Zdd Zdd Zedd ZdS )rM  aT  The imaginary unit, `i = \sqrt{-1}`.

    I is a singleton, and can be accessed by ``S.I``, or can be
    imported as ``I``.

    Examples
    ========

    >>> from sympy import I, sqrt
    >>> sqrt(-1)
    I
    >>> I*I
    -1
    >>> 1/I
    -I

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Imaginary_unit
    TFr6   c                 C  s
   |j d S )NZimaginary_unit_latex)Z	_settingsr  r6   r6   r7   r  &  s    zImaginaryUnit._latexc                   C  s   t jS r1   r  r6   r6   r6   r7   rO  )  s    zImaginaryUnit.__abs__c                 C  s   | S r1   r6   r   r6   r6   r7   r   -  s    zImaginaryUnit._eval_evalfc                 C  s   t j S r1   )r   rM  r   r6   r6   r7   r   0  s    zImaginaryUnit._eval_conjugatec                 C  s   t |trL|d }|dkr tjS |dkr.tjS |dkr<tjS |dkrLtj S t |trt|d\}}ttj|dd}|d rt	tj|ddS |S dS )	a  
        b is I = sqrt(-1)
        e is symbolic object but not equal to 0, 1

        I**r -> (-1)**(r/2) -> exp(r/2*Pi*I) -> sin(Pi*r/2) + cos(Pi*r/2)*I, r is decimal
        I**0 mod 4 -> 1
        I**1 mod 4 -> I
        I**2 mod 4 -> -1
        I**3 mod 4 -> -I
        r  r   r   r/   r  FrG  N)
r@   rk   r   r   rM  r   ro   r   rK  r$  )r   rZ   r5   r   r\   r6   r6   r7   rL  3  s     

zImaginaryUnit._eval_powerc                 C  s   t jt jfS r1   )r   r   rr  r   r6   r6   r7   as_base_expP  s    zImaginaryUnit.as_base_expc                 C  s   t djt djfS )Nr   r   )rD   ra   r   r6   r6   r7   _mpc_S  s    zImaginaryUnit._mpc_N)r   r  r  r  r  Zis_imaginaryrg   r2   r  r  r   r
  r	  r  r  rO  r   r   rL  rJ  rh  rK  r6   r6   r6   r7   rM    s$   
rM  c           	      C  s  t | } t |}| js$|js$| |kS | jr<|jr<| j|jkS | jrL||  } }| jsVdS |j\}}}}| j| j }}|r|| }|dkr|dko||kS |dkr|dkrdS | | | krdS ||> |kS ||krdS | }| d |krdS d|> |kS dS )a  Compare expressions treating plain floats as rationals.

    Examples
    ========

    >>> from sympy import S, symbols, Rational, Float
    >>> from sympy.core.numbers import equal_valued
    >>> equal_valued(1, 2)
    False
    >>> equal_valued(1, 1)
    True

    In SymPy expressions with Floats compare unequal to corresponding
    expressions with rationals:

    >>> x = symbols('x')
    >>> x**2 == x**2.0
    False

    However an individual Float compares equal to a Rational:

    >>> Rational(1, 2) == Float(0.5)
    True

    In a future version of SymPy this might change so that Rational and Float
    compare unequal. This function provides the behavior currently expected of
    ``==`` so that it could still be used if the behavior of ``==`` were to
    change in future.

    >>> equal_valued(1, 1.0) # Float vs Rational
    True
    >>> equal_valued(S(1).n(3), S(1).n(5)) # Floats of different precision
    True

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

    In future SymPy verions Float and Rational might compare unequal and floats
    with different precisions might compare unequal. In that context a function
    is needed that can check if a number is equal to 1 or 0 etc. The idea is
    that instead of testing ``if x == 1:`` if we want to accept floats like
    ``1.0`` as well then the test can be written as ``if equal_valued(x, 1):``
    or ``if equal_valued(x, 2):``. Since this function is intended to be used
    in situations where one or both operands are expected to be concrete
    numbers like 1 or 0 the function does not recurse through the args of any
    compound expression to compare any nested floats.

    References
    ==========

    .. [1] https://github.com/sympy/sympy/pull/20033
    Fr   r   N)r
   rT  ra   r   rd   re   r   )	r   r   rX   rY   r  rO   rd   re   Zneg_expr6   r6   r7   rH  [  s8    5
rH  c                 C  s   dS r  r6   r   r6   r6   r7   r    s    c                 C  s   t | j| jdS rV  )ro   rp  rq  rx   r6   r6   r7   sympify_fractions  s    rL  c                 C  s   t t| S r1   )rk   rJ   r   r6   r6   r7   sympify_mpz  s    rN  c                 C  s   t t| jt| jS r1   )ro   rJ   rp  rq  rM  r6   r6   r7   sympify_mpq  s    rO  c                 C  s   | j \}}t||dS rV  )Z_mpq_ro   )r   rd   re   r6   r6   r7   sympify_mpmath_mpq  s    
rP  c                 C  s   t | | jjS r1   )r   Z_from_mpmathr  rW   rM  r6   r6   r7   sympify_mpmath  s    rQ  c                 C  s(   t tt| j| jf\}}|tj|  S r1   )r!  mapr   realimagr   rM  )rL   rS  rT  r6   r6   r7   sympify_complex  s    rU  )rK  r  )r$  )r  c                   C  s4   t jt t jt t jt t jt d S r1   )r  r   registerRealrD   ro   r  rk   r6   r6   r6   r7   _register_classes  s    rX  )N)F)
__future__r   r  r   rn  r}   rN  regexr   	functoolsr   
containersr   r   r   r   r	   r
   r   Z	singletonr   r   basicr   r  r   r   rY  r   cacher   r   Z
decoratorsr   Zlogicr   r
  r   Zsympy.external.gmpyr   r   r   Zsympy.multipledispatchr   rc   Zmpmath.libmpZlibmpr   r   r   rU   rS   r   r   r    r!   r"   Zmpmath.ctx_mpr#   Zmpmath.libmp.libmpfr$   r)  r%   r*  r&   r(  r'   r(   rT   r)   r*   Zsympy.utilities.miscr+   r,   r-   
parametersr.   r  Z_LOG2r<   r]   r_   r`   rf   rt   compilerv   rz   r   r|   Zigcd2r   r   r   r   r   rD   r   r   Z
RealNumberro   rk   rJ   r  r  r  r   r   r   rr  r   r   r   r   r   r  rJ  Zzoor\  r  Er#  r"  r4  r=  rB  rD  rM  r!  rH  rL  ro  rN  rO  r   ZmpzZmpqrP  r   rQ  rU  complexr  rK  r  mulr$  identityr*  r  rX  Z_illegalr6   r6   r6   r7   <module>   s    $

m


( .A  .    q    9   K    D3@  < (s
M9 A?F;?Ta

