o
    oÇhÇc  ã                	   @  s   d Z ddlmZ ddl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 ddlmZmZ dd	lm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/d0d1d2d3d4œ¥ZG d5d6„ d6eƒZd<d8d9„Zd:d;„ Zd7S )=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.

é    )Úannotations)ÚAny)ÚMulÚPowÚSÚRational)Ú_keep_coeff)Úequal_valued)Ú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ÚabsÚargÚangleÚbinomialÚbincoeffÚceilingÚceilÚ
chebyshevuÚ
chebyshevUÚ
chebyshevtÚ
chebyshevTÚChiÚcoshintÚCiÚcosintÚ	conjugateÚconjÚ
DiracDeltaÚdiracÚ	HeavisideÚ	heavisideÚimÚimagÚlaguerreÚ	laguerreLÚLambertWÚlambertwÚliÚlogintÚloggammaÚgammalnÚMaxÚmaxÚminÚmodÚpsiÚrealÚ
pochhammerÚsinhintÚsinint)ÚMinÚModÚ	polygammaÚreÚRisingFactorialÚShiÚSic                      s  e Zd ZU dZdZdZddddœZeej	fi di d	d	d
œ¤ŽZ	de
d< 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d/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd7d8„ Z e Z!e Z"e Z#d9d:„ Z$d;d<„ Z%d=d>„ Z&d?d@„ Z'dAdB„ Z(dCdD„ Z)dEdF„ Z*dGdH„ Z+dIdJ„ Z,dKdL„ Z-dMdN„ Z.dOdP„ Z/dQdR„ Z0dSdT„ Z1dUdV„ Z2dWdX„ Z3dYdZ„ Z4d[d\„ Z5d]d^„ Z6d_d`„ Z7dadb„ Z8dcdd„ Z9dedf„ Z:dgdh„ Z;didj„ Z<dkdl„ Z=e= Z>Z?dmdn„ Z@e@ ZAZBdodp„ ZCdqdr„ ZDdsdt„ ZE‡  ZFS )uÚOctaveCodePrinterzL
    A printer to convert expressions to strings of Octave/Matlab code.
    Ú_octaveÚOctaveú&ú|ú~)ÚandÚorÚnoté   T)Ú	precisionÚuser_functionsÚcontractÚinlinezdict[str, Any]Ú_default_settingsc                   sH   t ƒ  |¡ ttttƒƒ| _| j ttƒ¡ | di ¡}| j |¡ d S )Nr{   )	ÚsuperÚ__init__ÚdictÚzipÚknown_fcns_src1Úknown_functionsÚupdateÚknown_fcns_src2Úget)ÚselfÚsettingsÚ	userfuncs©Ú	__class__© úi/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/printing/octave.pyr€   Y   s
   zOctaveCodePrinter.__init__c                 C  s   |d S )Né   r   )rˆ   Úpr   r   rŽ   Ú_rate_index_positiona   ó   z&OctaveCodePrinter._rate_index_positionc                 C  s   d| S )Nz%s;r   )rˆ   Ú
codestringr   r   rŽ   Ú_get_statemente   r’   z OctaveCodePrinter._get_statementc                 C  s
   d  |¡S )Nz% {}©Úformat)rˆ   Útextr   r   rŽ   Ú_get_commenti   ó   
zOctaveCodePrinter._get_commentc                 C  s   d  ||¡S )Nz{} = {};r•   )rˆ   ÚnameÚvaluer   r   rŽ   Ú_declare_number_constm   ó   z'OctaveCodePrinter._declare_number_constc                 C  s
   |   |¡S ©N)Úindent_code)rˆ   Úlinesr   r   rŽ   Ú_format_codeq   r™   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>x   s   €$ z=OctaveCodePrinter._traverse_matrix_indices.<locals>.<genexpr>)Úshaper¢   )rˆ   ÚmatÚcolsr   r¦   rŽ   Ú_traverse_matrix_indicesu   s   
