
    wg                       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j                  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   ZdVd%ZdWdXd&Zee
e   eeef   ZedYdZd'       ZedYd[d(       ZdY	 d\d)ZdWd]d*Zd^d+Zd_d,Zd`d-Zdad.Zdbd/Zdcd0Zddd1Zded2Zdfd3ZdW	 dgd4Zdhd5Zdid6Zdjd7Zdkd8Zdld9Z	 	 dmd:Zdnd;Zdod<Zdpd=Zdqd>Zdrd?Zdsd@ZdtdAZdudBZd_dCZdvdDZdwdEZdxdFZdxdGZdydHZdzdIZd{dJZd|dKZd}dLZi adMedN<   dO Zd}dPZd~dQZ G dR dS      ZddTZ	 	 	 d	 	 	 	 	 ddUZy)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        |                   S )z8Return smallest integer, b, such that |n|/2**b < 1.
    )mpmath_bitcountabsint)ns    U/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/core/evalf.pyr8   r8   3   s     3s1v;''    iM  c                      e Zd Zy)PrecisionExhaustedN)__name__
__module____qualname__ rh   rg   rj   rj   B   s    rh   rj   c                :    | 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)xs    rg   fastlogrt   r   s%    > U
Q4!A$;rh   c                    | j                         \  }}|r*|j                         \  }}|t        j                  u r||fS y|r|t        j                  fS y)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)vor_realhtcis         rg   pure_complexr      sW    ( >>DAq~~1a4K  
!&&yrh   c                     y Nrn   magsigns     rg   scaled_zeror          rh   c                     y r   rn   r   s     rg   r   r      r   rh   c                   t        | t              r*t        |       dk(  rt        | d      r| d   d   f| dd z   S t        | t              r8|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        rg   r   r      s    4 #u#c(a-F3t4LAq	|c!"g%%	C	$w011$$bAAcVbf_1uHIIrh   c                    |s|  xs | d    xr | d    S | xr+ t        | d   t              xr | d   | d   cxk(  xr dk(  S c S )Nr=   r   r   )r   list)r   r   s     rg   r   r      sV    w4c!f*4SW4F:c!fd+FA#b'0FQ0FF0FFrh   c                    | 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INFrt   max)	resultrV   rW   re_accim_accre_sizeim_sizeabsolute_errorrelative_errors	            rg   complex_accuracyr      s~     """
#BFFJbkGbkG6)7V+;<N#c'7&;;N?rh   c           	        t        | |dz   |      }|t        j                  u r
t        d |d fS |\  }}}}|s||||f\  }}}}|rl| j                  r1t        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 y)Nrp   subsNNNN)
evalfr?   r   r   	is_numberrd   Nlibmpmpc_absr"   )exprprecoptionsr   rV   rW   r   r   abs_expr_accs              rg   get_absr     s    47+F"""T4%%#BFF!#VR!7FB	>>"'AdD1H,=(>(,q'#;HaaT3,, }}b"Xt4dFDHHt9dD$..	r{D&$..%rh   c                    |}d}	 t        | ||      }|t        j                  u r
t        d|dfS ||dd   \  }}|r||k\  s	|d    |kD  r|d|dfS |t	        dd|z        z  }|dz  }b)z/no = 0 for real part, no = 1 for imaginary partr   r=   Nrp      )r   r?   r   r   r   )	r   nor   r   r   r   resvalueaccuracys	            rg   get_complex_partr     s    H	A
D(G,!###tT))be!e*x(d*uQxi$.>$$..CAqDM!	Q rh   c                6    t        | j                  d   ||      S Nr   )r   argsr   r   r   s      rg   	evalf_absr   .  s    499Q<w//rh   c                8    t        | j                  d   d||      S r   r   r   r   s      rg   evalf_rer   2      DIIaL!T7;;rh   c                8    t        | j                  d   d||      S Nr   r=   r   r   s      rg   evalf_imr   6  r   rh   c                   | t         k(  r|t         k(  rt        d      | t         k(  rd |d |fS |t         k(  r| d |d fS t        |       }t        |      }||kD  r|}|t        ||z
   d      z   }n|}|t        ||z
   d      z   }| |||fS )Nz&got complex zero with unknown accuracyr   )r!   r   rt   min)rV   rW   r   size_resize_imr   r   s          rg   finalize_complexr   :  s    	U{rU{ABB	uRt##	u4t##bkGbkGg/0!44g/0!44r66!!rh   c                T   | 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\  }}|rD|rBt        |      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   NNrp   )r?   r   r:   rt   )r   r   rV   rW   r   r   deltas          rg   
chop_partsr   M  s     !!!"BFF	b	!wr{dUQY'>
F	b	!wr{dUQY'>
F	bgbk)A:56>dUQY6#JBA:56>TAX5#JBr66!!rh   c                @    t        |      }||k  rt        d| z        y )NzFailed to distinguish the expression: 

%s

from zero. Try simplifying the input, using chop=True, or providing a higher maxn for evalf)r   rj   )r   r   r   as       rg   check_targetr   c  s2     A4x  "&)-"/ 0 	0 rh   c                \   ddl m}m} d}t        | |      }|t        j
                  u rt        d      |\  }}	}
}|r'|	r%t        t        |      |
z
  t        |	      |z
        }n&|rt        |      |
