o
    wZhT                     @   s  d dl Z d dlZd dlZd dlZd dlmZmZmZmZm	Z	m
Z
 d dlmZmZ d dlZd dlmZ d dlmZ d dlmZ d dlmZ d dlmZmZmZ d d	l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& ddl'm(Z( erd dl)m*Z* e	dedZ+edZ,g dZ-dejde.fddZ/deee, ge+f deee, ge
e+ej0f f fddZ1dee. dee. dee. fdd Z2d!ej3d"ej3dej3fd#d$Z4G d%d& d&ej5Z6G d'd( d(ej5Z7G d)d* d*ej5Z8G d+d, d,ej5Z9G d-d. d.ej5Z:G d/d0 d0e6Z;G d1d2 d2ej5Z<G d3d4 d4ej5Z=G d5d6 d6ej5Z>G d7d8 d8ej5Z?G d9d: d:ej5Z@G d;d< d<eeZAG d=d> d>eAeZBG d?d@ d@eAeZCdAdB ZDdCdD ZEG dEdF dFej5ZFG dGdH dHej5ZGG dIdJ dJej5ZHG dKdL dLej5ZIG dMdN dNej5ZJG dOdP dPej5ZKG dQdR dRej5ZLG dSdT dTej5ZMG dUdV dVej5ZNG dWdX dXej5ZOG dYdZ dZej5ZPd[d\ ZQeQd]ZReQd^ZSeQd_ZTeQd`ZUeQdaZVeQdbZWeQdcZXeQddZYeQdeZZeQdfZ[eQdgZ\eQdhZ]eQdiZ^eQdjZ_dkdl Z`e`dmdnZae`dodpZbdS )q    N)CallableOptionalSupportsFloatTYPE_CHECKINGTypeVarUnion)TypeVarTupleUnpack)Ssympify)Expr)Application)_torf	fuzzy_andfuzzy_or)equal_valued)	LatticeOpShortCircuit)ordered)walk)
PRECEDENCE)sift   )int_oo)Iterable_T)bound_Ts)FloorDivModularIndexingWhere	PythonModModCleanDiv	CeilToInt
FloorToIntCeilDiv
IntTrueDivFloatTrueDivLShiftRShift!IsNonOverlappingAndDenseIndicatorTruncToFloat
TruncToInt
RoundToIntRoundDecimalToFloatFloatPowPowByNaturalIdentityexprreturnc                 C   s@   | j ot| jdko| jd jo| jd jo| jd | jd uS )N   r   r   )Zis_Addlen_argsZ	is_symbol)r5    r:   K/var/www/auris/lib/python3.10/site-packages/torch/utils/_sympy/functions.py_is_symbols_binary_summationY   s   

r<   fc                    s2   t  dtt dtttjf f fdd}|S )Nargsr6   c                     s8    |  }t dd | D rt|tjstt|}|S )Nc                 s       | ]	}t |tjV  qd S N)
isinstancesympyFloat.0ar:   r:   r;   	<genexpr>j       z-_keep_float.<locals>.inner.<locals>.<genexpr>)anyrA   rB   rC   float)r>   rr=   r:   r;   innerg   s   z_keep_float.<locals>.inner)	functoolswrapsr	   r   r   r   rB   rC   )r=   rM   r:   rL   r;   _keep_floatd   s   &rP   xyc                 C   s   d | |fv rd S | |kS r@   r:   )rQ   rR   r:   r:   r;   fuzzy_eqs   s   rS   pqc                    s   dt jdtfdd dt jdtf fdd}t|| ||}| | || } }ttt jjt j	| }t j|}|D ]t
fdd	|D rO| }q>|S )
a  
    Fast path for sympy.gcd, using a simple factoring strategy.

    We try to rewrite p and q in the form n*e*p1 + n*e*p2 and n*e*q0,
    where n is the greatest common integer factor and e is the largest
    syntactic common factor (i.e., common sub-expression) in p and q.
    Then the gcd returned is n*e, cancelling which we would be left with
    p1 + p2 and q0.

    Note that further factoring of p1 + p2 and q0 might be possible with
    sympy.factor (which uses domain-specific theories). E.g., we are unable
    to find that x*y + x + y + 1 is divisible by x + 1. More generally,
    when q is of the form q1 + q2 (instead of being already factored) it
    might be necessary to fall back on sympy.gcd.
    rQ   r6   c                 S   s    dd t j| D }t|S )Nc                 S   s(   g | ]}t |ttjfrtt|qS r:   )rA   intrB   IntegerabsrE   argr:   r:   r;   
<listcomp>   s    
zDsimple_floordiv_gcd.<locals>.integer_coefficient.<locals>.<listcomp>)rB   Mul	make_argsmathprod)rQ   Zinteger_coefficientsr:   r:   r;   integer_coefficient   s   

