B
    edC)                  @   s   d Z ddlmZ ddl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xeD ]Zeee< qW 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)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ddZdC 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,d- Zd.d/ ZdDd1d2Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Z d=d> Z!d?d@ Z"dAdB Z#  Z$S )EMapleCodePrinterzF
    Printer which converts a SymPy expression into a maple code.
    Z_mapleZmapleNautoT)orderZ	full_precZhumaninlineZallow_unknown_functionsc                s@   |d krt  }t | t t| _|di }| j| d S )NZuser_functions)dictsuper__init__known_functionsgetupdate)selfsettingsZ	userfuncs)	__class__ a/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/sympy/printing/maple.pyr.   c   s    
zMapleCodePrinter.__init__c             C   s   d| S )Nz%s;r5   )r2   Z
codestringr5   r5   r6   _get_statementk   s    zMapleCodePrinter._get_statementc             C   s
   d |S )Nz# {})format)r2   textr5   r5   r6   _get_commentn   s    zMapleCodePrinter._get_commentc             C   s   d ||| jd S )Nz	{} := {};	precision)r8   ZevalfZ	_settings)r2   namevaluer5   r5   r6   _declare_number_constq   s    z&MapleCodePrinter._declare_number_constc             C   s   |S )Nr5   )r2   linesr5   r5   r6   _format_codeu   s    zMapleCodePrinter._format_codec             C   s   |  t|S )N)_printlist)r2   exprr5   r5   r6   _print_tuplex   s    zMapleCodePrinter._print_tuplec             C   s   |  t|S )N)rA   rB   )r2   rC   r5   r5   r6   _print_Tuple{   s    zMapleCodePrinter._print_Tuplec             C   s&   |  |j}|  |j}dj||dS )Nz{lhs} := {rhs})lhsrhs)rA   rF   rG   r8   )r2   rC   rF   rG   r5   r5   r6   _print_Assignment~   s    z"MapleCodePrinter._print_Assignmentc             K   s   t |}|jdkr$d| |j| S |jdtjfkrDd| |j S |jdtj fkrfd| |j S dj| |j|| |j|dS d S )	Nz1/%sg      ?zsqrt(%s)g      z
1/sqrt(%s)z{base}^{exp})baser   )r   r   parenthesizerJ   r   ZHalfrA   r8   )r2   rC   kwargsPRECr5   r5   r6   
_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 )	NrI   TzAll 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})rP   )r   BooleanTruer8   rA   ).0rP   rO   )r2   r5   r6   
<listcomp>   s   z5MapleCodePrinter._print_Piecewise.<locals>.<listcomp>z, zpiecewise({_inbrace}))_inbrace)argsZcondr   rQ   
ValueErrorjoinr8   )r2   rC   Z
_coup_listrT   r5   )r2   r6   _print_Piecewise   s    "


z!MapleCodePrinter._print_Piecewisec             C   s,   t |jt |j }}djt|t|dS )Nz{p}/{q})pq)intrY   rZ   r8   str)r2   rC   rY   rZ   r5   r5   r6   _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})rF   rel_oprG   )r   rK   rF   rG   r^   spec_relational_opsr8   )r2   rC   rM   Zlhs_codeZrhs_codeopr5   r5   r6   _print_Relational   s    z"MapleCodePrinter._print_Relationalc             C   s   t | S )N)number_symbols)r2   rC   r5   r5   r6   _print_NumberSymbol   s    z$MapleCodePrinter._print_NumberSymbolc             C   s   dS )Nz	-infinityr5   )r2   rC   r5   r5   r6   _print_NegativeInfinity   s    z(MapleCodePrinter._print_NegativeInfinityc             C   s   dS )Ninfinityr5   )r2   rC   r5   r5   r6   _print_Infinity   s    z MapleCodePrinter._print_Infinityc             C   s   |  |jS )N)rA   label)r2   rC   r5   r5   r6   
_print_Idx   s    zMapleCodePrinter._print_Idxc             C   s   dS )Ntruer5   )r2   rC   r5   r5   r6   _print_BooleanTrue   s    z#MapleCodePrinter._print_BooleanTruec             C   s   dS )Nfalser5   )r2   rC   r5   r5   r6   _print_BooleanFalse   s    z$MapleCodePrinter._print_BooleanFalsec             C   s   |rdS dS )Nri   rk   r5   )r2   rC   r5   r5   r6   _print_bool   s    zMapleCodePrinter._print_boolc             C   s   dS )NZ	undefinedr5   )r2   rC   r5   r5   r6   
_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}))rB   rp   )r   ZZeroshaper8   rA   tolist)r2   rC   ro   Z_strMr5   r5   r6   _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)r8   rK   rv   r   rA   ij)r2   rC   r5   r5   r6   _print_MatrixElement   s    z%MapleCodePrinter._print_MatrixElementc             C   s   | j |ddS )NF)ro   )rs   )r2   rC   r5   r5   r6   _print_MatrixBase   s    z"MapleCodePrinter._print_MatrixBasec             C   s   | j |ddS )NT)ro   )rs   )r2   rC   r5   r5   r6   _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)
isinstancerowsr   r   rA   sympyZSparseMatrixr8   )r2   rC   r5   r5   r6   _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   ru   .c             3   s   | ]} | V  qd S )N)rK   )rR   _m)rM   r2   r5   r6   	<genexpr>   s    z1MapleCodePrinter._print_MatMul.<locals>.<genexpr>z{c}*{m}c             3   s   | ]} | V  qd S )N)rK   )rR   r   )rM   r2   r5   r6   r      s    )rO   m)r   rB   rU   r|   r~   Z
MatrixBaseZ
MatrixExprZMatrixSliceZMatrixSymbolrW   r8   )r2   rC   
_fact_listZ_constr5   )rM   r2   r6   _print_MatMul   s    
zMapleCodePrinter._print_MatMulc             C   s   dj | |j| |jdS )NzMatrixPower({A}, {n}))An)r8   rA   rJ   r   )r2   rC   r5   r5   r6   _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 )N)rK   )rR   r   )rM   r2   r5   r6   r      s    z:MapleCodePrinter._print_HadamardProduct.<locals>.<genexpr>)r   rB   rU   rW   )r2   rC   r   r5   )rM   r2   r6   _print_HadamardProduct   s    
z'MapleCodePrinter._print_HadamardProductc             C   sX   |j \}\}}|dkr2dj| || |d}ndj| |d}dj| ||dS )Nru   z{var}${order})varr*   z{var})r   zdiff({func_expr}, {sec_arg}))Z	func_exprZsec_arg)rU   r8   rA   )r2   rC   _fZ_var_orderZ_second_argr5   r5   r6   _print_Derivative   s    z"MapleCodePrinter._print_Derivative)N)F)%__name__
__module____qualname____doc__ZprintmethodlanguageZ_default_settingsr.   r7   r:   r>   r@   rD   rE   rH   rN   rX   r]   ra   rc   rd   rf   rh   rj   rl   rm   rn   rs   ry   rz   r{   r   r   r   r   r   __classcell__r5   r5   )r4   r6   r(   T   sJ   	

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)rC   Z	assign_tor3   r5   r5   r6   
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   )rC   r3   r5   r5   r6   print_maple_code*  s    r   )N)r   Z
sympy.corer   Zsympy.core.numbersr   r   Zsympy.printing.codeprinterr   Zsympy.printing.precedencer   r   r~   Z_known_func_same_namer/   _funcr$   ZExp1r%   Z
EulerGammaZGoldenRatiorb   r_   ZComplexInfinityZnot_supported_symbolr(   r   r   r5   r5   r5   r6   <module>	   sf   	

 0
'