a
    RG5dÄc  ã                   @   sÄ   d Z ddlmZmZ ddlmZmZmZm	Z	 ddl
mZ ddlmZ ddlmZmZ ddlmZ g 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!œZG d"d#„ d#eƒZd)d%d&„Zd'd(„ Zd$S )*ai  
Octave (and Matlab) code printer

The `OctaveCodePrinter` converts SymPy expressions into Octave expressions.
It uses a subset of the Octave language for Matlab compatibility.

A complete code generator, which uses `octave_code` extensively, can be found
in `sympy.utilities.codegen`.  The `codegen` module can be used to generate
complete source code files.

é    )ÚAnyÚDict)ÚMulÚPowÚSÚRational)Ú_keep_coeff)ÚCodePrinter)Ú
precedenceÚ
PRECEDENCE©Úsearch)1ÚsinÚcosÚtanÚcotÚsecÚcscÚasinÚacosÚacotÚatanÚatan2ÚasecÚacscÚsinhÚcoshÚtanhÚcothÚcschÚsechÚasinhÚacoshÚatanhÚacothÚasechÚacschÚerfcÚerfiÚerfÚerfinvÚerfcinvÚbesseliÚbesseljÚbesselkÚbesselyÚ	bernoulliÚbetaÚeulerÚexpÚ	factorialÚfloorÚfresnelcÚfresnelsÚgammaÚharmonicÚlogÚpolylogÚsignÚzetaÚlegendreÚabsÚangleZbincoeffÚceilZ
chebyshevUZ
chebyshevTZcoshintZcosintÚconjZdiracÚ	heavisideÚimagZ	laguerreLÚlambertwZlogintZgammalnÚmaxÚminÚmodÚpsiÚrealZ
pochhammerZsinhintZsinint)ÚAbsÚargÚbinomialÚceilingÚ
chebyshevuÚ
chebyshevtÚChiÚCiÚ	conjugateÚ
DiracDeltaÚ	HeavisideÚimÚlaguerreÚLambertWÚliÚloggammaÚMaxÚMinÚModÚ	polygammaÚreÚRisingFactorialÚShiÚSic                	       s  e Zd ZdZdZdZddddœZdd	d
i dddddœZi f‡ 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d0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Zd8d9„ ZeZeZeZ d:d;„ Z!d<d=„ Z"d>d?„ Z#d@dA„ Z$dBdC„ Z%dDdE„ Z&dFdG„ Z'dHdI„ Z(dJdK„ Z)dLdM„ Z*dNdO„ Z+dPdQ„ Z,dRdS„ Z-dTdU„ Z.dVdW„ Z/dXdY„ Z0dZd[„ Z1d\d]„ Z2d^d_„ Z3d`da„ Z4dbdc„ Z5ddde„ Z6dfdg„ Z7dhdi„ Z8djdk„ Z9dldm„ Z:e: Z;Z<dndo„ Z=e= Z>Z?dpdq„ Z@drds„ ZAdtdu„ ZB‡  ZCS )vÚOctaveCodePrinterzL
    A printer to convert expressions to strings of Octave/Matlab code.
    Z_octaveÚOctaveú&ú|ú~)ÚandÚorÚnotNÚautoé   TF)ÚorderÚ	full_precÚ	precisionÚuser_functionsÚhumanÚallow_unknown_functionsÚcontractÚinlinec                    sH   t ƒ  |¡ ttttƒƒ| _| j ttƒ¡ | di ¡}| j |¡ d S )Nrp   )	ÚsuperÚ__init__ÚdictÚzipÚknown_fcns_src1Úknown_functionsÚupdateÚknown_fcns_src2Úget)ÚselfÚsettingsÚ	userfuncs©Ú	__class__© úQ/var/www/html/django/DPS/env/lib/python3.9/site-packages/sympy/printing/octave.pyrv   [   s
    zOctaveCodePrinter.__init__c                 C   s   |d S )Né   rƒ   )r~   Úprƒ   rƒ   r„   Ú_rate_index_positionc   s    z&OctaveCodePrinter._rate_index_positionc                 C   s   d| S )Nz%s;rƒ   )r~   Ú
