
    Edc              	          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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/d0d1d2Z G d3 d4e          Zd8d6Zd7 Zd5S )9ai  
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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            	           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	Zd Z	d Z
d Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd  Zd! Zd" Zd# ZeZeZeZ d$ Z!d% Z"d& Z#d' Z$d( Z%d) Z&d* Z'd+ Z(d, Z)d- Z*d. Z+d/ Z,d0 Z-d1 Z.d2 Z/d3 Z0d4 Z1d5 Z2d6 Z3d7 Z4d8 Z5d9 Z6d: Z7d; Z8d< Z9d= Z:e:xZ;Z<d> Z=e=xZ>Z?d? Z@d@ ZAdA ZB xZCS )BOctaveCodePrinterzL
    A printer to convert expressions to strings of Octave/Matlab code.
    _octaveOctave&|~)andornotNauto   TF)order	full_prec	precisionuser_functionshumanallow_unknown_functionscontractinlinec                 Z   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__s      5lib/python3.11/site-packages/sympy/printing/octave.pyr   zOctaveCodePrinter.__init__[   s    """#C$I$IJJ##D$9$9:::LL!1266	##I.....    c                     |dz  S )N    )r   ps     r   _rate_index_positionz&OctaveCodePrinter._rate_index_positionc   s    s
r   c                     d|z  S )Nz%s;r   )r   
codestrings     r   _get_statementz OctaveCodePrinter._get_statementg   s    z!!r   c                 ,    d                     |          S )Nz% {}format)r   texts     r   _get_commentzOctaveCodePrinter._get_commentk   s    }}T"""r   c                 .    d                     ||          S )Nz{} = {};r   )r   namevalues      r   _declare_number_constz'OctaveCodePrinter._declare_number_consto   s      u---r   c                 ,    |                      |          S N)indent_code)r   liness     r   _format_codezOctaveCodePrinter._format_codes   s    &&&r   c                 N    |j         \  }fdt          |          D             S )Nc              3   D   K   | ]}t                    D ]}||fV  	d S r   )range).0jirowss      r   	<genexpr>z=OctaveCodePrinter._traverse_matrix_indices.<locals>.<genexpr>z   s:      AA1U4[[AAAAAAAAAAr   )shaper   )r   matcolsr   s      @r   _traverse_matrix_indicesz*OctaveCodePrinter._traverse_matrix_indicesw   s.    Y
dAAAAdAAAAr   c           	          g }g }|D ]f}t          | j        |j        |j        dz   |j        dz   g          \  }}}|                    d|d|d|           |                    d           g||fS )N   zfor  = :end)map_printlabellowerupperappend)r   indices
open_linesclose_linesr   varstartstops           r   _get_loop_opening_endingz*OctaveCodePrinter._get_loop_opening_ending}   s    
 	& 	&A"4;Wagk17Q;7 9  9C###uuuddCDDDu%%%%;&&r   c                     |j         rA|j        r:t          j        |z  j        r&d                     t          j         |z            z  S t          |          |                                \  }}|dk     rt          | |          }d}nd}g }g }g } j	        dvr|
                                }nt          j        |          }|D ]v}	|	j        r|	j        r|	j        j        r|	j        j        r|	j        dk    r1|                    t'          |	j        |	j         d                     et+          |	j        d         j                  d	k    r/t/          |	j        t                    r|                    |	           |                    t'          |	j        |	j                              |	j        rt|	t          j        urf|	j        d	k    r'|                    t5          |	j                             |	j        d	k    r'|                    t5          |	j                             a|                    |	           x|pt          j        g} fd
|D             }
 fd|D             }|D ]I}	|	j        |v r>d||                    |	j                           z  ||                    |	j                  <   Jd }|s| |||
          z   S t+          |          d	k    r,|d         j         rdnd}| |||
          z   |z   |d         z   S t=          d |D                       rdnd}| |||
          z   |z   d |||          z  z   S )Nz%sir   - )oldnoneF)evaluater   c                 <    g | ]}                     |          S r   parenthesizer   xprecr   s     r   
