
    Rie                       U d Z ddlmZ ddlmZmZmZm	Z	m
Z
mZmZmZmZm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mZmZmZ ddlmZ ddlm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z? ddlm@ZA dd	lBmCZC dd
lDmEZE ddlFmGZGmHZH ddlImIZI ddlJmKZK ddlLmMZM ddlNmOZO ddlPmQZQ ddlRmSZS er^ddlTmUZU ddlVmWZW ddlXmYZY ddlZm[Z[ ddl\m]Z] ddl^m_Z_ ddl`maZa ddlbmcZc ddldmeZemfZf ddlgmhZhmiZimjZj ddlkmlZlmmZm ddlnmoZo ddlpmqZqmrZrmsZsmtZtmuZu  ejf        d d!          Zve=Zwd" Z@ exe          Zy exe           Zzd#Z{ G d$ d%e|          Z}	 ee~e~e~e~f         Z	 eZeeef         Zdd*Zddd/Zee
e~         e~e~e~f         Zeddd3            Zeddd6            Zddd9Zddd=Zdd@ZddEZddGZddIZddKZddMZddPZddRZddSZdddUZddWZddYZdd[Zdd]Zdd_ZdddZddfZddhZddjZddlZddmZddoZddqZddtZdduZddxZdd{Zdd}Zdd~ZddZddZddZddZddZi aded<   d ZddZddZ G d d          ZddZ	 	 	 dddZdS )z^
Adaptive numerical evaluation of SymPy expressions, using mpmath
for mathematical functions.
    )annotations)
TupleOptionalUnionCallableListDictTypeTYPE_CHECKINGAnyoverloadN)	make_mpcmake_mpfmpmpcmpfnsumquadtsquadoscworkprec)inf) from_intfrom_man_expfrom_rationalfhalffnanfinffninffnonefonefzerompf_absmpf_addmpf_atan	mpf_atan2mpf_cmpmpf_cosmpf_empf_expmpf_logmpf_ltmpf_mulmpf_negmpf_pimpf_powmpf_pow_int	mpf_shiftmpf_sinmpf_sqrt	normalizeround_nearestto_intto_str)bitcount)MPZ)	_infs_nan)dps_to_precprec_to_dps   )sympify)S)
SYMPY_INTS)is_sequence)lambdifyas_int)ExprAddMulPow)SymbolIntegralSumProductexplog)Absreimceilingfloor)atan)FloatRationalIntegerAlgebraicNumberNumber
      c                T    t          t          t          |                               S )z8Return smallest integer, b, such that |n|/2**b < 1.
    )mpmath_bitcountabsint)ns    0lib/python3.11/site-packages/sympy/core/evalf.pyr8   r8   3   s     3s1vv;;'''    iM  c                      e Zd ZdS )PrecisionExhaustedN)__name__
__module____qualname__ rj   ri   rl   rl   B   s        Drj   rl   xOptional[MPF_TUP]returntUnion[int, Any]c                L    | r| t           k    rt          S | d         | d         z   S )a^  Fast approximation of log2(x) for an mpf value tuple x.

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

    Calculated as exponent + width of mantissa. This is an
    approximation for two reasons: 1) it gives the ceil(log2(abs(x)))
    value and 2) it is too high by 1 in the case that x is an exact
    power of 2. Although this is easy to remedy by testing to see if
    the odd mpf mantissa is 1 (indicating that one was dealing with
    an exact power of 2) that would decrease the speed and is not
    necessary as this is only being used as an approximation for the
    number of bits in x. The correct return value could be written as
    "x[2] + (x[3] if x[1] != 1 else 0)".
        Since mpf tuples always have an odd mantissa, no check is done
    to see if the mantissa is a multiple of 2 (in which case the
    result would be too large by 1).

    Examples
    ========

    >>> from sympy import log
    >>> from sympy.core.evalf import fastlog, bitcount
    >>> s, m, e = 0, 5, 1
    >>> bc = bitcount(m)
    >>> n = [1, -1][s]*m*2**e
    >>> n, (log(n)/log(2)).evalf(2), fastlog((s, m, e, bc))
    (10, 3.3, 4)
    rc      )r!   	MINUS_INF)rq   s    ri   fastlogrx   r   s,    >  U

Q4!A$;rj   Fv'Expr' tuple['Number', 'Number'] | Nonec                    |                                  \  }}|r*|                                \  }}|t          j        u r||fS n|r|t          j        fS dS )a  Return a and b if v matches a + I*b where b is not zero and
    a and b are Numbers, else None. If `or_real` is True then 0 will
    be returned for `b` if `v` is a real number.

    Examples
    ========

    >>> from sympy.core.evalf import pure_complex
    >>> from sympy import sqrt, I, S
    >>> a, b, surd = S(2), S(3), sqrt(2)
    >>> pure_complex(a)
    >>> pure_complex(a, or_real=True)
    (2, 0)
    >>> pure_complex(surd)
    >>> pure_complex(a + b*I)
    (2, 3)
    >>> pure_complex(I)
    (0, 1)
    N)as_coeff_Addas_coeff_Mulr?   ImaginaryUnitZero)ry   or_realhtcis         ri   pure_complexr      sf    ( >>DAq ~~1a4K  	 !&y4rj   magSCALED_ZERO_TUPMPF_TUPc                    d S Nrp   r   signs     ri   scaled_zeror          Crj   rg   tTuple[SCALED_ZERO_TUP, int]c                    d S r   rp   r   s     ri   r   r      r   rj   tUnion[SCALED_ZERO_TUP, int]-tUnion[MPF_TUP, tTuple[SCALED_ZERO_TUP, int]]c                ~   t          | t                    r>t          |           dk    r+t          | d          r| d         d         f| dd         z   S t          | t                    rG|dvrt          d          t          t          |           d	}}|dk    rdnd}|gf|dd         z   }||fS t          d
          )al  Return an mpf representing a power of two with magnitude ``mag``
    and -1 for precision. Or, if ``mag`` is a scaled_zero tuple, then just
    remove the sign from within the list that it was initially wrapped
    in.

    Examples
    ========

    >>> from sympy.core.evalf import scaled_zero
    >>> from sympy import Float
    >>> z, p = scaled_zero(100)
    >>> z, p
    (([0], 1, 100, 1), -1)
    >>> ok = scaled_zero(z)
    >>> ok
    (0, 1, 100, 1)
    >>> Float(ok)
    1.26765060022823e+30
    >>> Float(ok, p)
    0.e+30
    >>> ok, p = scaled_zero(100, -1)
    >>> Float(scaled_zero(ok), p)
    -0.e+30
       Tscaledr   r=   N)r=   zsign must be +/-1r   z-scaled zero expects int or scaled_zero tuple.)
isinstancetupleleniszeror@   
ValueErrorr1   r    )r   r   rvpss        ri   r   r      s    4 #u 
J#c((a--F3t4L4L4L-Aq	|c!""g%%	C	$	$ Jw0111$$$bAAAcVbf_1uHIIIrj   r   &tUnion[MPF_TUP, SCALED_ZERO_TUP, None]Optional[bool]c                    |s|  p| d          o| d          S | o6t          | d         t                    o| d         | d         cxk    odk    nc S )Nr=   r   r   )r   list)r   r   s     ri   r   r      sj     5w4c!f*4SW4F:c!fd++FA#b'0F0F0F0FQ0F0F0F0FFrj   resultTMP_RESc                    | t           j        u rt          S | \  }}}}|s|st          S |S |s|S t          |          }t          |          }t	          ||z
  ||z
            }|t	          ||          z
  }| S )a  
    Returns relative accuracy of a complex number with given accuracies
    for the real and imaginary parts. The relative accuracy is defined
    in the complex norm sense as ||z|+|error|| / |z| where error
    is equal to (real absolute error) + (imag absolute error)*i.

    The full expression for the (logarithmic) error can be approximated
    easily by using the max norm to approximate the complex norm.

    In the worst case (re and im equal), this is wrong by a factor
    sqrt(2), or by log2(sqrt(2)) = 0.5 bit.
    )r?   ComplexInfinityINFrx   max)	r   rW   rX   re_accim_accre_sizeim_sizeabsolute_errorrelative_errors	            ri   complex_accuracyr      s     """
#BFF  	J bkkGbkkG6)7V+;<<N#c'7&;&;;N?rj   exprprecoptionsOPT_DICTc           	        t          | |dz   |          }|t          j        u rt          d |d fS |\  }}}}|s||||f\  }}}}|rv| j        r=t          t          t          | |dz                       |dz   |          \  }}	}
}	|d |
d fS d|v rt          j        ||f|          d |d fS t          |           d |d fS |rt          |          d |d fS dS )Nrc   subsNNNN)
evalfr?   r   r   	is_numberrf   Nlibmpmpc_absr"   )r   r   r   r   rW   rX   r   r   abs_expr_accs              ri   get_absr     s   47++F"""T4%%#BFF 8!#VR!7FB	 &> 	/"'AdD1H,=,=(>(>(,q'#; #;HaaT3,,  }b"Xt44dFDHHt99dD$..	 &r{{D&$..%%rj   noc                    |}d}	 t          | ||          }|t          j        u rt          d|dfS ||dd         \  }}|r||k    s|d          |k    r|d|dfS |t	          dd|z            z  }|dz  }o)z/no = 0 for real part, no = 1 for imaginary partr   r=   Nrc      )r   r?   r   r   r   )	r   r   r   r   r   r   resvalueaccuracys	            ri   get_complex_partr     s    H	A	D(G,,!###tT))be!e*x 	/(d**uQxi$.>.>$$..CAqDMM!	Q	rj   'Abs'c                :    t          | j        d         ||          S Nr   )r   argsr   r   r   s      ri   	evalf_absr   .  s    49Q<w///rj   're'c                <    t          | j        d         d||          S r   r   r   r   s      ri   evalf_rer   2      DIaL!T7;;;rj   'im'c                <    t          | j        d         d||          S Nr   r=   r   r   s      ri   evalf_imr   6  r   rj   rW   rX   c                J   | t           k    r|t           k    rt          d          | t           k    rd |d |fS |t           k    r| d |d fS t          |           }t          |          }||k    r|}|t          ||z
   d          z   }n|}|t          ||z
   d          z   }| |||fS )Nz&got complex zero with unknown accuracyr   )r!   r   rx   min)rW   rX   r   size_resize_imr   r   s          ri   finalize_complexr   :  s    	U{{rU{{ABBB	uRt##	u4t##bkkGbkkGg/0!444g/0!444r66!!rj   r   c                |   | t           j        u r| S | \  }}}}|r%|t          vrt          |          | dz   k     rd\  }}|r%|t          vrt          |          | dz   k     rd\  }}|rP|rNt          |          t          |          z
  }|dk     r||z
  | dz   k    rd\  }}|dk     r||z
  |dz
  k    rd\  }}||||fS )z.
    Chop off tiny real or complex parts.
    r   NNrc   )r?   r   r:   rx   )r   r   rW   rX   r   r   deltas          ri   
chop_partsr   M  s    !!!"BFF	  b	!!wr{{dUQY'>'>
F	  b	!!wr{{dUQY'>'>
F	 $b $gbkk)A::56>dUQY66#JBA::56>TAX55#JBr66!!rj   c                T    t          |          }||k     rt          d| z            d S )NzFailed to distinguish the expression: 

%s

from zero. Try simplifying the input, using chop=True, or providing a higher maxn for evalf)r   rl   )r   r   r   as       ri   check_targetr   c  s>      A4xx  "&)-"/ 0 0 	0 xrj   !tUnion[TMP_RES, tTuple[int, int]]c                   ddl m}m} d}t          | |          }|t          j        u rt          d          |\  }}	}
}|r3|	r1t          t          |          |
z
  t          |	          |z
            }n0|rt          |          |
z
  }n|	rt          |	          |z
  }n|rdS dS d}|| k    r||z   |z   t          |           \  }}	}
}n|dfd}d\  }}}}|r | || d          |          \  }}|	r | || d          |	          \  }}|rFt          t          |pt                              t          t          |pt                              fS ||||fS )z
    With no = 1, computes ceiling(expr)
    With no = -1, computes floor(expr)

    Note: this function either gives the exact result or signals failure.
    r   rW   rX   r   z+Cannot get integer part of Complex Infinityr   r   r   rb   re_imrz   nexprr   c                6   ddl m} |\  }}}}|dk    }t          t          |t                              }|rt          | |z
  d          \  }}}	}
|rJ t          |           dz   }|k    rt          | |          \  }}}	}
|rJ |}t          t          |t                              }|\  }}}}|dk    }|sF                    dd          }|rd}|                                D ]^}	 t          |d	           # t          $ r= 	 d
 |                                D              Y @# t          t          f$ r d}Y Y  nw xY ww xY w|r|                     |          }  || | d          } t          | d          \  }}}}	 t          | |d |d fd           n3# t          $ r& |                     d          st          t"          }Y nw xY w|t          t%          |pt"          t"                    k    z            z  }t'          |          }|t(          fS )Nr=   rF   r   rb   rc   r   FTstrictc                0    g | ]}t          |d           S )Fr   rC   ).0r   s     ri   
<listcomp>z7get_integer_part.<locals>.calc_part.<locals>.<listcomp>  s%    OOOVAe444OOOrj   evaluaterv   )addrG   rg   r6   rndr   rx   getvaluesrD   r   as_real_imagAttributeErrorr   r   rl   equalsr!   r&   r   r   )r   r   rG   r   exponentis_intnintireiimire_acciim_accsizenew_expr   doitry   rq   x_accr   r   r   s                     ri   	calc_partz#get_integer_part.<locals>.calc_part  s   !1hQ6%%%&& 	" */b'*+ *+&CgwNNNCLL=1$Dd{{-24.* .**S'7veS))**D"Aq'1\F 	? FE**A *  	" 	"A"q/////% " " ""OOann>N>NOOOO$H *N; " " "#(D!EEE"	"  *!JJqMMECuu555E"5"g66NAq%UQeT$:A>>>>%   ||A -,, CGAJ66"<=>>>D~~Sys<   .D  
ED++E>EEEF -GGFr   )r   rz   r   r   )$sympy.functions.elementary.complexesrW   rX   r   r?   r   r   r   rx   rg   r6   r!   )r   r   r   return_intsrW   rX   assumed_sizer   r   r   r   r   gapmarginr  re_im_r   r   r   s    ``                @ri   get_integer_partr  k  s    <;;;;;;;L4w//F"""FGGG!'Cgw  *s *'#,,('#,,*@AA	 	*cllW$	 *cllW$  	*4))F
vg~~$s*%*$&! &!"S'77 
5 5 5 5 5 5 5 5n  6Cff
 ?i4% 8 8 8#>>V
 ?i4% 8 8 8#>>V D6#,''((#fS\E.B.B*C*CCCVV##rj   	'ceiling'c                :    t          | j        d         d|          S r   r  r   r   s      ri   evalf_ceilingr    s    DIaL!W555rj   'floor'c                :    t          | j        d         d|          S )Nr   r   r  r   s      ri   evalf_floorr    s    DIaL"g666rj   'Float'c                    | j         d |d fS r   )_mpf_r   s      ri   evalf_floatr    s    :tT4''rj   
'Rational'c                @    t          | j        | j        |          d |d fS r   )r   r   qr   s      ri   evalf_rationalr    s!    ..dD@@rj   	'Integer'c                4    t          | j        |          d |d fS r   )r   r   r   s      ri   evalf_integerr    s    DFD!!4t33rj   termsr   target_prec=tTuple[tUnion[MPF_TUP, SCALED_ZERO_TUP, None], Optional[int]]c                N   d | D             } | sdS t          |           dk    r| d         S g }ddlm} | D ]C} |j        |d         d          }|t          j        u s|j        r|                    |           D|r-ddlm	} t           || |dz   i           }|d         |d         fS d|z  }	d	\  }
}g }| D ]\  }}|\  }}}}|r| }|                    ||z   |z
             ||z
  }||k    r9||	k    r*|
r#|t          t          |
                    z
  |	k    r|}
|}g|
||z  z  }
p| }||z
  |	k    r|
s||}}
|
|z  |z   }
|}t          |          }|
st          |          S |
dk     rd}|
 }
nd}t          |
          }||z   |z
  }t          ||
|||t                     |f}|S )
a'  
    Helper for evalf_add. Adds a list of (mpfval, accuracy) terms.

    Returns
    =======

    - None, None if there are no non-zero terms;
    - terms[0] if there is only 1 term;
    - scaled_zero if the sum of the terms produces a zero by cancellation
      e.g. mpfs representing 1 and -1 would produce a scaled zero which need
      special handling since they are not actually zero and they are purposely
      malformed to ensure that they cannot be used in anything but accuracy
      calculations;
    - a tuple that is scaled to target_prec that corresponds to the
      sum of the terms.

    The returned mpf tuple will be normalized to target_prec; the input
    prec is used to define the working precision.

    XXX explain why this is needed and why one cannot just loop using mpf_add
    c                <    g | ]}t          |d                    |S )r   )r   )r   r   s     ri   r   zadd_terms.<locals>.<listcomp>  s'    2221VAaD\\2Q222rj   r   r=   r   r]   rF   r   rc   r   )r   numbersr]   _newr?   NaNis_infiniteappendr   rG   r   r8   rf   r   r   r4   r   )r  r   r   specialr]   r   argrG   r   working_precsum_mansum_expabsolute_errrq   r   r   manrT   bcr   r   sum_signsum_bcsum_accuracyrs                            ri   	add_termsr6    sn   0 32222E z	UqQx G    ej1q!!!%<<3?<NN3 33=$(B//!ube|T6LGW L  8c3 	$CBHx/000g'>> %% &#g,,///,>>C5L)FErzL(( 0'*CWG"e+s2&&N +>***{{(gFV#n4L(GWfk	 		AHrj   'Add'c                ^   t          |           }|r7|\  }}t          |          \  }}}}t          |          \  }	}}
}||	||
fS                     dt                    }d}}	 t	          |dz            d<   fd| j        D             }|                    t          j                  }|dk    rt          d d fS t          d |D             |          \  }}t          d |D             |          \  }	}
|dk    rC|t          t          t          fv s|	t          t          t          fv rt          d d fS t          j        S t          ||	||
f          }||k    r)                    d          rt          d	|d
||
           nX|z
  d         k    rnHt          dd|z  z   ||z
            z   |dz  }                    d          rt          d           y|d<   t!          |d          rt#          |          }t!          |	d          rt#          |	          }	||	||
fS )Nmaxprecr   r=   rc   c                8    g | ]}t          |d z             S )rb   )r   )r   r+  r   r   s     ri   r   zevalf_add.<locals>.<listcomp>X  s)    BBBCsD2Iw//BBBrj   c                ^    g | ]*}t          |t                    |d          |d dd         +S )r   Nrc   r   r   r   r   s     ri   r   zevalf_add.<locals>.<listcomp>]  :    EEEz!U';';E!EQqt!tWEEErj   c                ^    g | ]*}t          |t                    |d          |d dd         +S )r=   Nrc   r<  r=  s     ri   r   zevalf_add.<locals>.<listcomp>_  r>  rj   verbosezADD: wantedaccurate bits, gotrb   zADD: restarting with precTr   )r   r   r   DEFAULT_MAXPRECr   r   countr?   r   r   r6  r   r   r   printr   r   r   )ry   r   r   r   r   r   rW   r   r   rX   r   
oldmaxprecr   r   r  rh   r   s    ``              ri   	evalf_addrF  I  s   
q//C
 &1 D'22Avq D'22Avq2vv%%Y88J	AK9 QtV44	BBBBB16BBBKK)**66tT))EEeEEEt[Z Z
FEEeEEEt[Z Z
F66dE4(((B42E,E,ET4--$$B788+{{9%% Xm[2FPVWWW{"gi&888#b1a4is):;;;DFA{{9%% 91488879: $GIb __b __r66!!rj   'Mul'c           
        t          |           }|r!|\  }}t          |||          \  }}}}d |d |fS t          | j                  }d}	g }
ddlm} |D ]}t          |||          }|t          j        u r|
                    |           7|d         |d         d}	J |j	        |d         d          }|t          j
        u rt          d |d fc S |j        r|
                    |           |
r*|	rt          d |d fS ddlm} t           ||
 |dz   i           S |	rdS |}|t          |          z   d	z   }t!          d          ddfx}\  }}}t          |          }d}|                    t          j                   g }t%          |          D ]\  }}||k    r0t          |          r!|d
         |z                                  |d
<   ;||k    r|t          j        u rPt          |||          \  }}}}|r|r|                    ||||f           |r||c\  }}}}} n|r||c\  }}}}} |dz  }n dS |d|z  z  }||z  }||z  }||z  }|d|z  k    r||z  }||z  }||z  }|d|z  k    t)          ||           }|dz  dz	  }!|s7t+          |!||t-          |          |t.                    } |dz  rd | d |fS | d |d fS |||f|k    r)|!||t-          |          fdt!          d          ddf}}d}"n4|d         \  }#}$}%}&t)          |t1          |#|$|%|&f                    }|#}|$}d}"||"d          D ]\  }#}$}%}&t)          |t1          |#|$|%|&f                    }|}'t3          ||#|'          }(t3          t5          |          |$|'          })t3          ||$|'          }*t3          ||#|'          }+t7          |(|)|'          }t7          |*|+|'          }|                    d          rt;          d|d|           |dz  rt5          |          |}}||||fS )NFr=   r$  r   TrH   r   r      r   rc   rv   r@  zMUL: wantedrA  )r   r   r   r   r%  r]   r?   r   r)  r&  r'  r   r(  mulrI   r   r9   One	enumerateexpandr   r4   r8   r   r   r,   r-   r#   r   rD  ),ry   r   r   r   r   r   rX   r   r   has_zeror*  r]   r+  r   numrI   r   r,  startr0  rT   r1  last	directioncomplex_factorsr   rW   r   r   mebw_accr   i0wrewimwre_accwim_accuse_precABCDs,                                               ri   	evalf_mulrb  z  sV   
q//C
 &1 D'22AvqRv%%<<D HG    sD'**Q&&&NN6"""!9ay ejA&&!%<<tT))))? 	 NN3 2 	*tT))SS']D1Hb111 &%% C #d))#a'L q661a<'ELCb t99DIKKOD//  399c**9R,,..DH$YY3!%<<!&sL'!B!BB 		*" 		*""BFF#;<<< 	*"$fLQ1a%% 	*"$fLQ1a%NII)))QqS	qq
a1\>!!L C<C,B 1\>!! #uooMaD ( dChsmmT3??q= 	&D#%%dC%% b>U""Chsmm4q#a&&!Q6GBBB *9);&Cgwc&S'7'CDDF FCBBB*9"##*> 	) 	)&Cgw c&S'7'CDDF FC $HC**AS(33AC**AC**AAx((BAx((BB;;y!! 	B-';SAAAq= 	%R[["B2sCrj   'Pow'c                   |}| j         \  }}|j        r8|j        }|st          d |d fS |t	          t          j        t          |          d                    z  }t          ||dz   |          }|t          j
        u r
|dk     rdS |S |\  }}	}
}|r|	st          |||          d |d fS |	rb|s`t          |	||          }|dz  }|dk    r|d |d fS |dk    rd |d |fS |dk    rt          |          d |d fS |dk    rd t          |          d |fS |s|dk     rt          j
        S dS t          j        ||	f||          \  }}	t          ||	|          S t          ||dz   |          }|t          j
        u r|j        r
