o
    à8VaEA  ã                   @   sü   d Z ddlZddlmZ ddlmZ ddlmZmZ ddl	m
Z
 edƒZer<edƒjZed	ƒjZe ¡  e ¡  e ¡  d
dgiZG dd„ deƒZG dd„ deƒZg aeƒ adaG dd„ dƒZG dd„ deƒZG dd„ dƒZdd„ Ze
ddddd
„ƒZdS )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                       sh   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Z‡  ZS )ÚLLVMJitPrinterzConvert expressions to LLVM IRc                    sX   |  di ¡| _tstdƒ‚tƒ j|i |¤Ž t ¡ | _|| _	|| _
|| _i | _i | _d S )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__© ú</usr/lib/python3/dist-packages/sympy/printing/llvmjitcode.pyr      s   

zLLVMJitPrinter.__init__c                 C   s   || j |< d S ©N)r   )r   ÚnameÚvaluer   r   r   Ú_add_tmp_var(   s   zLLVMJitPrinter._add_tmp_varc                 C   s   t  | jt|ƒ¡S r   )r   ÚConstantr   Úfloat)r   Únr   r   r   Ú_print_Number+   s   zLLVMJitPrinter._print_Numberc                 C   s   t  | jt|jƒ¡S r   )r   r"   r   r#   Úp©r   Úexprr   r   r   Ú_print_Integer.   s   zLLVMJitPrinter._print_Integerc                 C   s0   | j  |¡}|s| j |¡}|std| ƒ‚|S )NúSymbol not found: %s)r   Úgetr
   ÚLookupError)r   ÚsÚvalr   r   r   Ú_print_Symbol1   s   zLLVMJitPrinter._print_Symbolc                 C   s  |   |j¡}|jtjkr| j t | j	d¡|¡S |jtj
krE| j d¡}|s<t | j	| j	g¡}t | j|d¡}|| jd< | j ||gd¡S |jdkrQ| j ||¡S |   |j¡}| j d¡}|swt | j	| j	| j	g¡}t | j|d¡}|| jd< | j |||gd¡S )Ng      ð?Zsqrté   Úpow)Ú_printÚbaseZexpr   ZNegativeOner   Zfdivr   r"   r   ZHalfr   r+   ÚFunctionTypeÚFunctionr   ÚcallÚfmul)r   r(   Zbase0r   Úfn_typeZexp0r   r   r   Ú
_print_Pow:   s&   


zLLVMJitPrinter._print_Powc                    ó@   ‡ fdd„|j D ƒ}|d }|dd … D ]	}ˆ j ||¡}q|S )Nc                    ó   g | ]}ˆ   |¡‘qS r   ©r2   ©Ú.0Úa©r   r   r   Ú
<listcomp>Q   ó    z-LLVMJitPrinter._print_Mul.<locals>.<listcomp>r   é   )r   r   r7   ©r   r(   ZnodesÚeZnoder   r@   r   Ú
_print_MulP   ó
   zLLVMJitPrinter._print_Mulc                    r:   )Nc                    r;   r   r<   r=   r@   r   r   rA   X   rB   z-LLVMJitPrinter._print_Add.<locals>.<listcomp>r   rC   )r   r   ZfaddrD   r   r@   r   Ú
_print_AddW   rG   zLLVMJitPrinter._print_Addc                 C   sf   |j j}|  |jd ¡}| j |¡}|s*t | j| jg¡}t 	| j
||¡}|| j|< | j ||g|¡S ©Nr   )ÚfuncÚ__name__r2   r   r   r+   r   r4   r   r5   r   r   r6   )r   r(   r   Ze0r   r8   r   r   r   Ú_print_Function`   s   
zLLVMJitPrinter._print_Functionc                 C   s   t dt|ƒ ƒ‚)Nz,Unsupported type for LLVM JIT conversion: %s)Ú	TypeErrorÚtyper'   r   r   r   ÚemptyPrinterj   s   ÿzLLVMJitPrinter.emptyPrinter)rK   Ú
__module__Ú__qualname__Ú__doc__r   r!   r%   r)   r/   r9   rF   rH   rL   rO   Ú__classcell__r   r   r   r   r	      s    		
r	   c                       ó,   e Zd Z‡ fdd„Zdd„ Zdd„ Z‡  ZS )ÚLLVMJitCallbackPrinterc                    s   t ƒ j|i |¤Ž d S r   ©r   r   )r   r   r   r   r   r   r   r   s   zLLVMJitCallbackPrinter.__init__c                 C   sf   | j |j \}}t|jd  ¡ ƒ}| j |t t 	d¡|¡g¡}| j 
|t | j¡¡}| j |¡}|S )Nr   é    )r
   r3   ÚintÚindicesZevalfr   Úgepr   r"   ÚIntTypeÚbitcastÚPointerTyper   Úload)r   r(   ÚarrayÚidxÚoffsetÚ	array_ptrÚfp_array_ptrr    r   r   r   Ú_print_Indexedu   s   z%LLVMJitCallbackPrinter._print_Indexedc                 C   s~   | j  |¡}|r
|S | j |d dg¡\}}|std| ƒ‚| j |t t d¡|¡g¡}| j 	|t 
| j¡¡}| j |¡}|S )Nr   r*   rW   )r   r+   r
   r,   r   rZ   r   r"   r[   r\   r]   r   r^   )r   r-   r.   r_   r`   rb   rc   r    r   r   r   r/   }   s   
