o
    oh,Y                     @   s   d Z ddlZddlmZmZmZ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ZG d	d
 d
Zdd ZG dd dZdS )a   rewrite of lambdify - This stuff is not stable at all.

It is for internal use in the new plotting module.
It may (will! see the Q'n'A in the source) be rewritten.

It's completely self contained. Especially it does not use lambdarepr.

It does not aim to replace the current lambdify. Most importantly it will never
ever support anything else than SymPy expressions (no Matrices, dictionaries
and so on).
    N)INumberSymboloozoo)Symbol)numbered_symbols)import_modulec                   @       e Zd ZdZdd Zdd ZdS )vectorized_lambdifya3   Return a sufficiently smart, vectorized and lambdified function.

    Returns only reals.

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

    This function uses experimental_lambdify to created a lambdified
    expression ready to be used with numpy. Many of the functions in SymPy
    are not implemented in numpy so in some cases we resort to Python cmath or
    even to evalf.

    The following translations are tried:
      only numpy complex
      - on errors raised by SymPy trying to work with ndarray:
          only Python cmath and then vectorize complex128

    When using Python cmath there is no need for evalf or float/complex
    because Python cmath calls those.

    This function never tries to mix numpy directly with evalf because numpy
    does not understand SymPy Float. If this is needed one can use the
    float_wrap_evalf/complex_wrap_evalf options of experimental_lambdify or
    better one can be explicit about the dtypes that numpy works with.
    Check numpy bug http://projects.scipy.org/numpy/ticket/1013 to know what
    types of errors to expect.
    c                 C   sf   || _ || _td| _t||dd| _| j| _t||dd| _| jj| jt	gd| _
| j| _d| _d S )NnumpyT)use_np)use_python_cmath)otypesF)argsexprr   npexperimental_lambdifylambda_func_1vector_func_1lambda_func_2	vectorizecomplexvector_func_2vector_funcfailureselfr   r    r   x/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/plotting/experimental_lambdify.py__init__m   s   

zvectorized_lambdify.__init__c                    s   | j  z& fdd|D }| j| } jj |jd | k|jdd}|W S  tyG   | jr4 d| _| j	| _t
d | j|  Y S w )Nc                 3   s    | ]
} j |td V  qdS ))dtypeN)arrayr   .0ar   r   r   	<genexpr>   s    z/vectorized_lambdify.__call__.<locals>.<genexpr>Hz>F)copyTThe evaluation of the expression is problematic. We are trying a failback method that may still work. Please report this as a bug.)r   r   mamasked_whereabsimagreal
ValueErrorr   r   warningswarn__call__)r   r   	temp_argsresultsr   r%   r   r2   ~   s&   
zvectorized_lambdify.__call__N__name__
__module____qualname____doc__r   r2   r   r   r   r   r
   Q   s    r
   c                   @   r	   )lambdifya.  Returns the lambdified function.

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

    This function uses experimental_lambdify to create a lambdified
    expression. It uses cmath to lambdify the expression. If the function
    is not implemented in Python cmath, Python cmath calls evalf on those
    functions.
    c                 C   sT   || _ || _t||ddd| _t||ddd| _t||ddd| _| j| _d| _d S )NT)r   	use_evalf)use_python_mathr;   )r;   complex_wrap_evalfF)r   r   r   r   r   lambda_func_3lambda_funcr   r   r   r   r   r      s   
zlambdify.__init__c              
   C   s   zt | |}t|jdt| krW d S |jW S  ttfy%   Y d S  tyc } z3| jr1|| j| j	krF| j
