B
    }[b              0   @   s8  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
mZ ddlmZ ddlmZ ddlmZ ddlmZmZ dd	lmZ d
dddddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9g0Zd:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPZG dQdR dReZdXdTdUZdVdW ZdSS )Yai  
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.

    )print_functiondivision)MulPowSRational)string_typesrange)_keep_coeff)
Assignment)CodePrinter)
precedence
PRECEDENCE)searchZsinZcosZtanZcotZsecZcscZasinZacosZacotZatanZatan2ZasecZacscZsinhZcoshZtanhZcothZcschZsechZasinhZacoshZatanhZacothZasechZacschZerfcZerfiZerfZerfinvZerfcinvZbesselibesseljZbesselkbesselyZ	bernoulliZbetaZeulerexpZ	factorialZfloorZfresnelcZfresnelsZgammaZharmoniclogZpolylogsignZzetaabsZangleZceilZ
chebyshevUZ
chebyshevTZcoshintZcosintZconjZdiracZ	heavisideimagZ	laguerreLZlambertwZlogintZgammalnmaxminZpsirealZ
pochhammerZsinhintZsinint)ZAbsargZceilingZ
chebyshevuZ
chebyshevtZChiZCi	conjugateZ
DiracDeltaZ	HeavisideZimZlaguerreZLambertWZliZloggammaZMaxZMinZ	polygammareZRisingFactorialZShiZSic            	       s  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d	Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 ZeZeZd6d7 Zd8d9 Zd:d; Z d<d= Z!d>d? Z"e! Z# Z$ Z% Z&Z'e" Z(Z)d@dA Z*dBdC Z+dDdE Z,dFdG Z-dHdI Z.dJdK Z/dLdM Z0dNdO Z1dPdQ Z2dRdS Z3dTdU Z4dVdW Z5dXdY Z6dZd[ Z7d\d] Z8d^d_ Z9d`da Z:dbdc Z;ddde Z<e< Z=Z>dfdg Z?e? Z@ZAdhdi ZBdjdk ZCdldm ZD  ZES )nOctaveCodePrinterzL
    A printer to convert expressions to strings of Octave/Matlab code.
    Z_octaveZOctave&|~)andornotNauto   TF)orderZ	full_precZ	precisionuser_functionsZhumanZallow_unknown_functionscontractinlinec                sL   t t| | tttt| _| jtt |	di }| j| d S )Nr'   )
superr   __init__dictzipknown_fcns_src1known_functionsupdateknown_fcns_src2get)selfsettingsZ	userfuncs)	__class__ 4lib/python3.7/site-packages/sympy/printing/octave.pyr+   Z   s
    zOctaveCodePrinter.__init__c             C   s   |d S )N   r6   )r3   pr6   r6   r7   _rate_index_positionb   s    z&OctaveCodePrinter._rate_index_positionc             C   s   d| S )Nz%s;r6   )r3   Z
codestringr6   r6   r7   _get_statementf   s    z OctaveCodePrinter._get_statementc             C   s
   d |S )Nz% {0})format)r3   textr6   r6   r7   _get_commentj   s    zOctaveCodePrinter._get_commentc             C   s   d ||S )Nz
{0} = {1};)r<   )r3   namevaluer6   r6   r7   _declare_number_constn   s    z'OctaveCodePrinter._declare_number_constc             C   s
   |  |S )N)indent_code)r3   linesr6   r6   r7   _format_coder   s    zOctaveCodePrinter._format_codec                s    |j \ } fddt|D S )Nc             3   s$   | ]}t  D ]}||fV  qqd S )N)r	   ).0ji)rowsr6   r7   	<genexpr>y   s    z=OctaveCodePrinter._traverse_matrix_indices.<locals>.<genexpr>)shaper	   )r3   Zmatcolsr6   )rH   r7   _traverse_matrix_indicesv   s    
z*OctaveCodePrinter._traverse_matrix_indicesc             C   sb   g }g }xP|D ]H}t | j|j|jd |jd g\}}}|d|||f  |d qW ||fS )N   zfor %s = %s:%send)map_printlabellowerupperappend)r3   indicesZ
open_linesZclose_linesrG   varstartstopr6   r6   r7   _get_loop_opening_ending|   s    
 z*OctaveCodePrinter._get_loop_opening_endingc                s  |j r.|jr.tj| jr.dtj |  S t| | \}}|dk r\t| |}d}nd}g }g }g }j	dkr|
 }n
t|}x|D ]}	|	jr$|	jr$|	jjr$|	jjr$|	jdkr|t|	j|	j dd nDt|	jd jd	krt|	jtr||	 |t|	j|	j  q|	jrr|	tjk	rr|	jd	krT|t|	j |	jd	kr||t|	j q||	 qW |ptjg} fd
d|D }
 fdd|D }x:|D ]2}	|	j|krd|||	j  |||	j< qW dd }t|dkr||||
 S t|d	krR|d j r4dnd}||||
 | |d  S tdd |D rjdnd}||||
 | d|||  S d S )Nz%sir   - )oldZnoneF)ZevaluaterM   c                s   g | ]} | qS r6   )parenthesize)rE   x)precr3   r6   r7   
<listcomp>   s    z0OctaveCodePrinter._print_Mul.<locals>.<listcomp>c                s   g | ]} | qS r6   )r^   )rE   r_   )r`   r3   r6   r7   ra      s    z(%s)c             S   sJ   |d }x<t dt| D ]*}| |d  jr.dnd}|| ||  }qW |S )Nr   rM   *z.*)r	   len	is_number)aa_strrrG   Zmulsymr6   r6   r7   multjoin   s
    z.OctaveCodePrinter._print_Mul.<locals>.multjoin/z./c             S   s   g | ]
}|j qS r6   )rd   )rE   Zbir6   r6   r7   ra      s    )rd   Zis_imaginaryr   ZImaginaryUnitZ
is_IntegerrP   r   Zas_coeff_Mulr
   r&   Zas_ordered_factorsr   Z	make_argsis_commutativeZis_Powr   Zis_RationalZis_negativerT   r   baserc   args
