o
    *ήci                     @   s  d dl mZ d dlmZ d dlmZ d dlmZ d dl	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 d dlmZmZmZ d d	lmZ d d
lmZmZmZ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 dl-m.Z.m/Z/m0Z0m1Z1m2Z2 d dl3m4Z4 d dl5m6Z6m7Z7 d dl8m9Z9 d dl:m;Z; G dd deZ<G dd de<Z=G dd deZ>G dd de<e>dZ?dd Z@G d d! d!eZAG d"d# d#eZBd$S )%    )product)Tuple)Expr)sympify)Add)cacheit)FunctionArgumentIndexError
expand_log
expand_mulFunctionClass	PoleErrorexpand_multinomialexpand_complex)	fuzzy_and	fuzzy_notfuzzy_or)Mul)IntegerRationalpiIImaginaryUnit)global_parameters)Pow)S)WildDummy)	factorial)arg
unpolarifyimreAbs)sqrt)multiplicityperfect_power)	factorint)cancelc                   @   s   e Zd ZdZejfZedd ZdddZ	dd Z
ed	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )ExpBaseTc                 C   s   | j jS N)expkindself r/   M/tmp/pip-target-vg8gfxp4/lib/python/sympy/functions/elementary/exponential.pyr,   )   s   zExpBase.kind   c                 C      t S )z=
        Returns the inverse function of ``exp(x)``.
        logr.   argindexr/   r/   r0   inverse-      zExpBase.inversec                 C   s@   | j }|j}|s| js| }|rtj| | fS | tjfS )a-  
        Returns this with a positive exponent as a 2-tuple (a fraction).

        Examples
        ========

        >>> from sympy import exp
        >>> from sympy.abc import x
        >>> exp(-x).as_numer_denom()
        (1, exp(x))
        >>> exp(x).as_numer_denom()
        (exp(x), 1)
        )r+   is_negativecould_extract_minus_signr   Onefunc)r.   r+   neg_expr/   r/   r0   as_numer_denom3   s   
zExpBase.as_numer_denomc                 C   s
   | j d S )z7
        Returns the exponent of the function.
        r   )argsr-   r/   r/   r0   r+   K   s   
zExpBase.expc                 C   s   |  dt| j fS )z7
        Returns the 2-tuple (base, exponent).
        r1   )r<   r   r?   r-   r/   r/   r0   as_base_expR   s   zExpBase.as_base_expc                 C      |  | j S r*   )r<   r+   adjointr-   r/   r/   r0   _eval_adjointX      zExpBase._eval_adjointc                 C   rA   r*   )r<   r+   	conjugater-   r/   r/   r0   _eval_conjugate[   rD   zExpBase._eval_conjugatec                 C   rA   r*   )r<   r+   	transposer-   r/   r/   r0   _eval_transpose^   rD   zExpBase._eval_transposec                 C   s.   | j }|jr|jrdS |jrdS |jrdS d S NTF)r+   is_infiniteis_extended_negativeis_extended_positive	is_finiter.   r   r/   r/   r0   _eval_is_finitea   s   zExpBase._eval_is_finitec                 C   sJ   | j | j }|j | j kr"|jj}|rdS |jjrt|r dS d S d S |jS rI   )r<   r?   r+   is_zerois_rationalr   )r.   szr/   r/   r0   _eval_is_rationalk   s   zExpBase._eval_is_rationalc                 C   s   | j tju S r*   )r+   r   NegativeInfinityr-   r/   r/   r0   _eval_is_zerov      zExpBase._eval_is_zeroc                 C   s"   |   \}}tt||dd|S )z;exp(arg)**e -> exp(arg*e) if assumptions allow it.
        Fevaluate)r@   r   _eval_power)r.   otherber/   r/   r0   rZ   y   s   zExpBase._eval_powerc                    s|   ddl m} ddlm}  jd }|jr$|jr$t fdd|jD S t	||r9|jr9| 
|jg|jR  S  
|S )Nr   )Product)Sumc                 3   s    | ]}  |V  qd S r*   )r<   ).0xr-   r/   r0   	<genexpr>   s    z1ExpBase._eval_expand_power_exp.<locals>.<genexpr>)sympy.concrete.productsr^   sympy.concrete.summationsr_   r?   is_Addis_commutativer   fromiter
isinstancer<   functionlimits)r.   hintsr^   r_   r   r/   r-   r0   _eval_expand_power_exp   s   