codestringrƒ   rƒ   r„   Ú_get_statementg   s    z OctaveCodePrinter._get_statementc                 C   s
   d  |¡S )Nz% {}©Úformat)r~   Útextrƒ   rƒ   r„   Ú_get_commentk   s    zOctaveCodePrinter._get_commentc                 C   s   d  ||¡S )Nz{} = {};rŠ   )r~   ÚnameÚvaluerƒ   rƒ   r„   Ú_declare_number_consto   s    z'OctaveCodePrinter._declare_number_constc                 C   s
   |   |¡S ©N)Úindent_code)r~   Úlinesrƒ   rƒ   r„   Ú_format_codes   s    zOctaveCodePrinter._format_codec                    s    |j \‰ }‡ fdd„t|ƒD ƒS )Nc                 3   s$   | ]}t ˆ ƒD ]}||fV  qqd S r‘   )Úrange)Ú.0ÚjÚi©Úrowsrƒ   r„   Ú	<genexpr>z   ó    z=OctaveCodePrinter._traverse_matrix_indices.<locals>.<genexpr>)Úshaper•   )r~   ÚmatÚcolsrƒ   r™   r„   Ú_traverse_matrix_indicesw   s    
z*OctaveCodePrinter._traverse_matrix_indicesc                 C   s^   g }g }|D ]H}t | j|j|jd |jd gƒ\}}}| d|||f ¡ | d¡ q||fS )Né   zfor %s = %s:%sÚend)ÚmapÚ_printÚlabelÚlowerÚupperÚappend)r~   ÚindicesÚ
open_linesÚclose_linesr˜   ÚvarÚstartÚstoprƒ   rƒ   r„   Ú_get_loop_opening_ending}   s    ÿ
z*OctaveCodePrinter._get_loop_opening_endingc                    s‚  |j r.|jr.tj| jr.dˆ tj | ¡ S t|ƒ‰ | ¡ \}}|dk r\t| |ƒ}d}nd}g }g }g }ˆj	dvr€| 
¡ }n
t |¡}|D ]ì}	|	jr"|	jr"|	jjr"|	jjr"|	jdkrÜ| t|	j|	j dd¡ nDt|	jd jƒd	kr
t|	jtƒr
| |	¡ | t|	j|	j ƒ¡ qŽ|	jrp|	tjurp|	jd	krR| t|	jƒ¡ |	jd	krz| t|	jƒ¡ qŽ| |	¡ qŽ|pˆtjg}‡ ‡fd
d„|D ƒ}
‡ ‡fdd„|D ƒ}|D ]2}	|	j|v r¶d|| |	j¡  || |	j¡< q¶dd„ }|s||||
ƒ S t|ƒd	krB|d j r$dnd}||||
ƒ | |d  S tdd„ |D ƒƒrZdnd}||||
ƒ | d|||ƒ  S d S )Nz%sir   ú-Ú )ÚoldÚnoneéÿÿÿÿF)Úevaluater¡   c                    s   g | ]}ˆ  |ˆ ¡‘qS rƒ   ©Úparenthesize©r–   Úx©Úprecr~   rƒ   r„   Ú
<listcomp>¸   rœ   z0OctaveCodePrinter._print_Mul.<locals>.<listcomp>c                    s   g | ]}ˆ  |ˆ ¡‘qS rƒ   r¶   r¸   rº   rƒ   r„   r¼   ¹   rœ   z(%s)c                 S   sF   |d }t dt| ƒƒD ]*}| |d  jr,dnd}|| ||  }q|S )Nr   r¡   Ú*ú.*)r•   ÚlenÚ	is_number)ÚaÚa_strÚrr˜   Úmulsymrƒ   rƒ   r„   ÚmultjoinÁ   s
    z.OctaveCodePrinter._print_Mul.<locals>.multjoinú/ú./c                 s   s   | ]}|j V  qd S r‘   ©rÀ   )r–   Úbirƒ   rƒ   r„   r›   Ï   rœ   z/OctaveCodePrinter._print_Mul.<locals>.<genexpr>)rÀ   Úis_imaginaryr   ÚImaginaryUnitÚ
is_Integerr¤   r
   Úas_coeff_Mulr   rm   Úas_ordered_factorsr   Ú	make_argsÚis_commutativeÚis_Powr3   Úis_RationalÚis_negativer¨   r   Úbaser¿   ÚargsÚ
