
    Ed[                         d Z ddlmZmZ ddlmZmZmZm	Z	 ddl
mZ ddlmZ ddlmZmZ ddlmZ g dZd	d
ddddddZ G d de          ZddZd ZdS )a  
Julia code printer

The `JuliaCodePrinter` converts SymPy expressions into Julia expressions.

A complete code generator, which uses `julia_code` extensively, can be found
in `sympy.utilities.codegen`.  The `codegen` module can be used to generate
complete source code files.

    )AnyDict)MulPowSRational)_keep_coeff)CodePrinter)
precedence
PRECEDENCEsearch)3sincostancotseccscasinacosatanacotasecacscsinhcoshtanhcothsechcschasinhacoshatanhacothasechacschsincatan2signfloorlogexpcbrtsqrterferfcerfi	factorialgammadigammatrigamma	polygammabetaairyaiairyaiprimeairybiairybiprimebesseljbesselybesselibesselkerfinverfcinvabsceilconjhankelh1hankelh2imagreal)Absceiling	conjugatehankel1hankel2imrec            	       ^    e Zd ZdZdZdZddddZdd	d
i dddddZi f fd	Zd Z	d Z
d Zd Zd Zd Zd Zd Zd Zd Zd Z fdZd Z fdZ fdZ fdZ fdZd  Zd! Zd" Zd# Zd$ Zd% ZeZ d& Z!d' Z"d( Z#d) Z$d* Z%d+ Z&d, Z'd- Z(d. Z)d/ Z*d0 Z+d1 Z,d2 Z-d3 Z.d4 Z/d5 Z0d6 Z1d7 Z2 xZ3S )8JuliaCodePrinterzD
    A printer to convert expressions to strings of Julia code.
    _juliaJuliaz&&z||!)andornotNauto   TF)order	full_prec	precisionuser_functionshumanallow_unknown_functionscontractinlinec                 Z   t                                          |           t          t          t          t                              | _        | j                            t          t                               |                    di           }| j                            |           d S )Nr]   )	super__init__dictzipknown_fcns_src1known_functionsupdateknown_fcns_src2get)selfsettings	userfuncs	__class__s      4lib/python3.11/site-packages/sympy/printing/julia.pyrd   zJuliaCodePrinter.__init__I   s    """#C$I$IJJ##D$9$9:::LL!1266	##I.....    c                     |dz  S )N    )rl   ps     rp   _rate_index_positionz%JuliaCodePrinter._rate_index_positionQ   s    s
rq   c                     d|z  S )Nz%srt   )rl   
codestrings     rp   _get_statementzJuliaCodePrinter._get_statementU   s    j  rq   c                 ,    d                     |          S )Nz# {}format)rl   texts     rp   _get_commentzJuliaCodePrinter._get_commentY   s    }}T"""rq   c                 .    d                     ||          S )Nzconst {} = {}r{   )rl   namevalues      rp   _declare_number_constz&JuliaCodePrinter._declare_number_const]   s    %%dE222rq   c                 ,    |                      |          S N)indent_code)rl   liness     rp   _format_codezJuliaCodePrinter._format_codea   s    &&&rq   c                 N    |j         \  }fdt          |          D             S )Nc              3   D   K   | ]}t                    D ]}||fV  	d S r   )range).0jirowss      rp   	<genexpr>z<JuliaCodePrinter._traverse_matrix_indices.<locals>.<genexpr>h   s:      AA1U4[[AAAAAAAAAArq   )shaper   )rl   matcolsr   s      @rp   _traverse_matrix_indicesz)JuliaCodePrinter._traverse_matrix_indicese   s.    Y
dAAAAdAAAArq   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)rl   indices
open_linesclose_linesr   varstartstops           rp   _get_loop_opening_endingz)JuliaCodePrinter._get_loop_opening_endingk   s    
 	& 	&A"4;Wagk17Q;7 9  9C###uuuddCDDDu%%%%;&&rq   c                 J    |j         rL|j        rE|                                d         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 ]D}	|	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        rB|	t
          j        ur4|	j        d	k    r)|                    t5          |	j                             /|                    |	           F|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   d|d|d         S t=          d |D                       rdnd}| |||
          z   d|d |||          dS )Nr   z%sim- )oldnoneF)evaluater   c                 <    g | ]}                     |          S rt   parenthesizer   xprecrl   s     rp   