z0simple_floordiv_gcd.<locals>.integer_coefficientr5   c                    s    t  tj| }ttj|S r@   )maprB   Addr]   rN   reducer^   gcd)r5   Zinteger_factors)r`   r:   r;   integer_factor   s   z+simple_floordiv_gcd.<locals>.integer_factorc                 3   s    | ]} |v V  qd S r@   r:   )rE   Z
base_split)rQ   r:   r;   rG          z&simple_floordiv_gcd.<locals>.<genexpr>)rB   BasicrV   r^   rd   listra   r\   r]   rb   all)rT   rU   re   rd   Zbase_splitsZdivisor_splitr:   )r`   rQ   r;   simple_floordiv_gcdy   s   rj   c                	   @   s   e Zd ZU dZdZeedf ed< dZeed< dZ	e
ed< ed	ejfd
dZed	ejfddZdejjd	efddZedejdejd	eejdf fddZdd ZdS )r   a  
    We maintain this so that:
    1. We can use divisibility guards to simplify FloorDiv(a, b) to a / b.
    2. Printing out the expression is nicer (compared to say, representing a//b as (a - a % b) / b)

    NB: This is Python-style floor division, round to -Inf
    r7   .nargs#   
precedenceT
is_integerr6   c                 C   
   | j d S Nr   r>   selfr:   r:   r;   base      
zFloorDiv.basec                 C   rp   Nr   rr   rs   r:   r:   r;   divisor   rv   zFloorDiv.divisorprinterc                 C   >   | | jtd d }| | jtd d }d| d| dS )NAtom      ?(z//)Zparenthesizeru   r   rx   rt   ry   ru   rx   r:   r:   r;   	_sympystr      zFloorDiv._sympystrru   rx   Nc           	      C   sr  |j rtd|tt tjtj fv r"|tt tjtj fv r"tjS |tju s,|tju r/tjS |j r6tjjS |jr@t	|dr@|S |jrNt	|drNt
|dS t|tjrt|tjr|tt tjtj fv sr|tt tjtj fv rt|t| }|tjkrtS |tj krt S t|rtjS tt|S t|tjrt|tjrtt|t| S t|trt|jd |jd | S t|tjrd}g }tj|D ]}|| }|jr|| ||7 }qt|dkrt|tj|ddi || S z1t||}t	|drt|tjrt||}t	|ds*tt|| t|| W S W d S  tjy8   Y d S w )Ndivision by zeror   r   evaluateF)is_zeroZeroDivisionErrorr   rB   oonanr
   Zeroro   r   r\   rA   NumberrJ   r^   infisnanrW   floorrV   r   r>   rb   r]   appendr8   rj   rd   simplifyPolynomialError)	clsru   rx   rK   Z	quotientsZtermstermZquotientrd   r:   r:   r;   eval   s~   






zFloorDiv.evalc                 C   rz   )Nr{   r|   zfloor(/r~   r   r   r:   r:   r;   _ccode&  r   zFloorDiv._ccode)__name__
__module____qualname____doc__rl   tuplerV   __annotations__rn   ro   boolpropertyrB   rg   ru   rx   ZprintingZ
StrPrinterstrr   classmethodrW   r   r   r   r:   r:   r:   r;   r      s&   
 Rr   c                
   @   s   e Zd ZU dZdZeedf ed< dZe	ed< dZ
eed< ed	ejd
ejdejdeej fddZdee	 fddZdee	 fddZdS )r    zK
    ModularIndexing(a, b, c) => (a // b) % c where % is the C modulus
       .rl   Tro   rm   rn   ru   rx   modulusr6   c                 C   sz  |dks|dkrt jjS t|t jr$t|t jr$t|t jr$|| | S z!|dkrDt ||}|dkrDtt || t || |W S W n
 t jyO   Y nw t|t j	rg }d}|j
D ]7}t ||| || krt|t jru|dk st|t jrt|j
d t jr|j
d dk rd} n|| q]t|t|j
kr|rtt|||S t|trt|j
d |j
d | |S d S )Nr   r   TF)rB   r
   r   rA   rW   rd   r    r   r   rb   r>   r\   r   r8   sumr   )r   ru   rx   r   rd   Z	new_termsZall_positiver   r:   r:   r;   r   5  sT   






zModularIndexing.evalc                 C       | j d d \}}t|j|jS Nr7   )r>   rS   is_nonnegativert   rT   rU   r:   r:   r;   _eval_is_nonnegativej     z$ModularIndexing._eval_is_nonnegativec                 C   r   r   )r>   rS   is_positiver   r:   r:   r;   _eval_is_positiven  r   z!ModularIndexing._eval_is_positiveN)r   r   r   r   rl   r   rV   r   ro   r   rn   r   rB   rW   r   rg   r   r   r   r:   r:   r:   r;   r    ,  s"   
 4r    c                
   @   s   e Zd ZU dZdZeedf ed< dZeed< de	e
 fdd	Zde	e
 fd
