
    wgC                      d dl m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	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mZm Z m!Z!m"Z" ddl#m$Z$ ddl%m&Z& d dl'm(Z(m)Z)m*Z* d dl+m,Z, d dl-Z-d dl.m/Z0 d dl.m1Z1m2Z3 d dl4m5Z5 d dl.m6Z6m7Z7m8Z8m9Z9 d dl:m;Z; d dl<m=Z>m?Z@mAZBmCZCmDZEmFZFmGZG d dlHmIZI ddlJmKZK  ej                  d      ZMdadZNd ZOddiZPdbdZQd ZRd ZSeTj                  eVj                  d             ZXd! ZY G d" d#e      ZZ G d$ d%eZ      Z[e[xe
e\<   e
ej                  <   e[Z^ G d& d'eZ      Z_ G d( d)e_      Z`e`e
ea<    G d* d+e      Zb G d, d-e_      Zc G d. d/e`      Zd G d0 d1ede2      Ze G d3 d4ede2      Zf G d5 d6ede2      Zg G d7 d8ece2      Zh G d9 d:eZe2      Ziej                  Zj G d; d<eZe2      Zk G d= d>eZe2      Zlej                  Zm e,ele      d?        Zn G d@ dAee2      Zoej                  Zp G dB dCe      Zq G dD dEeqe2      Zrej                  Zs G dF dGeqe2      Ztej                  Zu G dH dIeqe2      Zv G dJ dKeqe2      Zw G dL dMeqe2      Zx G dN dOeqe2      Zy G dP dQee2      Zzej                  Z{dR Z|dS Z}dcdTZ~ e,eeZ      dU        ZndV Zee
ej                   <   e)?dW ZdX Zee
 e e)j                  d            <   ee
 e e)j
                  dd            <   e*?dY ZdZ Zee
 e e*j                  d            <   ee
 e e*j                  dd            <   d[ Zee
e;<   d\ Zee
e<   dd]lmZ dd^lmZ  ef       e_        dd_lmZ  ee       e_        d` Z e        ej                  ej                  ej                  ej                  fZy)d    )annotationsN   )Tuple)SympifyError_sympy_convertersympify_convert_numpy_types_sympify_is_numpy_instance)S	Singleton)Basic)Expr
AtomicExpr)pure_complex)cacheitclear_cache)
_sympifyit)
num_digitsigcdilcmmod_inverseinteger_nthroot)	fuzzy_not)
NumberKind)
SYMPY_INTSgmpyflint)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)debug)global_parameters   c                D   t        |t              r&t        | d      st        d      t        |       |k(  S | s|| }} | sy|s| |}}|dk(  rt        |      t        |      k(  S |t	        |      t	        |      }}t        d ||fD              st        d      |j                  t              }|j                  t              }|s|s||k(  S t        d      D ]U  }t        |d      }|r|r9|j                  t        t        d |D                          }t        |d      } n
||}}||}}W t        |      }	|	s9|r7|j                  t        t        d	 |D                          }t        |d      }	r(|	r&|d
   s|	d
   rt        d t        ||	      D              S dt        t        |j                  t        |d|j                                    z  }t        t!        ||z
        |z        dk  S t!        | |z
        }
t!        |       }|r|d
kD  r|
|z  |k  S |
|k  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)or_realz$when z2 is a str z1 must be a Number c              3  4   K   | ]  }|j                     y wN)	is_number.0is     W/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/core/numbers.py	<genexpr>zcomp.<locals>.<genexpr>q   s     3qq{{3   zexpecting 2 numbersr2   c              3  4   K   | ]  }|j                     y wr7   _precr9   s     r<   r=   zcomp.<locals>.<genexpr>}   s     /DA/Dr>   c              3  4   K   | ]  }|j                     y wr7   r@   r9   s     r<   r=   zcomp.<locals>.<genexpr>   s     '<A'<r>   r   c              3  :   K   | ]  \  }}t        ||        y wr7   )comp)r:   r;   js      r<   r=   zcomp.<locals>.<genexpr>   s     >$!Q41:>s   
   rA      )
isinstancestrr   
ValueErrorr   allatomsFloatrangenr.   minziprA   getattrintabs)z1z2tolabfafb_cacbdiffaz1s               r<   rD   rD   &   s   z "cB-CDD2w"}RB21"9q6SV##;1:wqzqA3QF33 !677BBbAv1X 	$!!T2CCC/D/D,D EF)!T:!#RB !1	$ aB"CCC'<'<$<=>!!T2bber!u>#b"+>>>k#aggwq'177/K"LMMCs1q5z#~&!++rBw<D
b'C	cAgCx3s{    c                l    | \  }}}}|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#   rvs           r<   mpf_normrm      sH     D#tR L J )	tSXtRs	;BIra   divideFc                D    t         d   | k7  rt                | t         d<   yy)z
    Should SymPy raise an exception on 0/0 or return a nan?

    divide == True .... raise an exception
    divide == False ... return nan
    rn   N)_errdictr   )rn   s    r<   seterrrq      s%     V## $ra   c                    t        | dt        j                  |       j                        \  }}}}ddg|dz     |z  } |dk  rd| z  }n
d}| d|z  z  } t	        |       t	        |      fS )N_mpf_r   r2   r   )rR   mpmathrf   rs   rS   )pneg_powri   rj   r\   qs         r<   _as_integer_ratiory      st    #Aw

10C0CDGS$	
B!S AaxuH	QWq63q6>ra   c                6   | j                         st        d| z        | j                         \  }}}t        |      }|dk\  rt	        t        |             }||fS d|z  }t        d t        t        |            D              }t        ||z  d| z        }||fS )z3Convert an ordinary decimal instance to a Rational.zdec must be finite, got %s.r   rt   c              3  2   K   | ]  \  }}|d |z  z    yw)rF   N )r:   r;   dis      r<   r=   z,_decimal_to_Rational_prec.<locals>.<genexpr>   s     =UQ2q5=s   rF   )
	is_finite	TypeErroras_tuplelenIntegerrS   sum	enumeratereversedRational)decsderg   rl   s         r<   _decimal_to_Rational_precr      s    ==?5;<<llnGAq!q6DAvSX
 t8O !G=i&<==ac2r6"t8Ora   
1234567890c                   | j                  d      }t        |      dkD  ryt        |      dk(  r'|\  }}|j                  t        d            r|dd }|sy| d}}|j                  d      }t        |      dkD  ryt        |      dk(  r|\  }}n|d}}|s|sy|r|d	   dv r|dd }|sd}|xs d}|||fD ]2  }|j                  d
      D ]  }|r|j	                  t
              s  y 4 y)aq  return True if s is space-trimmed number literal else False

    Python allows underscore as digit separators: there must be a
    digit on each side. So neither a leading underscore nor a
    double underscore are valid as part of a number. A number does
    not have to precede the decimal point, but there must be a
    digit before the optional "e" or "E" that begins the signs
    exponent of the number which must be an integer, perhaps with
    underscore separators.

    SymPy allows space as a separator; if the calling routine replaces
    them with underscores then the same semantics will be enforced
    for them as for underscores: there can only be 1 *between* digits.

    We don't check for error from float(s) because we don't know
    whether s is malicious or not. A regex for this could maybe
    be written but will it be understood by most who read it?
    r   r2   Fz+-r   N1.r   r\   T)splitr   
startswithtuple	translate_dig)r   partsmr   r;   frO   gs           r<   _literal_floatr      s   ( GGCLE
5zA~
5zQ1<<d$!"A#1GGCLE
5zA~	Uq1#1QQqTT\abE	SAAY  	AD)	 ra   c                      e Zd ZdZdZdZdZdZdZ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ed        Zed,d       Z ede       d        Z! ede       d        Z" ede       d        Z# ede       d        Z$d Z%d Z&d Z'd Z(d  Z)d! Z* fd"Z+d# Z,dd$d%Z-d& Z.d-d'Z/d-d(Z0d) Z1d* Z2d+ Z3 xZ4S ).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
    Tr|   rt   c                   t        |      dk(  r|d   }t        |t              r|S t        |t              rt	        |      S t        |t
              rt        |      dk(  rt        | S t        |t        t        j                  t        j                  f      rt        |      S t        |t              r|j                         }|dk(  rt        j                   S |dk(  rt        j"                  S |dk(  rt        j"                  S |dk(  rt        j$                  S t'        |      }t        |t              r|S t)        d|z        d	}t+        |t-        |      j.                  z        )
Nr   r   r2   naninf+inf-infz$String "%s" does not denote a Numberz<expected str|int|long|float|Decimal|Number object but got %r)r   rH   r   r   r   r   r   floatru   rf   decimalDecimalrM   rI   lowerr   NaNInfinityNegativeInfinityr   rJ   r   type__name__)clsobj_objvalmsgs        r<   __new__zNumber.__new__<  s   s8q=a&Cc6"Jc:&3<c5!c#h!mS>!cE6::w?@:c399;Du}uuzz!zz!)))#,C#v&
 !G#!MNNLd3i00011ra   c                ,    t        | j                        S r7   )boolis_extended_negativeselfs    r<   could_extract_minus_signzNumber.could_extract_minus_signZ  s    D--..ra   c                Z    ddl m} t        |dd      rt        | |      S  || |g|i |S )Nr   )invertr8   T)sympy.polys.polytoolsr   rR   r   )r   othergensargsr   s        r<   r   zNumber.invert]  s5    05+t,tU++dE1D1D11ra   c                   ddl m} 	 t        |      }| j                  st        j
                  | |fv r t        j
                  t        j
                  fS 	 |st        d      | j                  r2|j                  r&t        t        | j                  |j                         S t        |t              r| t        |      z  }n| |z  }|j                   rg|dk\  rt#        |      nt#        |      dz
  }| ||z  z
  }|t        |      k(  r|dz  }d}t        | t              st        |t              r)t        |      }n| r ||        ||      k(  rdnd}|r|n| }t        ||      S # t        $ r	 t        cY S w xY w)Nr   )rh   zmodulo by zeror   rt   )$sympy.functions.elementary.complexesrh   r   is_infiniter   r   r   NotImplementedZeroDivisionError
is_Integerr   divmodrv   rH   rM   r   r~   rS   )r   r   rh   ratwrs         r<   
__divmod__zNumber.__divmod__c  sD   =	"5ME155T5M#9quu~% $: #$455??u//&122u%x&Cu*C??1HC#c(Q,AuQwAE%L Q$&*UE*B!H$t*U";"AAQ{+  	"!!	"s   A
E E10E1c                \    	 t        |      }t        ||       S # t        $ r	 t        cY S w xY wr7   )r   r   r   r   r   r   s     r<   __rdivmod__zNumber.__rdivmod__  s6    	"5ME eT""  	"!!	"s    ++c                F    t        d| j                  j                  z        )z7Evaluation of mpf tuple accurate to at least prec bits.z%s needs ._as_mpf_val() methodNotImplementedError	__class__r   r   rg   s     r<   _as_mpf_valzNumber._as_mpf_val  s$    !"B^^$$#& ' 	'ra   c                L    t         j                  | j                  |      |      S r7   rM   _newr   r   s     r<   _eval_evalfzNumber._eval_evalf      zz$**40$77ra   c                T    t        || j                        }| j                  |      |fS r7   )maxrA   r   r   s     r<   
_as_mpf_opzNumber._as_mpf_op  s(    4$%t++ra   c                J    t        j                  | j                  d            S )N5   )mlibto_floatr   r   s    r<   	__float__zNumber.__float__  s    }}T--b122ra   c                F    t        d| j                  j                  z        )Nz%s needs .floor() methodr   r   s    r<   floorzNumber.floor  s$    !"<^^$$#& ' 	'ra   c                F    t        d| j                  j                  z        )Nz%s needs .ceiling() methodr   r   s    r<   ceilingzNumber.ceiling  s$    !">^^$$#& ' 	'ra   c                "    | j                         S r7   r   r   s    r<   	__floor__zNumber.__floor__      zz|ra   c                "    | j                         S r7   r   r   s    r<   __ceil__zNumber.__ceil__      ||~ra   c                    | S r7   r|   r   s    r<   _eval_conjugatezNumber._eval_conjugate      ra   c                <    ddl m}  |t        j                  g| S )Nr   )Order)sympy.series.orderr   r   One)r   symbolsr   s      r<   _eval_orderzNumber._eval_order  s    ,QUU%W%%ra   c                    ||  k(  r| S | S r7   r|   r   oldnews      r<   
_eval_subszNumber._eval_subs  s    4%<4Kra   c                     y)N)r   r   r   r|   r   s    r<   	class_keyzNumber.class_key  s    ra   c                *    | j                         dd| fS )N)r   r|   r|   )r   )r   orders     r<   sort_keyzNumber.sort_key  s    ~~"d22ra   r   c                :   t        |t              rvt        j                  rf|t        j
                  u rt        j
                  S |t        j                  u rt        j                  S |t        j                  u rt        j                  S t        j                  | |      S r7   )
rH   r   r1   evaluater   r   r   r   r   __add__r   s     r<   r   zNumber.__add__  sl    eV$):)C)C~uu!**$zz!!,,,)))!!$..ra   c                :   t        |t              rvt        j                  rf|t        j
                  u rt        j
                  S |t        j                  u rt        j                  S |t        j                  u rt        j                  S t        j                  | |      S r7   )