zExpBase._eval_expand_power_expNr1   )__name__
__module____qualname__
unbranchedr   ComplexInfinity_singularitiespropertyr,   r7   r>   r+   r@   rC   rF   rH   rO   rT   rV   rZ   rl   r/   r/   r/   r0   r)   $   s$    



r)   c                   @   s@   e Zd ZdZdZdZdd Zdd Zdd	 Zd
d Z	dd Z
dS )	exp_polara<  
    Represent a *polar number* (see g-function Sphinx documentation).

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

    ``exp_polar`` represents the function
    `Exp: \mathbb{C} \rightarrow \mathcal{S}`, sending the complex number
    `z = a + bi` to the polar number `r = exp(a), \theta = b`. It is one of
    the main functions to construct polar numbers.

    Examples
    ========

    >>> from sympy import exp_polar, pi, I, exp

    The main difference is that polar numbers do not "wrap around" at `2 \pi`:

    >>> exp(2*pi*I)
    1
    >>> exp_polar(2*pi*I)
    exp_polar(2*I*pi)

    apart from that they behave mostly like classical complex numbers:

    >>> exp_polar(2)*exp_polar(3)
    exp_polar(5)

    See Also
    ========

    sympy.simplify.powsimp.powsimp
    polar_lift
    periodic_argument
    principal_branch
    TFc                 C   s   t t| jd S Nr   )r+   r"   r?   r-   r/   r/   r0   	_eval_Abs   s   zexp_polar._eval_Absc                 C   sx   t | jd }z|t kp|tk}W n ty   d}Y nw |r"| S t| jd |}|dkr:t |dk r:t|S |S )z. Careful! any evalf of polar numbers is flaky r   T)r!   r?   r   	TypeErrorr+   _eval_evalfr"   )r.   precibadresr/   r/   r0   ry      s   zexp_polar._eval_evalfc                 C   s   |  | jd | S rv   )r<   r?   )r.   r[   r/   r/   r0   rZ      s   zexp_polar._eval_powerc                 C   s   | j d jrdS d S )Nr   T)r?   is_extended_realr-   r/   r/   r0   _eval_is_extended_real   s   z exp_polar._eval_is_extended_realc                 C   s"   | j d dkr| tjfS t| S rv   )r?   r   r;   r)   r@   r-   r/   r/   r0   r@      s   

zexp_polar.as_base_expN)rn   ro   rp   __doc__is_polaris_comparablerw   ry   rZ   r   r@   r/   r/   r/   r0   ru      s    %ru   c                   @   s   e Zd Zdd ZdS )ExpMetac                 C   s&   t |jjv rdS t|to|jtju S )NT)r+   	__class____mro__rh   r   baser   Exp1)clsinstancer/   r/   r0   __instancecheck__   s   zExpMeta.__instancecheck__N)rn   ro   rp   r   r/   r/   r/   r0   r      s    r   c                   @   s   e Zd ZdZd,ddZdd Zedd Zed	d
 Z	e
edd Zd-ddZdd Zdd Zdd Zdd Zdd Zd.ddZdd Zd/d d!Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ ZdS )0r+   a9  
    The exponential function, :math:`e^x`.

    Examples
    ========

    >>> from sympy import exp, I, pi
    >>> from sympy.abc import x
    >>> exp(x)
    exp(x)
    >>> exp(x).diff(x)
    exp(x)
    >>> exp(I*pi)
    -1

    Parameters
    ==========

    arg : Expr

    See Also
    ========

    log
    r1   c                 C   s   |dkr| S t | |)z@
        Returns the first derivative of this function.
        r1   )r	   r5   r/   r/   r0   fdiff   s   
z	exp.fdiffc                 C   s   ddl m}m} | jd }|jrctjtj }||| fv r tjS |	tj
tj }|re||d| rg|||r>tjS |||rHtjS |||tj rVtj S |||tj ritjS d S d S d S d S )Nr   )askQ   )sympy.assumptionsr   r   r?   is_Mulr   r   InfinityNaNas_coefficientPiintegerevenr;   oddNegativeOneHalf)r.   assumptionsr   r   r   Ioocoeffr/   r/   r0   _eval_refine   s*   
zexp._eval_refinec                 C   sp  ddl m} ddlm} ddlm} ddlm} t||r!|	 S t
jr*ttj|S |jrU|tju r5tjS |jr;tjS |tju rCtjS |tju rKtjS |tju rStjS nZ|tju r]tjS t|trg|jd S t||rw|t	|jt	|jS t||r|| S |jrU|tjtj }|rd| j r|j!rtjS |j"rtj#S |tj$ j!rtj S |tj$ j"rtjS n|j%r|d }|dkr|d8 }||kr| |tj tj S |& \}}|tjtjfv r|j'r|tju r| }t(|jr|tjurtjS t(|j)rt*|tjurtjS t(|j+rtjS d S |gd }	}
t,-|D ](}||}t|tr<|
d u r9|jd }
q! d S |j.rG|	/| q! d S |
rS|
t,|	  S d S |j0rg }g }d}|jD ]:}|tju rq|/| qb| |}t|| r|jd |kr|/|jd  d	}qb|/| qb|/| qb|s|rt,| | t1| dd
 S |jrtjS d S )Nr   AccumBounds)