| _| |W  Y d }~S d| _| j| _tjddd | |W  Y d }~S d }~ww )Nr'   Tr)      )
stacklevel)r   r?   r,   r-   r.   ZeroDivisionErrorOverflowError	TypeErrorr   r   r   r2   r>   r0   r1   )r   r   resulter   r   r   r2      s,   zlambdify.__call__Nr5   r   r   r   r   r:      s    r:   c                  O   s   t | i |}|S N)
Lambdifier)r   kwargslr   r   r   r      s   r   c                   @   s  e Zd Z				dBddZdd Zdddd	Zd
diZg dZddddddddddddddddZddddZ	g d Z
ddd!d"Zd#d$d%Zg d&Zdd'd(Zd)d*d%Zd#d$d%Zg d+Zd,d-dd.Zd/d0 Zd1d2 Zd3d4 Zed5d6 Zd7d8 Zd9d: Zd;d< Zed=d> Zed?d@ ZdAS )CrH   Fc                    s  || _ || _|| _|| _|| _|| _|	| _|
| _tdd |D s%t	dt
|jd  fdd|D }|tt||}ddd |D } ~~|  | _|  | _t|}| | |}i }|| | || | d	d
lm} |d|i |ddd i |ddd i |r|dtdi |	r|dtdi |rz|dtdi W n ty   tdw |
r|dtddgdi |dtdi | j rt| d||f }|| _t d| | |d | _!d S )Nc                 s   s    | ]}t |tV  qd S rG   )
isinstancer   r"   r   r   r   r&      s    z&Lambdifier.__init__.<locals>.<genexpr>zThe arguments must be Symbols.)excludec                    s   g | ]}t  qS r   )next)r#   _symsr   r   
<listcomp>       z'Lambdifier.__init__.<locals>.<listcomp>z, c                 S   s   g | ]}t |qS r   )strr"   r   r   r   rQ      rR   r   )sqrtrT   Eqc                 S   s   | |kS rG   r   xyr   r   r   <lambda>       z%Lambdifier.__init__.<locals>.<lambda>Nec                 S   s   | |kS rG   r   rV   r   r   r   rY      rZ   mathcmathr   r   z-experimental_lambdify failed to import numpy.imathzsympy.plotting.intervalmathintervalmath)fromlistzlambda %s : ( %s )zMYNEWLAMBDA = %sMYNEWLAMBDA)"print_lambdar;   float_wrap_evalfr=   r   r<   r   use_intervalallr/   r   free_symbolsxreplacedictzipjoinget_dict_strdict_strget_dict_fundict_funrS   tree2str_translatestr2treeupdatesympy_atoms_namespacesympy_expression_namespace(sympy.functions.elementary.miscellaneousrT   
__import__ImportErrorprinteval_strexecr?   )r   r   r   rb   r;   rc   r=   r   r<   r   rd   newargsargstrexprstrnewexpr	namespacerT   rx   r   rO   r   r      sb   


zLambdifier.__init__c                 O   s   | j |i |S rG   )r?   )r   r   rI   r   r   r   r2     s   zLambdifier.__call__minmaxr,   )MinMaxAbsr   1j)sincostansinhcoshtanhexplogrT   floor	conjugatesignarccosarccoshanglearcsinarcsinharctanarctan2arctanhceilr-   r   amaxaminr.   )acosacoshargasinasinhatanatan2atanhceilingimlnr   r   rer   znp.piznp.infznp.e)pir   E)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   erfrT   r   	factorialgammalgamma)r   r   loggammazmath.pizmath.e)r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   rT   phase)r   r   zcmath.pizcmath.e)r   r   r   r   r   r   rT   r   r   r   r   r   r   r   r   r   r   AndOriminimax)r   r   r   c                 C   sV   t | j}| jr|| j | jr|| j | jr || j | j	r)|| j
 |S rG   )rh   builtin_not_functionsr   rq   numpy_not_functionsr<   math_not_functionsr   cmath_not_functionsrd   interval_not_functions)r   rl   r   r   r   rk     s   
zLambdifier.get_dict_strc                 C   s  t | j}| jr$| jD ]}d| ||< q| j D ]
\}}d| ||< q| jrC| jD ]}d| ||< q*| j D ]
\}}d| ||< q8| j	rb| j
D ]}d| ||< qI| j D ]
\}}d| ||< qW| jr| jD ]}d| ||< qh| j D ]
\}}d| ||< qv|S )Nznp.zmath.zcmath.zimath.)rh   builtin_functions_differentr   numpy_functions_samenumpy_functions_differentitemsr<   math_functions_samemath_functions_differentr   cmath_functions_samecmath_functions_differentrd   interval_functions_sameinterval_functions_different)r   rn   skvr   r   r   rm     s,   




