
    Qi                       U d Z ddlmZ ddl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'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8 ddl
m9Z: dd	l;m<Z< dd
l=m>Z> ddl?m@Z@mAZA ddlBmBZB ddlCmDZD ddlEmFZF ddlGmHZH ddlImJZJ ddlKmLZL er^ddlMmNZN ddlOmPZP ddlQmRZR ddlSmTZT ddlUmVZV ddlWmXZX ddlYmZZZ ddl[m\Z\ ddl]m^Z^m_Z_ ddl`maZambZbmcZc ddldmeZemfZf ddlgmhZh ddlimjZjmkZkmlZlmmZmmnZn  e	jo        d           Zpe5Zqd! Z9 ere          Zs ere           Ztd"Zu G d# d$ev          Zw	 exeyeyeyeyf         Zz	 eZ{e|e}ef         Z~dd)Zddd.Zexeey         eyeyeyf         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)CallableTYPE_CHECKINGAnyoverloadTypeN)	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mpf_tan)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    h/var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/sympy/core/evalf.pyr4   r4   2   s     3s1vv;;'''    iM  c                      e Zd ZdS )PrecisionExhaustedN)__name__
__module____qualname__ re   rd   rg   rg   A   s        Dre   rg   xMPF_TUP | Nonereturn	int | Anyc                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rl   s    rd   fastlogru   r   s,    >  U

Q4!A$;re   FvrA   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)rv   or_realhtcis         rd   pure_complexr      sf    ( >>DAq ~~1a4K  	 !&y4re   magSCALED_ZERO_TUPMPF_TUPc                    d S Nrk   r   signs     rd   scaled_zeror          Cre   rb   tuple[SCALED_ZERO_TUP, int]c                    d S r   rk   r   s     rd   r   r      r   re   SCALED_ZERO_TUP | int%MPF_TUP | tuple[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   r9   N)r9   zsign must be +/-1r   z-scaled zero expects int or scaled_zero tuple.)
isinstancetupleleniszeror<   
ValueErrorr,   r   )r   r   rvpss        rd   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Ire   r    MPF_TUP | SCALED_ZERO_TUP | Nonebool | Nonec                    |s|  p| d          o| d          S | o6t          | d         t                    o| d         | d         cxk    odk    nc S )Nr9   r   r   )r   list)r   r   s     rd   r   r      sj     5w4c!f*4SW4F:c!fd++FA#b'0F0F0F0FQ0F0F0F0FFre   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INFru   max)	r   rS   rT   re_accim_accre_sizeim_sizeabsolute_errorrelative_errors	            rd   complex_accuracyr      s     """
#BFF  	J bkkGbkkG6)7V+;<<N#c'7&;&;;N?re   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 )Nrq   subsNNNN)
evalfr;   r   r   	is_numberra   Nlibmpmpc_absr   )r   r   r   r   rS   rT   r   r   abs_expr_accs              rd   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&$..%%re   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   r9   Nrq      )r   r;   r   r   r   )	r   r   r   r   r   r   resvalueaccuracys	            rd   get_complex_partr     s    H	A	D(G,,!###tT))be!e*x 	/(d**uQxi$.>.>$$..CAqDMM!	Q	re   'Abs'c                :    t          | j        d         ||          S Nr   )r   argsr   r   r   s      rd   	evalf_absr   /  s    49Q<w///re   're'c                <    t          | j        d         d||          S r   r   r   r   s      rd   evalf_rer   3      DIaL!T7;;;re   'im'c                <    t          | j        d         d||          S Nr   r9   r   r   s      rd   evalf_imr   7  r   re   rS   rT   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   ru   min)rS   rT   r   size_resize_imr   r   s          rd   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!!re   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   NNrq   )r;   r   r6   ru   )r   r   rS   rT   r   r   deltas          rd   
chop_partsr   N  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!!re   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   rg   )r   r   r   as       rd   check_targetr   d  s>      A4xx  "&)-"/ 0 0 	0 xre   TMP_RES | tuple[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\  }}}}|%|t          k    r | || d          |          \  }}|	%|	t          k    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   rS   rT   r   z+Cannot get integer part of Complex Infinityr   r   r   r^   re_imrA   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 )Nr9   rB   r   r^   rq   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   r?   ).0r   s     rd   
<listcomp>zLget_integer_part.<locals>.calc_part.<locals>.is_int_reim.<locals>.<listcomp>  s%    OOOVAe444OOOre   )r@   r   as_real_imagrt   s    rd   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   rk   )r   rv   r   s     rd   	<genexpr>z6get_integer_part.<locals>.calc_part.<locals>.<genexpr>  s+      ::!{{1~~::::::re   evaluaterr   )addrC   rb   r1   rndr   ru   getallvaluesr   r   rg   equalsr   r!   r   r   )r   r   rC   r   exponentis_intnintireiimire_acciim_accsizenew_expr   rl   x_accr   r   r   r   s                   @rd   	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NFr   )r   rA   r   r   )$sympy.functions.elementary.complexesrS   rT   r   r;   r   r   r   ru   r   rb   r1   )r   r   r   return_intsrS   rT   assumed_sizer   r   r   r   r   gapmarginr  re_im_r   r   r   s    ``                @rd   get_integer_partr  l  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
3%<<i4% 8 8 8#>>V
3%<<i4% 8 8 8#>>V D6#,''((#fS\E.B.B*C*CCCVV##re   	'ceiling'c                :    t          | j        d         d|          S r   r  r   r   s      rd   evalf_ceilingr    s    DIaL!W555re   'floor'c                :    t          | j        d         d|          S )Nr   r   r  r   s      rd   evalf_floorr    s    DIaL"g666re   'Float'c                    | j         d |d fS r   )_mpf_r   s      rd   evalf_floatr    s    :tT4''re   
'Rational'c                @    t          | j        | j        |          d |d fS r   )r   r   qr   s      rd   evalf_rationalr    s!    ..dD@@re   	'Integer'c                4    t          | j        |          d |d fS r   )r   r   r   s      rd   evalf_integerr    s    DFD!!4t33re   termsr   target_prec3tuple[MPF_TUP | SCALED_ZERO_TUP | None, int | None]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     rd   r   zadd_terms.<locals>.<listcomp>  s'    2221VAaD\\2Q222re   r   r9   r   rY   rB   r   rq   r   )r   numbersrY   _newr;   NaNis_infiniteappendr   rC   r   r4   ra   r   r   r/   r   )r  r   r  specialrY   r   argrC   r   working_precsum_mansum_expabsolute_errrl   r   r   manrP   bcr   r   sum_signsum_bcsum_accuracyrs                            rd   	add_termsr5    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Hre   '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   r9   rq   c                8    g | ]}t          |d z             S )r^   )r   )r   r*  r   r   s     rd   r   zevalf_add.<locals>.<listcomp>Z  s)    BBBCsD2Iw//BBBre   c                ^    g | ]*}t          |t                    |d          |d dd         +S )r   Nrq   r   r   r   r   s     rd   r   zevalf_add.<locals>.<listcomp>_  :    EEEz!U';';E!EQqt!tWEEEre   c                ^    g | ]*}t          |t                    |d          |d dd         +S )r9   Nrq   r;  r<  s     rd   r   zevalf_add.<locals>.<listcomp>a  r=  re   verbosezADD: wantedaccurate bits, gotr^   zADD: restarting with precTr   )r   r   r   DEFAULT_MAXPRECr   r   countr;   r   r   r5  r   r   r   printr   r   r   )rv   r   r   r   r~   r   rS   r   r   rT   r   
oldmaxprecr   r  r  rc   r   s    ``              rd   	evalf_addrE  K  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!!re   '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 )NFr9   r#  r   TrD   r   r      r   rq   rr   r?  zMUL: wantedr@  )r   r   r   r   r$  rY   r;   r   r(  r%  r&  r   r'  mulrE   r   r5   One	enumerateexpandr   r/   r4   r   r   r'   r(   r   r   rC  ),rv   r   r   r   r   r~   rT   r   r   has_zeror)  rY   r*  r   numrE   r   r+  startr/  rP   r0  last	directioncomplex_factorsr   rS   r   r   mebw_accr   i0wrewimwre_accwim_accuse_precABCDs,                                               rd   	evalf_mulra  |  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re   '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 )	NrH  r   r   r   r9   rq   rr   r^   )r   
is_Integerr   r   rb   mathlog2ra   r   r;   r   r+   r(   r   mpc_pow_intr   is_RationalNotImplementedErrorHalfmpc_sqrtr   r&   r.   r   ru   Exp1mpc_expr$   mpc_powmpc_pow_mpfr*   )rv   r   r   r  baserP   r   r   rS   rT   r   r   zcasexreximr   yreyimysizes                       rd   	evalf_powrx    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Fre   'exp'c                f    ddl m} t           |t          j        | j        d          ||          S )Nr9   rF   Fr   )powerrG   rx  r;   rl  rP   )r   r   r   rG   s       rd   	evalf_expr|  |  s;    SSE:::D'JJJre   c                6   ddl m}m}m} t	          | |          rt
          }n7t	          | |          rt          }nt	          | |          rt          }nt          | j	        d         }|dz   }t          |||          \  }	}
}}|
rCd|v r|                     |d                   } t          |                     |          ||          S |	sFt	          | |          rt          d|dfS t	          | |          rd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 )zH
    This function handles sin , cos and tan of complex arguments.

    r   )cossintan   r   Nr   r9   r^   r?  zSIN/COS/TANwantedr  r8  )(sympy.functions.elementary.trigonometricr~  r  r  r   r"   r-   r3   ri  r   r   r   _eval_evalfr   ru   r   r   rC  r2   rA  )rv   r   r   r~  r  r  funcr*  xprecrS   rT   r   r   xsizeyrw  r  r   s                     rd   
evalf_trigr    s   
 GFFFFFFFFF!S "	As		 "	Ac		 "!!
&)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%% %mXxucJJJfQmm$$$w{{9o>>>>$$..SLE%*3w%?%?"BFFdD$&&re   '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 )
Nr9   r   r^   )rR   )rQ   Fr   rq   rB   )r   r   doitr   r;   r   r   r  rR   &sympy.functions.elementary.exponentialrQ   	evalf_logr    r!   r%   r   r   ru   r   rC   NegativeOnerE  r   r   r)   )r   r   r   r*  r   r   rs  rt  xaccr   rR   rQ   rS   rT   imaginary_termr   rC   r   prec2r   s                       rd   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%%re   'atan'c                    | j         d         }t          ||dz   |          \  }}}}||cxu rn ndS |rt          t          ||t                    d |d fS )Nr   rH  r   )r   r   ri  r   r   )rv   r   r   r*  rs  rt  reaccimaccs           rd   
evalf_atanr    sv    
&)C"3q'::CeU
cy
 "!!Cs##T455re   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   rU  s        rd   
evalf_subsr     sX    G

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

Nre   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          )Nr9   )rY   r[   r   r  )r$  rY   r[   r   r  copyhasattrr   r   floatrb   ri  )r   r   r   rY   r[   newoptss         rd   evalf_piecewiser    s    ''''''''yyD'&/::;;,,..FO4   	.tW---dE"" 	5tdG444dC   	7g666 re   r   'AlgebraicNumber'c                H    t          |                                 ||          S r   )r   to_root)r   r   r   s      rd   evalf_alg_numr    s    dG,,,re   r   	mpc | mpfc                :   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 )Nr9   )InfinityNegativeInfinityr|   g        r   r   z-inf)	r$  r  r  r|   r:   r   r   r   quad_to_mpmath)rl   r   r   r  r  r|   r   s          rd   	as_mpmathr  &  s    9999999999

A!T a3hh1vv!X 5zz!%&& 6{{1dG$$F&!!!re   
'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   r9   r8  rq   rH     )r~  r  )WildFr   rA   rn   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   r9   )r   r   r   r   ru   r   r   r   )
r   rS   rT   r   r   r  	have_partmax_imag_termmax_real_termrl   s
        rd   fzdo_integral.<locals>.fW  s    %*46Aq6:J%K%K"BFF-1IaL-1IaLwr{{;;Mwr{{;;M ,2;+++r{U###re   quadoscr]  )excluder^  r`  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   rA   rn   r  )r   free_symbolsr   r   rA  r   r   r  r  r~  r  symbolr  rs   matchr   r;   Pir   r   ru   r  realr   r   rb   r   imag)r   r   r   xlowxhighdiffrD  r~  r  r  r  r]  r^  r`  rS  r  r   quadrature_errorquadrature_errrS   re_sr   rT   im_sr   r  r  r  r  rl   s                            @@@@@rd   do_integralr  4  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	%.%.	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ ;;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| 	 #[.;;&sCmEU,V,V+V'W'WXXLD&T""BB#mgbkk9D@BRSSSTTFF
F| 	 #[.;;&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 )Nr9   r   rr   r8  r   rq   )	limitsr   ri  r   r   r  r   r   r   )	r   r   r   r  r   r   r8  r   r   s	            rd   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  Mre   numerdenomrc   rH   tuple[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   )PolyNr9   )equal_valued)sympy.polys.polytoolsr  degreeLCr$  r  ra   
all_coeffs)r  r  rc   r  npoldpolr   r  rateconstantr  pcqcs                rd   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...re   rO  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.
    r9   )rY   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.rH  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   )rb   r5   r
   r   )k_termfunc1func2r  s     rd   summandzhypsum.<locals>.summand  s     3AA!HHHEE!a%LL 1 11HHH!HHHUU1q5\\!2!22HHHU1Xv > >???re   
richardson)method)r$  rY   r  sympy.simplify.simplifyr  r  ri  r   as_numer_denomr>   r  r   rh  ra   r5   r   r  r   r8   r   r   
mpmath_infr  )r   rc   rO  r   rY   r  r  hsrN  denr~   gr   etermtermr   r  altvoldndigterm0r  rv   vfr  r  r  s                           @@@rd   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;<<<IIaOOE Y!"WXXXD 	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 )r9   rq   N)rd  )r   ls     rd   r   zevalf_prod.<locals>.<genexpr>'  s1      
9
9AaD1Q4K#
9
9
9
9
9
9re   )r   r   r   rK   )r   r  r   r  sympy.concrete.summationsrL   rewrite)r   r   r   r   rL   s        rd   
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Mre   '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)Nr9   r#  r   r   rr   r^   ig       @rH  rq   F)rS  rc   epseval_integralr  )r$  rY   r   functionr  r   ri  is_zeror;   r  r  rb   r  ru   r   rangeeuler_maclaurinr   r&  ra   )r   r   r   rY   r  r  r  rc   r   rU  rv   r   r  r   rS  r   errrS   rT   r   r   s                        rd   	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   rs   r   r:   )rl   r   r   valcachecachedcached_precrv   s           rd   evalf_symbolr  _  s    
&/!
C#s  	*))y$d**7"" "GH!#iiD)+<==$M'#,,g..t9are   z:dict[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*}m+} ddl,m-}  i |t\          |t\          |t^          |t`          |tb          |d |d |d |d |d |d |
d |d |	d |td          |tf          |tf          i |tf          |th          |tj          |tl          |tn          |tp          |tr          |tt          |tv          |tx          |tz          | t|          |t~          | t          |t          |t          aCd S )Nr   rM   rK   r9   rB   rD   )rl  rY   rj  r{   r[   r&  r  rJ  r  rZ   r|   r   r\   rF   )DummyrH   )rR   rT   rS   rO   rU   )	Piecewise)rX   r~  r  r  rI   c                    d d |d fS r   rk   rl   r   r   s      rd   <lambda>z%_create_evalf_table.<locals>.<lambda>  s    dD$'? re   c                    t           d |d fS r   r   r  s      rd   r  z%_create_evalf_table.<locals>.<lambda>      tT4&> re   c                    t           d |d fS r   )r   r  s      rd   r  z%_create_evalf_table.<locals>.<lambda>  s    tT4'@ re   c                (    t          |          d |d fS r   )r)   r  s      rd   r  z%_create_evalf_table.<locals>.<lambda>  s    fTllD$%E re   c                (    t          |          d |d fS r   )r#   r  s      rd   r  z%_create_evalf_table.<locals>.<lambda>  s    dT4'F re   c                    d t           d |fS r   r  r  s      rd   r  z%_create_evalf_table.<locals>.<lambda>  s    tT40H re   c                    t           d |d fS r   )r   r  s      rd   r  z%_create_evalf_table.<locals>.<lambda>  s    udD$.G re   c                    t           j        S r   )r;   r   r  s      rd   r  z%_create_evalf_table.<locals>.<lambda>  s	    !2C re   c                    t           d |d fS r   )r   r  s      rd   r  z%_create_evalf_table.<locals>.<lambda>  r  re   )Dsympy.concrete.productsrN   r  rL   r   rC   rI  rE   r$  rl  rY   rj  r{   r[   r&  r  rJ  r  rZ   r|   r   r\   r{  rG   r  r  rH   r  rR   rT   rS   r  rP   rQ   #sympy.functions.elementary.integersrV   rW   $sympy.functions.elementary.piecewiser  r  rX   r~  r  r  sympy.integrals.integralsrJ   r  r  r  r  r|  r  rE  ra  rx  r  r  r   r   r   r  r  r  r  r  r  r  r  )!rN   rL   rC   rE   rl  rY   rj  r{   r[   r&  r  rJ  r  rZ   r|   r   r\   rG   r  rH   rR   rT   rS   rP   rQ   rV   rW   r  rX   r~  r  r  rJ   s!                                    rd   _create_evalf_tabler#  s  so   //////------/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / /%%%%%%%%@@@@@@@@@@????????BBBBBBBB>>>>>>LLLLLLLLLLLL222222))|) 	{) 	.	)
 	) 	??) 	>>) 	@@) 	EE) 	FF) 	HH) 	GG) 	CC) 	>>)  	Y!)$ 	Z%)& 	Z') )( 	Z)), 	Y-). 	Y/)0 	Y1)4 	Y5)6 	j7)8 	Y9)< 	H=)> 	H?)@ 	{A)B 	C)F 	.G)H 	YI)J 	K)L 	?M)P 	Q) )KKKre   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          |	sd}	d}n(|	j        r |	j        |d          j        }	|}nt          |
