
    Ed.                        d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZmZ ddlmZ d Z G d	 d
e          Zd Z G d de          Z e	d          Zd Z G d de          Zd Z G d de          Zd Z G d de          Z e	d          Zd Z G d de          Zd Z G d de          Zd Z G d  d!e          Z d" Z! G d# d$e          Z"d%S )&a#  
This module contains SymPy functions mathcin corresponding to special math functions in the
C standard library (since C99, also available in C++11).

The functions defined in this module allows the user to express functions such as ``expm1``
as a SymPy function for symbolic manipulation.

    )ArgumentIndexErrorFunction)Rational)Pow)S)explog)sqrtc                 :    t          |           t          j        z
  S Nr   r   Onexs    8lib/python3.11/site-packages/sympy/codegen/cfunctions.py_expm1r      s    q66AE>    c                   P    e Zd ZdZdZd
dZd Zd ZeZe	d             Z
d Zd Zd	S )expm1a*  
    Represents the exponential function minus one.

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

    The benefit of using ``expm1(x)`` over ``exp(x) - 1``
    is that the latter is prone to cancellation under finite precision
    arithmetic when x is close to zero.

    Examples
    ========

    >>> from sympy.abc import x
    >>> from sympy.codegen.cfunctions import expm1
    >>> '%.0e' % expm1(1e-99).evalf()
    '1e-99'
    >>> from math import exp
    >>> exp(1e-99) - 1
    0.0
    >>> expm1(x).diff(x)
    exp(x)

    See Also
    ========

    log1p
       c                 J    |dk    rt          | j         S t          | |          @
        Returns the first derivative of this function.
        r   )r   argsr   selfargindexs     r   fdiffzexpm1.fdiff4   s,     q= 	5	?"$T8444r   c                     t          | j         S r   )r   r   r   hintss     r   _eval_expand_funczexpm1._eval_expand_func=       ty!!r   c                 :    t          |          t          j        z
  S r   r   r   argkwargss      r   _eval_rewrite_as_expzexpm1._eval_rewrite_as_exp@   s    3xx!%r   c                 P    t          j        |          }||t          j        z
  S d S r   )r   evalr   r   )clsr&   exp_args      r   r*   z
expm1.evalE   s-    (3-- 	#QU?"	# 	#r   c                 &    | j         d         j        S Nr   )r   is_realr   s    r   _eval_is_realzexpm1._eval_is_realK       y|##r   c                 &    | j         d         j        S r.   )r   	is_finiter0   s    r   _eval_is_finitezexpm1._eval_is_finiteN   s    y|%%r   Nr   )__name__
__module____qualname____doc__nargsr   r"   r(   _eval_rewrite_as_tractableclassmethodr*   r1   r5    r   r   r   r      s         8 E5 5 5 5" " "      "6# # [#
$ $ $& & & & &r   r   c                 :    t          | t          j        z             S r   )r	   r   r   r   s    r   _log1pr@   R   s    q15y>>r   c                   b    e Zd ZdZdZddZd Zd ZeZe	d             Z
d Zd Zd	 Zd
 Zd ZdS )log1paf  
    Represents the natural logarithm of a number plus one.

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

    The benefit of using ``log1p(x)`` over ``log(x + 1)``
    is that the latter is prone to cancellation under finite precision
    arithmetic when x is close to zero.

    Examples
    ========

    >>> from sympy.abc import x
    >>> from sympy.codegen.cfunctions import log1p
    >>> from sympy import expand_log
    >>> '%.0e' % expand_log(log1p(1e-99)).evalf()
    '1e-99'
    >>> from math import log
    >>> log(1 + 1e-99)
    0.0
    >>> log1p(x).diff(x)
    1/(x + 1)

    See Also
    ========

    expm1
    r   c                 |    |dk    r't           j        | j        d         t           j        z   z  S t          | |          r   r   r   )r   r   r   r   r   s     r   r   zlog1p.fdiffw   s:     q= 	55$)A,.//$T8444r   c                     t          | j         S r   )r@   r   r    s     r   r"   zlog1p._eval_expand_func   r#   r   c                      t          |          S r   )r@   r%   s      r   _eval_rewrite_as_logzlog1p._eval_rewrite_as_log       c{{r   c                     |j         rt          |t          j        z             S |j        s!t          j        |t          j        z             S |j        r)t          t          |          t          j        z             S d S r   )is_Rationalr	   r   r   is_Floatr*   	is_numberr   r+   r&   s     r   r*   z