rH   r   r1   r   r   r   r   r   r   __sub__r   s     r<   r  zNumber.__sub__  sl    eV$):)C)C~uu!**$)))!,,,zz!!!$..ra   c                F   t        |t              rt        j                  r|t        j
                  u rt        j
                  S |t        j                  u rH| j                  rt        j
                  S | j                  rt        j                  S t        j                  S |t        j                  u r^| j                  rt        j
                  S | j                  rt        j                  S t        j                  S t        |t              rt        S t        j                  | |      S r7   )rH   r   r1   r   r   r   r   is_zerois_positiver   r   r   r   __mul__r   s     r<   r  zNumber.__mul__  s    eV$):)C)C~uu!**$<<55L%%::%---!,,,<<55L%%---::%u%!!!!$..ra   c                   t        |t              rdt        j                  rT|t        j
                  u rt        j
                  S |t        j                  t        j                  fv rt        j                  S t        j                  | |      S r7   )rH   r   r1   r   r   r   r   r   Zeror   __truediv__r   s     r<   r  zNumber.__truediv__  s\    eV$):)C)C~uu1::q'9'9::vv%%dE22ra   c                F    t        d| j                  j                  z        )Nz%s needs .__eq__() methodr   r   s     r<   __eq__zNumber.__eq__  $    !"=^^$$#& ' 	'ra   c                F    t        d| j                  j                  z        )Nz%s needs .__ne__() methodr   r   s     r<   __ne__zNumber.__ne__  r  ra   c                    	 t        |      }t        d| j                  j
                  z        # t        $ r t        d| d|      w xY w)NInvalid comparison z < z%s needs .__lt__() methodr
   r   r   r   r   r   r   s     r<   __lt__zNumber.__lt__  sX    	JUOE ""=^^$$#& ' 	'  	JD%HII	J	   / A