z
  }n|	rt        |	      |z
  }n|ryyd}|| k\  r||z   |z   t        |       \  }}	}
}n|dfd}d\  }}}}|r | || d	
      |      \  }}|	r | || d	
      |	      \  }}|r8t        t        |xs t                    t        t        |xs 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   rV   rW   r   z+Cannot get integer part of Complex Infinityr   r   r   ra   c                   ddl m} |\  }}}}|dk(  }t        t        |t                    }|rjt        | |z
  d      \  }}}	}
|rJ t        |       dz   }|kD  rt        | |      \  }}}	}
|rJ |}t        t        |t                    }|\  }}}}|dk(  }|sj                  dd      }|r6d t        fd	|j                         D              r| j                  |      }  || | d
      } t        | d      \  }}}}	 t        | |d |d fd       |t        t        |xs t        t              k(  z        z  }t!        |      }|t"        fS # t        $ r  | j                  d      st        t        }Y ew xY w)Nr=   rE   r   ra   rp   r   Fc                    	 t        | d       y# t        $ rC 	 | j                         D cg c]  }t        |d       nc c}w c} Y y# t        $ r Y Y yw xY ww xY w)z)Check for integer or integer + I*integer.F)strictT)rC   r   as_real_imag)rs   r   s     rg   is_int_reimz8get_integer_part.<locals>.calc_part.<locals>.is_int_reim  s\    )q/#% ))>?nn>NOVAe4OO#') )#()	)s1    	AAA 	A	AAAAc              3  .   K   | ]  } |        y wr   rn   ).0rz   r   s     rg   	<genexpr>z6get_integer_part.<locals>.calc_part.<locals>.<genexpr>  s     :!{1~:s   evaluaterq   )addrF   re   r6   rndr   rt   getallvaluesr   r   rj   equalsr!   r&   r   r   )re_imnexprrF   r   exponentis_intnintireiimire_acciim_accsizenew_expr   rs   x_accr   r   r   r   s                   @rg   	calc_partz#get_integer_part.<locals>.calc_part  s   !1hQ6%%& */b'*+&CgwN7CL=1$Dd{-24.**S'7wveS)*D"Aq'1\F FE*A
) :qxxz::!JJqMEuu5E"5"g6NAq%UQeT$:A>
 CGAJ6"<=>>D~Sy & ||A,,s   E &E<;E<Fr   )r   'Expr'r   MPF_TUP)$sympy.functions.elementary.complexesrV   rW   r   r?   r   r   r   rt   re   r6   r!   )r   r   r   return_intsrV   rW   assumed_sizer   r   r   r   r   gapmarginr   re_im_r   r   r   s    ``                @rg   get_integer_partr   k  sV    <L4w/F"""FGG!'Cgw s'#,('#,*@A	clW$	clW$ )F
vg~$s*%*$&!"S'7 
6p  6Cff
4% 8#>V
4% 8#>V6#,'(#fS\E.B*CCCVV##rh   c                6    t        | j                  d   d|      S r   r   r   r   s      rg   evalf_ceilingr     s    DIIaL!W55rh   c                6    t        | j                  d   d|      S )Nr   r   r   r   s      rg   evalf_floorr     s    DIIaL"g66rh   c                "    | j                   d |d fS r   )_mpf_r   s      rg   evalf_floatr    s    ::tT4''rh   c                L    t        | j                  | j                  |      d |d fS r   )r   r   qr   s      rg   evalf_rationalr    s"    .dD@@rh   c                6    t        | j                  |      d |d fS r   )r   r   r   s      rg   evalf_integerr    s    DFFD!4t33rh   c                &   | D cg c]  }t        |d         r| } }| syt        |       dk(  r| d   S g }ddlm} | D ]H  } |j                  |d   d      }|t
        j                  u s|j                  s8|j                  |       J |r#ddl	m
} t         || |dz   i       }|d   |d   fS d|z  }	d\  }
}g }| D ]~  \  }}|\  }}}}|r| }|j                  ||z   |z
         ||z
  }||k\  r/||	kD  r!|
r|t        t        |
            z
  |	kD  r|}
|}Y|
||z  z  }
b| }||z
  |	kD  r|
rp||}}
u|
|z  |z   }
|} t        |      }|
st        |      S |
dk  rd}|
 }
nd}t        |
      }||z   |z
  }t!        ||
|||t"              |f}|S c c}w )	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
    r   r   r=   r\   rE   r   rp   r   )r   r   numbersr\   _newr?   NaNis_infiniteappendr   rF   r   r8   rd   r   r   r4   r   )termsr   target_precr}   specialr\   argrF   r   working_precsum_mansum_expabsolute_errrs   r   r   manrS   bcr   r   sum_signsum_bcsum_accuracyrs                            rg   	add_termsr    s   0 21VAaD\Q2E2	UqQx G  ejj1q!!%%<3??NN3  3=$(B/!ube|T6LGW L 8c3$CBHx/0g'> %#g,//,>C5L)FErzL('*CWG"e+s212 &N>**{(gFV#n4L(GWfk		AHw 3s
   FFc           	        t        |       }|r/|\  }}t        |||      \  }}}}t        |||      \  }	}}
}||	||
fS |j                  dt              }d}|}	 t	        |d|z        |d<   | j
                  D cg c]  }t        ||dz   |       }}|j                  t        j                        }|dk\  r
