o
    GZŽhR  ã                   @   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)ÚS)Údefault_sort_key)Úpreorder_traversalc                 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   úF/var/www/auris/lib/python3.10/site-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   Ú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replacementsÚnodeÚreplacementr   r   r   Úsub_post)   s   
  €r!   N)Ú__doc__Z
sympy.corer   r   r   Zsympy.core.singletonr   Zsympy.core.sortingr   Zsympy.core.traversalr   r   r!   r   r   r   r   Ú<module>   s    