B
    [                 @   s`   d Z ddlmZmZ ddlmZmZmZ ddlm	Z	 ddl
mZ ddlmZ dd Zd	d
 ZdS )zS Optimizations of the expression tree representation for better CSE
opportunities.
    )print_functiondivision)AddBasicMul)preorder_traversal)S)default_sort_keyc             C   s   dd |  tD }|jtd | tdd |D } t| tri }xDt|  ttdD ].}||ksn|	 rZt
tjtj| g||< qZW | |} | S )zD Replace y - x with -(x - y) if -1 can be extracted from y - x.
    c             S   s   g | ]}|  r|qS  )could_extract_minus_sign).0ar
   r
   6lib/python3.7/site-packages/sympy/simplify/cse_opts.py
<listcomp>   s    zsub_pre.<locals>.<listcomp>)keyc             s   s$   | ]}|t tj| gfV  qd S )N)r   
_from_argsr   NegativeOne)r   r   r
   r
   r   	<genexpr>   s    zsub_pre.<locals>.<genexpr>)Zatomsr   sortr	   xreplacedict
isinstancer   sortedr   r   r   r   Oner   )eZrepsZnegsr   r
   r
   r   sub_pre   s    

r   c          	   C   s   g }xZt | D ]N}t|tr|jd tjkr|jd tjkr||t|jdd  f qW x|D ]\}}| 	||i} qfW | S )z Replace 1*-1*x with -x.
    r         N)
r   r   r   argsr   r   r   appendr   r   )r   ZreplacementsZnodeZreplacementr
   r
   r   sub_post    s    
 $r    N)__doc__Z
__future__r   r   Z
sympy.corer   r   r   Zsympy.core.basicr   Zsympy.core.singletonr   Zsympy.utilities.iterablesr	   r   r    r
   r
   r
   r   <module>   s   