o
    à8VaU  ã                   @   sP   d 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.
é    )ÚAddÚBasicÚMul)Úpreorder_traversal)ÚS)Údefault_sort_keyc                 C   sÔ   dd„ |   t¡D ƒ}i }tƒ }|D ]}| }|jr| |¡ qt tj|g¡||< q|  	|¡} t
| tƒrhi }t|   t¡tdD ]#}||v rFq?||v rQ|| ||< q?| ¡ rbt tjtj| g¡||< q?|  	|¡} | 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   ú9/usr/lib/python3/dist-packages/sympy/simplify/cse_opts.pyÚ
<listcomp>   s    zsub_pre.<locals>.<listcomp>)Úkey)Zatomsr   ÚsetZis_MulÚaddr   Ú
_from_argsr   ÚNegativeOneÚxreplaceÚ
isinstancer   Úsortedr   r	   ÚOne)ÚeZaddsZrepsÚignorer   ZnaZnegsr   r   r   Úsub_pre
   s,   


€
r   c              	   C   s|   g }t | ƒD ]'}t|tƒr-|jd tju r-|jd tju r-| |t |jdd… ¡ f¡ q|D ]\}}|  	||i¡} q0| 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
sympy.corer   r   r   Zsympy.core.basicr   Zsympy.core.singletonr   Zsympy.utilities.iterablesr   r   r   r   r   r   r   Ú<module>   s    