t        d |d fS t        |D cg c]!  }t        |t              s|d   s|dd d   # c}||      \  }}t        |D cg c]!  }t        |t              s|d   s|dd d   # c}||      \  }	}
|dk(  r@|t        t        t        fv s|	t        t        t        fv r
t        d |d fS t        j                  S t        ||	||
f      }||k\  r!|j                  d      rt!        d|d||
       nH||z
  |d   kD  rn<|t#        dd|z  z   ||z
        z   }|dz  }|j                  d      rt!        d	|       ||d<   t%        |d
      rt'        |      }t%        |	d
      rt'        |	      }	||	||
fS c c}w c c}w c c}w )Nmaxprecr   r=   rp   ra   verbosezADD: wantedaccurate bits, gotzADD: restarting with precTr   )r   r   r   DEFAULT_MAXPRECr   r   countr?   r   r   r  r   r   r   r   r   printr   r   r   )rz   r   r   r   r|   r~   rV   r   r   rW   r   
oldmaxprecr   r  r  r  rf   r   r   s                      rg   	evalf_addr'  J  sk   
q/C
1 D'2Avq D'2Avq2vv%%Y8J	AK
 QtV4	;<66BCsD2Iw/BBKK))*6tT))#Ez!U';!Qqt!tWEt[Z
F#Ez!U';!Qqt!tWEt[Z
F6dE4((B42E,ET4--$$$B78+{{9%m[2FPVW{"gi&88#b1a4is):;;DFA{{9%1487 : $GIb_b_r66!!? C
 FEs*   8H2H7H7$
H7H<H<
H<c           
        t        |       }|r|\  }}t        |||      \  }}}}d |d |fS t        | j                        }d}	g }
ddlm} |D ]  }t        |||      }|t        j                  u r|
j                  |       4|d   |d   d}	A |j                  |d   d      }|t        j                  u rt        d |d fc S |j                  s|
j                  |        |
r%|	r
t        d |d fS ddlm} t         ||
 |dz   i       S |	ry|}|t        |      z   d	z   }t!        d      ddfx}\  }}}t        |      }d}|j                  t        j"                         g }t%        |      D ]  \  }}||k7  r%t        |      r|d
   |z  j'                         |d
<   0||k(  r|t        j"                  u rHt        |||      \  }}}}|r|r|j                  ||||f       t|r||c\  }}}}} n|r||c\  }}}}} |dz  }n y|d|z  z  }||z  }||z  }||z  }|d|z  kD  r||z  }||z  }||z  }|d|z  kD  rt)        ||       } |dz  dz	  }!|s.t+        |!||t-        |      |t.              } |dz  rd | d |fS | d |d fS |||f|k7  r!|!||t-        |      fdt!        d      ddf}}d}"n)|d   \  }#}$}%}&t)        |t1        |#|$|%|&f            }|#}|$}d}"||"d  D ]y  \  }#}$}%}&t)        |t1        |#|$|%|&f            }|}'t3        ||#|'      }(t3        t5        |      |$|'      })t3        ||$|'      }*t3        ||#|'      }+t7        |(|)|'      }t7        |*|+|'      }{ |j9                  d      rt;        d|d|       |dz  rt5        |      |}}||||fS )NFr=   r
  r   TrG   r   r      r   rp   rq   r!  zMUL: wantedr"  )r   r   r   r   r  r\   r?   r   r  r  r  r   r  mulrH   r   r9   One	enumerateexpandr   r4   r8   r   r   r,   r-   r#   r   r%  ),rz   r   r   r   r   r|   rW   r   r   has_zeror  r\   r  r   numrH   r   r  startr  rS   r  last	directioncomplex_factorsr   rV   r   r   mebw_accr   i0wrewimwre_accwim_accuse_precABCDs,                                               rg   	evalf_mulrB  {  sb   
q/C
1 D'2AvqRv%%<D HG  sD'*Q&&&NN6"!9ay ejjA&!%%<tT))??NN3  tT))S']D1Hb11% C #d)#a'L q61a<'ELCb t9DIKKOD/ 39c*R,,.DH$Y3!%%<!&sL'!BB"""BFF#;<"$fLQ1a%"$fLQ1a%NI)QqS	qq
a1\>!L C<C,B 1\>! #uo34 MaDdChsmT3?q=D#%%dC%% b>U"Chsm4q#a&!Q6GBB *9);&Cgwc&S'7'CDFCBBB*9"#*> 	)&Cgw c&S'7'CDFC $HC*AS(3AC*AC*AAx(BAx(B	) ;;y!-';SAq=R["B2sCrh   c                   |}| j                   \  }}|j                  r|j                  }|s
t        d |d fS |t	        t        j                  t        |                  z  }t        ||dz   |      }|t        j                  u r|dk  ry|S |\  }}	}
}|r|	st        |||      d |d fS |	rR|sPt        |	||      }|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 yt        j                  ||	f||      \  }}	t        ||	|      S t        ||dz   |      }|t        j                  u r|j                   r|dk  ry|S t"        |t        j$                  u ru|\  }}}}|r0t        j&                  |xs t(        |f|      \  }}	t        ||	|      S |sy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kD  r||z  }t        |||      \  }}}}|t        j2                  u rB|r0t        j4                  |xs 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 y|rCt        j8                  |xs t(        |xs t(        f|xs t(        |f|      \  }}	t        ||	|      S |r1t        j:                  |xs t(        |f||      \  }}	t        ||	|      S t+        |t(              r-t        j:                  |t(        f||      \  }}	t        ||	|      S t=        |||      d |d fS )	Nr)  r   r   r   r=   rp   rq   ra   )r   
is_Integerr   r    re   mathlog2rd   r   r?   r   r0   r-   r   mpc_pow_intr   is_RationalNotImplementedErrorHalfmpc_sqrtr!   r+   r3   r   rt   Exp1mpc_expr)   mpc_powmpc_pow_mpfr/   )rz   r   r   r  baserS   r   r   rV   rW   r   r   zcasexreximr   yreyimysizes                       rg   	evalf_powrX    s"   KID#
 ~~tT)) 	DIIc!f%&&tTAXw/Q&&&1u-M!'Bbr1k2D+tKKbB;/Aq5Dqy$T11qyQk11qyqz4d::qyWQZ{::1u((()""B8Q5BB4447+F"""??Qw-M!! aff}S!Q^^S\E3$7>FB#BD11)#u'#,5tTAAT"D$44 	BJD3g&F"""T4%%NCa3T4%%CLE qysD'2S!Q qvv~]]CL5##6=FB#BK88sK($TAA473NCa3tT))q6Q;$$$% \E3<%(3<%*=B  B44