MatrixBaseSetExpr
logcombiner   r1   FTrX   )2sympy.calculusr   sympy.matrices.matricesr   sympy.sets.setexprr   sympy.simplify.simplifyr   rh   r+   r   
exp_is_powr   r   r   	is_Numberr   rP   r;   r   rU   Zerorr   r4   r?   minmax
_eval_funcr   r   r   r   
is_integeris_evenis_oddr   r   is_Rationalas_coeff_Mul	is_numberr"   is_positiver!   r9   r   	make_argsr   appendre   r   )r   r   r   r   r   r   r   ncoefftermscoeffslog_termtermterm_outadd
argchangedanewar/   r/   r0   eval  s   















zexp.evalc                 C   s   t jS )z?
        Returns the base of the exponential function.
        )r   r   r-   r/   r/   r0   r   |  s   zexp.basec                 G   sT   | dk rt jS | dkrt jS t|}|r"|d }|dur"|| |  S ||  t|  S )zJ
        Calculates the next term in the Taylor series expansion.
        r   N)r   r   r;   r   r   )nra   previous_termspr/   r/   r0   taylor_term  s   zexp.taylor_termTc                 K   st   ddl m}m} | jd  \}}|r%|j|fi |}|j|fi |}||||}}t|| t|| fS )aJ  
        Returns this function as a 2-tuple representing a complex number.

        Examples
        ========

        >>> from sympy import exp, I
        >>> from sympy.abc import x
        >>> exp(x).as_real_imag()
        (exp(re(x))*cos(im(x)), exp(re(x))*sin(im(x)))
        >>> exp(1).as_real_imag()
        (E, 0)
        >>> exp(I).as_real_imag()
        (cos(1), sin(1))
        >>> exp(1+I).as_real_imag()
        (E*cos(1), E*sin(1))

        See Also
        ========

        sympy.functions.elementary.complexes.re
        sympy.functions.elementary.complexes.im
        r   )cossin)(sympy.functions.elementary.trigonometricr   r   r?   as_real_imagexpandr+   )r.   deeprk   r   r   r"   r!   r/   r/   r0   r     s   zexp.as_real_imagc                 C   s   |j rt|jt|j }n
|tju r|jrt}t|ts"|tju r1dd }t	|| |||S |tu rA|jsA|| j
|| S t	| ||S )Nc                 S   s&   | j st| trt|  ddiS | S )NrY   F)is_Powrh   r+   r   r@   )r   r/   r/   r0   <lambda>  s
   z exp._eval_subs.<locals>.<lambda>)r   r+   r4   r   r   r   is_Functionrh   r   
_eval_subs_subsr   )r.   oldnewfr/   r/   r0   r     s   zexp._eval_subsc                 C   sF   | j d jrdS | j d jr!td tj | j d  tj }|jS d S )Nr   Tr   )r?   r~   is_imaginaryr   r   r   r   r.   arg2r/   r/   r0   r     s    zexp._eval_is_extended_realc                 C   s   dd }t || jd S )Nc                 s   s    | j V  | jV  d S r*   )
is_complexrK   r   r/   r/   r0   complex_extended_negative  s   z7exp._eval_is_complex.<locals>.complex_extended_negativer   )r   r?   )r.   r   r/   r/   r0   _eval_is_complex  s   zexp._eval_is_complexc                 C   sJ   | j tj tj jrdS t| j jr!| j jrdS | j tj jr#dS d S d S rI   )r+   r   r   r   rQ   r   rP   is_algebraicr-   r/   r/   r0   _eval_is_algebraic  s   zexp._eval_is_algebraicc                 C   sB   | j jr| jd tjuS | j jrtj | jd  tj }|jS d S rv   )	r+   r~   r?   r   rU   r   r   r   r   r   r/   r/   r0   _eval_is_extended_positive  s   zexp._eval_is_extended_positiver   c              	      s  ddl m  ddlm} ddlm} ddlm} ddlm	} | j
}	|	j|||d}
|
jr0d|
 S ||
 |d}|tju rD||| |S |tju rK| S t fd	d
