U
    ZÊ¹jã/  ã                   @   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 d dlmZ d d	lmZ d d
lmZ d dlm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S )é    ©ÚTuple)ÚBasic)ÚExpr)ÚAppliedUndef)Ú
Relational)ÚDummy)Úsympify)ÚBooleanFunction)ÚImageSet)Ú	FiniteSet)ÚIndexedc                 C   sr   t | tttfƒs| g} tdd„ | D ƒƒr.tƒ S tƒ jdd„ | D ƒŽ }|jdd„ | D ƒŽ }|pptƒ jdd„ | D ƒŽ S )a  Returns the free symbols of a symbolic expression.

    If the expression contains any of these elements, assume that they are
    the "free symbols" of the expression:

    * indexed objects
    * applied undefined function (useful for sympy.physics.mechanics module)
    c                 s   s   | ]}t |ƒV  qd S ©N©Úcallable©Ú.0Úe© r   úS/var/www/html/bwave/python/venv/lib/python3.8/site-packages/sympy/plotting/utils.pyÚ	<genexpr>   s     z$_get_free_symbols.<locals>.<genexpr>c                 S   s   g | ]}|  t¡‘qS r   )Úatomsr   r   r   r   r   Ú
<listcomp>   s     z%_get_free_symbols.<locals>.<listcomp>c                 S   s   g | ]}|  t¡‘qS r   )r   r   r   r   r   r   r      s     c                 S   s   g | ]
}|j ‘qS r   ©Úfree_symbolsr   r   r   r   r      s     )Ú
isinstanceÚlistÚtupleÚsetÚallÚunion)ÚexprsÚfreer   r   r   Ú_get_free_symbols   s    	r#   é
   c                    sH   |   t¡}|D ]4}t|ƒ‰ t‡ fdd„td|ƒD ƒŽ }|  ||¡} q| S )a“  Extract numerical solutions from a set solution (computed by solveset,
    linsolve, nonlinsolve). Often, it is not trivial do get something useful
    out of them.

    Parameters
    ==========

    n : int, optional
        In order to replace ImageSet with FiniteSet, an iterator is created
        for each ImageSet contained in `set_sol`, starting from 0 up to `n`.
        Default value: 10.
    c                    s   g | ]}t ˆ ƒ‘qS r   )Únext)r   Ún©Úitr   r   r   1   s     z$extract_solution.<locals>.<listcomp>r   )Úfindr   Úiterr   ÚrangeÚsubs)Zset_solr&   ZimagesZimÚsr   r'   r   Úextract_solution!   s    
r.   c                 C   sŠ   t | tƒr| S t| ƒ} t| ƒD ]f\}}t |ttfƒrLtt|ƒddiŽ| |< qt |ttfƒst	|ƒs|j
jdkrxt |tƒrt|ƒ| |< q| S )a  This function recursively loop over the arguments passed to the plot
    functions: the sympify function will be applied to all arguments except
    those of type string/dict.

    Generally, users can provide the following arguments to a plot function:

    expr, range1 [tuple, opt], ..., label [str, opt], rendering_kw [dict, opt]

    `expr, range1, ...` can be sympified, whereas `label, rendering_kw` can't.
    In particular, whenever a special character like $, {, }, ... is used in
    the `label`, sympify will raise an error.
    r	   FZVector)r   r   r   Ú	enumerater   r   Ú_plot_sympifyÚstrÚdictr   Ú	__class__Ú__name__r   r	   )ÚargsÚiÚar   r   r   r0   6   s    

ûûr0   Ú Nc                 C   sZ  dd„ }t | ƒ}|dk	r&| | ¡ ¡}t|ƒ|krTtdd |¡ d t|ƒ|¡ ƒ‚t|ƒ|krttdt|ƒ|f ƒ‚tƒ  dd	„ |D ƒ¡}t|ƒt|ƒkr¢td
ƒ‚t|ƒ|k �r| |¡}|tƒ krÜ|D ]}	| ||	ƒ¡ qÈt	|t|ƒ ƒD ]}
| |t
ƒ ƒ¡ qìt|ƒ|k�rVtƒ  dd	„ |D ƒ¡}t| |¡ƒdk�rVtdd |¡ d |¡ ƒ‚|S )a  This function does two things:

    1. Check if the number of free symbols is in agreement with the type of
       plot chosen. For example, plot() requires 1 free symbol;
       plot3d() requires 2 free symbols.
    2. Sometime users create plots without providing ranges for the variables.
       Here we create the necessary ranges.

    Parameters
    ==========

    exprs : iterable
        The expressions from which to extract the free symbols
    ranges : iterable
        The limiting ranges provided by the user
    npar : int
        The number of free symbols required by the plot functions.
        For example,
        npar=1 for plot, npar=2 for plot3d, ...
    params : dict
        A dictionary mapping symbols to parameters for interactive plot.
    c                 S   s   t | ddƒS )Niöÿÿÿr$   r   )Úsymbolr   r   r   Ú<lambda>l   ó    z _create_ranges.<locals>.<lambda>NzToo many free symbols.