""CL5##6[IBB44	U	""C<kBBB44 sC-t[$FFrh   c                j    ddl m} t         |t        j                  | j
                  d      ||      S )Nr=   rI   Fr   )powerrJ   rX  r?   rL  rS   )r   r   r   rJ   s       rg   	evalf_expr[  {  s%    SE:D'JJrh   c                .   ddl m}m} t        | |      rt        }nt        | |      rt
        }nt        | j                  d   }|dz   }t        |||      \  }}	}
}|	r4d|v r| j                  |d         } t        | j                  |      ||      S |s)t        | |      r
t        d|dfS t        | |      ryt        t        |      }|dk  r |||t              d|dfS |dk\  r||z   }t        |||      \  }}	}
}	  |||t              }t        |      }| }||z
  |z
  }||k  rm|j                  d	      r%t        d
|d|d|       t        t!        |d             ||j                  dt"              kD  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=   ra   r!  zSIN/COSwantedr   r   )(sympy.functions.elementary.trigonometricr^  r_  r   r'   r2   rI  r   r   r   _eval_evalfr    rt   r   r   r%  r7   r#  )rz   r   r   r^  r_  funcr  xprecrV   rW   r   r   xsizeyrW  r   r   s                    rg   
evalf_trigrh    s    B!S	As	!!
&&)C 2IE"3w7BFF	Wwv'AQ]]4($88atT))3)%% BKE qyBc"D$44{u!&sE7!;B T3
fEMS(d?{{9%i8T5#FfQm$w{{9o>>$$..SLE%*3w%?"BFFdD$&&rh   c           	     0   t        | j                        dkD  r| j                         } t        | ||      S | j                  d   }|dz   }t        |||      }|t        j
                  u r|S |\  }}}}	||cxu r	n nt        }|rJddlm}
 ddl	m
} t         | |
|d      d      ||      }t        ||xs t        |      }|d   ||d   |fS t        |t              dk  }t        t        |      |t               }t#        |      }||z
  |kD  rq|t        k7  rhdd	lm}  |t        j(                  |d      }t+        |||      \  }}}	}	|t#        |      z
  }t        t        t-        |t.        |            |t               }|}|r|t1        |      ||fS |d |d fS )
Nr=   r   ra   )rU   )rT   Fr   rp   rE   )r   r   doitr   r?   r   r!   r   rU   &sympy.functions.elementary.exponentialrT   	evalf_logr%   r&   r*   r"   r   rt   r   rF   NegativeOner'  r#   r    r.   )r   r   r   r  r   r   rS  rT  xaccr   rU   rT   rV   rW   imaginary_termr   rF   r   prec2r   s                       rg   rl  rl    s   
499~ayy{T4))
))A,CbyH3'*F"""CdA c 
<> C%(594JsCL5$/!ub"Q%%%c5)A-N	tS	)B2;Dd{X"+!--u5"3g6S!Q73<'WWS$67sCF6$<--4%%rh   c                    | j                   d   }t        ||dz   |      \  }}}}||cxu ry |rt        t        ||t              d |d fS )Nr   r)  r   )r   r   rI  r$   r   )rz   r   r   r  rS  rT  reaccimaccs           rg   
evalf_atanrt    s[    
&&)C"3q':CeU
c 
!!Cs#T455rh   c                    i }|j                         D ]2  \  }}t        |      }|j                  r|j                  |       }|||<   4 |S )z< Change all Float entries in `subs` to have precision prec. )itemsr?   is_Floatrc  )r   r   newsubsr   r6  s        rg   
evalf_subsry    sN    G

 1aD::d#A
	
 Nrh   c                F   ddl m}m} d|v r| j                  t	        ||d               } |j                         }|d= t        | d      rt        | ||      S t        | t              rt         ||       ||      S t        | t              rt         ||       ||      S t        )Nr=   )r\   r^   r   rd  )r  r\   r^   r   ry  copyhasattrr   r   floatre   rI  )r   r   r   r\   r^   newoptss         rg   evalf_piecewiser    s    'yyD'&/:;,,.FO4 tW--dE"tdG44dC g66 rh   c                8    t        | j                         ||      S r   )r   to_root)r   r   r   s      rg   evalf_alg_numr    s    dG,,rh   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NegativeInfinityry           r   r   z-inf)	r  r  r  ry   r>   r   r   r   quad_to_mpmath)rs   r   r   r  r  ry   r   s          rg   	as_mpmathr  "  sl    99
