U
     ‹Ñh“)  ã                    @   sü   d Z ddlmZ ddlmZmZmZ ddlmZ ddl	m
Z
mZ ddlZdZdd	d
dddddddddddddddddddddd d!d"d#d$d%d&d'œZeD ]Zeee< q”ejd(ejd)ejd*ejd+ejd,iZd-d.d/œZejgZG d0d1„ d1eƒZd6d2d3„Zd4d5„ ZdS )7a	  
Maple code printer

The MapleCodePrinter converts single SymPy expressions into single
Maple expressions, using the functions defined in the Maple objects where possible.


FIXME: This module is still under actively developed. Some functions may be not completed.
é    )ÚS)ÚIntegerÚIntegerConstantÚequal_valued)ÚCodePrinter)Ú
precedenceÚ
PRECEDENCEN) ÚsinÚcosÚtanÚsecZcscZcotÚsinhÚcoshÚtanhZsechZcschZcothÚexpÚfloorÚ	factorialZ	bernoulliZeulerZ	fibonacciÚgcdZlcmÚ	conjugateZCiÚChiZEiZLiZSiZShiÚerfÚerfcZharmonicZLambertWÚsqrtÚabsÚlnZarcsinZarccosZarctanZarcsecZarccscZarccotZarcsinhZarccoshZarctanhZarcsechZarccschZarccothÚceilÚmaxÚminZdoublefactorialZ
pochhammerZBesselIZBesselJZBesselKZBesselYZHankelH1ZHankelH2ZAiryAiZAiryBiZAppellF1ZFresnelCZFresnelSZLerchPhi)ZAbsÚlogÚasinÚacosÚatanZasecZacscZacotÚasinhÚacoshÚatanhZasechZacschZacothZceilingZMaxZMinZ
factorial2ZRisingFactorialZbesseliZbesseljZbesselkZbesselyZhankelh1Zhankelh2ZairyaiZairybiZappellf1ZfresnelcZfresnelsZlerchphiÚPizexp(1)ÚCatalanÚgammaz(1/2 + (1/2)*sqrt(5))ú=z<>)z==z!=c                       s,  e Zd ZdZdZdZddddœZeej	fddd	œŽZ	dF‡ 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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/d0„ Zd1d2„ ZdGd4d5„Zd6d7„ Zd8d9„ Z d:d;„ Z!d<d=„ Z"d>d?„ Z#d@dA„ Z$dBdC„ Z%dDdE„ Z&‡  Z'S )HÚMapleCodePrinterzF
    Printer which converts a SymPy expression into a maple code.
    Z_mapleZmapleÚandÚorznot )r*   r+   ÚnotT)ÚinlineZallow_unknown_functionsNc                    s>   |d kri }t ƒ  |¡ ttƒ| _| di ¡}| j |¡ d S )NZuser_functions)ÚsuperÚ__init__ÚdictÚknown_functionsÚgetÚupdate)ÚselfÚsettingsZ	userfuncs©Ú	__class__© úB/tmp/user/2202/pip-unpacked-wheel-qe3242ng/sympy/printing/maple.pyr/   f   s    
zMapleCodePrinter.__init__c                 C   s   d| S )Nz%s;r8   )r4   Z
codestringr8   r8   r9   Ú_get_statementn   s    zMapleCodePrinter._get_statementc                 C   s
   d  |¡S )Nz# {})Úformat)r4   Útextr8   r8   r9   Ú_get_commentq   s    zMapleCodePrinter._get_commentc                 C   s   d  || | jd ¡¡S )Nz	{} := {};Z	precision)r;   ZevalfZ	_settings)r4   ÚnameÚvaluer8   r8   r9   Ú_declare_number_constt   s    ÿz&MapleCodePrinter._declare_number_constc                 C   s   |S ©Nr8   )r4   Úlinesr8   r8   r9   Ú_format_codex   s    zMapleCodePrinter._format_codec                 C   s   |   t|ƒ¡S rA   ©Ú_printÚlist©r4   Úexprr8   r8   r9   Ú_print_tuple{   s    zMapleCodePrinter._print_tuplec                 C   s   |   t|ƒ¡S rA   rD   rG   r8   r8   r9   Ú_print_Tuple~   s    zMapleCodePrinter._print_Tuplec                 C   s&   |   |j¡}|   |j¡}dj||d�S )Nz{lhs} := {rhs})ÚlhsÚrhs)rE   rK   rL   r;   )r4   rH   rK   rL   r8   r8   r9   Ú_print_Assignment�   s    z"MapleCodePrinter._print_Assignmentc                 K   s„   t |ƒ}t|jdƒr&d|  |j|¡ S t|jdƒrBd|  |j¡ S t|jdƒr^d|  |j¡ S dj|  |j|¡|  |j|¡d�S d S )	Néÿÿÿÿz1/%sg      à?zsqrt(%s)g      à¿z