c                    	 t        |      }t        d| j                  j
                  z        # t        $ r t        d| d|      w xY w)Nr  z <= z%s needs .__le__() methodr  r   s     r<   __le__zNumber.__le__   sX    	KUOE ""=^^$$#& ' 	'  	KT5IJJ	Kr  c                    	 t        |      }t        |      j                  |       S # t        $ r t        d| d|      w xY w)Nr  z > )r
   r   r   r  r   s     r<   __gt__zNumber.__gt__  sK    	JUOE %%d++  	JD%HII	J	   ' Ac                    	 t        |      }t        |      j                  |       S # t        $ r t        d| d|      w xY w)Nr  z >= )r
   r   r   r  r   s     r<   __ge__zNumber.__ge__  sK    	KUOE %%d++  	KT5IJJ	Kr  c                     t         |          S r7   super__hash__r   r   s    r<   r  zNumber.__hash__      w!!ra   c                     y)NTr|   )r   wrtflagss      r<   is_constantzNumber.is_constant      ra   )rationalc                   | j                   s|s| dfS | j                  rt        j                  |  ffS t        j                  | ffS Nr|   )is_Rationalis_negativer   NegativeOner   )r   r%  depskwargss       r<   as_coeff_mulzNumber.as_coeff_mul  sB    88O==D5(**uutg~ra   c                H    | j                   r| dfS t        j                  | ffS r'  )r(  r   r  )r   r+  s     r<   as_coeff_addzNumber.as_coeff_add$  s$    8Ovvwra   c                N    |s| t         j                  fS t         j                  | fS z1Efficiently extract the coefficient of a product.r   r   r   r%  s     r<   as_coeff_MulzNumber.as_coeff_Mul*  s!    ;uud{ra   c                N    |s| t         j                  fS t         j                  | fS z3Efficiently extract the coefficient of a summation.r   r  r3  s     r<   as_coeff_AddzNumber.as_coeff_Add0  s!    <vvt|ra   c                     ddl m}  || |      S )z#Compute GCD of `self` and `other`. r   )gcd)r   r:  )r   r   r:  s      r<   r:  z
Number.gcd6      -4ra   c                     ddl m}  || |      S )z#Compute LCM of `self` and `other`. r   )lcm)r   r=  )r   r   r=  s      r<   r=  z
Number.lcm;  r;  ra   c                     ddl m}  || |      S )z1Compute GCD and cofactors of `self` and `other`. r   )	cofactors)r   r?  )r   r   r?  s      r<   r?  zNumber.cofactors@  s    3u%%ra   r7   F)5r   
__module____qualname____doc__is_commutativer8   	is_Number	__slots__rA   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/  r4  r8  r:  r=  r?  __classcell__r   s   @r<   r   r     s^   6 NIII ED2</2<#'
8,3''&

   3 3 (/ )/ (/ )/ (/ )/, (3 )3'''',," ,0  
 
&ra   r   c                     e Zd ZU dZdZded<   dZdZdZdZ	dZ
dZej                  ej                  d            Zd1dZed2d	       Zd
 Zd Zd Zd Zd Zd Zed        Zd Zd Zd Zd Zd Z d Z!d Z"d Z#d Z$d Z%d Z&d Z' e(de)      d        Z* e(de)      d        Z+ e(de)      d         Z, e(de)      d!        Z- e(de)      d"        Z. e(de)      d#        Z/d$ Z0d% Z1d& Z2d' Z3d( Z4d) Z5d* Z6d+ Z7d, Z8d- Z9d. Z:d3d/Z;d0 Z<y)4rM   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.
    rs   rA   ztuple[int, int, int, int]rs   NTz-+_.c           	        ||t        d      t        |t              r|j                         x}}|j	                  dd      j                         }|j                  d      rt        |      dkD  rd|z   }n|j                  d      rt        |      dkD  r
d	|dd  z   }n|d
v rt        j                  S |dk(  rt        j                  S |dk(  rt        j                  S t        |      smt        d|z        t        |t              r	|dk(  rd}nFt        |t              r|t        d      k(  rt        j                  S t        |t              r|t        d      k(  rt        j                  S t        |t              r%t        j                  |      rt        j                  S t        |t         t"        f      rt        |      }n|t        j                  u r|S |t        j                  u r|S |t        j                  u r|S t%        |      rt'        |      }n@t        |t(        j*                        r&|||j,                  j.                  }|j0                  }||d}t        |t2              r|S t        |t              r	 t5        j6                  |      }d|v}t9        |      \  }}|j:                  r|rt=        |t?        |            }t=        d|      }tA        |      }n|dk(  r||w|dk(  rrt        |t              st        d      	 t5        j6                  |      }d|v}t9        |      \  }}|j:                  r#|r!t=        |t?        |            }tA        |      }	 ||dk(  rtA        |      }tE        |      }t        |t              rtG        jH                  ||tJ              }nUt        |t              rtG        jL                  ||tJ              }n(t        |t4        j6                        r|jO                         r&tG        jL                  t        |      |tJ              }n|jQ                         rt        j                  S |jS                         r%|dkD  rt        j                  S t        j                  S t        dt        |      z        t        |tT              rt        |      dv rt        |d   t              rgtW        |      }|d   jY                  d      r|d   d d |d<   |d   j                  d      r|d   dd  |d<   t[        |d   d      |d<   tU        |      }nt        |      dk(  rt2        j]                  ||      S t_        |d   dv |d   dk\  t_        d |D              f      st        d|      t2        j]                  |d   |d   |d   ta        |d         f|      S t        |tb        td        f      r|jg                  |      }n!t)        j*                  ||      j0                  }| j]                  ||d       S # t4        jB                  $ r Y w xY w# t4        jB                  $ r t        d|z        w xY w)!Nz=Both decimal and binary precision supplied. Supply only one.  r\   r   r   0z-.r2   z-0.)r   r   r   r   zstring-float not recognized: %sr   r      r5   z^The null string can only be used when the number to Float is passed as a string or an integer.z*string-float not recognized by Decimal: %szunexpected decimal value %s)      Lrt   0x   rR  )r   r   c              3  J   K   | ]  }t        |      t        t        fv   y wr7   )r   rS   r9   s     r<   r=   z Float.__new__.<locals>.<genexpr>  s     C!QC: 5Cs   !#zmalformed mpf: )rg   F)zero)4rJ   rH   rI   stripreplacer   r   r   r   r   r   r   r   r   mathisnanr   r   r   r	   ru   rf   contextrg   rs   rM   r   r   r   r   r   r   r/   InvalidOperationrS   r   
from_floatre   from_strr~   is_nanr   r   listendswithr#   r   rK   r    r   NumberSymbolr   )r   numdps	precision_numNumisintrs   s           r<   r   zFloat.__new__   sz   ?y4 1 2 2 c3$D3++c3'--/C~~c"s3x!|Ci%#c(Q,c!"go'zz!)))uu#C( !BT!IJJU#qCU#uU|(;::U#uV}(<%%%U#

355Lj'23c(CAJJJA&&&JAEE\J$&s+CVZZ( ; # 0 0I))C;9,C#u%
#s#1!//#.C  sNE8=HC~~% "#z#7b#,C +C 0I"_	0AcRic3'  "K L L
1ooc* 34S9S>>ec:c?3C +C 0I 	R#C(I	N	c5!OOCC8ES!MM#y#6EW__-}}c#h	3?uu"7::%))) !>S!IJJU#CF(:#a&#&3i q6??3' VCR[CF q6$$T* VABZCFSVRAc
s8q= ::c955Ff,FaKCsCC  
 ))EFF !::QQQ#a&1AB!# # fl34OOI.EJJs399Exxyux55y // " ++ U !MPS!STTUs   X X0 X-,X-0"Yc                ,   |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        j                  |       }t        ||      |_        ||_        |S r7   )r,   r   r  _mpf_nanr   _mpf_infr   	_mpf_ninfr   r   r   rm   rs   rA   )r   rs   rA   rW  r   s        r<   r   z
Float._new  su     EUN66Mh55Lh::i%%%ll3UE*		
ra   c                n    | j                   \  }}}}|t        |      dd  ||f}d| j                  i}|f|fS )Nr2   rf  )rs   hexrA   )r   rh   ri   exprk   argr,  s          r<   __getnewargs_ex__zFloat.__getnewargs_ex__  sG    !ZZc3SXab\3+tzz*ra   c                2    | j                   | j                  fS r7   rL  r   s    r<   _hashable_contentzFloat._hashable_content  s    

DJJ''ra   c           
         t        t        t        j                  t        j                  | j
                  | j                                          S r7   )r   rS   r   to_int	mpf_floorrs   rA   r   s    r<   r   zFloat.floor  s7    s4;;NN4::tzz24 5 6 	6ra   c           
         t        t        t        j                  t        j                  | j
                  | j                                          S r7   )r   rS   r   rv  mpf_ceilrs   rA   r   s    r<   r   zFloat.ceiling  s7    s4;;MM$**djj13 4 5 	5ra   c                "    | j                         S r7   r   r   s    r<   r   zFloat.__floor__  r   ra   c                "    | j                         S r7   r   r   s    r<   r   zFloat.__ceil__  r   ra   c                @    t        j                  | j                        S r7   )ru   rf   rs   r   s    r<   rd  z	Float.num  s    zz$**%%ra   c                    t        | j                  |      }|| j                  k7  r%| j                  |k(  rt        | j                  |       |S r7   )rm   rs   rA   r0   r   rg   rl   s      r<   r   zFloat._as_mpf_val  s<    djj$'

d 2$**b!	ra   c                F    | j                   t        || j                        fS r7   )rs   r   rA   r   s     r<   r   zFloat._as_mpf_op  s    zz3tTZZ000ra   c                6    | j                   t        t        fv ryyNFTrs   rl  rm  r   s    r<   _eval_is_finitezFloat._eval_is_finite  s    ::(I..ra   c                6    | j                   t        t        fv ryyNTFr  r   s    r<   _eval_is_infinitezFloat._eval_is_infinite  s    ::(I..ra   c                D    | j                   t        k(  ryt        |       syy r  )rs   r,   
int_valuedr   s    r<   _eval_is_integerzFloat._eval_is_integer  s!    ::$  ra   c                R    | j                   t        t        fv ry| j                  dk  S NFr   rs   rm  rl  rd  r   s    r<   _eval_is_negativezFloat._eval_is_negative  $    ::)X..xx!|ra   c                R    | j                   t        t        fv ry| j                  dkD  S r  r  r   s    r<   _eval_is_positivezFloat._eval_is_positive  r  ra   c                p    | j                   t        k(  ry| j                   t        k(  ry| j                  dk  S NTFr   r  r   s    r<   _eval_is_extended_negativez Float._eval_is_extended_negative  s.    ::"::!xx!|ra   c                p    | j                   t        k(  ry| j                   t        k(  ry| j                  dkD  S r  )rs   rl  rm  rd  r   s    r<   _eval_is_extended_positivez Float._eval_is_extended_positive  s.    ::!::"xx!|ra   c                (    | j                   t        k(  S r7   rs   r,   r   s    r<   _eval_is_zerozFloat._eval_is_zero      zzU""ra   c                (    | j                   t        k7  S r7   r  r   s    r<   __bool__zFloat.__bool__  r  ra   c                    | s| S t         j                  t        j                  | j                        | j
                        S r7   )rM   r   r   mpf_negrs   rA   r   s    r<   __neg__zFloat.__neg__  s,    Kzz$,,tzz2DJJ??ra   r   c                   t        |t              rht        j                  rX|j	                  | j
                        \  }}t        j                  t        j                  | j                  ||t              |      S t        j                  | |      S r7   )rH   r   r1   r   r   rA   rM   r   r   mpf_addrs   re   r   r   r   rhsrg   s       r<   r   zFloat.__add__  b    eV$):)C)C((4IC::dll4::sD#FMM~~dE**ra   c                   t        |t              rht        j                  rX|j	                  | j
                        \  }}t        j                  t        j                  | j                  ||t              |      S t        j                  | |      S r7   )rH   r   r1   r   r   rA   rM   r   r   mpf_subrs   re   r  r  s       r<   r  zFloat.__sub__   r  ra   c                   t        |t              rht        j                  rX|j	                  | j
                        \  }}t        j                  t        j                  | j                  ||t              |      S t        j                  | |      S r7   )rH   r   r1   r   r   rA   rM   r   r   mpf_mulrs   re   r  r  s       r<   r  zFloat.__mul__  r  ra   c                (   t        |t              rm|dk7  rht        j                  rX|j	                  | j
                        \  }}t        j                  t        j                  | j                  ||t              |      S t        j                  | |      S Nr   )rH   r   r1   r   r   rA   rM   r   r   mpf_divrs   re   r  r  s       r<   r  zFloat.__truediv__  sj    eV$!8I8R8R((4IC::dll4::sD#FMM!!$..ra   c                   t        |t              rS|j                  dk7  rDt        j                  r4t        t        j                  t        |       |      | j                        S t        |t
              rKt        j                  r;| |z  }t        |      r+t        dt        | j                  |j                              S t        |t              rht        j                  rX|j                  | j                        \  }}t
        j                  t        j                  | j                  ||t               |      S t        j                  | |      S )Nr   rf  r   )rH   r   rx   r1   r   rM   __mod__rA   r  r   r   r   r   r   mpf_modrs   re   )r   r   r   r  rg   s        r<   r  zFloat.__mod__  s    eX&577a<<M<V<V))(4.%@#'::/ /eU#(9(B(BU
A!}Q#djj%++*FGGeV$):)C)C((4IC::dll4::sD#FMM~~dE**ra   c                   t        |t              r!t        j                  r|j	                  |       S t        |t
              rht        j                  rX|j                  | j                        \  }}t        j                  t        j                  || j                  |t              |      S t
        j                  | |      S r7   )rH   rM   r1   r   r  r   r   rA   r   r   r  rs   re   __rmod__r  s       r<   r  zFloat.__rmod__$  s    eU#(9(B(B==&&eV$):)C)C((4IC::dll3

D#FMMtU++ra   c                   t        | d      r*|j                  r| S |j                  rt        j                  S t        |t              rt        |t              rP| j                  }t        j                  t        j                  | j                  |j                  |t              |      S t        |t               rY|j                  dk(  rJ|j"                  dz  r;| j$                  r/t'        t        j(                  |d      |  j+                  |      z  S |j-                  | j                        \  }}| j                  }	 t/        |||t              }t        j                  ||      S y# t        j0                  $ rl t        j2                  |t4        f|t4        f|t              \  }}t        j                  ||      t        j                  ||      t        j6                  z  z   cY S w xY w)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   r2   Fr   N)equal_valuedis_extended_positiver   r   ComplexInfinityrH   r   r   rA   rM   r   r   mpf_pow_intrs   rv   re   r   rx   r)  Powr*  _eval_powerr   r$   ComplexResultmpc_powr,   ImaginaryUnit)r   rj   rg   mpfselfyreims          r<   r  zFloat._eval_power-  s    a (((((((dF#$(zzzz$$TZZsCTK KD(+FFaKDFFQJ43C3C1==$?E;;t,- -4JD$jjG9GT45zz!T** $ %% 9e$tUmT3@Bzz"d+JJr4(89 99s   <'E% %A<G$#G$c                |    t         j                  t        j                  | j                        | j
                        S r7   )rM   r   r   mpf_absrs   rA   r   s    r<   __abs__zFloat.__abs__M  s$    zz$,,tzz2DJJ??ra   c                z    | j                   t        k(  ryt        t        j                  | j                               S r  )rs   r,   rS   r   rv  r   s    r<   __int__zFloat.__int__P  s)    ::4;;tzz*++ra   c                d    t        |t              rt        |      }t        j                  | |      S r7   )rH   r   rM   r   r
  r   s     r<   r
  zFloat.__eq__U  s&    eU#%LE||D%((ra   c                >    | j                  |      }|t        u r|S | S r7   )r
  r   )r   r   eqs      r<   r  zFloat.__ne__Z  s$    [[I6Mra   c                    t        |       }t        j                  |      st        |      S t	        j
                  |       S r7   )r   rZ  isinfhashr   r  )r   	float_vals     r<   r  zFloat.__hash__a  s0    $K	zz)$	?"~~d##ra   c           
         	 t        |      }|j                  rx	 t	        j
                  | j                  t	        j                  |j                              }t	        j                  |j                        }t        t         |||                  S |j                  r/t        t         || j                  |j                                    S |j                  r|t        j                  t        j                  fvr|j!                  t#        | j$                              }|j$                  dkD  rK|j&                  r>t        t         || j                  |j)                  | j$                                          S y y y y # t        $ r	 t        cY S w xY wNr   )r
   r   r   r(  r   r  rs   from_intrx   rv   r   is_Floatis_comparabler   r   r   evalfr.   rA   rE  r   )r   r   opsmpfompfs        r<   _FrelzFloat._Frelg  sB   	"UOE  <<

DMM%'',BCD==)DDD$011^^D4::u{{35 6 6  U

A..30 &0KKDJJ 78E{{Q??#D4::u'8'8'DE%G H H # &0 %  	"!!	"s   E; ;FFc                    t        |t              r|j                  |       S | j                  |t        j
                        }|t        j                  | |      S |S r7   )rH   rc  r  r  r   mpf_gtr   r  r   r   rl   s      r<   r  zFloat.__gt__  J    e\*<<%%ZZt{{+:;;tU++	ra   c                    t        |t              r|j                  |       S | j                  |t        j
                        }|t        j                  | |      S |S r7   )rH   rc  r  r  r   mpf_ger   r  r  s      r<   r  zFloat.__ge__  r  ra   c                    t        |t              r|j                  |       S | j                  |t        j
                        }|t        j                  | |      S |S r7   )rH   rc  r  r  r   mpf_ltr   r  r  s      r<   r  zFloat.__lt__  r  ra   c                    t        |t              r|j                  |       S | j                  |t        j
                        }|t        j                  | |      S |S r7   )rH   rc  r  r  r   mpf_ler   r  r  s      r<   r  zFloat.__le__  r  ra   c                6    t        | |z
        t        |      k  S r7   )rT   rM   )r   r   epsilons      r<   
epsilon_eqzFloat.epsilon_eq  s    4%< 5>11ra   c                R    t        t        j                  t        |             |      S r7   )formatr   r   rI   )r   format_specs     r<   
__format__zFloat.__format__  s    gooc$i0+>>ra   NNT)z1e-15)=r   rA  rB  rC  rF  __annotations__is_rationalis_irrationalr8   is_realis_extended_realr  rI   	maketransdictfromkeys_remove_non_digitsr   rH  r   rr  rt  r   r   r   r   propertyrd  r   r   r  r  r  r  r  r  r  r  r  r  r   r   r   r  r  r  r  r  r  r  r  r
  r  r  r  r  r  r  r  r  r  r|   ra   r<   rM   rM   F  s   dJ #I$$ KMIGHt}}V'<=O6b    (65 & &1



##@
 (+ )+ (+ )+ (+ )+ (/ )/ (+ )+ (, ),9@@,
)
$H:2?ra   rM   c                  f    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d2d       Zd3d	Zd
 Zd Zd Zd Zd Z ede      d        ZeZ ede      d        Z ede      d        Z ede      d        ZeZ ede      d        Z ede      d        Z ede      d        Z ede      d        Zd Zd Z d Z!d Z"d Z#d Z$d Z%d Z&d  Z'd! Z(d" Z)d# Z*d$ Z+d% Z,d& Z-d' Z. fd(Z/	 	 d4d)Z0e1d*        Z2e1d+        Z3 ede      d,        Z4 ede      d-        Z5d. Z6d5d/Z7d6d0Z8d6d1Z9 xZ:S )7r   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rv   rx   rS   rv   rx   c                ~   |It        |t              r|S t        |t              rn!t        |t        t        f      rt        t        |       S t        |t              s	 t        |      }n|j                  d      dkD  rt        d|z        |j                  dd      }|j                  dd      }t        |      dk(  r4|\  }}t        j                   |      }t        j                   |      }||z  }	 t        j                   |      }t        |j"                  |j$                  d      S t        |t              st        d|z        d}d}d}t        |t              s't        |      }||j(                  z  }|j*                  }nt-        |      }t        |t              s*t        |      }||j(                  z  }||j*                  z  }n|t-        |      z  }|}|dk(  r9|dk(  r$t.        d   rt'        d	      t0        j2                  S t0        j4                  S |dk  r| }| }|st7        t9        |      |      }|dkD  r
||z  }||z  }|dk(  rt;        |      S |dk(  r|dk(  rt0        j<                  S t?        j@                  |       }||_        ||_        |S # t        t        f$ r Y w xY w# t&        $ r Y w xY w)
N/r   zinvalid input: %srN  r5   r2   r   rn   zIndeterminate 0/0)!rH   r   r   r   rM   ry   rI   r   r   SyntaxErrorcountr   rY  rsplitr   	fractionsFraction	numeratordenominatorrJ   rx   rv   rS   rp   r   r   r  r   rT   r   Halfr   r   )	r   rv   rx   r:  pqfpfqQr   s	            r<   r   zRational.__new__  s   9!X&!Z(a%0#%6q%9::!!S)#AJ wws|a''(;a(?@@		#r*A#q)B2w!|!1&//2&//2rEG%..q1  (Q]]AFF!!X.#$7!$;<<AC!Z(AHAAAA!Z(AHAHAQKA 6AvH%$%89955L$$$q5AAs1vq/C7#IA#IA61:6a1f66Mll3
A )+6  & s$    J 5J/ J,+J,/	J<;J<c           	         t        j                  | j                  | j                        }t	        |j                  t        j                  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

        )r  r  rv   rx   r   limit_denominatorrS   )r   max_denominatorr   s      r<   r  zRational.limit_denominatorl  sD     tvvtvv.++I,>,>s??S,TUVVra   c                2    | j                   | j                  fS r7   r  r   s    r<   __getnewargs__zRational.__getnewargs__|      ra   c                2    | j                   | j                  fS r7   r  r   s    r<   rt  zRational._hashable_content  r  ra   c                     | j                   dkD  S r  rv   r   s    r<   r  zRational._eval_is_positive  s    vvzra   c                     | j                   dk(  S r  r  r   s    r<   r  zRational._eval_is_zero  s    vv{ra   c                D    t        | j                   | j                        S r7   )r   rv   rx   r   s    r<   r  zRational.__neg__  s    ((ra   r   c                   t         j                  rt        |t              r;t	        | j
                  | j                  |j
                  z  z   | j                  d      S t        |t              rTt	        | j
                  |j                  z  | j                  |j
                  z  z   | j                  |j                  z        S t        |t              r|| z   S t        j                  | |      S t        j                  | |      S r  )
r1   r   rH   r   r   rv   rx   rM   r   r   r   s     r<   r   zRational.__add__  s    %%%) 7CCE8,uww ?PPE5)t|#~~dE22~~dE**ra   c                   t         j                  rt        |t              r;t	        | j
                  | j                  |j
                  z  z
  | j                  d      S t        |t              rTt	        | j
                  |j                  z  | j                  |j
                  z  z
  | j                  |j                  z        S t        |t              r| | z   S t        j                  | |      S t        j                  | |      S r  )
r1   r   rH   r   r   rv   rx   rM   r   r  r   s     r<   r  zRational.__sub__  s    %%%) 7CCE8,uww ?PPE5)v}$~~dE22~~dE**ra   c                   t         j                  rt        |t              r;t	        | j
                  |j                  z  | j                  z
  | j
                  d      S t        |t              rTt	        | j
                  |j                  z  | j                  |j
                  z  z
  | j
                  |j
                  z        S t        |t              r|  |z   S t        j                  | |      S t        j                  | |      S r  )
r1   r   rH   r   r   rx   rv   rM   r   __rsub__r   s     r<   r  zRational.__rsub__  s    %%%)uww 7CCE8,uww ?PPE5)uu}$tU33tU++ra   c           	     r   t         j                  rt        |t              rLt	        | j
                  |j
                  z  | j                  t        |j
                  | j                              S t        |t              rzt	        | j
                  |j
                  z  | j                  |j                  z  t        | j
                  |j                        t        | j                  |j
                        z        S t        |t              r|| z  S t        j                  | |      S t        j                  | |      S r7   )r1   r   rH   r   r   rv   rx   r   rM   r   r  r   s     r<   r  zRational.__mul__  s    %%%)uwwUWWdff8MNNE8,uwwuwwTVVUWW@UVZ[_[a[achcjcjVk@kllE5)Tz!~~dE22~~dE**ra   c           	        t         j                  rMt        |t              r| j                  r-|j                  t
        j                  k(  rt
        j                  S t        | j                  | j                  |j                  z  t        | j                  |j                              S t        |t              rzt        | j                  |j                  z  | j                  |j                  z  t        | j                  |j                        t        | j                  |j                        z        S t        |t              r| d|z  z  S t        j                  | |      S t        j                  | |      S r  )r1   r   rH   r   rv   r   r  r  r   rx   r   rM   r   r  r   s     r<   r  zRational.__truediv__  s    %%%)66egg/,,,#DFFDFF577ND<QRRE8,uwwuwwTVVUWW@UVZ[_[a[achcjcjVk@kllE5)QuW~%))$66!!$..ra   c           	     x   t         j                  rt        |t              rLt	        |j
                  | j                  z  | j
                  t        | j
                  |j
                              S t        |t              rzt	        |j
                  | j                  z  |j                  | j
                  z  t        | j
                  |j
                        t        | j                  |j                        z        S t        |t              r|d| z  z  S t        j                  | |      S t        j                  | |      S r  )r1   r   rH   r   r   rv   rx   r   rM   r   __rtruediv__r   s     r<   r  zRational.__rtruediv__  s    %%%)TVVUWW8MNNE8,TVVUWW@UVZ[_[a[achcjcjVk@kllE5)af~%**477""4//ra   c                ,   t         j                  rt        |t              r| j                  |j
                  z  |j                  | j
                  z  z  }t        | j                  |j
                  z  ||j                  z  | j
                  z  z
  | j
                  |j
                  z        S t        |t              r/t        | j                  t        |            |j                        S t        j                  | |      S t        j                  | |      S )Nr  )
r1   r   rH   r   rv   rx   rM   r  rA   r   )r   r   rO   s      r<   r  zRational.__mod__  s    %%%*VVEGG^8uww5774661A A466%''>RR%'T\\(5/:',{{4 4>>$..~~dE**ra   c                z    t        |t              rt        j                  ||       S t        j	                  | |      S r7   )rH   r   r  r   r  r   s     r<   r  zRational.__rmod__  s0    eX&##E400tU++ra   c                "   t        |t              r`t        |t              r| j                  |j                        |z  S |j
                  r| }|t        j                  u r t        | j                  | j                        S | j                  r8t        j                  |z  t        | j                  | j                         |z  z  S t        | j                  | j                        |z  S |t        j                  u r| j                  | j                  kD  rt        j                  S | j                  | j                   k  r2t        j                  t        j                  t        j                  z  z   S t        j                  S t        |t               r;t        | j                  |j                  z  | j                  |j                  z  d      S t        |t              r|j                  |j                  z  }|r|dz  }||j                  z  |j                  z
  }t        ||j                        }| j                  dk7  rLt!        | j                        |z  t!        | j                        |z  z  t        d| j                  |z  d      z  S t!        | j                        |z  t        d| j                  |z  d      z  S |j                  |j                  z
  }t        ||j                        }| j                  dk7  rIt!        | j                        |z  t!        | j                        |z  z  t        d| j                  d      z  S t!        | j                        |z  t        d| j                  d      z  S | j
                  r|j"                  r|  |z  S y r  )rH   r   rM   r   rA   r   r   r   r   rx   rv   r)  r*  r   r  r  r   is_even)r   rj   neintpartremfracpartratfracparts         r<   r  zRational._eval_power  s   dF#$&''

3T99((U!%%K#DFFDFF33##==$.x/H"/LLL#DFFDFF3R77qzz!66DFF?::%66TVVG#::

1??(BBBvv$(BB$)&&DFF*qLG")$&&.466"9K"*;"?Kvv{&tvv4WTVV_k5QQRZ[\^b^d^dfm^mopRqqq"466?K7DFFGOUV8WWW"&&&466/K"*;"?Kvv{&tvv4WTVV_k5QQRZ[\^b^d^dfgRhhh"466?K7DFFA8NNN$$ED= ra   c                b    t        j                  | j                  | j                  |t              S r7   )r   from_rationalrv   rx   re   r   s     r<   r   zRational._as_mpf_val  s!    !!$&&$&&$<<ra   c                    t        j                  t        j                  | j                  | j
                  ||            S r7   )ru   make_mpfr   r  rv   rx   r   rg   re   s      r<   _mpmath_zRational._mpmath_  s*    t11$&&$&&$LMMra   c                T    t        t        | j                        | j                        S r7   )r   rT   rv   rx   r   s    r<   r  zRational.__abs__"  s    DFFTVV,,ra   c                x    | j                   | j                  }}|dk  rt        | |z         S t        ||z        S r  )rv   rx   rS   )r   rv   rx   s      r<   r  zRational.__int__%  s:    vvtvv1q5AJ;1a4yra   c                F    t        | j                  | j                  z        S r7   r   rv   rx   r   s    r<   r   zRational.floor+  s    tvv'((ra   c                J    t        | j                   | j                  z         S r7   r'  r   s    r<   r   zRational.ceiling.  s    466)***ra   c                "    | j                         S r7   r   r   s    r<   r   zRational.__floor__1  r   ra   c                "    | j                         S r7   r   r   s    r<   r   zRational.__ceil__4  r   ra   c                F   	 t        |      }t        |t              sy| s| S |j
                  r|j                  ry|j                  |       S |j                  r4| j                  |j                  k(  xr | j                  |j                  k(  S y# t        $ r	 t        cY S w xY wNF)r
   r   r   rH   r   is_NumberSymbolr  r
  r(  rv   rx   r   s     r<   r
  zRational.__eq__7  s    	"UOE %( 9  ""<<%% 66UWW$:577)::!  	"!!	"s   B B B c                    | |k(   S r7   r|   r   s     r<   r  zRational.__ne__L      5=  ra   c                   	 t        |      }|j                  rd }| |}}|j                  rt        ||      }nu|j                  rt        ||      }n\|j                  rPt        |j                  |j                  z        t        |j                  |j                  z        }}t        ||      }|r ||      S |j                  r1|j                  r$t        |j                        |j                  |z  fS y y y # t        $ r	 t        cY S w xY wr7   )r
   r   r   rE  r-  rR   r  r(  r   rv   rx   r8   r  )r   r   attrr  r   os         r<   _RrelzRational._RrelO  s    	"UOE ??BqA$$Q%Q%""qss133w'QSS)91Q%!u{{q11qss|QSSU**  2{   	"!!	"s   C6 6DDc                |    | j                  |d      }|| |f}nt        |t              s|S t        j                  | S )Nr  )r3  rH   r   r   r  r  s      r<   r  zRational.__gt__d  >    ZZx(:uBB&I{{Bra   c                |    | j                  |d      }|| |f}nt        |t              s|S t        j                  | S )Nr  )r3  rH   r   r   r  r  s      r<   r  zRational.__ge__l  r5  ra   c                |    | j                  |d      }|| |f}nt        |t              s|S t        j                  | S )Nr  )r3  rH   r   r   r  r  s      r<   r  zRational.__lt__t  r5  ra   c                |    | j                  |d      }|| |f}nt        |t              s|S t        j                  | S )Nr  )r3  rH   r   r   r  r  s      r<   r  zRational.__le__|  r5  ra   c                     t         |          S r7   r  r  s    r<   r  zRational.__hash__  r  ra   c                F    ddl m}  || |||||      j                         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;  s           r<   factorszRational.factors  s)     	4Ui%w%''+tv	.ra   c                    | j                   S r7   r  r   s    r<   r  zRational.numerator      vvra   c                    | j                   S r7   )rx   r   s    r<   r  zRational.denominator  rF  ra   c                   t        |t              r]|t        j                  k(  r|S t        t	        | j
                  |j
                        t        | j                  |j                              S t        j                  | |      S r7   )
rH   r   r   r  r   rv   r   rx   r   r:  r   s     r<   r:  zRational.gcd  s`    eX&TVVUWW%TVVUWW%' ' zz$&&ra   c                   t        |t              rbt        | j                  t        | j                  |j                        z  |j                  z  t        | j                  |j                              S t
        j                  | |      S r7   )rH   r   rv   r   rx   r   r=  r   s     r<   r=  zRational.lcm  sb    eX&$tvvuww//%''9TVVUWW%' ' zz$&&ra   c                V    t        | j                        t        | j                        fS r7   r'  r   s    r<   as_numer_denomzRational.as_numer_denom  s    tvv//ra   c                    | r1| 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clears      r<   as_content_primitivezRational.as_content_primitive  s=     QUU{"5!--''uud{ra   c                &    | t         j                  fS r1  r2  r3  s     r<   r4  zRational.as_coeff_Mul  s    QUU{ra   c                &    | t         j                  fS r6  r7  r3  s     r<   r8  zRational.as_coeff_Add  s    QVV|ra   r  )i@B NTFFFF)FTr@  );r   rA  rB  rC  r  
is_integerr  r8   rF  r  r(  r   r   r  r  rt  r  r  r  r   r   r   __radd__r  r  r  __rmul__r  r  r  r  r  r   r#  r  r  r   r   r   r   r
  r  r3  r  r  r  r  r  rD  r  r  r  r:  r=  rK  rO  r4  r8  rI  rJ  s   @r<   r   r     s!   [x GJKII
F
FKN N`W   ) (+ )+ H(
+ )
+ (
, )
, (
+ )
+ H(/ )/ (
0 )
0 (
+ )
+ (, ),
+Z=N-)+*!+*    " ;@5:
.     (' )' (' )'0(ra   r   c                  l    e Zd ZdZdZdZdZdZdZd Z	d Z
ed        Zd Zd	 Zd
 Zd Zd Zd Z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 Z$d  Z%d! Z&d" Z' fd#Z(d$ Z)d% Z*d& Z+ e,d'e-      d(        Z.d) Z/d* Z0d+ Z1d, Z2d- Z3d. Z4d/ Z5d0 Z6d1 Z7d2 Z8d3 Z9d4 Z: xZ;S )5r   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   Tr|   c                L    t        j                  | j                  |t              S r7   )r   r  rv   re   r   s     r<   r   zInteger._as_mpf_val  s    }}TVVT3//ra   c                J    t        j                  | j                  |            S r7   )ru   r!  r   r"  s      r<   r#  zInteger._mpmath_  s    t//566ra   c                N   t        |t              r|j                  dd      }	 t        |      }|dk(  rt
        j                  S |dk(  rt
        j                  S |dk(  rt
        j                  S t        j                  |       }||_        |S # t        $ r t	        d|z        w xY w)NrN  r5   z6Argument of Integer should be of numeric type, got %s.r   rt   r   )rH   rI   rY  rS   r   r   r   r*  r  r   r   rv   )r   r;   ivalr   s       r<   r   zInteger.__new__  s    a		#r"A	Nq6D 1955L2:== 1966Mll3
  	NH1LN N	Ns   B B$c                    | j                   fS r7   r  r   s    r<   r  zInteger.__getnewargs__  s    yra   c                    | j                   S r7   r  r   s    r<   r  zInteger.__int__      vvra   c                ,    t        | j                        S r7   r   rv   r   s    r<   r   zInteger.floor      tvvra   c                ,    t        | j                        S r7   r_  r   s    r<   r   zInteger.ceiling  r`  ra   c                "    | j                         S r7   r   r   s    r<   r   zInteger.__floor__  r   ra   c                "    | j                         S r7   r   r   s    r<   r   zInteger.__ceil__!  r   ra   c                .    t        | j                         S r7   r_  r   s    r<   r  zInteger.__neg__$      wra   c                P    | j                   dk\  r| S t        | j                          S r  )rv   r   r   s    r<   r  zInteger.__abs__'  s#    66Q;KDFF7##ra   c                    t        |t              r6t        j                  r&t	        t        | j                  |j                         S t        j                  | |      S r7   )	rH   r   r1   r   r   r   rv   r   r   r   s     r<   r   zInteger.__divmod__-  sB    eW%*;*D*D6$&&%''244$$T511ra   c                P   t        |t              r,t        j                  rt	        t        || j                         S 	 t        |      }t        j                  ||       S # t        $ r= d}t        |      j                  }t        |       j                  }t        |||fz        w xY w)Nz7unsupported operand type(s) for divmod(): '%s' and '%s')rH   rS   r1   r   r   r   rv   r   r   r   r   r   )r   r   r   onamesnames        r<   r   zInteger.__rdivmod__3  s    eS!&7&@&@6%0226u $$UD11  6OU,,T
++uen 455	6s   A AB%c                   t         j                  rt        |t              rt	        | j
                  |z         S t        |t              r"t	        | j
                  |j
                  z         S t        |t              r;t        | j
                  |j                  z  |j
                  z   |j                  d      S t        j                  | |      S t        | |      S r  )
r1   r   rH   rS   r   rv   r   rx   r   Addr   s     r<   r   zInteger.__add__A  s    %%%%tvv~..E7+tvv/00E8,uww 8%''1EE##D%00tU##ra   c                `   t         j                  rt        |t              rt	        || j
                  z         S t        |t              r;t        |j
                  | j
                  |j                  z  z   |j                  d      S t        j                  | |      S t        j                  | |      S r  )	r1   r   rH   rS   r   rv   r   rx   rT  r   s     r<   rT  zInteger.__radd__M      %%%%utvv~..E8,$&&. 8%''1EE$$T511  u--ra   c                   t         j                  rt        |t              rt	        | j
                  |z
        S t        |t              r"t	        | j
                  |j
                  z
        S t        |t              r;t        | j
                  |j                  z  |j
                  z
  |j                  d      S t        j                  | |      S t        j                  | |      S r  )	r1   r   rH   rS   r   rv   r   rx   r  r   s     r<   r  zInteger.__sub__V  s    %%%%tvv~..E7+tvv/00E8,uww 8%''1EE##D%00e,,ra   c                `   t         j                  rt        |t              rt	        || j
                  z
        S t        |t              r;t        |j
                  | j
                  |j                  z  z
  |j                  d      S t        j                  | |      S t        j                  | |      S r  )	r1   r   rH   rS   r   rv   r   rx   r  r   s     r<   r  zInteger.__rsub__a  rn  ra   c                   t         j                  rt        |t              rt	        | j
                  |z        S t        |t              r"t	        | j
                  |j
                  z        S t        |t              rLt        | j
                  |j
                  z  |j                  t        | j
                  |j                              S t        j                  | |      S t        j                  | |      S r7   )
r1   r   rH   rS   r   rv   r   rx   r   r  r   s     r<   r  zInteger.__mul__j  s    %%%%tvve|,,E7+tvvegg~..E8,uwwdffegg9NOO##D%00e,,ra   c                   t         j                  rt        |t              rt	        || j
                  z        S t        |t              rLt        |j
                  | j
                  z  |j                  t        | j
                  |j                              S t        j                  | |      S t        j                  | |      S r7   )
r1   r   rH   rS   r   rv   r   rx   r   rU  r   s     r<   rU  zInteger.__rmul__u  s    %%%%uTVV|,,E8,dffegg9NOO$$T511  u--ra   c                .   t         j                  rpt        |t              rt	        | j
                  |z        S t        |t              r"t	        | j
                  |j
                  z        S t        j                  | |      S t        j                  | |      S r7   )r1   r   rH   rS   r   rv   r   r  r   s     r<   r  zInteger.__mod__~  sp    %%%%tvv~..E7+tvv/00##D%00e,,ra   c                .   t         j                  rpt        |t              rt	        || j
                  z        S t        |t              r"t	        |j
                  | j
                  z        S t        j                  | |      S t        j                  | |      S r7   )r1   r   rH   rS   r   rv   r   r  r   s     r<   r  zInteger.__rmod__  sp    %%%%utvv~..E7+uww/00$$T511  u--ra   c                    t        |t              r| j                  |k(  S t        |t              r| j                  |j                  k(  S t        j                  | |      S r7   )rH   rS   rv   r   r   r
  r   s     r<   r
  zInteger.__eq__  sH    eS!FFeO$w'FFegg%&tU++ra   c                    | |k(   S r7   r|   r   s     r<   r  zInteger.__ne__  r/  ra   c                    	 t        |      }|j                  r"t        | j                  |j                  kD        S t
        j                  | |      S # t        $ r	 t        cY S w xY wr7   )r
   r   r   r   rv   r   r  r   s     r<   r  zInteger.__gt__  [    	"UOE DFFUWW,--tU++	  	"!!	"   A A#"A#c                    	 t        |      }|j                  r"t        | j                  |j                  k        S t
        j                  | |      S # t        $ r	 t        cY S w xY wr7   )r
   r   r   r   rv   r   r  r   s     r<   r  zInteger.__lt__  rx  ry  c                    	 t        |      }|j                  r"t        | j                  |j                  k\        S t
        j                  | |      S # t        $ r	 t        cY S w xY wr7   )r
   r   r   r   rv   r   r  r   s     r<   r  zInteger.__ge__  [    	"UOE DFFegg-..tU++	  	"!!	"ry  c                    	 t        |      }|j                  r"t        | j                  |j                  k        S t
        j                  | |      S # t        $ r	 t        cY S w xY wr7   )r
   r   r   r   rv   r   r  r   s     r<   r  zInteger.__le__  r|  ry  c                ,    t        | j                        S r7   )r  rv   r   s    r<   r  zInteger.__hash__  s    DFF|ra   c                    | j                   S r7   r  r   s    r<   	__index__zInteger.__index__  r]  ra   c                2    t        | j                  dz        S Nr2   )r   rv   r   s    r<   _eval_is_oddzInteger._eval_is_odd  s    DFFQJra   c           
        ddl m} |t        j                  u r_| j                  t        j
                  kD  rt        j                  S t        j                  t        j                  t        j                  z  z   S |t        j                  u rt        d| d      t        j                  z  S t        |t              s| j                  r|j                  r|  |z  S t        |t              rt        | =  |      S t        |t              sy|t        j                   u r*| j                  rt        j                  t#        |  |      z  S |j                  rN| }| j                  r%t        j$                  |z  t        d|  d      |z  z  S t        d| j                  d      |z  S t'        t)        | j                        |j*                        \  }}|rEt-        |t)        |j                        z        }| j                  r|t        j$                  |z  z  }|S t/        t)        | j                              } ||      }|dur|d   |d   i}	nt-        |      j1                  d      }	d}
d}d}d}i }|	j3                         D ]  \  }}||j                  z  }t5        ||j*                        \  }}|dkD  r|
||z  z  }
|dkD  sAt7        ||j*                        }|dk7  r+|t#        |t        ||z  |j*                  |z  d            z  }|||<    |j3                         D ]   \  }}|dk(  r|}t7        ||      }|dk(  s  n |j3                         D ]  \  }}||||z  z  z  } ||k(  r|
dk(  r	|dk(  rd}|S |
|z  t#        |t        ||j*                              z  }| j                  r|t#        t        j$                  |      z  }|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<  )rA  r  r   r   rv   r   r  r   r   rH   r   r)  r  rM   r  r  r  r  r*  r   rT   rx   r   rS   rD  itemsr   r   )r   rj   r  r  xxexactresultb_posrv   r  out_intout_radsqr_intsqr_gcdsqr_dictprimeexponentdiv_ediv_mr   exkvr   s                          r<   r  zInteger._eval_power  sk   * 	81::vv~zz!::

 :::1%%%AtQ'33$' DLL}$dE"7&t,,$)166>d..??3ud#333B}}d*8Aua+@"+DDD4661-r11#CK8	6QDFF^,F!----M CK % E>aD!A$<D5>)))6D #zz| 	,OE8H!(DFF3LE5qy5%<'qy '6s5(5!8TVVQY*JKKG&+HUO	, ^^% 	EAr!|w+a<	 NN$ 	'DAqq1g:&G	'e1AF
  W_S(7DFF2K%LLF#ammT22ra   c                    ddl m}  ||       S )Nr   )isprime)sympy.ntheory.primetestr  )r   r  s     r<   _eval_is_primezInteger._eval_is_prime5  s    3t}ra   c                8    | dkD  rt        | j                        S y)Nr   F)r   is_primer   s    r<   _eval_is_compositezInteger._eval_is_composite:  s    !8T]]++ra   c                &    | t         j                  fS r7   r2  r   s    r<   rK  zInteger.as_numer_denom@  s    QUU{ra   r   c                    t        |t              st        S t        |t              rt        | j                  |z        S t        | |      d   S r  )rH   r   r   r   rv   r   r   s     r<   __floordiv__zInteger.__floordiv__C  sA    %&!!eW%466U?++dE"1%%ra   c                X    t        t        |      j                  | j                  z        S r7   r_  r   s     r<   __rfloordiv__zInteger.__rfloordiv__K  s     wu~''466122ra   c                    t        |t        t        t        j                  f      r!t        | j
                  t        |      z        S t        S r7   rH   rS   r   numbersIntegralrv   r   r   s     r<   
__lshift__zInteger.__lshift__U  7    ec7G,<,<=>466SZ/00!!ra   c                    t        |t        t        j                  f      r!t	        t        |      | j
                  z        S t        S r7   rH   rS   r  r  r   rv   r   r   s     r<   __rlshift__zInteger.__rlshift__[  5    ec7#3#3453u:/00!!ra   c                    t        |t        t        t        j                  f      r!t        | j
                  t        |      z	        S t        S r7   r  r   s     r<   
__rshift__zInteger.__rshift__a  r  ra   c                    t        |t        t        j                  f      r!t	        t        |      | j
                  z	        S t        S r7   r  r   s     r<   __rrshift__zInteger.__rrshift__g  r  ra   c                    t        |t        t        t        j                  f      r!t        | j
                  t        |      z        S t        S r7   r  r   s     r<   __and__zInteger.__and__m  7    ec7G,<,<=>466CJ.//!!ra   c                    t        |t        t        j                  f      r!t	        t        |      | j
                  z        S t        S r7   r  r   s     r<   __rand__zInteger.__rand__s  5    ec7#3#3453u:.//!!ra   c                    t        |t        t        t        j                  f      r!t        | j
                  t        |      z        S t        S r7   r  r   s     r<   __xor__zInteger.__xor__y  r  ra   c                    t        |t        t        j                  f      r!t	        t        |      | j
                  z        S t        S r7   r  r   s     r<   __rxor__zInteger.__rxor__  r  ra   c                    t        |t        t        t        j                  f      r!t        | j
                  t        |      z        S t        S r7   r  r   s     r<   __or__zInteger.__or__  r  ra   c                    t        |t        t        j                  f      r!t	        t        |      | j
                  z        S t        S r7   r  r   s     r<   __ror__zInteger.__ror__  r  ra   c                .    t        | j                         S r7   r_  r   s    r<   
__invert__zInteger.__invert__  re  ra   )<r   rA  rB  rC  rx   rS  r8   r   rF  r   r#  r   r   r  r  r   r   r   r   r  r  r   r   r   rT  r  r  r  rU  r  r  r
  r  r  r  r  r  r  r  r  r  r  r  rK  r   r   r  r  r  r  r  r  r  r  r  r  r  r  r  rI  rJ  s   @r<   r   r     s5   4 	
AJIJI07  4 $22
$.	-.	-.-.,!,,,,
 jX
 (& )&3"""""""""" ra   r   c                       e Zd ZU dZdZdZdZdZeZ	 e
       Zded<   ddZ fdZd Zed	        Zdd
ZddZd Zd Zd Zd Zd Zed        Zd ZddZd ZddZ x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_symbolsc                p   ddl m}m} ddlm} t        |      }d}d}	t        |t        t        f      r |\  }
}|
j                  skddl
m}  ||
      }
n\|j                  r4|j                  |j                  |j                  |j                   f\  }
}}}	n |||j#                  d      d      |}}
|
j%                         }|it        ||      s%|j'                  t        |      d|      }t        | }nY|j)                  |j+                         d|      }t        |j+                          }n!|j)                  d	dgd|      }t        d	d      }|Gdd
lm}  ||j+                         |j+                         |      }|j)                  |d|      }t        | }|j1                         |
j1                         k\  r|j3                  |
j                        }||f}|xs |	}| d	dlm} t        ||      s ||      }||fz   }t9        j:                  | g| }||_        ||_        ||_        |
|_        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polysr   )dup_compose)Symbol)sympy.polys.polyclassesr  r  sympy.polys.numberfieldsr  r   rH   r   r   is_Polyr   r  is_AlgebraicNumberr  r  r  r  get
get_domainfrom_sympy_list	from_listto_listsympy.polys.densetoolsr  degreeremsymbolr  r   r   r  )r   exprcoeffsr  r   r  r  r  rep0alias0r  r  r  domr  scoeffsr  csargsr  r   s                        r<   r   zAlgebraicNumber.__new__  s   R 	5?t}dUEN+ MGT??6w-$$+/<<+/88TZZ+A'GT4 /dhhuoT348 G   "fc*))'&/1cB.mmFNN$4a=!12 --A3/CAqkG:CKKM4<<>3?A--1c*CQiG::<7>>++'''++&Cw&eV,uUH$Ell3''	
ra   c                     t         |          S r7   r  r  s    r<   r  zAlgebraicNumber.__hash__	  r  ra   c                @    | j                         j                  |      S r7   )as_expr_evalfr   s     r<   r   zAlgebraicNumber._eval_evalf	  s    ||~$$T**ra   c                    | j                   duS )z'Returns ``True`` if ``alias`` was set. N)r  r   s    r<   
is_aliasedzAlgebraicNumber.is_aliased	  s     zz%%ra   c                   ddl m}m} ||j                  | j                  |      S | j
                  &|j                  | j                  | j
                        S ddlm} |j                  | j                   |d            S )z&Create a Poly instance from ``self``. r   )r  PurePolyr   Dummyr  )r   r  r  r   r  r  r  r  )r   r  r  r  r  s        r<   as_polyzAlgebraicNumber.as_poly	  s^    8=88DHHa((zz%xx$**55)||DHHeCj99ra   c                x    | j                  |xs | j                        j                         j                         S )z)Create a Basic expression from ``self``. )r  r  r  expand)r   r  s     r<   r  zAlgebraicNumber.as_expr	  s+    ||AN+335<<>>ra   c                    | j                   j                         D cg c]'  }| j                   j                  j                  |      ) c}S c c}w )z7Returns all SymPy coefficients of an algebraic number. )r  