log1p.eval   so    ? 	.sQU{### 	.8C!%K(((] 	.x}}qu,---	. 	.r   c                 @    | j         d         t          j        z   j        S r.   )r   r   r   is_nonnegativer0   s    r   r1   zlog1p._eval_is_real   s    	!qu$44r   c                 h    | j         d         t          j        z   j        rdS | j         d         j        S )Nr   F)r   r   r   is_zeror4   r0   s    r   r5   zlog1p._eval_is_finite   s.    IaL15 ) 	5y|%%r   c                 &    | j         d         j        S r.   )r   is_positiver0   s    r   _eval_is_positivezlog1p._eval_is_positive   s    y|''r   c                 &    | j         d         j        S r.   )r   rQ   r0   s    r   _eval_is_zerozlog1p._eval_is_zero   r2   r   c                 &    | j         d         j        S r.   )r   rO   r0   s    r   _eval_is_nonnegativezlog1p._eval_is_nonnegative   s    y|**r   Nr6   )r7   r8   r9   r:   r;   r   r"   rG   r<   r=   r*   r1   r5   rT   rV   rX   r>   r   r   rB   rB   V   s         : E5 5 5 5" " "   "6. . [.5 5 5& & &
( ( ($ $ $+ + + + +r   rB      c                 ,    t          t          |           S r   )r   _Twor   s    r   _exp2r\      s    tQ<<r   c                   D    e Zd ZdZdZddZd ZeZd Ze	d             Z
dS )	exp2a  
    Represents the exponential function with base two.

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

    The benefit of using ``exp2(x)`` over ``2**x``
    is that the latter is not as efficient under finite precision
    arithmetic.

    Examples
    ========

    >>> from sympy.abc import x
    >>> from sympy.codegen.cfunctions import exp2
    >>> exp2(2).evalf() == 4
    True
    >>> exp2(x).diff(x)
    log(2)*exp2(x)

    See Also
    ========

    log2
    r   c                 \    |dk    r| t          t                    z  S t          | |          r   )r	   r[   r   r   s     r   r   z
exp2.fdiff   s0     q= 	5D		>!$T8444r   c                      t          |          S r   )r\   r%   s      r   _eval_rewrite_as_Powzexp2._eval_rewrite_as_Pow       Szzr   c                     t          | j         S r   )r\   r   r    s     r   r"   zexp2._eval_expand_func       di  r   c                 2    |j         rt          |          S d S r   )rL   r\   rM   s     r   r*   z	exp2.eval   s"    = 	::	 	r   Nr6   )r7   r8   r9   r:   r;   r   ra   r<   r"   r=   r*   r>   r   r   r^   r^      sz         2 E5 5 5 5   "6! ! !   [  r   r^   c                 J    t          |           t          t                    z  S r   )r	   r[   r   s    r   _log2rg          q66#d))r   c                   J    e Zd ZdZdZd	dZed             Zd Zd Z	d Z
e
ZdS )
log2a  
    Represents the logarithm function with base two.

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

    The benefit of using ``log2(x)`` over ``log(x)/log(2)``
    is that the latter is not as efficient under finite precision
    arithmetic.

    Examples
    ========

    >>> from sympy.abc import x
    >>> from sympy.codegen.cfunctions import log2
    >>> log2(4).evalf() == 2
    True
    >>> log2(x).diff(x)
    1/(x*log(2))

    See Also
    ========

    exp2
    log10
    r   c                     |dk    r/t           j        t          t                    | j        d         z  z  S t          | |          rD   )r   r   r	   r[   r   r   r   s     r   r   z
log2.fdiff   >     q= 	55#d))DIaL011$T8444r   c                     |j         r&t          j        |t                    }|j        r|S d S |j        r|j        t          k    r	|j        S d S d S N)base)rL   r	   r*   r[   is_Atomis_Powro   r   r+   r&   results      r   r*   z	log2.eval  l    = 	Xc---F~  Z 	CH, 	7N	 	 	 	r   c                 L     |                      t                    j        |i |S r   )rewriter	   evalf)r   r   r'   s      r   _eval_evalfzlog2._eval_evalf  s&    &t||C  &7777r   c                     t          | j         S r   )rg   r   r    s     r   r"   zlog2._eval_expand_func  rd   r   c                      t          |          S r   )rg   r%   s      r   rG   zlog2._eval_rewrite_as_log  rb   r   Nr6   )r7   r8   r9   r:   r;   r   r=   r*   rx   r"   rG   r<   r>   r   r   rj   rj      s         4 E5 5 5 5   [8 8 8! ! !   "6r   rj   c                     | |z  |z   S r   r>   )r   yzs      r   _fmar~     s    Q37Nr   c                   ,    e Zd ZdZdZddZd Zd	dZdS )
fmaa  
    Represents "fused multiply add".

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

    The benefit of using ``fma(x, y, z)`` over ``x*y + z``
    is that, under finite precision arithmetic, the former is
    supported by special instructions on some CPUs.

    Examples
    ========

    >>> from sympy.abc import x, y, z
    >>> from sympy.codegen.cfunctions import fma
    >>> fma(x, y, z).diff(x)
    y

       r   c                 n    |dv r| j         d|z
           S |dk    rt          j        S t          | |          )r   r   rY   rY   r   )r   r   r   r   r   s     r   r   z	fma.fdiff6  sE     v 	59Q\**] 	55L$T8444r   c                     t          | j         S r   )r~   r   r    s     r   r"   zfma._eval_expand_funcB  s    TYr   Nc                      t          |          S r   )r~   )r   r&   limitvarr'   s       r   r<   zfma._eval_rewrite_as_tractableE  s    Cyyr   r6   r   )r7   r8   r9   r:   r;   r   r"   r<   r>   r   r   r   r      s\         & E	5 	5 	5 	5          r   r   
   c                 J    t          |           t          t                    z  S r   )r	   _Tenr   s    r   _log10r   L  rh   r   c                   D    e Zd ZdZdZddZed             Zd Zd Z	e	Z