dZde	e
 fddZedejdejdejde	ej fddZdS )r!   z#
    Good ol' ternary operator
    r   .rl   rm   rn   r6   c                 C       | j d jr| j d jrdS d S Nr   r7   Tr>   ro   rs   r:   r:   r;   _eval_is_integer{      zWhere._eval_is_integerc                 C   r   r   )r>   r   rs   r:   r:   r;   r   ~  s   

zWhere._eval_is_nonnegativec                 C   r   r   r>   r   rs   r:   r:   r;   r     r   zWhere._eval_is_positivecrT   rU   c                 C   s    |t jkr|S |t jkr|S d S r@   )rB   truefalse)r   r   rT   rU   r:   r:   r;   r     s
   

z
Where.evalN)r   r   r   r   rl   r   rV   r   rn   r   r   r   r   r   r   rB   rg   r   r:   r:   r:   r;   r!   s  s"   
 r!   c                   @   s   e Zd ZU dZeedf ed< dZeed< dZe	ed< e
dejd	ejd
eej fddZd
ee	 fddZd
ee	 fddZdS )r"   rk   .rl   rm   rn   Tro   rT   rU   r6   c                 C   s   |j rtd|tju s||| fv s|dkrtjS |jr$|jr$|| S |jr7|dkr7|jr1tjS |jr7tjS || }|jrAtjS ||k }|j	rQt
|rQ|jrQ|S t||dkr\tjS d S )NModulo by zeror   r7   r   )r   r   r
   r   	is_Numberis_evenis_oddOnero   
is_Booleanr   r   rB   r#   r   rT   rU   rK   lessr:   r:   r;   r     s(    zPythonMod.evalc                 C      | j d jrdS d S Nr   Tr   rs   r:   r:   r;   r        zPythonMod._eval_is_nonnegativec                 C   r   r   )r>   is_negativers   r:   r:   r;   _eval_is_nonpositive  r   zPythonMod._eval_is_nonpositiveN)r   r   r   rl   r   rV   r   rn   ro   r   r   rB   r   r   r   r   r   r:   r:   r:   r;   r"     s   
 "-r"   c                   @   s2   e Zd ZU dZdZeed< dZdZe	dd Z
dS )r#   rk   rm   rn   Tc                 C   s   |j rtd|tju s||| fv s|dkrtjS |jr4|jr4|dks(J ||dks0J ||| S |jrG|dkrG|jrAtjS |jrGtjS || }|jrQtjS ||k }|j	rat
|rc|jre|S d S d S d S )Nr   r   r   r7   )r   r   r
   r   r   r   r   r   ro   r   r   r   r   r:   r:   r;   r     s(   	 zMod.evalN)r   r   r   rl   rn   rV   r   ro   r   r   r   r:   r:   r:   r;   r#     s   
 r#   c                   @   s   e Zd ZdZdS )r$   zZ
    Div where we can assume no rounding.
    This is to enable future optimizations.
    N)r   r   r   r   r:   r:   r:   r;   r$     s    r$   c                   @      e Zd ZdZedd ZdS )r%   Tc                 C   N   |t jtfv r	tS |t j t fv rt S t|t jr%t tt|S d S r@   )	rB   r   r   rA   r   rW   r^   ceilrJ   r   numberr:   r:   r;   r        zCeilToInt.evalNr   r   r   ro   r   r   r:   r:   r:   r;   r%         r%   c                   @   r   )r&   Tc                 C   s\   |t jtfv r	tS |t j tfv rt S t|t jr|S t|t jr,t tt|S d S r@   )	rB   r   r   rA   rW   r   r^   r   rJ   r   r:   r:   r;   r     s   zFloorToInt.evalNr   r:   r:   r:   r;   r&     r   r&   c                   @   s   e Zd ZdZdZdd ZdS )r'   z.
    Div used in indexing that rounds up.
    Tc                 C   s@   t |}t |}t |||krt||S t||d  |S rw   )rB   r   rd   r$   r   r   ru   rx   r:   r:   r;   __new__1  s
   


zCeilDiv.__new__N)r   r   r   r   ro   r   r:   r:   r:   r;   r'   *  s    r'   c                   @   r   )r*   Tc                 C   s   |dk rt d|d|  S Nr   znegative shift countr7   )
ValueErrorr   ru   shiftr:   r:   r;   r   =  s   zLShift.evalNr   r:   r:   r:   r;   r*   :  r   r*   c                   @   r   )r+   Tc                 C   s   |dk rt dt|d| S r   )r   r   r   r:   r:   r;   r   G  s   zRShift.evalNr   r:   r:   r:   r;   r+   D  r   r+   c                   @   s^  e Zd Zdd Zedeeejj	j
  fddZe	d+deeejj	j
  deeejj	j
  fdd	Zed