all_coeffsr  to_sympyr   r  s     r<   r  zAlgebraicNumber.coeffs	  s6    37883F3F3HJa&&q)JJJs   ,Ac                6    | j                   j                         S )z8Returns all native coefficients of an algebraic number. )r  r  r   s    r<   native_coeffszAlgebraicNumber.native_coeffs	  s    xx""$$ra   c                x   ddl m} | j                  }|j                         dk(  r| S |j                         |j	                         dz
  z  }|j                   ||j                  |j                         z              }||z  }|j                         | j                  z  }t        ||f| j                               S )z*Convert ``self`` to an algebraic integer. r   r  r   )
r   r  r  LCr  composer  r  r  r  )r   r  r   coeffpolyr  r  s          r<   to_algebraic_integerz$AlgebraicNumber.to_algebraic_integer	  s    .LL446Q;Ka(yyaeeADDFl+,u*ttvdii>>ra   c                L   ddl m} ddlm} |d   |d   }}| j                  j	                         D cg c]  }|j
                  |k7  s| c}D ]M  } || j                  |z
        j                  s# ||      | || j                        z  k  sBt        |      c S  | S c c}w )Nr   )CRootOfr  measureratio)	sympy.polys.rootoftoolsr  sympy.polysr  	all_rootsfuncr  	is_Symbolr  )r   r,  r  r  r  r  r   s          r<   _eval_simplifyzAlgebraicNumber._eval_simplify	  s    3'	*F7O!\\335K79J!K 	.Atyy1}%//1:gdii&8 88*1--		.
  Ls   B!B!c                ^    t        | j                  | j                  f|| j                        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  s     r<   field_elementzAlgebraicNumber.field_element	  s,    P \\499%fDJJH 	Hra   c                T    | j                         }|ddgk(  xs || 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  s     r<   is_primitive_elementz$AlgebraicNumber.is_primitive_element

  s-     KKMQF{.aDII;..ra   c                D    | j                   r| S | j                  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   s    r<   primitive_elementz!AlgebraicNumber.primitive_element
  s'     $$K!!1a&))ra   c                |    | j                   r| S | j                         }| j                  |      }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   s       r<   to_primitive_elementz$AlgebraicNumber.to_primitive_element#
  s@    B $$K##%LL(L+1v&&ra   c                    | j                   ]| j                  r| j                  | _         | j                   S ddlm} | j	                         } || j                  |      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$.

        r   r  Tr  )r  r  r   sympy.polys.numberfields.minpolyr  r  )r   r  thetas      r<   r  z"AlgebraicNumber.minpoly_of_elementj
  sk     $(($(LL!
     E..0$+DLL,?t$L!   ra   c                4   | j                   r&t        | j                  t              s| j                  S |xs | j	                         }|j                  |      }t        |      dk(  r|d   S | j                         }|D ]  }|j                  ||      s|c S  y)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  rH   r  r  r  r	  r   r  	same_root)r   r  r  r   rootsr  rY   s          r<   r  zAlgebraicNumber.to_root
  s    ( $$Z		?-S990t..0X.u:?8O\\^ 	A{{1b!	ra   r  r7   r  )TN)r   rA  rB  rC  rF  r  is_algebraicr8   r   rG  setr  r  r   r  r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  rI  rJ  s   @r<   r  r    s    " DILI D
  #uL*$fP"+ & &
:?K%?"	)HV / /*E'N!,ra   r  c                      e Zd ZdZdZd Zy)RationalConstantz
    Abstract base class for rationals with specific behaviors

    Derived classes must define class attributes p and q and should probably all
    be singletons.
    r|   c                ,    t        j                  |       S r7   r   r   r   s    r<   r   zRationalConstant.__new__
      !!#&&ra   N)r   rA  rB  rC  rF  r   r|   ra   r<   r"  r"  
  s     I'ra   r"  c                      e Zd ZdZd Zy)IntegerConstantr|   c                ,    t        j                  |       S r7   r$  r   s    r<   r   zIntegerConstant.__new__
  r%  ra   N)r   rA  rB  rF  r   r|   ra   r<   r'  r'  
  s    I'ra   r'  c                  h    e Zd ZdZdZdZdZdZdZdZ	dZ