z*OctaveCodePrinter._traverse_matrix_indicesc                 C  s^   g }g }|D ]$}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                   sb  |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 ]l}	|	jr‹|	jr‹|	jjr‹|	jjr‹|	jdkrj| t|	j|	j dd¡ qGt|	jd jƒd	krt|	jtƒr| |	¡ | t|	j|	j ƒ¡ qG|	jr®|	tjur®|	jd	kr | t|	jƒ¡ |	jd	kr­| t|	jƒ¡ qG| |	¡ qG|p¹tjg}‡ ‡fd
d„|D ƒ}
‡ ‡fdd„|D ƒ}|D ]}	|	j|v rçd|| |	j¡  || |	j¡< qÐdd„ }|sõ||||
ƒ S t|ƒd	kr|d j rdnd}||||
ƒ | |d  S tdd„ |D ƒƒrdnd}||||
ƒ | d|||ƒ  S )Nz%sir   ú-Ú )ÚoldÚnoneéÿÿÿÿF)Úevaluater­   c                   ó   g | ]}ˆ  |ˆ ¡‘qS r   ©Úparenthesize©r£   Úx©Úprecrˆ   r   rŽ   Ú
<listcomp>¶   ó    z0OctaveCodePrinter._print_Mul.<locals>.<listcomp>c                   rÂ   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  ó    | ]}|j V  qd S rž   ©rÎ   )r£   Úbir   r   rŽ   r¨   Í   ó   € z/OctaveCodePrinter._print_Mul.<locals>.<genexpr>)rÎ   Úis_imaginaryr   ÚImaginaryUnitÚ
is_Integerr°   r   Úas_coeff_Mulr   ÚorderÚas_ordered_factorsr   Ú	make_argsÚis_commutativeÚis_Powr4   Ú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‡   sf   
ÿ


ÿ
 


€
 €ÿÿ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|ƒ}t|jdƒr d|  |j¡ S |jrXt|jdƒr=|jjr/dnd	}d
