a
    RG5dO=                     @   s   d Z ddlmZmZ ddlmZmZ ddlmZ ddlm	Z	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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  ddl!Z!G dd deZ"edddddZ#dS )aQ  Implicit plotting module for SymPy.

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

The module implements a data series called ImplicitSeries which is used by
``Plot`` class to plot implicit plots for different backends. The module,
by default, implements plotting using interval arithmetic. It switches to a
fall back algorithm if the expression cannot be plotted using interval arithmetic.
It is also possible to specify to use the fall back algorithm for all plots.

Boolean combinations of expressions cannot be plotted by the fall back
algorithm.

See Also
========

sympy.plotting.plot

References
==========

.. [1] Jeffrey Allen Tupper. Reliable Two-Dimensional Graphing Methods for
Mathematical Formulae with Two Free Variables.

.. [2] Jeffrey Allen Tupper. Graphing Equations with Generalized Interval
Arithmetic. Master's thesis. University of Toronto, 1996

   )
BaseSeriesPlot)experimental_lambdifyvectorized_lambdifyinterval    )EqualityGreaterThanLessThan
RelationalStrictLessThanStrictGreaterThan)Tuple)Eq)DummySymbol)sympify)import_module)BooleanFunction)
_sort_gens)doctest_depends_on)flattenNc                       sD   e Zd ZdZdZ fddZdd Zdd Zd	d
 Zdd Z	  Z
S )ImplicitSeriesz" Representation for Implicit plot Tc	           	         s   t    t|| _| j| _t|d | _t|d | _t|d | _t|d | _	t|d | _
t|d | _| j| _|| _|| _|| _d| | _|| _d S )Nr   r         )super__init__r   exprlabelvar_xfloatstart_xend_xvar_ystart_yend_y
get_raster
get_pointshas_equalitynb_of_pointsuse_interval_mathdepth
line_color)	selfr   var_start_end_xvar_start_end_yr)   r+   r,   r*   r-   	__class__ X/var/www/html/django/DPS/env/lib/python3.9/site-packages/sympy/plotting/plot_implicit.pyr   5   s    


zImplicitSeries.__init__c                 C   s<   dt | jt | jt | j| jft | jt | j| jff S )Nz3Implicit equation: %s for %s over %s and %s over %s)strr   r    r"   r#   r$   r%   r&   )r.   r3   r3   r4   __str__I   s    zImplicitSeries.__str__c                 C   s   t | j| jf| jdd}t| j| j}t| j| j}z||| W n, t	yn   | j
rdtjddd d| _
Y n0 | j
r| |S |  S d S )NT)use_intervalzOAdaptive meshing could not be applied to the expression. Using uniform meshing.   )
stacklevelF)r   r    r$   r   r   r"   r#   r%   r&   AttributeErrorr+   warningswarn_get_raster_interval_get_meshes_grid)r.   funcZ	xintervalZ	yintervalr3   r3   r4   r'   R   s     
zImplicitSeries.get_rasterc                    s  | j }g }td}|| j| jd}|| j| jd}|jt	|d d | j| j  d }|jt	|d d | j| j  d }||7 }||7 }dd t
|dd	 |dd D }	d
d t
|dd	 |dd D fdd|	D }g }
 fdd}|dkr6t	|r6||\}}|
| |d }q | jr|D ]F}|d }|d } ||}|d rB|d durB|
||g qB|
dfS )z: Uses interval math to adaptively mesh and obtain the plotnumpy!   r   r   i   c                 S   s   g | ]\}}t ||qS r3   r   ).0x1x2r3   r3   r4   
<listcomp>z       z7ImplicitSeries._get_raster_interval.<locals>.<listcomp>Nc                 S   s   g | ]\}}t ||qS r3   r   )rB   y1y2r3   r3   r4   rE   |   rF   c                    s   g | ]} D ]}||gqqS r3   r3   )rB   xy)yinterr3   r4   rE   ~   rF   c                    s   g }g }| D ]}|d }|d } ||}|d du s|d du rDq|dkr\| ||g q|d du st|d du r|j}|j}t|j|}	t||j}
t|j|}t||j}| |	|g | |	|g | |
|g | |
|g q||fS )zj Evaluates the intervals and subdivides the interval if the
            expression is partially satisfied.r   r   F)TTN)appendmidr   startend)interval_listZtemp_interval_list	plot_list	intervals	intervalx	intervaly	func_evalZavgxZavgyabcd)r?   r3   r4   refine_pixels   s,    
