
    wgC                        d Z ddl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  ed      ZerT ed	      j                  Z ed
      j                  Zej#                          ej%                          ej'                          ddgiZ G d de      Z G d de      Zg a e       ada G d d      Z G d de      Z G d d      Zd Z ed      dd       Zy)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                   X     e Zd ZdZ fdZd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Z xZS )LLVMJitPrinterzConvert expressions to LLVM IRc                     |j                  di       | _        t        st        d      t	        |   |i | t        j                         | _        || _	        || _
        || _        i | _        i | _        y )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         _/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/printing/llvmjitcode.pyr   zLLVMJitPrinter.__init__   sg    "JJ~r:GHH$)&)}}    c                 "    || j                   |<   y N)r   )r   namevalues      r   _add_tmp_varzLLVMJitPrinter._add_tmp_var)   s    "Tr   c                 T    t         j                  | j                  t        |            S r!   )r   Constantr   float)r   ns     r   _print_NumberzLLVMJitPrinter._print_Number,   s    {{4<<q22r   c                 h    t         j                  | j                  t        |j                              S r!   )r   r&   r   r'   pr   exprs     r   _print_IntegerzLLVMJitPrinter._print_Integer/   s    {{4<<tvv77r   c                     | j                   j                  |      }|s| j                  j                  |      }|st        d|z        |S )NSymbol not found: %s)r   getr   LookupError)r   svals      r   _print_SymbolzLLVMJitPrinter._print_Symbol2   sH    llq!##''*C4q899
r   c                    | j                  |j                        }|j                  t        j                  k(  r:| j
                  j                  t        j                  | j                  d      |      S |j                  t        j                  k(  r| j                  j                  d      }|s[t        j                  | j                  | j                  g      }t        j                  | j                  |d      }|| j                  d<   | j
                  j!                  ||gd      S |j                  dk(  r| j
                  j#                  ||      S | j                  |j                        }| j                  j                  d      }|sft        j                  | j                  | j                  | j                  g      }t        j                  | j                  |d      }|| j                  d<   | j
                  j!                  |||gd      S )Ng      ?sqrt   pow)_printbaseexpr   NegativeOner   fdivr   r&   r   Halfr   r1   FunctionTypeFunctionr   callfmul)r   r-   base0r   fn_typeexp0s         r   
_print_PowzLLVMJitPrinter._print_Pow;   ss   DII&88q}}$<<$$R[[s%CUKK88qvv(B//$,,G[[gv>&(F#<<$$R%&9988q=<<$$UE22{{488$[[__U#oodllT\\4<<4PQGT[['59B!#DKK||  eT]E::r   c                     |j                   D cg c]  }| j                  |       }}|d   }|dd  D ]  }| j                  j                  ||      }  |S c c}w Nr      )r   r:   r   rC   r   r-   anodesenodes         r   
_print_MulzLLVMJitPrinter._print_MulQ   _    )-3AQ33!H!"I 	+D!!!T*A	+	 4   Ac                     |j                   D cg c]  }| j                  |       }}|d   }|dd  D ]  }| j                  j                  ||      }  |S c c}w rI   )r   r:   r   faddrK   s         r   
_print_AddzLLVMJitPrinter._print_AddX   rQ   rR   c                    |j                   j                  }| j                  |j                  d         }| j                  j                  |      }|s[t        j                  | j                  | j                  g      }t        j                  | j                  ||      }|| j                  |<   | j                  j                  ||g|      S Nr   )func__name__r:   r   r   r1   r   r@   r   rA   r   r   rB   )r   r-   r"   e0r   rE   s         r   _print_FunctionzLLVMJitPrinter._print_Functiona   s    yy!![[1&[[__T"oodllT\\NCGT[['48B "DKK||  bT400r   c                 0    t        dt        |      z        )Nz,Unsupported type for LLVM JIT conversion: %s)	TypeErrortyper,   s     r   emptyPrinterzLLVMJitPrinter.emptyPrinterk   s    Ft*% & 	&r   )rY   
__module____qualname____doc__r   r$   r)   r.   r5   rG   rP   rU   r[   r_   __classcell__r   s   @r   r   r      s8    (
#38;,1&r   r   c                   *     e Zd Z fdZd Zd Z xZS )LLVMJitCallbackPrinterc                 $    t        |   |i | y r!   r   r   )r   r   r   r   s      r   r   zLLVMJitCallbackPrinter.__init__s   s    $)&)r   c                    | j                   |j                     \  }}t        |j                  d   j	                               }| j
                  j                  |t        j                  t        j                  d      |      g      }| j
                  j                  |t        j                  | j                              }| j
                  j                  |      }|S )Nr       )r   r;   intindicesevalfr   gepr   r&   IntTypebitcastPointerTyper   load)r   r-   arrayidxoffset	array_ptrfp_array_ptrr#   s           r   _print_Indexedz%LLVMJitCallbackPrinter._print_Indexedv   s    &&tyy1
