
    "`)                         d Z ddlmZmZ ddlmZ ddlmZ  G d de      Z	dd	gd
dd
d
g d
fdZ
d Zg dZd Zdd	gdd	gdd
dd
d
d
fdZdd	gdd	gdddd
d
d
fdZe
e	_
        ee	_        ee	_        ee	_        ee	_        y
)z 
Plotting (requires matplotlib)
    )
hsv_to_rgb
hls_to_rgb   )NoConvergence)xrangec                       e Zd ZeeeefZy)VisualizationMethodsN)__name__
__module____qualname__
ValueErrorArithmeticErrorZeroDivisionErrorr   plot_ignore     6/usr/lib/python3/dist-packages/mpmath/visualization.pyr	   r	   	   s    0A=QKr   r	      N   c	           	         |rd}d}	|s%ddl }
|
j                         }	|	j                  d      }t        |t        t
        f      s|g}|\  }}g d}t        |      D ]#  \  }}| j                  ||||z
  t        |      z        }g }g }d}t        t        |            D ]"  }	 |dk7  r!|D ]  }||dz
     |k  s||   |k\  st          |||         }| j                  |      st        |      dkD  rt        t        |d      rn|j                  rbt        |j                         }t        |j                        }|sd	}|j#                  |       g }|j#                  t        ||         ||f       nN|rd}|j#                  |       g }t        |d
      r|j                   }|j#                  t        ||         |f       % |r|j#                  |       |D ]  }|D cg c]  }|d   	 }}|D cg c]  }|d   	 }}|s*||t        |      z     }t        |d         dk(  rC|D cg c]  }|d   	 }}|j'                  ||d|z   d       |j'                  ||d|z   d       |j'                  |||d        & |j)                  |D cg c]  }t        |       c}       |r(|j+                  |D cg c]  }t        |       c}       |j-                  d       |j/                  d       |j1                  d	       |	r'|r
j3                  ||       y
j5                          yy# | j$                  $ r |r|j#                  |       g }Y w xY wc c}w c c}w c c}w c c}w c c}w )a  
    Shows a simple 2D plot of a function `f(x)` or list of functions
    `[f_0(x), f_1(x), \ldots, f_n(x)]` over a given interval
    specified by *xlim*. Some examples::

        plot(lambda x: exp(x)*li(x), [1, 4])
        plot([cos, sin], [-4, 4])
        plot([fresnels, fresnelc], [-4, 4])
        plot([sqrt, cbrt], [-4, 4])
        plot(lambda t: zeta(0.5+t*j), [-20, 20])
        plot([floor, ceil, abs, sign], [-5, 5])

    Points where the function raises a numerical exception or
    returns an infinite value are removed from the graph.
    Singularities can also be excluded explicitly
    as follows (useful for removing erroneous vertical lines)::

        plot(cot, ylim=[-5, 5])   # bad
        plot(cot, ylim=[-5, 5], singularities=[-pi, 0, pi])  # good

    For parts where the function assumes complex values, the
    real part is plotted with dashes and the imaginary part
    is plotted with dots.

    .. note :: This function requires matplotlib (pylab).
    Nr   o   )brgmkFr   gu <7~imagTreal      z--)	linewidth:xzf(x)dpi)pylabfigureadd_subplot
isinstancetuplelist	enumeratearangefloatr   lenr   isnanabshasattrr   r   appendr   plotset_xlimset_ylim
set_xlabel
set_ylabelgridsavefigshow)ctxfxlimylimpointsfiler&   singularitiesaxesfigr'   ar   colorsnfuncr$   segmentssegment
in_complexisingvreimsycz_s                                 r   r5   r5      sI   8 
Cllns#a%'CDAq&FQ< /04JJq!ac5=01
A 	A6 - -QqS6T>addl",,- 1J99Q<3q6E>$$1f%!&&qvvBqvvB%%)
 0"$NNE!A$KR#89!%*
 0"$q&)FFNNE!A$K#341	: OOG$ 	0G&'!1'A'&'!1'A'q3v;'A71:!##*+aQqT++		!QQ!	4		!QA	3		!QQ	/	0I/0` 	MMT*58*+.AuQx./OOCOOFIIdO