A!Ta3h1v!X5z!%&6{1dG$F&!!rh   c           	        | j                   d   | j                   d   \  }}||k(  rdx}}n<j                  vr.|j                  |j                  z  r||z
  }|j                  rd|}}|j                  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
}
|j                  d      dk(  r |	dg      } |	dg      } |	d      }j                   ||z  |z         |z        }|s j                   ||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        ||d<   d   rtj,                  j*                  }|t.        k(  r/t1        t3        t5        |                   \  }}t1        |      }n+t3        t5        t)        |      z
  |z
               }nd\  }}d   rtj6                  j*                  }|t.        k(  r/t1        t3        t5        |                   \  }}t1        |      }n+t3        t5        t)        |      z
  |z
               }nd\  }}||||f}|S # 1 sw Y   xY w)Nr   r=   r   rp   r)     r]  )WildFc                "   t        t        j                  d	| ii      \  }}}}|xs d   d<   |xs d   d<   t        t	        |            t        t	        |            |rt        |xs t        |      S t        |xs t              S )Nr   r   r=   )r   r   r   r   rt   r   r!   r   )
r}   rV   rW   r   r   rd  	have_partmax_imag_termmax_real_termrs   s
        rg   fzdo_integral.<locals>.fS  s    %*46Aq6:J%K"BFF-1IaL-1IaLwr{;Mwr{;M2;++r{U##rh   quadoscr>  )excluder?  rA  zbAn integrand of the form sin(A*x+B)*f(x) or cos(A*x+B)*f(x) is required for oscillatory quadrature)period)errorr   )r}   r   returntUnion[mpc, mpf])r   free_symbolsr   r   r#  r   r   r  rb  r^  r_  symbolr  rr   matchr   r?   Pir   r   rt   r  realr!   r   re   r   imag)r   r   r   xlowxhighdiffr&  r^  r_  r  r  r>  r?  rA  r4  r  r   quadrature_errorquadrature_errrV   re_sr   rW   im_sr   rd  r  r  r  rs   s                            @@@@@rg   do_integralr  0  s   99Q<DYYq\NAtUu}u	
$##	#
  1 114<D~~eY8JZ40GI	$(	 /=tby'2%G4 	F EN	,5,5	$ 	$ ;;v%'S1#&AS1#&AS	A

3qsQw<>*AJJs1Q37|A~.  "N O Oqvad{D2Iw?FQuf=F(%+Ae}A%F"FN&~';';<_/=b $GI| & 1 1;&sCmEU,V+V'WXLD&T"B#mgbk9D@BRSSTF
F| & 1 1;&sCmEU,V+V'WXLD&T"B#mgbk9D@BRSSTF
FVV#FMU/= /=s   $D0K%%K/c                H   | j                   }t        |      dk7  st        |d         dk7  rt        |}d}|j                  dt              }	 t        | ||      }t        |      }||k\  r	 |S ||k\  r	 |S |dk(  r|dz  }n|t        |d|z        z  }t        ||      }|dz  }W)Nr=   r   rq   r   r   rp   )	limitsr   rI  r   r   r  r   r   r   )	r   r   r   r  r   r   r   r   r   s	            rg   evalf_integralr    s    [[F
6{a3vay>Q.!!H	Akk)S)G
T8W5#F+t M w M r> MHD!Q$'Hx)	Q rh   c                   ddl m}  || |      } |||      }|j                         }|j                         }||z
  }|r|ddfS |j                         |j                         z  }	ddlm}
  |
t        |	      d      s||	dfS |j                         |j                         cxk(  rdk(  rn n||	dfS |j                         d   }|j                         d   }||	||z
  |j                         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  rd   
all_coeffs)numerdenomrf   r  npoldpolr   r  rateconstantr  pcqcs                rg   check_convergencer    s    . +q>Dq>DAAq5DT4wwy4779$H%Hq)Xt##{{}**Xq  		1	B		1	BBGTWWY...rh   c                   ddl m}m} ddlm} |t        d      k(  rt        d      |r| j                  |||z         }  || |      }|t        d      |j                         \  }}	t        ||      t        ||	      t        ||	|      \  }
}}|
dk  rt        d	|
 z        | j                  |d      }|j                  st        d
      |
dkD  s|
dk(  rt        |      dkD  rt        |j                        |z  |j                   z  }|}d}t        |      dkD  rG|t         |dz
              z  }|t         |dz
              z  }||z  }|dz  }t        |      dkD  rGt#        ||       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        ||      }|||k(  r	 |j,                  S ||z  }|}# 1 sw Y   .xY w)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.r)  zSum diverges like (%i)^nzSum diverges like n^%ir   c           	         | rIt        |       } |dxx   t         | dz
              z  cc<   |dxx   t         | dz
              z  cc<   t        t        |d                S r   )re   r9   r   r   )k_termfunc1func2rp  s     rg   summandzhypsum.<locals>.summand  s]    AA!HE!a%L 11H!HU1q5\!22HU1Xv >??rh   