|dk     rdS |S t"          |t          j        u r|\  }}}}|r2t          j        |pt(          |f|          \  }}	t          ||	|          S |sdS t+          |t(                    r!d t-          t          |          |          d |fS t-          ||          d |d fS |dz  }t          |||          }|t          j
        u rt.          d |d fS |\  }}}}|s|st          d |d fS t1          |          }|dk    r||z  }t          |||          \  }}}}|t          j        u rH|r2t          j        |pt(          |f|          \  }}	t          ||	|          S t7          ||          d |d fS t          ||dz   |          \  }}}}|s)|s'|rt.          d |d fS |d         dk    rt          j
        S dS |rCt          j        |pt(          |pt(          f|pt(          |f|          \  }}	t          ||	|          S |r3t          j        |pt(          |f||          \  }}	t          ||	|          S t+          |t(                    r1t          j        |t(          f||          \  }}	t          ||	|          S t=          |||          d |d fS )	Nrc   rI  r   r   r   r=   rv   rb   )r   
is_Integerr   r    rg   mathrU   rf   r   r?   r   r0   r-   r   mpc_pow_intr   is_RationalNotImplementedErrorHalfmpc_sqrtr!   r+   r3   r   rx   Exp1mpc_expr)   mpc_powmpc_pow_mpfr/   )ry   r   r   r   baserT   r   r   rW   rX   r   r   zcasexreximr   yreyimysizes                       ri   	evalf_powrx    s   KID#
 ~ %5 	*tT)) 	DHSVVQ''(((tTAXw//Q&&&1uu--M!'B 	Lb 	Lr1k22D+tKK 
	;b 
	;B;//Aq5Dqyy$T11qyyQk11qyyqzz4d::qyyWQZZ{:: 	*1uu(())"B8Q55BB44447++F"""? 	Qww--M!! af}}S!Q 	2^S\E3$7>>FB#BD111 	*))#u 	B'#,,55tTAAT""D$44 	BJD3g&&F"""T4%%NCa &3 &T4%%CLLE qyysD'22S!Q qv~~ 	9]CL5##6==FB#BK888sK(($TAA4733NCa &3 & 	*tT))q6Q;;$$%%  5\E3<%(3<%*= B  B444
 	G"CL5##6[IIBB444	U		 G"C<kBBBB444 sC--t[$FFrj   'exp'c                f    ddl m} t           |t          j        | j        d          ||          S )Nr=   rJ   Fr   )powerrK   rx  r?   rl  rT   )r   r   r   rK   s       ri   	evalf_expr|  z  s;    SSE:::D'JJJrj   c                   ddl m}m} t          | |          rt          }nt          | |          rt
          }nt          | j        d         }|dz   }t          |||          \  }}	}
}|	rCd|v r| 	                    |d                   } t          | 
                    |          ||          S |s4t          | |          rt          d|dfS t          | |          rdS t          t          |          }|dk     r |||t                    d|dfS |dk    r||z   }t          |||          \  }}	}
}	  |||t                    }t          |          }| }||z
  |z
  }||k     r|                    d	          r1t          d