dS )	log10a"  
    Represents the logarithm function with base ten.

    Examples
    ========

    >>> from sympy.abc import x
    >>> from sympy.codegen.cfunctions import log10
    >>> log10(100).evalf() == 2
    True
    >>> log10(x).diff(x)
    1/(x*log(10))

    See Also
    ========

    log2
    r   c                     |dk    r/t           j        t          t                    | j        d         z  z  S t          | |          rD   )r   r   r	   r   r   r   r   s     r   r   zlog10.fdiffe  rl   r   c                     |j         r&t          j        |t                    }|j        r|S d S |j        r|j        t          k    r	|j        S d S d S rn   )rL   r	   r*   r   rp   rq   ro   r   rr   s      r   r*   z
log10.evalo  rt   r   c                     t          | j         S r   )r   r   r    s     r   r"   zlog10._eval_expand_funcx  r#   r   c                      t          |          S r   )r   r%   s      r   rG   zlog10._eval_rewrite_as_log{  rH   r   Nr6   )r7   r8   r9   r:   r;   r   r=   r*   r"   rG   r<   r>   r   r   r   r   P  sv         $ E5 5 5 5   [" " "   "6r   r   c                 6    t          | t          j                  S r   )r   r   Halfr   s    r   _Sqrtr     s    q!&>>r   c                   .    e Zd ZdZdZddZd Zd ZeZdS )Sqrta  
    Represents the square root function.

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

    The reason why one would use ``Sqrt(x)`` over ``sqrt(x)``
    is that the latter is internally represented as ``Pow(x, S.Half)`` which
    may not be what one wants when doing code-generation.

    Examples
    ========

    >>> from sympy.abc import x
    >>> from sympy.codegen.cfunctions import Sqrt
    >>> Sqrt(x)
    Sqrt(x)
    >>> Sqrt(x).diff(x)
    1/(2*sqrt(x))

    See Also
    ========

    Cbrt
    r   c                     |dk    r1t          | j        d         t          dd                    t          z  S t	          | |          )r   r   r   rY   r   r   r   r[   r   r   s     r   r   z
Sqrt.fdiff  sC     q= 	5ty|Xb!__55d::$T8444r   c                     t          | j         S r   )r   r   r    s     r   r"   zSqrt._eval_expand_func  rd   r   c                      t          |          S r   )r   r%   s      r   ra   zSqrt._eval_rewrite_as_Pow  rb   r   Nr6   	r7   r8   r9   r:   r;   r   r"   ra   r<   r>   r   r   r   r     s[         2 E5 5 5 5! ! !   "6r   r   c                 >    t          | t          dd                    S )Nr   r   )r   r   r   s    r   _Cbrtr     s    q(1a..!!!r   c                   .    e Zd ZdZdZddZd Zd ZeZdS )Cbrta  
    Represents the cube root function.

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

    The reason why one would use ``Cbrt(x)`` over ``cbrt(x)``
    is that the latter is internally represented as ``Pow(x, Rational(1, 3))`` which
    may not be what one wants when doing code-generation.

    Examples
    ========

    >>> from sympy.abc import x
    >>> from sympy.codegen.cfunctions import Cbrt
    >>> Cbrt(x)
    Cbrt(x)
    >>> Cbrt(x).diff(x)
    1/(3*x**(2/3))

    See Also
    ========

    Sqrt
    r   c                     |dk    r4t          | j        d         t          t           dz                      dz  S t	          | |          )r   r   r   r   r   r   s     r   r   z