dZd Zed        Zed	        Zd
 Zd Zd Zy)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   FTr|   c                     yr'  r|   r   s    r<   r  zZero.__getnewargs__
      ra   c                 "    t         j                  S r7   r7  r|   ra   r<   r  zZero.__abs__
      vvra   c                 "    t         j                  S r7   r7  r|   ra   r<   r  zZero.__neg__
  r-  ra   c                ^   |j                   r| S |j                  rt        j                  S |j                  du rt        j
                  S |j                  rt        j                  S |j                         \  }}|j                  rt        j                  |z  S |t        j                  ur| |z  S y r,  )
r  r   r   r  r  r   r  r   r4  r)  )r   rj   r  termss       r<   r  zZero._eval_power
  s    $$K$$$$$  E)55L<<55L
 ((*u$$e++; ra   c                    | S r7   r|   r   r   s     r<   r   zZero._eval_order
      ra   c                     yr,  r|   r   s    r<   r  zZero.__bool__
  s    ra   N)r   rA  rB  rC  rv   rx   r  r)  r  r8   r  rF  r  staticmethodr  r  r  r   r  r|   ra   r<   r  r  
  sm    & 	
A	AKKGIMI    &ra   r  )	metaclassc                  l    e Zd ZdZdZdZdZdZdZd Z	e
d        Ze
d        Zd Zd	 Ze
	 	 dd       Zy
)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   r|   c                     yr'  r|   r   s    r<   r  zOne.__getnewargs__  r+  ra   c                 "    t         j                  S r7   r2  r|   ra   r<   r  zOne.__abs__      uura   c                 "    t         j                  S r7   )r   r*  r|   ra   r<   r  zOne.__neg__  s    }}ra   c                    | S r7   r|   r   rj   s     r<   r  zOne._eval_power  r   ra   c                     y r7   r|   r2  s     r<   r   zOne._eval_order  s    ra   Nc                *    |rt         j                  S i S r7   r2  )r<  r=  r>  r?  r@  rC  s         r<   rD  zOne.factors"  s     55LIra   rR  )r   rA  rB  rC  r8   r  rv   rx   rF  r  r5  r  r  r  r   rD  r|   ra   r<   r   r   
  ss      IK	A	AI     CH&+ ra   r   c                  L    e Zd ZdZdZdZdZdZd Ze	d        Z