|d|d|           t          t!          |d                     ||                    dt"                    k    r|d|dfS ||z  }t          |||          \  }}	}
}|d|dfS )zy
    This function handles sin and cos of complex arguments.

    TODO: should also handle tan of complex arguments.
    r   cossin   r   Nr   r=   rb   r@  zSIN/COSwantedr  r9  )(sympy.functions.elementary.trigonometricr  r  r   r'   r2   ri  r   r   r   _eval_evalfr    rx   r   r   rD  r7   rB  )ry   r   r   r  r  funcr+  xprecrW   rX   r   r   xsizeyrw  r  r   s                    ri   
evalf_trigr    sS    BAAAAAAA!S "	As		 "!!
&)C 2IE"3w77BFF	 9Wwv''AQ]]4(($888 &a 	&tT))3 	&))%% BKKE qyytBc""D$44{{u!&sE7!;!;B'DT3

fEMS(d??{{9%% %i8T5#FFFfQmm$$$w{{9o>>>>$$..SLE%*3w%?%?"BFFdD$&&rj   'log'c           	        t          | j                  dk    r%|                                 } t          | ||          S | j        d         }|dz   }t          |||          }|t          j        u r|S |\  }}}}	||cxu r
n nt          }|r]ddlm}
 ddl	m
} t           | |
|d          d          ||          }t          ||pt          |          }|d         ||d         |fS t          |t                    dk     }t          t          |          |t                     }t#          |          }||z
  |k    r|t          k    r}dd	lm}  |t          j        |d          }t+          |||          \  }}}	}	|t#          |          z
  }t          t          t-          |t.          |                    |t                     }|}|r|t1          |          ||fS |d |d fS )