<listcomp>z/JuliaCodePrinter._print_Mul.<locals>.<listcomp>   )    777""1d++777rq   c                 <    g | ]}                     |          S rt   r   r   s     rp   r   z/JuliaCodePrinter._print_Mul.<locals>.<listcomp>   r   rq   (%s)c                     |d         }t          dt          |                     D ]&}| |dz
           j        rdnd}|d|d||         }'|S )Nr   r   *z.* )r   len	is_number)aa_strrr   mulsyms        rp   multjoinz-JuliaCodePrinter._print_Mul.<locals>.multjoin   sd    aA1c!ff%% 7 7 !!A# 0:d"#!!VVVU1XX6Hrq   /./r   c              3   $   K   | ]}|j         V  d S r   r   )r   bis     rp   r   z.JuliaCodePrinter._print_Mul.<locals>.<genexpr>   s$      99999999rq   z ())r   is_imaginaryas_coeff_Mul
is_integerr   r   ImaginaryUnitr   r	   rZ   as_ordered_factorsr   	make_argsis_commutativeis_Powr,   is_Rationalis_negativer   r   baser   args
isinstanceInfinityru   r   qOneindexall)rl   exprcer)   r   b	pow_parenr   itemr   b_strr   divsymr   s   `             @rp   
_print_MulzJuliaCodePrinter._print_Mulw   s   N 	?t0 	?!!##A&1	?DKK(8(=>>>> $  ""1q5 	r1%%DDDD	:_, 	'**,,DD =&&D  	 	D#  8L ,8r> 8HHSTXIFFFGGGG49Q<,--2 /z$)S7Q7Q /!((...HHSTXI667777! d!*&< 1 
 $&))****L!%77777Q77777777Q777  	O 	ODyA~ O,2U17749;M;M5N,Naggdi(()	 	 	  	Z((1e,,,,VVq[ 	ZaDN4SSF!%hhq%&8&8!8!8!8&&&%((KK99q99999CSStF#'((1e*<*<#<#<#<fffhhqRWFXFXFXFXYYrq   c                     |                      |j                  }|                      |j                  }|j        }d                    |||          S )Nz{} {} {})r   lhsrhsrel_opr|   )rl   r   lhs_coderhs_codeops        rp   _print_Relationalz"JuliaCodePrinter._print_Relational   sG    ;;tx((;;tx(([  2x888rq   c                 j   t          d |j        D                       rdnd}t          |          }|j        t          j        k    rd|                     |j                  z  S |j        r|j        t          j         k    r1|j        j	        rdnd}d|d|                     |j                  d	S |j        t          j
         k    r1|j        j	        rdnd}d|d
|                     |j        |          S |                     |j        |          d
|d
|                     |j        |          S )Nc              3   $   K   | ]}|j         V  d S r   r   )r   r   s     rp   r   z.JuliaCodePrinter._print_Pow.<locals>.<genexpr>   s$      >>qq{>>>>>>rq   ^z.^zsqrt(%s)r   r   z1 z sqrt(r   r   )r   r   r   r,   r   Halfr   r   r   r   r   r   )rl   r   	powsymbolPRECsyms        rp   
_print_PowzJuliaCodePrinter._print_Pow   sH   >>DI>>>>>HCCD	$8qv 	7DI 6 666 	NxAF7" G!Y0:ccd*-##t{{49/E/E/E/EFFxAE6! N!Y0:ccd%(SS$*;*;DIt*L*L*LMM!..ty$????,,TXt<<<> 	>rq   c                     t          |          }|                     |j        |          d|                     |j        |          S )Nz ^ )r   r   r   r,   rl   r   r   s      rp   _print_MatPowzJuliaCodePrinter._print_MatPow   sL    $ --di>>>>++DHd;;;= 	=rq   c                 d    | j         d         rdS t                                          |          S )Nra   pi	_settingsrc   _print_NumberSymbolrl   r   ro   s     rp   	_print_PizJuliaCodePrinter._print_Pi   s/    >(# 	5477..t444rq   c                     dS )NrN   rt   rl   r   s     rp   _print_ImaginaryUnitz%JuliaCodePrinter._print_ImaginaryUnit   s    trq   c                 d    | j         d         rdS t                                          |          S )Nra   r   r   r   s     rp   _print_Exp1zJuliaCodePrinter._print_Exp1   s/    >(# 	5377..t444rq   c                 d    | j         d         rdS t                                          |          S )Nra   
eulergammar   r   s     rp   _print_EulerGammaz"JuliaCodePrinter._print_EulerGamma   s/    >(# 	5<77..t444rq   c                 d    | j         d         rdS t                                          |          S )Nra   catalanr   r   s     rp   _print_CatalanzJuliaCodePrinter._print_Catalan   s/    >(# 	5977..t444rq   c                 d    | j         d         rdS t                                          |          S )Nra   goldenr   r   s     rp   _print_GoldenRatioz#JuliaCodePrinter._print_GoldenRatio  s/    >(# 	5877..t444rq   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)IndexedBasera   r`   r   )sympy.codegen.astr  $sympy.functions.elementary.piecewiser  sympy.tensor.indexedr  r   r   r   r   r   r   rf   r   has_doprint_loopsry   )rl   r   r  r  r  r   r   expressions
conditionsr   r   tempr   r   s                 rp   _print_Assignmentz"JuliaCodePrinter._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Hrq   c                     dS )NInfrt   r   s     rp   _print_Infinityz JuliaCodePrinter._print_Infinity%      urq   c                     dS )Nz-Infrt   r   s     rp   _print_NegativeInfinityz(JuliaCodePrinter._print_NegativeInfinity)      vrq   c                     dS )NNaNrt   r   s     rp   
_print_NaNzJuliaCodePrinter._print_NaN-  r  rq   c                 R     dd                      fd|D                       z   dz   S )NzAny[, c              3   B   K   | ]}                     |          V  d S r   r   r   r   rl   s     rp   r   z/JuliaCodePrinter._print_list.<locals>.<genexpr>2  s-      !?!?Q$++a..!?!?!?!?!?!?rq   ])joinr   s   ` rp   _print_listzJuliaCodePrinter._print_list1  s4    		!?!?!?!?$!?!?!????#EErq   c                     t          |          dk    rd|                     |d                   z  S d|                     |d          z  S )Nr   z(%s,)r   r   r"  )r   r   	stringifyr   s     rp   _print_tuplezJuliaCodePrinter._print_tuple5  sH    t99> 	7T[[a1111DNN46666rq   c                     dS )Ntruert   r   s     rp   _print_BooleanTruez#JuliaCodePrinter._print_BooleanTrue=  r  rq   c                     dS )Nfalsert   r   s     rp   _print_BooleanFalsez$JuliaCodePrinter._print_BooleanFalseA  s    wrq   c                 D    t          |                                          S r   )strr   r   s     rp   _print_boolzJuliaCodePrinter._print_boolE  s    4yy   rq   c                 p    t           j        |j        v rd|j        d|j        dS |j        |j        fdk    rd|d         z  S |j        dk    rd|                     ddd	
          z  S |j        dk    r$dd                     fd|D                       z  S d|                     dddd	          z  S )Nzzeros(r"  r   )r   r   z[%s])r   r   r   r   r   )rowstartrowendcolsepc                 :    g | ]}                     |          S rt   r$  r%  s     rp   r   z6JuliaCodePrinter._print_MatrixBase.<locals>.<listcomp>W  s#    &A&A&A!t{{1~~&A&A&Arq   z;
)r6  r7  rowsepr8  )r   Zeror   r   r   tabler'  )rl   As   ` rp   _print_MatrixBasez"JuliaCodePrinter._print_MatrixBaseM  s    6QW 	C 	C&'fffafff55faf' 	CAdG##Vq[ 	CAGGD2bGMMMMVq[ 	CDII&A&A&A&Aq&A&A&ABBBBr"',S   : : : 	:rq   c                 ^   ddl m} |                                } |d |D                       } |d |D                       } |d |D                       }d|                     |          d|                     |          d|                     |          d|j        d|j        dS )	Nr   )Matrixc                 $    g | ]}|d          dz   S )r   r   rt   r   ks     rp   r   z;JuliaCodePrinter._print_SparseRepMatrix.<locals>.<listcomp>`       (((AaD1H(((rq   c                 $    g | ]}|d          d z   S )r   rt   rB  s     rp   r   z;JuliaCodePrinter._print_SparseRepMatrix.<locals>.<listcomp>a  rD  rq   c                     g | ]
}|d          S )   rt   rB  s     rp   r   z;JuliaCodePrinter._print_SparseRepMatrix.<locals>.<listcomp>b  s    &&&qad&&&rq   zsparse(r"  r   )sympy.matricesr@  col_listr   r   r   )rl   r=  r@  LIJAIJs          rp   _print_SparseRepMatrixz'JuliaCodePrinter._print_SparseRepMatrix\  s    ))))))JJLLF((a((())F((a((())f&&A&&&''/3{{1~~~~t{{1~~~~,0KK,<,<,<,<afffafffN 	Nrq   c                     |                      |j        t          d         d          d|j        dz   d|j        dz   dz   S )NAtomT)strict[r   ,r&  )r   parentr   r   r   r   s     rp   _print_MatrixElementz%JuliaCodePrinter._print_MatrixElementg  sJ      j.@ NNN6A:::tvzzz23 	3rq   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   rG  r   r   )r   r'  )r   limlhsteplstrhstrrl   s          rp   strslicez5JuliaCodePrinter._print_MatrixSlice.<locals>.strslicem  s    !qA!AQ4D;;q>>DH855$++a..Dqy A6 a3h 36 -K#:,,xxt{{4'8'8$ ?@@@rq   rR  r   rS  r   r&  )r   rT  rowslicer   colslice)rl   r   r^  s   `  rp   _print_MatrixSlicez#JuliaCodePrinter._print_MatrixSlicel  s    	A 	A 	A 	A 	A DK((3.(9!(<==>@CD(9!(<==>@CD 	Erq   c                       fd|j         D             }                     |j        j                  dd                    |          dS )Nc                 :    g | ]}                     |          S rt   r$  )r   r   rl   s     rp   r   z3JuliaCodePrinter._print_Indexed.<locals>.<listcomp>  s#    777AQ777rq   rR  rS  r&  )r   r   r   r   r'  )rl   r   indss   `  rp   _print_IndexedzJuliaCodePrinter._print_Indexed  sM    7777777;;ty7777$HHrq   c                 6    |                      |j                  S r   )r   r   r   s     rp   
_print_IdxzJuliaCodePrinter._print_Idx  s    {{4:&&&rq   c                 H    d|                      |j        d                   z  S )Nzeye(%s)r   )r   r   r   s     rp   _print_Identityz JuliaCodePrinter._print_Identity  s    4;;tz!}5555rq   c                 T     d                      fdj        D                       S )Nz .* c                 V    g | ]%}                     |t                              &S rt   r   r   r   argr   rl   s     rp   r   z;JuliaCodePrinter._print_HadamardProduct.<locals>.<listcomp>  sA     0 0 0! !--c:d3C3CDD 0 0 0rq   )r'  r   r   s   ``rp   _print_HadamardProductz'JuliaCodePrinter._print_HadamardProduct  sG    {{ 0 0 0 0 0%)Y0 0 0 1 1 	1rq   c                     t          |          }d                    |                     |j        |          |                     |j        |          g          S )Nz.**)r   r'  r   r   r,   r   s      rp   _print_HadamardPowerz%JuliaCodePrinter._print_HadamardPower  sT    $zzdi..dh--   	rq   c                 b    |j         dk    rt          |j                  S |j        d|j         S )Nr   z // )r   r3  ru   r   s     rp   _print_Rationalz JuliaCodePrinter._print_Rational  s3    6Q; 	tv;;!VVVTVV,,rq   c                     ddl m}m} |j        } |t          j        d|z  z             ||j        t          j        z   |          z  }|                     |          S )Nr   )r.   r<   rG  )	sympy.functionsr.   r<   argumentr   PirZ   r   r   )rl   r   r.   r<   r   expr2s         rp   	_print_jnzJuliaCodePrinter._print_jn  f    11111111MQT1Q3Z  af)<a!@!@@{{5!!!rq   c                     ddl m}m} |j        } |t          j        d|z  z             ||j        t          j        z   |          z  }|                     |          S )Nr   )r.   r=   rG  )	ru  r.   r=   rv  r   rw  rZ   r   r   )rl   r   r.   r=   r   rx  s         rp   	_print_ynzJuliaCodePrinter._print_yn  rz  rq   c                 r    |j         d         j        dk    rt          d          g } j        d         rc fd|j         d d         D             }d                     |j         d         j                  z  }d                    |          |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.ra   c                     g | ]A\  }}d                                          |                              |                    BS )z({}) ? ({}) :)r|   r   )r   r   r   rl   s      rp   r   z5JuliaCodePrinter._print_Piecewise.<locals>.<listcomp>  sV     3 3 31a '--AA8 8 3 3 3rq   z (%s)
(r   r   zif (%s)r   elsezelseif (%s)r   )
r   cond
ValueErrorr   r   r   r'  	enumerater   r   )
rl   r   r   ecpairselastpwr   r   r   code0s
   `         rp   _print_Piecewisez!JuliaCodePrinter._print_Piecewise  s   9R=% 	0  / 0 0 0
 >(# 	$3 3 3 3#'9SbS>3 3 3G dkk$)B-*<===E7##e+B 8c>!&ty11 
( 
(	6Aq6 ALLT[[^^!;<<<<#di..1,, Ad ALL((((LLQ!?@@@AU###DI** (LL'''99U###rq   c                 N                                     \  }}d}|j        rZ|                                \  }}|j        r|j        rt          | |          d}n!|j        r|j        rt          | |          d}|d                     fdj        D                       z   S )Nr   r   z * c              3   ^   K   | ]'}                     |t                              V  (d S r   rl  rm  s     rp   r   z1JuliaCodePrinter._print_MatMul.<locals>.<genexpr>  s;      KK#TsJt$4$455KKKKKKrq   )as_coeff_mmulr   as_real_imagis_zeror   r	   r'  r   )rl   r   r   mr)   rO   rN   s   ``     rp   _print_MatMulzJuliaCodePrinter._print_MatMul  s    !!##1; 	^^%%FBz bn "A2q))  "A2q))ejjKKKKKKKK
 
 
 	
rq   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     rp   r   z0JuliaCodePrinter.indent_code.<locals>.<listcomp>  s$    666U##666rq   c                 b    g | ]*t          t          fd D                                 +S )c              3   8   K   | ]}t          |          V  d S r   r   r   rO   r  s     rp   r   z:JuliaCodePrinter.indent_code.<locals>.<listcomp>.<genexpr>  -      BB"VB--BBBBBBrq   intany)r   r  	inc_regexs    @rp   r   z0JuliaCodePrinter.indent_code.<locals>.<listcomp>  O     ( ( ( BBBB	BBBBBCC ( ( (rq   c                 b    g | ]*t          t          fd D                                 +S )c              3   8   K   | ]}t          |          V  d S r   r   r  s     rp   r   z:JuliaCodePrinter.indent_code.<locals>.<listcomp>.<genexpr>  r  rq   r  )r   r  	dec_regexs    @rp   r   z0JuliaCodePrinter.indent_code.<locals>.<listcomp>  r  rq   r   )r   r  )r   r3  r   
splitlinesr'  r  r   )rl   code
code_linestabincreasedecreaseprettylevelnr  r  r  s             @@rp   r   zJuliaCodePrinter.indent_code  sF    dC   	'))$//$*?*?@@J77:&&&I	3	 76666( ( ( (!%( ( (( ( ( (!%( ( (   	! 	!GAtz! d###Xa[ EMMCIItt4555Xa[ EErq   )4__name__
__module____qualname____doc__printmethodlanguage
_operators_default_settingsrd   rv   ry   r~   r   r   r   r   r   r   r   r   r   r   r   r  r  r	  r  r  r  r   r(  r+  _print_Tupler.  r1  r4  r>  rN  rU  ra  re  rg  ri  ro  rq  rs  ry  r|  r  r  r   __classcell__)ro   s   @rp   rQ   rQ   .   sP         KH  J #(	 	 !# / / / / / /  ! ! !# # #3 3 3' ' 'B B B	' 	' 	'HZ HZ HZT9 9 9> > >(= = =5 5 5 5 5  5 5 5 5 55 5 5 5 55 5 5 5 55 5 5 5 5I I I:      F F F7 7 7
  L    ! ! !: : :N N N3 3 3
E E E*I I I
' ' '6 6 61 1 1  - - -" " "" " ""$ "$ "$H
 
 
$      rq   rQ   Nc                 H    t          |                              | |          S )a)  Converts `expr` to a string of Julia code.

    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 julia_code, symbols, sin, pi
    >>> x = symbols('x')
    >>> julia_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")
    >>> julia_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 possible in Julia to write "vectorized"
    code.  It is harmless if the values are scalars.

    >>> julia_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)
    >>> julia_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:

    >>> julia_code(x**2*y*A**3)
    '(x .^ 2 .* y) * A ^ 3'

    Matrices are supported using Julia 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)]])
    >>> julia_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))
    >>> julia_code(pw, assign_to=tau)
    'tau = ((x > 0) ? (x + 1) : (x))'

    Note that any expression that can be generated normally can also exist
    inside a Matrix:

    >>> mat = Matrix([[x**2, pw, sin(x)]])
    >>> julia_code(mat, assign_to='A')
    'A = [x .^ 2 ((x > 0) ? (x + 1) : (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 Julia function.

    >>> from sympy import Function
    >>> f = Function('f')
    >>> g = Function('g')
    >>> custom_functions = {
    ...   "f": "existing_julia_fcn",
    ...   "g": [(lambda x: x.is_Matrix, "my_mat_fcn"),
    ...         (lambda x: not x.is_Matrix, "my_fcn")]
    ... }
    >>> mat = Matrix([[1, x]])
    >>> julia_code(f(x) + g(x) + g(mat), user_functions=custom_functions)
    'existing_julia_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]))
    >>> julia_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_torm   s      rp   
julia_coder    s#    L H%%--dI>>>rq   c                 :    t          t          | fi |           dS )z~Prints the Julia representation of the given expression.

    See `julia_code` for the meaning of the optional arguments.
    N)printr  )r   rm   s     rp   print_julia_coder    s(    
 
*T
&
&X
&
&'''''rq   r   )r  typingr   r   tDict
sympy.corer   r   r   r   sympy.core.mulr	   sympy.printing.codeprinterr
   sympy.printing.precedencer   r   rO   r   rg   rj   rQ   r  r  rt   rq   rp   <module>r     s4  	 	 & % % % % % % % , , , , , , , , , , , , & & & & & & 2 2 2 2 2 2 < < < < < < < <      
( 
( 
( 

 Q Q Q Q Q{ Q Q QhF? F? F? F?R( ( ( ( (rq   