isinstanceZInfinityr9   r   qOneindexall)r3   exprcer   re   bZ	pow_parenrl   itemrf   Zb_strrh   Zdivsymr6   )r`   r3   r7   
_print_Mul   sV    





$

&zOctaveCodePrinter._print_Mulc             C   s   t dd |jD rdnd}t|}|jtjkr@d| |j S |jr|jtj kr||jj	r`dnd}d| d| |j  S |jtj
 kr|jj	rdnd}d| d	| |j|  S d
| |j||| |j|f S )Nc             S   s   g | ]
}|j qS r6   )rd   )rE   r_   r6   r6   r7   ra      s    z0OctaveCodePrinter._print_Pow.<locals>.<listcomp>^z.^zsqrt(%s)ri   z./1z%sz%s%s%s)rq   rl   r   r   r   HalfrP   rk   rj   rd   ro   r^   )r3   rr   Z	powsymbolPRECZsymr6   r6   r7   
_print_Pow   s    zOctaveCodePrinter._print_Powc             C   s(   t |}d| |j|| |j|f S )Nz%s^%s)r   r^   rk   r   )r3   rr   r{   r6   r6   r7   _print_MatPow   s    zOctaveCodePrinter._print_MatPowc             C   s   dS )NZpir6   )r3   rr   r6   r6   r7   	_print_Pi   s    zOctaveCodePrinter._print_Pic             C   s   dS )NZ1ir6   )r3   rr   r6   r6   r7   _print_ImaginaryUnit   s    z&OctaveCodePrinter._print_ImaginaryUnitc             C   s   dS )Nzexp(1)r6   )r3   rr   r6   r6   r7   _print_Exp1   s    zOctaveCodePrinter._print_Exp1c             C   s   dS )Nz(1+sqrt(5))/2r6   )r3   rr   r6   r6   r7   _print_GoldenRatio   s    z$OctaveCodePrinter._print_GoldenRatioc             C   s   ddl m} ddlm} |j}|j}| jd st|j|rg }g }x,|jD ]"\}}	|	t
|| |	|	 qJW |t|| }
| |
S | jd r||s||r| ||S | |}| |}| d||f S d S )Nr   )	Piecewise)IndexedBaser)   r(   z%s = %s)Z$sympy.functions.elementary.piecewiser   Zsympy.tensor.indexedr   lhsrhs	_settingsrm   rl   rT   r   r-   rP   ZhasZ_doprint_loopsr;   )r3   rr   r   r   r   r   ZexpressionsZ
conditionsrt   rs   ZtempZlhs_codeZrhs_coder6   r6   r7   _print_Assignment   s$    