isinstanceÚInfinityr†   r   ÚqÚOneÚindexÚall)r~   ÚexprÚcÚer<   rÁ   ÚbÚ	pow_parenrÕ   ÚitemrÂ   Úb_strrÅ   Údivsymrƒ   rº   r„   Ú
_print_Mul‰   sb    
ÿ


ÿ
$
$ÿÿzOctaveCodePrinter._print_Mulc                 C   s,   |   |j¡}|   |j¡}|j}d |||¡S )Nz{} {} {})r¤   ÚlhsÚrhsÚrel_opr‹   )r~   rÜ   Úlhs_codeÚrhs_codeÚoprƒ   rƒ   r„   Ú_print_RelationalÓ   s    z#OctaveCodePrinter._print_Relationalc                 C   sÖ   t dd„ |jD ƒƒrdnd}t|ƒ}|jtjkr@d|  |j¡ S |jr´|jtj kr||jj	r`dnd}d| d|  |j¡  S |jtj
 kr´|jj	r–dnd}d| d	|  |j|¡  S d
|  |j|¡||  |j|¡f S )Nc                 s   s   | ]}|j V  qd S r‘   rÈ   r¸   rƒ   rƒ   r„   r›   Ú   rœ   z/OctaveCodePrinter._print_Pow.<locals>.<genexpr>ú^z.^zsqrt(%s)rÆ   rÇ   Ú1ú%sz%s%s%s)rÛ   rÕ   r
   r3   r   ÚHalfr¤   rÔ   rÐ   rÀ   rÙ   r·   )r~   rÜ   Ú	powsymbolÚPRECÚsymrƒ   rƒ   r„   Ú
_print_PowÙ   s    ÿzOctaveCodePrinter._print_Powc                 C   s(   t |ƒ}d|  |j|¡|  |j|¡f S )Nz%s^%s)r
   r·   rÔ   r3   ©r~   rÜ   rñ   rƒ   rƒ   r„   Ú_print_MatPowí   s    ÿzOctaveCodePrinter._print_MatPowc                 C   s(   t |ƒ}d|  |j|¡|  |j|¡f S )Nz%s \ %s)r
   r·   ÚmatrixÚvectorrô   rƒ   rƒ   r„   Ú_print_MatrixSolveò   s    ÿz$OctaveCodePrinter._print_MatrixSolvec                 C   s   dS )NÚpirƒ   ©r~   rÜ   rƒ   rƒ   r„   Ú	_print_Pi÷   s    zOctaveCodePrinter._print_Pic                 C   s   dS )NZ1irƒ   rú   rƒ   rƒ   r„   Ú_print_ImaginaryUnitû   s    z&OctaveCodePrinter._print_ImaginaryUnitc                 C   s   dS )Nzexp(1)rƒ   rú   rƒ   rƒ   r„   Ú_print_Exp1ÿ   s    zOctaveCodePrinter._print_Exp1c                 C   s   dS )Nz(1+sqrt(5))/2rƒ   rú   rƒ   rƒ   r„   Ú_print_GoldenRatio  s    z$OctaveCodePrinter._print_GoldenRatioc                 C   sä   ddl m} ddlm} ddlm} |j}|j}| jd st	|j|ƒrg }g }|j