MM$CM(JJL	 1 ?? OOG, ('
 ,
 +.s=   #L19L1C?L1 M2M!)M&M+;M01$MMc                 &   | j                  |      ry| j                  |      ryd}t        | j                  |            | j                  z   d| j                  z  z  }|dz   dz  }dt        ddt        |      dz  z   z        z
  }t        ||d	      S )
N      ?rY   rY         ?r[   r[   (-DT!	@r!   r[   rY   r   333333?皙?)isinfr1   r/   argpir2   r   )r=   rU   ra   rF   r   s        r   default_color_functionrb   o   s    
yy|
yy|	B	swwqz	SVV	##&&1A	
SCAeAs3q63;'((AaC  r   )
)              re   re   )gffffff)皙?g?r[   )g      )re   r[   rY   )g)g?r^   r^   )re   rX   )g?)rY   ?r]   )r[   )rg   r[   re   )gffffff?)gffffff?rf   re   )rY   rd   )       @rd   c                    | j                  |      ry| j                  |      ryd}t        | j                  |            |z  }t	        t        |d      d      }t        dt        t                    D ]a  }t        |   d   |kD  st        |dz
     \  }\  }}}t        |   \  }	\  }
}}||z
  |	|z
  z  }||
|z
  |z  z   |||z
  |z  z   |||z
  |z  z   fc S  y )NrX   rZ   r\   rY   rc   r   r   )	r_   r1   r/   r`   maxminranger0   blue_orange_colors)r=   rU   ra   wrM   rF   ragabar   rbgbbbrR   s                 r   phase_color_functionru      s    
yy|
yy|	BcggajBAC3KA1S+,- <a #a'015OA|B03OA|B11Ar"uai<RUAIr2b5!)|;;<r   i  Fc
                    ||dk(  r| j                   }|dk(  r| j                  }ddl}
|rd}	d}|	s!|
j                         }|j	                  d      }	|\  }}|\  }}||z
  }||z
  }t        | j                  ||z  |z        dz         }t        | j                  ||z  |z        dz         }|
j                  |||      }|
j                  |||      }|
j                  ||df      }t        |      D ]f  }t        |      D ]0  }| j                  ||   ||         }	  | ||            }||||f<   2 |sDt        t        |      d	z   t        |      z          h ||||fD cg c]  }t        |       c}\  }}}}|	j                  |||||fd
       |	j!                  d       |	j#                  d       |r'|r|
j%                  ||       y|
j'                          yy# | j                  $ r d}Y w xY wc c}w )a  
    Plots the given complex-valued function *f* over a rectangular part
    of the complex plane specified by the pairs of intervals *re* and *im*.
    For example::

        cplot(lambda z: z, [-2, 2], [-10, 10])
        cplot(exp)
        cplot(zeta, [0, 1], [0, 50])

    By default, the complex argument (phase) is shown as color (hue) and
    the magnitude is show as brightness. You can also supply a
    custom color function (*color*). This function should take a
    complex number as input and return an RGB 3-tuple containing
    floats in the range 0.0-1.0.

    Alternatively, you can select a builtin color function by passing
    a string as *color*:

      * "default" - default color scheme
      * "phase" - a color scheme that only renders the phase of the function,
         with white for positive reals, black for negative reals, gold in the
         upper half plane, and blue in the lower half plane.

    To obtain a sharp image, the number of points may need to be
    increased to 100,000 or thereabout. Since evaluating the
    function that many times is likely to be slow, the 'verbose'
    option is useful to display progress.

    .. note :: This function requires matplotlib (pylab).
    Ndefaultphaser   r   r   r    rZ   z of lower)extentoriginzRe(z)zIm(z)r%   )rb   ru   r'   r(   r)   intsqrtlinspacezerosr   mpcr   printstrr/   imshowr8   r9   r;   r<   )r=   r>   rP   rQ   rA   colorverboserB   r&   rD   r'   rE   rearebimaimbdredimMNr$   rS   rn   rH   r   rU   rO   rV   s                               r   cplotr      s   @ }***((
Cllns#HCHC
)C
)CCHHVCZ^$Q&'ACHHVCZ^$Q&'AsC#AsC#A
 	Q1IAAY 	, 	A!ad#A$!A$K AacF	 #a&6/CF*+	, .1#sC,@Aq%(ACc3KK3S#.wK?OOGOOG
MM$CM(JJL	  ?? $#$
 Bs   G "G7 G43G4d   Tc
           	         ddl }
ddlm} |rd}	d}|	s+|
j                         }|j                  j                  |      }	|\  }}|\  }}||z
  }||z
  }t        |t        t        f      s||g}|\  }}|
j                  |||      }|
j                  |||      }t        d      D cg c]  }|
j                  ||f       c}\  }}}t        d      D cg c]  }ddg c}\  }}}t        |      D ]  }t        |      D ]  } || j                  ||         | j                  ||               }	 |\  |||f<   |||f<   |||f<   |||f   |f|||f   |f|||f   |ffD ]   \  }} || d   k  r|| d<   || d   kD  s|| d<   "   |r|	j                  |||dd       n|	j                  |||dd       |	j!                  d       |	j#                  d       |	j%                  d	       |r|||fD  cg c]  } | d   | d   z
   c} \  }!}"}#t'        |!|"|#      }$|!|$k  r)|$|!z
  }%|	j)                  |d   |%d
z  z
  |d   |%d
z  z          |"|$k  r)|$|"z
  }%|	j+                  |d   |%d
z  z
  |d   |%d
z  z          |#|$k  r)|$|#z
  }%|	j-                  |d   |%d
z  z
  |d   |%d
z  z          |r'|r|
j/                  ||       y|
j1                          yyc c}w c c}w # t        $ r  ||   ||   |c|||f<   |||f<   |||f<   Y w xY wc c} w )a  
    Plots the surface defined by `f`.

    If `f` returns a single component, then this plots the surface
    defined by `z = f(x,y)` over the rectangular domain with
    `x = u` and `y = v`.

    If `f` returns three components, then this plots the parametric
    surface `x, y, z = f(u,v)` over the pairs of intervals `u` and `v`.

    For example, to plot a simple function::

        >>> from mpmath import *
        >>> f = lambda x, y: sin(x+y)*cos(y)
        >>> splot(f, [-pi,pi], [-pi,pi])    # doctest: +SKIP

    Plotting a donut::

        >>> r, R = 1, 2.5
        >>> f = lambda u, v: [r*cos(u), (R+r*sin(u))*cos(v), (R+r*sin(u))*sin(v)]
        >>> splot(f, [0, 2*pi], [0, 2*pi])    # doctest: +SKIP

    .. note :: This function requires matplotlib (pylab) 0.98.5.3 or higher.
    r   Nr    r      )rstridecstrider$   rS   rU   rh   r%   )r'   mpl_toolkits.mplot3dmplot3dr(   axes3dAxes3Dr*   r,   r+   r~   r   r   convert	TypeErrorplot_wireframeplot_surfacer8   r9   
set_zlabelrj   
set_xlim3d
set_ylim3d
set_zlim3dr;   r<   )&r=   r>   urO   rA   keep_aspect	wireframerB   r&   rD   r'   r   rE   uaubvavbdudvr   r   rM   r$   rS   rU   xabyabzabrH   r   fdatarT   cabdxdydzmaxddeltas&                                         r   splotr      sr   4 *
Clln~~$$S)FBFB	bB	bBftUm,&!DAqr2q!Ar2q!A,21I6qu{{Aq6"6GAq!%+AY/aV/MCcAY  
	Ackk!A$'QqT):;E;).&!A#!A#!A# acFC=1QqS63-!AaC&#G 3s1v:CFs1v:CF	
	 Aq!Q:!Q1a8OOCOOCOOC25sCA#c!fs1voA
B2r292IEOOCFUS[0#a&53;2FG92IEOOCFUS[0#a&53;2FG92IEOOCFUS[0#a&53;2FG
MM$CM(JJL	 C 7/  ;)*1qtU&!A#!A#!A#; Bs$   $K	K.K/K?%K<;K<)__doc__colorsysr   r   libmpr   libmp.backendr   objectr	   r5   rb   rm   ru   r   r   r   r   r   <module>r      s    ,   !R6 R !f4$D4aF	! < VAt4$TJX Fr!fSd$TPf !  .D  +,@  )"  "  r   