z#OctaveCodePrinter._print_Assignmentc             C   s   dS )Ninfr6   )r3   rr   r6   r6   r7   _print_Infinity  s    z!OctaveCodePrinter._print_Infinityc             C   s   dS )Nz-infr6   )r3   rr   r6   r6   r7   _print_NegativeInfinity  s    z)OctaveCodePrinter._print_NegativeInfinityc             C   s   dS )NZNaNr6   )r3   rr   r6   r6   r7   
_print_NaN#  s    zOctaveCodePrinter._print_NaNc                s    dd  fdd|D  d S )N{z, c             3   s   | ]}  |V  qd S )N)rP   )rE   re   )r3   r6   r7   rI   (  s    z0OctaveCodePrinter._print_list.<locals>.<genexpr>})join)r3   rr   r6   )r3   r7   _print_list'  s    zOctaveCodePrinter._print_listc             C   s   dS )Ntruer6   )r3   rr   r6   r6   r7   _print_BooleanTrue-  s    z$OctaveCodePrinter._print_BooleanTruec             C   s   dS )NZfalser6   )r3   rr   r6   r6   r7   _print_BooleanFalse1  s    z%OctaveCodePrinter._print_BooleanFalsec             C   s   t | S )N)strrR   )r3   rr   r6   r6   r7   _print_bool5  s    zOctaveCodePrinter._print_boolc                sz    j  jfdkrdS  j dks( jdkr8d j  jf S  j  jfdkrV d S dd fdd	t j D  S )
N)r   r   z[]r   zzeros(%s, %s))rM   rM   z[%s]z; c             3   s2   | ]*}d  fdd |ddf D V  qdS ) c                s   g | ]}  |qS r6   )rP   )rE   re   )r3   r6   r7   ra   F  s    zAOctaveCodePrinter._print_MatrixBase.<locals>.<genexpr>.<listcomp>N)r   )rE   rg   )Ar3   r6   r7   rI   F  s   z6OctaveCodePrinter._print_MatrixBase.<locals>.<genexpr>)rH   rK   rP   r   r	   )r3   r   r6   )r   r3   r7   _print_MatrixBase=  s    z#OctaveCodePrinter._print_MatrixBasec             C   sx   ddl m} | }|dd |D g}|dd |D g}|dd |D g}d| || || ||j|jf S )Nr   )Matrixc             S   s   g | ]}|d  d qS )r   rM   r6   )rE   kr6   r6   r7   ra   N  s    z9OctaveCodePrinter._print_SparseMatrix.<locals>.<listcomp>c             S   s   g | ]}|d  d  qS )rM   r6   )rE   r   r6   r6   r7   ra   O  s    c             S   s   g | ]}|d  qS )   r6   )rE   r   r6   r6   r7   ra   P  s    zsparse(%s, %s, %s, %s, %s))Zsympy.matricesr   Zcol_listrP   rH   rK   )r3   r   r   LIJZAIJr6   r6   r7   _print_SparseMatrixJ  s    z%OctaveCodePrinter._print_SparseMatrixc             C   s.   | j |jtd ddd|jd |jd f  S )NZAtomT)strictz(%s, %s)rM   )r^   parentr   rG   rF   )r3   rr   r6   r6   r7   _print_MatrixElementc  s    z&OctaveCodePrinter._print_MatrixElementc                sL    fdd}  |jd ||j|jjd  d ||j|jjd  d S )Nc                s   | d d }| d }| d }  |}||kr2dn  |}|dkrr|dkrX||krXdS ||krd|S |d | S nd|  ||fS d S )Nr   rM   r   rN   :)rP   r   )r_   ZlimlhstepZlstrZhstr)r3   r6   r7   strslicei  s    
z6OctaveCodePrinter._print_MatrixSlice.<locals>.strslice(r   z, rM   ))rP   r   ZrowslicerJ   Zcolslice)r3   rr   r   r6   )r3   r7   _print_MatrixSliceh  s    z$OctaveCodePrinter._print_MatrixSlicec                s0    fdd|j D }d |jjd|f S )Nc                s   g | ]}  |qS r6   )rP   )rE   rG   )r3   r6   r7   ra   ~  s    z4OctaveCodePrinter._print_Indexed.<locals>.<listcomp>z%s(%s)z, )rU   rP   rk   rQ   r   )r3   rr   Zindsr6   )r3   r7   _print_Indexed}  s    z OctaveCodePrinter._print_Indexedc             C   s   |  |jS )N)rP   rQ   )r3   rr   r6   r6   r7   
_print_Idx  s    zOctaveCodePrinter._print_Idxc                s&   t d  dt fdd|jD  S )Nr   zdouble(%s == %s)c             3   s   | ]} | V  qd S )N)r^   )rE   r_   )r`   r3   r6   r7   rI     s   z:OctaveCodePrinter._print_KroneckerDelta.<locals>.<genexpr>)r   tuplerl   )r3   rr   r6   )r`   r3   r7   _print_KroneckerDelta  s    z'OctaveCodePrinter._print_KroneckerDeltac                sP   |j }t|dkr,|d |d kr,|d g}d fdd|D }d| d S )	Nr   r   rM   z, c             3   s   | ]}  |V  qd S )N)rP   )rE   n)r3   r6   r7   rI     s    z4OctaveCodePrinter._print_Identity.<locals>.<genexpr>zeye(r   )rJ   rc   r   )r3   rr   rJ   sr6   )r3   r7   _print_Identity  s
    
