
    g                       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jv        d           Zwe=Zxd! Z@ eye          Zz eye           Z{d"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ddd2            Zeddd5            Zddd8Zddd<Zdd?ZddDZddFZddHZddJZddLZddOZddQZddRZdddTZddVZddXZddZZdd\Zdd^ZddcZddeZddgZddiZddkZddlZddnZddpZddsZddtZddwZddz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    L/var/www/html/ai-engine/env/lib/python3.11/site-packages/sympy/core/evalf.pyr8   r8   3   s     3s1vv;;'''    iM  c                      e Zd ZdS )PrecisionExhaustedN)__name__
__module____qualname__ ri   rh   rk   rk   B   s        Dri   rk   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)
          )r!   	MINUS_INFrp   s    rh   fastlogry   r   s,    >  U

Q4!A$;ri   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)rz   or_realhtcis         rh   pure_complexr      sf    ( >>DAq ~~1a4K  	 !&y4ri   magSCALED_ZERO_TUPMPF_TUPc                    d S Nro   r   signs     rh   scaled_zeror          Cri   rf   tTuple[SCALED_ZERO_TUP, int]c                    d S r   ro   r   s     rh   r   r      r   ri   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        rh   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Iri   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     rh   r   r      sj     5w4c!f*4SW4F:c!fd++FA#b'0F0F0F0FQ0F0F0F0FFri   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INFry   max)	r   rW   rX   re_accim_accre_sizeim_sizeabsolute_errorrelative_errors	            rh   complex_accuracyr      s     """
#BFF  	J bkkGbkkG6)7V+;<<N#c'7&;&;;N?ri   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 )Nru   subsNNNN)
evalfr?   r   r   	is_numberre   Nlibmpmpc_absr"   )r   r   r   r   rW   rX   r   r   abs_expr_accs              rh   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&$..%%ri   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=   Nru      )r   r?   r   r   r   )	r   r   r   r   r   r   resvalueaccuracys	            rh   get_complex_partr     s    H	A	D(G,,!###tT))be!e*x 	/(d**uQxi$.>.>$$..CAqDMM!	Q	ri   'Abs'c                :    t          | j        d         ||          S Nr   )r   argsr   r   r   s      rh   	evalf_absr   .  s    49Q<w///ri   're'c                <    t          | j        d         d||          S r   r   r   r   s      rh   evalf_rer   2      DIaL!T7;;;ri   'im'c                <    t          | j        d         d||          S Nr   r=   r   r   s      rh   evalf_imr   6  r   ri   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   ry   min)rW   rX   r   size_resize_imr   r   s          rh   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!!ri   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   NNru   )r?   r   r:   ry   )r   r   rW   rX   r   r   deltas          rh   
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!!ri   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   rk   )r   r   r   as       rh   check_targetr   c  s>      A4xx  "&)-"/ 0 0 	0 xri   !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_imr{   nexprr   c                   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    }|s                    dd          }|rEd t          fd	|	                                D                       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   ru   r   Fc                    	 t          | d           dS # t          $ r5 	 d |                                 D              Y dS # t          $ r Y Y dS w xY ww xY w)z)Check for integer or integer + I*integer.FstrictTc                0    g | ]}t          |d           S )Fr   rC   ).0r   s     rh   
<listcomp>zLget_integer_part.<locals>.calc_part.<locals>.is_int_reim.<locals>.<listcomp>  s%    OOOVAe444OOOri   )rD   r   as_real_imagrx   s    rh   is_int_reimz8get_integer_part.<locals>.calc_part.<locals>.is_int_reim  s    )q////#t% ) ) ))OOann>N>NOOOO#'44) ) ) )#(555)	)s&    
AA
AAAAc              3  .   K   | ]} |          V  d S r   ro   )r   rz   r   s     rh   	<genexpr>z6get_integer_part.<locals>.calc_part.<locals>.<genexpr>  s+      ::!{{1~~::::::ri   evaluaterv   )addrG   rf   r6   rndr   ry   getallvaluesr   r   rk   equalsr!   r&   r   r   )r   r   rG   r   exponentis_intnintireiimire_acciim_accsizenew_expr   rp   x_accr   r   r   r   s                   @rh   	calc_partz#get_integer_part.<locals>.calc_part  s3   !1hQ6%%%&& 	" */b'*+ *+&CgwNNNCLL=1$Dd{{-24.* .**S'7veS))**D"Aq'1\F 	? FE**A *
) 
) 
) ::::qxxzz::::: *!JJqMMECuu555E"5"g66NAq%UQeT$:A>>>>%   ||A -,, CGAJ66"<=>>>D~~Sys    E -FFFr   )r   r{   r   r   )$sympy.functions.elementary.complexesrW   rX   r   r?   r   r   r   ry   rf   r6   r!   )r   r   r   return_intsrW   rX   assumed_sizer   r  r  r  r  gapmarginr  re_im_r   r   r   s    ``                @rh   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 
6 6 6 6 6 6 6 6p  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##ri   	'ceiling'c                :    t          | j        d         d|          S r   r  r   r   s      rh   evalf_ceilingr    s    DIaL!W555ri   'floor'c                :    t          | j        d         d|          S )Nr   r   r  r   s      rh   evalf_floorr    s    DIaL"g666ri   'Float'c                    | j         d |d fS r   )_mpf_r   s      rh   evalf_floatr    s    :tT4''ri   
'Rational'c                @    t          | j        | j        |          d |d fS r   )r   r   qr   s      rh   evalf_rationalr    s!    ..dD@@ri   	'Integer'c                4    t          | j        |          d |d fS r   )r   r   r   s      rh   evalf_integerr"    s    DFD!!4t33ri   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     rh   r   zadd_terms.<locals>.<listcomp>  s'    2221VAaD\\2Q222ri   r   r=   r   r]   rF   r   ru   r   )r   numbersr]   _newr?   NaNis_infiniteappendr   rG   r   r8   re   r   r   r4   r   )r#  r   r$  specialr]   r   argrG   r   working_precsum_mansum_expabsolute_errrp   r   r   manrT   bcr   r   sum_signsum_bcsum_accuracyrs                            rh   	add_termsr:    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Hri   '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=   ru   c                8    g | ]}t          |d z             S )rb   )r   )r   r/  r   r   s     rh   r   zevalf_add.<locals>.<listcomp>Y  s)    BBBCsD2Iw//BBBri   c                ^    g | ]*}t          |t                    |d          |d dd         +S )r   Nru   r   r   r   r   s     rh   r   zevalf_add.<locals>.<listcomp>^  :    EEEz!U';';E!EQqt!tWEEEri   c                ^    g | ]*}t          |t                    |d          |d dd         +S )r=   Nru   r@  rA  s     rh   r   zevalf_add.<locals>.<listcomp>`  rB  ri   verbosezADD: wantedaccurate bits, gotrb   zADD: restarting with precTr   )r   r   r   DEFAULT_MAXPRECr   r   countr?   r   r   r:  r   r   r   printr   r   r   )rz   r   r   r   r   r   rW   r   r   rX   r   
oldmaxprecr   r$  r#  rg   r   s    ``              rh   	evalf_addrJ  J  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!!ri   '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   ru   rv   rD  zMUL: wantedrE  )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   rH  ),rz   r   r   r   r   r   rX   r   r   has_zeror.  r]   r/  r   numrI   r   r0  startr4  rT   r5  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,                                               rh   	evalf_mulrf  {  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ri   'Pow'c                   |}| j         \  }}|j        r7|j        }|st          d |d fS |t	          t          j        t          |                              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 )	NrM  r   r   r   r=   ru   rv   rb   )r   
is_Integerr   r    rf   mathlog2re   r   r?   r   r0   r-   r   mpc_pow_intr   is_RationalNotImplementedErrorHalfmpc_sqrtr!   r+   r3   r   ry   Exp1mpc_expr)   mpc_powmpc_pow_mpfr/   )rz   r   r   r$  baserT   r   r   rW   rX   r   r   zcasexreximr   yreyimysizes                       rh   	evalf_powr}    s   KID#
 ~ %5 	*tT)) 	DIc!ff%%&&&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Fri   'exp'c                f    ddl m} t           |t          j        | j        d          ||          S )Nr=   rJ   Fr   )powerrK   r}  r?   rq  rT   )r   r   r   rK   s       rh   	evalf_expr  {  s;    SSE:::D'JJJri   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   rD  zSIN/COSwantedr  r=  )(sympy.functions.elementary.trigonometricr  r  r   r'   r2   rn  r   r   r   _eval_evalfr    ry   r   r   rH  r7   rF  )rz   r   r   r  r  funcr/  xprecrW   rX   r   r   xsizeyr|  r  r   s                    rh   
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$&&ri   '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   ru   rF   )r   r   doitr   r?   r   r!   r	  rV   &sympy.functions.elementary.exponentialrU   	evalf_logr%   r&   r*   r"   r   ry   r   rG   NegativeOnerJ  r#   r    r.   )r   r   r   r/  r   r   rx  ry  xaccr   rV   rU   rW   rX   imaginary_termr  rG   r   prec2r   s                       rh   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%%ri   'atan'c                    | j         d         }t          ||dz   |          \  }}}}||cxu rn ndS |rt          t          ||t                    d |d fS )Nr   rM  r   )r   r   rn  r$   r   )rz   r   r   r/  rx  ry  reaccimaccs           rh   
evalf_atanr    sv    
&)C"3q'::CeU
cy
 "!!Cs##T455ri   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   rZ  s        rh   