zLambdifier.get_dict_func                 C   s   t d|}|du r|S | }| }|d| }||| }||d }d}t|D ]\}	}
|
dkr9|d7 }n|
dkrA|d8 }|dkrG nq,| |d|	 }| ||	d }|||f|fS )a  Converts an expression string to a tree.

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

        Functions are represented by ('func_name(', tree_of_arguments).
        Other expressions are (head_string, mid_tree, tail_str).
        Expressions that do not contain functions are directly returned.

        Examples
        ========

        >>> from sympy.abc import x, y, z
        >>> from sympy import Integral, sin
        >>> from sympy.plotting.experimental_lambdify import Lambdifier
        >>> str2tree = Lambdifier([x], x).str2tree

        >>> str2tree(str(Integral(x, (x, 1, y))))
        ('', ('Integral(', 'x, (x, 1, y)'), ')')
        >>> str2tree(str(x+y))
        'x + y'
        >>> str2tree(str(x+y*sin(z)+1))
        ('x + y*', ('sin(', 'z'), ') + 1')
        >>> str2tree('sin(y*(y + 1.1) + (sin(y)))')
        ('', ('sin(', ('y*(y + 1.1) + (', ('sin(', 'y'), '))')), ')')
        z(\w+\()Nr   (   ))r   searchstartend	enumeraterp   )r   r|   	first_parr   r   headfunctailcountic	func_tailr   r   r   rp     s(   
zLambdifier.str2treec                 C   s    t |tr|S dt| j|S )a  Converts a tree to string without translations.

        Examples
        ========

        >>> from sympy.abc import x, y, z
        >>> from sympy import sin
        >>> from sympy.plotting.experimental_lambdify import Lambdifier
        >>> str2tree = Lambdifier([x], x).str2tree
        >>> tree2str = Lambdifier([x], x).tree2str

        >>> tree2str(str2tree(str(x+y*sin(z)+1)))
        'x + y*sin(z) + 1'
         )rK   rS   rj   maptree2str)clstreer   r   r   r     s   
zLambdifier.tree2strc                    s^   t |tr
 |S t |tr#t|dkr# |d dd |d S d fdd|D S )	zConverts a tree to string with translations.

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

        Function names are translated by translate_func.
        Other strings are translated by translate_str.
        r@   r   Nr   r   r   c                    s   g | ]}  |qS r   )ro   )r#   tr   r   r   rQ     s    z1Lambdifier.tree2str_translate.<locals>.<listcomp>)rK   rS   translate_strtuplelentranslate_funcrj   )r   r   r   r   r   ro   	  s
   
	
zLambdifier.tree2str_translatec                 C   s&   | j  D ]\}}t|||}q|S )zWTranslate substrings of estr using in order the dictionaries in
        dict_tuple_str.)rl   r   r   sub)r   estrpatternreplr   r   r   r     s   zLambdifier.translate_strc           
      C   s   || j v r| j | }| |}|d | S |dv r)ddd}d|| | |S | jr.dnd}| jr8d| }n| jr?d	| }| j}| j}d
| _d
| _||| |f }	|| _|| _|	S )aL  Translate function names and the tree of arguments.

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

        If the function name is not in the dictionaries of dict_tuple_fun then the
        function is surrounded by a float((...).evalf()).

        The use of float is necessary as np.<function>(sympy.Float(..)) raises an
        error.r   )rU   r[   z==z!=z(lambda x, y: x {} y)({}z(%s(%s)).evalf(z%s(%sz	float(%s)zcomplex(%s)F)rn   ro   formatr;   rc   r=   )
r   	func_nameargtreenew_namer{   optemplaterc   r=   retr   r   r   r      s(   




zLambdifier.translate_funcc                 C   s   |du ri S t |j}ddg}|D ]}t||}|dur#| d }qi }|jD ]}t|ttfs9|t	t
tfv r:q)|| | q)|||ji |S )a[  Traverses the (func, args) tree of an expression and creates a SymPy
        namespace. All other modules are imported only as a module name. That way
        the namespace is not polluted and rests quite small. It probably causes much
        more variable lookups and so it takes more time, but there are no tests on
        that for the moment.Nz!<class \'sympy[\w.]*?.([\w]*)\'>$z<function ([\w]*) at 0x[\w]*>$r   )rS   r   r   matchgroupsr   rK   r   r   r   r   r   rq   rs   )r   r   funcname	regexlistrm	args_dictr$   r   r   r   rs   H  s$   

z%Lambdifier.sympy_expression_namespacec                 C   s0   |  ttttt}i }|D ]}||t|< q|S )zmFor no real reason this function is separated from
        sympy_expression_namespace. It can be moved to it.)atomsr   r   r   r   r   rS   )r   r   dr$   r   r   r   rr   x  s
   z Lambdifier.sympy_atoms_namespaceN)FFFFFFFF)r6   r7   r8   r   r2   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rk   rm   rp   classmethodr   ro   r   r   rs   staticmethodrr   r   r   r   r   rH      s    
B


1
(
/rH   )r9   r   sympy.core.numbersr   r   r   r   sympy.core.symbolr   sympy.utilities.iterablesr   sympy.externalr   r0   r
   r:   r   rH   r   r   r   r   <module>   s    :D2