z:ImplicitSeries._get_raster_interval.<locals>.refine_pixelsr   Ffill)r,   r   linspacer"   r#   r%   r&   randomrandlenzipextendr)   rM   )r.   r?   krQ   npZxsampleZysampleZjitterxZjitteryZxinterrR   r[   Zplot_list_temprS   rT   rU   rV   r3   )r?   rL   r4   r=   g   sd    





z#ImplicitSeries._get_raster_intervalc           
      C   s  d}t | jtr&| jj| jj }d}nLt | jttfrH| jj| jj }n*t | jttfrj| jj| jj }nt	dt
d}|| j| j| j}|| j| j| j}|||\}}t| j| jf|}|||}	d|	|j|	dk < d|	|j|	dk< |r|||	dfS |||	d	fS d
S )zGenerates the mesh for generating a contour.

        In the case of equality, ``contour`` function of matplotlib can
        be used. In other cases, matplotlib's ``contourf`` is used.
        FTzDThe expression is not supported for plotting in uniform meshed plot.r@   rG   r   r   contourcontourfN)
isinstancer   r	   lhsrhsr
   r   r   r   NotImplementedErrorr   r]   r"   r#   r*   r%   r&   meshgridr   r    r$   mawhere)
r.   equalr   rd   xarrayyarrayZx_gridZy_gridr?   Zz_gridr3   r3   r4   r>      s(    
zImplicitSeries._get_meshes_grid)__name__
__module____qualname____doc__is_implicitr   r6   r'   r=   r>   __classcell__r3   r3   r1   r4   r   1   s   	Lr   )
matplotlib)modulesT,  bluec              	      s  d}	 fdd g t | tr@ |  tdd D rnd}	n.t | tsZt| d} d}	nt | tttfrnd}	dd	 ||fD }
| j}t	t
|
 j}|| }t||@ d