d Zedd Ze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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S ),
MinMaxBasec                 O   s   ddl m} |d|j}dd |D }|sd n| |}|rKz	t| |}W n ty4   | j Y S w |d u rK| j	|fi |}| j
|fi |}t|}|sT| jS t|dkr`t| S tj| gt|R i |}||_||_|S )Nr   )global_parametersr   c                 s   s    | ]}t |V  qd S r@   r   rY   r:   r:   r;   rG   S  rf   z%MinMaxBase.__new__.<locals>.<genexpr>r   )Zsympy.core.parametersr   popr   "_satisfy_unique_summations_symbols	frozenset_new_args_filterr   zero_collapse_arguments_find_localzerosidentityr8   rh   r   r   r   Z_argsetunique_summations_symbols)r   Zoriginal_argsassumptionsr   r   r>   r   objr:   r:   r;   r   O  s2   
zMinMaxBase.__new__r6   c                 C   s   t |dkrdS t|d tr|d |d fn|d |d f\}}t|s'dS t|r0| |S t|trFt|dd}|durF| |g|S dS )a  
        One common case in some models is building expressions of the form
        max(max(max(a+b...), c+d), e+f) which is simplified to max(a+b, c+d, e+f, ...).
        For such expressions, we call the Max constructor X times (once for each nested
        max) and the expression gets flattened.

        An expensive cost in constructing those expressions is running _collapse_arguments
        and _find_localzeros. However, those two optimizations are unnecessary when the args
        to max are all of the form a+b, c+d, ..etc where each term uses a unique set of symbols.

        This function is used to detect such properties of the expressions we are building
        and if so inform that we do not need to run those optimizations. To detect those,
        we store a property in the expression that tells that this expression is a min/max
        operation over terms that use unique symbols "unique_summations_symbols". This property
        also memoize the set of symbols used in all the terms to make it faster to detect this
        property inductively.

        When we apply max to add a new term, all we need to do is check if the new term uses
        unique symbols (with respect to existing terms and itself).
        Example:
        t = Max(a+b, c+d) ==> satisfies the property
        Max(t, h+j)       ==> h,j not in [a,b,c,d] => satisfy the property.

        The function returns None if the new expression does not satisfy the unique_summations_symbols
        property. Otherwise, it returns a new set of unique symbols.
        r7   Nr   r   r   )r8   rA   r   r<   _unique_symbolsgetattr)r   r>   lhsrhsZlhs_unique_summations_symbolsr:   r:   r;   r   |  s"   

z-MinMaxBase._satisfy_unique_summations_symbolsNinitial_setc                 C   s`   |du rt  n|}|D ]"}| D ]}t|tjjjs  dS ||v r'  dS || qq|S )z
        Return seen_symbols if all atoms in all args are all unique symbols,
        else returns None. initial_set can be used to represent initial value for seen_symbols
        N)setZatomsrA   rB   coresymbolSymboladd)r   r>   r   Zseen_symbolsrZ   elementr:   r:   r;   r     s   zMinMaxBase._unique_symbolsc                    s8  |s|S t t|}tu rtnt|d jrg g f }\}}|D ]}t|ttD ]}|jd jr<|t|t 	| q*q"tj
}|D ]}|jd }|jrU||k dkrU|}qCtj
}	|D ]}|jd }|jrm||	kdkrm|}	q[tu r|D ]}
|
js{ n	|
|k dkr|
}qtntkr|D ]}
|
js n	|
|	kdkr|
}	qd}tu r|tj
krt|}n	|	tj
krt|	}|durtt|D ]#}||  t r jd }tkr||kn||k dkrj
||< qfddt|D ]\}  fdd||d d D ||d d< qfd	d
}t|dkr||}|S )a}  Remove redundant args.

        Examples
        ========

        >>> from sympy import Min, Max
        >>> from sympy.abc import a, b, c, d, e

        Any arg in parent that appears in any
        parent-like function in any of the flat args
        of parent can be removed from that sub-arg:

        >>> Min(a, Max(b, Min(a, c, d)))
        Min(a, Max(b, Min(c, d)))

        If the arg of parent appears in an opposite-than parent
        function in any of the flat args of parent that function
        can be replaced with the arg:

        >>> Min(a, Max(b, Min(c, d, Max(a, e))))
        Min(a, Max(b, Min(a, c, d)))
        r   TNc                    sr   t | ttfs	| S  | jv }|s!| j fdd| jD ddiS t | r7| j fdd| jD ddiS  S )Nc                       g | ]}| qS r:   r:   rE   irF   dor:   r;   r[   #      z>MinMaxBase._collapse_arguments.<locals>.do.<locals>.<listcomp>r   Fc                    s   g | ]}| kr| qS r:   r:   r   r   r:   r;   r[   %  s    )rA   MinMaxr>   func)airF   Zcond)r   r   )rF   r;   r     s   
"
"z*MinMaxBase._collapse_arguments.<locals>.doc                    r   r:   r:   )rE   r   r   r:   r;   r[   )  r   z2MinMaxBase._collapse_arguments.<locals>.<listcomp>r   c           	         s   fdd}t | |dd\}}|s| S dd |D }tj|   s#| S t } fdd|D }t|rGfdd|D }||d	d
i |d	d
i}||g S )Nc                    s
   t |  S r@   )rA   )rZ   otherr:   r;   <lambda>3  s   
 zGMinMaxBase._collapse_arguments.<locals>.factor_minmax.<locals>.<lambda>T)binaryc                 S   s   g | ]}t |jqS r:   )r   r>   rY   r:   r:   r;   r[   9  r   zIMinMaxBase._collapse_arguments.<locals>.factor_minmax.<locals>.<listcomp>c                    s   g | ]}|  qS r:   r:   )rE   Zarg_setcommonr:   r;   r[   ?      c                    s   g | ]	} |d diqS )r   Fr:   )rE   sr   r:   r;   r[   D  s    r   F)r   r   intersectionrh   ri   r   )	r>   Zis_otherZ