D ]"\}	}
| |||	ƒ¡ | |
¡ qT|t||ƒŽ }|  |¡S | jd rº| |¡s®| |¡rº|  ||¡S |  |¡}|  |¡}|  d||f ¡S d S )Nr   )Ú
Assignment)Ú	Piecewise)ÚIndexedBasert   rs   z%s = %s)Úsympy.codegen.astrÿ   Ú$sympy.functions.elementary.piecewiser   Úsympy.tensor.indexedr  rå   ræ   Ú	_settingsrÖ   rÕ   r¨   rx   r¤   ÚhasÚ_doprint_loopsr‰   )r~   rÜ   rÿ   r   r  rå   ræ   ÚexpressionsÚ
conditionsrÞ   rÝ   Útemprè   ré   rƒ   rƒ   r„   Ú_print_Assignment	  s(    
ÿ

z#OctaveCodePrinter._print_Assignmentc                 C   s   dS )NÚinfrƒ   rú   rƒ   rƒ   r„   Ú_print_Infinity&  s    z!OctaveCodePrinter._print_Infinityc                 C   s   dS )Nz-infrƒ   rú   rƒ   rƒ   r„   Ú_print_NegativeInfinity*  s    z)OctaveCodePrinter._print_NegativeInfinityc                 C   s   dS )NÚNaNrƒ   rú   rƒ   rƒ   r„   Ú
_print_NaN.  s    zOctaveCodePrinter._print_NaNc                    s    dd  ‡ fdd„|D ƒ¡ d S )NÚ{ú, c                 3   s   | ]}ˆ   |¡V  qd S r‘   ©r¤   ©r–   rÁ   ©r~   rƒ   r„   r›   3  rœ   z0OctaveCodePrinter._print_list.<locals>.<genexpr>Ú}©Újoinrú   rƒ   r  r„   Ú_print_list2  s    zOctaveCodePrinter._print_listc                 C   s   dS )NÚtruerƒ   rú   rƒ   rƒ   r„   Ú_print_BooleanTrue9  s    z$OctaveCodePrinter._print_BooleanTruec                 C   s   dS )NÚfalserƒ   rú   rƒ   rƒ   r„   Ú_print_BooleanFalse=  s    z%OctaveCodePrinter._print_BooleanFalsec                 C   s   t |ƒ ¡ S r‘   )Ústrr¦   rú   rƒ   rƒ   r„   Ú_print_boolA  s    zOctaveCodePrinter._print_boolc                    sr   ˆ j ˆ jfdkrdS tjˆ jv r0dˆ j ˆ jf S ˆ j ˆ jfdkrNˆ ˆ d ¡S dd ‡ ‡fdd„tˆ j ƒD ƒ¡ S )	N)r   r   z[]zzeros(%s, %s))r¡   r¡   z[%s]z; c                 3   s2   | ]*}d   ‡fdd„ˆ |dd…f D ƒ¡V  qdS )ú c                    s   g | ]}ˆ   |¡‘qS rƒ   r  r  r  rƒ   r„   r¼   R  rœ   zAOctaveCodePrinter._print_MatrixBase.<locals>.<genexpr>.<listcomp>Nr  )r–   rÃ   ©ÚAr~   rƒ   r„   r›   R  s   ÿz6OctaveCodePrinter._print_MatrixBase.<locals>.<genexpr>)rš   rŸ   r   ÚZeror   r¤   r  r•   )r~   r"  rƒ   r!  r„   Ú_print_MatrixBaseI  s    ÿz#OctaveCodePrinter._print_MatrixBasec                 C   sx   ddl m} | ¡ }|dd„ |D ƒgƒ}|dd„ |D ƒgƒ}|dd„ |D ƒgƒ}d|  |¡|  |¡|  |¡|j|jf S )Nr   )ÚMatrixc                 S   s   g | ]}|d  d ‘qS )r   r¡   rƒ   ©r–   Úkrƒ   rƒ   r„   r¼   Z  rœ   z<OctaveCodePrinter._print_SparseRepMatrix.<locals>.<listcomp>c                 S   s   g | ]}|d  d  ‘qS )r¡   rƒ   r&  rƒ   rƒ   r„   r¼   [  rœ   c                 S   s   g | ]}|d  ‘qS )é   rƒ   r&  rƒ   rƒ   r„   r¼   \  rœ   zsparse(%s, %s, %s, %s, %s))Úsympy.matricesr%  Úcol_listr¤   rš   rŸ   )r~   r"  r%  ÚLÚIÚJÚAIJrƒ   rƒ   r„   Ú_print_SparseRepMatrixV  s    ÿz(OctaveCodePrinter._print_SparseRepMatrixc                 C   s.   | j |jtd ddd|jd |jd f  S )NÚAtomT)Ústrictz(%s, %s)r¡   )r·   Úparentr   r˜   r—   rú   rƒ   rƒ   r„   Ú_print_MatrixElementa  s    ÿz&OctaveCodePrinter._print_MatrixElementc                    sL   ‡ fdd„}ˆ   |j¡d ||j|jjd ƒ d ||j|jjd ƒ d S )Nc                    sŒ   | d d }| d }| d }ˆ   |¡}||kr2dnˆ   |¡}|dkrr|dkrX||krXdS ||krd|S |d | S nd |ˆ   |¡|f¡S d S )Nr   r¡   r(  r¢   ú:)r¤   r  )r¹   ÚlimÚlÚhÚstepÚlstrÚhstrr  rƒ   r„   Ústrsliceg  s    
z6OctaveCodePrinter._print_MatrixSlice.<locals>.strsliceú(r   r  r¡   ú))r¤   r2  Úrowslicer   Úcolslice)r~   rÜ   r;  rƒ   r  r„   Ú_print_MatrixSlicef  s    ÿÿþþz$OctaveCodePrinter._print_MatrixSlicec                    s0   ‡ fdd„|j D ƒ}dˆ  |jj¡d |¡f S )Nc                    s   g | ]}ˆ   |¡‘qS rƒ   r  )r–   r˜   r  rƒ   r„   r¼   |  rœ   z4OctaveCodePrinter._print_Indexed.<locals>.<listcomp>z%s(%s)r  )r©   r¤   rÔ   r¥   r  )r~   rÜ   Úindsrƒ   r  r„   Ú_print_Indexed{  s    z OctaveCodePrinter._print_Indexedc                 C   s   |   |j¡S r‘   )r¤   r¥   rú   rƒ   rƒ   r„   Ú
_print_Idx€  s    zOctaveCodePrinter._print_Idxc                    s&   t d ‰ dt‡ ‡fdd„|jD ƒƒ S )Nr   zdouble(%s == %s)c                 3   s   | ]}ˆ  |ˆ ¡V  qd S r‘   r¶   r¸   rº   rƒ   r„   r›   †  s   ÿz:OctaveCodePrinter._print_KroneckerDelta.<locals>.<genexpr>)r   ÚtuplerÕ   rú   rƒ   rº   r„   Ú_print_KroneckerDelta„  s    ÿz'OctaveCodePrinter._print_KroneckerDeltac                    s   d  ‡ ‡fdd„ˆ jD ƒ¡S )Nr¾   c                    s   g | ]}ˆ  |tˆ ƒ¡‘qS rƒ   )r·   r
   )r–   rL   ©rÜ   r~   rƒ   r„   r¼   Š  s   ÿz<OctaveCodePrinter._print_HadamardProduct.<locals>.<listcomp>)r  rÕ   rú   rƒ   rF  r„   Ú_print_HadamardProduct‰  s    ÿz(OctaveCodePrinter._print_HadamardProductc                 C   s*   t |ƒ}d |  |j|¡|  |j|¡g¡S )Nz.**)r
   r  r·   rÔ   r3   rô   rƒ   rƒ   r„   Ú_print_HadamardPower  s
    þz&OctaveCodePrinter._print_HadamardPowerc                    sP   |j }t|ƒdkr,|d |d kr,|d g}d ‡ fdd„|D ƒ¡}d| d S )	Nr(  r   r¡   r  c                 3   s   | ]}ˆ   |¡V  qd S r‘   r  )r–   Únr  rƒ   r„   r›   ˜  rœ   z4OctaveCodePrinter._print_Identity.<locals>.<genexpr>zeye(r=  )r   r¿   r  )r~   rÜ   r   Úsrƒ   r  r„   Ú_print_Identity”  s
    