1/sqrt(%s)z{base}^{exp})Úbaser   )r   r   r   ÚparenthesizerO   rE   r;   )r4   rH   ÚkwargsÚPRECr8   r8   r9   Ú
_print_Pow†   s    þzMapleCodePrinter._print_Powc                    sT   |j d jdk	r*|j d jtjkr*tdƒ‚‡ fdd„|j D ƒ}d |¡}dj|d�S )	NrN   Tz¼All Piecewise expressions must contain an (expr, True) statement to be used as a default condition. Without one, the generated expression may not evaluate to anything under some condition.c                    sN   g | ]F\}}|d k	r8|t jk	r8djˆ  |¡ˆ  |¡d�ndjˆ  |¡d�‘qS )Tz{c}, {e})ÚcÚez{e})rU   )r   ÚBooleanTruer;   rE   )Ú.0rU   rT   ©r4   r8   r9   Ú
<listcomp>œ   s   þÿÿÿÿz5MapleCodePrinter._print_Piecewise.<locals>.<listcomp>z, zpiecewise({_inbrace}))Ú_inbrace)ÚargsZcondr   rV   Ú
ValueErrorÚjoinr;   )r4   rH   Z
_coup_listrZ   r8   rX   r9   Ú_print_Piecewise“   s    "
ü
z!MapleCodePrinter._print_Piecewisec                 C   s,   t |jƒt |jƒ }}djt|ƒt|ƒd�S )Nz{p}/{q})ÚpÚq)Úintr_   r`   r;   Ústr)r4   rH   r_   r`   r8   r8   r9   Ú_print_Rational¤   s    z MapleCodePrinter._print_Rationalc                 C   sJ   t |ƒ}|  |j|¡}|  |j|¡}|j}|tkr:t| }dj|||d�S )Nz{lhs} {rel_op} {rhs})rK   Úrel_oprL   )r   rP   rK   rL   rd   Úspec_relational_opsr;   )r4   rH   rR   Zlhs_codeZrhs_codeÚopr8   r8   r9   Ú_print_Relational¨   s    z"MapleCodePrinter._print_Relationalc                 C   s   t | S rA   )Únumber_symbolsrG   r8   r8   r9   Ú_print_NumberSymbol±   s    z$MapleCodePrinter._print_NumberSymbolc                 C   s   dS )Nz	-infinityr8   rG   r8   r8   r9   Ú_print_NegativeInfinity´   s    z(MapleCodePrinter._print_NegativeInfinityc                 C   s   dS )NZinfinityr8   rG   r8   r8   r9   Ú_print_Infinity·   s    z MapleCodePrinter._print_Infinityc                 C   s   |   |j¡S rA   )rE   ÚlabelrG   r8   r8   r9   Ú
_print_Idxº   s    zMapleCodePrinter._print_Idxc                 C   s   dS )NÚtruer8   rG   r8   r8   r9   Ú_print_BooleanTrue½   s    z#MapleCodePrinter._print_BooleanTruec                 C   s   dS )NÚfalser8   rG   r8   r8   r9   Ú_print_BooleanFalseÀ   s    z$MapleCodePrinter._print_BooleanFalsec                 C   s   |rdS dS )Nrn   rp   r8   rG   r8   r8   r9   Ú_print_boolÃ   s    zMapleCodePrinter._print_boolc                 C   s   dS )NZ	undefinedr8   rG   r8   r8   r9   Ú
_print_NaNÆ   s    zMapleCodePrinter._print_NaNFc                 C   sF   t j|jkr"dj|rdndd�}n dj|  | ¡ ¡|r:dndd�}|S )NzMatrix([], storage = {storage})ÚsparseZrectangular)Ústoragez#Matrix({list}, storage = {storage}))rF   ru   )r   ZZeroÚshaper;   rE   Útolist)r4   rH   rt   Z_strMr8   r8   r9   Ú_get_matrixÉ   s    
ÿ
þzMapleCodePrinter._get_matrixc                 C   s:   dj | j|jtd dd�|  |jd ¡|  |jd ¡d�S )Nz{parent}[{i_maple}, {j_maple}]ZAtomT)Ústricté   )ÚparentZi_mapleZj_maple)r;   rP   r{   r   rE   ÚiÚjrG   r8   r8   r9   Ú_print_MatrixElementÓ   s
    ýz%MapleCodePrinter._print_MatrixElementc                 C   s   | j |dd�S )NF©rt   ©rx   rG   r8   r8   r9   Ú_print_MatrixBaseÙ   s    z"MapleCodePrinter._print_MatrixBasec                 C   s   | j |dd�S )NTr   r€   rG   r8   r8   r9   Ú_print_SparseRepMatrixÜ   s    z'MapleCodePrinter._print_SparseRepMatrixc                 C   s8   t |jttfƒr |  t |¡¡S dj|  |j¡d�S d S )Nz$Matrix({var_size}, shape = identity))Zvar_size)Ú
