
    gc              	      *   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 d5 d6e          Zd:d8Zd9 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            	          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	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/d/ Z0d0 Z1d1 Z2d2 Z3d3 Z4d4 Z5d5 Z6d6 Z7d7 Z8d8 Z9d9 Z:d: Z;d; Z<d< Z=e=xZ>Z?d= Z@e@xZAZBd> ZCd? ZDd@ ZE xZFS )A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                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      Q/var/www/html/ai-engine/env/lib/python3.11/site-packages/sympy/printing/octave.pyr   zOctaveCodePrinter.__init__Y   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_positiona   s    s
r   c                    d|z  S )Nz%s;r   )r   
codestrings     r   _get_statementz OctaveCodePrinter._get_statemente   s    z!!r   c                ,    d                     |          S )Nz% {}format)r   texts     r   _get_commentzOctaveCodePrinter._get_commenti   s    }}T"""r   c                .    d                     ||          S )Nz{} = {};r   )r   namevalues      r   _declare_number_constz'OctaveCodePrinter._declare_number_constm   s      u---r   c                ,    |                      |          S N)indent_code)r   liness     r   _format_codezOctaveCodePrinter._format_codeq   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>x   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_indicesu   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	   orderas_ordered_factorsr   	make_argsis_commutativeis_Powr5   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q55r1%%DDDD	:_,,**,,DD =&&D  	 	D#  8L ,8r>>HHSTXIFFFGGGG49Q<,--22z$)S7Q7Q2!((...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~~,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                l   t          d |j        D                       rdnd}t          |          }t          |j        d          rd|                     |j                  z  S |j        rt          |j        d          r3|j        j        rdnd}d	|z   d|                     |j                  z  z   S t          |j        d
          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.^g      ?zsqrt(%s)g      r   r   1r   %s)
r   r   r   r
   r5   r   r   r   r   r   )r   r   	powsymbolPRECsyms        r   
_print_PowzOctaveCodePrinter._print_Pow   sE   >>DI>>>>>HCCD	$#&& 	7DI 6 666 	MDHd++ G!Y0:ccdSy:DI0F0F#FFFDHb)) 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   r5   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,  r5  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>1  s-      <<!t{{1~~<<<<<<r   }joinr  s   ` r   _print_listzOctaveCodePrinter._print_list0  s4    TYY<<<<t<<<<<<sBBr   c                    dS )Ntruer   r  s     r   _print_BooleanTruez$OctaveCodePrinter._print_BooleanTrue7  r8  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_bool?  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(r>  ))r   r   z[%s]z; c              3  n   K   | ]/}d                      fd|ddf         D                       V  0dS ) c                :    g | ]}                     |          S r   r@  rA  s     r   r   zAOctaveCodePrinter._print_MatrixBase.<locals>.<genexpr>.<listcomp>P  s#    +L+L+LqDKKNN+L+L+Lr   NrC  )r   r   Ar   s     r   r   z6OctaveCodePrinter._print_MatrixBase.<locals>.<genexpr>P  sd       ": ":&' #&((+L+L+L+LAadG+L+L+L"M"M ": ": ": ": ": ":r   )r   r   r   Zeror   r   rD  r   )r   rT  s   ``r   _print_MatrixBasez#OctaveCodePrinter._print_MatrixBaseG  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>X       )))!QqTAX)))r   c                $    g | ]}|d          d z   S )r   r   rZ  s     r   r   z<OctaveCodePrinter._print_SparseRepMatrix.<locals>.<listcomp>Y  r\  r   c                    g | ]
}|d          S )   r   rZ  s     r   r   z<OctaveCodePrinter._print_SparseRepMatrix.<locals>.<listcomp>Z  s    '''qt'''r   zsparse(r>  rP  )sympy.matricesrX  col_listr   r   r   )r   rT  rX  LIJAIJs          r   _print_SparseRepMatrixz(OctaveCodePrinter._print_SparseRepMatrixT  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   r>  rP  )r   parentr   r   r   r  s     r   _print_MatrixElementz&OctaveCodePrinter._print_MatrixElement_  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   r_  r   r   )r   rD  )r   limlhsteplstrhstrr   s          r   strslicez6OctaveCodePrinter._print_MatrixSlice.<locals>.strslicee  s    !qA!AQ4D;;q>>DHH55$++a..Dqyy66a3hh366K#:,,xxt{{4'8'8$ ?@@@r   rj  r   r>  r   rP  )r   rk  rowslicer   colslice)r   r   ru  s   `  r   _print_MatrixSlicez$OctaveCodePrinter._print_MatrixSliced  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   r@  )r   r   r   s     r   r   z4OctaveCodePrinter._print_Indexed.<locals>.<listcomp>z  s#    777AQ777r   rj  r>  rP  )r   r   r   r   rD  )r   r   indss   `  r   _print_Indexedz OctaveCodePrinter._print_Indexedy  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   rC   r   r   s     r   r   z<OctaveCodePrinter._print_HadamardProduct.<locals>.<listcomp>  sA     0 0 0! ++CD1A1ABB 0 0 0r   )rD  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   rD  r   r   r5   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 )Nr_  r   r   r>  c              3  B   K   | ]}                     |          V  d S r   r@  )r   nr   s     r   r   z4OctaveCodePrinter._print_Identity.<locals>.<genexpr>  s-      44dkk!nn444444r   zeye(rP  )r   r   rD  )r   r   r   ss   `   r   _print_Identityz!OctaveCodePrinter._print_Identity  sk    
u::??uQx583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, rP  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, rP  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/   r_  )	sympy.functionsr  r/   r  r   r  r   Halfr   )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  r1   r_  )	r  r  r1   r  r   r  r   r  r   )r   r   r  r1   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  s?    	A77,,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 )Nr_  z{name}({args})r>  c                :    g | ]}                     |          S r   r@  )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__rD  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 ...
rP  rj  r   zif (%s)r   elsezelseif (%s)r   
)
r   cond
ValueErrorr+  r   r   rD  r   	enumerater   )
r   r   r   ecpairselastpwr   r   r   code0s
   `         r   _print_Piecewisez"OctaveCodePrinter._print_Piecewise  s   9R=%%  / 0 0 0
 >(# 	$3 3 3 3#'9SbS>3 3 3G 4;;ty}'9:::Ew''%/#c'll2BBB 8c>!&ty11 
( 
(	6Aq66LLT[[^^!;<<<<#di..1,,,d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  sH    ty>>Q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   rm   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   rM  r   
splitlinesrD  r  r   )r   code
code_linestabincreasedecreaseprettylevelr  r  r  r  s             @@r   r   zOctaveCodePrinter.indent_code  sD    dC   	'))$//$*?*?@@J77:&&&I	3	 76666( ( ( (!%( ( (( ( ( (!%( ( (   	! 	!GAtz!!d###Xa[ EMMCIItt4555Xa[ EEr   )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#  r1  r4  r7  r;  rE  _print_tuple_print_Tuple_print_ListrH  rK  rN  rV  rf  rl  rx  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   rr   rr   A   s          KH  J )-[-J ) )	O O ) )     !# / / / / / /  " " "# # #. . .' ' '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   rr   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));'
    )rr   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  
__future__r   typingr   
sympy.corer   r   r   r   sympy.core.mulr	   sympy.core.numbersr
   sympy.printing.codeprinterr   sympy.printing.precedencer   r   rm   r   r   r   rr   r  r  r   r   r   <module>r     s(  
 
 # " " " " "       , , , , , , , , , , , , & & & & & & + + + + + + 2 2 2 2 2 2 < < < < < < < <      : : :	5	7 
 v	
 , , 
9 	(  '  	&  
 	(  	!" 
5#$ 
#
1  8w w w w w w w wtH@ H@ H@ H@V) ) ) ) )r   