|jD rY| S td}|}z||	j||d| }W n ttfyz   d}Y nw |r|dkr||| }t
|||}t
||||
|  }||t|| kr|S |r|dkr|||
| | |||d |   7 }n|||
| | |7 }| }||ddd}dd }td|gd}|tj| ttj| }|S )Nr   signceiling)limitOrderpowsimpr   logxr1   c                 3   s    | ]
}t | tfV  qd S r*   )rh   r   )r`   r   r   r/   r0   rb     s    z$exp._eval_nseries.<locals>.<genexpr>tr   Tr+   r   combinec                 S   s   | j o| jdv S )N)         )r   q)ra   r/   r/   r0   r     s    z#exp._eval_nseries.<locals>.<lambda>w)
properties) $sympy.functions.elementary.complexesr   #sympy.functions.elementary.integersr   sympy.series.limitsr   sympy.series.orderr   sympy.simplify.powsimpr   r+   _eval_nseriesis_OrderremoveOr   rU   r   anyr?   r   as_leading_termgetnNotImplementedErrorr   _taylorsubsr4   r   r   replacer   r   )r.   ra   r   r   cdirr   r   r   r   r   
arg_seriesarg0r   ntermscf
exp_seriesr	simpleratr   r/   r   r0   r     sL   

(zexp._eval_nseriesc                 C   sN   g }d }t |D ]}| || jd |}|j||d}||  qt| S )Nr   r   )ranger   r?   nseriesr   r   r   )r.   ra   r   lgr{   r/   r/   r0   r    s   zexp._taylorNc                 C   s   ddl m} | jd  j||d}||d}|tju r tjS t||r5t	|tj
k r1t| S t|S |tju r@||d}|jdu rIt|S td|  )Nr   r   r   FCannot expand %s around 0)sympy.calculus.utilr   r?   r(   r   r  r   r   rh   r"   r   r+   r   rJ   r   )r.   ra   r   r  r   r   r  r/   r/   r0   _eval_as_leading_term  s   




zexp._eval_as_leading_termc                 K   s8   ddl m} tj}||| tjd  ||||   S )Nr   )r   r   )r   r   r   r   r   )r.   r   kwargsr   r   r/   r/   r0   _eval_rewrite_as_sin*     &zexp._eval_rewrite_as_sinc                 K   s8   ddl m} tj}||| |||| tjd    S )Nr   )r   r   )r   r   r   r   r   )r.   r   r  r   r   r/   r/   r0   _eval_rewrite_as_cos/  r  zexp._eval_rewrite_as_cosc                 K   s,   ddl m} d||d  d||d   S )Nr   )tanhr1   r   )%sympy.functions.elementary.hyperbolicr  )r.   r   r  r  r/   r/   r0   _eval_rewrite_as_tanh4  s    zexp._eval_rewrite_as_tanhc                 K   s   ddl m}m} |jr9|tjtj }|r;|jr=|tj| |tj| }}t	||s?t	||sA|tj|  S d S d S d S d S d S )Nr   )r   r   )
r   r   r   r   r   r   r   r   r   rh   )r.   r   r  r   r   r   cosinesiner/   r/   r0   _eval_rewrite_as_sqrt8  s   
zexp._eval_rewrite_as_sqrtc                 K   s@   |j rdd |jD }|rt|d jd ||d S d S d S )Nc                 S   s(   g | ]}t |trt|jd kr|qS rm   )rh   r4   lenr?   )r`   r   r/   r/   r0   
<listcomp>C  s   ( z,exp._eval_rewrite_as_Pow.<locals>.<listcomp>r   )r   r?   r   r   )r.   r   r  logsr/   r/   r0   _eval_rewrite_as_PowA  s   zexp._eval_rewrite_as_Powrm   Tr   rv   )rn   ro   rp   r   r   r   classmethodr   rt   r   staticmethodr   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r"  r/   r/   r/   r0   r+      s2    
	