z!OctaveCodePrinter._print_Identityc                 C   s$   d  |  |jd ¡|  |jd ¡¡S )Nz (gammainc({1}, {0}).*gamma({0}))r   r¡   ©r‹   r¤   rÕ   rú   rƒ   rƒ   r„   Ú_print_lowergamma›  s    ÿz#OctaveCodePrinter._print_lowergammac                 C   s$   d  |  |jd ¡|  |jd ¡¡S )Nz)(gammainc({1}, {0}, 'upper').*gamma({0}))r   r¡   rL  rú   rƒ   rƒ   r„   Ú_print_uppergamma¡  s    ÿz#OctaveCodePrinter._print_uppergammac                 C   s   d|   |jd tj ¡ S )Nzsinc(%s)r   )r¤   rÕ   r   ÚPirú   rƒ   rƒ   r„   Ú_print_sinc¦  s    zOctaveCodePrinter._print_sincc                 C   s   d|   |j¡|   |j¡f S )Nzbesselh(%s, 1, %s)©r¤   rm   Úargumentrú   rƒ   rƒ   r„   Ú_print_hankel1«  s    
ÿz OctaveCodePrinter._print_hankel1c                 C   s   d|   |j¡|   |j¡f S )Nzbesselh(%s, 2, %s)rQ  rú   rƒ   rƒ   r„   Ú_print_hankel2°  s    
ÿz OctaveCodePrinter._print_hankel2c                 C   sD   ddl m}m} |j}|tjd|  ƒ||jtj |ƒ }|  |¡S )Nr   )Úsqrtr-   r(  )	Úsympy.functionsrU  r-   rR  r   rO  rm   rï   r¤   )r~   rÜ   rU  r-   r¹   Úexpr2rƒ   rƒ   r„   Ú	_print_jn¶  s    $zOctaveCodePrinter._print_jnc                 C   sD   ddl m}m} |j}|tjd|  ƒ||jtj |ƒ }|  |¡S )Nr   )rU  r/   r(  )	rV  rU  r/   rR  r   rO  rm   rï   r¤   )r~   rÜ   rU  r/   r¹   rW  rƒ   rƒ   r„   Ú	_print_yn½  s    $zOctaveCodePrinter._print_ync                 C   s   d|   |jd ¡ S )Nzairy(0, %s)r   ©r¤   rÕ   rú   rƒ   rƒ   r„   Ú_print_airyaiÄ  s    zOctaveCodePrinter._print_airyaic                 C   s   d|   |jd ¡ S )Nzairy(1, %s)r   rZ  rú   rƒ   rƒ   r„   Ú_print_airyaiprimeÈ  s    z$OctaveCodePrinter._print_airyaiprimec                 C   s   d|   |jd ¡ S )Nzairy(2, %s)r   rZ  rú   rƒ   rƒ   r„   Ú_print_airybiÌ  s    zOctaveCodePrinter._print_airybic                 C   s   d|   |jd ¡ S )Nzairy(3, %s)r   rZ  rú   rƒ   rƒ   r„   Ú_print_airybiprimeÐ  s    z$OctaveCodePrinter._print_airybiprimec                 C   s*   |j \}}|dkr|  |¡S d|  |¡ S )Nr¡   z
expint(%s))rÕ   Ú_print_not_supportedr¤   )r~   rÜ   Úmur¹   rƒ   rƒ   r„   Ú_print_expintÔ  s    