sT\\!_**,-LL$$UR[[B-P,QR	||++Ir~~dll7ST!!,/r   c                    | j                   j                  |      }|r|S | j                  j                  |d dg      \  }}|st        d|z        | j                  j                  |t        j                  t        j                  d      |      g      }| j                  j                  |t        j                  | j                              }| j                  j                  |      }|S )Nr   r0   rj   )r   r1   r   r2   r   rn   r   r&   ro   rp   rq   r   rr   )r   r3   r4   rs   rt   rv   rw   r#   s           r   r5   z$LLVMJitCallbackPrinter._print_Symbol~   s    llq!J&&**1tQi8
s4q899LL$$UR[[B-M,NO	||++I,.NN4<<,HJ!!,/r   )rY   r`   ra   r   rx   r5   rc   rd   s   @r   rf   rf   r   s    *r   rf   c                   B    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)LLVMJitCodec                     || _         t        j                         | _        t        j	                  d      | _        d | _        g | _        | j                  | _        i | _	        d| _
        y )Nmod1 )	signaturer   r   r   Moduler   r   llvm_arg_typesllvm_ret_type
param_dict	link_name)r   r   s     r   r   zLLVMJitCode.__init__   sO    "}}ii' !\\r   c                 6   |t         j                  k(  rt        j                  d      S |t         j                  k(  r| j
                  S |t        j                  t         j                        k(  rt        j                  | j
                        S |t         j                  k(  r(t        j                  t        j                  d            S |t         j                  k(  r(t        j                  t        j                  d            S t        dt        |      z         y )Nrj   zUnhandled ctype = %s)ctypesc_intr   ro   c_doubler   POINTERrq   c_void_p	py_objectprintstr)r   ctypes     r   _from_ctypezLLVMJitCode._from_ctype   s    FLL ::b>!FOO#<<FNN6??33>>$,,//FOO#>>"**R.11F$$$>>"**R.11$s5z12r   c                     | j                  | j                  j                        | _        | j                  j                  D cg c]  }| j                  |       c}| _        yc c}w )z#Create types for function argumentsN)r   r   ret_typer   
arg_ctypesr   )r   	func_argsrL   s      r   _create_argszLLVMJitCode._create_args   sM    !--dnn.E.EF*...*C*CDQTa D 	Ds   A$c                 <   d}t         dz  a |t        t               z   | _        t        j	                  | j                         t
        j                  | j                  | j                        }t
        j                  | j                  || j                        | _        y)z,Create function with name and type signaturejit_funcrJ   )r"   N)current_link_suffixr   r   
link_namesaddr   r@   r   r   rA   r   r   )r   default_link_namerE   s      r   _create_function_basez!LLVMJitCode._create_function_base   sq     'q *S1D-EEt~~&//$"4"4d6I6IJ++dkk7+Hr   c                     t        |      D ]R  \  }}t        |      | j                  j                  |   _        | j                  j                  |   | j
                  |<   T y)z0Mapping of symbolic values to function argumentsN)	enumerater   r   r   r"   r   r   r   irL   s       r   _create_param_dictzLLVMJitCode._create_param_dict   sL    i( 	1DAq#&q6DGGLLO !%aDOOA	1r   c                 d   | j                   j                  d      }t        j                  |      }t	        | j
                  || j                   | j                        }| j                  ||      }|j                  j                  | j                  ||             t        | j
                        }|S )'Create function body and return LLVM IRentryr   )r   append_basic_blockr   	IRBuilderr   r   r   _convert_exprr   ret_wrap_returnr   )r   r-   bb_entryr   ljr   strmods          r   _create_functionzLLVMJitCode._create_function   s    77--g6,,x(DKK$'')-:   T*


t((S12T[[!r   c                 P   | j                   j                  t        j                  k(  r|d   S t        j                  t        j                  d            }t        j                  || j                  g      }t        j                  |j                  |d      }|D cg c]  }|j                  j                  ||g      ! }}t        |      dk(  r|d   }|S t        j                  d      g}	|	j                  |gt        |      z         t        j                  ||	      }
t        j                  |j                  |
d      }t        j                  t        j                  d      t        |            g}|j                  |       |j                  j                  ||      }|S c c}w )Nr   rj   PyFloat_FromDoublerJ   PyTuple_Pack)r   r   r   r   r   rq   ro   r@   r   rA   r   r   rB   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   zLLVMJitCode._wrap_return   sI   
 >>""foo57N >>"**R.1 OOHt||n=	++bii4HI?CD!

!5D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                 |   	 t        |      dk(  rw|d   }|d   }t        |      dk7  r2| j                  j                  t        j                  k(  rt        d      |D ](  \  }}|j                  |      }|j                  ||       * D cg c]  }|j                  |       }}|S # t        $ r |g}Y .w xY wc c}w )Nr8   r   rJ   z>Return of multiple expressions not supported for this callback)	r   r   r   r   r   NotImplementedErrorr:   r$   r]   )	r   r   r-   	tmp_exprsfinal_exprsr"   rN   r4   r   s	            r   r   zLLVMJitCode._convert_expr   s    	!4yA~ G	"1g{#q(T^^-D-D-W-.noo( /GD!))A,COOD#./ '22		!22  	!&K	! 3s   BB' B9'B65B6c                    t         j                  |      }t         j                         }d|_        t         j	                         }|j                  |       |j                  |       t         j                  j                         j                         }t         j                  ||      }|j                          t        j                  |       	 |j!                  | j"                        }|S )Nr8   )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_functionzLLVMJitCode._compile_function	  s    ##F+..0668\" KK++-CCE 	,,UNC!7# ++DNN;r   N)rY   r`   ra   r   r   r   r   r   r   r   r   r    r   r   r{   r{      s2    3E	I1!F$r   r{   c                   *     e Zd Z fdZd Zd Z xZS )LLVMJitCodeCallbackc                 $    t         |   |       y r!   rh   )r   r   r   s     r   r   zLLVMJitCodeCallback.__init__$  s    #r   c                 b   t        |      D ]  \  }}t        |t              rP| j                  j                  |   |f| j
                  |<   t        |      | j                  j                  |   _        f| j                  j                  | j                  j                     |f| j
                  |<    y r!   )
r   
isinstancer   r   r   r   r   r"   r   	input_argr   s       r   r   z&LLVMJitCodeCallback._create_param_dict'  s    i( 	)DAq![)&*ggll1oq%9"'*1vQ$&*ggll4>>3K3K&L&'&)"	)r   c                 x   | j                   j                  d      }t        j                  |      }t	        | j
                  || j                   | j                        }| j                  ||      }| j                  j                  r|j                  | j                   j                  | j                  j                     t        j                  | j                              }t        |      D ]S  \  }}t        j                  t        j!                  d      |      }	|j#                  ||	g      }