richardson)method)r  r\   r  sympy.simplify.simplifyr  r}  rI  r   as_numer_denomrB   r  r   rH  rd   r9   r   r  r   r<   r   r   
mpmath_infr  )r   rf   r0  r   r\   r  r  hsr/  denr|   gr   termr   r  altvoldndigterm0r  rz   vfr  r  rp  s                          @@@rg   hypsumr    s}    -1uU|!"=>>yyAI&	4	B	z!"GHH  "HCQEQES!,GAq!1u4;<<99Q?D!"WXX 	1uaCFQJDFFt#.$i!mCa!e%%DSq1u&&DIAFA	 $i!m
 Au%%!eq6A:7#ac(BCCq5\!Q'5!<==4  dFE[E)dff4E"' @ $ H1j/,GHq$BDBJ ww DLDD) H Hs   IIc                    t        d | j                  D              rt        | j                         ||      }|S ddlm} t        | j                  |      ||      }|S )Nc              3  F   K   | ]  }|d    |d   z
  j                     yw)r=   rp   N)rD  )r   ls     rg   r   zevalf_prod.<locals>.<genexpr>!  s"     
9AaD1Q4K##
9s   !)r   r   r   rN   )r   r  r   rj  sympy.concrete.summationsrO   rewrite)r   r   r   r   rO   s        rg   
evalf_prodr     sP    

