o
    8Vak(                  
   @   s4  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g dZi 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'd(d)d*d+d,d-d.d/d0ZeD ]Zeee< qfejd1ejd2ejd3ejd4ejd5iZd6d7d8ZejgZG d9d: d:eZd?d;d<Zd=d> ZdS )@a	  
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)ZsinZcosZtanZsecZcscZcotZsinhZcoshZtanhZsechZcschZcothexpZfloorZ	factorialZAbsabslogZlnZasinZarcsinZacosZarccosZatanZarctanZasecZarcsecZacscZarccscZacotZarccotZasinhZarcsinhZacoshZarccoshZatanhZarctanhZasechZarcsechZacschZarccschZacothZarccothZceilingZceilZbesseliZBesselIZbesseljZBesselJZBesselKZBesselYZHankelH1ZHankelH2ZAiryAiZAiryBi)ZbesselkZbesselyZhankelh1Zhankelh2ZairyaiZairybiPizexp(1)CatalanZ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humanZinlineZallow_unknown_functionsc                    s@   |d u rt  }t | t t| _|di }| j| d S )NZuser_functions)dictsuper__init__known_functionsgetupdate)selfsettingsZ	userfuncs	__class__ 6/usr/lib/python3/dist-packages/sympy/printing/maple.pyr   W   s   
zMapleCodePrinter.__init__c                 C   s   d| S )Nz%s;r   )r   Z
codestringr   r   r   _get_statement_      zMapleCodePrinter._get_statementc                 C   s
   d |S )Nz# {})format)r   textr   r   r   _get_commentb   s   
zMapleCodePrinter._get_commentc                 C   s   d ||| jd S )Nz	{} := {};Z	precision)r   ZevalfZ	_settings)r   namevaluer   r   r   _declare_number_conste   s   z&MapleCodePrinter._declare_number_constc                 C   s   |S Nr   )r   linesr   r   r   _format_codei      zMapleCodePrinter._format_codec                 C      |  t|S r%   _printlistr   exprr   r   r   _print_tuplel      zMapleCodePrinter._print_tuplec                 C   r)   r%   r*   r-   r   r   r   _print_Tupleo   r0   zMapleCodePrinter._print_Tuplec                 C   s&   |  |j}|  |j}dj||dS )Nz{lhs} := {rhs})lhsrhs)r+   r2   r3   r   )r   r.   r2   r3   r   r   r   _print_Assignmentr   s   z"MapleCodePrinter._print_Assignmentc                 K   s   t |}|jdkrd| |j| S |jdks |jtdd kr(d| |j S |jdks7|jtd d kr?d| |j S d	j| |j|| |j|d
S )Nz1/%sg      ?      zsqrt(%s)g      z
1/sqrt(%s)z{base}^{exp})baser   )r   r   parenthesizer8   r   r+   r   )r   r.   kwargsPRECr   r   r   
_print_Poww   s   
zMapleCodePrinter._print_Powc                    sT   |j d jdur|j d jtjkrtd fdd|j D }d|}dj|dS )	Nr5   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 | ]#\}}|d ur|t jurdj | |dndj |dqS )Tz{c}, {e})cez{e})r>   )r   BooleanTruer   r+   ).0r>   r=   r   r   r   
<listcomp>   s    z5MapleCodePrinter._print_Piecewise.<locals>.<listcomp>z, zpiecewise({_inbrace}))_inbrace)argsZcondr   r?   
ValueErrorjoinr   )r   r.   Z
_coup_listrC   r   rA   r   _print_Piecewise   s   "

