+
    i}c                       R t ^ RIHt ^ RIHt ^ RIHtHtHtH	t	 ^ RI
Ht ^ RIHt ^ RIHt ^ RIHtHt ^ RIHt . R	NR
NRNRNRNRNRNRNRNRNRNRNRNRNRNRNRNRNRNRNRNRNRNR NR!NR"NR#NR$NR%NR&NR'NR(NR)NR*NR+NR,NR-NR.NR/NR0NR1NR2NR3NR4NR5NR6NR7NR8NR9Nt/ R:R;bR<R=bR>R?bR@RAbRBRCbRDREbRFRGbRHRIbRJRKbRLRMbRNRObRPRQbRRRSbRTRUbRVRWbRXRYbRZR[bR\R]R^R_R`RaRbRcRdReRfRgRhRi/Ct ! Rj Rk]4      tRoRm ltRn tRl# )pai  
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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minModmod	polygammapsirerealRisingFactorial
pochhammerShisinhintSisinintc                    a  ] tR t^At$ RtRtRtRRRRRR	/t]! ]	P                  3/ R
^R/ RRRR/B t
R]R&   / 3V 3R lltR tR tR tR tR tR tR tR tR tR tR tR tR tR tR  tR! tR" tR# tR$ tR% t R& t!]!t"]!t#]!t$R' t%R( t&R) t'R* t(R+ t)R, t*R- t+R. t,R/ t-R0 t.R1 t/R2 t0R3 t1R4 t2R5 t3R6 t4R7 t5R8 t6R9 t7R: t8R; t9R< t:R= t;R> t<R? t=]=;t>t?R@ t@]@;tAtBRA tCRB tDRC tERDtFV ;tG# )EOctaveCodePrinterzD
A printer to convert expressions to strings of Octave/Matlab code.
_octaveOctaveand&or|not~	precisionuser_functionscontractTinlinezdict[str, Any]_default_settingsc                	  < \         SV `  V4       \        \        \        \        4      4      V n        V P
                  P                  \        \        4      4       VP                  R / 4      pV P
                  P                  V4       R# )r{   N)	super__init__dictzipknown_fcns_src1known_functionsupdateknown_fcns_src2get)selfsettings	userfuncs	__class__s   && u/Users/tonyclaw/.openclaw/workspace/skills/math-calculator/venv/lib/python3.14/site-packages/sympy/printing/octave.pyr   OctaveCodePrinter.__init__Y   sb    "#C$IJ##D$9:LL!126	##I.    c                	    V^,          # )    )r   ps   &&r   _rate_index_position&OctaveCodePrinter._rate_index_positiona   s    s
r   c                	    R V,          # )z%s;r   )r   
codestrings   &&r   _get_statement OctaveCodePrinter._get_statemente   s    z!!r   c                	$    R P                  V4      # )z% {}format)r   texts   &&r   _get_commentOctaveCodePrinter._get_commenti   s    }}T""r   c                	$    R P                  W4      # )z{} = {};r   )r   namevalues   &&&r   _declare_number_const'OctaveCodePrinter._declare_number_constm   s      --r   c                	$    V P                  V4      # N)indent_code)r   liness   &&r   _format_codeOctaveCodePrinter._format_codeq   s    &&r   c                	L   a VP                   w  opV3R  l\        V4       4       # )c              3  N   <"   T F  p\        S4       F  q"V3x  K
  	  K  	  R # 5ir   )range).0jirowss   &  r   	<genexpr>=OctaveCodePrinter._traverse_matrix_indices.<locals>.<genexpr>x   s     A1U4[A[s   "%)shaper   )r   matcolsr   s   && @r   _traverse_matrix_indices*OctaveCodePrinter._traverse_matrix_indicesu   s    YY
dAdAAr   c           	     	   . p. pV Fy  p\        V P                  VP                  VP                  ^,           VP                  ^,           .4      w  rVpVP                  RV: RV: RV: 24       VP                  R4       K{  	  W#3# )   zfor  = :end)map_printlabellowerupperappend)r   indices
open_linesclose_linesr   varstartstops   &&      r   _get_loop_opening_ending*OctaveCodePrinter._get_loop_opening_ending{   sw    
A"4;;WWaggk177Q;7 9C#udCDu%  &&r   c           	     	   VP                   '       dh   VP                  '       dV   \        P                  V,          P                  '       d/   R V P                  \        P                  ) V,          4      ,          # \        V4      pVP                  4       w  r4V^ 8  d   \        V) V4      pRpMRp. p. p. pV P                  R9  d   VP                  4       p	M\        P                  ! V4      p	V	 EF  p
V
P                  '       Ed    V
P                  '       Ed   V
P                  P                   '       d   V
P                  P"                  '       d   V
P                  R8w  d5   VP%                  \'        V
P(                  V
P                  ) RR7      4       K  \+        V
P,                  ^ ,          P,                  4      ^8w  d2   \/        V
P(                  \        4      '       d   VP%                  V
4       VP%                  \'        V
P(                  V
P                  ) 4      4       EK6  V
P                   '       d   V
\        P0                  Jdq   V
P2                  ^8w  d%   VP%                  \5        V
P2                  4      4       V
P6                  ^8w  d(   VP%                  \5        V
P6                  4      4       EK  EK  VP%                  V
4       EK  	  T;'       g    \        P8                  .pV Uu. uF  qP;                  W4      NK  	  ppV Uu. uF  qP;                  W4      NK  	  ppV FY  p
V
P(                  V9   g   K  RWP=                  V
P(                  4      ,          ,          WP=                  V
P(                  4      &   K[  	  R pV'       g   W^! Wl4      ,           # \+        V4      ^8X  dA   V^ ,          P                   '       d   RMRpW^! Wl4      ,           V,           V^ ,          ,           # \>        ;QJ d    R	 V 4       F  '       d   K   RM	  R
M! R	 V 4       4      '       d   RMRpW^! Wl4      ,           V,           RV! W}4      ,          ,           # u upi u upi )z%si- F)evaluatez(%s)c                    V^ ,          p\        ^\        V 4      4       F;  pW^,
          ,          P                  '       d   RMRpW$,           W,          ,           pK=  	  V# )    *.*)r   len	is_number)aa_strrr   mulsyms   &&   r   multjoin.OctaveCodePrinter._print_Mul.<locals>.multjoin   sM    aA1c!f% !A# 0 0 0dJ) & Hr   /./c              3  8   "   T F  qP                   x  K  	  R # 5ir   r   )r   bis   & r   r   /OctaveCodePrinter._print_Mul.<locals>.<genexpr>   s     9qq   T)oldnone) 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parenthesizeindexall)r   exprpreccer=   r   b	pow_parenr   itemxr   b_strr   divsyms   &&              r   
_print_MulOctaveCodePrinter._print_Mul   s&   NNNt000%1114;;'7'<=== $  "q5r1%DDD	::_,**,D ==&D D###8L8L8L,,,88r>HHSTXXIFG499Q<,,-2z$))S7Q7Q!((.HHSTXXI67!!!d!**&<66Q;HHXdff-.66Q;HHXdff-.   " LL!%%567Q""1+Q7567Q""1+Q7 DyyA~,2U77499;M5N,Nggdii() 
	 (1,,,Vq[aDNNNSF(1,,v5a@@C9q9CCC9q999StF8A--#hq&889 :1 87s   1QQc                	    V P                  VP                  4      pV P                  VP                  4      pVP                  pR P	                  W$V4      # )z{} {} {})r   lhsrhsrel_opr   )r   r   lhs_coderhs_codeops   &&   r   _print_Relational#OctaveCodePrinter._print_Relational   sB    ;;txx(;;txx([[  x88r   c                	   \         ;QJ d&    R  VP                   4       F  '       d   K   RM	  RM! R  VP                   4       4      '       d   RMRp\        V4      p\        VP                  R4      '       d#   RV P                  VP                  4      ,          # VP                  '       d   \        VP                  R4      '       dQ   VP                  P                  '       d   RMRpR	V,           RV P                  VP                  4      ,          ,           # \        VP                  R4      '       dR   VP                  P                  '       d   RMRpR	V,           R
V P                  VP                  V4      ,          ,           # V P                  VP                  V4      : V: V P                  VP                  V4      : 2# )c              3  8   "   T F  qP                   x  K  	  R # 5ir   r   )r   r  s   & r   r   /OctaveCodePrinter._print_Pow.<locals>.<genexpr>   s     >Iq{{Ir   FT^z.^g      ?zsqrt(%s)r   r   1%sg      r   )
r   r   r   r	   r4   r   r   r   r   r   )r   r   	powsymbolPRECsyms   &&   r   
_print_PowOctaveCodePrinter._print_Pow   s8   3>DII>333>DII>>>CD	$#&&DII 666DHHd++!YY000cdSy:DII0F#FFFDHHb))!YY000cdSy4$*;*;DIIt*L#LLL,,TYY=y,,TXXt<> 	>r   c                	    \        V4      pV P                  VP                  V4      : R V P                  VP                  V4      : 2# )r  )r   r   r   r4   r   r   r  s   && r   _print_MatPowOctaveCodePrinter._print_MatPow   s>    $++DIIt<++DHHd;= 	=r   c                	    \        V4      pV P                  VP                  V4      : R V P                  VP                  V4      : 2# )z \ )r   r   matrixvectorr  s   && r   _print_MatrixSolve$OctaveCodePrinter._print_MatrixSolve   s@    $!..t{{DA!..t{{DAC 	Cr   c                	    R # )pir   r   r   s   &&r   	_print_PiOctaveCodePrinter._print_Pi       r   c                	    R # )1ir   r%  s   &&r   _print_ImaginaryUnit&OctaveCodePrinter._print_ImaginaryUnit   r(  r   c                	    R # )zexp(1)r   r%  s   &&r   _print_Exp1OctaveCodePrinter._print_Exp1   s    r   c                	    R # )z(1+sqrt(5))/2r   r%  s   &&r   _print_GoldenRatio$OctaveCodePrinter._print_GoldenRatio  s     r   c                	   ^ RI Hp ^ RIHp ^ RIHp VP                  pVP                  pV P                  R,          '       g~   \        VP                  V4      '       db   . p. pVP                   F-  w  rVP                  V! WY4      4       VP                  V
4       K/  	  V! \        Wx4      !  pV P                  V4      # V P                  R,          '       d@   VP                  V4      '       g   VP                  V4      '       d   V P                  We4      # V P                  V4      pV P                  V4      pV P!                  V: RV: 24      # )r   )
Assignment)	Piecewise)IndexedBaser}   r|   r   )sympy.codegen.astr4  $sympy.functions.elementary.piecewiser5  sympy.tensor.indexedr6  r  r  	_settingsr   r   r   r   r   has_doprint_loopsr   )r   r   r4  r5  r6  r  r  expressions
conditionsr   r   tempr
  r  s   &&            r   _print_Assignment#OctaveCodePrinter._print_Assignment  s
   0B4hhhh~~h''Jtxx,K,K KJ(("":c#56!!!$ # c+:;D;;t$$>>*%%377;+?+?$$ &&s00{{3'H{{3'H&&Hh'GHHr   c                	    R # )infr   r%  s   &&r   _print_Infinity!OctaveCodePrinter._print_Infinity$      r   c                	    R # )z-infr   r%  s   &&r   _print_NegativeInfinity)OctaveCodePrinter._print_NegativeInfinity(      r   c                	    R # )NaNr   r%  s   &&r   
_print_NaNOctaveCodePrinter._print_NaN,  rF  r   c                	V   a  R RP                  V 3R lV 4       4      ,           R,           # ){, c              3  F   <"   T F  pSP                  V4      x  K  	  R # 5ir   r   )r   r   r   s   & r   r   0OctaveCodePrinter._print_list.<locals>.<genexpr>1  s     <t!t{{1~~t   !})joinr%  s   f&r   _print_listOctaveCodePrinter._print_list0  s"    TYY<t<<<sBBr   c                	    R # )truer   r%  s   &&r   _print_BooleanTrue$OctaveCodePrinter._print_BooleanTrue7  rJ  r   c                	    R # )falser   r%  s   &&r   _print_BooleanFalse%OctaveCodePrinter._print_BooleanFalse;  s    r   c                	4    \        V4      P                  4       # r   )strr   r%  s   &&r   _print_boolOctaveCodePrinter._print_bool?  s    4y  r   c                	  a a SP                   SP                  3R8X  d   R# \        P                  SP                  9   d    RSP                   : RSP                  : R2# SP                   SP                  3R	8X  d   S P                  SR,          4      # RRP                  VV 3R l\        SP                   4       4       4      ,          # )
r   z[]zzeros(rQ  )z[%s]z; c           	   3     <"   T F>  pR P                  SVR3,           Uu. uF  pSP                  V4      NK  	  up4      x  K@  	  R# u upi 5i) :NNNN)rW  r   )r   r   r   Ar   s   &  r   r   6OctaveCodePrinter._print_MatrixBase.<locals>.<genexpr>P  sI      ":+8a #&((AadG+LGqDKKNG+L"M"M+8 ,Ms   AA	
A)r   r   )r   r   )r   r   r   Zeror   r   rW  r   )r   rj  s   ffr   _print_MatrixBase#OctaveCodePrinter._print_MatrixBaseG  s    FFAFFv%VVqww&'ffaff55ffaff';;qw''		 ":+0=": : : 	:r   c                	   ^ RI Hp VP                  4       pT! V Uu. uF  qD^ ,          ^,           NK  	  up.4      pT! V Uu. uF  qD^,          ^,           NK  	  up.4      pT! V Uu. uF  qD^,          NK  	  up.4      pRV P                  V4      : RV P                  V4      : RV P                  V4      : RVP                  : RVP
                  : R2# u upi u upi u upi )r   )Matrixzsparse(rQ  rg  )sympy.matricesrp  col_listr   r   r   )r   rj  rp  LkIJAIJs   &&      r   _print_SparseRepMatrix(OctaveCodePrinter._print_SparseRepMatrixT  s    )JJLq)q!qTAXXq)*+q)q!qTAXXq)*+Q'QttQ'()/3{{1~t{{1~,0KK,<affaffN 	N *)'s   CC)C$c                	    V P                  VP                  \        R ,          RR7      RVP                  ^,           : RVP                  ^,           : R2,           # )AtomT)strict(rQ  rg  )r   parentr   r   r   r%  s   &&r   _print_MatrixElement&OctaveCodePrinter._print_MatrixElement_  sF      j.@ N FFQJJ

34 	4r   c                	<  a  V 3R  lpS P                  VP                  4      R,           V! VP                  VP                  P                  ^ ,          4      ,           R,           V! VP                  VP                  P                  ^,          4      ,           R,           # )c                >  < V ^ ,          ^,           pV ^,          pV ^,          pSP                  V4      pW18X  d   RMSP                  V4      pV^8X  d(   V^8X  d	   W18X  d   R# W#8X  d   V# VR,           V,           # RP                  VSP                  V4      V34      # )r   r   r   )r   rW  )r  limlhsteplstrhstrr   s   &&     r   strslice6OctaveCodePrinter._print_MatrixSlice.<locals>.strslicee  s    !qA!AQ4D;;q>DH5$++a.Dqy6ah6K#:,,xxt{{4'8$ ?@@r   r}  rQ  rg  )r   r~  rowslicer   colslice)r   r   r  s   f& r   _print_MatrixSlice$OctaveCodePrinter._print_MatrixSliced  s|    	A DKK(3.(9(9!(<=>@DE(9(9!(<=>@CD 	Er   c                	    VP                    Uu. uF  q P                  V4      NK  	  ppV P                  VP                  P                  4      : R RP	                  V4      : R2# u upi )r}  rQ  rg  )r   r   r   r   rW  )r   r   r   indss   &&  r   _print_Indexed OctaveCodePrinter._print_Indexedy  sI    )-7AQ7;;tyy74II 8s   A&c                	   a a \         R ,          oR\        ;QJ d(    . VV 3R lVP                   4       F  NK  	  5,          # ! VV 3R lVP                   4       4      ,          # )r   zdouble(%s == %s)c              3  H   <"   T F  pSP                  VS4      x  K  	  R # 5ir   )r   )r   r  r   r   s   & r   r   :OctaveCodePrinter._print_KroneckerDelta.<locals>.<genexpr>  s(      *>3<a +/*;*;At*D*D3<s   ")r   tupler   )r   r   r   s   f&@r   _print_KroneckerDelta'OctaveCodePrinter._print_KroneckerDelta~  sS    % !EE *>3799*>E > 	>E *>3799*> %> > 	>r   c                	    R P                  VP                   Uu. uF  pV P                  V\        V4      4      NK   	  up4      # u upi )r   )rW  r   r   r   )r   r   rB   s   && r   _print_HadamardProduct(OctaveCodePrinter._print_HadamardProduct  sI    yy%)YY0%.c ++CD1AB%.0 1 	1 0s   $Ac                	    \        V4      pR P                  V P                  VP                  V4      V P                  VP                  V4      .4      # )z.**)r   rW  r   r   r4   r  s   && r   _print_HadamardPower&OctaveCodePrinter._print_HadamardPower  sJ    $zzdii.dhh-  	r   c                	   a  VP                   p\        V4      ^8X  d    V^ ,          V^,          8X  d   V^ ,          .pRP                  V 3R lV 4       4      pRV,           R,           # )   rQ  c              3  F   <"   T F  pSP                  V4      x  K  	  R # 5ir   rS  )r   nr   s   & r   r   4OctaveCodePrinter._print_Identity.<locals>.<genexpr>  s     4edkk!nnerU  zeye(rg  )r   r   rW  )r   r   r   ss   f&  r   _print_Identity!OctaveCodePrinter._print_Identity  sT    

u:?uQx5831XJEII4e44zCr   c                	    R P                  V P                  VP                  ^ ,          4      V P                  VP                  ^,          4      4      # )z (gammainc({1}, {0}).*gamma({0}))r   r   r   r%  s   &&r   _print_lowergamma#OctaveCodePrinter._print_lowergamma  s>    188KK		!%t{{499Q<'@B 	Br   c                	    R P                  V P                  VP                  ^ ,          4      V P                  VP                  ^,          4      4      # )z)(gammainc({1}, {0}, 'upper').*gamma({0}))r  r%  s   &&r   _print_uppergamma#OctaveCodePrinter._print_uppergamma  s>    :AAKK		!%t{{499Q<'@B 	Br   c                	~    R V P                  VP                  ^ ,          \        P                  ,          4      ,          # )zsinc(%s))r   r   r   Pir%  s   &&r   _print_sincOctaveCodePrinter._print_sinc  s'    DKK		!QTT(9:::r   c                	|    R V P                  VP                  4      : RV P                  VP                  4      : R2# )besselh(z, 1, rg  r   r   argumentr%  s   &&r   _print_hankel1 OctaveCodePrinter._print_hankel1  .    '+{{4::'>'+{{4=='AC 	Cr   c                	|    R V P                  VP                  4      : RV P                  VP                  4      : R2# )r  z, 2, rg  r  r%  s   &&r   _print_hankel2 OctaveCodePrinter._print_hankel2  r  r   c                	    ^ RI HpHp VP                  pV! \        P
                  ^V,          ,          4      V! VP                  \        P                  ,           V4      ,          pV P                  V4      # )r   )sqrtr.   )	sympy.functionsr  r.   r  r   r  r   Halfr   )r   r   r  r.   r  expr2s   &&    r   	_print_jnOctaveCodePrinter._print_jn  L    1MMQTT1Q3Z aff)<a!@@{{5!!r   c                	    ^ RI HpHp VP                  pV! \        P
                  ^V,          ,          4      V! VP                  \        P                  ,           V4      ,          pV P                  V4      # )r   )r  r0   )	r  r  r0   r  r   r  r   r  r   )r   r   r  r0   r  r  s   &&    r   	_print_ynOctaveCodePrinter._print_yn  r  r   c                	T    R V P                  VP                  ^ ,          4      ,          # )zairy(0, %s)r   r   r%  s   &&r   _print_airyaiOctaveCodePrinter._print_airyai      t{{499Q<888r   c                	T    R V P                  VP                  ^ ,          4      ,          # )zairy(1, %s)r  r%  s   &&r   _print_airyaiprime$OctaveCodePrinter._print_airyaiprime  r  r   c                	T    R V P                  VP                  ^ ,          4      ,          # )zairy(2, %s)r  r%  s   &&r   _print_airybiOctaveCodePrinter._print_airybi  r  r   c                	T    R V P                  VP                  ^ ,          4      ,          # )zairy(3, %s)r  r%  s   &&r   _print_airybiprime$OctaveCodePrinter._print_airybiprime  r  r   c                	~    VP                   w  r#V^8w  d   V P                  V4      # RV P                  V4      ,          # )r   z
expint(%s))r   _print_not_supportedr   )r   r   mur  s   &&  r   _print_expintOctaveCodePrinter._print_expint  s8    		7,,T22dkk!n,,r   c                	4   \        VP                  4      ^8:  g   Q hRP                  V P                  VP                  P
                  ,          RP                  \        VP                  4       Uu. uF  q P                  V4      NK  	  up4      R7      # u upi )r  z{name}({args})rQ  )r   r   )	r   r   r   r   r   __name__rW  reversedr   )r   r   r  s   && r   _one_or_two_reversed_args+OctaveCodePrinter._one_or_two_reversed_args  sw    499~"""&&%%dnn&=&=>HTYY4GH4GqKKN4GHI ' 
 	
Hs   0Bc           
     	   R P                  V P                  VP                  P                  ,          V P	                  VP
                  ^ ,          4      V P	                  VP                  ! VP
                  R,          !  4      R7      # )z{name}({arg1}, {arg2}):r   NN)r   arg1arg2)r   r   r   r  r   r   funcr%  s   &&r   _nested_binary_math_func*OctaveCodePrinter._nested_binary_math_func  sc    '..%%dnn&=&=>TYYq\*TYY		"67 /  	r   c           
     	R   VP                   R,          P                  R8w  d   \        R4      h. pV P                  R,          '       d   VP                   RR  UUu. uF5  w  r4RP	                  V P                  V4      V P                  V4      4      NK7  	  pppRV P                  VP                   R,          P                  4      ,          pRP                  V4      V,           R\        V4      ,          ,           pR	V,           R,           # \        VP                   4       F  w  pw  r4V^ 8X  d)   VP                  R
V P                  V4      ,          4       MaV\        VP                   4      ^,
          8X  d   VR8X  d   VP                  R4       M'VP                  RV P                  V4      ,          4       V P                  V4      p	VP                  V	4       V\        VP                   4      ^,
          8X  g   K  VP                  R4       K  	  RP                  V4      # u uppi )r   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}   Nz({0}).*({1}) + (~({0})).*(r  z ...
rg  r}  zif (%s)elsezelseif (%s)r   
r   )r   cond
ValueErrorr:  r   r   r   rW  r   	enumerater   )
r   r   r   r   r   ecpairselastpwr   code0s
   &&        r   _print_Piecewise"OctaveCodePrinter._print_Piecewise  s   99R=%  / 0 0
 >>(## $(99Sb>3#141 4::AA8#1  3 4;;tyy}'9'9::Ew'%/#c'l2BBB 8c>!&tyy1	6A6LLT[[^!;<#dii.1,,dLL(LLQ!?@AU#DII**LL' 2 99U##)3s   ;H#c                	    \        VP                  4      ^8X  d*   RV P                  VP                  ^ ,          4      ,          # V P                  V4      # )r   zzeta(%s))r   r   r   r  r%  s   &&r   _print_zetaOctaveCodePrinter._print_zeta  sA    tyy>QDIIaL 999 ,,T22r   c           	     &  a \        V\        4      '       d2   V P                  VP                  R4      4      pRP	                  V4      # RpRpR	pV Uu. uF  qfP                  R4      NK  	  ppV Uau. uFF  o\        \        ;QJ d    V3R lV 4       F  '       g   K   RM	  RM! V3R lV 4       4      4      NKH  	  ppV Uau. uFF  o\        \        ;QJ d    V3R lV 4       F  '       g   K   RM	  RM! V3R lV 4       4      4      NKH  	  pp. p	^ p
\        V4       F[  w  poSR
9   d   V	P                  S4       K   WV,          ,          p
V	P                  W:,          : S: 24       WV,          ,          p
K]  	  V	# u upi u upi u upi )z0Accepts a string of code or a list of code linesTr   z  z 	c              3  <   <"   T F  p\        VS4      x  K  	  R # 5ir   r   r   rh   lines   & r   r   0OctaveCodePrinter.indent_code.<locals>.<genexpr>&       B	"VB--	   Fc              3  <   <"   T F  p\        VS4      x  K  	  R # 5ir   r   r  s   & r   r   r  (  r  r  )z
^function z^if ^elseif ^else$z^for )z^end$r  r  )r   r  )
r   rc  r   
splitlinesrW  lstripintanyr  r   )r   code
code_linestab	inc_regex	dec_regexr  increasedecreaseprettylevelr  s   &&    `     r   r   OctaveCodePrinter.indent_code  s`    dC  ))$//$*?@J77:&&I	3	 156U#6 "&(!% B	BB	BBC!% 	 ( "&(!% B	BB	BBC!% 	 (  GAtz!d#a[ EMMCIt45a[ E ' ! 7((s   F3)F	 F	)F2 F)r   )Hr  
__module____qualname____firstlineno____doc__printmethodlanguage
_operatorsr   r
   r~   __annotations__r   r   r   r   r   r   r   r   r  r  r  r  r!  r&  r+  r.  r1  r@  rD  rH  rM  rX  _print_tuple_print_Tuple_print_Listr\  r`  rd  rm  rx  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   __static_attributes____classcell__)r   s   @r   rq   rq   A   s    KH 	scsJ )-[-J-J )R"D$	O )~  !# /"#.'B	'H:T9>(=
C
I:CLLK!
:N4
E*J
>
1 BB
;
C
C""9999-
 +DC 76J"$J3 r   rq   Nc                6    \        V4      P                  W4      # )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   7  s    P X&..t??r   c                0    \        \        V 3/ VB 4       R# )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   rh   r   r   r   rq   r   r#  r   r   r   <module>r+     s  
 #  , , & + 2 < :5 :% : : :u :e ::!:#):+1:3::<B:DJ::!:#):+1:39:;A: : $: &-: /6: 8?: AH: 	: "	: $)	: +3	: 5>	:
 :
 (:
 *3:
 5>: : !': )0: 27: 9D: FM: :  *: ,3: 5?: AF: : %: '-: /9:	5	7 
 v	
 , , 
9 	(  '  	&  
 	(  	!" 
5#$ 
5	5&|	9(18s slH@V)r   