zOctaveCodePrinter._print_expintc                    sD   t |jƒdksJ ‚djˆ j|jj d ‡ fdd„t|jƒD ƒ¡dS )Nr(  z{name}({args})r  c                    s   g | ]}ˆ   |¡‘qS rƒ   r  r¸   r  rƒ   r„   r¼   ß  rœ   z?OctaveCodePrinter._one_or_two_reversed_args.<locals>.<listcomp>)rŽ   rÕ   )r¿   rÕ   r‹   rz   r‚   Ú__name__r  Úreversedrú   rƒ   r  r„   Ú_one_or_two_reversed_argsÛ  s
    þz+OctaveCodePrinter._one_or_two_reversed_argsc              	   C   s<   dj | j|jj |  |jd ¡|  |j|jdd … Ž ¡dS )Nz{name}({arg1}, {arg2})r   r¡   )rŽ   Úarg1Úarg2)r‹   rz   r‚   rb  r¤   rÕ   Úfuncrú   rƒ   rƒ   r„   Ú_nested_binary_math_funcæ  s
    ýz*OctaveCodePrinter._nested_binary_math_funcc           
         s,  |j d jdkrtdƒ‚g }ˆ jd r~‡ fdd„|j d d… D ƒ}dˆ  |j d j¡ }d |¡| d	t|ƒ  }d
| d	 S t|j ƒD ]”\}\}}|dkr²| 	dˆ  |¡ ¡ n:|t|j ƒd krØ|dkrØ| 	d¡ n| 	dˆ  |¡ ¡ ˆ  |¡}	| 	|	¡ |t|j ƒd krˆ| 	d¡ qˆd |¡S 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.rt   c                    s(   g | ] \}}d   ˆ  |¡ˆ  |¡¡‘qS )z({0}).*({1}) + (~({0})).*()r‹   r¤   )r–   rÞ   rÝ   r  rƒ   r„   r¼   þ  s   þÿz6OctaveCodePrinter._print_Piecewise.<locals>.<listcomp>rî   z ...
r=  r<  r   zif (%s)r¡   Úelsezelseif (%s)r¢   Ú
)
rÕ   ÚcondÚ
ValueErrorr  r¤   rÜ   r  r¿   Ú	enumerater¨   )
r~   rÜ   r“   ÚecpairsÚelastÚpwr˜   rÞ   rÝ   Úcode0rƒ   r  r„   Ú_print_Piecewiseð  s*    