zExpected {} free symbols.
zReceived {}: {}z)Too many ranges. Received %s, expected %sc                 S   s   g | ]}|d  ‘qS ©r   r   ©r   Úrr   r   r   r   ~   s     z"_create_ranges.<locals>.<listcomp>z$Multiple ranges with the same symbolc                 S   s   g | ]}|d  ‘qS r<   r   r=   r   r   r   r   �   s     r   z>Incompatible free symbols of the expressions with the ranges.
z$Free symbols in the expressions: {}
zFree symbols in the ranges: {})r#   Ú
differenceÚkeysÚlenÚ
ValueErrorÚformatr   r    Úappendr+   r   )r!   ÚrangesÚnparÚlabelÚparamsZget_default_ranger   ZrfsÚsymbolsr-   r6   r   r   r   Ú_create_rangesU   sJ    ÿþÿÿ

þýÿrJ   c                 C   sR   t | tƒoPt| ƒdkoPt | jd tƒ oP| jd joPt | jd tƒ oP| jd jS )zUA range is defined as (symbol, start, end). start and end should
    be numbers.
    é   é   é   )r   r   rA   r5   r1   Z	is_number)r>   r   r   r   Ú	_is_range›   s    

ÿþ
þý
ürN   c                  G   sx   dd„ | D ƒ}dd„ | D ƒ}|s$dn|d }dd„ | D ƒ}|sBdn|d }dd„ | D ƒ}dd„ t | |ƒD ƒ}||||fS )	a¢  Given a list/tuple of arguments previously processed by _plot_sympify()
    and/or _check_arguments(), separates and returns its components:
    expressions, ranges, label and rendering keywords.

    Examples
    ========

    >>> from sympy import cos, sin, symbols
    >>> from sympy.plotting.utils import _plot_sympify, _unpack_args
    >>> x, y = symbols('x, y')
    >>> args = (sin(x), (x, -10, 10), "f1")
    >>> args = _plot_sympify(args)
    >>> _unpack_args(*args)
    ([sin(x)], [(x, -10, 10)], 'f1', None)

    >>> args = (sin(x**2 + y**2), (x, -2, 2), (y, -3, 3), "f2")
    >>> args = _plot_sympify(args)
    >>> _unpack_args(*args)
    ([sin(x**2 + y**2)], [(x, -2, 2), (y, -3, 3)], 'f2', None)

    >>> args = (sin(x + y), cos(x - y), x + y, (x, -2, 2), (y, -3, 3), "f3")
    >>> args = _plot_sympify(args)
    >>> _unpack_args(*args)
    ([sin(x + y), cos(x - y), x + y], [(x, -2, 2), (y, -3, 3)], 'f3', None)
    c                 S   s   g | ]}t |ƒr|‘qS r   ©rN   ©r   Útr   r   r   r   Â   s      z _unpack_args.<locals>.<listcomp>c                 S   s   g | ]}t |tƒr|‘qS r   ©r   r1   rP   r   r   r   r   Ã   s     
 Nr   c                 S   s   g | ]}t |tƒr|‘qS r   ©r   r2   rP   r   r   r   r   Å   s     
 c                 S   s,   g | ]$}t |ƒp$t|ttfƒp$|d k ‘qS r   )rN   r   r1   r2   ©r   r7   r   r   r   r   É   s     c                 S   s   g | ]\}}|r|‘qS r   r   )r   r7   Úbr   r   r   r   Ê   s      )Úzip)r5   rE   ÚlabelsrG   Úrendering_kwÚresultsr!   r   r   r   Ú_unpack_args¨   s    rZ   c                    s6  | sg S g }|  dd¡}tdd„ | d|… D ƒƒrÆt| Ž \}}}}	tƒ jdd„ |D ƒŽ }