|j%                  ||
       U |j'                  t        j                  t        j!                  d      d             n+|j(                  j'                  | j+                  ||             t-        | j
                        }|S )r   r   r   rj   r   )r   r   r   r   rf   r   r   r   r   ret_argrp   r   rq   r   r   r&   ro   rn   storer   r   r   r   )r   r-   r   r   r   r   output_fp_ptrr   r4   indexoutput_array_ptrr   s               r   r   z$LLVMJitCodeCallback._create_function0  s?   77--g6,,x(#DKK$''15B   T*>>!!#OODGGLL9O9O,P,.NN4<<,HJM#C. 53BJJrNA6#*;;}ug#F c#345 KKBJJrNA67JJNN4,,R56T[[!r   )rY   r`   ra   r   r   r   rc   rd   s   @r   r   r   #  s    $)r   r   c                       e Zd Zd Zy)CodeSignaturec                 <    || _         g | _        d| _        d | _        y rW   )r   r   r   r   )r   r   s     r   r   zCodeSignature.__init__J  s"       r   N)rY   r`   ra   r   r   r   r   r   r   I  s    	r   r   c                     |t        |      }nt        |      }|j                  |        |j                          |j	                  |        |j                  |      }	 |j                  |      }|S )z5Create a native code function from a SymPy expression)r{   r   r   r   r   r   r   r   )r   r-   r   callback_typejitr   r   s          r   _llvm_jit_coder   V  sn    )$!),T4 !!$'F   (DKr   )r   scipy)modulesc                    t         st        d      t        t        j                        }g }|*| D ]#  }t        j
                  }|j                  |       % n|dv rpt        j
                  |_        t        j                  t        j                  t        j
                        g}t        j                  t        j
                  g}d|_
        n|dk(  rt        j                  t        j                  t        j
                        t        j                  t        j                  t        j                  t        j
                        g}t        j                  |_        d|_
        d|_        nt        d|z        ||_        t        | |||      }|r|dk(  r}|j                  t        j                  k(  rt        j                   }	nt        j"                  }	  |	|j                  g| |      }
|
S )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.testrJ   cubature   zUnknown callback type: %sr   )r   r   r   r   r   r   r   r   r   r   r   r   r   
ValueErrorr   r   
PYFUNCTYPE	CFUNCTYPE)r   r-   r   r   r   _	arg_ctypearg_ctypes_formalr   FUNCTYPEcfuncs              r   r	   r	   h  s   j @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$i?D*;;&
 V---$$##5HY''5*5d;ELr   r!   ) rb   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   rf   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
 J JZ#+ #L
 
$ 12B 3Br   