z!OctaveCodePrinter._print_Identityc             C   s$   d|  |jd |  |jd f S )Nzgammainc(%s, %s, 'upper')rM   r   )rP   rl   )r3   rr   r6   r6   r7   _print_uppergamma  s    z#OctaveCodePrinter._print_uppergammac             C   s$   d|  |jd |  |jd f S )Nzgammainc(%s, %s, 'lower')rM   r   )rP   rl   )r3   rr   r6   r6   r7   _print_lowergamma  s    z#OctaveCodePrinter._print_lowergammac             C   s   d|  |jd tj  S )Nzsinc(%s)r   )rP   rl   r   Pi)r3   rr   r6   r6   r7   _print_sinc  s    zOctaveCodePrinter._print_sincc             C   s   d|  |j|  |jf S )Nzbesselh(%s, 1, %s))rP   r&   argument)r3   rr   r6   r6   r7   _print_hankel1  s    z OctaveCodePrinter._print_hankel1c             C   s   d|  |j|  |jf S )Nzbesselh(%s, 2, %s))rP   r&   r   )r3   rr   r6   r6   r7   _print_hankel2  s    z OctaveCodePrinter._print_hankel2c             C   sD   ddl m}m} |j}|tjd|  ||jtj | }| |S )Nr   )sqrtr   r   )	sympy.functionsr   r   r   r   r   r&   rz   rP   )r3   rr   r   r   r_   expr2r6   r6   r7   	_print_jn  s    $zOctaveCodePrinter._print_jnc             C   sD   ddl m}m} |j}|tjd|  ||jtj | }| |S )Nr   )r   r   r   )	r   r   r   r   r   r   r&   rz   rP   )r3   rr   r   r   r_   r   r6   r6   r7   	_print_yn  s    $zOctaveCodePrinter._print_ync             C   s   d|  |jd  S )Nzairy(0, %s)r   )rP   rl   )r3   rr   r6   r6   r7   _print_airyai  s    zOctaveCodePrinter._print_airyaic             C   s   d|  |jd  S )Nzairy(1, %s)r   )rP   rl   )r3   rr   r6   r6   r7   _print_airyaiprime  s    z$OctaveCodePrinter._print_airyaiprimec             C   s   d|  |jd  S )Nzairy(2, %s)r   )rP   rl   )r3   rr   r6   r6   r7   _print_airybi  s    zOctaveCodePrinter._print_airybic             C   s   d|  |jd  S )Nzairy(3, %s)r   )rP   rl   )r3   rr   r6   r6   r7   _print_airybiprime  s    z$OctaveCodePrinter._print_airybiprimec             C   s*   |j \}}|dkr| |S d| | S )NrM   z
expint(%s))rl   _print_not_supportedrP   )r3   rr   Zmur_   r6   r6   r7   _print_expint  s    