i

 	
,
		r+   )	metaclassc                 C   sR   | j tjdd\}}|dkr|jr||fS |tj}|r'|jr'|jr'||fS dS )a  
    Try to match expr with $a + Ib$ for real $a$ and $b$.

    ``match_real_imag`` returns a tuple containing the real and imaginary
    parts of expr or ``(None, None)`` if direct matching is not possible. Contrary
    to :func:`~.re()`, :func:`~.im()``, and ``as_real_imag()``, this helper will not force things
    by returning expressions themselves containing ``re()`` or ``im()`` and it
    does not expand its argument either.

    Tas_Addr   )NN)as_independentr   r   is_realr   )exprr_i_r/   r/   r0   match_real_imagH  s   r/  c                   @   s   e Zd ZU dZee ed< ejej	fZ
d+ddZd+ddZed,d	d
Zdd Zeedd Zd-ddZdd Zd-ddZdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd.d'd(Zd/d)d*ZdS )0r4   a  
    The natural logarithm function `\ln(x)` or `\log(x)`.

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

    Logarithms are taken with the natural base, `e`. To get
    a logarithm of a different base ``b``, use ``log(x, b)``,
    which is essentially short-hand for ``log(x)/log(b)``.

    ``log`` represents the principal branch of the natural
    logarithm. As such it has a branch cut along the negative
    real axis and returns values having a complex argument in
    `(-\pi, \pi]`.

    Examples
    ========

    >>> from sympy import log, sqrt, S, I
    >>> log(8, 2)
    3
    >>> log(S(8)/3, 2)
    -log(3)/log(2) + 3
    >>> log(-1 + I*sqrt(3))
    log(2) + 2*I*pi/3

    See Also
    ========

    exp

    r?   r1   c                 C   s    |dkrd| j d  S t| |)z?
        Returns the first derivative of the function.
        r1   r   )r?   r	   r5   r/   r/   r0   r     s   
z	log.fdiffc                 C   r2   )zC
        Returns `e^x`, the inverse function of `\log(x)`.
        )r+   r5   r/   r/   r0   r7     r8   zlog.inverseNc                 C   sJ  ddl m} ddlm} t|}|d ur`t|}|dkr&|dkr#tjS tjS zt||}|r=|t	|||  t	|  W S t	|t	| W S  t
yN   Y nw |tjur\| || | S | |S |jr|jritjS |tju rqtjS |tju rytjS |tju rtjS |tju rtjS |jr|jdkr| |j S |jr|jtju r|jjr|jS tj}t|tr|jjr|jS t|tr|jjrt|j\}}|r|jr|dtj ; }|tjkr|dtj 8 }|t|| dd S n<t|t rt!|jS t||r|j"j#r	|t	|j"t	|j$S |j"jr|tjt	|j$S tjS t||r%|%| S |jrI|j&r7tj| | |  S |tju r@tjS |tju rItjS |jrPtjS |j's|(|}	|	d ur|	tju rgtjS |	tju rptjS |	jr|	j)rtj| tj* | |	 S tj | tj* | |	  S |jr|j+r|j,|dd\}	}
|	j&r|	d	9 }	|
d	9 }
t|
dd}
|
j,|d
d\}}|(|}|	j-r|r|j-r|j-r|jr|j#rtj| tj* | |	|  S |j&rtj | tj* | |	|   S d S ddl.m/} || 0 }| 0 }i t1dtjd dtjd t1ddt1d  tjd t1dt1dt1d  dt1d  tjd t1ddt1d  tjt2dd t1dt1t1dd  d	t1d  tjt2dd t1dd tjd t1dd tjd t1dt1d t1t1dd  tjd t1dd tjt2dd t1t1dd t1dt1d  tjt2dd t1ddt1d d  tjd t1d t1d dt1t1dd   tjd t1ddt1d d  tjt2dd t1dt1d dt1dt1d   tjt2dd dt1d tjd d	t1d dt1d  tjd dt1d tjt2dd dt1d d	t1d  tjt2dd i}||v rn||	t3|
 }|j#ra| ||||   S | |||| tj   S ||v r||	t3|
 }|j#r| ||||    S | ||tj||    S d S d S d S d S d S d S d S )Nr   r   r   r1   r   Fr   r(  r   T)ratsimpr   r      r      
      )4r   r   r   r   r   r   r   rr   r%   r4   
ValueErrorr   r   rP   r;   r   r   rU   r   r   r   r   r   r+   r~   r   rh   r   r/  r   r   r   ru   r    r   r   r   r   r9   re   r   is_nonnegativer   r   r*  r+  sympy.simplifyr1  r(   r$   r   r#   )r   r   r   r   r   r   r   r-  r.  r   arg_r1  r   t1
atan_tablemodulusr/   r/   r0   r     s"  















 

* 0	&
,,$0$

	'zlog.evalc                 C   s
   | t jfS )zE
        Returns this function in the form (base, exponent).
        )r   r;   r-   r/   r/   r0   r@   &  s   