Nr=   r   rb   )rV   )rU   Fr   rc   rF   )r   r   r  r   r?   r   r!   r  rV   &sympy.functions.elementary.exponentialrU   	evalf_logr%   r&   r*   r"   r   rx   r   rG   NegativeOnerF  r#   r    r.   )r   r   r   r+  r   r   rs  rt  xaccr   rV   rU   rW   rX   imaginary_termr   rG   r   prec2r   s                       ri   r  r    s&   
49~~ayy{{T4)))
)A,CbyH3'**F"""CdA c 
 &<<<<<<>>>>>> CC%(((59994J JsCL5$//!ub"Q%%%c5))A-N	tS	)	)B2;;Dd{X"++c!-u555"3g66S!Q73<<'WWS$6677sCCF &6$<<--4%%rj   'atan'c                    | j         d         }t          ||dz   |          \  }}}}||cxu rn ndS |rt          t          ||t                    d |d fS )Nr   rI  r   )r   r   ri  r$   r   )ry   r   r   r+  rs  rt  reaccimaccs           ri   
evalf_atanr    sv    
&)C"3q'::CeU
cy
 "!!Cs##T455rj   r   dictc                    i }|                                 D ]5\  }}t          |          }|j        r|                    |           }|||<   6|S )z< Change all Float entries in `subs` to have precision prec. )itemsr?   is_Floatr  )r   r   newsubsr   rV  s        ri   
evalf_subsr    sX    G

  1aDD: 	$d##A

Nrj   c                   ddl m}m} d|v r|                     t	          ||d                             } |                                }|d= t          | d          rt          | ||          S t          | t                    rt           ||           ||          S t          | t                    rt           ||           ||          S t          )Nr=   )r]   r_   r   r  )r%  r]   r_   r   r  copyhasattrr   r   floatrg   ri  )r   r   r   r]   r_   newoptss         ri   evalf_piecewiser    s    ''''''''yyD'&/::;;,,..FO4   	.tW---dE"" 	5tdG444dC   	7g666 rj   r   'AlgebraicNumber'c                H    t          |                                 ||          S r   )r   to_root)r   r   r   s      ri   evalf_alg_numr    s    dG,,,rj   r   tUnion[mpc, mpf]c                :   ddl m}m}m} t	          |           } t          | |          s| dk    rt          d          S t          | |          rt          d          S t          | |          rt          d          S t          | ||          }t          |          S )Nr=   )InfinityNegativeInfinityr           r   r   z-inf)	r%  r  r  r   r>   r   r   r   quad_to_mpmath)rq   r   r   r  r  r   r   s          ri   	as_mpmathr  !  s    9999999999