Cbrt.fdiff  sI     q= 	5ty|XteAg%6%67799$T8444r   c                     t          | j         S r   )r   r   r    s     r   r"   zCbrt._eval_expand_func  rd   r   c                      t          |          S r   )r   r%   s      r   ra   zCbrt._eval_rewrite_as_Pow  rb   r   Nr6   r   r>   r   r   r   r     s[         2 E5 5 5 5! ! !   "6r   r   c                 ^    t          t          | d          t          |d          z             S )NrY   )r
   r   )r   r|   s     r   _hypotr     s%    Aq		C1II%&&&r   c                   .    e Zd ZdZdZddZd Zd ZeZdS )	hypota  
    Represents the hypotenuse function.

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

    The hypotenuse function is provided by e.g. the math library
    in the C99 standard, hence one may want to represent the function
    symbolically when doing code-generation.

    Examples
    ========

    >>> from sympy.abc import x, y
    >>> from sympy.codegen.cfunctions import hypot
    >>> hypot(3, 4).evalf() == 5
    True
    >>> hypot(x, y)
    hypot(x, y)
    >>> hypot(x, y).diff(x)
    x/hypot(x, y)

    rY   r   c                     |dv r+d| j         |dz
           z  t           | j        | j          z  z  S t          | |          )r   r   rY   r   )r   r[   funcr   r   s     r   r   zhypot.fdiff  sL     v 	5TYxz**DDI1F,FGG$T8444r   c                     t          | j         S r   )r   r   r    s     r   r"   zhypot._eval_expand_func  r#   r   c                      t          |          S r   )r   r%   s      r   ra   zhypot._eval_rewrite_as_Pow  rH   r   Nr6   r   r>   r   r   r   r     s[         . E5 5 5 5" " "   "6r   r   N)#r:   sympy.core.functionr   r   sympy.core.numbersr   sympy.core.powerr   sympy.core.singletonr   &sympy.functions.elementary.exponentialr   r	   (sympy.functions.elementary.miscellaneousr
   r   r   r@   rB   r[   r\   r^   rg   rj   r~   r   r   r   r   r   r   r   r   r   r   r>   r   r   <module>r      s    = < < < < < < < ' ' ' ' ' '             " " " " " " ; ; ; ; ; ; ; ; 9 9 9 9 9 9  :& :& :& :& :&H :& :& :&z  K+ K+ K+ K+ K+H K+ K+ K+Z 	qtt  1 1 1 1 18 1 1 1h  96 96 96 96 968 96 96 96x  & & & & &( & & &R 	quu  .6 .6 .6 .6 .6H .6 .6 .6b  +6 +6 +6 +6 +68 +6 +6 +6\" " ",6 ,6 ,6 ,6 ,68 ,6 ,6 ,6^' ' '*6 *6 *6 *6 *6H *6 *6 *6 *6 *6r   