zlog.as_base_expc                 G   s   ddl m} | dk rtjS t|}| dkr|S |r1|d }|dur1||  | | | d  dddS dd	| d	   || d   | d  S )
zV
        Returns the next term in the Taylor series expansion of `\log(1+x)`.
        r   r   r   Nr1   Tr+   r   r   )r   r   r   r   r   )r   ra   r   r   r   r/   r/   r0   r   ,  s    $zlog.taylor_termTc                 K   s^  ddl m}m} |dd}|dd}t| jdkr&t| j| j ||dS | jd }|jrct	|}d }	d}
|durC|\}}
| |}	|rZt
|}|| vrZtd	d
 | D }	|	d urb|
|	 S n|jrpt|jt|j S |jrg }g }|jD ]C}|s|js|jr| |}t|tr|| |jdi | qz|| qz|jr| | }|| |tj qz|| qzt| tt|  S |jst|tr|s|jjr|j js|jd jr|jd j!s|j jr|j }|j}| |}t|tr	t"||jdi | S t"|| S nt||r*|s|j#jr*|t|j#g|j$R  S | |S )Nr   )r_   r^   forceFfactorr   )r   r=  r1   c                 s   s     | ]\}}|t | V  qd S r*   r3   )r`   valr   r/   r/   r0   rb   Q  s    z'log._eval_expand_log.<locals>.<genexpr>r/   )%sympy.concreter_   r^   getr  r?   r
   r<   
is_Integerr&   r'   keyssumitemsr   r4   r   r   r   r   r   rh   r   _eval_expand_logr9   r   r   r   r   r   r+   r~   r   is_nonpositiver    ri   rj   )r.   r   rk   r_   r^   r=  r>  r   r   logargr   r,  nonposra   r   r\   r]   r/   r/   r0   rF  >  sp   







	
zlog._eval_expand_logc                 K   s   ddl m}m}m} t| jdkr|| j| j fi |S | || jd fi |}|d r3||}||dd}t|| g|d dS )	Nr   )r
   simplifyinversecombiner   r7   Tr0  measure)key)r   r
   rJ  rK  r  r?   r<   r   )r.   r  r
   rJ  rK  r,  r/   r/   r0   _eval_simplifyw  s   zlog._eval_simplifyc                 K   s   | j d }|r| j d j|fi |}t|}||kr | tjfS t|}|ddr;d|d< t|j|fi ||fS t||fS )a  
        Returns this function as a complex coordinate.

        Examples
        ========

        >>> from sympy import I, log
        >>> from sympy.abc import x
        >>> log(x).as_real_imag()
        (log(Abs(x)), arg(x))
        >>> log(I).as_real_imag()
        (0, pi/2)
        >>> log(1 + I).as_real_imag()
        (log(sqrt(2)), pi/4)
        >>> log(I*x).as_real_imag()
        (log(Abs(x)), arg(I*x))

        r   r4   Fcomplex)r?   r   r#   r   r   r   rA  r4   )r.   r   rk   sargsarg_abssarg_argr/   r/   r0   r     s   

zlog.as_real_imagc                 C   s^   | j | j }|j | j kr,| jd d jrdS |jd jr(t| jd d jr*dS d S d S |jS Nr   r1   TF)r<   r?   rP   rQ   r   r.   rR   r/   r/   r0   rT     s    zlog._eval_is_rationalc                 C   s^   | j | j }|j | j kr,| jd d jrdS t| jd d jr(| jd jr*dS d S d S |jS rS  )r<   r?   rP   r   r   rT  r/   r/   r0   r     s   zlog._eval_is_algebraicc                 C      | j d jS rv   r?   rL   r-   r/   r/   r0   r     rW   zlog._eval_is_extended_realc                 C   s   | j d }t|jt|jgS rv   )r?   r   r   r   rP   )r.   rS   r/   r/   r0   r     s   
zlog._eval_is_complexc                 C   s   | j d }|jr
dS |jS Nr   F)r?   rP   rM   rN   r/   r/   r0   rO     s   
zlog._eval_is_finitec                 C      | j d d jS Nr   r1   rV  r-   r/   r/   r0   r     rD   zlog._eval_is_extended_positivec                 C   rX  rY  )r?   rP   r-   r/   r/   r0   rV     rD   zlog._eval_is_zeroc                 C   rX  rY  )r?   is_extended_nonnegativer-   r/   r/   r0   _eval_is_extended_nonnegative  rD   z!log._eval_is_extended_nonnegativer   c                     s  ddl m} ddlm} |}|st|}| jd |kr|S | jd }tdtd}	}
||	||
  }|d urY||	 ||
 }	}