ÿz$LLVMJitCallbackPrinter._print_Symbol)rK   rP   rQ   r   rd   r/   rS   r   r   r   r   rU   q   s    rU   c                   @   sT   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ ZdS )ÚLLVMJitCodec                 C   s@   || _ t ¡ | _t d¡| _d | _g | _| j| _i | _	d| _
d S )NZmod1Ú )Ú	signaturer   r   r   ZModuler   r   Úllvm_arg_typesÚllvm_ret_typeÚ
param_dictÚ	link_name©r   rg   r   r   r   r   –   s   

zLLVMJitCode.__init__c                 C   sˆ   |t jkr
t d¡S |t jkr| jS |t  t j¡kr t | j¡S |t jkr-t t d¡¡S |t j	kr:t t d¡¡S t
dt|ƒ ƒ d S )NrW   zUnhandled ctype = %s)ÚctypesÚc_intr   r[   Úc_doubler   ÚPOINTERr]   Úc_void_pÚ	py_objectÚprintÚstr)r   Zctyper   r   r   Ú_from_ctype    s   




zLLVMJitCode._from_ctypec                    s,   ˆ   ˆ jj¡ˆ _‡ fdd„ˆ jjD ƒˆ _dS )z#Create types for function argumentsc                    r;   r   )ru   r=   r@   r   r   rA   ²   rB   z,LLVMJitCode._create_args.<locals>.<listcomp>N)ru   rg   Úret_typeri   Ú
arg_ctypesrh   )r   Ú	func_argsr   r@   r   Ú_create_args®   s   ÿzLLVMJitCode._create_argsc                 C   sP   d}t d7 a |tt ƒ | _t | j¡ t | j| j¡}tj	| j
|| jd| _dS )z,Create function with name and type signatureZjit_funcrC   )r   N)Úcurrent_link_suffixrt   rk   Ú
link_namesÚaddr   r4   ri   rh   r5   r   r   )r   Zdefault_link_namer8   r   r   r   Ú_create_function_base´   s   z!LLVMJitCode._create_function_basec                 C   s:   t |ƒD ]\}}t|ƒ| jj| _| jj| | j|< qdS )z0Mapping of symbolic values to function argumentsN)Ú	enumeratert   r   r   r   rj   ©r   rx   Úir?   r   r   r   Ú_create_param_dict¿   s   þzLLVMJitCode._create_param_dictc                 C   sZ   | j  d¡}t |¡}t| j|| j | jd}|  ||¡}|j 	|  
||¡¡ t| jƒ}|S )ú'Create function body and return LLVM IRÚentry©r
   )r   Úappend_basic_blockr   Ú	IRBuilderr	   r   rj   Ú_convert_exprr   ÚretÚ_wrap_returnrt   )r   r(   Úbb_entryr   Úljrˆ   Ústrmodr   r   r   Ú_create_functionÅ   s   
ÿ
zLLVMJitCode._create_functionc                    sâ   | j jtjkr|d S t t d¡¡}t || jg¡}t 	ˆ j
|d¡‰‡ ‡fdd„|D ƒ}t|ƒdkr9|d }|S t d¡g}| |gt|ƒ ¡ t ||¡}t 	ˆ j
|d¡}	t t d¡t|ƒ¡g}
|
 |¡ ˆ j |	|
¡}|S )Nr   rW   ZPyFloat_FromDoublec                    s   g | ]
}ˆ j  ˆ|g¡‘qS r   )r   r6   )r>   Úv©r‹   Zwrap_fnr   r   rA   â   s    z,LLVMJitCode._wrap_return.<locals>.<listcomp>rC   ZPyTuple_Pack)rg   rv   rm   ro   r   r]   r[   r4   r   r5   r   ÚlenÚextendr"   r   r6   )r   r‹   ÚvalsZvoid_ptrZ	wrap_typeZwrapped_valsZ	final_valZtuple_arg_typesZ
tuple_typeZtuple_fnZ
tuple_argsr   r   r   r‰   Ó   s"   õ
zLLVMJitCode._wrap_returnc           	         s–   z3t |ƒdkr2|d }|d }t |ƒdkr | jjtjkr tdƒ‚|D ]\}}ˆ  |¡}ˆ  ||¡ q"W n ty?   |g}Y nw ‡ fdd„|D ƒ}|S )Nr0   r   rC   z>Return of multiple expressions not supported for this callbackc                    r;   r   r<   )r>   rE   ©r‹   r   r   rA     rB   z-LLVMJitCode._convert_expr.<locals>.<listcomp>)	r   rg   rv   rm   ro   ÚNotImplementedErrorr2   r!   rM   )	r   r‹   r(   Z	tmp_exprsZfinal_exprsr   rE   r.   r’   r   r“   r   r‡   ö   s   
€
ÿzLLVMJitCode._convert_exprc                 C   sr   t  |¡}t  ¡ }d|_t  ¡ }| |¡ | |¡ t j ¡  	¡ }t  
||¡}| ¡  t |¡ 	 | | j¡}|S )Nr0   )ÚllvmZparse_assemblyZcreate_pass_manager_builderÚ	opt_levelZcreate_module_pass_managerZpopulateÚrunZTargetZfrom_default_tripleZcreate_target_machineZcreate_mcjit_compilerZfinalize_objectÚexe_enginesÚappendrs   Zemit_assemblyZget_function_addressrk   )r   rŒ   ZllmodZpmbZpass_managerZtarget_machineZexe_engÚfptrr   r   r   Ú_compile_function  s   


