o
    §ƒðjK)  ã                   @   sD  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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/d0d1d2d3d4d5d6d7d8œ¥ZeD ]Zeee< qnejd9ejd:ejd;ejd<ejd=iZd>d?d@œZejgZG dAdB„ dBeƒZdGdCdD„ZdEdF„ ZdS )Ha	  
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ÚsecÚcscÚcotÚsinhÚcoshÚtanhÚsechÚcschÚcothÚexpÚfloorÚ	factorialÚ	bernoulliÚeulerÚ	fibonacciÚgcdÚlcmÚ	conjugateÚCiÚChiÚEiÚLiÚSiÚShiÚerfÚerfcÚharmonicÚLambertWÚsqrtÚAbsÚabsÚlogÚlnÚasinÚarcsinÚacosÚarccosÚatanÚarctanÚasecÚarcsecÚacscÚarccscÚacotÚarccotÚasinhÚarcsinhÚacoshÚarccoshÚatanhÚarctanhÚasechÚarcsechÚacschÚarccschÚacothÚarccothÚceilingÚceilÚMaxÚmaxÚMinÚminÚdoublefactorialÚ
pochhammerÚBesselIÚBesselJÚBesselKÚBesselYÚHankelH1ÚHankelH2ÚAiryAiÚAiryBiÚAppellF1ÚFresnelCÚFresnelSÚLerchPhi)Ú
factorial2ÚRisingFactorialÚbesseliÚbesseljÚbesselkÚbesselyÚhankelh1Úhankelh2ÚairyaiÚairybiÚappellf1ÚfresnelcÚfresnelsÚ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i ddd	œ¤ŽZ	dD‡ 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dEd2d3„Zd4d5„ Zd6d7„ Zd8d9„ Z d:d;„ Z!d<d=„ Z"d>d?„ Z#d@dA„ Z$dBdC„ Z%‡  Z&S )FÚMapleCodePrinterzF
    Printer which converts a SymPy expression into a maple code.
    Ú_mapleÚmapleÚandÚorznot )rn   ro   ÚnotT)ÚinlineÚallow_unknown_functionsNc                    s>   |d u ri }t ƒ  |¡ ttƒ| _| di ¡}| j |¡ d S )NÚuser_functions)ÚsuperÚ__init__ÚdictÚknown_functionsÚgetÚupdate)ÚselfÚsettingsÚ	userfuncs©Ú	__class__© ú–/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/printing/maple.pyru   f   s   
zMapleCodePrinter.__init__c                 C   s   d| S )Nz%s;r   )rz   Ú
codestringr   r   r€   Ú_get_statementn   ó   zMapleCodePrinter._get_statementc                 C   s
   d  |¡S )Nz# {})Úformat)rz   Útextr   r   r€   Ú_get_commentq   s   
zMapleCodePrinter._get_commentc                 C   s   d  || | jd ¡¡S )Nz	{} := {};Ú	precision)r„   ÚevalfÚ	_settings)rz   ÚnameÚvaluer   r   r€   Ú_declare_number_constt   s   ÿz&MapleCodePrinter._declare_number_constc                 C   s   |S ©Nr   )rz   Úlinesr   r   r€   Ú_format_codex   ó   zMapleCodePrinter._format_codec                 C   ó   |   t|ƒ¡S r�   ©Ú_printÚlist©rz   Úexprr   r   r€   Ú_print_tuple{   ó   zMapleCodePrinter._print_tuplec                 C   r‘   r�   r’   r•   r   r   r€   Ú_print_Tuple~   r˜   zMapleCodePrinter._print_Tuplec                 C   s&   |   |j¡}|   |j¡}dj||d�S )Nz{lhs} := {rhs})ÚlhsÚrhs)r“   rš   r›   r„   )rz   r–   rš   r›   r   r   r€   Ú_print_Assignment�   s   z"MapleCodePrinter._print_Assignmentc                 K   s€   t |ƒ}t|jdƒrd|  |j|¡ S t|jdƒr!d|  |j¡ S t|jdƒr/d|  |j¡ S dj|  |j|¡|  |j|¡d�S )	Néÿÿÿÿz1/%sg      à?zsqrt(%s)g      à¿z
1/sqrt(%s)z{base}^{exp})Úbaser   )r   r   r   Úparenthesizerž   r“   r„   )rz   r–   ÚkwargsÚPRECr   r   r€   Ú
_print_Pow†   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 )	Nr�   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 | ]#\}}|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£   ©rz   r   r€   Ú
<listcomp>œ   s    þÿÿÿþz5MapleCodePrinter._print_Piecewise.<locals>.<listcomp>z, zpiecewise({_inbrace}))Ú_inbrace)ÚargsÚcondr   r¥   Ú
ValueErrorÚjoinr„   )rz   r–   Ú
_coup_listr©   r   r§   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)Úintr°   r±   r„   Ústr)rz   r–   r°   r±   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})rš   Úrel_opr›   )r   rŸ   rš   r›   rµ   Úspec_relational_opsr„   )rz   r–   r¡   Úlhs_codeÚ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   r½   )NÚinfinityr   r•   r   r   r€   Ú_print_Infinity·   r�   z MapleCodePrinter._print_Infinityc                 C   r½   )NÚtruer   r•   r   r   r€   Ú_print_BooleanTrueº   r�   z#MapleCodePrinter._print_BooleanTruec                 C   r½   )NÚfalser   r•   r   r   r€   Ú_print_BooleanFalse½   r�   z$MapleCodePrinter._print_BooleanFalsec                 C   s   |rdS dS )NrÁ   rÃ   r   r•   r   r   r€   Ú_print_boolÀ   s   zMapleCodePrinter._print_boolc                 C   r½   )NÚ	undefinedr   r•   r   r   r€   Ú
_print_NaNÃ   r�   zMapleCodePrinter._print_NaNFc                 C   sH   t j|jv rdj|rdndd�}|S dj|  | ¡ ¡|rdndd�}|S )NzMatrix([], storage = {storage})ÚsparseÚrectangular)Ústoragez#Matrix({list}, storage = {storage}))r”   rÊ   )r   ÚZeroÚshaper„   r“   Útolist)rz   r–   rÈ   Ú_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}]ÚAtomT)Ústricté   )ÚparentÚi_mapleÚj_maple)r„   rŸ   rÓ   r   r“   ÚiÚjr•   r   r   r€   Ú_print_MatrixElementÐ   s
   ýz%MapleCodePrinter._print_MatrixElementc                 C   ó   | j |dd�S )NF©rÈ   ©rÏ   r•   r   r   r€   Ú_print_MatrixBaseÖ   r˜   z"MapleCodePrinter._print_MatrixBasec                 C   rÙ   )NTrÚ   rÛ   r•   r   r   r€   Ú_print_SparseRepMatrixÙ   r˜   z'MapleCodePrinter._print_SparseRepMatrixc                 C   s4   t |jttfƒr|  t |¡¡S dj|  |j¡d�S )Nz$Matrix({var_size}, shape = identity))Úvar_size)Ú
