
    EdR                     N    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 Zd ZdS )	zS Optimizations of the expression tree representation for better CSE
opportunities.
    )AddBasicMul)S)default_sort_key)preorder_traversalc                    d |                      t                    D             }i }t                      }|D ]E}| }|j        r|                    |           "t          j        t          j        |g          ||<   F| 	                    |          } t          | t                    ri }t          |                      t                    t                    D ]Z}||v r||v r||         ||<   |                                r/t          j        t          j        t          j        | g          ||<   [| 	                    |          } | S )zD Replace y - x with -(x - y) if -1 can be extracted from y - x.
    c                 :    g | ]}|                                 |S  )could_extract_minus_sign).0as     7lib/python3.11/site-packages/sympy/simplify/cse_opts.py
<listcomp>zsub_pre.<locals>.<listcomp>   s)    DDD!q'A'A'C'CDADDD    )key)atomsr   setis_Muladdr   
_from_argsr   NegativeOnexreplace
isinstancer   sortedr   r   One)eaddsrepsignorer   nanegss          r   sub_prer#   
   sH    EDqwws||DDDDDUUF 6 6R9 	JJqMMM.!-!455Q	

4A !U 	*:;;; 	E 	EAF{ Dy Eq'Q++-- E.!%)CDDQJJtHr   c           	      h   g }t          |           D ]}t          |t                    ri|j        d         t          j        u rP|j        d         t          j        u r7|                    |t          j        |j        dd                    f           |D ]\  }}| 	                    ||i          } | S )z Replace 1*-1*x with -x.
    r         N)
r   r   r   argsr   r   r   appendr   r   )r   replacementsnodereplacements       r   sub_postr,   )   s     L"1%% H HdC   	HIaLAE!	H&*ilam&C	Hty}(E(E'E FGGG) , ,kJJk*++Hr   N)__doc__
sympy.corer   r   r   sympy.core.singletonr   sympy.core.sortingr   sympy.core.traversalr   r#   r,   r   r   r   <module>r2      s     ' & & & & & & & & & " " " " " " / / / / / / 3 3 3 3 3 3  >    r   