s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   F)
allow_intsr?  z	### inputz
### output2   z### rawchopTg rh
g      @rr   r9   r   )r  rS   rT   r  typeKeyErrorr   r  r  ri  getattrhasr   
_to_mpmathr  r   rC  r   r   r2   r   rb   roundre  log10r   r   )rl   r   r   r	  r
  rfr4  xer   rS   rT   reprecimprecr'  	chop_precs                  rd   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#;; 	&&"&++ 	&%% 	&BFF\ 	&t666<BFF%% 	&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D>=D>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   rS   rT   r   s          rd   r  r    s    k((s|Ck((s|CA!!LBAq	  	BsB8}}	 s2wws5zzre   c                  F    e Zd ZU dZdZded<   dd	ZeZddZddZ	ddZ
dS )
EvalfMixinz$Mixin class adding evalf capability.rk   ztuple[str, ...]	__slots__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
        r9   )rY   r]   Nr  z"subs must be given as a dictionary)_magrq   )r8  r'  r   r?  r   r  r   )r$  rY   r]   r=   	TypeErrorr   r   r;  r   r-  r  r#  r7   r   rb   LG10ri  r  r   r  r   r;   r   r&  r   r%  r|   r{   )selfrc   r   maxnr'  r   r  r?  rY   r]   r;  r   rS  r   r   r   rv   rS   rT   r   r   r   s                         rd   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r   rb   rn   rA   c                8    |                      |          }|| }|S )z@Helper for evalf. Does the same thing but takes binary precision)r  )r>  r   r4  s      rd   _evalfzEvalfMixin._evalf  s$    T""9Are   Expr | Nonec                    d S r   rk   )r>  r   s     rd   r  zEvalfMixin._eval_evalf  s    tre   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)rd  r   r  r
   rE  r   r  ri  r  r   r  r  r   r   r	   )r>  r   r%  errmsgr   rv   rS   rT   s           rd   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  Nr9  FFNF)r   rb   rn   rA   )r   rb   rn   rB  )T)rh   ri   rj   __doc__r8  __annotations__r   rc   rA  r  r,  rk   re   rd   r7  r7    s         ..!#I####y y y yv 	A      & & & & & &re   r7  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   )rl   rc   r   s      rd   r   r     s,    . +71t$$$*188888re   r  rB  rS  OPT_DICT | Nonec                   |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 positiver9   )rh  r  r   r   ru   r   )rl   r  rS  r   r4  r   r   dnrnirc   r   s               rd   _evalf_with_bounded_errorrP    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re   )rl   rm   rn   ro   )F)rv   rA   rn   rw   )r9   )r   r   rn   r   )r   rb   rn   r   )r   r   rn   r   )r   r   rn   r   )r   r   rn   ro   )r   rA   r   rb   r   r   rn   r   )
