
    \h,Y                         S r SSKrSSKJrJrJrJr  SSKJr  SSK	J
r
  SSKJr  SSKr " S S5      r " S	 S
5      rS r " S S5      rg)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                   $    \ rS rSrSrS rS rSrg)vectorized_lambdifyQ   a  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                 &   Xl         X l        [        S5      U l        [	        XSS9U l        U R
                  U l        [	        XSS9U l        U R                  R                  U R                  [        /S9U l
        U R                  U l        SU l        g )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   s      \/var/www/auris/envauris/lib/python3.13/site-packages/sympy/plotting/experimental_lambdify.py__init__vectorized_lambdify.__init__m   s    		(2t%!//2/!WW..y / 2  --    c                   ^ U R                   m U4S jU 5       nU R                  " U6 nTR                  R                  TR	                  UR
                  5      STR	                  U5      -  :  UR                  SS9nU$ ! [         aR    U R                  (       a  e SU l        U R                  U l        [        R                  " S5        U R                  " U6 s $ f = f)Nc              3   L   >#    U  H  nTR                  U[        S 9v   M     g7f))dtypeN)arrayr   ).0ar   s     r    	<genexpr>/vectorized_lambdify.__call__.<locals>.<genexpr>   s     BT!73Ts   !$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   s       @r    r7   vectorized_lambdify.__call__~   s    WW	(BTBI&&	2Gee((w||$tbffWo'==5 ) *G N 
	(||DL#11DMM/0 ==$''
	(s   A/A? ?ACC)	r   r   r   r   r   r   r   r   r   N__name__
__module____qualname____firstlineno____doc__r!   r7   __static_attributes__ r#   r    r   r   Q   s    6"(r#   r   c                   $    \ rS rSrSrS rS rSrg)lambdify   a  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                     Xl         X l        [        XSSS9U l        [        XSSS9U l        [        XSSS9U l        U R                  U l        SU l        g )NT)r   	use_evalf)use_python_mathrG   )rG   complex_wrap_evalfF)r   r   r   r   r   lambda_func_3lambda_funcr   r   s      r    r!   lambdify.__init__   s^    		2?2>2$4A--r#   c                     [        U R                  U5      5      n[        UR                  5      S[        U5      -  :  a  g UR                  $ ! [
        [        4 a     g [         a  nU R                  (       a  UeU R                  U R                  :X  a'  U R                  U l        U R                  U5      s S nA$ SU l        U R                  U l        [        R                  " SSS9  U R                  U5      s S nA$ S nAff = f)Nr,   Tr.      )
stacklevel)r   rK   r1   r2   r3   ZeroDivisionErrorOverflowError	TypeErrorr   r   r   r7   rJ   r5   r6   )r   r   resultes       r    r7   lambdify.__call__   s    	'T--d34F6;;$V"44;;!=1 	 	'||4#5#55#'#5#5 }}T**DL#11DMM/;<> ==&&	's6   ?A A D 	D)AD 7D==D :D D)r   r   r   rK   r   r   rJ   Nr;   rB   r#   r    rD   rD      s    	
'r#   rD   c                      [        U 0 UD6nU$ N)
Lambdifier)r   kwargsls      r    r   r      s    D#F#AHr#   c                   $   \ rS rSr    S8S jrS rSSSS.rSS	0r/ S
QrSSSSSSSSSSSSSSSS.r	SSSS.r
/ SQrSSSS .rS!S"S#.r/ S$QrSS%S&.rS'S(S#.rS!S"S#.r/ S)QrS*S+SS,.rS- rS. rS/ r\S0 5       rS1 rS2 rS3 r\S4 5       r\S5 5       rS6rg7)9rX      c                 |   X0l         X@l        XPl        X`l        Xpl        Xl        Xl        Xl        [        S U 5       5      (       d  [        S5      e[        UR                  S9nU Vs/ s H  n[        U5      PM     nnUR                  [        [        X5      5      5      nSR!                  U Vs/ s H  n[#        U5      PM     sn5      nAAAU R%                  5       U l        U R)                  5       U l        [#        U5      nU R-                  U R/                  U5      5      n0 nUR1                  U R3                  U5      5        UR1                  U R5                  U5      5        SSKJn  UR1                  SU05        UR1                  SS	 05        UR1                  S
S 05        U(       a  UR1                  S[;        S5      05        U	(       a  UR1                  S[;        S5      05        U(       a   UR1                  S[;        S5      05        U
(       a8  UR1                  S[;        SS/S905        UR1                  S[;        S5      05        U R                   (       a  [?        U5        SU< SU< S3nUU l         [C        SU-  U5        US   U l"        g s  snf s  snf ! [<         a    [=        S5      ef = f)Nc              3   B   #    U  H  n[        U[        5      v   M     g 7frW   )
isinstancer   )r(   r)   s     r    r*   &Lambdifier.__init__.<locals>.<genexpr>   s     7$Q:a(($s   zThe arguments must be Symbols.)excludez, r   )sqrtrb   Eqc                 
    X:H  $ rW   rB   xys     r    <lambda>%Lambdifier.__init__.<locals>.<lambda>       QVr#   Nec                 
    X:g  $ rW   rB   re   s     r    rh   ri      rj   r#   mathcmathr   r   z-experimental_lambdify failed to import numpy.imathzsympy.plotting.intervalmathintervalmath)fromlistzlambda z : ( z )zMYNEWLAMBDA = %sMYNEWLAMBDA)#print_lambdarG   float_wrap_evalfrI   r   rH   r   use_intervalallr4   r   free_symbolsnextxreplacedictzipjoinstrget_dict_strdict_strget_dict_fundict_funtree2str_translatestr2treeupdatesympy_atoms_namespacesympy_expression_namespace(sympy.functions.elementary.miscellaneousrb   
__import__ImportErrorprinteval_strexecrK   )r   r   r   rs   rG   rt   rI   r   rH   r   ru   syms_newargsr)   argstrexprstrnewexpr	namespacerb   r   s                        r    r!   Lambdifier.__init__   s   
 )" 0"4. 0( 7$777=>>(9(9:'+,t!4:t,}}T#d"456G4GqCFG45'4 ))+))+d)))$--*@A 	33D9:88>? 	B&$($ 345$ 345fj&89:gz'':;<E  $
7(;!<= gz-8H(J K Lfj&89: 'N+17; (*I6$]3Y -48  E!CE EEs   *J3J 8J% %J;c                 &    U R                   " U0 UD6$ rW   )rK   )r   r   rY   s      r    r7   Lambdifier.__call__  s    000r#   minmaxr1   )MinMaxAbsr   1j)sincostansinhcoshtanhexplogrb   floor	conjugatesignarccosarccoshanglearcsinarcsinharctanarctan2arctanhceilr2   r   amaxaminr3   )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   erfrb   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   rb   phase)r   r   zcmath.pizcmath.e)r   r   r   r   r   r   rb   r   r   r   r   r   r   r   r   r   r   AndOriminimax)r   r   r   c                    [        U R                  5      nU R                  (       a  UR                  U R                  5        U R
                  (       a  UR                  U R                  5        U R                  (       a  UR                  U R                  5        U R                  (       a  UR                  U R                  5        U$ rW   )rz   builtin_not_functionsr   r   numpy_not_functionsrH   math_not_functionsr   cmath_not_functionsru   interval_not_functions)r   r   s     r    r~   Lambdifier.get_dict_str  s    223;;OOD445OOD334  OOD445OOD778r#   c                    [        U R                  5      nU R                  (       aD  U R                   H
  nSU-   X'   M     U R                  R                  5        H  u  p4SU-   X'   M     U R                  (       aD  U R                   H
  nSU-   X'   M     U R                  R                  5        H  u  p4SU-   X'   M     U R                  (       aD  U R                   H
  nSU-   X'   M     U R                  R                  5        H  u  p4SU-   X'   M     U R                  (       aD  U R                   H
  nSU-   X'   M     U R                  R                  5        H  u  p4SU-   X'   M     U$ )Nznp.zmath.zcmath.zimath.)rz   builtin_functions_differentr   numpy_functions_samenumpy_functions_differentitemsrH   math_functions_samemath_functions_differentr   cmath_functions_samecmath_functions_differentru   interval_functions_sameinterval_functions_different)r   r   skvs        r    r   Lambdifier.get_dict_fun  s>   889;;..#ai /66<<>#ai ?--%k .55;;=%k >  ..&l /66<<>&l ?11&l 299??A&l Br#   c                 X   [         R                  " SU5      nUc  U$ UR                  5       nUR                  5       nUSU nXU nXS nSn[	        U5       H$  u  pU
S:X  a  US-  nOU
S:X  a  US-  nUS:X  d  M$    O   U R                  USW	 5      nU R                  XyS 5      nXVU4U4$ )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	enumerater   )r   r   	first_parr   r   headfunctailcountic	func_tails               r    r   Lambdifier.str2tree  s    8 IIj'2	NOO%E--/C6E?D%D4=DE!$8QJE#XQJEB; ( d2Ah/I==b*D+T22r#   c                 z    [        U[        5      (       a  U$ SR                  [        U R                  U5      5      $ )aT  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'
 )r_   r}   r|   maptree2str)clstrees     r    r   Lambdifier.tree2str  s0      dC  K773s||T233r#   c                 6   [        U[        5      (       a  U R                  U5      $ [        U[        5      (       a*  [	        U5      S:X  a  U R                  US   SS US   5      $ SR                  U Vs/ s H  o R                  U5      PM     sn5      $ s  snf )zConverts a tree to string with translations.

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

Function names are translated by translate_func.
Other strings are translated by translate_str.
rN   r   Nr   r   r   )r_   r}   translate_strtuplelentranslate_funcr|   r   )r   r   ts      r    r   Lambdifier.tree2str_translate	  s     dC  %%d++e$$Ta&&tAws|T!W==77E133A6EFFEs   5Bc                 z    U R                   R                  5        H  u  p#[        R                  " X#U5      nM     U$ )zOTranslate substrings of estr using in order the dictionaries in
dict_tuple_str.)r   r   r   sub)r   estrpatternrepls       r    r   Lambdifier.translate_str  s2     "]]002MGvvgT2 3r#   c                    XR                   ;   a(  U R                   U   nU R                  U5      nUS-   U-   $ US;   a(  SSS.nSR                  XQ   U R                  U5      5      $ U R                  (       a  SOSnU R                  (       a  SU-  nOU R
                  (       a  S	U-  nU R                  nU R
                  nS
U l        S
U l        XaU R                  U5      4-  n	Xpl        Xl        U	$ )a  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   )rc   rk   z==z!=z(lambda x, y: x {} y)({}z(%s(%s)).evalf(z%s(%sz	float(%s)zcomplex(%s)F)r   r   formatrG   rt   rI   )
r   	func_nameargtreenew_namer   optemplatert   rI   rets
             r    r   Lambdifier.translate_func   s     %}}Y/H,,W5Fc>F**,&D)B-44R]DD[D[\cDdee,0NN(H$$&1(((83  $44!%!8!8$)D!&+D#$*A*A'*JKKC$4!&8#Jr#   c                    Uc  0 $ [        UR                  5      nSS/nU H1  n[        R                  " XB5      nUc  M  UR	                  5       S   nM3     0 nUR
                   HU  n[        U[        [        45      (       d  U[        [        [        4;   a  M5  UR                  U R                  U5      5        MW     UR                  X!R                  05        U$ )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.z!<class \'sympy[\w.]*?.([\w]*)\'>$z<function ([\w]*) at 0x[\w]*>$r   )r}   r   r   matchgroupsr   r_   r   r   r   r   r   r   r   )r   r   funcname	regexlistrm	args_dictr)   s           r    r   %Lambdifier.sympy_expression_namespaceH  s     <I499~H& 51I
 HHQ)= xxz!}H  IYYq6<"899Q1c2,=N$$S%C%CA%FG	 
 h		23r#   c                     U R                  [        [        [        [        [
        5      n0 nU H  nX2[        U5      '   M     U$ )zeFor no real reason this function is separated from
sympy_expression_namespace. It can be moved to it.)atomsr   r   r   r   r   r}   )r   r  dr)   s       r    r    Lambdifier.sympy_atoms_namespacex  s;     

6<C<Ac!fI  r#   )rI   r   r   r   rt   rK   rs   rG   ru   r   r   rH   N)FFFFFFFF) r<   r=   r>   r?   r!   r7   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r~   r   r   classmethodr   r   r   r   r   staticmethodr   rA   rB   r#   r    rX   rX      sj   AF<AGL#@4D1 # 	T !(     !  
 $ "
8/3b 4 4(G "P - -^  r#   rX   )r@   r   sympy.core.numbersr   r   r   r   sympy.core.symbolr   sympy.utilities.iterablesr   sympy.externalr	   r5   r   rD   r   rX   rB   r#   r    <module>r#     sM   
 
 9 9 $ 6t ) 
A( A(H/' /'d
u ur#   