A!T a3hh1vv!X 5zz!%&& 6{{1dG$$F&!!!rj   
'Integral'c           	        | j         d         | j         d         \  }}||k    rdx}}n(j        vr|j        |j        z  r||z
  }|j        rd|}}|                    dt                    }t          |d|z            |d<   t          |dz             5  t          ||dz   |          }t          ||dz   |          }ddlm	}m
} ddlm}	 d	d	gt          t          dfd}
|                    d          dk    r |	dg          } |	dg          } |	d          }                     ||z  |z             |z            }|s'                     ||z  |z             |z            }|st          d          t          dt           j        z  ||         z  |dz   |          }t%          |
||g|          }t          }n+t'          |
||gd          \  }}t)          |j                  }d d d            n# 1 swxY w Y   ||d<   d         r|j        j        }|t.          k    r?t1          t3          t5          ||                               \  }}t1          |          }n7t3          t5          t)          |          z
  |z
  |                     }nd\  }}d         r|j        j        }|t.          k    r?t1          t3          t5          ||                               \  }}t1          |          }n7t3          t5          t)          |          z
  |z
  |                     }nd\  }}||||f}|S )Nr   r=   r9  rc   rI     r~  )WildFr   rz   rs   r  c                R   t          t          j        d	| ii          \  }}}}|pd         d<   |pd         d<   t          t	          |                    t          t	          |                    |rt          |pt          |          S t          |pt                    S )Nr   r   r=   )r   r   r   r   rx   r   r!   r   )
r   rW   rX   r   r   r  	have_partmax_imag_termmax_real_termrq   s
        ri   fzdo_integral.<locals>.fR  s    %*46Aq6:J%K%K"BFF-1IaL-1IaLwr{{;;Mwr{{;;M ,2;+++r{U###rj   quadoscr^  )excluder_  ra  zbAn integrand of the form sin(A*x+B)*f(x) or cos(A*x+B)*f(x) is required for oscillatory quadrature)period)errorr   )r   rz   rs   r  )r   free_symbolsr   r   rB  r   r   r  r  r  r  symbolr  rw   matchr   r?   Pir   r   rx   r  realr!   r   rg   r   imag)r   r   r   xlowxhighdiffrE  r  r  r  r  r^  r_  ra  rT  r  r   quadrature_errorquadrature_errrW   re_sr   rX   im_sr   r  r  r  r  rq   s                            @@@@@ri   do_integralr  /  s   9Q<DYq\NAtUu}}uu	
$#	#	#
  11 	&4<D~ &eY88JZ400GI	$(		 /= /=tby'22%G44 	FEEEEEEE      EN	,5,5	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ ;;v%''S1#&&&AS1#&&&AS		A

33qsQw<<>**A /JJss1Q37||A~.. O  "N O O Oqvad{D2Iw??FQuf===F(%+Ae}A%F%F%F"FN&~';<<_/= /= /= /= /= /= /= /= /= /= /= /= /= /= /=b $GI| 	  & 1;;&sCmEU,V,V+V'W'WXXLD&T""BB#mgbkk9D@BRSSSTTFF
F| 	  & 1;;&sCmEU,V,V+V'W'WXXLD&T""BB#mgbkk9D@BRSSSTTFF
FVV#FMs   E%HHHc                   | j         }t          |          dk    st          |d                   dk    rt          |}d}|                    dt                    }	 t          | ||          }t          |          }||k    rn?||k    rn8|dk    r|dz  }n|t          |d|z            z  }t          ||          }|dz  }f|S )Nr=   r   rv   r9  r   rc   )	limitsr   ri  r   r   r  r   r   r   )	r   r   r   r  r   r   r9  r   r   s	            ri   evalf_integralr    s    [F
6{{a3vay>>Q..!!H	Akk)S))GT8W55#F++twr>> MHHD!Q$'Hx))	Q  Mrj   numerdenomrh   'Symbol'tTuple[int, Any, Any]c                T   ddl m}  || |          } |||          }|                                }|                                }||z
  }|r|ddfS |                                |                                z  }	ddlm}
  |
t          |	          d          s||	dfS |                                |                                cxk    rdk    rn n||	dfS |                                d         }|                                d         }||	||z
  |                                z  fS )aI  
    Returns
    =======

    (h, g, p) where
    -- h is:
        > 0 for convergence of rate 1/factorial(n)**h
        < 0 for divergence of rate factorial(n)**(-h)
        = 0 for geometric or polynomial convergence or divergence

    -- abs(g) is:
        > 1 for geometric convergence of rate 1/h**n
        < 1 for geometric divergence of rate h**n
        = 1 for polynomial convergence or divergence

        (g < 0 indicates an alternating series)

    -- p is:
        > 1 for polynomial convergence of rate 1/n**h
        <= 1 for polynomial divergence of rate n**(-h)

    r   )PolyNr=   )equal_valued)sympy.polys.polytoolsr  degreeLCr%  r  rf   
all_coeffs)r  r  rh   r  npoldpolr   r  rateconstantr  pcqcs                ri   check_convergencer    sD   . +*****4q>>D4q>>DAAq5D  T4wwyy47799$H%%%%%%<Hq)) $Xt##{{}}*********Xq  			1	B			1	BBGTWWYY...rj   rP  c                   ddl m}m} ddlm} |t          d          k    rt          d          |r|                     |||z             }  || |          }|t          d          |                                \  }}	t          ||          t          ||	          t          ||	|          \  }
}}|
dk     rt          d	|
 z            |                     |d          }|j        st          d
          |