e	d        Zd	 Zy
)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

    Trt   r   r|   c                     yr'  r|   r   s    r<   r  zNegativeOne.__getnewargs__I  r+  ra   c                 "    t         j                  S r7   r2  r|   ra   r<   r  zNegativeOne.__abs__L  r:  ra   c                 "    t         j                  S r7   r2  r|   ra   r<   r  zNegativeOne.__neg__P  r:  ra   c                   |j                   rt        j                  S |j                  rt        j                  S t        |t              rt        |t              rt        d      |z  S |t        j                  u rt        j                  S |t        j                  t        j                  fv rt        j                  S |t        j                  u rt        j                  S t        |t              ry|j                  dk(  r&t        j                  t        |j                         z  S t#        |j                   |j                        \  }}|r| |z  | t        ||j                        z  z  S y )Ng      r2   )is_oddr   r*  r  r   rH   r   rM   r   r   r   r  r  r   rx   r   rv   r   )r   rj   r;   r   s       r<   r  zNegativeOne._eval_powerT  s    ;;== <<55LdF#$&T{D((quu}uu

A$6$677uuqvv~&$)66Q;??GDFFO;;dffdff-174!TVV)<#<<<ra   N)r   rA  rB  rC  r8   rv   rx   rF  r  r5  r  r  r  r|   ra   r<   r*  r*  +  sO    , I
A	AI    ra   r*  c                  6    e Zd ZdZdZdZdZdZd Ze	d        Z
y)	r  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   r2   r|   c                     yr'  r|   r   s    r<   r  zHalf.__getnewargs__  r+  ra   c                 "    t         j                  S r7   )r   r  r|   ra   r<   r  zHalf.__abs__  r-  ra   N)r   rA  rB  rC  r8   rv   rx   rF  r  r5  r  r|   ra   r<   r  r  k  s6      I	A	AI  ra   r  c                      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dZdd	Z ed
e      d        ZeZ ed
e      d        Z ed
e      d        Z ed
e      d        ZeZ ed
e      d        Zd Zd Zd Zd Z fdZd Z d Z!e"jF                  Z#e"jH                  Z$e"jJ                  Z%e"jL                  Z& ed
e      d        Z'e'Z(d Z)d Z* xZ+S )r   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
    TFr|   c                ,    t        j                  |       S r7   r$  r   s    r<   r   zInfinity.__new__  r%  ra   c                     y)Nz\inftyr|   r   printers     r<   _latexzInfinity._latex      ra   c                    | |k(  r|S y r7   r|   r   s      r<   r   zInfinity._eval_subs      3;J ra   c                    t        d      S Nr   rM   r   s     r<   r   zInfinity._eval_evalf  s    U|ra   c                $    | j                  |      S r7   r   r   rg   optionss      r<   r  zInfinity.evalf      %%ra   r   c                    t        |t              rDt        j                  r4|t        j
                  t        j                  fv rt        j                  S | S t        j                  | |      S r7   )rH   r   r1   r   r   r   r   r   r   s     r<   r   zInfinity.__add__  K    eV$):)C)C++QUU33uuK~~dE**ra   c                    t        |t              rDt        j                  r4|t        j
                  t        j                  fv rt        j                  S | S t        j                  | |      S r7   )rH   r   r1   r   r   r   r   r  r   s     r<   r  zInfinity.__sub__  I    eV$):)C)CQUU++uuK~~dE**ra   c                &    |  j                  |      S r7   r   r   s     r<   r  zInfinity.__rsub__      u%%ra   c                   t        |t              r\t        j                  rL|j                  s|t
        j                  u rt
        j                  S |j                  r| S t
        j                  S t        j                  | |      S r7   )
rH   r   r1   r   r  r   r   r  r   r  r   s     r<   r  zInfinity.__mul__  sZ    eV$):)C)C}}uu))%%%~~dE**ra   c                6   t        |t              rtt        j                  rd|t        j
                  u s$|t        j                  u s|t        j                  u rt        j                  S |j                  r| S t        j                  S t        j                  | |      S r7   
rH   r   r1   r   r   r   r   r   is_extended_nonnegativer  r   s     r<   r  zInfinity.__truediv__  sp    eV$):)C)C

"+++QUUNuu,,%%%!!$..ra   c                "    t         j                  S r7   r   r   r   s    r<   r  zInfinity.__abs__      zzra   c                "    t         j                  S r7   )r   r   r   s    r<   r  zInfinity.__neg__  s    !!!ra   c                   |j                   rt        j                  S |j                  rt        j                  S |t        j
                  u rt        j
                  S |t        j                  u rt        j
                  S |j                  du r|j                  ruddl	m
}  ||      }|j                  rt        j                  S |j                  rt        j                  S |j                  rt        j
                  S | |j                         z  S yy)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   )r  N)r  r   r   r   r  r   r  r  r8   r   r  r  r)  r  r  )r   rj   r  	expt_reals       r<   r  zInfinity._eval_power  s    $ $$::$$66M155=55L1$$$55L  E)dnn?4I$$((($$vv  uu%% /=)ra   c                "    t         j                  S r7   )r   r)   r   s     r<   r   zInfinity._as_mpf_val'  s    yyra   c                     t         |          S r7   r  r  s    r<   r  zInfinity.__hash__*  r  ra   c                F    |t         j                  u xs |t        d      k(  S rS  r   r   r   r   s     r<   r
  zInfinity.__eq__-  s    

";euU|&;;ra   c                F    |t         j                  uxr |t        d      k7  S rS  rn  r   s     r<   r  zInfinity.__ne__0  s    AJJ&@5E%L+@@ra   c                N    t        |t              st        S t        j                  S r7   rH   r   r   r   r   r   s     r<   r  zInfinity.__mod__8      %&!!uura   c                    | S r7   r|   r   s    r<   r   zInfinity.floor@  r   ra   c                    | S r7   r|   r   s    r<   r   zInfinity.ceilingC  r   ra   r7   ),r   rA  rB  rC  rD  r8   
is_complexr  r   r  r  r  rF  r   rN  r   r   r  r   r   r   rT  r  r  r  rU  r  r  r  r  r   r  r
  r  r   r  r  r  r  r  r  r   r   rI  rJ  s   @r<   r   r     sY   &P NIJKMHI'& (+ )+ H(+ )+ (& )& (+ )+ H(	/ )	/"$&L"<A [[F[[F[[F[[F( )
 Hra   r   c                      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dZdd	Z ed
e      d        ZeZ ed
e      d        Z ed
e      d        Z ed
e      d        ZeZ ed
e      d        Zd Zd Zd Zd Z fdZd Z d Z!e"jF                  Z#e"jH                  Z$e"jJ                  Z%e"jL                  Z& ed
e      d        Z'e'Z(d Z)d Z*d Z+ xZ,S )r   zNegative infinite quantity.

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

    See Also
    ========

    Infinity
    TFr|   c                ,    t        j                  |       S r7   r$  r   s    r<   r   zNegativeInfinity.__new__`  r%  ra   c                     y)Nz-\inftyr|   rL  s     r<   rN  zNegativeInfinity._latexc  s    ra   c                    | |k(  r|S y r7   r|   r   s      r<   r   zNegativeInfinity._eval_subsf  rQ  ra   c                    t        d      S Nr   rT  r   s     r<   r   zNegativeInfinity._eval_evalfj  s    V}ra   c                $    | j                  |      S r7   rV  rW  s      r<   r  zNegativeInfinity.evalfm  rY  ra   r   c                    t        |t              rDt        j                  r4|t        j
                  t        j                  fv rt        j                  S | S t        j                  | |      S r7   )rH   r   r1   r   r   r   r   r   r   s     r<   r   zNegativeInfinity.__add__p  r]  ra   c                    t        |t              rDt        j                  r4|t        j
                  t        j                  fv rt        j                  S | S t        j                  | |      S r7   )rH   r   r1   r   r   r   r   r  r   s     r<   r  zNegativeInfinity.__sub__y  r[  ra   c                &    |  j                  |      S r7   r_  r   s     r<   r  zNegativeInfinity.__rsub__  r`  ra   c                   t        |t              r\t        j                  rL|j                  s|t
        j                  u rt
        j                  S |j                  r| S t
        j                  S t        j                  | |      S r7   )