|
dkrY|
|sY|	|sYt|	|
|  }|S dd }z|	|\}}|j
| | |d}W n& tttfy   |j
| |d}|jr d	7  |j
| |d}|jsY nw |r||r|j||d
tj}}n#z| 	|\}}W n tttfy   | |tj}}Y nw t||||   d	   }|tr||}t||r|  |||\}}|jsRt|||  }|}tdddddddddd	}| jdi |}| s4| r4|| t| jdi |}n||t|jdi |}||krI|S |||  | S  fdd}i }t|D ]}|||\}}||tj|  ||< q_tj }	i }|}|	|  k rtj!|	  |	 }|D ]}||tj|||   ||< q|||}|	tj 7 }	|	|  k st|||  }|D ]}||| ||  7 }q|dkr| jd "||}|j#r|j$rt%|dk r|dt& tj' 8 }|||  | S )Nr   r   r   kr  c              	   S   s   t jt j}}t| D ]/}||r7| \}}||kr6z| |W   S  ty5   | t jf Y   S w q||9 }q||fS r*   )	r   r;   r   r   r   hasr@   leadtermr6  )r   ra   r   r+   r>  r   r/   r/   r0   	coeff_exp  s   

z$log._eval_nseries.<locals>.coeff_expr   r1   r   TF)	r   r4   mul	power_exp
power_basemultinomialbasicr=  r>  c                    sN   i }t | |D ]\}}|| }| k r$||tj| | ||   ||< q|S r*   )r   rA  r   r   )d1d2r}   e1e2exr  r/   r0   r`    s   "zlog._eval_nseries.<locals>.mulr   r/   )(r   r   r   r   r4   r?   r   matchr]  r^  r  r6  r  r   r   r   r   r   r   r(   r   r   r+   rh   r   r   dictr:   r  r   r   rA  r;   r   dirr+  r9   r!   r   r   ) r.   ra   r   r   r  r   r   _logxr   r\  r  r  r_  r   r\   rR   r   _dr}   _reslogflagsr,  r`  ptermsr   co1rg  r   pkr   ri  r/   r  r0   r     s   
 



"
"


zlog._eval_nseriesc                 C   s  | j d  }tdddd}|dkrd}|||| }z|j||dd\}}W n ty=   |j|||d}	t|	 Y S w ||rY|||| }|dkrUt	d|  t|S |t
jkrm|t
jkrm|t
j j||dS t||t|  }
|d u rt|n|}|
|| 7 }
|jrt|dkrdd	lm} t||D ]\}}|jr|d
kr nq|d
k r||\}}|
dt t
j |t| d 7 }
|
S )Nr   r   T)realpositiver1   )r   r  r  r   )	Heavisider2  )r?   togetherr   r  r^  r6  r   r4   r]  r   r   r;   r   r9   r!   'sympy.functions.special.delta_functionsrw  	enumeratelseriesr+  as_coeff_exponentr   r   )r.   ra   r   r  r  r   rS   cr]   r   r}   rw  r{   r   r   rn  r/   r/   r0   r  7  s>   
"zlog._eval_as_leading_termrm   r*   r#  r$  rv   ) rn   ro   rp   r   tTupler   __annotations__r   r   rr   rs   r   r7   r%  r   r@   r&  r   r   rF  rN  r   rT   r   r   r   rO   r   rV   r[  r   r  r/   r/   r/   r0   r4   ]  s4   
 !

	 
9
 

