
    \hB                        S r SSKrSSKJr  SSKJr  SSKJr  SSKJ	r	  SSK
Jr  \" S5      r\(       aT  \" S	5      R                  r\" S
5      R                  r\R#                  5         \R%                  5         \R'                  5         SS/0r " S S\5      r " S S\5      r/ r\" 5       rSq " S S5      r " S S\5      r " S S5      rS r\" SS9SS j5       rg)z
Use llvmlite to create executable functions from SymPy expressions

This module requires llvmlite (https://github.com/numba/llvmlite).
    N)import_module)Printer)S)IndexedBase)doctest_depends_onllvmlitezllvmlite.irzllvmlite.bindingllvm_callablec                   b   ^  \ rS rSrSrU 4S jrS rS rS rS r	S r
S	 rS
 rS rS rSrU =r$ )LLVMJitPrinter   zConvert expressions to LLVM IRc                    > UR                  S0 5      U l        [        (       d  [        S5      e[        TU ]  " U0 UD6  [        R                  5       U l        Xl	        X l
        X0l        0 U l        0 U l        g )Nfunc_arg_mapz'llvmlite is required for LLVMJITPrinter)popr   r   ImportErrorsuper__init__ll
DoubleTypefp_typemodulebuilderfnext_fntmp_var)selfr   r   r   argskwargs	__class__s         R/var/www/auris/envauris/lib/python3.13/site-packages/sympy/printing/llvmjitcode.pyr   LLVMJitPrinter.__init__   sc    "JJ~r:xGHH$)&)}}    c                      X R                   U'   g N)r   )r   namevalues      r   _add_tmp_varLLVMJitPrinter._add_tmp_var)   s    "Tr!   c                 T    [         R                  U R                  [        U5      5      $ r#   )r   Constantr   float)r   ns     r   _print_NumberLLVMJitPrinter._print_Number,   s    {{4<<q22r!   c                 h    [         R                  U R                  [        UR                  5      5      $ r#   )r   r)   r   r*   pr   exprs     r   _print_IntegerLLVMJitPrinter._print_Integer/   s    {{4<<tvv77r!   c                     U R                   R                  U5      nU(       d  U R                  R                  U5      nU(       d  [        SU-  5      eU$ )NSymbol not found: %s)r   getr   LookupError)r   svals      r   _print_SymbolLLVMJitPrinter._print_Symbol2   sH    llq!##''*C4q899
r!   c                 ,   U R                  UR                  5      nUR                  [        R                  :X  a:  U R
                  R                  [        R                  U R                  S5      U5      $ UR                  [        R                  :X  a  U R                  R                  S5      nU(       dZ  [        R                  U R                  U R                  /5      n[        R                  U R                  US5      nX0R                  S'   U R
                  R!                  X2/S5      $ UR                  S:X  a  U R
                  R#                  X"5      $ U R                  UR                  5      nU R                  R                  S5      nU(       de  [        R                  U R                  U R                  U R                  /5      n[        R                  U R                  US5      nX0R                  S'   U R
                  R!                  X2U/S5      $ )Ng      ?sqrt   pow)_printbaseexpr   NegativeOner   fdivr   r)   r   Halfr   r6   FunctionTypeFunctionr   callfmul)r   r1   base0r   fn_typeexp0s         r   
_print_PowLLVMJitPrinter._print_Pow;   si   DII&88q}}$<<$$R[[s%CUKK88qvv(B//$,,G[[gv>&(F#<<$$R&9988q=<<$$U22{{488$[[__U#oodllT\\4<<4PQGT[['59B!#KK||  T]E::r!   c                     UR                    Vs/ s H  o R                  U5      PM     nnUS   nUSS   H  nU R                  R                  XE5      nM      U$ s  snf Nr      )r   r@   r   rI   r   r1   anodesenodes         r   
_print_MulLLVMJitPrinter._print_MulQ   W    )-3AQ3!H!"ID!!!*A 	 4   Ac                     UR                    Vs/ s H  o R                  U5      PM     nnUS   nUSS   H  nU R                  R                  XE5      nM      U$ s  snf rP   )r   r@   r   faddrR   s         r   
_print_AddLLVMJitPrinter._print_AddX   rY   rZ   c                    UR                   R                  nU R                  UR                  S   5      nU R                  R                  U5      nU(       dY  [        R                  U R                  U R                  /5      n[        R                  U R                  XR5      nX@R                  U'   U R                  R                  XC/U5      $ Nr   )func__name__r@   r   r   r6   r   rF   r   rG   r   r   rH   )r   r1   r$   e0r   rK   s         r   _print_FunctionLLVMJitPrinter._print_Functiona   s    yy!![[1&[[__T"oodllT\\NCGT[['8B "KK||  T400r!   c                 0    [        S[        U5      -  5      e)Nz,Unsupported type for LLVM JIT conversion: %s)	TypeErrortyper0   s     r   emptyPrinterLLVMJitPrinter.emptyPrinterk   s    Ft*% & 	&r!   )r   r   r   r   r   r   r   )rb   
__module____qualname____firstlineno____doc__r   r&   r,   r2   r:   rM   rW   r]   rd   ri   __static_attributes____classcell__r   s   @r   r   r      s=    (
#38;,1& &r!   r   c                   4   ^  \ rS rSrU 4S jrS rS rSrU =r$ )LLVMJitCallbackPrinterr   c                 &   > [         TU ]  " U0 UD6  g r#   r   r   )r   r   r   r   s      r   r   LLVMJitCallbackPrinter.__init__s   s    $)&)r!   c                    U R                   UR                     u  p#[        UR                  S   R	                  5       5      nU R
                  R                  U[        R                  [        R                  S5      U5      /5      nU R
                  R                  U[        R                  U R                  5      5      nU R
                  R                  U5      nU$ )Nr       )r   rA   intindicesevalfr   gepr   r)   IntTypebitcastPointerTyper   load)r   r1   arrayidxoffset	array_ptrfp_array_ptrr%   s           r   _print_Indexed%LLVMJitCallbackPrinter._print_Indexedv   s    &&tyy1
T\\!_**,-LL$$UR[[B-P,QR	||++Ir~~dll7ST!!,/r!   c                    U R                   R                  U5      nU(       a  U$ U R                  R                  US S/5      u  p4U(       d  [        SU-  5      eU R                  R                  U[        R                  [        R                  S5      U5      /5      nU R                  R                  U[        R                  U R                  5      5      nU R                  R                  U5      nU$ )Nr   r5   ry   )r   r6   r   r7   r   r}   r   r)   r~   r   r   r   r   )r   r8   r9   r   r   r   r   r%   s           r   r:   $LLVMJitCallbackPrinter._print_Symbol~   s    llq!J&&**1tQi8
4q899LL$$UR[[B-M,NO	||++I,.NN4<<,HJ!!,/r!    )	rb   rk   rl   rm   r   r   r:   ro   rp   rq   s   @r   rs   rs   r   s    * r!   rs   c                   J    \ rS rSrS rS rS rS rS rS r	S r
S	 rS
 rSrg)LLVMJitCode   c                     Xl         [        R                  5       U l        [        R	                  S5      U l        S U l        / U l        U R                  U l        0 U l	        SU l
        g )Nmod1 )	signaturer   r   r   Moduler   r   llvm_arg_typesllvm_ret_type
param_dict	link_name)r   r   s     r   r   LLVMJitCode.__init__   sM    "}}ii' !\\r!   c                 B   U[         R                  :X  a  [        R                  S5      $ U[         R                  :X  a  U R
                  $ U[         R                  " [         R                  5      :X  a  [        R                  U R
                  5      $ U[         R                  :X  a(  [        R                  [        R                  S5      5      $ U[         R                  :X  a(  [        R                  [        R                  S5      5      $ [        S[        U5      -  5        g )Nry   zUnhandled ctype = %s)ctypesc_intr   r~   c_doubler   POINTERr   c_void_p	py_objectprintstr)r   ctypes     r   _from_ctypeLLVMJitCode._from_ctype   s    FLL ::b>!FOO#<<FNN6??33>>$,,//FOO#>>"**R.11F$$$>>"**R.11$s5z12r!   c                     U R                  U R                  R                  5      U l        U R                  R                   Vs/ s H  o R                  U5      PM     snU l        gs  snf )z#Create types for function argumentsN)r   r   ret_typer   
arg_ctypesr   )r   	func_argsrS   s      r   _create_argsLLVMJitCode._create_args   sQ    !--dnn.E.EF*...*C*CD*CQa *CD 	Ds   A&c                 4   Sn[         S-  q U[        [         5      -   U l        [        R	                  U R                  5        [
        R                  U R                  U R                  5      n[
        R                  U R                  X R                  S9U l        g)z,Create function with name and type signaturejit_funcrQ   )r$   N)current_link_suffixr   r   
link_namesaddr   rF   r   r   rG   r   r   )r   default_link_namerK   s      r   _create_function_base!LLVMJitCode._create_function_base   so     'q *S1D-EEt~~&//$"4"4d6I6IJ++dkk7+Hr!   c                     [        U5       HR  u  p#[        U5      U R                  R                  U   l        U R                  R                  U   U R
                  U'   MT     g)z0Mapping of symbolic values to function argumentsN)	enumerater   r   r   r$   r   r   r   irS   s       r   _create_param_dictLLVMJitCode._create_param_dict   sE    i(DA#&q6DGGLLO !%aDOOA )r!   c                 X   U R                   R                  S5      n[        R                  U5      n[	        U R
                  X0R                   U R                  S9nU R                  XA5      nUR                  R                  U R                  XE5      5        [        U R
                  5      nU$ )'Create function body and return LLVM IRentryr   )r   append_basic_blockr   	IRBuilderr   r   r   _convert_exprr   ret_wrap_returnr   )r   r1   bb_entryr   ljr   strmods          r   _create_functionLLVMJitCode._create_function   s    77--g6,,x(DKK'')-:   *


t((12T[[!r!   c                 P   U R                   R                  [        R                  :X  a  US   $ [        R                  [        R                  S5      5      n[        R                  X0R                  /5      n[        R                  UR                  US5      nU Vs/ s H  oaR                  R                  XV/5      PM      nn[        U5      S:X  a  US   nU$ [        R                  S5      /n	U	R                  U/[        U5      -  5        [        R                  X95      n
[        R                  UR                  U
S5      n[        R                  [        R                  S5      [        U5      5      /nUR                  U5        UR                  R                  X5      nU$ s  snf )Nr   ry   PyFloat_FromDoublerQ   PyTuple_Pack)r   r   r   r   r   r   r~   rF   r   rG   r   r   rH   lenextendr)   )r   r   valsvoid_ptr	wrap_typewrap_fnvwrapped_vals	final_valtuple_arg_types
tuple_typetuple_fn
tuple_argss                r   r   LLVMJitCode._wrap_return   s@   
 >>""foo57N >>"**R.1 OOH||n=	++bii4HI?CDt!

5tDt9>$QI    "zz"~.O""H:c$i#78CJ{{299j.IH++bjjnc,6GHIJl+

=I% Es   %F#c                     [        U5      S:X  ay  US   nUS   n[        U5      S:w  a3  U R                  R                  [        R                  :X  a  [        S5      eU H'  u  pVUR                  U5      nUR                  XW5        M)     W Vs/ s H  oaR                  U5      PM     nnU$ ! [         a    U/n N1f = fs  snf )Nr>   r   rQ   z>Return of multiple expressions not supported for this callback)	r   r   r   r   r   NotImplementedErrorr@   r&   rg   )	r   r   r1   	tmp_exprsfinal_exprsr$   rU   r9   r   s	            r   r   LLVMJitCode._convert_expr   s    	!4yA~ G	"1g{#q(T^^-D-D-W-.noo(GD))A,COOD.  ) '22k		!k2  	!&K	! 3s   BB, B?,B<;B<c                    [         R                  U5      n[         R                  5       nSUl        [         R	                  5       nUR                  U5        UR                  U5        [         R                  R                  5       R                  5       n[         R                  X%5      nUR                  5         [        R                  U5         UR!                  U R"                  5      nU$ )Nr>   )llvmparse_assemblycreate_pass_manager_builder	opt_levelcreate_module_pass_managerpopulaterunTargetfrom_default_triplecreate_target_machinecreate_mcjit_compilerfinalize_objectexe_enginesappendr   emit_assemblyget_function_addressr   )r   r   llmodpmbpass_managertarget_machineexe_engfptrs           r   _compile_functionLLVMJitCode._compile_function	  s    ##F+..0668\" KK++-CCE 	,,UC!7# ++DNN;r!   )r   r   r   r   r   r   r   r   N)rb   rk   rl   rm   r   r   r   r   r   r   r   r   r   ro   r   r!   r   r   r      s2    3E	I1!F$r!   r   c                   4   ^  \ rS rSrU 4S jrS rS rSrU =r$ )LLVMJitCodeCallbacki"  c                 $   > [         TU ]  U5        g r#   rv   )r   r   r   s     r   r   LLVMJitCodeCallback.__init__#  s    #r!   c                 p   [        U5       H  u  p#[        U[        5      (       aQ  U R                  R                  U   U4U R
                  U'   [        U5      U R                  R                  U   l        Mk  U R                  R                  U R                  R                     U4U R
                  U'   M     g r#   )
r   
isinstancer   r   r   r   r   r$   r   	input_argr   s       r   r   &LLVMJitCodeCallback._create_param_dict&  s    i(DA![))&*ggll1oq%9"'*1vQ$&*ggll4>>3K3K&L&'&)" )r!   c                 t   U R                   R                  S5      n[        R                  U5      n[	        U R
                  X0R                   U R                  S9nU R                  XA5      nU R                  R                  (       a  UR                  U R                   R                  U R                  R                     [        R                  U R                  5      5      n[        U5       HQ  u  px[        R                  [        R!                  S5      U5      n	UR#                  Xi/5      n
UR%                  X5        MS     UR'                  [        R                  [        R!                  S5      S5      5        O*UR(                  R'                  U R+                  XE5      5        [-        U R
                  5      nU$ )r   r   r   ry   r   )r   r   r   r   rs   r   r   r   r   ret_argr   r   r   r   r   r)   r~   r}   storer   r   r   r   )r   r1   r   r   r   r   output_fp_ptrr   r9   indexoutput_array_ptrr   s               r   r   $LLVMJitCodeCallback._create_function/  s-   77--g6,,x(#DKK''15B   *>>!!#OODGGLL9O9O,P,.NN4<<,HJM#C.BJJrNA6#*;;}g#F c4 ) KKBJJrNA67JJNN4,,R56T[[!r!   r   )	rb   rk   rl   rm   r   r   r   ro   rp   rq   s   @r   r   r   "  s    $) r!   r   c                       \ rS rSrS rSrg)CodeSignatureiH  c                 :    Xl         / U l        SU l        S U l        g r`   )r   r   r   r   )r   r   s     r   r   CodeSignature.__init__I  s        r!   )r   r   r   r   N)rb   rk   rl   rm   r   ro   r   r!   r   r  r  H  s    	r!   r  c                     Uc  [        U5      nO[        U5      nUR                  U 5        UR                  5         UR	                  U 5        UR                  U5      n UR                  U5      nU$ )z5Create a native code function from a SymPy expression)r   r   r   r   r   r   r   r   )r   r1   r   callback_typejitr   r   s          r   _llvm_jit_coder  U  sn    )$!),T4 !!$'F   (DKr!   )r   scipy)modulesc                     [         (       d  [        S5      e[        [        R                  5      n/ nUc,  U  H$  n[        R
                  nUR                  U5        M&     GO$US;   aq  [        R
                  Ul        [        R                  [        R                  " [        R
                  5      /n[        R                  [        R
                  /nSUl
        OUS:X  a  [        R                  [        R                  " [        R
                  5      [        R                  [        R                  [        R                  " [        R
                  5      /n[        R                  Ul        SUl
        SUl        O[        SU-  5      eXCl        [        XX25      nU(       a  US:X  a  WnUR                  [        R                  :X  a  [        R                   n	O[        R"                  n	U	" UR                  /UQ76 " U5      n
U
$ )a
  Compile function from a SymPy expression

Expressions are evaluated using double precision arithmetic.
Some single argument math functions (exp, sin, cos, etc.) are supported
in expressions.

Parameters
==========

args : List of Symbol
    Arguments to the generated function.  Usually the free symbols in
    the expression.  Currently each one is assumed to convert to
    a double precision scalar.
expr : Expr, or (Replacements, Expr) as returned from 'cse'
    Expression to compile.
callback_type : string
    Create function with signature appropriate to use as a callback.
    Currently supported:
       'scipy.integrate'
       'scipy.integrate.test'
       'cubature'

Returns
=======

Compiled function that can evaluate the expression.

Examples
========

>>> import sympy.printing.llvmjitcode as jit
>>> from sympy.abc import a
>>> e = a*a + a + 1
>>> e1 = jit.llvm_callable([a], e)
>>> e.subs(a, 1.1)   # Evaluate via substitution
3.31000000000000
>>> e1(1.1)  # Evaluate using JIT-compiled code
3.3100000000000005


Callbacks for integration functions can be JIT compiled.

>>> import sympy.printing.llvmjitcode as jit
>>> from sympy.abc import a
>>> from sympy import integrate
>>> from scipy.integrate import quad
>>> e = a*a
>>> e1 = jit.llvm_callable([a], e, callback_type='scipy.integrate')
>>> integrate(e, (a, 0.0, 2.0))
2.66666666666667
>>> quad(e1, 0.0, 2.0)[0]
2.66666666666667

The 'cubature' callback is for the Python wrapper around the
cubature package ( https://github.com/saullocastro/cubature )
and ( http://ab-initio.mit.edu/wiki/index.php/Cubature )

There are two signatures for the SciPy integration callbacks.
The first ('scipy.integrate') is the function to be passed to the
integration routine, and will pass the signature checks.
The second ('scipy.integrate.test') is only useful for directly calling
the function using ctypes variables. It will not pass the signature checks
for scipy.integrate.

The return value from the cse module can also be compiled.  This
can improve the performance of the compiled function.  If multiple
expressions are given to cse, the compiled function returns a tuple.
The 'cubature' callback handles multiple expressions (set `fdim`
to match in the integration call.)

>>> import sympy.printing.llvmjitcode as jit
>>> from sympy import cse
>>> from sympy.abc import x,y
>>> e1 = x*x + y*y
>>> e2 = 4*(x*x + y*y) + 8.0
>>> after_cse = cse([e1,e2])
>>> after_cse
([(x0, x**2), (x1, y**2)], [x0 + x1, 4*x0 + 4*x1 + 8.0])
>>> j1 = jit.llvm_callable([x,y], after_cse)
>>> j1(1.0, 2.0)
(5.0, 28.0)
z$llvmlite is required for llvmjitcode)scipy.integratezscipy.integrate.testrQ   cubature   zUnknown callback type: %sr  )r   r   r  r   r   r   r   r   r   r   r   r   r   
ValueErrorr   r  
PYFUNCTYPE	CFUNCTYPE)r   r1   r
  r   r   _	arg_ctypearg_ctypes_formalr   FUNCTYPEcfuncs              r   r	   r	   g  s   j 8@AAf../IJAIi(  
E	E#__	llFNN6??$CD
#\\6??;		*	$llnnV__5oollnnV__5	
 $\\			4}DEE%$i?D*;;&
 V---$$##Y''5*5d;ELr!   r#   ) rn   r   sympy.externalr   sympy.printing.printerr   sympy.core.singletonr   sympy.tensor.indexedr   sympy.utilities.decoratorr   r   irr   bindingr   
initializeinitialize_native_targetinitialize_native_asmprinter__doctest_requires__r   rs   r   setr   r   r   r   r  r  r	   r   r!   r   <module>r'     s     ( * " , 8$	}	%	(	(B+,44DOO!!#%%' )J<8 R&W R&n^ :  U
 I IX#+ #L
 
$ 12B 3Br!   