| d|  |j¡  S t|jdƒrX|jjrIdnd	}d
| d|  	|j|¡  S d|  	|j|¡||  	|j|¡f S )Nc                 s  rÖ   rž   r×   rÅ   r   r   rŽ   r¨   Ø   rÙ   z/OctaveCodePrinter._print_Pow.<locals>.<genexpr>ú^z.^g      à?zsqrt(%s)g      à¿rÔ   rÕ   Ú1rÀ   ú%sz%s%s%s)
rì   ræ   r   r	   r4   r°   rå   rá   rÎ   rÄ   )rˆ   rí   Ú	powsymbolÚPRECÚsymr   r   rŽ   Ú
_print_Pow×   s   ÿzOctaveCodePrinter._print_Powc                 C  ó(   t |ƒ}d|  |j|¡|  |j|¡f S )Nz%s^%s)r   rÄ   rå   r4   ©rˆ   rí   r  r   r   rŽ   Ú_print_MatPowë   ó   ÿzOctaveCodePrinter._print_MatPowc                 C  r  )Nz%s \ %s)r   rÄ   ÚmatrixÚvectorr  r   r   rŽ   Ú_print_MatrixSolveð   r  z$OctaveCodePrinter._print_MatrixSolvec                 C  ó   dS )NÚpir   ©rˆ   rí   r   r   rŽ   Ú	_print_Piõ   ó   zOctaveCodePrinter._print_Pic                 C  r  )NÚ1ir   r  r   r   rŽ   Ú_print_ImaginaryUnitù   r  z&OctaveCodePrinter._print_ImaginaryUnitc                 C  r  )Nzexp(1)r   r  r   r   rŽ   Ú_print_Exp1ý   r  zOctaveCodePrinter._print_Exp1c                 C  r  )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 sHt	|j|ƒrHg }g }|j
D ]\}	}
| |||	ƒ¡ | |
¡ q*|t||ƒŽ }|  |¡S | jd r]| |¡sW| |¡r]|  ||¡S |  |¡}|  |¡}|  d||f ¡S )Nr   )Ú
Assignment)Ú	Piecewise)ÚIndexedBaser}   r|   z%s = %s)Úsympy.codegen.astr  Ú$sympy.functions.elementary.piecewiser  Úsympy.tensor.indexedr  rö   r÷   Ú	_settingsrç   ræ   r´   r‚   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  r  )NÚinfr   r  r   r   rŽ   Ú_print_Infinity$  r  z!OctaveCodePrinter._print_Infinityc                 C  r  )Nz-infr   r  r   r   rŽ   Ú_print_NegativeInfinity(  r  z)OctaveCodePrinter._print_NegativeInfinityc                 C  r  )NÚNaNr   r  r   r   rŽ   Ú
_print_NaN,  r  zOctaveCodePrinter._print_NaNc                   s    dd  ‡ fdd„|D ƒ¡ d S )NÚ{ú, c                 3  ó    | ]}ˆ   |¡V  qd S rž   ©r°   ©r£   rÏ   ©rˆ   r   rŽ   r¨   1  ó   € z0OctaveCodePrinter._print_list.<locals>.<genexpr>Ú}©Újoinr  r   r+  rŽ   Ú_print_list0  s    zOctaveCodePrinter._print_listc                 C  r  )NÚtruer   r  r   r   rŽ   Ú_print_BooleanTrue7  r  z$OctaveCodePrinter._print_BooleanTruec                 C  r  )NÚfalser   r  r   r   rŽ   Ú_print_BooleanFalse;  r  z%OctaveCodePrinter._print_BooleanFalsec                 C  s   t |ƒ ¡ S rž   )Ústrr²   r  r   r   rŽ   Ú_print_bool?  r   zOctaveCodePrinter._print_boolc                   sr   ˆ j ˆ jfdkr
dS tjˆ jv rdˆ j ˆ jf S ˆ j ˆ jfdkr'ˆ ˆ 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  s4    | ]}d   ‡fdd„ˆ |dd…f D ƒ¡V  qdS )ú c                   ó   g | ]}ˆ   |¡‘qS r   r)  r*  r+  r   rŽ   rÉ   P  ó    zAOctaveCodePrinter._print_MatrixBase.<locals>.<genexpr>.<listcomp>Nr.  )r£   rÑ   ©ÚArˆ   r   rŽ   r¨   P  s   € ,ÿz6OctaveCodePrinter._print_MatrixBase.<locals>.<genexpr>)r§   r«   r   ÚZeror©   r°   r/  r¢   )rˆ   r;  r   r:  rŽ   Ú_print_MatrixBaseG  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É   X  rÊ   z<OctaveCodePrinter._print_SparseRepMatrix.<locals>.<listcomp>c                 S  s   g | ]}|d  d  ‘qS )r­   r   r?  r   r   rŽ   rÉ   Y  rÊ   c                 S  s   g | ]}|d  ‘qS )é   r   r?  r   r   rŽ   rÉ   Z  s    zsparse(%s, %s, %s, %s, %s))Úsympy.matricesr>  Úcol_listr°   r§   r«   )rˆ   r;  r>  ÚLÚIÚJÚAIJr   r   rŽ   Ú_print_SparseRepMatrixT  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_MatrixElement_  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rdnˆ   |¡}|dkr8|dkr,||kr,dS ||kr2|S |d | S d |ˆ   |¡|f¡S )Nr   r­   rA  r®   ú:)r°   r/  )rÆ   ÚlimÚlÚhÚstepÚlstrÚhstrr+  r   rŽ   Ústrslicee  s   
z6OctaveCodePrinter._print_MatrixSlice.<locals>.strsliceú(r   r'  r­   ú))r°   rK  Úrowslicer©   Úcolslice)rˆ   rí   rT  r   r+  rŽ   Ú_print_MatrixSliced  s   ÿÿþþz$OctaveCodePrinter._print_MatrixSlicec                   s0   ‡ fdd„|j D ƒ}dˆ  |jj¡d |¡f S )Nc                   r8  r   r)  )r£   r¥   r+  r   rŽ   rÉ   z  r9  z4OctaveCodePrinter._print_Indexed.<locals>.<listcomp>z%s(%s)r'  )rµ   r°   rå   r±   r/  )rˆ   rí   Úindsr   r+  rŽ   Ú_print_Indexedy  s   z OctaveCodePrinter._print_Indexedc                 C  s   |   |j¡S rž   )r°   r±   r  r   r   rŽ   Ú
_print_Idx~  r   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£   rB   ©rí   rˆ   r   rŽ   rÉ   ˆ  s    ÿz<OctaveCodePrinter._print_HadamardProduct.<locals>.<listcomp>)r/  ræ   r  r   r_  rŽ   Ú_print_HadamardProduct‡  s   ÿz(OctaveCodePrinter._print_HadamardProductc                 C  s*   t |ƒ}d |  |j|¡|  |j|¡g¡S )Nz.**)r   r/  rÄ   rå   r4   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 )	NrA  r   r­   r'  c                 3  r(  rž   r)  )r£   Únr+  r   rŽ   r¨   –  r,  z4OctaveCodePrinter._print_Identity.<locals>.<genexpr>zeye(rV  )r©   rÍ   r/  )rˆ   rí   r©   Úsr   r+  rŽ   Ú_print_Identity’  s
   
