
    \h}c              	      $   S r SSKJr  SSKJr  SSKJrJrJrJ	r	  SSK
Jr  SSKJr  SSKJr  SSKJrJr  SS	KJr  / S
Qr0 SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS _S!S"_S#S$_S%S&_S'S(_S)S*_S+S,_S-S.S/S0S1S2S3S4.Er " S5 S6\5      rS:S8 jrS9 rg7);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            	        ^  \ rS rSr% SrSrSrSSSS.r\" \	R                  40 S	0 S
S
S.D6r
S\S'   0 4U 4S jjrS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS  rS! rS" r S# r!\!r"\!r#\!r$S$ r%S% r&S& r'S' r(S( r)S) r*S* r+S+ r,S, r-S- r.S. r/S/ r0S0 r1S1 r2S2 r3S3 r4S4 r5S5 r6S6 r7S7 r8S8 r9S9 r:S: r;S; r<S< r=\==r>r?S= r@\@=rArBS> rCS? rDS@ rESArFU =rG$ )BOctaveCodePrinterA   zD
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                  > [         TU ]  U5        [        [        [        [        5      5      U l        U R
                  R                  [        [        5      5        UR                  S0 5      nU R
                  R                  U5        g )Nr~   )	super__init__dictzipknown_fcns_src1known_functionsupdateknown_fcns_src2get)selfsettings	userfuncs	__class__s      M/var/www/auris/envauris/lib/python3.13/site-packages/sympy/printing/octave.pyr   OctaveCodePrinter.__init__Y   sb    "#C$IJ##D$9:LL!126	##I.    c                    US-  $ )N    )r   ps     r   _rate_index_position&OctaveCodePrinter._rate_index_positiona   s    s
r   c                    SU-  $ )Nz%s;r   )r   
codestrings     r   _get_statement OctaveCodePrinter._get_statemente   s    z!!r   c                $    SR                  U5      $ )Nz% {}format)r   texts     r   _get_commentOctaveCodePrinter._get_commenti   s    }}T""r   c                $    SR                  X5      $ )Nz{} = {};r   )r   namevalues      r   _declare_number_const'OctaveCodePrinter._declare_number_constm   s      --r   c                $    U R                  U5      $ N)indent_code)r   liness     r   _format_codeOctaveCodePrinter._format_codeq   s    &&r   c                L   ^ UR                   u  mnU4S j[        U5       5       $ )Nc              3  P   >#    U  H  n[        T5        H  o"U4v   M
     M     g 7f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           	         / n/ nU Hq  n[        U R                  UR                  UR                  S-   UR                  S-   /5      u  pVnUR                  SU< SU< SU< 35        UR                  S5        Ms     X#4$ )N   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           	        UR                   (       aY  UR                  (       aH  [        R                  U-  R                  (       a&  SU R                  [        R                  * U-  5      -  $ [        U5      nUR                  5       u  p4US:  a  [        U* U5      nSnOSn/ n/ n/ nU R                  S;  a  UR                  5       n	O[        R                  " U5      n	U	 GH  n
U
R                  (       Ga  U
R                  (       Ga   U
R                  R                   (       a  U
R                  R"                  (       a  U
R                  S:w  a1  UR%                  ['        U
R(                  U
R                  * SS95        M  [+        U
R,                  S   R,                  5      S	:w  a0  [/        U
R(                  [        5      (       a  UR%                  U
5        UR%                  ['        U
R(                  U
R                  * 5      5        GM(  U
R                   (       a  U
[        R0                  Lan  U
R2                  S	:w  a$  UR%                  [5        U
R2                  5      5        U
R6                  S	:w  a'  UR%                  [5        U
R6                  5      5        GM  GM  UR%                  U
5        GM     U=(       d    [        R8                  /nU Vs/ s H  oR;                  X5      PM     nnU Vs/ s H  oR;                  X5      PM     nnU HP  n
U
R(                  U;   d  M  S
XR=                  U
R(                  5         -  XR=                  U
R(                  5      '   MR     S nU(       d
  X^" Xl5      -   $ [+        U5      S	:X  a+  US   R                   (       a  SOSnX^" Xl5      -   U-   US   -   $ [?        S U 5       5      (       a  SOSnX^" Xl5      -   U-   S
U" X}5      -  -   $ s  snf s  snf )Nz%sir   - )oldnoneF)evaluater   z(%s)c                    US   n[        S[        U 5      5       H&  nXS-
     R                  (       a  SOSnX$-   X   -   nM(     U$ )Nr   r   *.*)r   len	is_number)aa_strrr   mulsyms        r   multjoin.OctaveCodePrinter._print_Mul.<locals>.multjoin   sJ    aA1c!f% !A# 0 0dJ) & Hr   /./c              3  8   #    U  H  oR                   v   M     g 7fr   r   )r   bis     r   r   /OctaveCodePrinter._print_Mul.<locals>.<genexpr>   s     9qq   ) 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parenthesizeindexall)r   exprpreccer>   r   b	pow_parenr   itemxr   b_strr   divsyms                   r   
_print_MulOctaveCodePrinter._print_Mul   s   NNt00%114;;'7'<=== $  "q5r1%DDD	::_,**,D ==&D D###8L8L,,88r>HHSTXXIFG499Q<,,-2z$))S7Q7Q!((.HHSTXXI67!!d!**&<66Q;HHXdff-.66Q;HHXdff-.   " L!%%567Q""1+Q7567Q""1+Q7 DyyA~,2U77499;M5N,Nggdii() 
	 (1,,,Vq[aDNNSF(1,,v5a@@9q999StF8A--#hq&889 :1 87s   O!-O&c                    U R                  UR                  5      nU R                  UR                  5      nUR                  nSR	                  X$U5      $ )Nz{} {} {})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                   [        S UR                   5       5      (       a  SOSn[        U5      n[        UR                  S5      (       a  SU R                  UR                  5      -  $ UR                  (       a  [        UR                  S5      (       aC  UR                  R                  (       a  SOSnS	U-   SU R                  UR                  5      -  -   $ [        UR                  S
5      (       aD  UR                  R                  (       a  SOSnS	U-   SU R                  UR                  U5      -  -   $ U R                  UR                  U5      < U< U R                  UR                  U5      < 3$ )Nc              3  8   #    U  H  oR                   v   M     g 7fr   r   )r   r  s     r   r   /OctaveCodePrinter._print_Pow.<locals>.<genexpr>   s     >Iq{{Ir   ^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_PowOctaveCodePrinter._print_Pow   s   >DII>>>CD	$#&&DII 666DHHd++!YY00cdSy:DII0F#FFFDHHb))!YY00cdSy4$*;*;DIIt*L#LLL,,TYY=y,,TXXt<> 	>r   c                    [        U5      nU R                  UR                  U5      < SU R                  UR                  U5      < 3$ )Nr  )r   r   r   r5   r   r   r  s      r   _print_MatPowOctaveCodePrinter._print_MatPow   s>    $++DIIt<++DHHd;= 	=r   c                    [        U5      nU R                  UR                  U5      < SU R                  UR                  U5      < 3$ )Nz \ )r   r   matrixvectorr  s      r   _print_MatrixSolve$OctaveCodePrinter._print_MatrixSolve   s@    $!..t{{DA!..t{{DAC 	Cr   c                    g)Npir   r   r   s     r   	_print_PiOctaveCodePrinter._print_Pi       r   c                    g)N1ir   r'  s     r   _print_ImaginaryUnit&OctaveCodePrinter._print_ImaginaryUnit   r*  r   c                    g)Nzexp(1)r   r'  s     r   _print_Exp1OctaveCodePrinter._print_Exp1   s    r   c                    g)Nz(1+sqrt(5))/2r   r'  s     r   _print_GoldenRatio$OctaveCodePrinter._print_GoldenRatio  s     r   c                   SSK Jn  SSKJn  SSKJn  UR                  nUR                  nU R                  S   (       d{  [        UR                  U5      (       a`  / n/ nUR                   H-  u  pUR                  U" XY5      5        UR                  U
5        M/     U" [        Xx5      6 nU R                  U5      $ U R                  S   (       a=  UR                  U5      (       d  UR                  U5      (       a  U R                  Xe5      $ U R                  U5      nU R                  U5      nU R!                  U< SU< 35      $ )Nr   )
Assignment)	Piecewise)IndexedBaser   r   r   )sympy.codegen.astr6  $sympy.functions.elementary.piecewiser7  sympy.tensor.indexedr8  r	  r
  	_settingsr   r   r   r   r   has_doprint_loopsr   )r   r   r6  r7  r8  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                    g)Ninfr   r'  s     r   _print_Infinity!OctaveCodePrinter._print_Infinity$      r   c                    g)Nz-infr   r'  s     r   _print_NegativeInfinity)OctaveCodePrinter._print_NegativeInfinity(      r   c                    g)NNaNr   r'  s     r   
_print_NaNOctaveCodePrinter._print_NaN,  rH  r   c                F   ^  SSR                  U 4S jU 5       5      -   S-   $ )N{, c              3  F   >#    U  H  nTR                  U5      v   M     g 7fr   r   )r   r   r   s     r   r   0OctaveCodePrinter._print_list.<locals>.<genexpr>1  s     <t!t{{1~~t   !})joinr'  s   ` r   _print_listOctaveCodePrinter._print_list0  s"    TYY<t<<<sBBr   c                    g)Ntruer   r'  s     r   _print_BooleanTrue$OctaveCodePrinter._print_BooleanTrue7  rL  r   c                    g)Nfalser   r'  s     r   _print_BooleanFalse%OctaveCodePrinter._print_BooleanFalse;  s    r   c                4    [        U5      R                  5       $ r   )strr   r'  s     r   _print_boolOctaveCodePrinter._print_bool?  s    4y  r   c                ~  ^ ^ TR                   TR                  4S:X  a  g[        R                  TR                  ;   a  STR                   < STR                  < S3$ TR                   TR                  4S:X  a  T R                  TS   5      $ SSR                  UU 4S	 j[        TR                   5       5       5      -  $ )
N)r   r   z[]zzeros(rS  ))r   r   z[%s]z; c           	   3     >#    U  H=  nS R                  TUSS24    Vs/ s H  nTR                  U5      PM     sn5      v   M?     gs  snf 7f) N)rY  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   Zeror   r   rY  r   )r   rl  s   ``r   _print_MatrixBase#OctaveCodePrinter._print_MatrixBaseG  s    FFAFFv%VVqww&'ffaff55ffaff';;qw''		 ":+0=": : : 	:r   c                   SSK Jn  UR                  5       nU" U Vs/ s H
  oDS   S-   PM     sn/5      nU" U Vs/ s H
  oDS   S-   PM     sn/5      nU" U Vs/ s H  oDS   PM	     sn/5      nSU R                  U5      < SU R                  U5      < SU R                  U5      < SUR                  < SUR
                  < S3$ s  snf s  snf s  snf )Nr   )Matrixr      zsparse(rS  ri  )sympy.matricesrr  col_listr   r   r   )r   rl  rr  LkIJAIJs           r   _print_SparseRepMatrix(OctaveCodePrinter._print_SparseRepMatrixT  s    )JJLq)q!qTAXq)*+q)q!qTAXq)*+Q'QtQ'()/3{{1~t{{1~,0KK,<affaffN 	N *)'s   C	CCc                    U R                  UR                  [        S   SS9SUR                  S-   < SUR                  S-   < S3-   $ )NAtomT)strict(r   rS  ri  )r   parentr   r   r   r'  s     r   _print_MatrixElement&OctaveCodePrinter._print_MatrixElement_  sB      j.@ N FFQJ
34 	4r   c                  ^  U 4S jnT R                  UR                  5      S-   U" UR                  UR                  R                  S   5      -   S-   U" UR                  UR                  R                  S   5      -   S-   $ )Nc                  > U S   S-   nU S   nU S   nTR                  U5      nX1:X  a  SOTR                  U5      nUS:X  a  US:X  a  X1:X  a  gX#:X  a  U$ US-   U-   $ SR                  UTR                  U5      U45      $ )Nr   r   rs  r   r   )r   rY  )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  r   rS  r   ri  )r   r  rowslicer   colslice)r   r   r  s   `  r   _print_MatrixSlice$OctaveCodePrinter._print_MatrixSliced  s|    	A DKK(3.(9(9!(<=>@DE(9(9!(<=>@CD 	Er   c                    UR                    Vs/ s H  o R                  U5      PM     nnU R                  UR                  R                  5      < SSR	                  U5      < S3$ s  snf )Nr  rS  ri  )r   r   r   r   rY  )r   r   r   indss       r   _print_Indexed OctaveCodePrinter._print_Indexedy  sI    )-7AQ7;;tyy74II 8s   A'c                ^   ^ ^ [         S   mS[        UU 4S jUR                   5       5      -  $ )Nr   zdouble(%s == %s)c              3  H   >#    U  H  nTR                  UT5      v   M     g 7f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   ` @r   _print_KroneckerDelta'OctaveCodePrinter._print_KroneckerDelta~  s2    % !E *>3799*> %> > 	>r   c                    SR                  UR                   Vs/ s H  nU R                  U[        U5      5      PM      sn5      $ s  snf )Nr   )rY  r   r   r   )r   r   rC   s      r   _print_HadamardProduct(OctaveCodePrinter._print_HadamardProduct  sI    yy%)YY0%.c ++CD1AB%.0 1 	1 0s   %Ac                    [        U5      nSR                  U R                  UR                  U5      U R                  UR                  U5      /5      $ )Nz.**)r   rY  r   r   r5   r  s      r   _print_HadamardPower&OctaveCodePrinter._print_HadamardPower  sJ    $zzdii.dhh-  	r   c                   ^  UR                   n[        U5      S:X  a  US   US   :X  a  US   /nSR                  U 4S jU 5       5      nSU-   S-   $ )Nrs  r   r   rS  c              3  F   >#    U  H  nTR                  U5      v   M     g 7fr   rU  )r   nr   s     r   r   4OctaveCodePrinter._print_Identity.<locals>.<genexpr>  s     4edkk!nnerW  zeye(ri  )r   r   rY  )r   r   r   ss   `   r   _print_Identity!OctaveCodePrinter._print_Identity  sT    

u:?uQx5831XJEII4e44zCr   c                    SR                  U R                  UR                  S   5      U R                  UR                  S   5      5      $ )Nz (gammainc({1}, {0}).*gamma({0}))r   r   r   r   r   r'  s     r   _print_lowergamma#OctaveCodePrinter._print_lowergamma  s>    188KK		!%t{{499Q<'@B 	Br   c                    SR                  U R                  UR                  S   5      U R                  UR                  S   5      5      $ )Nz)(gammainc({1}, {0}, 'upper').*gamma({0}))r   r   r  r'  s     r   _print_uppergamma#OctaveCodePrinter._print_uppergamma  s>    :AAKK		!%t{{499Q<'@B 	Br   c                f    SU R                  UR                  S   [        R                  -  5      -  $ )Nzsinc(%s)r   )r   r   r   Pir'  s     r   _print_sincOctaveCodePrinter._print_sinc  s'    DKK		!QTT(9:::r   c                |    SU R                  UR                  5      < SU R                  UR                  5      < S3$ )Nbesselh(z, 1, ri  r   r   argumentr'  s     r   _print_hankel1 OctaveCodePrinter._print_hankel1  .    '+{{4::'>'+{{4=='AC 	Cr   c                |    SU R                  UR                  5      < SU R                  UR                  5      < S3$ )Nr  z, 2, ri  r  r'  s     r   _print_hankel2 OctaveCodePrinter._print_hankel2  r  r   c                    SSK JnJn  UR                  nU" [        R
                  SU-  -  5      U" UR                  [        R                  -   U5      -  nU R                  U5      $ )Nr   )sqrtr/   rs  )	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                    SSK JnJn  UR                  nU" [        R
                  SU-  -  5      U" UR                  [        R                  -   U5      -  nU R                  U5      $ )Nr   )r  r1   rs  )	r  r  r1   r  r   r  r   r  r   )r   r   r  r1   r  r  s         r   	_print_ynOctaveCodePrinter._print_yn  r  r   c                D    SU R                  UR                  S   5      -  $ )Nzairy(0, %s)r   r   r   r'  s     r   _print_airyaiOctaveCodePrinter._print_airyai      t{{499Q<888r   c                D    SU R                  UR                  S   5      -  $ )Nzairy(1, %s)r   r  r'  s     r   _print_airyaiprime$OctaveCodePrinter._print_airyaiprime  r  r   c                D    SU R                  UR                  S   5      -  $ )Nzairy(2, %s)r   r  r'  s     r   _print_airybiOctaveCodePrinter._print_airybi  r  r   c                D    SU R                  UR                  S   5      -  $ )Nzairy(3, %s)r   r  r'  s     r   _print_airybiprime$OctaveCodePrinter._print_airybiprime  r  r   c                t    UR                   u  p#US:w  a  U R                  U5      $ SU R                  U5      -  $ )Nr   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                &   [        UR                  5      S::  d   eSR                  U R                  UR                  R
                     SR                  [        UR                  5       Vs/ s H  o R                  U5      PM     sn5      S9$ s  snf )Nrs  z{name}({args})rS  )r   r   )	r   r   r   r   r   __name__rY  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   +Bc                    SR                  U R                  UR                  R                     U R	                  UR
                  S   5      U R	                  UR                  " UR
                  SS  6 5      S9$ )Nz{name}({arg1}, {arg2})r   r   )r   arg1arg2)r   r   r   r  r   r   funcr'  s     r   _nested_binary_math_func*OctaveCodePrinter._nested_binary_math_func  se    '..%%dnn&=&=>TYYq\*TYY		!"67 /  	r   c           
        UR                   S   R                  S:w  a  [        S5      e/ nU R                  S   (       a  UR                   S S  VVs/ s H5  u  p4SR	                  U R                  U5      U R                  U5      5      PM7     nnnSU R                  UR                   S   R                  5      -  nSR                  U5      U-   S[        U5      -  -   nS	U-   S-   $ [        UR                   5       H  u  nu  p4US
:X  a$  UR                  SU R                  U5      -  5        OWU[        UR                   5      S-
  :X  a  US:X  a  UR                  S5        O#UR                  SU R                  U5      -  5        U R                  U5      n	UR                  U	5        U[        UR                   5      S-
  :X  d  M  UR                  S5        M     SR                  U5      $ s  snnf )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   z({0}).*({1}) + (~({0})).*(r  z ...
ri  r  r   zif (%s)r   elsezelseif (%s)r   
)r   cond
ValueErrorr<  r   r   r   rY  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   <G*c                    [        UR                  5      S:X  a!  SU R                  UR                  S   5      -  $ U R                  U5      $ )Nr   zzeta(%s)r   )r   r   r   r  r'  s     r   _print_zetaOctaveCodePrinter._print_zeta  sA    tyy>QDIIaL 999 ,,T22r   c           	     l  ^ [        U[        5      (       a1  U R                  UR                  S5      5      nSR	                  U5      $ SnSnSnU Vs/ s H  ofR                  S5      PM     nnU V^s/ s H!  m[        [        U4S jU 5       5      5      PM#     nnU V^s/ s H!  m[        [        U4S jU 5       5      5      PM#     nn/ n	S	n
[        U5       HF  u  nmTS
;   a  U	R                  T5        M  XU   -  n
U	R                  X:-  < T< 35        XU   -  n
MH     U	$ s  snf s  snf s  snf )z0Accepts a string of code or a list of code linesTr   z  )z
^function z^if ^elseif ^else$z^for )z^end$r  r  z 	c              3  <   >#    U  H  n[        UT5      v   M     g 7fr   r   r   rm   lines     r   r   0OctaveCodePrinter.indent_code.<locals>.<genexpr>&       B	"VB--	   c              3  <   >#    U  H  n[        UT5      v   M     g 7fr   r   r  s     r   r   r  (  r  r  r   )r   r  )
r   re  r   
splitlinesrY  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C!% 	 ( "&(!% B	BBC!% 	 (  GAtz!d#a[ EMMCIt45a[ E ' ! 7((s   D'3(D,"(D1)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-  r0  r3  rB  rF  rJ  rO  rZ  _print_tuple_print_Tuple_print_Listr^  rb  rf  ro  r{  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   rr   rr   A   s    KH J )-[-J-J )	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   rr   Nc                6    [        U5      R                  X5      $ )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"  7  s    P X&..t??r   c                .    [        [        U 40 UD65        g)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-     s5  
 #  , , & + 2 < :	5	7 
 v	
 , , 
9 	(  '  	&  
 	(  	!" 
5#$ 
#
18s slH@V)r   