krtdt	ddfdd}t|
dkrtt|}
||
d }|d }t|
d
kr8||v s|s*|
td|j  n|
|  ||
d }|dkrTd}n|dk rbd}t| |||	||||}tdd |dd D |d< tdd |dd D |d< |d|d  |d|d  t|fi |}|r|  |S )a	  A plot function to plot implicit equations / inequalities.

    Arguments
    =========

    - ``expr`` : The equation / inequality that is to be plotted.
    - ``x_var`` (optional) : symbol to plot on x-axis or tuple giving symbol
      and range as ``(symbol, xmin, xmax)``
    - ``y_var`` (optional) : symbol to plot on y-axis or tuple giving symbol
      and range as ``(symbol, ymin, ymax)``

    If neither ``x_var`` nor ``y_var`` are given then the free symbols in the
    expression will be assigned in the order they are sorted.

    The following keyword arguments can also be used:

    - ``adaptive`` Boolean. The default value is set to True. It has to be
        set to False if you want to use a mesh grid.

    - ``depth`` integer. The depth of recursion for adaptive mesh grid.
        Default value is 0. Takes value in the range (0, 4).

    - ``points`` integer. The number of points if adaptive mesh grid is not
        used. Default value is 300.

    - ``show`` Boolean. Default value is True. If set to False, the plot will
        not be shown. See ``Plot`` for further information.

    - ``title`` string. The title for the plot.

    - ``xlabel`` string. The label for the x-axis

    - ``ylabel`` string. The label for the y-axis

    Aesthetics options:

    - ``line_color``: float or string. Specifies the color for the plot.
        See ``Plot`` to see how to set color for the plots.
        Default value is "Blue"

    plot_implicit, by default, uses interval arithmetic to plot functions. If
    the expression cannot be plotted using interval arithmetic, it defaults to
    a generating a contour using a mesh grid of fixed number of points. By
    setting adaptive to False, you can force plot_implicit to use the mesh
    grid. The mesh grid method can be effective when adaptive plotting using
    interval arithmetic, fails to plot with small line width.

    Examples
    ========

    Plot expressions:

    .. plot::
        :context: reset
        :format: doctest
        :include-source: True

        >>> from sympy import plot_implicit, symbols, Eq, And
        >>> x, y = symbols('x y')

    Without any ranges for the symbols in the expression:

    .. plot::
        :context: close-figs
        :format: doctest
        :include-source: True

        >>> p1 = plot_implicit(Eq(x**2 + y**2, 5))

    With the range for the symbols:

    .. plot::
        :context: close-figs
        :format: doctest
        :include-source: True

        >>> p2 = plot_implicit(
        ...     Eq(x**2 + y**2, 3), (x, -3, 3), (y, -3, 3))

    With depth of recursion as argument:

    .. plot::
        :context: close-figs
        :format: doctest
        :include-source: True

        >>> p3 = plot_implicit(
        ...     Eq(x**2 + y**2, 5), (x, -4, 4), (y, -4, 4), depth = 2)

    Using mesh grid and not using adaptive meshing:

    .. plot::
        :context: close-figs
        :format: doctest
        :include-source: True

        >>> p4 = plot_implicit(
        ...     Eq(x**2 + y**2, 5), (x, -5, 5), (y, -2, 2),
        ...     adaptive=False)

    Using mesh grid without using adaptive meshing with number of points
    specified:

    .. plot::
        :context: close-figs
        :format: doctest
        :include-source: True

        >>> p5 = plot_implicit(
        ...     Eq(x**2 + y**2, 5), (x, -5, 5), (y, -2, 2),
        ...     adaptive=False, points=400)

    Plotting regions:

    .. plot::
        :context: close-figs
        :format: doctest
        :include-source: True

        >>> p6 = plot_implicit(y > x**2)

    Plotting Using boolean conjunctions:

    .. plot::
        :context: close-figs
        :format: doctest
        :include-source: True

        >>> p7 = plot_implicit(And(y > x, y > -x))

    When plotting an expression with a single variable (y - 1, for example),
    specify the x or the y variable explicitly:

    .. plot::
        :context: close-figs
        :format: doctest
        :include-source: True

        >>> p8 = plot_implicit(y - 1, y_var=y)
        >>> p9 = plot_implicit(x - 1, x_var=x)
    Fc                    s8   | j D ],}t|tr | qt|tr| qdS )zJ
        Recursively expands the arguments of an Boolean Function
        N)argsrg   r   r   rM   )Z	bool_exprarg)
arg_expandarg_listr3   r4   r}   h  s
    



z!plot_implicit.<locals>.arg_expandc                 s   s   | ]}t |tttfV  qd S N)rg   r	   r
   r   )rB   er3   r3   r4   	<genexpr>w  s   z plot_implicit.<locals>.<genexpr>Tr   c                 S   s   g | ]}|d ur|qS r   r3   )rB   ir3   r3   r4   rE     rF   z!plot_implicit.<locals>.<listcomp>r   z>Implicit plotting is not implemented for more than 2 variables   c                    s:   t | trt|   S t| dkr*t|  S td|  d S )N   z2symbol or `(symbol, min, max)` expected but got %s)rg   r   r   r`   
ValueError)s)default_ranger3   r4   _range_tuple  s
    
z#plot_implicit.<locals>._range_tuplezf(%s)r   r   c                 s   s   | ]}t |V  qd S r   r!   )rB   rJ   r3   r3   r4   r     rF   Nxlimc                 s   s   | ]}t |V  qd S r   r   )rB   rK   r3   r3   r4   r     rF   ylimxlabelylabel)rg   r   anyr   r   r	   r
   r   free_symbolsr   r   r`   rj   listr   rM   r   namepopr   tuple
setdefaultr   show)r   x_vary_varadaptiver,   pointsr-   r   kwargsr)   Zxyvarr   Zrange_symbolsZ
undeclaredr   r/   rJ   r0   Zseries_argumentpr3   )r}   r~   r   r4   plot_implicit   s`     






r   )NNTr   ry   rz   T)$rt   plotr   r   r   r   intervalmathr   sympy.core.relationalr	   r
   r   r   r   r   sympy.core.containersr   r   sympy.core.symbolr   r   sympy.core.sympifyr   sympy.externalr   sympy.logic.boolalgr   sympy.polys.polyutilsr   sympy.utilities.decoratorr   sympy.utilities.iterablesr   r;   r   r   r3   r3   r3   r4   <module>   s(     %  