isinstanceZrowsr   r   rE   ÚsympyZSparseMatrixr;   rG   r8   r8   r9   Ú_print_Identityß   s    z MapleCodePrinter._print_Identityc                    sœ   t |ƒ‰ t|jƒ}d }t|d tjtjtjtjfƒsJ|d |dd …  }}|d ksZ|dkrtd 	‡ ‡fdd„|D ƒ¡S dj
|d 	‡ ‡fdd„|D ƒ¡d�S d S )	Nr   rz   Ú.c                 3   s   | ]}ˆ  |ˆ ¡V  qd S rA   ©rP   ©rW   Ú_m©rR   r4   r8   r9   Ú	<genexpr>î   s     z1MapleCodePrinter._print_MatMul.<locals>.<genexpr>z{c}*{m}c                 3   s   | ]}ˆ  |ˆ ¡V  qd S rA   r‡   rˆ   rŠ   r8   r9   r‹   ð   s     )rT   Úm)r   rF   r[   rƒ   r„   Z
MatrixBaseZ
MatrixExprZMatrixSliceZMatrixSymbolr]   r;   )r4   rH   Ú
_fact_listZ_constr8   rŠ   r9   Ú_print_MatMulå   s    
 ÿzMapleCodePrinter._print_MatMulc                 C   s   dj |  |j¡|  |j¡d�S )NzMatrixPower({A}, {n}))ÚAÚn)r;   rE   rO   r   rG   r8   r8   r9   Ú_print_MatPowò   s    zMapleCodePrinter._print_MatPowc                    s,   t |ƒ‰ t|jƒ}d ‡ ‡fdd„|D ƒ¡S )NÚ*c                 3   s   | ]}ˆ  |ˆ ¡V  qd S rA   r‡   rˆ   rŠ   r8   r9   r‹   ù   s     z:MapleCodePrinter._print_HadamardProduct.<locals>.<genexpr>)r   rF   r[   r]   )r4   rH   r�   r8   rŠ   r9   Ú_print_HadamardProductö   s    
z'MapleCodePrinter._print_HadamardProductc                 C   sX   |j \}\}}|dkr2dj|  |¡|  |¡d�}ndj|  |¡d�}dj|  |¡|d�S )Nrz   z{var}${order})ÚvarÚorderz{var})r”   zdiff({func_expr}, {sec_arg}))Z	func_exprZsec_arg)r[   r;   rE   )r4   rH   Ú_fZ_varÚ_orderZ_second_argr8   r8   r9   Ú_print_Derivativeû   s    ÿz"MapleCodePrinter._print_Derivative)N)F)(Ú__name__Ú
__module__Ú__qualname__Ú__doc__ZprintmethodÚlanguageÚ
_operatorsr0   r   Z_default_settingsr/   r:   r=   r@   rC   rI   rJ   rM   rS   r^   rc   rg   ri   rj   rk   rm   ro   rq   rr   rs   rx   r~   r�   r‚   r…   rŽ   r‘   r“   r˜   Ú__classcell__r8   r8   r6   r9   r)   T   sP   ýþ	

r)   c                 K   s   t |ƒ | |¡S )aU  Converts ``expr`` to a string of Maple code.

    Parameters
    ==========

    expr : Expr
        A SymPy expression to be converted.
    assign_to : optional
        When given, the argument is used as the name of the variable to which
        the expression is assigned.  Can be a string, ``Symbol``,
        ``MatrixSymbol``, or ``Indexed`` type.  This can be helpful for
        expressions that generate multi-line statements.
    precision : integer, optional
        The precision for numbers such as pi  [default=16].
    user_functions : dict, optional
        A dictionary where keys are ``FunctionClass`` instances and values are
        their string representations.  Alternatively, the dictionary value can
        be a list of tuples i.e. [(argument_test, cfunction_string)].  See
        below for examples.
    human : bool, optional
        If True, the result is a single string that may contain some constant
        declarations for the number symbols.  If False, the same information is
        returned in a tuple of (symbols_to_declare, not_supported_functions,
        code_text).  [default=True].
    contract: bool, optional
        If True, ``Indexed`` instances are assumed to obey tensor contraction
        rules and the corresponding nested loops over indices are generated.
        Setting contract=False will not generate loops, instead the user is
        responsible to provide values for the indices in the code.
        [default=True].
    inline: bool, optional
        If True, we try to create single-statement code instead of multiple
        statements.  [default=True].

    )r)   Zdoprint)rH   Z	assign_tor5   r8   r8   r9   Ú
maple_code  s    $r    c                 K   s   t t| f|Žƒ dS )a&  Prints the Maple representation of the given expression.

    See :func:`maple_code` for the meaning of the optional arguments.

    Examples
    ========

    >>> from sympy import print_maple_code, symbols
    >>> x, y = symbols('x y')
    >>> print_maple_code(x, assign_to=y)
    y := x
    N)Úprintr    )rH   r5   r8   r8   r9   Úprint_maple_code-  s    r¢   )N)rœ   Z
sympy.corer   Zsympy.core.numbersr   r   r   Zsympy.printing.codeprinterr   Zsympy.printing.precedencer   r   r„   Z_known_func_same_namer1   Z_funcr%   ZExp1r&   Z
EulerGammaZGoldenRatiorh   re   ZComplexInfinityZnot_supported_symbolr)   r    r¢   r8   r8   r8   r9   Ú<module>   sx   

ß$
     úýÿ 3
'