dk    s|
dk    rt          |          dk    rt          |j                  |z  |j        z  }|}d}t          |          dk    rY|t           |dz
                      z  }|t           |dz
                      z  }||z  }|dz  }t          |          dk    Yt#          ||           S |dk     }t          |          dk     r"t          dt          d|z            z            |dk     s ||d          r|st          d| z            d}t%          |          }	 d|z  t          |j                  z  |j        z  }|gffd	}t'          |          5  t)          |dt*          gd          }ddd           n# 1 swxY w Y    |||          }|||k    rn||z  }|}|j        S )z
    Sum a rapidly convergent infinite hypergeometric series with
    given general term, e.g. e = hypsum(1/factorial(n), n). The
    quotient between successive terms must be a quotient of integer
    polynomials.
    r=   )r]   r  r   )	hypersimpr   zdoes not support inf precNz#a hypergeometric series is requiredzSum diverges like (n!)^%iz3Non rational term functionality is not implemented.rI  zSum diverges like (%i)^nzSum diverges like n^%iTr   c           	        | rat          |           } |dxx         t           | dz
                      z  cc<   |dxx         t           | dz
                      z  cc<   t          t          |d                              S r   )rg   r9   r   r   )k_termfunc1func2r  s     ri   summandzhypsum.<locals>.summand  s     3AA!HHHEE!a%LL 1 11HHH!HHHUU1q5\\!2!22HHHU1Xv > >???rj   
richardson)method)r%  r]   r  sympy.simplify.simplifyr  r  ri  r   as_numer_denomrB   r  r   rh  rf   r9   r   r  r   r<   r   r   
mpmath_infr  )r   rh   rP  r   r]   r  r  hsrO  denr   gr   termr   r  altvoldndigterm0r  ry   vfr  r  r  s                          @@@ri   hypsumr    s    -,,,,,,,111111uU||!"=>>> 'yyAI&&	4		B	z!"GHHH  ""HCQEQES!,,GAq!1uu4;<<<99Q??D Y!"WXXX 	1uuaCFFQJJDFt#.$ii!mmCa!e%%%DSq1u&&&DIAFA	 $ii!mm
 Au%%%!eq66A::7#ac((BCCCq55\\!Q''555!<===4  	 dFE[[E)df4E"' @ @ @ @ @ @ @ @ $ H H1j/,GGGH H H H H H H H H H H H H H Hq$BDBJJDLDD)	, ws   -JJJ	'Product'c                    t          d | j        D                       r%t          |                                 ||          }n+ddlm} t          |                     |          ||          }|S )Nc              3  B   K   | ]}|d          |d         z
  j         V  dS )r=   rc   N)re  )r   ls     ri   	<genexpr>zevalf_prod.<locals>.<genexpr>   s1      
9
9AaD1Q4K#
9
9
9
9
9
9rj   )r   r   r   rO   )allr  r   r  sympy.concrete.summationsrP   rewrite)r   r   r   r   rP   s        ri   
evalf_prodr    sy    

9
9T[
9
9
999 Ftyy{{w???111111t||C((tWEEEMrj   'Sum'c                &   ddl m} d|v r|                     |d                   } | j        }| j        }t          |          dk    st          |d                   dk    rt          |j        rd d |d fS |dz   }	 |d         \  }}}	|	t          j	        us!|t          j
        u s|t          |          k    rt          t          ||t          |          |          }
|t          |
          z
  }t          |
          dk     rt          ||t          |          |          }