z!OctaveCodePrinter._print_Identityc                 C  ó$   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  re  )Nz)(gammainc({1}, {0}, 'upper').*gamma({0}))r   r­   rf  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  ó   d|   |j¡|   |j¡f S )Nzbesselh(%s, 1, %s)©r°   rÞ   Úargumentr  r   r   rŽ   Ú_print_hankel1©  ó   
ÿz OctaveCodePrinter._print_hankel1c                 C  rk  )Nzbesselh(%s, 2, %s)rl  r  r   r   rŽ   Ú_print_hankel2®  ro  z OctaveCodePrinter._print_hankel2c                 C  óD   ddl m}m} |j}|tjd|  ƒ||jtj |ƒ }|  |¡S )Nr   )Úsqrtr.   rA  )	Úsympy.functionsrr  r.   rm  r   ri  rÞ   ÚHalfr°   )rˆ   rí   rr  r.   rÆ   Úexpr2r   r   rŽ   Ú	_print_jn´  ó   $
zOctaveCodePrinter._print_jnc                 C  rq  )Nr   )rr  r0   rA  )	rs  rr  r0   rm  r   ri  rÞ   rt  r°   )rˆ   rí   rr  r0   rÆ   ru  r   r   rŽ   Ú	_print_yn»  rw  zOctaveCodePrinter._print_ync                 C  ó   d|   |jd ¡ S )Nzairy(0, %s)r   ©r°   ræ   r  r   r   rŽ   Ú_print_airyaiÂ  ó   zOctaveCodePrinter._print_airyaic                 C  ry  )Nzairy(1, %s)r   rz  r  r   r   rŽ   Ú_print_airyaiprimeÆ  r|  z$OctaveCodePrinter._print_airyaiprimec                 C  ry  )Nzairy(2, %s)r   rz  r  r   r   rŽ   Ú_print_airybiÊ  r|  zOctaveCodePrinter._print_airybic                 C  ry  )Nzairy(3, %s)r   rz  r  r   r   rŽ   Ú_print_airybiprimeÎ  r|  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 )NrA  z{name}({args})r'  c                   r8  r   r)  rÅ   r+  r   rŽ   rÉ   Ý  r9  z?OctaveCodePrinter._one_or_two_reversed_args.<locals>.<listcomp>)rš   ræ   )rÍ   ræ   r–   r„   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–   r„   rŒ   rƒ  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 ]J\}\}}|dkrY| 	dˆ  |¡ ¡ n|t|j ƒd krl|dkrl| 	d¡ n
| 	dˆ  |¡ ¡ ˆ  |¡}	| 	|	¡ |t|j ƒd krŽ| 	d¡ qDd |¡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.r}   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 ...
rV  rU  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  s,   t |jƒdkrd|  |jd ¡ S |  |¡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 ]%\}}	|	dv rG| |	¡ q9||| 8 }| d|| |	f ¡ ||| 7 }q9|S )z0Accepts a string of code or a list of code linesTr½   z  )z
^function z^if ú^elseif ú^else$z^for )z^end$r•  r–  c                 S  s   g | ]}|  d ¡‘qS )z 	)Úlstrip)r£   Úliner   r   rŽ   rÉ   (  r9  z1OctaveCodePrinter.indent_code.<locals>.<listcomp>c                   ó&   g | ]‰ t t‡ fd d„ˆD ƒƒƒ‘qS )c                 3  ó    | ]}t |ˆ ƒV  qd S rž   r   ©r£   rl   ©r˜  r   rŽ   r¨   *  r,  ú;OctaveCodePrinter.indent_code.<locals>.<listcomp>.<genexpr>©ÚintÚany©r£   )Ú	inc_regexrœ  rŽ   rÉ   *  ó    ÿc                   r™  )c                 3  rš  rž   r   r›  rœ  r   rŽ   r¨   ,  r,  r  rž  r¡  )Ú	dec_regexrœ  rŽ   rÉ   ,  r£  r   )r½   r‹  z%s%s)rç   r5  rŸ   Ú
splitlinesr/  rŽ  r´   )
rˆ   ÚcodeÚ
code_linesÚtabÚincreaseÚdecreaseÚprettyÚlevelrb  r˜  r   )r¤  r¢  rŽ   rŸ     s.   


ÿ
ÿ
zOctaveCodePrinter.indent_code)Grƒ  Ú
__module__Ú__qualname__Ú__doc__ÚprintmethodÚlanguageÚ
_operatorsr   r
   r~   Ú__annotations__r€   r‘   r”   r˜   rœ   r¡   r¬   r»   rõ   rü   r  r  r
  r  r  r  r  r   r"  r#  r%  r0  Ú_print_tupleÚ_print_TupleÚ_print_Listr2  r4  r6  r=  rH  rL  rY  r[  r\  r^  r`  ra  rd  rg  rh  rj  rn  rp  rv  rx  r{  r}  r~  r  r‚  r…  Ú_print_DiracDeltaÚ_print_LambertWr‰  Ú
_print_MaxÚ
_print_Minr“  r”  rŸ   Ú__classcell__r   r   r‹   rŽ   rp   A   sŽ   
 ý
üJ%rp   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));'
    )rp   Ú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À  rž   )r¯  Ú
__future__r   Útypingr   Ú
sympy.corer   r   r   r   Úsympy.core.mulr   Úsympy.core.numbersr	   Úsympy.printing.codeprinterr
   Úsympy.printing.precedencer   r   rl   r   rƒ   r†   rp   r¾  rÀ  r   r   r   rŽ   Ú<module>   sx    ÿþýüûúùø	÷
öõôóòñðïè   
} 