t|||||ƒ}|dkr†t|ƒ|kr†t|ƒf}|D ]6}t|t	t
tfƒ}|r¨|fn|}| ||||	f˜¡ qŠ�nlt| Ž \}}}}	|rà|gng }t|ƒt|ƒ |	dk	�rt|	ƒnd }|dk�r"| d| … n| }t|d tttfƒ�sB|g}|D ]ê‰ d	d„ ˆ D ƒ}|�sb|}d
d„ ˆ D ƒ}|�s~| ¡ }dd„ ˆ D ƒ}t|ƒdk�rž|	n|d }‡ fdd„t|ƒD ƒ‰ tƒ }
tdd„ ˆ D ƒƒ�rê|
jdd„ ˆ D ƒŽ }
t|ƒ|k�rtˆ ||d|ƒ}|�sdn|d }| ˆ |||f˜¡ �qF|S )a,  Checks the arguments and converts into tuples of the
    form (exprs, ranges, label, rendering_kw).

    Parameters
    ==========

    args
        The arguments provided to the plot functions
    nexpr
        The number of sub-expression forming an expression to be plotted.
        For example:
        nexpr=1 for plot.
        nexpr=2 for plot_parametric: a curve is represented by a tuple of two
            elements.
        nexpr=1 for plot3d.
        nexpr=3 for plot3d_parametric_line: a curve is represented by a tuple
            of three elements.
    npar
        The number of free symbols required by the plot functions. For example,
        npar=1 for plot, npar=2 for plot3d, ...
    **kwargs :
        keyword arguments passed to the plotting function. It will be used to
        verify if ``params`` has ben provided.

    Examples
    ========

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

       >>> from sympy import cos, sin, symbols
       >>> from sympy.plotting.plot import _check_arguments
       >>> x = symbols('x')
       >>> _check_arguments([cos(x), sin(x)], 2, 1)
       [(cos(x), sin(x), (x, -10, 10), None, None)]

       >>> _check_arguments([cos(x), sin(x), "test"], 2, 1)
       [(cos(x), sin(x), (x, -10, 10), 'test', None)]

       >>> _check_arguments([cos(x), sin(x), "test", {"a": 0, "b": 1}], 2, 1)
       [(cos(x), sin(x), (x, -10, 10), 'test', {'a': 0, 'b': 1})]

       >>> _check_arguments([x, x**2], 1, 1)
       [(x, (x, -10, 10), None, None), (x**2, (x, -10, 10), None, None)]
    rH   Nc                 s   s   | ]}t |tttfƒV  qd S r   )r   r   r   r
   rT   r   r   r   r     s     z#_check_arguments.<locals>.<genexpr>c                 S   s   g | ]
}|j ‘qS r   r   r   r   r   r   r   	  s     z$_check_arguments.<locals>.<listcomp>rL   r   c                 S   s   g | ]}t |tƒr|‘qS r   rR   rT   r   r   r   r   /  s     
 c                 S   s   g | ]}t |ƒr|‘qS r   rO   rT   r   r   r   r   2  s      c                 S   s   g | ]}t |tƒr|‘qS r   rS   rT   r   r   r   r   5  s     
 c                    s   g | ]}ˆ | ‘qS r   r   )r   r6   ©Úargr   r   r   :  s     c                 s   s   | ]}t |ƒ V  qd S r   r   rT   r   r   r   r   <  s     c                 S   s   g | ]
}|j ‘qS r   r   rT   r   r   r   r   =  s     r8   )Úgetr   rZ   r   r    rJ   rA   r   r   r   r   r
   rD   r   r   Úcopyr+   )r5   ZnexprrF   ÚkwargsÚoutputrH   r!   rE   rG   rX   r   ÚexprZis_exprr   Ú_rW   r&   Únew_argsÚlr>   Zrend_kwr   r[   r   Ú_check_argumentsÎ   sR    0
ÿre   )r$   )r8   N)Zsympy.core.containersr   Zsympy.core.basicr   Zsympy.core.exprr   Zsympy.core.functionr   Zsympy.core.relationalr   Zsympy.core.symbolr   Zsympy.core.sympifyr	   Zsympy.logic.boolalgr
   Zsympy.sets.fancysetsr   Zsympy.sets.setsr   Zsympy.tensor.indexedr   r#   r.   r0   rJ   rN   rZ   re   r   r   r   r   Ú<module>   s"   

F&