U
    Hvf¹  ã                   @   s\   d Z ddlZzddlZddlmZ W n ek
r8   Y nX dd„ Zdd„ Z	e
dkrXe	ƒ  dS )zŠCompute a Pade approximation for the principal branch of the
Lambert W function around 0 and compare it to various other
approximations.

é    Nc                  C   s,   dd„ t dƒD ƒ} t | dd¡\}}||fS )Nc                 S   s   g | ]}t jt jd |d‘qS )r   )Ún)ÚmpmathZdiffÚlambertw)Ú.0r   © r   úF/tmp/pip-unpacked-wheel-96ln3f52/scipy/special/_precompute/lambertw.pyÚ
<listcomp>   s     z!lambertw_pade.<locals>.<listcomp>é   é   é   )Úranger   Zpade)ZderivsÚpÚqr   r   r   Úlambertw_pade   s    r   c               	   C   sP  t tƒ t d¡J tƒ \} }| d d d… |d d d…  } }t d | ¡ƒ t d |¡ƒ W 5 Q R X t ddd¡t ddd¡ }}t ||¡\}}|d|  }g }| 	¡ D ]}| 
tt |¡ƒ¡ q¨t |¡ |j¡}tjd	d
d\}}t dd„ | D ƒ¡} t dd„ |D ƒ¡}t | |¡t ||¡ }	t|	| ƒ}
|d  |||
¡ t |¡t t |¡¡ }t|| ƒ}|d
  |||¡ t dt d
¡| d
  ¡} d|  | d d	  }t|| ƒ}|d  |||¡}|j|| ¡  ¡ d t ¡  tjd
d
d\}}|
|k }| |||¡}t dd¡}| dt|ƒ d |d¡ |j||d t ¡  d S )Né2   éÿÿÿÿzp = {}zq = {}g      ø¿g      ø?éK   y              ð?r
   é   )ZnrowsZncolsc                 S   s   g | ]}t |ƒ‘qS r   ©Úfloat)r   Zp0r   r   r   r   '   s     zmain.<locals>.<listcomp>c                 S   s   g | ]}t |ƒ‘qS r   r   )r   Zq0r   r   r   r   (   s     r   r   )Úaxg333333Ó¿g333333Ó?g      Àgš™™™™™É?Úr)ÚprintÚ__doc__r   Zworkdpsr   ÚformatÚnpZlinspaceZmeshgridÚflattenÚappendÚcomplexr   ÚarrayZreshapeÚshapeÚpltZsubplotsZpolyvalÚabsZ
pcolormeshÚlogÚsqrtÚexpZcolorbarZravelÚtolistÚshowZplot)r   r   ÚxÚyÚzZlambertw_stdZz0ZfigZaxesZpade_approxZpade_errZ
asy_approxZasy_errZseries_approxZ
series_errZimr   Zpade_betterÚtr   r   r   Úmain   sF    
r,   Ú__main__)r   Znumpyr   r   Zmatplotlib.pyplotZpyplotr!   ÚImportErrorr   r,   Ú__name__r   r   r   r   Ú<module>   s   .