isinstanceÚrowsr   r   r“   ÚsympyÚSparseMatrixr„   r•   r   r   r€   Ú_print_IdentityÜ   s   z MapleCodePrinter._print_Identityc                    s˜   t |ƒ‰ t|jƒ}d }t|d tjtjtjtjfƒs%|d |dd … }}|d u s-|dkr:d 	‡ ‡fdd„|D ƒ¡S dj
|d 	‡ ‡fdd„|D ƒ¡d�S )	Nr   rÒ   Ú.c                 3   ó   � | ]	}ˆ  |ˆ ¡V  qd S r�   ©rŸ   ©r¦   Ú_m©r¡   rz   r   r€   Ú	<genexpr>ë   ó   € z1MapleCodePrinter._print_MatMul.<locals>.<genexpr>z{c}*{m}c                 3   rå   r�   ræ   rç   ré   r   r€   rê   í   rë   )r£   Úm)r   r”   rª   rß   rá   Ú
MatrixBaseÚ
MatrixExprÚMatrixSliceÚMatrixSymbolr­   r„   )rz   r–   Ú
_fact_listÚ_constr   ré   r€   Ú_print_MatMulâ   s   
ÿ$zMapleCodePrinter._print_MatMulc                 C   s   dj |  |j¡|  |j¡d�S )NzMatrixPower({A}, {n}))ÚAÚn)r„   r“   rž   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   rå   r�   ræ   rç   ré   r   r€   rê   ö   rë   z:MapleCodePrinter._print_HadamardProduct.<locals>.<genexpr>)r   r”   rª   r­   )rz   r–   rñ   r   ré   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 )NrÒ   z{var}${order})ÚvarÚorderz{var})rù   zdiff({func_expr}, {sec_arg}))Ú	func_exprÚsec_arg)rª   r„   r“   )rz   r–   Ú_fÚ_varÚ_orderÚ_second_argr   r   r€   Ú_print_Derivativeø   s   ÿz"MapleCodePrinter._print_Derivativer�   )F)'Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚprintmethodÚlanguageÚ
_operatorsrv   r   Ú_default_settingsru   r‚   r†   rŒ   r�   r—   r™   rœ   r¢   r¯   r´   rº   r¼   r¾   rÀ   rÂ   rÄ   rÅ   rÇ   rÏ   rØ   rÜ   rÝ   rã   ró   rö   rø   r  Ú__classcell__r   r   r}   r€   rk   T   sP    ý

þ	

rk   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].

    )rk   Údoprint)r–   Ú	assign_tor{   r   r   r€   Ú
maple_code  s   $r  c                 K   s   t t| fi |¤Žƒ 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  )r–   r{   r   r   r€   Úprint_maple_code*  s   r  r�   )r  Ú
sympy.corer   Úsympy.core.numbersr   r   r   Úsympy.printing.codeprinterr   Úsympy.printing.precedencer   r   rá   Ú_known_func_same_namerw   Ú_funcrg   ÚExp1rh   Ú
EulerGammaÚGoldenRatior»   r¶   ÚComplexInfinityÚnot_supported_symbolrk   r  r  r   r   r   r€   Ú<module>   s”    
þýüûúùø	÷
öõôóòñðïîß$
úýÿ 
0'