o
    !ô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 ƒŽ }|p8tƒ 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   úg/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/sympy/plotting/utils.pyÚ	<genexpr>   s   € z$_get_free_symbols.<locals>.<genexpr>c                 S   ó   g | ]}|  t¡‘qS r   )Úatomsr   r   r   r   r   Ú
<listcomp>   ó    z%_get_free_symbols.<locals>.<listcomp>c                 S   r   r   )r   r   r   r   r   r   r      r   c                 S   ó   g | ]}|j ‘qS r   ©Úfree_symbolsr   r   r   r   r      ó    )Ú
isinstanceÚlistÚtupleÚsetÚallÚunion)ÚexprsÚfreer   r   r   Ú_get_free_symbols   s   	r'   é
   c                    sH   |   t¡}|D ]}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   ó    z$extract_solution.<locals>.<listcomp>r   )Úfindr   Úiterr   ÚrangeÚsubs)Zset_solr*   ZimagesÚimÚsr   r+   r   Úextract_solution!   s   
r4   c                 C   sŠ   t | tƒr| S t| ƒ} t| ƒD ]3\}}t |ttfƒr&tt|ƒddiŽ| |< qt |ttfƒsBt	|ƒsB|j
jdkr<t |tƒrBt|ƒ| |< 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	   FÚVector)r   r   r    Ú	enumerater!   r   Ú_plot_sympifyÚstrÚdictr   Ú	__class__Ú__name__r   r	   )ÚargsÚiÚar   r   r   r7   6   s   
€r7   Ú Nc                 C   sT  dd„ }t | ƒ}|dur| | ¡ ¡}t|ƒ|kr*tdd |¡ d t|ƒ|¡ ƒ‚t|ƒ|kr:tdt|ƒ|f ƒ‚tƒ  dd	„ |D ƒ¡}t|ƒt|ƒkrQtd
ƒ‚t|ƒ|k r€| |¡}|tƒ krm|D ]	}	| ||	ƒ¡ qct	|t|ƒ ƒD ]
}
| |t
ƒ ƒ¡ qut|ƒ|kr¨tƒ  dd	„ |D ƒ¡}t| |¡ƒdkr¨t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   s    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   ó   g | ]}|d  ‘qS ©r   r   ©r   Úrr   r   r   r   ~   r-   z"_create_ranges.<locals>.<listcomp>z$Multiple ranges with the same symbolc                 S   rB   rC   r   rD   r   r   r   r   �   r-   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$   Úappendr0   r   )r%   ÚrangesÚnparÚlabelÚparamsZget_default_ranger   ZrfsÚsymbolsr3   r=   r   r   r   Ú_create_rangesU   sJ   ÿþÿÿ

þýÿrQ   c                 C   sR   t | tƒo(t| ƒdko(t | jd tƒ o(| jd jo(t | jd tƒ o(| jd jS )zUA range is defined as (symbol, start, end). start and end should
    be numbers.
    é   é   é   )r   r   rH   r<   r8   Ú	is_number)rE   r   r   r   Ú	_is_range›   s   

ÿþ
þý
ürV   c                  G   sx   dd„ | D ƒ}dd„ | D ƒ}|sdn|d }dd„ | D ƒ}|s!d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   ó   g | ]}t |ƒr|‘qS r   ©rV   ©r   Útr   r   r   r   Â   ó    z _unpack_args.<locals>.<listcomp>c                 S   ó   g | ]	}t |tƒr|‘qS r   ©r   r8   rY   r   r   r   r   Ã   ó    Nr   c                 S   r\   r   ©r   r9   rY   r   r   r   r   Å   r^   c                 S   s,   g | ]}t |ƒpt|ttfƒp|d u  ‘qS r   )rV   r   r8   r9   ©r   r>   r   r   r   r   É   s   , c                 S   s   g | ]\}}|r|‘qS r   r   )r   r>   Úbr   r   r   r   Ê   r[   )Úzip)r<   rL   ÚlabelsrN   Úrendering_kwÚresultsr%   r   r   r   Ú_unpack_args¨   s   rf   c                    s6  | sg S g }|  dd¡}tdd„ | d|… D ƒƒrgt| Ž \}}}}	tƒ jdd„ |D ƒŽ }
t|||||ƒ}|dkrCt|ƒ|krCt|ƒf}|D ]}t|t	t
tfƒ}|rT|fn|}| g |¢|¢|‘|	‘R ¡ qE|S t| Ž \}}}}	|rt|gng }t|ƒt|ƒ |	dur…t|	ƒnd }|dkr“| d| … n| }t|d tttfƒs¢|g}|D ]t‰ d	d„ ˆ D ƒ}|s±|}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 }| g ˆ ¢|¢|‘|‘R ¡ q¤|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)]
    rO   Nc                 s   s    � | ]}t |tttfƒV  qd S r   )r   r   r   r
   r`   r   r   r   r     s   € z#_check_arguments.<locals>.<genexpr>c                 S   r   r   r   r   r   r   r   r   	  r   z$_check_arguments.<locals>.<listcomp>rS   r   c                 S   r\   r   r]   r`   r   r   r   r   /  r^   c                 S   rW   r   rX   r`   r   r   r   r   2  r[   c                 S   r\   r   r_   r`   r   r   r   r   5  r^   c                    s   g | ]}ˆ | ‘qS r   r   )r   r=   ©Úargr   r   r   :  r-   c                 s   s   � | ]}t |ƒ V  qd S r   r   r`   r   r   r   r   <  s   € c                 S   r   r   r   r`   r   r   r   r   =  r   r?   )Úgetr#   rf   r"   r$   rQ   rH   r!   r   r   r   r
   rK   r    r   Úcopyr0   )r<   ÚnexprrM   ÚkwargsÚoutputrO   r%   rL   rN   rd   r   ÚexprZis_exprr   Ú_rc   r*   Únew_argsÚlrE   Zrend_kwr   rg   r   Ú_check_argumentsÎ   sT   0
-Úÿrr   )r(   )r?   N)Úsympy.core.containersr   Úsympy.core.basicr   Úsympy.core.exprr   Úsympy.core.functionr   Úsympy.core.relationalr   Úsympy.core.symbolr   Úsympy.core.sympifyr	   Úsympy.logic.boolalgr
   Úsympy.sets.fancysetsr   Úsympy.sets.setsr   Úsympy.tensor.indexedr   r'   r4   r7   rQ   rV   rf   rr   r   r   r   r   Ú<module>   s$    

F&