other_argsremaining_argsZarg_setsZnew_other_argsZarg_sets_diffZother_args_diffZother_args_factored)r   r   r   r;   factor_minmax2  s   

z5MinMaxBase._collapse_arguments.<locals>.factor_minmax)rh   r   r   r   	is_numberr   r>   is_comparablerA   r   r   ranger8   	enumerate)r   r>   r   ZsiftedZminsZmaxsr   vZsmallbigrZ   TZa0r   r:   )rF   r   r   r   r;   r     s   








.	zMinMaxBase._collapse_argumentsc                 c   s|    |D ]8}t |tr|jdu s|jr|jstd| d|| jkr&t||| jkr,q|j	| kr8|j
E dH  q|V  qdS )z
        Generator filtering args.

        first standard filter, for cls.zero and cls.identity.
        Also reshape ``Max(a, Max(b, c))`` to ``Max(a, b, c)``,
        and check arguments for comparability
        FzThe argument 'z' is not comparable.N)rA   r   is_extended_realr   r   r   r   r   r   r   r>   )r   Zarg_sequencerZ   r:   r:   r;   r   O  s$   	



zMinMaxBase._new_args_filterc                 K   s   t  }d}|D ],}|jr.|du r|}q| tu rt||}q| tu r't||}qtd|  || q|du r:|S t|dkrC|hS t|dkrot	t
|}|dv r_|jr_| tu r\|S |hS |dkro|jro| tu rl|S |hS || |S )a  
        Sequentially allocate values to localzeros.

        When a value is identified as being more extreme than another member it
        replaces that member; if this is never true, then the value is simply
        appended to the localzeros.

        Unlike the sympy implementation, we only look for zero and one, we don't
        do generic is connected test pairwise which is slow
        Nzimpossible r   r   )g        r   )r   r   r   maxr   minAssertionErrorr   r8   nextiterr   r   )r   valuesoptionsZother_valuesZ	num_valuerZ   Zother_valuer:   r:   r;   r   j  s0   