<listcomp>z0OctaveCodePrinter._print_Mul.<locals>.<listcomp>   )    777""1d++777r   c                 <    g | ]}                     |          S r   r   r   s     r   r   z0OctaveCodePrinter._print_Mul.<locals>.<listcomp>   r   r   z(%s)c                     |d         }t          dt          |                     D ]$}| |dz
           j        rdnd}||z   ||         z   }%|S )Nr   r   *.*)r   len	is_number)aa_strrr   mulsyms        r   multjoinz.OctaveCodePrinter._print_Mul.<locals>.multjoin   s\    aA1c!ff%% * * !!A# 0:dJq)Hr   /./c              3   $   K   | ]}|j         V  d S r   r   )r   bis     r   r   z/OctaveCodePrinter._print_Mul.<locals>.<genexpr>   s$      99999999r   )r   is_imaginaryr   ImaginaryUnit
is_Integerr   r   as_coeff_Mulr	   r|   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   s   `             @r   
_print_MulzOctaveCodePrinter._print_Mul   s   N 	>t0 	>%1	>4;;'7'<==== $  ""1q5 	r1%%DDDD	:_, 	'**,,DD =&&D  	 	D#  8L ,8r> 8HHSTXIFFFGGGG49Q<,--2 /z$)S7Q7Q /!((...HHSTXI667777! d!*&< 6Q; /HHXdf--...6Q; /HHXdf--...L!%77777Q77777777Q777  	O 	ODyA~ O,2U17749;M;M5N,Naggdi(()	 	 	  	:((1e,,,,VVq[ 	:aDN4SSF((1e,,,v5a@@99q99999CSStF88Au---#hhq%&8&889 :r   c                     |                      |j                  }|                      |j                  }|j        }d                    |||          S )Nz{} {} {})r   lhsrhsrel_opr   )r   r   lhs_coderhs_codeops        r   _print_Relationalz#OctaveCodePrinter._print_Relational   sG    ;;tx((;;tx(([  2x888r   c                 p   t          d |j        D                       rdnd}t          |          }|j        t          j        k    rd|                     |j                  z  S |j        r|j        t          j         k    r3|j        j	        rdnd}d|z   d|                     |j                  z  z   S |j        t          j
         k    r4|j        j	        rdnd}d|z   d|                     |j        |          z  z   S |                     |j        |          ||                     |j        |          S )	Nc              3   $   K   | ]}|j         V  d S r   r   )r   r   s     r   r   z/OctaveCodePrinter._print_Pow.<locals>.<genexpr>   s$      >>qq{>>>>>>r   ^z.^zsqrt(%s)r   r   1%s)r   r   r   r4   r   Halfr   r   r   r   r   r   )r   r   	powsymbolPRECsyms        r   
_print_PowzOctaveCodePrinter._print_Pow   s@   >>DI>>>>>HCCD	$8qv 	7DI 6 666 	MxAF7" G!Y0:ccdSy:DI0F0F#FFFxAE6! M!Y0:ccdSy4$*;*;DIt*L*L#LLL,,TY===yy,,TXt<<<> 	>r   c                     t          |          }|                     |j        |          d|                     |j        |          S )Nr  )r   r   r   r4   r   r   r  s      r   _print_MatPowzOctaveCodePrinter._print_MatPow   sL    $++DIt<<<<++DHd;;;= 	=r   c                     t          |          }|                     |j        |          d|                     |j        |          S )Nz \ )r   r   matrixvectorr  s      r   _print_MatrixSolvez$OctaveCodePrinter._print_MatrixSolve   sN    $!..t{DAAAA!..t{DAAAC 	Cr   c                     dS )Npir   r   r   s     r   	_print_PizOctaveCodePrinter._print_Pi       tr   c                     dS )N1ir   r  s     r   _print_ImaginaryUnitz&OctaveCodePrinter._print_ImaginaryUnit   r  r   c                     dS )Nzexp(1)r   r  s     r   _print_Exp1zOctaveCodePrinter._print_Exp1   s    xr   c                     dS )Nz(1+sqrt(5))/2r   r  s     r   _print_GoldenRatioz$OctaveCodePrinter._print_GoldenRatio  s	     r   c                    ddl m} ddlm} ddlm} |j        }|j        }| j        d         st          |j        |          rmg }g }|j
        D ]9\  }	}
|                     |||	                     |                    |
           : |t          ||           }|                     |          S | j        d         r@|                    |          s|                    |          r|                     ||          S |                     |          }|                     |          }|                     |d|          S )Nr   )
Assignment)	Piecewise)IndexedBaser   r   r   )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  s                 r   _print_Assignmentz#OctaveCodePrinter._print_Assignment	  sz   000000BBBBBB444444hh~h' 		%Jtx,K,K 		% KJ( % %A""::c1#5#5666!!!$$$$9c+z::;D;;t$$$>*% 	I377;+?+? 	I$$	I &&sC000{{3''H{{3''H&&HHHhh'GHHHr   c                     dS )Ninfr   r  s     r   _print_Infinityz!OctaveCodePrinter._print_Infinity&      ur   c                     dS )Nz-infr   r  s     r   _print_NegativeInfinityz)OctaveCodePrinter._print_NegativeInfinity*      vr   c                     dS )NNaNr   r  s     r   
_print_NaNzOctaveCodePrinter._print_NaN.  r8  r   c                 R     dd                      fd|D                       z   dz   S )N{, c              3   B   K   | ]}                     |          V  d S r   r   r   r   r   s     r   r   z0OctaveCodePrinter._print_list.<locals>.<genexpr>3  s-      <<!t{{1~~<<<<<<r   }joinr  s   ` r   _print_listzOctaveCodePrinter._print_list2  s4    TYY<<<<t<<<<<<sBBr   c                     dS )Ntruer   r  s     r   _print_BooleanTruez$OctaveCodePrinter._print_BooleanTrue9  r;  r   c                     dS )Nfalser   r  s     r   _print_BooleanFalsez%OctaveCodePrinter._print_BooleanFalse=  s    wr   c                 D    t          |                                          S r   )strr   r  s     r   _print_boolzOctaveCodePrinter._print_boolA  s    4yy   r   c                 B    j         j        fdk    rdS t          j        j        v rdj         dj        dS j         j        fdk    r                     d                   S dd                     fd	t          j                   D                       z  S )
N)r   r   z[]zzeros(rA  ))r   r   z[%s]z; c              3   n   K   | ]/}d                      fd|ddf         D                       V  0dS ) c                 :    g | ]}                     |          S r   rC  rD  s     r   r   zAOctaveCodePrinter._print_MatrixBase.<locals>.<genexpr>.<listcomp>R  s#    +L+L+LqDKKNN+L+L+Lr   NrF  )r   r   Ar   s     r   r   z6OctaveCodePrinter._print_MatrixBase.<locals>.<genexpr>R  sd       ": ":&' #&((+L+L+L+LAadG+L+L+L"M"M ": ": ": ": ": ":r   )r   r   r   Zeror   r   rG  r   )r   rW  s   ``r   _print_MatrixBasez#OctaveCodePrinter._print_MatrixBaseI  s    FAFv% 	(4Vqw 	( 	(&'fffafff55faf' 	(;;qw'''		 ": ": ": ": ":+0==": ": ": : : : 	:r   c                 d   ddl m} |                                } |d |D             g          } |d |D             g          } |d |D             g          }d|                     |          d|                     |          d|                     |          d|j        d|j        dS )	Nr   )Matrixc                 $    g | ]}|d          dz   S )r   r   r   r   ks     r   r   z<OctaveCodePrinter._print_SparseRepMatrix.<locals>.<listcomp>Z       )))!QqTAX)))r   c                 $    g | ]}|d          d z   S )r   r   r]  s     r   r   z<OctaveCodePrinter._print_SparseRepMatrix.<locals>.<listcomp>[  r_  r   c                     g | ]
}|d          S )   r   r]  s     r   r   z<OctaveCodePrinter._print_SparseRepMatrix.<locals>.<listcomp>\  s    '''qt'''r   zsparse(rA  rS  )sympy.matricesr[  col_listr   r   r   )r   rW  r[  LIJAIJs          r   _print_SparseRepMatrixz(OctaveCodePrinter._print_SparseRepMatrixV  s    ))))))JJLLF))q)))*++F))q)))*++f''Q'''())/3{{1~~~~t{{1~~~~,0KK,<,<,<,<afffafffN 	Nr   c                     |                      |j        t          d         d          d|j        dz   d|j        dz   dz   S )NAtomT)strict(r   rA  rS  )r   parentr   r   r   r  s     r   _print_MatrixElementz&OctaveCodePrinter._print_MatrixElementa  sJ      j.@ NNN FQJJJ


34 	4r   c                       fd}                      |j                  dz    ||j        |j        j        d                   z   dz    ||j        |j        j        d                   z   dz   S )Nc                 <   | d         dz   }| d         }| d         }                     |          }||k    rdn                     |          }|dk    r|dk    r||k    rdS ||k    r|S |dz   |z   S d                    |                     |          |f          S )Nr   r   rb  r   r   )r   rG  )r   limlhsteplstrhstrr   s          r   strslicez6OctaveCodePrinter._print_MatrixSlice.<locals>.strsliceg  s    !qA!AQ4D;;q>>DH855$++a..Dqy A6 a3h 36 -K#:,,xxt{{4'8'8$ ?@@@r   rm  r   rA  r   rS  )r   rn  rowslicer   colslice)r   r   rx  s   `  r   _print_MatrixSlicez$OctaveCodePrinter._print_MatrixSlicef  s    	A 	A 	A 	A 	A DK((3.(9!(<==>@DE(9!(<==>@CD 	Er   c                       fd|j         D             }                     |j        j                  dd                    |          dS )Nc                 :    g | ]}                     |          S r   rC  )r   r   r   s     r   r   z4OctaveCodePrinter._print_Indexed.<locals>.<listcomp>|  s#    777AQ777r   rm  rA  rS  )r   r   r   r   rG  )r   r   indss   `  r   _print_Indexedz OctaveCodePrinter._print_Indexed{  sM    7777777;;ty77774IIr   c                 6    |                      |j                  S r   )r   r   r  s     r   
_print_IdxzOctaveCodePrinter._print_Idx  s    {{4:&&&r   c                 h     t           d         dt           fd|j        D                       z  S )Nr   zdouble(%s == %s)c              3   D   K   | ]}                     |          V  d S r   r   r   s     r   r   z:OctaveCodePrinter._print_KroneckerDelta.<locals>.<genexpr>  sG       *> *>./ +/*;*;At*D*D *> *> *> *> *> *>r   )r   tupler   )r   r   r   s   ` @r   _print_KroneckerDeltaz'OctaveCodePrinter._print_KroneckerDelta  sT    % !E *> *> *> *> *>379*> *> *> %> %> > 	>r   c                 T     d                      fdj        D                       S )Nr   c                 V    g | ]%}                     |t                              &S r   )r   r   )r   rB   r   r   s     r   r   z<OctaveCodePrinter._print_HadamardProduct.<locals>.<listcomp>  sA     0 0 0! ++CD1A1ABB 0 0 0r   )rG  r   r  s   ``r   _print_HadamardProductz(OctaveCodePrinter._print_HadamardProduct  sG    yy 0 0 0 0 0%)Y0 0 0 1 1 	1r   c                     t          |          }d                    |                     |j        |          |                     |j        |          g          S )Nz.**)r   rG  r   r   r4   r  s      r   _print_HadamardPowerz&OctaveCodePrinter._print_HadamardPower  sT    $zzdi..dh--   	r   c                      |j         }t          |          dk    r|d         |d         k    r	|d         g}d                     fd|D                       }d|z   dz   S )Nrb  r   r   rA  c              3   B   K   | ]}                     |          V  d S r   rC  )r   nr   s     r   r   z4OctaveCodePrinter._print_Identity.<locals>.<genexpr>  s-      44dkk!nn444444r   zeye(rS  )r   r   rG  )r   r   r   ss   `   r   _print_Identityz!OctaveCodePrinter._print_Identity  sp    
u::? 	uQx583 	1XJEII4444e44444zCr   c                     d                     |                     |j        d                   |                     |j        d                             S )Nz (gammainc({1}, {0}).*gamma({0}))r   r   r   r   r   r  s     r   _print_lowergammaz#OctaveCodePrinter._print_lowergamma  sF    188KK	!%%t{{49Q<'@'@B B 	Br   c                     d                     |                     |j        d                   |                     |j        d                             S )Nz)(gammainc({1}, {0}, 'upper').*gamma({0}))r   r   r  r  s     r   _print_uppergammaz#OctaveCodePrinter._print_uppergamma  sF    :AAKK	!%%t{{49Q<'@'@B B 	Br   c                 b    d|                      |j        d         t          j        z            z  S )Nzsinc(%s)r   )r   r   r   Pir  s     r   _print_sinczOctaveCodePrinter._print_sinc  s&    DKK	!QT(9::::r   c                 t    d|                      |j                  d|                      |j                  dS )Nbesselh(z, 1, rS  r   r|   argumentr  s     r   _print_hankel1z OctaveCodePrinter._print_hankel1  >     '+{{4:'>'>'>'>'+{{4='A'A'A'AC 	Cr   c                 t    d|                      |j                  d|                      |j                  dS )Nr  z, 2, rS  r  r  s     r   _print_hankel2z OctaveCodePrinter._print_hankel2  r  r   c                     ddl m}m} |j        } |t          j        d|z  z             ||j        t          j        z   |          z  }|                     |          S )Nr   )sqrtr.   rb  )	sympy.functionsr  r.   r  r   r  r|   r  r   )r   r   r  r.   r   expr2s         r   	_print_jnzOctaveCodePrinter._print_jn  f    11111111MQT1Q3Z  af)<a!@!@@{{5!!!r   c                     ddl m}m} |j        } |t          j        d|z  z             ||j        t          j        z   |          z  }|                     |          S )Nr   )r  r0   rb  )	r  r  r0   r  r   r  r|   r  r   )r   r   r  r0   r   r  s         r   	_print_ynzOctaveCodePrinter._print_yn  r  r   c                 H    d|                      |j        d                   z  S )Nzairy(0, %s)r   r   r   r  s     r   _print_airyaizOctaveCodePrinter._print_airyai      t{{49Q<8888r   c                 H    d|                      |j        d                   z  S )Nzairy(1, %s)r   r  r  s     r   _print_airyaiprimez$OctaveCodePrinter._print_airyaiprime  r  r   c                 H    d|                      |j        d                   z  S )Nzairy(2, %s)r   r  r  s     r   _print_airybizOctaveCodePrinter._print_airybi  r  r   c                 H    d|                      |j        d                   z  S )Nzairy(3, %s)r   r  r  s     r   _print_airybiprimez$OctaveCodePrinter._print_airybiprime  r  r   c                 |    |j         \  }}|dk    r|                     |          S d|                     |          z  S )Nr   z
expint(%s))r   _print_not_supportedr   )r   r   mur   s       r   _print_expintzOctaveCodePrinter._print_expint  sB    	A7 	3,,T222dkk!nn,,r   c           	           t          |j                  dk    sJ d                     j        |j        j                 d                     fdt          |j                  D                                 S )Nrb  z{name}({args})rA  c                 :    g | ]}                     |          S r   rC  )r   r   r   s     r   r   z?OctaveCodePrinter._one_or_two_reversed_args.<locals>.<listcomp>  s#    HHHqDKKNNHHHr   )r   r   )r   r   r   r   r   __name__rG  reversedr  s   ` r   _one_or_two_reversed_argsz+OctaveCodePrinter._one_or_two_reversed_args  sw    49~~""""&&%dn&=>HHHHHTY4G4GHHHII ' 
 
 	
r   c                     d                     | j        |j        j                 |                     |j        d                   |                      |j        |j        dd                               S )Nz{name}({arg1}, {arg2})r   r   )r   arg1arg2)r   r   r   r  r   r   funcr  s     r   _nested_binary_math_funcz*OctaveCodePrinter._nested_binary_math_func  sf    '..%dn&=>TYq\**YTY	!""677 /   	r   c                     |j         d         j        dk    rt          d          g } j        d         rv fd|j         d d         D             }d                     |j         d         j                  z  }d                    |          |z   dt          |          z  z   }d	|z   dz   S t          |j                   D ]\  }\  }}|d
k    r,|	                    d                     |          z             nb|t          |j                   dz
  k    r|dk    r|	                    d           n+|	                    d                     |          z                                  |          }	|	                    |	           |t          |j                   dz
  k    r|	                    d           d                    |          S )Nr   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.r   c                     g | ]A\  }}d                                          |                              |                    BS )z({0}).*({1}) + (~({0})).*()r   r   )r   r   r   r   s      r   r   z6OctaveCodePrinter._print_Piecewise.<locals>.<listcomp>  sV     3 3 31a 4::AA8 8 3 3 3r   r  z ...
rS  rm  r   zif (%s)r   elsezelseif (%s)r   
)
r   cond
ValueErrorr.  r   r   rG  r   	enumerater   )
r   r   r   ecpairselastpwr   r   r   code0s
   `         r   _print_Piecewisez"OctaveCodePrinter._print_Piecewise  s   9R=% 	0  / 0 0 0
 >(# 	$3 3 3 3#'9SbS>3 3 3G 4;;ty}'9:::Ew''%/#c'll2BBB 8c>!&ty11 
( 
(	6Aq6 ALLT[[^^!;<<<<#di..1,, Ad ALL((((LLQ!?@@@AU###DI** (LL'''99U###r   c                     t          |j                  dk    r#d|                     |j        d                   z  S |                     |          S )Nr   zzeta(%s)r   )r   r   r   r  r  s     r   _print_zetazOctaveCodePrinter._print_zeta  sJ    ty>>Q 	3DIaL 9 999 ,,T222r   c                   
 t          |t                    r=|                     |                    d                    }d                    |          S d}dd