rH   r   r1   r   r  r   r   r  r   r  r   s     r<   r  zNegativeInfinity.__mul__  sX    eV$):)C)C}}uu))::~~dE**ra   c                6   t        |t              rtt        j                  rd|t        j
                  u s$|t        j                  u s|t        j                  u rt        j                  S |j                  r| S t        j
                  S t        j                  | |      S r7   rc  r   s     r<   r  zNegativeInfinity.__truediv__  sn    eV$):)C)C

"+++QUUNuu,,::!!$..ra   c                "    t         j                  S r7   rf  r   s    r<   r  zNegativeInfinity.__abs__  rg  ra   c                "    t         j                  S r7   rf  r   s    r<   r  zNegativeInfinity.__neg__  rg  ra   c                *   |j                   r|t        j                  u s$|t        j                  u s|t        j                  u rt        j                  S t        |t              r8|j                  r,|j                  rt        j                  S t        j                  S t        j                  |z  }t        j                  |z  }|dk(  r|j                  r|S |t        j                  u r(|j                  r|j                  st        j                  S ||z  S y)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)r8   r   r   r   r   rH   r   r  rE  r*  r~   r  r  )r   rj   inf_parts_parts       r<   r  zNegativeInfinity._eval_power  s    , >>quu}

"A...uu$(T-F-F;;---::%zz4'H]]D(F1}!1!1A---$$V^^((((?"' ra   c                "    t         j                  S r7   )r   r*   r   s     r<   r   zNegativeInfinity._as_mpf_val  s    zzra   c                     t         |          S r7   r  r  s    r<   r  zNegativeInfinity.__hash__  r  ra   c                F    |t         j                  u xs |t        d      k(  S r{  r   r   r   r   s     r<   r
  zNegativeInfinity.__eq__  s!    ***DeuV}.DDra   c                F    |t         j                  uxr |t        d      k7  S r{  r  r   s     r<   r  zNegativeInfinity.__ne__  s!    A...I5E&M3IIra   c                N    t        |t              st        S t        j                  S r7   rq  r   s     r<   r  zNegativeInfinity.__mod__  rr  ra   c                    | S r7   r|   r   s    r<   r   zNegativeInfinity.floor  r   ra   c                    | S r7   r|   r   s    r<   r   zNegativeInfinity.ceiling  r   ra   c                F    t         j                  dt         j                  diS r  )r   r*  r   r   s    r<   as_powers_dictzNegativeInfinity.as_powers_dict  s    q!**a00ra   r7   )-r   rA  rB  rC  r  ru  rD  r   r  r   r8   r  rF  r   rN  r   r   r  r   r   r   rT  r  r  r  rU  r  r  r  r  r   r  r
  r  r   r  r  r  r  r  r  r   r   r  rI  rJ  s   @r<   r   r   I  s^   	 JNKMIHI'& (+ )+ H(+ )+ (& )& (+ )+ H(	/ )	/)#V"EJ [[F[[F[[F[[F( )
 H1ra   r   c                  f    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 Zd Zd Z ed	e      d
        Z ed	e      d        Z ed	e      d        Z ed	e      d        Zd Zd Zd Z fdZd Z d Z!e"jF                  Z#e"jH                  Z$e"jJ                  Z%e"jL                  Z& x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

    TNFr|   c                ,    t        j                  |       S r7   r$  r   s    r<   r   zNaN.__new__1  r%  ra   c                     y)Nz
\text{NaN}r|   rL  s     r<   rN  z
NaN._latex4  s    ra   c                    | S r7   r|   r   s    r<   r  zNaN.__neg__7  r   ra   r   c                    | S r7   r|   r   s     r<   r   zNaN.__add__:  r3  ra   c                    | S r7   r|   r   s     r<   r  zNaN.__sub__>  r3  ra   c                    | S r7   r|   r   s     r<   r  zNaN.__mul__B  r3  ra   c                    | S r7   r|   r   s     r<   r  zNaN.__truediv__F  r3  ra   c                    | S r7   r|   r   s    r<   r   z	NaN.floorJ  r   ra   c                    | S r7   r|   r   s    r<   r   zNaN.ceilingM  r   ra   c                    t         S r7   )rk  r   s     r<   r   zNaN._as_mpf_valP  s    ra   c                     t         |          S r7   r  r  s    r<   r  zNaN.__hash__S  r  ra   c                &    |t         j                  u S r7   r   r   r   s     r<   r
  z
NaN.__eq__V  s    ~ra   c                &    |t         j                  uS r7   r  r   s     r<   r  z
NaN.__ne__Z  s    AEE!!ra   )(r   rA  rB  rC  rD  r  r  r  r  is_transcendentalrS  r  r~   r  r  r  r)  r8   rF  r   rN  r  r   r   r   r  r  r  r   r   r   r  r
  r  r   r  r  r  r  rI  rJ  s   @r<   r   r     s   .^ NGKLJMIGHKKII' ( ) ( ) ( ) ( )"" [[F[[F[[F[[Fra   r   c                     yr,  r|   )rX   rY   s     r<   _eval_is_eqr  e      ra   c                  n    e Zd ZdZdZdZdZdZdZdZ	e
ZdZd Zd Zed        Zd Zd	 Zed
        Zd Zy)r  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
    TFr|   c                ,    t        j                  |       S r7   r$  r   s    r<   r   zComplexInfinity.__new__  r%  ra   c                     y)Nz\tilde{\infty}r|   rL  s     r<   rN  zComplexInfinity._latex  s     ra   c                 "    t         j                  S r7   rf  r|   ra   r<   r  zComplexInfinity.__abs__  s    zzra   c                    | S r7   r|   r   s    r<   r   zComplexInfinity.floor  r   ra   c                    | S r7   r|   r   s    r<   r   zComplexInfinity.ceiling  r   ra   c                 "    t         j                  S r7   )r   r  r|   ra   r<   r  zComplexInfinity.__neg__  s       ra   c                    |t         j                  u rt         j                  S t        |t              rH|j
                  rt         j                  S |j                  rt         j                  S t         j                  S y r7   )r   r  r   rH   r   r  r  r  r=  s     r<   r  zComplexInfinity._eval_power  sV    1$$$55LdF#||uu##,,,66M $ra   N)r   rA  rB  rC  rD  r   r8   r  ru  r  r   rG  rF  r   rN  r5  r  r   r   r  r  r|   ra   r<   r  r  j  ss    > NKIHJDI'!   ! !"ra   r  c                  f     e Zd ZdZdZdZdZdZeZ	d Z
d Zd Zd Zd Zd Zd	 Zd
 Z fdZ xZS )rc  Tr|   c                ,    t        j                  |       S r7   r$  r   s    r<   r   zNumberSymbol.__new__  r%  ra   c                     y)z Return an interval with number_cls endpoints
        that contains the value of NumberSymbol.
        If not implemented, then return None.
        Nr|   r   
number_clss     r<   approximationzNumberSymbol.approximation  s    ra   c                L    t         j                  | j                  |      |      S r7   r   r   s     r<   r   zNumberSymbol._eval_evalf  r   ra   c                    	 t        |      }| |u ry|j                  r| j                  ryy# t        $ r	 t        cY S w xY wr  )r
   r   r   rE  r  r   s     r<   r
  zNumberSymbol.__eq__  sH    	"UOE 5=??t11  	"!!	"s   , >>c                    | |k(   S r7   r|   r   s     r<   r  zNumberSymbol.__ne__  r/  ra   c                V    | |u rt         j                  S t        j                  | |      S r7   )r   truer   r  r   s     r<   r  zNumberSymbol.__le__  #    5=66M{{4''ra   c                V    | |u rt         j                  S t        j                  | |      S r7   )r   r  r   r  r   s     r<   r  zNumberSymbol.__ge__  r  ra   c                    t         r7   )r   r   s    r<   r  zNumberSymbol.__int__  s    !!ra   c                     t         |          S r7   r  r  s    r<   r  zNumberSymbol.__hash__  r  ra   )r   rA  rB  rD  r~   r8   rF  r-  r   rG  r   r  r   r
  r  r  r  r  r  rI  rJ  s   @r<   rc  rc    sS    NIIIOD'8
!(
(
"" "ra   rc  c                  p    e Zd ZdZdZdZdZdZdZdZ	dZ
dZd Zed        Zd Zd Zd	 Zd
 Zd Zd Zd Zy)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
    TFr|   c                     y)Nr   r|   rL  s     r<   rN  zExp1._latex  r$  ra   c                 "    t         j                  S r7   )r   r  r|   ra   r<   r  zExp1.__abs__  r-  ra   c                     yr  r|   r   s    r<   r  zExp1.__int__       ra   c                    t        |      S r7   )r&   r   s     r<   r   zExp1._as_mpf_val#  s    T{ra   c                r    t        |t              rt        d      t        d      fS t        |t              ry y )Nr2   rQ  
issubclassr   r   r  s     r<   approximation_intervalzExp1.approximation_interval&  s1    j'*AJ
++
H- .ra   c                `    t         j                  r| j                  |      S ddlm}  ||      S )Nr   )rp  )r1   
exp_is_pow_eval_power_exp_is_pow&sympy.functions.elementary.exponentialrp  )r   rj   rp  s      r<   r  zExp1._eval_power,  s)    ''..t44Bt9ra   c                   |j                   r(|t        u rt        S |t         k(  rt        j                  S ddlm} t        ||      r|j                  d   S |j                  st        t        z  }||| fv rt        S |j                  t        t        z        }|rd|z  j                  r|j                  rt        j                  S |j                   rt        j"                  S |t        j$                  z   j                  rt         S |t        j$                  z   j                   r[t        S |j&                  rI|dz  }|dkD  r|dz  }||k7  r5t        j(                  |t        j*                  z  t        j,                  z  z  S |j/                         \  }}|t        t         fv ry |gd }}t1        j2                  |      D ]A  }	t        |	|      r||	j                  d   }! y |	j4                  r|j7                  |	       A y  |r|t1        | z  S d S |j                  rg }
g }d}|j                  D ]  }|t        j                  u r|j7                  |       '| |z  }t        |t8              rM|j:                  | u r?|j<                  |k7  r|j7                  |j<                         d}w|j7                  |       |
j7                  |        |
s|rt1        |
 t9        | t?        | d      z  S y |j@                  r|j=                         S y )Nr   )logr2   r   FTr  )!rE  oor   r  r  r  rH   r   is_AddIr   r  pirS  r  r   rE  r*  r  r(  r  Pir  r4  Mul	make_argsr  appendr  baserp  rl  	is_Matrix)r   rq  r  Ioor  ncoeffr0  r  log_termtermoutadd
argchangedrX   newas                  r<   r  zExp1._eval_power_exp_is_pow3  s   ==by	vv>c388A; B$CsSDk!
IIbdOEeG''}} uu }},!&&.11 !r	!&&.00 &&"QYFz! vvqttAOO(CDD ++-LE5 bS	! %wHFe, 	dC('#'99Q<''MM$'	 .68S&\)?4?ZZCCJXX %:JJqMQwdC(TYY$->xx1}

488,%)


1JJt$% jCyT39u!EEE !]]779 ra   c                v    ddl m}  |t        t        j                  dz  z         t         |t              z  z
  S )Nr   )sinr2   )(sympy.functions.elementary.trigonometricr  r  r   r  )r   r,  r  s      r<   _eval_rewrite_as_sinzExp1._eval_rewrite_as_sin  s(    @1qttAv:3q6))ra   c                v    ddl m}  |t              t         |t        t        j                  dz  z         z  z   S )Nr   )cosr2   )r  r  r  r   r  )r   r,  r  s      r<   _eval_rewrite_as_coszExp1._eval_rewrite_as_cos  s)    @1v#a!$$q&j/)))ra   N)r   rA  rB  rC  r  r  r)  r  r8   r  r  rF  rN  r5  r  r  r   r  r  r  r  r  r|   ra   r<   r  r    sp    6 GKKMILI  N`**ra   r  c                  X    e Zd ZdZdZdZdZdZdZdZ	dZ
dZd Zed        Zd Zd Zd	 Zy
)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
    TFr|   c                     y)Nz\pir|   rL  s     r<   rN  z	Pi._latex  s    ra   c                 "    t         j                  S r7   )r   r  r|   ra   r<   r  z