zMinMaxBase._find_localzerosc                 C      t dd | jD S )Nc                 s       | ]}|j V  qd S r@   )Zis_algebraicr   r:   r:   r;   rG         &MinMaxBase.<lambda>.<locals>.<genexpr>r   r>   r   r:   r:   r;   r     r   zMinMaxBase.<lambda>c                 C   r  )Nc                 s   r  r@   )Zis_antihermitianr   r:   r:   r;   rG         
r  r  r  r:   r:   r;   r         c                 C   r  )Nc                 s   r  r@   )Zis_commutativer   r:   r:   r;   rG     r  r  r  r  r:   r:   r;   r     r  c                 C   r  )Nc                 s   r  r@   )Z
is_complexr   r:   r:   r;   rG     r  r  r  r  r:   r:   r;   r     r   c                 C   r  )Nc                 s   r  r@   )Zis_compositer   r:   r:   r;   rG     r  r  r  r  r:   r:   r;   r     r   c                 C   r  )Nc                 s   r  r@   )r   r   r:   r:   r;   rG     r  r  r  r  r:   r:   r;   r     r   c                 C   r  )Nc                 s   r  r@   )	is_finiter   r:   r:   r;   rG     r  r  r  r  r:   r:   r;   r     r   c                 C   r  )Nc                 s   r  r@   )Zis_hermitianr   r:   r:   r;   rG     r  r  r  r  r:   r:   r;   r     r   c                 C   r  )Nc                 s   r  r@   )Zis_imaginaryr   r:   r:   r;   rG     r  r  r  r  r:   r:   r;   r     r   c                 C   r  )Nc                 s   r  r@   )is_infiniter   r:   r:   r;   rG     r  r  r  r  r:   r:   r;   r     r   c                 C   r  )Nc                 s   r  r@   )ro   r   r:   r:   r;   rG     r  r  r  r  r:   r:   r;   r     r   c                 C   r  )Nc                 s   r  r@   )Zis_irrationalr   r:   r:   r;   rG     r  r  r  r  r:   r:   r;   r     r   c                 C   r  )Nc                 s   r  r@   r   r   r:   r:   r;   rG     r  r  r  r  r:   r:   r;   r     r   c                 C   r  )Nc                 s   r  r@   )Zis_nonintegerr   r:   r:   r;   rG     r  r  r  r  r:   r:   r;   r     r   c                 C   r  )Nc                 s   r  r@   r   r   r:   r:   r;   rG     r  r  r  r  r:   r:   r;   r     r  c                 C   r  )Nc                 s   r  r@   )Zis_nonpositiver   r:   r:   r;   rG     r  r  r  r  r:   r:   r;   r     r  c                 C   r  )Nc                 s   r  r@   )Z
is_nonzeror   r:   r:   r;   rG     r  r  r  r  r:   r:   r;   r     r   c                 C   r  )Nc                 s   r  r@   )r   r   r:   r:   r;   rG     r  r  r  r  r:   r:   r;   r     r   c                 C   r  )Nc                 s   r  r@   )Zis_polarr   r:   r:   r;   rG     r  r  r  r  r:   r:   r;   r     r   c                 C   r  )Nc                 s   r  r@   r   r   r:   r:   r;   rG     r  r  r  r  r:   r:   r;   r     r   c                 C   r  )Nc                 s   r  r@   )Zis_primer   r:   r:   r;   rG     r  r  r  r  r:   r:   r;   r     r   c                 C   r  )Nc                 s   r  r@   )Zis_rationalr   r:   r:   r;   rG     r  r  r  r  r:   r:   r;   r     r   c                 C   r  )Nc                 s   r  r@   )is_realr   r:   r:   r;   rG     r  r  r  r  r:   r:   r;   r     r   c                 C   r  )Nc                 s   r  r@   )r  r   r:   r:   r;   rG     r  r  r  r  r:   r:   r;   r     r  c                 C   r  )Nc                 s   r  r@   )Zis_transcendentalr   r:   r:   r;   rG     r  r  r  r  r:   r:   r;   r     r  c                 C   r  )Nc                 s   r  r@   )r   r   r:   r:   r;   rG     r  r  r  r  r:   r:   r;   r     r   r@   )*r   r   r   r   r   r   r   rB   r   r   r   r   r   r   r   r   Z_eval_is_algebraicZ_eval_is_antihermitianZ_eval_is_commutativeZ_eval_is_complexZ_eval_is_compositeZ_eval_is_evenZ_eval_is_finiteZ_eval_is_hermitianZ_eval_is_imaginaryZ_eval_is_infiniter   Z_eval_is_irrational_eval_is_negativeZ_eval_is_nonintegerr   r   Z_eval_is_nonzeroZ_eval_is_oddZ_eval_is_polarr   Z_eval_is_primeZ_eval_is_rational_eval_is_realZ_eval_is_extended_realZ_eval_is_transcendentalZ_eval_is_zeror:   r:   r:   r;   r   N  s\    -7
 

.r   c                   @   4   e Zd ZdZejZejZdd Z	dd Z
dd ZdS )	r   z=
    Return, if possible, the maximum value of the list.
    c                 C   r  )Nc                 s   r  r@   r  rD   r:   r:   r;   rG     r  z(Max._eval_is_positive.<locals>.<genexpr>r   r>   rs   r:   r:   r;   r     r   zMax._eval_is_positivec                 C   r  )Nc                 s   r  r@   r  rD   r:   r:   r;   rG     r  z+Max._eval_is_nonnegative.<locals>.<genexpr>r  rs   r:   r:   r;   r     r   zMax._eval_is_nonnegativec                 C   r  )Nc                 s   r  r@   r  rD   r:   r:   r;   rG     r  z(Max._eval_is_negative.<locals>.<genexpr>r   r>   rs   r:   r:   r;   r    r   zMax._eval_is_negativeN)r   r   r   r   r
   Infinityr   NegativeInfinityr   r   r   r  r:   r:   r:   r;   r         r   c                   @   r  )	r   z=
    Return, if possible, the minimum value of the list.
    c                 C   r  )Nc                 s   r  r@   r  rD   r:   r:   r;   rG     r  z(Min._eval_is_positive.<locals>.<genexpr>r  rs   r:   r:   r;   r     r   zMin._eval_is_positivec                 C   r  )Nc                 s   r  r@   r  rD   r:   r:   r;   rG     r  z+Min._eval_is_nonnegative.<locals>.<genexpr>r  rs   r:   r:   r;   r     r   zMin._eval_is_nonnegativec                 C   r  )Nc                 s   r  r@   r  rD   r:   r:   r;   rG     r  z(Min._eval_is_negative.<locals>.<genexpr>r  rs   r:   r:   r;   r    r   zMin._eval_is_negativeN)r   r   r   r   r
   r!  r   r   r   r   r   r  r:   r:   r:   r;   r     r"  r   c                 C   s4   d}| dk r|  } |d dkrdnd}|t | | S )Nr   r   r7   r   )	_safe_pow)ru   expsignr:   r:   r;   safe_pow  s
   r&  c                 C   sr   |dk rt d|dkrdS t| |d }|tu rtS || }|tjkr&tS |d dkr7|| 9 }|tjkr7tS |S )Nr   zExponent must be non-negative.r   r7   )r   r&  r   sysmaxsize)ru   exponentZhalf_expresultr:   r:   r;   r#    s   