ÿ
zLLVMJitCode._compile_functionN)rK   rP   rQ   r   ru   ry   r}   r   r   r‰   r‡   r›   r   r   r   r   re   •   s    
#re   c                       rT   )ÚLLVMJitCodeCallbackc                    s   t ƒ  |¡ d S r   rV   rl   r   r   r   r   #  s   zLLVMJitCodeCallback.__init__c                 C   sd   t |ƒD ]+\}}t|tƒr"| jj| |f| j|< t|ƒ| jj| _q| jj| jj	 |f| j|< qd S r   )
r~   Ú
isinstancer   r   r   rj   rt   r   rg   Ú	input_argr   r   r   r   r   &  s   
ÿûz&LLVMJitCodeCallback._create_param_dictc                 C   sÚ   | j  d¡}t |¡}t| j|| j | jd}|  ||¡}| jj	r\| 
| j j| jj	 t | j¡¡}t|ƒD ]\}}t t d¡|¡}	| ||	g¡}
| ||
¡ q4| t t d¡d¡¡ n
|j |  ||¡¡ t| jƒ}|S )r‚   rƒ   r„   rW   r   )r   r…   r   r†   rU   r   rj   r‡   rg   Úret_argr\   r   r]   r   r~   r"   r[   rZ   Ústorerˆ   r   r‰   rt   )r   r(   rŠ   r   r‹   rˆ   Zoutput_fp_ptrr€   r.   ÚindexZoutput_array_ptrrŒ   r   r   r   r   /  s$   
ÿ
ÿ
z$LLVMJitCodeCallback._create_function)rK   rP   rQ   r   r   r   rS   r   r   r   r   rœ   "  s    	rœ   c                   @   s   e Zd Zdd„ ZdS )ÚCodeSignaturec                 C   s   || _ g | _d| _d | _d S rI   )rv   rw   rž   rŸ   )r   rv   r   r   r   r   I  s   
zCodeSignature.__init__N)rK   rP   rQ   r   r   r   r   r   r¢   H  s    r¢   c                 C   sP   |du r	t |ƒ}nt|ƒ}| | ¡ | ¡  | | ¡ | |¡}	 | |¡}|S )z5Create a native code function from a Sympy expressionN)re   rœ   ry   r}   r   r   rs   r›   )r   r(   rg   Úcallback_typeZjitrŒ   rš   r   r   r   Ú_llvm_jit_codeU  s   




r¤   )r   Zscipy)Úmodulesc           
      C   s
  t stdƒ‚ttjƒ}g }|du r| D ]
}tj}| |¡ qnF|dks'|dkr>tj|_tjt 	tj¡g}tjtjg}d|_
n'|dkr_tjt 	tj¡tjtjt 	tj¡g}tj|_d|_
d|_ntd| ƒ‚||_t| |||ƒ}|rw|dkrw|}tj|jg|¢R Ž |ƒ}	|	S )	aN  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) # doctest: +SKIP
    >>> j1(1.0, 2.0)                             # doctest: +SKIP
    (5.0, 28.0)
    z$llvmlite is required for llvmjitcodeNzscipy.integratezscipy.integrate.testrC   Zcubatureé   zUnknown callback type: %s)r   r   r¢   rm   rr   ro   r™   rv   rn   rp   rž   rq   rŸ   Ú
ValueErrorrw   r¤   Z	CFUNCTYPE)
r   r(   r£   rg   rw   Ú_Z	arg_ctypeZarg_ctypes_formalrš   Zcfuncr   r   r   r   g  s>   S
þ

ür   )rR   rm   Zsympy.externalr   Zsympy.printing.printerr   Zsympyr   r   Zsympy.utilities.decoratorr   r   Zirr   Zbindingr•   Z
initializeZinitialize_native_targetZinitialize_native_asmprinterZ__doctest_requires__r	   rU   r˜   Úsetr{   rz   re   rœ   r¢   r¤   r   r   r   r   r   Ú<module>   s4    


W &