z!MapleCodePrinter._print_Piecewisec                 C   s,   t |jt |j}}djt|t|dS )Nz{p}/{q})pq)intrH   rI   r   str)r   r.   rH   rI   r   r   r   _print_Rational   s   z MapleCodePrinter._print_Rationalc                 C   sJ   t |}| |j|}| |j|}|j}|tv rt| }dj|||dS )Nz{lhs} {rel_op} {rhs})r2   rel_opr3   )r   r9   r2   r3   rM   spec_relational_opsr   )r   r.   r;   Zlhs_codeZrhs_codeopr   r   r   _print_Relational   s   z"MapleCodePrinter._print_Relationalc                 C   s   t | S r%   )number_symbolsr-   r   r   r   _print_NumberSymbol   r   z$MapleCodePrinter._print_NumberSymbolc                 C      dS )Nz	-infinityr   r-   r   r   r   _print_NegativeInfinity   r(   z(MapleCodePrinter._print_NegativeInfinityc                 C   rS   )NZinfinityr   r-   r   r   r   _print_Infinity   r(   z MapleCodePrinter._print_Infinityc                 C   s   |  |jS r%   )r+   Zlabelr-   r   r   r   
_print_Idx      zMapleCodePrinter._print_Idxc                 C   rS   )Ntruer   r-   r   r   r   _print_BooleanTrue   r(   z#MapleCodePrinter._print_BooleanTruec                 C   rS   )Nfalser   r-   r   r   r   _print_BooleanFalse   r(   z$MapleCodePrinter._print_BooleanFalsec                 C   s   |rdS dS )NrX   rZ   r   r-   r   r   r   _print_bool   rW   zMapleCodePrinter._print_boolc                 C   rS   )NZ	undefinedr   r-   r   r   r   
_print_NaN   r(   zMapleCodePrinter._print_NaNFc                 C   sP   |j dks
|jdkrdj|rdndd}|S dj| | |r"dndd}|S )Nr   zMatrix([], storage = {storage})sparseZrectangular)storagez#Matrix({list}, storage = {storage}))r,   r_   )Zcolsrowsr   r+   tolist)r   r.   r^   Z_strMr   r   r   _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)strictr6   )parentZi_mapleZj_maple)r   r9   rd   r   r+   ijr-   r   r   r   _print_MatrixElement   s
   z%MapleCodePrinter._print_MatrixElementc                 C      | j |ddS )NFr^   rb   r-   r   r   r   _print_MatrixBase   r0   z"MapleCodePrinter._print_MatrixBasec                 C   rh   )NTri   rj   r-   r   r   r   _print_SparseMatrix   r0   z$MapleCodePrinter._print_SparseMatrixc                 C   s<   t |jtst |jtr| t|S dj| |jdS )Nz$Matrix({var_size}, shape = identity))Zvar_size)
isinstancer`   r   r   r+   sympyZSparseMatrixr   r-   r   r   r   _print_Identity   s   z MapleCodePrinter._print_Identityc                    s   t | t|j}d }t|d tjs6t|d tjs6t|d tjs6t|d tjs6|d |dd  }}|d u s>|dkrKd	 fdd|D S dj
|d	 fdd|D dS )	Nr   r6   .c                 3       | ]	} | V  qd S r%   r9   r@   _mr;   r   r   r   	<genexpr>       z1MapleCodePrinter._print_MatMul.<locals>.<genexpr>z{c}*{m}c                 3   rq   r%   rr   rs   ru   r   r   rv      rw   )r=   m)r   r,   rD   rm   rn   Z
MatrixBaseZ
MatrixExprZMatrixSliceZMatrixSymbolrF   r   )r   r.   
_fact_listZ_constr   ru   r   _print_MatMul   s*   



$zMapleCodePrinter._print_MatMulc                 C   s   dj | |j| |jdS )NzMatrixPower({A}, {n}))An)r   r+   r8   r   r-   r   r   r   _print_MatPow   s   zMapleCodePrinter._print_MatPowc                    s,   t | t|j}d fdd|D S )N*c                 3   rq   r%   rr   rs   ru   r   r   rv      rw   z:MapleCodePrinter._print_HadamardProduct.<locals>.<genexpr>)r   r,   rD   rF   )r   r.   ry   r   ru   r   _print_HadamardProduct   s   
z'MapleCodePrinter._print_HadamardProductc                 C   sX   |j \}\}}|dkrdj| || |d}n	dj| |d}dj| ||dS )Nr6   z{var}${order})varr   z{var})r   zdiff({func_expr}, {sec_arg}))Z	func_exprZsec_arg)rD   r   r+   )r   r.   Z_fZ_varZ_orderZ_second_argr   r   r   _print_Derivative   s   z"MapleCodePrinter._print_Derivativer%   )F)%__name__
__module____qualname____doc__ZprintmethodlanguageZ_default_settingsr   r   r!   r$   r'   r/   r1   r4   r<   rG   rL   rP   rR   rT   rU   rV   rY   r[   r\   r]   rb   rg   rk   rl   ro   rz   r}   r   r   __classcell__r   r   r   r   r   H   sN    	

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)r.   Z	assign_tor   r   r   r   
maple_code   s   $r   c                 K   s   t t| fi | dS )aN  Prints the Maple representation of the given expression.

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

    Examples
    ========

    >>> from sympy.printing.maple import print_maple_code
    >>> from sympy import symbols
    >>> x, y = symbols('x y')
    >>> print_maple_code(x, assign_to=y)
    y := x
    N)printr   )r.   r   r   r   r   print_maple_code!  s   r   r%   )r   Z
sympy.corer   Zsympy.core.numbersr   r   Zsympy.printing.codeprinterr   Zsympy.printing.precedencer   r   rn   Z_known_func_same_namer   Z_funcr   ZExp1r   Z
EulerGammaZGoldenRatiorQ   rN   ZComplexInfinityZnot_supported_symbolr   r   r   r   r   r   r   <module>   s    
	

 
3'