r#  c                   @   *   e Zd ZU dZdZeed< edd ZdS )r3   T2   rn   c                 C   s   t |tjrt |tjrt||}|t tfv r|S t|S t |tjr+t||S |ttjfv r=|jr7tS |jr?tj	S d S d S r@   )
rA   rB   rW   r&  r   Powr   r   r   zoo)r   ru   r$  rK   r:   r:   r;   r     s   

zPowByNatural.evalN)	r   r   r   ro   rn   rV   r   r   r   r:   r:   r:   r;   r3     
   
 r3   c                   @   r+  )r2   T<   rn   c                 C   s6   t |tjrt |tjrtt|t| S d S d S r@   )rA   rB   r   rC   rJ   )r   ru   r$  r:   r:   r;   r   (  s   zFloatPow.evalN	r   r   r   r  rn   rV   r   r   r   r:   r:   r:   r;   r2   #  r/  r2   c                   @   r+  )r)   Trm   rn   c                 C   sD   |j rtdt|tjrt|tjr tt|t| S d S d S Nr   )r   r   rA   rB   r   rC   rJ   r   r:   r:   r;   r   >  s
   zFloatTrueDiv.evalNr1  r:   r:   r:   r;   r)   9  r/  r)   c                   @   r+  )r(   Trm   rn   c                 C   s   |j rtdt|tjr6t|tjr6|tt tjtj fv s+|tt tjtj fv r6tt|t| S t|tj	rMt|tj	rOtt
|t
| S d S d S r2  )r   r   rA   rB   r   r   r   rC   rJ   rW   rV   r   r:   r:   r;   r   W  s   

zIntTrueDiv.evalNr1  r:   r:   r:   r;   r(   R  r/  r(   c                   @   r   )r,   Tc                 G   s:  t |d dks
J t |d }|d| }||d  }ddlm} tdd |D r:|dd |D dd |D S |dkrX|d jrK|d dkrKdS |d jrX|d dk rXdS td	d |D r|dksgJ ttt||tdd
 \}}tdd |d d D r|d d d }|dd |D dd |D S d S )Nr7   r   )!eval_is_non_overlapping_and_densec                 s   r?   r@   rA   rB   rW   rD   r:   r:   r;   rG     rH   z9IsNonOverlappingAndDenseIndicator.eval.<locals>.<genexpr>c                 S      g | ]}t |qS r:   rV   rD   r:   r:   r;   r[     r   z:IsNonOverlappingAndDenseIndicator.eval.<locals>.<listcomp>r   c                 s   r?   r@   r4  rD   r:   r:   r;   rG     rH   )keyc                 s   r?   r@   r4  rD   r:   r:   r;   rG     rH   r   )*   c                 S   r5  r:   r6  rD   r:   r:   r;   r[     r   )	r8   Z%torch.fx.experimental.symbolic_shapesr3  ri   r   zipsortedoperator