evalf_subsr    sX    G

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

Nri   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   floatrf   rn  )r   r   r   r]   r_   newoptss         rh   evalf_piecewiser    s    ''''''''yyD'&/::;;,,..FO4   	.tW---dE"" 	5tdG444dC   	7g666 ri   r   'AlgebraicNumber'c                H    t          |                                 ||          S r   )r   to_root)r   r   r   s      rh   evalf_alg_numr    s    dG,,,ri   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)rp   r   r   r  r  r   r   s          rh   	as_mpmathr  "  s    9999999999

A!T a3hh1vv!X 5zz!%&& 6{{1dG$$F&!!!ri   
'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=   r=  ru   rM     r  )WildFr   r{   rr   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   ry   r   r!   r   )
r   rW   rX   r   r   r  	have_partmax_imag_termmax_real_termrp   s
        rh   fzdo_integral.<locals>.fS  s    %*46Aq6:J%K%K"BFF-1IaL-1IaLwr{{;;Mwr{{;;M ,2;+++r{U###ri   quadoscrb  )excluderc  re  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   r{   rr   r  )r   free_symbolsr   r   rF  r   r   r  r  r  r  symbolr  rw   matchr   r?   Pir   r   ry   r  realr!   r   rf   r   imag)r   r   r   xlowxhighdiffrI  r  r  r  r  rb  rc  re  rX  r  r   quadrature_errorquadrature_errrW   re_sr   rX   im_sr   r  r  r  r  rp   s                            @@@@@rh   do_integralr  0  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   r=  r   ru   )	limitsr   rn  r   r   r  r   r   r   )	r   r   r   r  r   r   r=  r   r   s	            rh   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  Mri   numerdenomrg   '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  re   
all_coeffs)r  r  rg   r  npoldpolr   r  rateconstantr  pcqcs                rh   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...ri   rT  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.rM  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   )rf   r9   r   r   )k_termfunc1func2r  s     rh   summandzhypsum.<locals>.summand  s     3AA!HHHEE!a%LL 1 11HHH!HHHUU1q5\\!2!22HHHU1Xv > >???ri   
richardson)method)r)  r]   r  sympy.simplify.simplifyr  r  rn  r   as_numer_denomrB   r  r   rm  re   r9   r   r  r   r<   r   r   
mpmath_infr  )r   rg   rT  r   r]   r  r  hsrS  denr   gr   termr   r  altvoldndigterm0r  rz   vfr  r  r  s                          @@@rh   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=   ru   N)ri  )r   ls     rh   r   zevalf_prod.<locals>.<genexpr>!  s1      
9
9AaD1Q4K#
9
9
9
9
9
9ri   )r   r   r   rO   )r   r  r   r  sympy.concrete.summationsrP   rewrite)r   r   r   r   rP   s        rh   
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Mri   '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       @rM  ru   F)rX  rg   epseval_integralr  )r)  r]   r   functionr  r   rn  is_zeror?   r  r  rf   r  ry   r   rangeeuler_maclaurinr   r+  re   )r   r   r   r]   r  r  r  rg   r   rZ  rz   r   r  r   rX  r   errrW   rX   r   r   s                        rh   	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>   )rp   r   r   valcachecachedcached_precrz   s           rh   evalf_symbolr  Y  s    
&/!
C#s  	*))y$d**7"" "GH!#iiD)+<==$M'#,,g..t9ari   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   )rq  r]   ro  r   r_   r+  r  rO  r  r^   r   r   r`   rJ   )DummyrL   )rV   rX   rW   rS   rY   )	Piecewise)r\   r  r  rM   c                    d d |d fS r   ro   rp   r   r   s      rh   <lambda>z%_create_evalf_table.<locals>.<lambda>  s    dD$'? ri   c                    t           d |d fS r   r    r  s      rh   r  z%_create_evalf_table.<locals>.<lambda>      tT4&> ri   c                    t           d |d fS r   )r   r  s      rh   r  z%_create_evalf_table.<locals>.<lambda>  s    tT4'@ ri   c                (    t          |          d |d fS r   )r.   r  s      rh   r  z%_create_evalf_table.<locals>.<lambda>  s    fTllD$%E ri   c                (    t          |          d |d fS r   )r(   r  s      rh   r  z%_create_evalf_table.<locals>.<lambda>  s    dT4'F ri   c                    d t           d |fS r   r  r  s      rh   r  z%_create_evalf_table.<locals>.<lambda>  s    tT40H ri   c                    t           d |d fS r   )r   r  s      rh   r  z%_create_evalf_table.<locals>.<lambda>  s    udD$.G ri   c                    t           j        S r   )r?   r   r  s      rh   r  z%_create_evalf_table.<locals>.<lambda>  s	    !2C ri   c                    t           d |d fS r   )r   r  s      rh   r  z%_create_evalf_table.<locals>.<lambda>  r  ri   )Csympy.concrete.productsrR   r   rP   r   rG   rN  rI   r)  rq  r]   ro  r   r_   r+  r  rO  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  rJ  rf  r}  r  r  r   r   r   r  r  r  r  r  r  r  r  ) rR   rP   rG   rI   rq  r]   ro  r   r_   r+  r  rO  r  r^   r   r   r`   rK   r  rL   rV   rX   rW   rT   rU   rZ   r[   r  r\   r  r  rN   s                                    rh   _create_evalf_tabler)  n  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( (KKKri   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_intsrD  z	### inputz
### output2   z### rawchopTg rh
g      @rv   r=   r   )r	  rW   rX   r  typeKeyErrorr   r  r  rn  getattrhasr   
_to_mpmathr  r   rH  r   r   r7   r!   rf   roundrj  log10r   r   )rp   r   r   r  r  rfr9  xer   rW   rX   reprecimprecr-  	chop_precs                  rh   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   rn  r!   )r  ctxr   r   rW   rX   r   s          rh   r  r    s    k((s|Ck((s|CA!!LBAq	  	BsB8}}	 s2wws5zzri   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.ro   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)_magru   )r=  r-  r   rD  r   r  r   )r)  r]   ra   rA   	TypeErrorr   r   r@  r   r3  r  r)  r;   r   rf   LG10rn  r  r   r  r   r?   r   r+  r   r*  r   r   )selfrg   r   maxnr-  r   r  rD  r]   ra   r@  r   rX  r   r   r   rz   rW   rX   r   r   r   s                         rh   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  )rC  r   r9  s      rh   _evalfzEvalfMixin._evalf  s$    T""9Ari   c                    d S r   ro   )rC  r   s     rh   r  zEvalfMixin._eval_evalf  s    ri   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)ri  r   r  r   rI  r   r  rn  r  r   r  r  r   r   r   )rC  r   r+  errmsgr   rz   rW   rX   s           rh   r2  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)
rl   rm   rn   __doc__	__slots__r   rg   rF  r  r2  ro   ri   rh   r=  r=    sq        ..Iy y y yv 	A    & & & & & &ri   r=  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   )rp   rg   r   s      rh   r   r     s,    . +71t$$$*188888ri   r  'Optional[Expr]'rX  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=   )rm  r  r   r   ry   r   )rp   r  rX  r   r9  r   r   dnrnirg   r   s               rh   _evalf_with_bounded_errorrU    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ri   )rp   rq   rr   rs   )F)rz   r{   rr   r|   )r=   )r   r   rr   r   )r   rf   rr   r   )r   r   rr   r   )r   r   rr   r   )r   r   rr   rs   )r   r{   r   rf   r   r   rr   r   )
r   r{   r   rf   r   rf   r   r   rr   r   )r   r   r   rf   r   r   rr   r   )r   r   r   rf   r   r   rr   r   )r   r   r   rf   r   r   rr   r   )rW   r   rX   r   r   rf   rr   r   )r   r   r   rf   rr   r   )r   r{   r   r   r   rf   )r   r{   r   rf   r   r   rr   r   )r   r  r   rf   r   r   rr   r   )r   r  r   rf   r   r   rr   r   )r   r  r   rf   r   r   rr   r   )r   r  r   rf   r   r   rr   r   )r   r   r   rf   r   r   rr   r   )r#  r   r   rf   r$  rf   rr   r%  )rz   r;  r   rf   r   r   rr   r   )rz   rK  r   rf   r   r   rr   r   )rz   rg  r   rf   rr   r   )r   r~  r   rf   r   r   rr   r   )rz   r{   r   rf   r   r   rr   r   )r   r  r   rf   r   r   rr   r   )rz   r  r   rf   r   r   rr   r   )r   rf   r   r  rr   r  )r   r  r   rf   r   r   rr   r   )rp   r   r   rf   r   r   rr   r  )r   r  r   rf   r   r   rr   r   )r  r{   r  r{   rg   r  rr   r  )
r   r{   rg   r  rT  rf   r   rf   rr   r   )r   r  r   rf   r   r   rr   r   )r   r  r   rf   r   r   rr   r   )rp   r{   r   rf   r   r   rr   r   r   )r  )Nr   N)
rp   r{   r  rO  rX  rf   r   rP  rr   r   )rK  
__future__r   typingr   tTupler   r   tUnionr   r   r	   tDictr
   r   r   r   rj  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   rd   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   rk  rB  r   r  r   rw   rF  ArithmeticErrorrk   rf   r   r   strr   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r"  r:  rJ  rf  r}  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  __annotations__r)  r   r  r=  r   rU  ro   ri   rh   <module>rm     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ty}}( ( ( 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l$ l$ l$ l$ l$^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 ri   