zOctaveCodePrinter._print_expintc                sD   t |jdkstdj j|jj d fddt|jD dS )Nr   z{name}({args})z, c                s   g | ]}  |qS r6   )rP   )rE   r_   )r3   r6   r7   ra     s    z?OctaveCodePrinter._one_or_two_reversed_args.<locals>.<listcomp>)r?   rl   )	rc   rl   AssertionErrorr<   r/   r5   __name__r   reversed)r3   rr   r6   )r3   r7   _one_or_two_reversed_args  s    z+OctaveCodePrinter._one_or_two_reversed_argsc          	   C   s<   dj | j|jj | |jd | |j|jdd   dS )Nz{name}({arg1}, {arg2})r   rM   )r?   Zarg1Zarg2)r<   r/   r5   r   rP   rl   func)r3   rr   r6   r6   r7   _nested_binary_math_func  s    z*OctaveCodePrinter._nested_binary_math_funcc       
         s0  |j d jdkrtdg } jd r~ fdd|j d d D }d |j d j }d|| d	t|  }d
| d	 S xt|j D ]\}\}}|dkr|	d |  n:|t|j d kr|dkr|	d n|	d |   |}	|	|	 |t|j d kr|	d qW d|S d S )Nr]   TzAll Piecewise expressions must contain an (expr, True) statement to be used as a default condition. Without one, the generated expression may not evaluate to anything under some condition.r)   c                s(   g | ] \}}d   | |qS )z({0}).*({1}) + (~({0})).*()r<   rP   )rE   rt   rs   )r3   r6   r7   ra     s   z6OctaveCodePrinter._print_Piecewise.<locals>.<listcomp>z%sz ...
r   r   r   zif (%s)rM   elsezelseif (%s)rN   
)
rl   Zcond
ValueErrorr   rP   rr   r   rc   	enumeraterT   )
r3   rr   rC   ZecpairsZelastZpwrG   rt   rs   Zcode0r6   )r3   r7   _print_Piecewise  s(    



z"OctaveCodePrinter._print_Piecewisec             C   s0   t |jdkr"d| |jd  S | |S d S )NrM   zzeta(%s)r   )rc   rl   rP   r   )r3   rr   r6   r6   r7   _print_zeta  s    zOctaveCodePrinter._print_zetac       
         s   t |tr$| |d}d|S d}dd dd |D }fdd|D } fd	d|D }g }d
}x^t|D ]R\}}	|	dks|	dkr||	 qt||| 8 }|d|| |	f  ||| 7 }qtW |S )z0Accepts a string of code or a list of code linesTr[   z  )z
^function z^if z^elseif z^else$z^for )z^end$z^elseif z^else$c             S   s   g | ]}| d qS )z 	)lstrip)rE   liner6   r6   r7   ra   "  s    z1OctaveCodePrinter.indent_code.<locals>.<listcomp>c                s&   g | ] t t fd dD qS )c                s   g | ]}t | qS r6   )r   )rE   r   )r   r6   r7   ra   $  s    z<OctaveCodePrinter.indent_code.<locals>.<listcomp>.<listcomp>)intany)rE   )	inc_regex)r   r7   ra   $  s   c                s&   g | ] t t fd dD qS )c                s   g | ]}t | qS r6   )r   )rE   r   )r   r6   r7   ra   &  s    z<OctaveCodePrinter.indent_code.<locals>.<listcomp>.<listcomp>)r   r   )rE   )	dec_regex)r   r7   ra   &  s   r   r   z%s%s)rm   r   rB   
splitlinesr   r   rT   )
r3   codeZ
code_linesZtabZincreaseZdecreaseZprettylevelr   r   r6   )r   r   r7   rB     s*    