9T[[
99tyy{w? M 	2t||C(tWEMrh   c                   ddl m} d|v r| j                  |d         } | j                  }| j                  }t        |      dk7  st        |d         dk7  rt        |j                  rd d |d fS |dz   }	 |d   \  }}}	|	t        j                  us |t        j                  u s|t        |      k7  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 ]S  }d
|z  |z  x}}| j!                  |||d      \  }}|j#                         }|t        j$                  u rt        ||k  sS n t        t#        t'              d|      d         }t#        ||      \  }}}}|| }|| }||||fcY S w xY w)Nr=   r
  r   r   rq   ra   ig       @r)  rp   F)r4  rf   epseval_integralr`  )r  r\   r   functionr  r   rI  is_zeror?   r  r  re   r  rt   r   rangeeuler_maclaurinr   r  rd   )r   r   r   r\   rd  r  rp  rf   r   r6  rz   r   r  r   r4  r   errrV   rW   r   r   s                        rg   	evalf_sumr  )  s   yy)==D[[F
6{a3vay>Q.!!||T4%%2IE&)1aAJJ!q'9'9"9Q#a&[%%4CFE*wqz!1:tQA.A$D%($.. &CjD5!q! 	AqD4KA))A# * %FAs))+Caee|))cz	 eCHb'2156!&q%!9B>TF>TF2vv%%%&s   4BD A3GAGGc                    |d   |    }t        |t              r|sy|j                  d |d fS d|vri |d<   |d   }|j                  | d t        f      \  }}||k\  r|S t        t        |      ||      }||f|| <   |S )Nr   r   _cache)r   r   r  r   rr   r   r>   )rs   r   r   valcachecachedcached_precrz   s           rg   evalf_symbolr  Y  s    
&/!
C#s)yy$d**7" "GH!#iiD)+<=$M'#,g.t9arh   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aBy )Nr   rP   rN   r=   rE   rG   )rL  r\   rJ  rx   r^   r  rm  r+  r  r]   ry   r   r_   rI   )DummyrK   )rU   rW   rV   rR   rX   )	Piecewise)r[   r^  r_  rL   c                    d d |d fS r   rn   rs   r   r   s      rg   <lambda>z%_create_evalf_table.<locals>.<lambda>  s    dD$'? rh   c                    t         d |d fS r   r    r  s      rg   r  z%_create_evalf_table.<locals>.<lambda>      tT4&> rh   c                    t         d |d fS r   )r   r  s      rg   r  z%_create_evalf_table.<locals>.<lambda>  s    tT4'@ rh   c                     t        |      d |d fS r   )r.   r  s      rg   r  z%_create_evalf_table.<locals>.<lambda>  s    fTlD$%E rh   c                     t        |      d |d fS r   )r(   r  s      rg   r  z%_create_evalf_table.<locals>.<lambda>  s    dT4'F rh   c                    d t         d |fS r   r  r  s      rg   r  z%_create_evalf_table.<locals>.<lambda>  s    tT40H rh   c                    t         d |d fS r   )r   r  s      rg   r  z%_create_evalf_table.<locals>.<lambda>  s    udD$.G rh   c                "    t         j                  S r   )r?   r   r  s      rg   r  z%_create_evalf_table.<locals>.<lambda>  s    !2C2C rh   c                    t         d |d fS r   )r   r  s      rg   r  z%_create_evalf_table.<locals>.<lambda>  r  rh   )Csympy.concrete.productsrQ   r  rO   r   rF   r*  rH   r  rL  r\   rJ  rx   r^   r  rm  r+  r  r]   ry   r   r_   rZ  rJ   r  r  rK   r   rU   rW   rV   rk  rS   rT   #sympy.functions.elementary.integersrY   rZ   $sympy.functions.elementary.piecewiser  rb  r[   r^  r_  sympy.integrals.integralsrM   r  r  r  r  r[  rh  r'  rB  rX  rl  rt  r   r   r   r   r   r  r  r  r  r  r  ) rQ   rO   rF   rH   rL  r\   rJ  rx   r^   r  rm  r+  r  r]   ry   r   r_   rJ   r  rK   rU   rW   rV   rS   rT   rY   rZ   r  r[   r^  r_  rM   s                                    rg   _create_evalf_tabler  n  sk   /-/ / / /%@@?B>GG2((|( 	{( 	.	(
 	( 	?( 	>( 	@( 	E( 	F( 	H( 	G( 	C( 	>(  	Y!($ 	Z%(& 	Z'(* 	YYYYjY
H
H{.Y?O(Krh   c                >   ddl m}m} 	 t        t	        |          } || ||      }|j                  d	      rUt!        d
|        t!        dt#        |t$              rt'        |d   xs t(        d      n|       t!        d|       t!                |j                  dd      }|rJ|du r|}n7t+        t-        dt/        j0                  |      z  dz               }|dk(  r|dz  }t3        ||      }|j                  d      rt5        | ||       |S # t
        $ r d|v r| j                  t        ||d               } | j                  |      }|t        t        |dd      }|t         |       \  }	}
 |	j                  |      s |
j                  |      rt        |	dk(  rd}	d}n3|	j                  r! |	j                  |d      j                  }	|}nt        |
dk(  rd}
d}n3|
j                  r! |
j                  |d      j                  }
|}nt        |	|
||f}Y w xY w)a  
    Evaluate the ``Expr`` instance, ``x``
    to a binary precision of ``prec``. This
    function is supposed to be used internally.

    Parameters
    ==========

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

    Returns
    =======

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

    NB: all these return values can be ``None``.
    If all values are ``None``, then that represents 0.
    Note that 0 is also represented as ``fzero = (0, 0, 0, 0)``.
    r   r   r   Nr   r  F)
allow_intsr!  z	### inputz
### output2   z### rawchopTg rh
g      @rq   r=   r   )r   rV   rW   r  typeKeyErrorr   ry  rc  rI  getattrhasr   
_to_mpmathr  r   r%  r   r   r7   r!   re   roundrE  log10r   r   )rs   r   r   r   r   rfr  xer   rV   rW   reprecimprecr  	chop_precs                  rg   r   r     s   > J #a!q$ @ {{9k1lAu9MF1Q4=5"5STUi;;vu%D4<I
 E&D)9"9C"?@AIA~Q	q)${{8Q4 He  #Wz$89A]]4 :%%r>48%%B266#;&"&&+%%9BF\\t6<<BF%%9BF\\t6<<BF%%FF";#s   D
 
DHH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   rV   rW   r   s          rg   r  r    sp    k(s||Ck(s||CA!!LBAq	BB8}	2w5zrh   c                  4    e Zd ZdZdZddZeZd Zd Zd	dZ	y)

EvalfMixinz$Mixin class adding evalf capability.rn   Nc           	     H   ddl m}m}	 ||nd}|rt        |      rt	        d      |dk(  rGt        | |	      r;ddlm}
 | j                  d||||||      } |
|      }|j                  d|z
        }|S t        s
t                t        |      }t        |t        |t        z              |||d}|||d<   |||d	<   	 t        | |d
z   |      }|t*        j,                  u r|S |\  }}}}|t*        j.                  u s|t*        j.                  u rt*        j.                  S |r*t        t1        ||      d      } |j2                  ||      }nt*        j4                  }|r?t        t1        ||      d      } |j2                  ||      }||t*        j6                  z  z   S |S # t         $ r| t#        | d      r#|!| j%                  |      j'                  |      }n| j'                  |      }|| cY S |j(                  s|cY S 	 t        |||      }n# t         $ r |cY cY S w xY wY Rw xY w)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\   r`   r  z"subs must be given as a dictionary)_magrp   )r   r  r   r!  r   r  r   )r  r\   r`   rA   	TypeErrorr   r   r  r   r  r  r  r;   r   re   LG10rI  r|  r   rc  r   r?   r   r  r   r  ry   rx   )selfrf   r   maxnr  r   r  r!  r\   r`   r  r   r4  r   r   r   rz   rV   rW   r   r   r   s                         rg   r   zEvalfMixin.evalf  s   B 	+ABK%@AA 6jv."AtT4wGBRA!a%BI!1~!$DI7G5"GFO"GFO	473F" Q&&&M!'B;"+55LCf%q)AB"BBCf%q)AB"B1??***I? # 	tV$)9IIdO//5$$T*y[[q$0&  	s=   >F AH!+H!<H
	H!