|
d t          ||          d fS # t          $ r  |d          | z  }t          dd	          D ]Y}d
|z  |z  x}}|                     |||d          \  }}|                                }|t          j        u rt          ||k    r nZt          t#          t'          |          d|          d                   }t#          |||          \  }}}}|| }|| }||||fcY S w xY w)Nr=   r$  r   r   rv   rb   ig       @rI  rc   F)rT  rh   epseval_integralr  )r%  r]   r   functionr  r   ri  is_zeror?   r  r  rg   r  rx   r   rangeeuler_maclaurinr   r'  rf   )r   r   r   r]   r  r  r  rh   r   rV  ry   r   r  r   rT  r   errrW   rX   r   r   s                        ri   	evalf_sumr  (  sX   yy))=D[F
6{{a3vay>>Q..!!| &T4%%2IE&)1aAJ!q'9"9"9Q#a&&[[%%4CFFE**wqzz!1::tQA..A$D%(($.. & & &eCjjD5!q! 	 	AqD4KA))A# * % %FAs))++Cae||))czz eCHHb'221566!&q%!9!9B>TF>TF2vv%%%%%&s   :B8D3 3CHHc                &   |d         |          }t          |t                    r|sdS |j        d |d fS d|vri |d<   |d         }|                    | d t          f          \  }}||k    r|S t          t          |          ||          }||f|| <   |S )Nr   r   _cache)r   r   r  r   rw   r   r>   )rq   r   r   valcachecachedcached_precry   s           ri   evalf_symbolr  X  s    
&/!
C#s  	*))y$d**7"" "GH!#iiD)+<==$M'#,,g..t9arj   z?tDict[Type['Expr'], Callable[['Expr', int, OPT_DICT], TMP_RES]]evalf_tablec                    ddl m}  ddlm} ddlm} ddlm} ddlm	}m
}m}m}m}m}	m}
m}m}m}m}m}m} ddlm} dd	lm}m} dd
lm}m}m} ddlm }m!} ddl"m#}m$} ddl%m&} ddl'm(}m)}m*} ddl+m,} i |tZ          |tZ          |t\          |t^          |t`          |d |d |d |d |d |d |
d |d |	d |tb          |td          |td          |tf          |th          |tj          |tl          |tn          |tp          |tr          |tt          |tv          |tx          |tz          |t|          | t~          |t          |t          iaBd S )Nr   rQ   rO   r=   rF   rH   )rl  r]   rj  r   r_   r'  r  rK  r  r^   r   r   r`   rJ   )DummyrL   )rV   rX   rW   rS   rY   )	Piecewise)r\   r  r  rM   c                    d d |d fS r   rp   rq   r   r   s      ri   <lambda>z%_create_evalf_table.<locals>.<lambda>  s    dD$'? rj   c                    t           d |d fS r   r    r  s      ri   r  z%_create_evalf_table.<locals>.<lambda>      tT4&> rj   c                    t           d |d fS r   )r   r  s      ri   r  z%_create_evalf_table.<locals>.<lambda>  s    tT4'@ rj   c                (    t          |          d |d fS r   )r.   r  s      ri   r  z%_create_evalf_table.<locals>.<lambda>  s    fTllD$%E rj   c                (    t          |          d |d fS r   )r(   r  s      ri   r  z%_create_evalf_table.<locals>.<lambda>  s    dT4'F rj   c                    d t           d |fS r   r  r  s      ri   r  z%_create_evalf_table.<locals>.<lambda>  s    tT40H rj   c                    t           d |d fS r   )r   r  s      ri   r  z%_create_evalf_table.<locals>.<lambda>  s    udD$.G rj   c                    t           j        S r   )r?   r   r  s      ri   r  z%_create_evalf_table.<locals>.<lambda>  s	    !2C rj   c                    t           d |d fS r   )r   r  s      ri   r  z%_create_evalf_table.<locals>.<lambda>  r  rj   )Csympy.concrete.productsrR   r  rP   r   rG   rJ  rI   r%  rl  r]   rj  r   r_   r'  r  rK  r  r^   r   r   r`   r{  rK   r  r  rL   r  rV   rX   rW   r  rT   rU   #sympy.functions.elementary.integersrZ   r[   $sympy.functions.elementary.piecewiser  r  r\   r  r  sympy.integrals.integralsrN   r  r  r  r  r|  r  rF  rb  rx  r  r  r   r   r   r  r  r  r  r  r  r  r  ) rR   rP   rG   rI   rl  r]   rj  r   r_   r'  r  rK  r  r^   r   r   r`   rK   r  rL   rV   rX   rW   rT   rU   rZ   r[   r  r\   r  r  rN   s                                    ri   _create_evalf_tabler%  m  s   //////------/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / /%%%%%%%%@@@@@@@@@@????????BBBBBBBB>>>>>>GGGGGGGGGG222222((|( 	{( 	.	(
 	( 	??( 	>>( 	@@( 	EE( 	FF( 	HH( 	GG( 	CC( 	>>(  	Y!($ 	Z%(& 	Z'(* 	YYYYjY
H
H{.Y?O( (KKKrj   c                   ddl m}m} 	 t          t	          |                    } || ||          }n# t
          $ r d|v r)|                     t          ||d                             } |                     |          }|t          t          |dd          }|t           |            \  }	}
 |	j        |          s |
j        |          rt          |	dk    rd}	d}n(|	j        r |	j        |d          j        }	|}nt          |
dk    rd}
d}n(|
j        r |
j        |d          j        }
|}nt          |	|
||f}Y nw xY w|                    d	          rpt!          d
|            t!          dt#          |t$                    rt'          |d         pt(          d          n|           t!          d|           t!                       |                    dd          }|rV|du r|}n?t+          t-          dt/          j        |          z  dz                       }|dk    r|dz  }t3          ||          }|                    d          rt5          | ||           |S )a  
    Evaluate the ``Expr`` instance, ``x``
    to a binary precision of ``prec``. This
    function is supposed to be used internally.

    Parameters
    ==========

    x : Expr
        The formula to evaluate to a float.
    prec : int
        The binary precision that the output should have.
    options : dict
        A dictionary with the same entries as
        ``EvalfMixin.evalf`` and in addition,
        ``maxprec`` which is the maximum working precision.

    Returns
    =======

    An optional tuple, ``(re, im, re_acc, im_acc)``
    which are the real, imaginary, real accuracy
    and imaginary accuracy respectively. ``re`` is
    an mpf value tuple and so is ``im``. ``re_acc``
    and ``im_acc`` are ints.

    NB: all these return values can be ``None``.
    If all values are ``None``, then that represents 0.
    Note that 0 is also represented as ``fzero = (0, 0, 0, 0)``.
    r   r   r   Nr   r  F)
allow_intsr@  z	### inputz
### output2   z### rawchopTg rh
g      @rv   r=   r   )r  rW   rX   r  typeKeyErrorr   r  r  ri  getattrhasr   
_to_mpmathr  r   rD  r   r   r7   r!   rg   roundrf  log10r   r   )rq   r   r   r
  r  rfr5  xer   rW   rX   reprecimprecr)  	chop_precs                  ri   r   r     s   > JIIIIIII #a!Bq$   # # #Wz$8899A]]4  :%%r>488%%B26#;; 	&&"&++ 	&%%99BFF\ 	&t666<BFF%%99BFF\ 	&t666<BFF%%FF";#> {{9 k1lAu9M9MTF1Q4=5"555STUUUi;;vu%%D 
%4<<II
 E&D)9)9"9C"?@@AAIA~~Q	q)$${{8 !Q4   Hs   '3 DEEc                    |t           n|j         }|t          n|j        }| t          j        u rt          | \  }}}}|r|st
          } |||f          S |r ||          S  |t
                    S )z@Turn the quad returned by ``evalf`` into an ``mpf`` or ``mpc``. )r   r   r?   r   ri  r!   )r  ctxr   r   rW   rX   r   s          ri   r  r     s    k((s|Ck((s|CA!!LBAq	  	BsB8}}	 s2wws5zzrj   c                  6    e Zd ZdZdZddZeZd Zd	 ZddZ	dS )
EvalfMixinz$Mixin class adding evalf capability.rp   r  Nd   Fc           	        ddl m}m}	 ||nd}|rt          |          rt	          d          |dk    rVt          | |	          rFddlm}
 |                     d||||||          } |
|          }|	                    d|z
            }|S t          st                       t          |          }t          |t          |t          z                      |||d}|||d	<   |||d
<   	 t          | |dz   |          }n# t           $ r t#          | d	          r+|)|                     |                              |          }n|                     |          }|| cY S |j        s|cY S 	 t          |||          }n# t           $ r |cY cY S w xY wY nw xY w|t*          j        u r|S |\  }}}}|t*          j        u s|t*          j        u rt*          j        S |r0t          t1          ||          d          } |j        ||          }nt*          j        }|rAt          t1          ||          d          } |j        ||          }||t*          j        z  z   S |S )a)  
        Evaluate the given formula to an accuracy of *n* digits.

        Parameters
        ==========

        subs : dict, optional
            Substitute numerical values for symbols, e.g.
            ``subs={x:3, y:1+pi}``. The substitutions must be given as a
            dictionary.

        maxn : int, optional
            Allow a maximum temporary working precision of maxn digits.

        chop : bool or number, optional
            Specifies how to replace tiny real or imaginary parts in
            subresults by exact zeros.

            When ``True`` the chop value defaults to standard precision.

            Otherwise the chop value is used to determine the
            magnitude of "small" for purposes of chopping.

            >>> from sympy import N
            >>> x = 1e-4
            >>> N(x, chop=True)
            0.000100000000000000
            >>> N(x, chop=1e-5)
            0.000100000000000000
            >>> N(x, chop=1e-4)
            0

        strict : bool, optional
            Raise ``PrecisionExhausted`` if any subresult fails to
            evaluate to full accuracy, given the available maxprec.

        quad : str, optional
            Choose algorithm for numerical quadrature. By default,
            tanh-sinh quadrature is used. For oscillatory
            integrals on an infinite interval, try ``quad='osc'``.

        verbose : bool, optional
            Print debug information.

        Notes
        =====

        When Floats are naively substituted into an expression,
        precision errors may adversely affect the result. For example,
        adding 1e16 (a Float) to 1 will truncate to 1e16; if 1e16 is
        then subtracted, the result will be 0.
        That is exactly what happens in the following:

        >>> from sympy.abc import x, y, z
        >>> values = {x: 1e16, y: 1, z: 1e16}
        >>> (x + y - z).subs(values)
        0

        Using the subs argument for evalf is the accurate way to
        evaluate such an expression:

        >>> (x + y - z).evalf(subs=values)
        1.00000000000000
        r=   )r]   ra   Nr  z"subs must be given as a dictionary)_magrc   )r9  r)  r   r@  r   r  r   )r%  r]   ra   rA   	TypeErrorr   r   r<  r   r/  r  r%  r;   r   rg   LG10ri  r  r   r  r   r?   r   r'  r   r&  r   r   )selfrh   r   maxnr)  r   r  r@  r]   ra   r<  r   rT  r   r   r   ry   rW   rX   r   r   r   s                         ri   r   zEvalfMixin.evalf  s   B 	+*******AAB 	BK%% 	B@AAA 66jv..6""""""AtT4wGGBRA!a%BI 	"!!!1~~!$DI77G5 5"GFO"GFO	4733FF" 	 	 	tV$$ +)9IIdOO//55$$T**y[ q$00&    	  Q&&&M!'B;;"++5L 	Cf%%q))AB""BBB 	Cf%%q))AB""B1?***Is=   (C= =AF	F)E;:F;FFFFFc                8    |                      |          }|| }|S )z@Helper for evalf. Does the same thing but takes binary precision)r  )r?  r   r5  s      ri   _evalfzEvalfMixin._evalf  s$    T""9Arj   c                    d S r   rp   )r?  r   s     ri   r  zEvalfMixin._eval_evalf  s    rj   Tc                   d}|r| j         r| j        S t          | d          r"t          |                     |                    S 	 t          | |i           }t          |          S # t          $ r |                     |          }|t          |          |j
        rt          |j                  cY S |                                \  }}|r|j         rt          |j                  }n|j
        r|j        }nt          |          |r|j         rt          |j                  }n|j
        r|j        }nt          |          t          ||f          cY S w xY w)Nzcannot convert to mpmath number_as_mpf_val)re  r   r  r   rE  r   r  ri  r  r   r  r  r   r   r   )r?  r   r'  errmsgr   ry   rW   rX   s           ri   r.  zEvalfMixin._to_mpmath  s   2 	$/ 	6M4'' 	4D,,T22333	&4r**F!&)))" 	& 	& 	&  &&Ay (((z )(((((^^%%FB )bm )bd^^ )X ((( )bm )bd^^ )X (((RH%%%%%)	&s   A& &AE3B EE)r  Nr:  FFNF)T)
rm   rn   ro   __doc__	__slots__r   rh   rB  r  r.  rp   rj   ri   r9  r9    sq        ..Iy y y yv 	A    & & & & & &rj   r9  r  c                <     t          | d          j        |fi |S )a  
    Calls x.evalf(n, \*\*options).

    Explanations
    ============

    Both .n() and N() are equivalent to .evalf(); use the one that you like better.
    See also the docstring of .evalf() for information on the options.

    Examples
    ========

    >>> from sympy import Sum, oo, N
    >>> from sympy.abc import k
    >>> Sum(1/k**k, (k, 1, oo))
    Sum(k**(-k), (k, 1, oo))
    >>> N(_, 4)
    1.291

    T)rational)r>   r   )rq   rh   r   s      ri   r   r     s,    . +71t$$$*188888rj   r  'Optional[Expr]'rT  Optional[OPT_DICT]c                   |K|j         s|j        r|dk    st          d          t          d|z  di           \  }}}}t	          |          }t          | di           \  }}}}t	          |          t	          |          }	}t          ||	          dz   }
t          d||
z   dz             }|pi }t          | ||          S )a%  
    Evaluate *x* to within a bounded absolute error.

    Parameters
    ==========

    x : Expr
        The quantity to be evaluated.
    eps : Expr, None, optional (default=None)
        Positive real upper bound on the acceptable error.
    m : int, optional (default=0)
        If *eps* is None, then use 2**(-m) as the upper bound on the error.
    options: OPT_DICT
        As in the ``evalf`` function.

    Returns
    =======

    A tuple ``(re, im, re_acc, im_acc)``, as returned by ``evalf``.

    See Also
    ========

    evalf

    Nr   zeps must be positiver=   )rh  r  r   r   rx   r   )rq   r  rT  r   r5  r   r   dnrnirh   r   s               ri   _evalf_with_bounded_errorrQ    s    :  	53< 	5a34441S5!R((
1aAJJq!RJAq!Q QZZBBaA 	Aq1uqyAmGAwrj   )rq   rr   rs   rt   )F)ry   rz   rs   r{   )r=   )r   r   rs   r   )r   rg   rs   r   )r   r   rs   r   )r   r   rs   r   )r   r   rs   rt   )r   rz   r   rg   r   r   rs   r   )
r   rz   r   rg   r   rg   r   r   rs   r   )r   r   r   rg   r   r   rs   r   )r   r   r   rg   r   r   rs   r   )r   r   r   rg   r   r   rs   r   )rW   r   rX   r   r   rg   rs   r   )r   r   r   rg   rs   r   )r   rz   r   r   r   rg   )r   rz   r   rg   r   r   rs   r   )r   r  r   rg   r   r   rs   r   )r   r  r   rg   r   r   rs   r   )r   r  r   rg   r   r   rs   r   )r   r  r   rg   r   r   rs   r   )r   r  r   rg   r   r   rs   r   )r  r   r   rg   r   rg   rs   r!  )ry   r7  r   rg   r   r   rs   r   )ry   rG  r   rg   r   r   rs   r   )ry   rc  r   rg   rs   r   )r   ry  r   rg   r   r   rs   r   )ry   rz   r   rg   r   r   rs   r   )r   r  r   rg   r   r   rs   r   )ry   r  r   rg   r   r   rs   r   )r   rg   r   r  rs   r  )r   r  r   rg   r   r   rs   r   )rq   r   r   rg   r   r   rs   r  )r   r  r   rg   r   r   rs   r   )r  rz   r  rz   rh   r  rs   r  )
r   rz   rh   r  rP  rg   r   rg   rs   r   )r   r  r   rg   r   r   rs   r   )r   r  r   rg   r   r   rs   r   )rq   rz   r   rg   r   r   rs   r   r   )r  )Nr   N)
rq   rz   r  rK  rT  rg   r   rL  rs   r   )rG  
__future__r   typingr   tTupler   r   tUnionr   r   r	   tDictr
   r   r   r   rf  mpmath.libmpr   mpmathr   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/   r0   r1   r2   r3   r4   r5   r6   r7   r8   re   mpmath.libmp.backendr9   mpmath.libmp.libmpcr:   mpmath.libmp.libmpfr;   r<   r>   	singletonr?   sympy.external.gmpyr@   sympy.utilities.iterablesrA   sympy.utilities.lambdifyrB   sympy.utilities.miscrD   sympy.core.exprrE   sympy.core.addrG   sympy.core.mulrI   sympy.core.powerrK   sympy.core.symbolrL   r$  rN   r  rP   r!  rR   r  rT   rU   r  rV   rW   rX   r"  rZ   r[   r  r\   r%  r]   r^   r_   r`   ra   r>  r   r  r   rw   rB  ArithmeticErrorrl   rg   r   r   strr   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r6  rF  rb  rx  r|  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  __annotations__r%  r   r  r9  r   rQ  rp   rj   ri   <module>ri     s	     # " " " " "                              G G G G G G G G G G G G G G G G G G G G G G $ $ $ $ $ $< < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < <
 5 4 4 4 4 4 $ $ $ $ $ $ ) ) ) ) ) ) 8 8 8 8 8 8 8 8             * * * * * * 1 1 1 1 1 1 - - - - - - ' ' ' ' ' ' K$$$$$$""""""""""""$$$$$$((((((222222------//////????????@@@@@@@@@@BBBBBBBB======JJJJJJJJJJJJJJtxA( ( ( eJE:+	 	 	 	 	 	 	 	 	 c3#
$&  c?! ! ! !H    > cCc12 
    
	    
$J $J $J $J $JNG G G G G   :& & & &,    0 0 0 0< < < << < < <" " " "&" " " ",0 0 0 0k$ k$ k$ k$ k$\6 6 6 67 7 7 7( ( ( (A A A A4 4 4 4S S S Sl." ." ." ."b{  {  {  { |xG xG xG xGDK K K K
:' :' :' :'z2& 2& 2& 2&j6 6 6 6      "- - - -" " " "\ \ \ \~   4'/ '/ '/ '/TJ J J JZ   '& '& '& '&`   $ PR Q Q Q Q8 8 8vU U U Up   "j& j& j& j& j& j& j& j&Z9 9 9 94 BF'(<@1  1  1  1  1  1  1 rj   