þ

z"OctaveCodePrinter._print_Piecewisec                 C   s0   t |jƒdkr"d|  |jd ¡ S |  |¡S d S )Nr¡   zzeta(%s)r   )r¿   rÕ   r¤   r_  rú   rƒ   rƒ   r„   Ú_print_zeta  s    zOctaveCodePrinter._print_zetac           
         sÂ   t |tƒr$|  | d¡¡}d |¡S d}d‰d‰ dd„ |D ƒ}‡fdd„|D ƒ}‡ fd	d„|D ƒ}g }d
}t|ƒD ]J\}}	|	dv rŽ| |	¡ qr||| 8 }| d|| |	f ¡ ||| 7 }qr|S )z0Accepts a string of code or a list of code linesTr±   z  )z
^function z^if ú^elseif ú^else$z^for )z^end$rt  ru  c                 S   s   g | ]}|  d ¡‘qS )z 	)Úlstrip)r–   Úlinerƒ   rƒ   r„   r¼   *  rœ   z1OctaveCodePrinter.indent_code.<locals>.<listcomp>c                    s&   g | ]‰ t t‡ fd d„ˆD ƒƒƒ‘qS )c                 3   s   | ]}t |ˆ ƒV  qd S r‘   r   ©r–   r_   ©rw  rƒ   r„   r›   ,  rœ   ú;OctaveCodePrinter.indent_code.<locals>.<listcomp>.<genexpr>©ÚintÚany©r–   )Ú	inc_regexry  r„   r¼   ,  s   ÿc                    s&   g | ]‰ t t‡ fd d„ˆD ƒƒƒ‘qS )c                 3   s   | ]}t |ˆ ƒV  qd S r‘   r   rx  ry  rƒ   r„   r›   .  rœ   rz  r{  r~  )Ú	dec_regexry  r„   r¼   .  s   ÿr   )r±   rj  z%s%s)rÖ   r  r’   Ú
splitlinesr  rm  r¨   )
r~   ÚcodeÚ
code_linesÚtabÚincreaseÚdecreaseÚprettyÚlevelrI  rw  rƒ   )r€  r  r„   r’     s.    