r   rA   r   rb   r   rb   r   r   rn   r   )r   r   r   rb   r   r   rn   r   )r   r   r   rb   r   r   rn   r   )r   r   r   rb   r   r   rn   r   )rS   r   rT   r   r   rb   rn   r   )r   r   r   rb   rn   r   )r   rA   r   r   r   rb   )r   rA   r   rb   r   r   rn   r   )r   r  r   rb   r   r   rn   r   )r   r  r   rb   r   r   rn   r   )r   r  r   rb   r   r   rn   r   )r   r  r   rb   r   r   rn   r   )r   r  r   rb   r   r   rn   r   )r  r   r   rb   r  rb   rn   r   )rv   r6  r   rb   r   r   rn   r   )rv   rF  r   rb   r   r   rn   r   )rv   rb  r   rb   rn   r   )r   ry  r   rb   r   r   rn   r   )rv   rA   r   rb   r   r   rn   r   )r   r  r   rb   r   r   rn   r   )rv   r  r   rb   r   r   rn   r   )r   rb   r   r  rn   r  )r   r  r   rb   r   r   rn   r   )rl   r   r   rb   r   r   rn   r  )r   r  r   rb   r   r   rn   r   )r  rA   r  rA   rc   rH   rn   r  )
r   rA   rc   rH   rO  rb   r   rb   rn   r   )r   r  r   rb   r   r   rn   r   )r   r  r   rb   r   r   rn   r   )rl   rA   r   rb   r   r   rn   r   r   )r  )Nr   N)
rl   rA   r  rB  rS  rb   r   rK  rn   r   )rG  
__future__r   typingr   r   r   r   r   re  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*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r`   mpmath.libmp.backendr5   mpmath.libmp.libmpcr6   mpmath.libmp.libmpfr7   r8   r:   	singletonr;   sympy.external.gmpyr<   sympy.utilities.iterablesr=   sympy.utilities.lambdifyr>   sympy.utilities.miscr@   sympy.core.exprrA   sympy.core.addrC   sympy.core.mulrE   sympy.core.powerrG   sympy.core.symbolrH   r"  rJ   r  rL   r  rN   r  rP   rQ   r  rR   rS   rT   r   rV   rW   r  rX   r$  rY   rZ   r[   r\   r]   rf  r=  r   r  r   rs   rA  ArithmeticErrorrg   r   rb   r   r   r  strr   ru   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r5  rE  ra  rx  r|  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rH  r#  r   r  r7  r   rP  rk   re   rd   <module>rd     s	     # " " " " " ? ? ? ? ? ? ? ? ? ? ? ? ? ?       G G G G G G G G G G G G G G G G G G G G G G $ $ $ $ $ $W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W W
 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:+	 	 	 	 	 	 	 	 	 S#s"
#&  S>! ! ! !H    > S	3S01 
    
	    
$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
=' =' =' ='@2& 2& 2& 2&j6 6 6 6      "- - - -" " " "\ \ \ \~   4'/ '/ '/ '/TL L L L^   '& '& '& '&`   " KM L L L L9 9 9xU U U Up   "j& j& j& j& j& j& j& j&Z9 9 9 94 ;?'(9=1  1  1  1  1  1  1 re   