HH!HH! H!c                0    | j                  |      }|| }|S )z@Helper for evalf. Does the same thing but takes binary precision)rc  )r  r   r  s      rg   _evalfzEvalfMixin._evalf  s!    T"9Arh   c                     y r   rn   )r  r   s     rg   rc  zEvalfMixin._eval_evalf  s    rh   c                   d}|r| j                   r| j                  S t        | d      rt        | j	                  |            S 	 t        | |i       }t        |      S # t        $ r | j                  |      }|t        |      |j                  rt        |j                        cY S |j                         \  }}|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   r  r   r  rI  rc  r   rw  r  r   r   r   )r  r   r  errmsgr   rz   rV   rW   s           rg   r  zEvalfMixin._to_mpmath  s   2$//66M4'D,,T233	&4r*F!&))" 	&  &Ay ((zz((^^%FBbmmbdd^XX ((bmmbdd^XX ((RH%%)	&s   A AE&B0EE)r  Nd   FFNF)T)
rk   rl   rm   __doc__	__slots__r   rf   r  rc  r  rn   rh   rg   r  r    s'    .Iyv 	A&rh   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   )rs   rf   r   s      rg   r   r     s#    . +71t$**1888rh   c                N   |H|j                   s|j                  r|dkD  st        d      t        d|z  di       \  }}}}t	        |      }t        | di       \  }}}}t	        |      t	        |      }	}t        ||	      dz   }
t        d||
z   dz         }|xs 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

    r   zeps must be positiver=   )rH  rw  r   r   rt   r   )rs   r  r4  r   r  r   r~   dnrnirf   r   s               rg   _evalf_with_bounded_errorr%    s    : 3<<a3441S5!R(
1aAJq!RJAq!Q QZBBaA 	Aq1uqyAmGAwrh   )rs   zOptional[MPF_TUP]r  tUnion[int, Any])F)rz   r   r  z tuple['Number', 'Number'] | None)r=   )r   SCALED_ZERO_TUPr  r   )r   re   r  ztTuple[SCALED_ZERO_TUP, int])r   ztUnion[SCALED_ZERO_TUP, int]r  z-tUnion[MPF_TUP, tTuple[SCALED_ZERO_TUP, int]])r   z&tUnion[MPF_TUP, SCALED_ZERO_TUP, None]r  zOptional[bool])r   TMP_RESr  r&  )r   r   r   re   r   OPT_DICTr  r(  )
r   r   r   re   r   re   r   r)  r  r(  )r   z'Abs'r   re   r   r)  r  r(  )r   z're'r   re   r   r)  r  r(  )r   z'im'r   re   r   r)  r  r(  )rV   r   rW   r   r   re   r  r(  )r   r(  r   re   r  r(  )r   r   r   r(  r   re   )r   r   r   re   r   r)  r  z!tUnion[TMP_RES, tTuple[int, int]])r   z	'ceiling'r   re   r   r)  r  r(  )r   z'floor'r   re   r   r)  r  r(  )r   z'Float'r   re   r   r)  r  r(  )r   z
'Rational'r   re   r   r)  r  r(  )r   z	'Integer'r   re   r   r)  r  r(  )r  r   r   re   r  re   r  z=tTuple[tUnion[MPF_TUP, SCALED_ZERO_TUP, None], Optional[int]])rz   z'Add'r   re   r   r)  r  r(  )rz   z'Mul'r   re   r   r)  r  r(  )rz   z'Pow'r   re   r  r(  )r   z'exp'r   re   r   r)  r  r(  )rz   r   r   re   r   r)  r  r(  )r   z'log'r   re   r   r)  r  r(  )rz   z'atan'r   re   r   r)  r  r(  )r   re   r   dictr  r*  )r   z'AlgebraicNumber'r   re   r   r)  r  r(  )rs   r   r   re   r   r)  r  r  )r   z
'Integral'r   re   r   r)  r  r(  )r  r   r  r   rf   'Symbol'r  ztTuple[int, Any, Any])
r   r   rf   r+  r0  re   r   re   r  r   )r   z	'Product'r   re   r   r)  r  r(  )r   z'Sum'r   re   r   r)  r  r(  )rs   r   r   re   r   r)  r  r(  r   )r  )Nr   N)
rs   r   r  z'Optional[Expr]'r4  re   r   zOptional[OPT_DICT]r  r(  )r  
__future__r   typingr   tTupler   r   tUnionr   r   r	   tDictr
   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/   r0   r1   r2   r3   r4   r5   r6   r7   r8   rc   mpmath.libmp.backendr9   mpmath.libmp.libmpcr:   mpmath.libmp.libmpfr;   r<   r>   	singletonr?   sympy.external.gmpyr@   sympy.utilities.iterablesrA   sympy.utilities.lambdifyrB   sympy.utilities.miscrC   sympy.core.exprrD   sympy.core.addrF   sympy.core.mulrH   sympy.core.powerrJ   sympy.core.symbolrK   r  rM   r  rO   r  rQ   rk  rS   rT   r   rU   rV   rW   r  rY   rZ   rb  r[   r  r\   r]   r^   r_   r`   rF  r  r   r}  r   rr   r#  ArithmeticErrorrj   re   r   r(  strr)  rt   r   r'  r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r'  rB  rX  r[  rh  rl  rt  ry  r  r  r  r  r  r  r  r  r  r  r  __annotations__r  r   r  r  r   r%  rn   rh   rg   <module>rC     s   #    G G G $< < < < < < < < <
 5 $ ) 8   * 1 - '$""$(2-/?@@B=JJtyy}( J:+	 	 	 c3#
$&  c?!H> cCc12 
 
	 
$J5$JNG:&, 0<<"&",0l$)l$^67(A4SESl."b{ |xGDK
:'z2&j6"-"\~4'/TJZ'&`$ PRL Q8vUp"j& j&Z94 BF'(<@1 !$1 '91 EL1 rh   