ÿ
ÿ
zOctaveCodePrinter.indent_code)Drb  Ú
__module__Ú__qualname__Ú__doc__ÚprintmethodÚlanguageÚ
_operatorsÚ_default_settingsrv   r‡   r‰   r   r   r”   r    r¯   rä   rë   ró   rõ   rø   rû   rü   rý   rþ   r  r  r  r  r  Ú_print_tupleÚ_print_TupleÚ_print_Listr  r  r  r$  r/  r3  r@  rB  rC  rE  rG  rH  rK  rM  rN  rP  rS  rT  rX  rY  r[  r\  r]  r^  ra  rd  Ú_print_DiracDeltaÚ_print_LambertWrh  Ú
_print_MaxÚ
_print_Minrr  rs  r’   Ú__classcell__rƒ   rƒ   r   r„   rc   ?   s’   ýøJ%rc   Nc                 K   s   t |ƒ | |¡S )aŠ  Converts `expr` to a string of Octave (or Matlab) code.

    The string uses a subset of the Octave language for Matlab compatibility.

    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].

    Examples
    ========

    >>> from sympy import octave_code, symbols, sin, pi
    >>> x = symbols('x')
    >>> octave_code(sin(x).series(x).removeO())
    'x.^5/120 - x.^3/6 + x'

    >>> from sympy import Rational, ceiling
    >>> x, y, tau = symbols("x, y, tau")
    >>> octave_code((2*tau)**Rational(7, 2))
    '8*sqrt(2)*tau.^(7/2)'

    Note that element-wise (Hadamard) operations are used by default between
    symbols.  This is because its very common in Octave to write "vectorized"
    code.  It is harmless if the values are scalars.

    >>> octave_code(sin(pi*x*y), assign_to="s")
    's = sin(pi*x.*y);'

    If you need a matrix product "*" or matrix power "^", you can specify the
    symbol as a ``MatrixSymbol``.

    >>> from sympy import Symbol, MatrixSymbol
    >>> n = Symbol('n', integer=True, positive=True)
    >>> A = MatrixSymbol('A', n, n)
    >>> octave_code(3*pi*A**3)
    '(3*pi)*A^3'

    This class uses several rules to decide which symbol to use a product.
    Pure numbers use "*", Symbols use ".*" and MatrixSymbols use "*".
    A HadamardProduct can be used to specify componentwise multiplication ".*"
    of two MatrixSymbols.  There is currently there is no easy way to specify
    scalar symbols, so sometimes the code might have some minor cosmetic
    issues.  For example, suppose x and y are scalars and A is a Matrix, then
    while a human programmer might write "(x^2*y)*A^3", we generate:

    >>> octave_code(x**2*y*A**3)
    '(x.^2.*y)*A^3'

    Matrices are supported using Octave inline notation.  When using
    ``assign_to`` with matrices, the name can be specified either as a string
    or as a ``MatrixSymbol``.  The dimensions must align in the latter case.

    >>> from sympy import Matrix, MatrixSymbol
    >>> mat = Matrix([[x**2, sin(x), ceiling(x)]])
    >>> octave_code(mat, assign_to='A')
    'A = [x.^2 sin(x) ceil(x)];'

    ``Piecewise`` expressions are implemented with logical masking by default.
    Alternatively, you can pass "inline=False" to use if-else conditionals.
    Note that if the ``Piecewise`` lacks a default term, represented by
    ``(expr, True)`` then an error will be thrown.  This is to prevent
    generating an expression that may not evaluate to anything.

    >>> from sympy import Piecewise
    >>> pw = Piecewise((x + 1, x > 0), (x, True))
    >>> octave_code(pw, assign_to=tau)
    'tau = ((x > 0).*(x + 1) + (~(x > 0)).*(x));'

    Note that any expression that can be generated normally can also exist
    inside a Matrix:

    >>> mat = Matrix([[x**2, pw, sin(x)]])
    >>> octave_code(mat, assign_to='A')
    'A = [x.^2 ((x > 0).*(x + 1) + (~(x > 0)).*(x)) sin(x)];'

    Custom printing can be defined for certain types by passing a dictionary of
    "type" : "function" to the ``user_functions`` kwarg.  Alternatively, the
    dictionary value can be a list of tuples i.e., [(argument_test,
    cfunction_string)].  This can be used to call a custom Octave function.

    >>> from sympy import Function
    >>> f = Function('f')
    >>> g = Function('g')
    >>> custom_functions = {
    ...   "f": "existing_octave_fcn",
    ...   "g": [(lambda x: x.is_Matrix, "my_mat_fcn"),
    ...         (lambda x: not x.is_Matrix, "my_fcn")]
    ... }
    >>> mat = Matrix([[1, x]])
    >>> octave_code(f(x) + g(x) + g(mat), user_functions=custom_functions)
    'existing_octave_fcn(x) + my_fcn(x) + my_mat_fcn([1 x])'

    Support for loops is provided through ``Indexed`` types. With
    ``contract=True`` these expressions will be turned into loops, whereas
    ``contract=False`` will just print the assignment expression that should be
    looped over:

    >>> from sympy import Eq, IndexedBase, Idx
    >>> len_y = 5
    >>> y = IndexedBase('y', shape=(len_y,))
    >>> t = IndexedBase('t', shape=(len_y,))
    >>> Dy = IndexedBase('Dy', shape=(len_y-1,))
    >>> i = Idx('i', len_y-1)
    >>> e = Eq(Dy[i], (y[i+1]-y[i])/(t[i+1]-t[i]))
    >>> octave_code(e.rhs, assign_to=e.lhs, contract=False)
    'Dy(i) = (y(i + 1) - y(i))./(t(i + 1) - t(i));'
    )rc   Údoprint)rÜ   Ú	assign_tor   rƒ   rƒ   r„   Úoctave_code=  s     	rš  c                 K   s   t t| fi |¤Žƒ dS )zŒPrints the Octave (or Matlab) representation of the given expression.

    See `octave_code` for the meaning of the optional arguments.
    N)Úprintrš  )rÜ   r   rƒ   rƒ   r„   Úprint_octave_codeÈ  s    rœ  )N)r‹  Útypingr   r   ÚtDictÚ
sympy.corer   r   r   r   Zsympy.core.mulr   Zsympy.printing.codeprinterr	   Úsympy.printing.precedencer
   r   r_   r   ry   r|   rc   rš  rœ  rƒ   rƒ   rƒ   r„   Ú<module>   sP   è    
 