d |D             }fd|D             }
fd|D             }g }d	}t          |          D ]Q\  }}	|	d
v r|                    |	           |||         z  }|                    ||z  |	           |||         z  }R|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                 8    g | ]}|                     d           S )z 	)lstrip)r   lines     r   r   z1OctaveCodePrinter.indent_code.<locals>.<listcomp>*  s$    666U##666r   c                 b    g | ]*t          t          fd D                                 +S )c              3   8   K   | ]}t          |          V  d S r   r   r   rl   r  s     r   r   z;OctaveCodePrinter.indent_code.<locals>.<listcomp>.<genexpr>,  -      BB"VB--BBBBBBr   intany)r   r  	inc_regexs    @r   r   z1OctaveCodePrinter.indent_code.<locals>.<listcomp>,  O     ( ( ( BBBB	BBBBBCC ( ( (r   c                 b    g | ]*t          t          fd D                                 +S )c              3   8   K   | ]}t          |          V  d S r   r   r  s     r   r   z;OctaveCodePrinter.indent_code.<locals>.<listcomp>.<genexpr>.  r  r   r  )r   r  	dec_regexs    @r   r   z1OctaveCodePrinter.indent_code.<locals>.<listcomp>.  r  r   r   )r   r  )r   rP  r   
splitlinesrG  r  r   )r   code
code_linestabincreasedecreaseprettylevelr  r  r  r  s             @@r   r   zOctaveCodePrinter.indent_code  sF    dC   	'))$//$*?*?@@J77:&&&I	3	 76666( ( ( (!%( ( (( ( ( (!%( ( (   	! 	!GAtz! d###Xa[ EMMCIItt4555Xa[ EEr   )Dr  
__module____qualname____doc__printmethodlanguage
_operators_default_settingsr   r   r   r   r   r   r   r   r  r	  r  r  r  r  r"  r$  r&  r4  r7  r:  r>  rH  _print_tuple_print_Tuple_print_ListrK  rN  rQ  rY  ri  ro  r{  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  _print_DiracDelta_print_LambertWr  
_print_Max
_print_Minr  r  r   __classcell__)r   s   @r   rq   rq   ?   s         KH  J #(	 	 !# / / / / / /  " " "# # #. . .' ' 'B B B	' 	' 	'H: H: H:T9 9 9> > >(= = =
C C C
        I I I:      C C CLLK    ! ! !
: 
: 
:N N N4 4 4
E E E*J J J
' ' '> > >
1 1 1       B B BB B B
; ; ;
C C C
C C C" " "" " "9 9 99 9 99 9 99 9 9- - -
 
 
 +DC   76J"$ "$ "$J3 3 3      r   rq   Nc                 H    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));'
    )rq   doprint)r   	assign_tor   s      r   octave_coder  =  s#    P X&&..tY???r   c                 :    t          t          | fi |           dS )zPrints the Octave (or Matlab) representation of the given expression.

    See `octave_code` for the meaning of the optional arguments.
    N)printr  )r   r   s     r   print_octave_coder    s(    
 
+d
'
'h
'
'(((((r   r   )r  typingr   r   tDict
sympy.corer   r   r   r   sympy.core.mulr	   sympy.printing.codeprinterr
   sympy.printing.precedencer   r   rl   r   r   r   rq   r  r  r   r   r   <module>r     s
  
 
 & % % % % % % % , , , , , , , , , , , , & & & & & & 2 2 2 2 2 2 < < < < < < < <      : : :	5	7 
 v	
 , , 
9 	(  '  	&  
 	(  	!" 
5#$ 
#
1  8{ { { { { { { {|H@ H@ H@ H@V) ) ) ) )r   