itemgetter)r   r>   dimsizesstridesr3  Zs_sizesZ	s_stridesr:   r:   r;   r   t  s2   
z&IsNonOverlappingAndDenseIndicator.evalNr   r:   r:   r:   r;   r,   q  r   r,   c                   @   r   )r-   Tc                 C   s$   t |tjrttt|S d S r@   )rA   rB   r   rC   r^   truncrJ   r   r:   r:   r;   r     s   zTruncToFloat.evalNr   r   r   r  r   r   r:   r:   r:   r;   r-     r   r-   c                   @   r   )r.   Tc                 C   r   r@   )	rB   r   r   rA   r   rW   r^   r@  rJ   r   r:   r:   r;   r     r   zTruncToInt.evalNr   r:   r:   r:   r;   r.     r   r.   c                   @   r   )r/   Tc                 C   sD   |t ju rtS |t j u rt S t|t jr t tt|dS d S rq   )rB   r   r   rA   r   rW   roundrJ   r   r:   r:   r;   r     s   
zRoundToInt.evalNr   r:   r:   r:   r;   r/     r   r/   c                   @   r   )r0   Tc                 C   s8   t |tjrt |tjrttt|t|S d S d S r@   )rA   rB   r   rW   rC   rB  rJ   rV   )r   r   ndigitsr:   r:   r;   r     s   zRoundDecimal.evalNrA  r:   r:   r:   r;   r0     r   r0   c                   @   r   )r1   Tc                 C   sT   |t jt j fv r|S t|t jrt t|S |tu rt jS |t u r(t j S d S r@   )rB   r   rA   rW   rC   rV   r   r   r:   r:   r;   r     s   
zToFloat.evalNrA  r:   r:   r:   r;   r1     r   r1   c                   @   s4   e Zd ZdZdZdd Zdd Zdd Zd	d
 ZdS )r4   z4
    Prevents expansion and other optimizations
    
   c                 C   s   d| j d  dS )Nz	Identity(r   r~   rr   rs   r:   r:   r;   __repr__  s   zIdentity.__repr__c                 C      | j d jS rq   )r>   r  rs   r:   r:   r;   r       zIdentity._eval_is_realc                 C   rF  rq   r   rs   r:   r:   r;   r     rG  zIdentity._eval_is_integerc                 K   rp   rq   rr   )rt   hintsr:   r:   r;   _eval_expand_identity  rv   zIdentity._eval_expand_identityN)	r   r   r   r   rn   rE  r  r   rI  r:   r:   r:   r;   r4     s    r4   c                    s.   G  fdddt j}d  }||_||_|S )Nc                       s$   e Zd ZdZ Ze fddZdS )z+make_opaque_unary_fn.<locals>.OpaqueUnaryFna  
        Unlike the builtin sympy functions on real numbers like sympy.sqrt,
        these equivalents do not do any nontrivial reasoning besides
        constant propagation.  This helps avoid performing transformations
        that are valid for real numbers but are invalid for floating point;
        in particular, while we are willing to make optimizations that change
        numerics for Tensor compute, we are NOT willing to make optimziations
        that change numerics for size compute.
        c                    s   t |tjtjfr'zttt t|W S  ty&   tt | Y S w |tjtj tj	tj	 t
t
 fv rY|t
u r?tj}|t
 u rHtj } dkrRt|dS tt |S d S )Nlog2r7   )rA   rB   rW   rC   r   r^   rJ   OverflowErrorr   r.  r   log)r   rF   namer:   r;   r   !  s   "
z0make_opaque_unary_fn.<locals>.OpaqueUnaryFn.evalN)r   r   r   r   _torch_handler_namer   r   r:   rM  r:   r;   OpaqueUnaryFn  s
    
rP  ZOpaqueUnaryFn_)rB   Functionr   r   )rN  rP  nmr:   rM  r;   make_opaque_unary_fn  s
   %rS  sqrtcoscoshsinsinhtantanhasinacosatanr$  rL  asinhrJ  c                    sZ    dkr	t d n dkrt d ntd  G  fdddtj}d  |_|S )	Nbitwise_andZ
BitwiseAnd
bitwise_orZ	BitwiseOrzunrecognized c                       s.   e Zd ZU  ZZeed< efddZdS )z)make_opaque_bitwise_fn.<locals>.BitwiseFnrn   c                    s   |j r|j rtt ||S |j rt|rdnd}|j r&t|r#dnd}t|tjtfrEt|tjtfrEttt t|t|S d S )Nr   r   )r   r   r;  rB   rW   rA   rV   )r   rF   b)real_op_namer:   r;   r   ]  s   
z.make_opaque_bitwise_fn.<locals>.BitwiseFn.evalN)	r   r   r   rO  rn   rV   r   r   r   r:   rN  precrb  r:   r;   	BitwiseFnY  s
   
 re  Z
BitwiseFn_)r   r  rB   rQ  r   )rN  rb  re  r:   rc  r;   make_opaque_bitwise_fnQ  s   


rf  r_  and_r`  or_)crN   r^   r;  r'  typingr   r   r   r   r   r   Ztyping_extensionsr   r	   rB   r
   Z
sympy.corer   Zsympy.core.exprr   Zsympy.core.functionr   Zsympy.core.logicr   r   r   Zsympy.core.numbersr   Zsympy.core.operationsr   r   Zsympy.core.sortingr   Zsympy.core.traversalr   Zsympy.printing.precedencer   Zsympy.utilities.iterablesr   numbersr   collections.abcr   r   r   __all__r   r<   rC   rP   rS   rg   rj   rQ  r   r    r!   r"   r#   r$   r%   r&   r'   r*   r+   r   r   r   r&  r#  r3   r2   r)   r(   r,   r-   r.   r/   r0   r1   r4   rS  ZOpaqueUnaryFn_sqrtZOpaqueUnaryFn_cosZOpaqueUnaryFn_coshZOpaqueUnaryFn_sinZOpaqueUnaryFn_sinhZOpaqueUnaryFn_tanZOpaqueUnaryFn_tanhZOpaqueUnaryFn_asinZOpaqueUnaryFn_acosZOpaqueUnaryFn_atanZOpaqueUnaryFn_expZOpaqueUnaryFn_logZOpaqueUnaryFn_asinhZOpaqueUnaryFn_log2rf  ZBitwiseFn_bitwise_andZBitwiseFn_bitwise_orr:   r:   r:   r;   <module>   s    !
">uG!<4	

  u	8.