zOctaveCodePrinter.indent_code)Fr   
__module____qualname____doc__ZprintmethodZlanguageZ
_operatorsZ_default_settingsr+   r:   r;   r>   rA   rD   rL   rY   rw   r|   r}   r~   r   r   r   r   r   r   r   r   Z_print_tupleZ_print_Tupler   r   r   r   r   Z_print_MatrixZ_print_DenseMatrixZ_print_MutableDenseMatrixZ_print_ImmutableMatrixZ_print_ImmutableDenseMatrixZ_print_MutableSparseMatrixZ_print_ImmutableSparseMatrixr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Z_print_DiracDeltaZ_print_LambertWr   Z
_print_MaxZ
_print_Minr   r   rB   __classcell__r6   r6   )r5   r7   r   >   s   K%r   Nc             K   s   t || |S )a  Converts `expr` to a string of Octave (or Matlab) code.

    The string uses a subset of the Octave language for Matlab compatibility.

    Parameters
    ==========

    expr : Expr
        A sympy expression to be converted.
    assign_to : optional
        When given, the argument is used as the name of the variable to which
        the expression is assigned.  Can be a string, ``Symbol``,
        ``MatrixSymbol``, or ``Indexed`` type.  This can be helpful for
        expressions that generate multi-line statements.
    precision : integer, optional
        The precision for numbers such as pi  [default=16].
    user_functions : dict, optional
        A dictionary where keys are ``FunctionClass`` instances and values are
        their string representations.  Alternatively, the dictionary value can
        be a list of tuples i.e. [(argument_test, cfunction_string)].  See
        below for examples.
    human : bool, optional
        If True, the result is a single string that may contain some constant
        declarations for the number symbols.  If False, the same information is
        returned in a tuple of (symbols_to_declare, not_supported_functions,
        code_text).  [default=True].
    contract: bool, optional
        If True, ``Indexed`` instances are assumed to obey tensor contraction
        rules and the corresponding nested loops over indices are generated.
        Setting contract=False will not generate loops, instead the user is
        responsible to provide values for the indices in the code.
        [default=True].
    inline: bool, optional
        If True, we try to create single-statement code instead of multiple
        statements.  [default=True].

    Examples
    ========

    >>> from sympy import octave_code, symbols, sin, pi
    >>> x = symbols('x')
    >>> octave_code(sin(x).series(x).removeO())
    'x.^5/120 - x.^3/6 + x'

    >>> from sympy import Rational, ceiling, Abs
    >>> 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, ccode
    >>> 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));'
    )r   Zdoprint)rr   Z	assign_tor4   r6   r6   r7   octave_code5  s     	r   c             K   s   t t| f| dS )zPrints the Octave (or Matlab) representation of the given expression.

    See `octave_code` for the meaning of the optional arguments.
    N)printr   )rr   r4   r6   r6   r7   print_octave_code  s    r   )N)r   Z
__future__r   r   Z
sympy.corer   r   r   r   Zsympy.core.compatibilityr   r	   Zsympy.core.mulr
   Zsympy.codegen.astr   Zsympy.printing.codeprinterr   Zsympy.printing.precedencer   r   r   r   r.   r1   r   r   r   r6   r6   r6   r7   <module>   s\   


   z
 