Pi.__abs__  s    ttra   c                     y)NrQ  r|   r   s    r<   r  z
Pi.__int__  r  ra   c                    t        |      S r7   )r%   r   s     r<   r   zPi._as_mpf_val  s    d|ra   c                    t        |t              rt        d      t        d      fS t        |t              rt        ddd      t        ddd      fS y )NrQ  rR     G   r         r  r  s     r<   r  zPi.approximation_interval  sK    j'*AJ
++
H-S"a((2q!*<== .ra   N)r   rA  rB  rC  r  r  r)  r  r8   r  r  rF  rN  r5  r  r  r   r  r|   ra   r<   r  r    s[    !F GKKMILI  >ra   r  c                  R    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eZy
)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
    TFr|   c                     y)Nz\phir|   rL  s     r<   rN  zGoldenRatio._latex  s    ra   c                     yr  r|   r   s    r<   r  zGoldenRatio.__int__  r  ra   c                f    t        j                  t        |dz         | dz
        }t        ||      S NrF   )r   from_man_expr'   rm   r~  s      r<   r   zGoldenRatio._as_mpf_val  s0    y3dURZ@D!!ra   c                b    ddl m} t        j                  t        j                   |d      z  z   S )Nr   )sqrtrG   )(sympy.functions.elementary.miscellaneousr  r   r  )r   hintsr  s      r<   _eval_expand_funczGoldenRatio._eval_expand_func  s     AvvtAw&&ra   c                |    t        |t              rt        j                  t	        d      fS t        |t              ry y r  r  r   r   r   r   r  s     r<   r  z"GoldenRatio.approximation_interval  1    j'*EE8A;''
H- .ra   N)r   rA  rB  rC  r  r  r)  r  r8   r  r  rF  rN  r  r   r  r  _eval_rewrite_as_sqrtr|   ra   r<   r  r    sS    8 GKKMILI"
' .ra   r  c                  X    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eZy)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
    TFr|   c                     y)Nz\text{TribonacciConstant}r|   rL  s     r<   rN  zTribonacciConstant._latex@  s    +ra   c                     yr  r|   r   s    r<   r  zTribonacciConstant.__int__C  r  ra   c                8    | j                  |      j                  S r7   )r   rs   r   s     r<   r   zTribonacciConstant._as_mpf_valF  s    %+++ra   c                d    | j                  d      j                  |dz         }t        ||      S )NT)functionrR  r  )r  r   rM   r~  s      r<   r   zTribonacciConstant._eval_evalfI  s1    ##T#2>>taxHR4((ra   c                p    ddl m}m} d |dd |d      z  z
        z    |dd |d      z  z         z   dz  S )Nr   )cbrtr  r      rQ  !   )r  r  r  )r   r  r  r  s       r<   r  z$TribonacciConstant._eval_expand_funcM  s<    GDaRj))DaRj,AAQFFra   c                |    t        |t              rt        j                  t	        d      fS t        |t              ry y r  r  r  s     r<   r  z)TribonacciConstant.approximation_intervalQ  r  ra   N)r   rA  rB  rC  r  r  r)  r  r8   r  r  rF  rN  r  r   r   r  r  r  r|   ra   r<   r   r     sZ    "H GKKMILI,,)G .ra   r   c                  @    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y)

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
    TFNr|   c                     y)Nz\gammar|   rL  s     r<   rN  zEulerGamma._latex  rO  ra   c                     yr  r|   r   s    r<   r  zEulerGamma.__int__  r  ra   c                    t         j                  j                  |dz         }t        j                  || dz
        }t	        ||      S r  )r   libhypereuler_fixedr  rm   r   rg   r  rl   s       r<   r   zEulerGamma._as_mpf_val  s?    MM%%dRi0q4%"*-D!!ra   c                    t        |t              r t        j                  t        j                  fS t        |t
              rt        j                  t        ddd      fS y )NrQ  rG   r   )r  r   r   r  r   r   r  r  s     r<   r  z!EulerGamma.approximation_interval  sE    j'*FFAEE?"
H-FFHQ1-.. .ra   )r   rA  rB  rC  r  r  r)  r  r8   rF  rN  r  r   r  r|   ra   r<   r  r  Z  s;    > GKKMII"/ra   r  c                  H    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
 Zy)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
    TFNr|   c                     yr  r|   r   s    r<   r  zCatalan.__int__  r  ra   c                ~    t        j                  |dz         }t        j                  || dz
        }t        ||      S r  )r   catalan_fixedr  rm   r  s       r<   r   zCatalan._as_mpf_val  s;    tby)q4%"*-D!!ra   c                    t        |t              r t        j                  t        j                  fS t        |t
              rt        ddd      t        j                  fS y )N	   rF   r   )r  r   r   r  r   r   r  s     r<   r  zCatalan.approximation_interval  sE    j'*FFAEE?"
H-QA&.. .ra   c                    ||| S ddl m} ddlm}  |ddd      } |t        j
                  |z  d|z  dz   dz  z  |dt        j                  f      S )	Nr   r  r   )Sumr  T)integernonnegativer2   )r  r  sympy.concrete.summationsr  r   r*  r   )r   k_symr   r  r  r  r  s          r<   _eval_rewrite_as_SumzCatalan._eval_rewrite_as_Sum  sX    7#6K!1#t61==!#qs1uqj01a2DEEra   c                     y)NGr|   rL  s     r<   rN  zCatalan._latex  s    ra   r  )r   rA  rB  rC  r  r  r)  r  r8   rF  r  r   r  r!  rN  r|   ra   r<   r  r    sA    6 GKKMII"/Fra   r  c                  n    e Zd ZdZdZdZdZdZdZdZ	e
ZdZd Zed        Zd Zd Zd	 Zd
 Zed        Zy)r  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
    TFr|   c                     |j                   d   S )Nimaginary_unit_latex)	_settingsrL  s     r<   rN  zImaginaryUnit._latex  s      !788ra   c                 "    t         j                  S r7   r2  r|   ra   r<   r  zImaginaryUnit.__abs__  r:  ra   c                    | S r7   r|   r   s     r<   r   zImaginaryUnit._eval_evalf  r   ra   c                $    t         j                   S r7   )r   r  r   s    r<   r   zImaginaryUnit._eval_conjugate   s    ra   c                   t        |t              rZ|dz  }|dk(  rt        j                  S |dk(  rt        j                  S |dk(  rt        j
                  S |dk(  rt        j                   S t        |t              rNt        |d      \  }}t        t        j                  |d      }|dz  rt        t        j
                  |d      S |S y)	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
        rR  r   r   r2   rQ  Fr  N)
rH   r   r   r   r  r*  r   r   r  r  )r   rj   r;   r   rl   s        r<   r  zImaginaryUnit._eval_power  s     dG$!8Dqyuu&}}$''dH%$?DAqQ__a%8B1u1=="u==I &ra   c                B    t         j                  t         j                  fS r7   )r   r*  r  r   s    r<   as_base_expzImaginaryUnit.as_base_exp   s    }}aff$$ra   c                V    t        d      j                  t        d      j                  fS )Nr   r   )rM   rs   r   s    r<   _mpc_zImaginaryUnit._mpc_#  s    aa//ra   N)r   rA  rB  rC  rD  is_imaginaryr~   r8   r  r  r   rG  rF  rN  r5  r  r   r   r  r-  r  r/  r|   ra   r<   r  r    ss    , NLIILDI9   :% 0 0ra   r  c                    t        | t        t        f      ryt        |       t        u r| j                         S t        | t              ryt        | t              r| j                  d   dk\  S y)zreturn True only for a literal Number whose internal
    representation as a fraction has a denominator of 1,
    else False, i.e. integer, with no fractional part.
    Tr2   r   F)	rH   r   rS   r   r   rS  r   rM   rs   r  s    r<   r  r  +  sZ    
 !j#&'Aw%||~!W!UwwqzQra   c                B   t        |       } t        |      }| j                  s|j                  s| |k(  S | j                  r%|j                  r| j                  |j                  k(  S | j                  r|| }} | j                  sy|j                  \  }}}}| j                  | j
                  }}|r| }|dk(  r|dk(  xr ||k(  S |dkD  r3|dk7  ry|j                         |j                         |z   k7  ry||z  |k(  S ||k7  ry| }|j                         dz
  |k7  ryd|z  |k(  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)
    False

    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   )r
   r  rs   r(  rv   rx   
bit_length)	r  r  rh   ri   rp  r\   rv   rx   neg_exps	            r<   r  r  <  s&   j 	AA ::ajjAv	


ww!''!!	
!1== D#sA33qAd
axAv"#("	q6<<>S^^-33czQ 8$<<>A(W""ra   c                r    t         t        ffdfd t        |       t        |      ||      S )a  Return True if expr1 and expr2 are numerically close.

    The expressions must have the same structure, but any Rational, Integer, or
    Float numbers they contain are compared approximately using rtol and atol.
    Any other parts of expressions are compared exactly.

    Relative tolerance is measured with respect to expr2 so when used in
    testing expr2 should be the expected correct answer.

    Examples
    ========

    >>> from sympy import exp
    >>> from sympy.abc import x, y
    >>> from sympy.core.numbers import all_close
    >>> expr1 = 0.1*exp(x - y)
    >>> expr2 = exp(x - y)/10
    >>> expr1
    0.1*exp(x - y)
    >>> expr2
    exp(x - y)/10
    >>> expr1 == expr2
    False
    >>> all_close(expr1, expr2)
    True
    c                   t        |       }t        |      }||k7  ry|r)t        t        | |z
        t        |      z  z   k        S | j                  r| |k(  S | j                  |j                  k7  s+t        | j                        t        |j                        k7  ry| j                  s| j                  r 	| |      S t        | j                  |j                        }t        fd|D              S )NFc              3  :   K   | ]  \  }} ||        y wr7   r|   )r:   a1a2
_all_closeatolrtols      r<   r=   z0all_close.<locals>._all_close.<locals>.<genexpr>  s      I&"bz"b$5Is   )rH   r   rT   is_Atomr
  r   r   r  is_MulrQ   rK   )
expr1expr2r=  r<  num1num2r   	NUM_TYPESr;  _all_close_acs
     ``   r<   r;  zall_close.<locals>._all_close  s    %+%+4<EEM*dT#e*_.DDEE]]E>!ZZ5::%UZZC

O)K\\U\\ tT::uzz5::.DIDIIIra   c                    t        |j                        }| j                  D ]4  }t        |      D ]#  \  }} ||||      s|j                  |        3  y yr  )ra  r   r   pop)	r@  rA  r=  r<  args2arg1r;   arg2r;  s	           r<   rE  z all_close.<locals>._all_close_ac  sd     UZZ JJ 	D$U+ 4dD$5IIaL
 	 ra   )r   rM   r
   )r@  rA  r=  r<  rD  r;  rE  s       @@@r<   	all_closerK    s5    6 5!IJ" huoxdCCra   c                     yr,  r|   r   s     r<   r  r    r  ra   c                D    t        | j                  | j                  d      S r  )r   r  r  )r   s    r<   sympify_fractionsrN    s    AKK22ra   c                *    t        t        |             S r7   r   rS   r2  s    r<   sympify_mpzrQ        s1vra   c                f    t        t        | j                        t        | j                              S r7   r   rS   r  r  r2  s    r<   sympify_mpqrU    !    AKK(#amm*<==ra   c                *    t        t        |             S r7   rP  r2  s    r<   sympify_fmpzrX    rR  ra   c                f    t        t        | j                        t        | j                              S r7   rT  r2  s    r<   sympify_fmpqrZ    rV  ra   c                V    t        j                  | | j                  j                        S r7   )r   _from_mpmathr\  rg   r2  s    r<   sympify_mpmathr]    s    Q		//ra   c                    t        t        t        | j                  | j                  f            \  }}|t
        j                  |z  z   S r7   )ra  mapr   realimagr   r  )rX   r`  ra  s      r<   sympify_complexrb    s8    c'AFFAFF#345JD$!//$&&&ra   )r  )r  )rl  c                    t         j                  j                  t               t         j                  j                  t               t         j
                  j                  t
               t         j                  j                  t               y r7   )r  r   registerRealrM   r   r  r   r|   ra   r<   _register_classesrf    sN    NNF#LL% h'g&ra   r7   r@  )gh㈵>g:0yE>)
__future__r   r  r   r  rZ  
containersr   r   r   r   r	   r
   r   	singletonr   r   basicr   r  r   r   r  r   cacher   r   
decoratorsr   intfuncr   r   r   r   r   logicr   rG  r   sympy.external.gmpyr   r   r   sympy.multipledispatchr   ru   mpmath.libmplibmpr   r    r!   re   rc   r#   r$   r%   r&   r'   mpmath.ctx_mp_pythonr(   mpmath.libmp.libmpfr)   rl  r*   rm  r+   rk  r,   r-   rd   r.   r/   sympy.utilities.miscr0   
parametersr1   r  _LOG2rD   rm   rp   rq   ry   r   rI   r  r  r  r   r   r   rM   r   r   
RealNumberr   r   rS   r  r"  r'  r  r   r*  r  r   r  r   r   r   r  r  zoorc  r  Er  r  r  r   r  r  r  r  r  r  rK  rN  r  rQ  rU  r   mpzmpqrX  rZ  fmpzfmpqr]  rb  complexpowerr  mulr  identityr  rl  rf  _illegalr|   ra   r<   <module>r     s   "     , , #  "  ' " I I   7 7 +   7 $ : : *   ' )jZ< e	$ }}T]]<013tn&Z n&b	b	?F b	?L ?D C  *7??; 
Xv XvE h E P    Ed EP
'x 
''g 'A?i AH0/Y 0f=/Y =@y @yv yv ZZd1v d1Nq&I qf ee	#t J"jI J"Z 6": 6"rV*<9 V*p FF?> ?>B TT<.,) <.~F. F.R8/ 8/v<li <~Q0J) Q0h OO"^#B<D~ 
% 3 (9 ## $ > +6T($((1+&'-8T($((1a.)* 	> -9T*%**Q-()/;T*%**Q*+,0 -  ' ,    u v'  EE1::q1113D3DEra   