jr4   c                       s|   e Zd ZdZeejddd ejfZe	dddZ
dd	d
Zdd Zdd Zdd ZdddZd fdd	Zdd Z  ZS )LambertWa  
    The Lambert W function $W(z)$ is defined as the inverse
    function of $w \exp(w)$ [1]_.

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

    In other words, the value of $W(z)$ is such that $z = W(z) \exp(W(z))$
    for any complex number $z$.  The Lambert W function is a multivalued
    function with infinitely many branches $W_k(z)$, indexed by
    $k \in \mathbb{Z}$.  Each branch gives a different solution $w$
    of the equation $z = w \exp(w)$.

    The Lambert W function has two partially real branches: the
    principal branch ($k = 0$) is real for real $z > -1/e$, and the
    $k = -1$ branch is real for $-1/e < z < 0$. All branches except
    $k = 0$ have a logarithmic singularity at $z = 0$.

    Examples
    ========

    >>> from sympy import LambertW
    >>> LambertW(1.2)
    0.635564016364870
    >>> LambertW(1.2, -1).n()
    -1.34747534407696 - 4.41624341514535*I
    >>> LambertW(-1).is_real
    False

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Lambert_W_function
    r   FrX   Nc                 C   sV  |t jkr	| |S |d u rt j}|jro|jrt jS |t ju r!t jS |dt j kr+t jS |td d kr9td S |dtd krEtdS |t j d krUt jt j d S |t	dt j krat jS |t j
u rit j
S |jrot jS t|jrz|jrzt jS |t ju r|t j d krt j t j d S |dt j krt jS |dt	d krtd S d S d S )Nr   r   r1   rx  )r   r   rP   r   r;   r   r4   r   r   r+   r   r   rU   r   )r   ra   r\  r/   r/   r0   r     sF   






zLambertW.evalr1   c                 C   sr   | j d }t| j dkr|dkrt||dt|   S n| j d }|dkr4t|||dt||   S t| |)z?
        Return the first derivative of this function.
        r   r1   )r?   r  r  r	   )r.   r6   ra   r\  r/   r/   r0   r     s   


zLambertW.fdiffc                 C   s   | j d }t| j dkrtj}n| j d }|jr.|dtj  jr"dS |dtj  jr,dS d S |d jrO|jr@|dtj  jr@dS |jsK|dtj  j	rMdS d S t
|jr`t
|d jrb|jrddS d S d S d S rS  )r?   r  r   r   rP   r   r   rG  r9   r7  r   r~   )r.   ra   r\  r/   r/   r0   r     s*   


zLambertW._eval_is_extended_realc                 C   rU  rv   )r?   rM   r-   r/   r/   r0   rO     rW   zLambertW._eval_is_finitec                 C   sF   | j | j }|j | j kr t| jd jr| jd jrdS d S d S |jS rW  )r<   r?   r   rP   r   rT  r/   r/   r0   r     s   zLambertW._eval_is_algebraicr   c                 C   sF   t | jdkr!| jd }||d }|js| |S ||S d S )Nr1   r   )r  r?   r  r(   rP   r<   r   )r.   ra   r   r  r   r  r/   r/   r0   r    s   


zLambertW._eval_as_leading_termc           
         s   t | jdkrXddlm} ddlm} | jd j|||d  j||d}d}|jr-|j	}||| dkrLt
 fddtd||| D  }	t|	}	ntj}	|	||| | S t |||S )	Nr1   r   r   r   r   r   c                    s@   g | ]}t j |d   t||d   t|d    |  qS )r1   r   )r   r;   r   r   )r`   r\  r   r/   r0   r     s    
z*LambertW._eval_nseries.<locals>.<listcomp>)r  r?   r   r   r   r   r  compute_leading_termr   r+   r   r  r   r   r   superr   )
r.   ra   r   r   r  r   r   ltlterR   r   r   r0   r     s    
zLambertW._eval_nseriesc                 C   s4   | j d }t| j dkr|jS t|j| j d jgS rY  )r?   r  rP   r   )r.   ra   r/   r/   r0   rV     s   
zLambertW._eval_is_zeror*   rm   rv   r$  )rn   ro   rp   r   r   r   r   rr   rs   r%  r   r   r   rO   r   r  r   rV   __classcell__r/   r/   r  r0   r  d  s    "
%
r  N)C	itertoolsr   typingr   r  sympy.core.exprr   
sympy.corer   sympy.core.addr   sympy.core.cacher   sympy.core.functionr   r	   r
   r   r   r   r   r   sympy.core.logicr   r   r   sympy.core.mulr   sympy.core.numbersr   r   r   r   r   sympy.core.parametersr   sympy.core.powerr   sympy.core.singletonr   sympy.core.symbolr   r   (sympy.functions.combinatorial.factorialsr   r   r   r    r!   r"   r#   (sympy.functions.elementary.miscellaneousr$   sympy.ntheoryr%   r&   sympy.ntheory.factor_r'   sympy.polys.polytoolsr(   r)   ru   r   r+   r/  r4   r  r/   r/   r/   r0   <module>   sB    (fI  p    