
    ΅i                     2   S SK r S SKrS SKrS SKrS SKJr  S SKJrJrJ	r	  S SK
JrJr  S SKrS SKJr  S SKJr  S SKJr  S SKJr  S S	KJrJrJr  S S
KJr  S SKJrJr  S SKJr  S SK J!r!  S SK"J#r#  S SK$J%r%  S SK&J'r'  SSK(J)r)  \(       a  S SKJ*r*  \	" S\S9r+\" S5      r,/ SQr-S\R$                  S\.4S jr/S\\\,   /\+4   S\\\,   /\+\R`                  -  4   4S jr1S\.S-  S\.S-  S\.S-  4S jr2S \Rf                  S!\Rf                  S\Rf                  4S" jr4 " S# S$\Rj                  5      r6 " S% S&\Rj                  5      r7 " S' S(\Rj                  5      r8 " S) S*\Rj                  5      r9 " S+ S,\Rj                  5      r: " S- S.\65      r; " S/ S0\Rj                  5      r< " S1 S2\Rj                  5      r= " S3 S4\Rj                  5      r> " S5 S6\Rj                  5      r? " S7 S8\Rj                  5      r@ " S9 S:\\5      rA " S; S<\A\5      rB " S= S>\A\5      rCS? rDS@ rE " SA SB\Rj                  5      rF " SC SD\Rj                  5      rG " SE SF\Rj                  5      rH " SG SH\Rj                  5      rI " SI SJ\Rj                  5      rJ " SK SL\Rj                  5      rK " SM SN\Rj                  5      rL " SO SP\Rj                  5      rM " SQ SR\Rj                  5      rN " SS ST\Rj                  5      rO " SU SV\Rj                  5      rPSW rQ\Q" SX5      rR\Q" SY5      rS\Q" SZ5      rT\Q" S[5      rU\Q" S\5      rV\Q" S]5      rW\Q" S^5      rX\Q" S_5      rY\Q" S`5      rZ\Q" Sa5      r[\Q" Sb5      r\\Q" Sc5      r]\Q" Sd5      r^\Q" Se5      r_Sf r`\`" SgSh5      ra\`" SiSj5      rb\`" SkSl5      rcg)m    N)Callable)SupportsFloatTYPE_CHECKINGTypeVar)TypeVarTupleUnpack)Ssympify)Expr)Application)_torf	fuzzy_andfuzzy_or)equal_valued)	LatticeOpShortCircuit)ordered)walk)
PRECEDENCE)sift)TorchVersion   )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                 d   [        U [        R                  5      =(       a    U R                  =(       a}    [	        U R
                  5      S:H  =(       a^    U R
                  S   R                  =(       a>    U R
                  S   R                  =(       a    U R
                  S   U R
                  S   L$ )N   r   r   )
isinstancesympyr   is_Addlen_args	is_symbol)r5   s    V/home/james-whalen/.local/lib/python3.13/site-packages/torch/utils/_sympy/functions.py_is_symbols_binary_summationr@   \   s     	4$ 	/KK	/

Oq 	/ JJqM##	/ JJqM##		/
 JJqMA.    fc                    ^  [         R                  " T 5      S[        [           S[        [
        R                  -  4U 4S jj5       nU$ )Nargsr6   c                     > T" U 6 n[        S U  5       5      (       a>  [        U[        R                  5      (       d  [        R                  " [	        U5      5      nU$ )Nc              3   V   #    U  H  n[        U[        R                  5      v   M!     g 7fN)r9   r:   Float.0as     r?   	<genexpr>-_keep_float.<locals>.inner.<locals>.<genexpr>n   s     84az!U[[))4   '))anyr9   r:   rH   float)rD   rrB   s     r?   inner_keep_float.<locals>.innerk   sM    h84888u{{B
 B
 E!H%ArA   )	functoolswrapsr   r   r   r:   rH   )rB   rR   s   ` r?   _keep_floatrV   h   sB     __QVC[ R%++%5   LrA   xyc                     S X4;   a  g X:H  $ rG    )rW   rX   s     r?   fuzzy_eqr[   x   s    v~6MrA   pqc                   ^^ S[         R                  S[        4S jmS[         R                  S[        4U4S jjn[        R                  " U" U 5      U" U5      5      nX-  X-  p[        [        [         R                  R                  [         R                  R                  U 5      5      5      n[         R                  R                  U5      nU H$  m[        U4S jU 5       5      (       d  M  UT-  nM&     U$ )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.
rW   r6   c                    [         R                  R                  U 5       Vs/ s H>  n[        U[        [         R
                  45      (       d  M*  [        [	        U5      5      PM@     nn[        R                  " U5      $ s  snf rG   )	r:   Mul	make_argsr9   intIntegerabsmathprod)rW   arginteger_coefficientss      r?   integer_coefficient0simple_floordiv_gcd.<locals>.integer_coefficient   sg     yy**1-+
-#U]]34 CCM- 	 +

 yy-..+
s   )A?A?r5   c                    > [        T[        R                  R                  U 5      5      n[        R
                  " [        R                  U5      $ rG   )mapr:   Addra   rT   reducere   gcd)r5   integer_factorsri   s     r?   integer_factor+simple_floordiv_gcd.<locals>.integer_factor   s:    ),!4!4T!:*
 /::rA   c              3   .   >#    U  H
  nTU;   v   M     g 7frG   rZ   )rJ   
base_splitrW   s     r?   rL   &simple_floordiv_gcd.<locals>.<genexpr>   s     =:qJs   )r:   Basicrb   re   ro   listrl   r`   ra   rm   all)r\   r]   rq   ro   base_splitsdivisor_splitri   rW   s         @@r?   simple_floordiv_gcdr{   ~   s    "/u{{ /s /;U[[ ;S ; xxq)>!+<=C7AGq15EII!4!4Q!782K .3YY-@-@-CM===='C  JrA   c                   B   \ rS rSr% SrSr\\S4   \S'   Sr	\\S'   Sr
\\S	'   \S
\R                  4S j5       r\S
\R                  4S j5       rS\R"                  R$                  S
\4S jr\S\R,                  S\R,                  S
\R                  S-  4S j5       rSrg)r      z
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
r8   .nargs#   
precedenceT
is_integerr6   c                      U R                   S   $ Nr   rD   selfs    r?   baseFloorDiv.base        yy|rA   c                      U R                   S   $ Nr   r   r   s    r?   divisorFloorDiv.divisor   r   rA   printerc                     UR                  U R                  [        S   S-
  5      nUR                  U R                  [        S   S-
  5      nSU SU S3$ )NAtom      ?(z//))parenthesizer   r   r   r   r   r   r   s       r?   	_sympystrFloorDiv._sympystr   sW    ##DIIz&/AC/GH&&t||Z5G#5MN4&7)1%%rA   r   r   Nc                 
   UR                   (       a  [        S5      eU[        [        * [        R                  [        R                  * 4;   a@  U[        [        * [        R                  [        R                  * 4;   a  [        R
                  $ U[        R
                  L d  U[        R
                  L a  [        R
                  $ UR                   (       a  [        R                  R                  $ UR                  (       a  [        US5      (       a  U$ UR                  (       a(  [        US5      (       a  [        R                  " US5      $ X:X  a  [        R                  R                  $ [        U[        R                  5      (       Ga"  [        U[        R                  5      (       Ga  U[        [        * [        R                  [        R                  * 4;   d0  U[        [        * [        R                  [        R                  * 4;   a  [        U5      [        U5      -  nU[        R                   :X  a  [        $ U[        R                   * :X  a  [        * $ [        R"                  " U5      (       a  [        R
                  $ [        R$                  " [        R&                  " U5      5      $ [        U[        R$                  5      (       aJ  [        U[        R$                  5      (       a+  [        R$                  " [)        U5      [)        U5      -  5      $ [        U[*        5      (       a)  [+        UR,                  S   UR,                  S   U-  5      $ [        U[        R$                  5      (       Ga  Sn/ n[        R.                  R1                  U5       H  nXb-  nS n[        U[        R                  5      (       am  [3        [        R4                  5      [3        S5      :  aG  UR7                  [        R8                  5      n	[;        S U	 5       5      n
UR                  =(       a    U
nOUR                  nU(       d  M  UR=                  U5        XG-  nM     [?        U5      S:w  a&  [+        U[        R.                  " USS06-
  U5      U-   $  [A        X5      n[        US5      (       a5  [        U[        R.                  5      (       a  [        RB                  " X5      n[        US5      (       d8  [+        [        RD                  " X-  5      [        RD                  " X+-  5      5      $  g ! [        RF                   a     g f = f)	Ndivision by zeror   r   z1.15.0c              3   >   #    U  H  oR                   S :H  v   M     g7f)r   N)r]   )rJ   rQ   s     r?   rL    FloorDiv.eval.<locals>.<genexpr>  s     ,Iy!SSAXys   evaluateF)$is_zeroZeroDivisionErrorr   r:   oonanr	   Zeror   r   r`   Oner9   NumberrP   re   infisnanrc   floorrb   r   rD   rm   ra   r   __version__atomsRationalrx   appendr<   r{   ro   simplifyPolynomialError)clsr   r   rQ   	quotientstermstermquotientquotient_is_integer	rationalsall_rationals_intsro   s               r?   evalFloorDiv.eval   s    ??#$677FVGUXXy99gGHHXXI	J
 ?
 99599599 499<<77<<??|GQ77K??|GR8899T2&&?77;; tU\\**7ELL11&%((UXXI>>vw588)DD deGn,ADHH}txxiwAyy }}TZZ]33dEMM**z'5==/Q/Q==Tc'l!:;;dH%%DIIaL$))A,*@AA gu}}--IE		++D1>
 '+#h		22|%%8 *8+ !)u~~ >I),,Iy,I)I&*2*=*=*TBT'*2*=*='&&LL&)I% 2( 5zQ TEIIu$Eu$EEwO 
		%d4CC##
7EII(F(Fii.Q''NN4:.w}0M  (  $$ 		s   BT+ +UUrZ   )__name__
__module____qualname____firstlineno____doc__r   tuplerb   __annotations__r   r   boolpropertyr:   rv   r   r   printing
StrPrinterstrr   classmethodrc   r   __static_attributes__rZ   rA   r?   r   r      s     "E5c?!JJekk     &!:!: &s & ^ ^ ^%++PTBT ^ ^rA   r   c            
           \ rS rSr% SrSr\\S4   \S'   Sr	\
\S'   Sr\\S	'   \S
\R                  S\R                  S\R                  S\R                  S-  4S j5       rS\
S-  4S jrSrg)r    i<  zC
ModularIndexing(a, b, c) => (a // b) % c where % is the C modulus
   .r   Tr   r   r   r   r   modulusr6   Nc                    US:X  d  US:X  a  [         R                  R                  $ [        U[         R                  5      (       aE  [        U[         R                  5      (       a&  [        U[         R                  5      (       a  X-  U-  $  US:w  aU  [         R
                  " X5      nUS:w  a9  [        [         R                  " X-  5      [         R                  " X$-  5      U5      $ [        U[         R                  5      (       Ga  / nSnUR                   H  n[         R
                  " XsU-  5      X2-  :w  d  M$  [        U[         R                  5      (       a  US:  d^  [        U[         R                  5      (       aC  [        UR                  S   [         R                  5      (       a  UR                  S   S:  a  Sn  OUR                  U5        M     [        U5      [        UR                  5      :w  a  U(       a  [        [        U5      X#5      $ [        U[        5      (       a*  [        UR                  S   UR                  S   U-  U5      $ g ! [         R                   a     GNf = f)Nr   r   TF)r:   r	   r   r9   rc   ro   r    r   r   rm   rD   r`   r   r<   sumr   )r   r   r   r   ro   	new_termsall_positiver   s           r?   r   ModularIndexing.evalE  s    19177<<tU]]++7EMM227EMM22Ow..
	!|ii.!8*tz2w}5  dEII&&-/I!%L		99TW#459JJ"477D1H"433&tyy|U]]CC IIaL1, (-!((. "  9~TYY/L&s9~wHHdH%%"499Q<11GQQ9 $$ 		s   AI I-,I-c                 d    U R                   S S u  p[        UR                  UR                  5      $ )Nr8   )rD   r[   is_nonnegative)r   r\   r]   s      r?   _eval_is_nonnegative$ModularIndexing._eval_is_nonnegativey  s,    yy!}((!*:*:;;rA   rZ   )r   r   r   r   r   r   r   rb   r   r   r   r   r   r:   rc   rv   r   r   r   rZ   rA   r?   r    r    <  s     "E5c?!JJ1==1+0==1CH==1	t	1 1f<dTk <rA   r    c            
           \ rS rSr% SrSr\\S4   \S'   Sr	\\S'   S\
S	-  4S
 jrS\
S	-  4S jrS\
S	-  4S jr\S\R                   S\R                   S\R                   S\R                   S	-  4S j5       rSrg	)r!   i  z
Good ol' ternary operator
r   .r   r   r   r6   Nc                     U R                   S   R                  (       a   U R                   S   R                  (       a  S$ S $ Nr   r8   TrD   r   r   s    r?   _eval_is_integerWhere._eval_is_integer  s.    yy|..499Q<3J3JtTPTTrA   c                     U R                   S   R                  (       a   U R                   S   R                  (       a  S$ S $ r   )rD   r   r   s    r?   r   Where._eval_is_nonnegative  s9     yy|**tyy|/J/J 	
 	
rA   c                     U R                   S   R                  (       a   U R                   S   R                  (       a  S$ S $ r   rD   is_positiver   s    r?   _eval_is_positiveWhere._eval_is_positive  s.    yy|//DIIaL4L4LtVRVVrA   cr\   r]   c                 \    U[         R                  :X  a  U$ U[         R                  :X  a  U$ g rG   )r:   truefalse)r   r   r\   r]   s       r?   r   
Where.eval  s&    

?H%++HrA   rZ   )r   r   r   r   r   r   r   rb   r   r   r   r   r   r   r   r:   rv   r   r   rZ   rA   r?   r!   r!     s     "E5c?!JU$+ U
dTk 
W4$; W U[[ U[[ U[[ U[[SWEW  rA   r!   c                       \ rS rSr% Sr\\S4   \S'   Sr\\S'   Sr	\
\S'   \S	\R                  S
\R                  S\R                  S-  4S j5       rS\
S-  4S jrS\
S-  4S jrS\4S jrSrg)r"   i  r~   .r   r   r   Tr   r\   r]   r6   Nc                    UR                   (       a  [        S5      eU[        R                  L d  XU* 4;   d  US:X  a  [        R                  $ UR                  (       a  UR                  (       a  X-  $ UR                  (       aH  US:X  aB  UR
                  (       a  [        R                  $ UR                  (       a  [        R                  $ X-  nUR                  (       a  [        R                  $ X:  nUR                  (       a#  [        U5      (       a  UR                  (       a  U$ [        R                  " X5      S:X  a  [        R                  $ g )NModulo by zeror   r8   r   )r   r   r	   r   	is_Numberis_evenis_oddr   r   
is_Booleanr   r   r:   r#   r   r\   r]   rQ   lesss        r?   r   PythonMod.eval  s     99#$455 ;!A2w,!q&66M ;;1;;5L ;;16yyvvxxuu E<<66M
 u??tDzzammH99Q?a66MrA   c                 F    U R                   S   R                  (       a  S$ S $ Nr   Tr   r   s    r?   r   PythonMod._eval_is_nonnegative      yy|//t9T9rA   c                 F    U R                   S   R                  (       a  S$ S $ r   )rD   is_negativer   s    r?   _eval_is_nonpositivePythonMod._eval_is_nonpositive  r   rA   c                 2   UR                  U R                  S   [        S   S-
  5      nUR                  U R                  S   [        S   S-
  5      nU R                  S   R                  (       a  [	        U5      OSU S3nSU SU S	U SU S
U SU SU 3$ )Nr   r   r   r   zabs(r   r   z % z) < 0 ? z + z : )r   rD   r   r   r   )r   r   r\   r]   abs_qs        r?   _ccodePythonMod._ccode  s      1z&/AC/GH  1z&/AC/GH))A,22A$qc1#S8A3c!Cwc!CsCCrA   rZ   )r   r   r   r   r   r   rb   r   r   r   r   r   r:   r   r   r   r   r   r   r   rZ   rA   r?   r"   r"     s    !E5c?!JJ+UZZ +EJJ +5::3D + +\:dTk ::dTk :D DrA   r"   c                   @    \ rS rSr% SrSr\\S'   SrSr	\
S 5       rSrg)	r#   i  r~   r   r   Tc                    UR                   (       a  [        S5      eU[        R                  L d  XU* 4;   d  US:X  a  [        R                  $ UR                  (       a7  UR                  (       a&  US:  a  [        U5      eUS:  a  [        U5      eX-  $ UR                  (       aH  US:X  aB  UR                  (       a  [        R                  $ UR                  (       a  [        R                  $ X-  nUR                  (       a  [        R                  $ X:  nUR                  (       a%  [        U5      (       a  UR                  (       a  U$ g g g )Nr   r   r   r8   )r   r   r	   r   r   AssertionErrorr   r   r   r   r   r   r   r   s        r?   r   Mod.eval  s     99#$455 ;!A2w,!q&66M ;;1;;1u$Q''1u$Q''5L ;;16yyvvxxuu E<<66M
 u??tDzzammH /<z?rA   rZ   N)r   r   r   r   r   r   rb   r   r   r   r   r   r   rZ   rA   r?   r#   r#     s-    EJJN+ +rA   r#   c                       \ rS rSrSrSrg)r$   i  zN
Div where we can assume no rounding.
This is to enable future optimizations.
rZ   N)r   r   r   r   r   r   rZ   rA   r?   r$   r$     s    rA   r$   c                   6    \ rS rSrSr\S 5       rS\4S jrSr	g)r%   i#  Tc                 .   U[         R                  [        4;   a  [        $ U[         R                  * [        * 4;   a  [        * $ [        U[         R                  5      (       a3  [         R
                  " [        R                  " [        U5      5      5      $ g rG   )	r:   r   r   r9   r   rc   re   ceilrP   r   numbers     r?   r   CeilToInt.eval&  sh     ehh''Muxxi&))7Nfell++==5=!9:: ,rA   r6   c                     UR                  U R                  S   U R                  S   R                  S-
  5      nSU S3$ )Nr   r   zceil(r   )r   rD   r   )r   r   r  s      r?   r   CeilToInt._ccode0  s>    %%diilDIIaL4K4Kc4QRvha  rA   rZ   N)
r   r   r   r   r   r   r   r   r   r   rZ   rA   r?   r%   r%   #  s%    J; ;! !rA   r%   c                   (    \ rS rSrSr\S 5       rSrg)r&   i6  Tc                 n   U[         R                  [        4;   a  [        $ U[         R                  * [        4;   a  [        * $ [        U[         R                  5      (       a  U$ [        U[         R
                  5      (       a3  [         R                  " [        R                  " [        U5      5      5      $ g rG   )	r:   r   r   r9   rc   r   re   r   rP   r  s     r?   r   FloorToInt.eval9  sz    ehh''Muxxi((7Nfemm,,Mfell++==E&M!:;; ,rA   rZ   Nr   r   r   r   r   r   r   r   rZ   rA   r?   r&   r&   6  s    J< <rA   r&   c                   "    \ rS rSrSrSrS rSrg)r'   iE  z&
Div used in indexing that rounds up.
Tc                     [         R                  " U5      n[         R                  " U5      n[         R                  " X5      U:X  a  [        X5      $ [	        XS-
  -   U5      $ r   )r:   r   ro   r$   r   r   r   r   s      r?   __new__CeilDiv.__new__L  sN    }}T"--(99T#w.D**DaK0'::rA   rZ   N)r   r   r   r   r   r   r  r   rZ   rA   r?   r'   r'   E  s     J;rA   r'   c                   (    \ rS rSrSr\S 5       rSrg)r*   iU  Tc                 4    US:  a  [        S5      eUSU-  -  $ Nr   znegative shift countr8   )
ValueErrorr   r   shifts      r?   r   LShift.evalX  s#    19344ahrA   rZ   Nr  rZ   rA   r?   r*   r*   U  s    J rA   r*   c                   (    \ rS rSrSr\S 5       rSrg)r+   i_  Tc                 B    US:  a  [        S5      e[        USU-  5      $ r  )r  r   r  s      r?   r   RShift.evalb  s&    19344ah''rA   rZ   Nr  rZ   rA   r?   r+   r+   _  s    J( (rA   r+   c                      \ rS rSrS r\S\\R                  R                  R                     S-  4S j5       r\ S&S\\R                  R                  R                     S-  S\\R                  R                  R                     S-  4S jj5       r\S 5       r\S	 5       r\S
 5       rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS r S r!S r"S r#S r$S  r%S! r&S" r'S# r(S$ r)S%r*g)'
MinMaxBaseii  c                 H   SSK Jn  UR                  SUR                  5      nS U 5       nU(       d  S OU R	                  U5      nU(       aD   [        U R                  U5      5      nUc&  U R                  " U40 UD6nU R                  " U40 UD6n[        U5      nU(       d  U R                  $ [        U5      S:X  a  [        U5      R                  5       $ [        R                  " U /[!        U5      Q70 UD6nXWl        Xgl        U$ ! [         a    U R                  s $ f = f)Nr   )global_parametersr   c              3   8   #    U  H  n[        U5      v   M     g 7frG   r
   )rJ   rg   s     r?   rL   %MinMaxBase.__new__.<locals>.<genexpr>n  s     6   r   )sympy.core.parametersr  popr   "_satisfy_unique_summations_symbols	frozenset_new_args_filterr   zero_collapse_arguments_find_localzerosidentityr<   rw   r   r  r   _argsetunique_summations_symbols)r   original_argsassumptionsr  r   rD   r-  objs           r?   r  MinMaxBase.__new__j  s   ;??:/@/I/IJ66
  77F 	"   !!5!5d!;< )0..tC{C ++D@K@<<t9>:>>## ll3>>+>(A%
3    xx s   D D! D!r6   Nc                 T   [        U5      S:w  a  g[        US   [        5      (       a
  US   US   4O	US   US   4u  p#[        U5      (       d  g[        U5      (       a  U R	                  U5      $ [        U[        5      (       a#  [        USS5      nUb  U R	                  U/U5      $ g)au  
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.
r8   Nr   r   r-  )r<   r9   r  r@   _unique_symbolsgetattr)r   rD   lhsrhslhs_unique_summations_symbolss        r?   r%  -MinMaxBase._satisfy_unique_summations_symbols  s    < t9> $q':.. !Wd1gq'47# 	 ,C00 (,,&&t,, c:&&,30$-) -8**C52OPPrA   initial_setc                    Uc
  [        5       OUnU Hi  nUR                  5        HR  n[        U[        R                  R
                  R                  5      (       d      gXS;   a      gUR                  U5        MT     Mk     U$ )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)setr   r9   r:   coresymbolSymboladd)r   rD   r9  seen_symbolsrg   elements         r?   r3  MinMaxBase._unique_symbols  sk     !, 3suC99;!'5::+<+<+C+CDD, $$W- '  rA   c                 |  ^ ^^ U(       d  U$ [        [        U5      5      nT [        L a  [        mO[        mUS   R                  (       Ga  / / 4=nu  pEU Ha  n[        U[        [        5       HE  nUR                  S   R                  (       d  M#  U[        U[        5         R                  U5        MG     Mc     [        R                  nU H1  nUR                  S   nUR                  (       d  M%  Xx:  S:X  d  M/  UnM3     [        R                  n	U H1  nUR                  S   nUR                  (       d  M%  Xy:  S:X  d  M/  Un	M3     T [        L a)  U H"  n
U
R                  (       d    OCX:  S:X  d  M   U
nM$     O2T [        :X  a(  U H"  n
U
R                  (       d    OX:  S:X  d  M   U
n	M$     SnT [        L a  U[        R                  :w  a  [        mUnOU	[        R                  :w  a  [        mU	nUbg  [        [        U5      5       HO  nX   n[        UT5      (       d  M  UR                  S   nT[        :X  a  X:  OX:  S:X  d  MA  T R                  X'   MQ     U U4S jm[        U5       H(  u  plXS-   S  Vs/ s H  nT" X5      PM     snXS-   S& M*     U U4S jn[        U5      S:  a  U" U5      nU$ s  snf )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           	      |  > [        U [        [        45      (       d  U $ XR                  ;   nU(       d3  U R                  " U R                   Vs/ s H  nT" X15      PM     snSS06$ [        U T5      (       a:  U R                  " U R                   Vs/ s H  o3U:w  d  M
  T" X15      PM     snSS06$ U$ s  snf s  snf )Nr   F)r9   MinMaxrD   func)airK   condir   dos       r?   rK  *MinMaxBase._collapse_arguments.<locals>.do9  s    b3*--	<Dww277 ;7aA7 ;LeLL"c""ww277 E7a1fA7 EVPUVVH	 !< !Fs   B4	B9B9r   c                   > U4S jn[        XSS9u  p#U(       d  U $ U Vs/ s H  n[        UR                  5      PM     nn[        R                  " U6 nU(       d  U $ [	        U5      nU Vs/ s H  oU-
  PM	     n	n[        U	5      (       a/  U	 V
s/ s H  n
T" U
SS06PM     nn
UR                  T" USS065        T" USS06nX</-   $ s  snf s  snf s  sn
f )Nc                    > [        U T5      $ rG   )r9   )rg   others    r?   <lambda>GMinMaxBase._collapse_arguments.<locals>.factor_minmax.<locals>.<lambda>O  s    :c5#9rA   T)binaryr   F)r   r;  rD   intersectionrw   rx   r   )rD   is_other
other_argsremaining_argsrg   arg_setscommonnew_other_argsarg_setarg_sets_diffsother_args_diffother_args_factoredr   rO  s                r?   factor_minmax5MinMaxBase._collapse_arguments.<locals>.factor_minmaxN  s    9H)-dT)J&J 2<<#CHHH<%%x0F!&\N=EFX'v-XMF =!!FS"Tm5!#<e#<m"T%%c?&KU&KL"'"H%"H!$999 = G
 #Us   C	-CC)rw   r   rE  rF  	is_numberr   rD   is_comparabler9   r   r+  ranger<   	enumerate)r   rD   r/  siftedminsmaxsrJ  vsmallbigrg   TrK   a0rH  r_  rK  rO  s   `               @@r?   r)  MinMaxBase._collapse_arguments  sb   0 KGDM"#:EE
 7"$b&(FZTac*Avvay...z!S1299!< +  LLEFF1I;;;AI$#6E  ,,CFF1I;;;AG#4C  czC==, #	  
 C==	d*!	  
 AczCLL(EA$}s4y)AA!!U++VVAY(-RV26!" '*llDG *		 dODA15!eg?2RY?DQM $	:0 t9q= &DI @s   ;J9c              #   v  #    U H  n[        U[        5      (       a1  UR                  SL d"  UR                  (       a   UR                  (       d  [        SU S35      eX R                  :X  a  [        U5      eX R                  :X  a  M  UR                  U :X  a  UR                   Sh  vN   M  Uv   M     g N7f)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)r9   r   is_extended_realra  rb  r  r(  r   r+  rG  rD   )r   arg_sequencerg   s      r?   r'  MinMaxBase._new_args_filterk  s       C sD))''50MM#*;*; >#6J!KLLhh"3''$S88##	!   $s   B%B9'B7(B9c                     [        5       nSnU Hf  nUR                  (       aA  Uc  UnM  U [        L a  [        XE5      nM1  U [        L a  [        XE5      nMG  [        SU  35      eUR                  U5        Mh     Uc  U$ [        U5      S:X  a  U1$ [        U5      S:X  a^  [        [        U5      5      nUS;   a  UR                  (       a  U [        L a  U$ U1$ US:X  a  UR                  (       a  U [        L a  U$ U1$ UR                  U5        U$ )aV  
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   rF  maxrE  minr   r?  r<   nextiterr   r   )r   valuesoptionsother_values	num_valuerg   other_values          r?   r*  MinMaxBase._find_localzeros  s    u	C}}$ #Icz$'	$7	$'	$7	,{3%-@AA  %  |!;|!tL12KH$)C)C'*cz|B	{BA~+"9"9'*cz|B	{B#rA   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frG   )is_algebraicrJ   rJ  s     r?   rL   &MinMaxBase.<lambda>.<locals>.<genexpr>       (HAr"  r   rD   r\  s    r?   rP  MinMaxBase.<lambda>      5(H(H#HrA   c                 :    [        S U R                   5       5      $ )Nc              3   :   #    U  H  nUR                   v   M     g 7frG   )is_antihermitianr  s     r?   rL   r          -A 	
   r  r  s    r?   rP  r        u -- (rA   c                 :    [        S U R                   5       5      $ )Nc              3   :   #    U  H  nUR                   v   M     g 7frG   )is_commutativer  s     r?   rL   r          +A 	
r  r  r  s    r?   rP  r        U ++ &rA   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frG   )
is_complexr  s     r?   rL   r         &DV||Vr"  r  r  s    r?   rP  r        &DQVV&D!DrA   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frG   )is_compositer  s     r?   rL   r    r  r"  r  r  s    r?   rP  r    r  rA   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frG   )r   r  s     r?   rL   r         #>v!IIvr"  r  r  s    r?   rP  r        e#>qvv#>>rA   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frG   )	is_finiter  s     r?   rL   r    s     %B6akk6r"  r  r  s    r?   rP  r    s    %B166%B BrA   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frG   )is_hermitianr  s     r?   rL   r    r  r"  r  r  s    r?   rP  r    r  rA   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frG   )is_imaginaryr  s     r?   rL   r    r  r"  r  r  s    r?   rP  r    r  rA   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frG   )is_infiniter  s     r?   rL   r         'Fv!vr"  r  r  s    r?   rP  r        %'Fqvv'F"FrA   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frG   )r   r  s     r?   rL   r    r  r"  r  r  s    r?   rP  r    r  rA   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frG   )is_irrationalr  s     r?   rL   r         )J6a//6r"  r  r  s    r?   rP  r        E)J166)J$JrA   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frG   r   r  s     r?   rL   r    r  r"  r  r  s    r?   rP  r    r  rA   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frG   )is_nonintegerr  s     r?   rL   r    r  r"  r  r  s    r?   rP  r    r  rA   c                 :    [        S U R                   5       5      $ )Nc              3   :   #    U  H  nUR                   v   M     g 7frG   r   r  s     r?   rL   r    r  r  r  r  s    r?   rP  r    r  rA   c                 :    [        S U R                   5       5      $ )Nc              3   :   #    U  H  nUR                   v   M     g 7frG   )is_nonpositiver  s     r?   rL   r    r  r  r  r  s    r?   rP  r    r  rA   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frG   )
is_nonzeror  s     r?   rL   r    r  r"  r  r  s    r?   rP  r    r  rA   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frG   )r   r  s     r?   rL   r    s     "<V88Vr"  r  r  s    r?   rP  r    s    U"<QVV"<<rA   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frG   )is_polarr  s     r?   rL   r         $@AZZr"  r  r  s    r?   rP  r        u$@$@@rA   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frG   r   r  s     r?   rL   r    r  r"  r  r  s    r?   rP  r    r  rA   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frG   )is_primer  s     r?   rL   r    r  r"  r  r  s    r?   rP  r    r  rA   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frG   )is_rationalr  s     r?   rL   r    r  r"  r  r  s    r?   rP  r    r  rA   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frG   )is_realr  s     r?   rL   r    r  r"  r  r  s    r?   rP  r    r  rA   c                 :    [        S U R                   5       5      $ )Nc              3   :   #    U  H  nUR                   v   M     g 7frG   )ro  r  s     r?   rL   r    r  r  r  r  s    r?   rP  r    r  rA   c                 :    [        S U R                   5       5      $ )Nc              3   :   #    U  H  nUR                   v   M     g 7frG   )is_transcendentalr  s     r?   rL   r    s      .A 	
r  r  r  s    r?   rP  r    s     .. )rA   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frG   )r   r  s     r?   rL   r    r  r"  r  r  s    r?   rP  r    r  rA   rZ   rG   )+r   r   r   r   r  r   r;  r:   r<  r=  r>  r%  r3  r)  r'  r*  _eval_is_algebraic_eval_is_antihermitian_eval_is_commutative_eval_is_complex_eval_is_composite_eval_is_even_eval_is_finite_eval_is_hermitian_eval_is_imaginary_eval_is_infiniter   _eval_is_irrational_eval_is_negative_eval_is_nonintegerr   r   _eval_is_nonzero_eval_is_odd_eval_is_polarr   _eval_is_prime_eval_is_rational_eval_is_real_eval_is_extended_real_eval_is_transcendental_eval_is_zeror   rZ   rA   r?   r  r  i  s   +Z 5	UZZ%%	&	-5 5n GK #EJJ$5$5$<$< = D	UZZ%%	&	- $ F FP  4 , ,\ I EH>MBOHHFDJFJ E<L@NF@NF>M ?MrA   r  c                   Z    \ rS rSrSr\R                  r\R                  r	S r
S rS rSrg)rF  i  z5
Return, if possible, the maximum value of the list.
c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frG   r  rI   s     r?   rL   (Max._eval_is_positive.<locals>.<genexpr>       9y!yr"  r   rD   r   s    r?   r   Max._eval_is_positive  s    9tyy999rA   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frG   r  rI   s     r?   rL   +Max._eval_is_nonnegative.<locals>.<genexpr>  s     <)Q(()r"  r  r   s    r?   r   Max._eval_is_nonnegative  s    <$))<<<rA   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frG   r  rI   s     r?   rL   (Max._eval_is_negative.<locals>.<genexpr>       :	1	r"  r   rD   r   s    r?   r  Max._eval_is_negative  s    :		:::rA   rZ   N)r   r   r   r   r   r	   Infinityr(  NegativeInfinityr+  r   r   r  r   rZ   rA   r?   rF  rF    s,     ::D!!H:=;rA   rF  c                   Z    \ rS rSrSr\R                  r\R                  r	S r
S rS rSrg)rE  i  z5
Return, if possible, the minimum value of the list.
c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frG   r  rI   s     r?   rL   (Min._eval_is_positive.<locals>.<genexpr>  r   r"  r  r   s    r?   r   Min._eval_is_positive  s    :		:::rA   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frG   r  rI   s     r?   rL   +Min._eval_is_nonnegative.<locals>.<genexpr>  s     =9a))9r"  r  r   s    r?   r   Min._eval_is_nonnegative  s    =499===rA   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frG   r  rI   s     r?   rL   (Min._eval_is_negative.<locals>.<genexpr>  r  r"  r  r   s    r?   r  Min._eval_is_negative  s    9tyy999rA   rZ   N)r   r   r   r   r   r	   r  r(  r  r+  r   r   r  r   rZ   rA   r?   rE  rE    s,     DzzH;>:rA   rE  c                 N    SnU S:  a  U * n US-  S:X  a  SOSnU[        X5      -  $ )Nr   r   r8   r   )	_safe_pow)r   expsigns      r?   safe_powr  	  s6    Daxu!GqLqb)D&&&rA   c                     US:  a  [        S5      eUS:X  a  g[        XS-  5      nU[        L a  [        $ X"-  nU[        R                  :  a  [        $ US-  S:X  a  X0-  nU[        R                  :  a  [        $ U$ )Nr   zExponent must be non-negative.r   r8   )r  r  r   sysmaxsize)r   exponenthalf_expresults       r?   r  r    s~    !|9::1}!m,H6
  F!|qCKKMMrA   c                   8    \ rS rSr% SrSr\\S'   \S 5       r	Sr
g)r3   i,  T2   r   c                    [        U[        R                  5      (       aS  [        U[        R                  5      (       a4  [        X5      nU[        * [        4;   a  U$ [        R                  " U5      $ [        U[        R                  5      (       a  [        R
                  " X5      $ U[        [        R                  4;   a9  UR                  (       a  [        $ UR                  (       a  [        R                  $ g g rG   )
r9   r:   rc   r  r   Powr   r   r   zoo)r   r   r  rQ   s       r?   r   PowByNatural.eval1  s    dEMM**z#u}}/M/M#AfWf%%==##c5==)) 99T''6588$$""!!yy  " %rA   rZ   N)r   r   r   r   r   r   rb   r   r   r   r   rZ   rA   r?   r3   r3   ,  s#    JJ! !rA   r3   c                   8    \ rS rSr% SrSr\\S'   \S 5       r	Sr
g)r2   iG  T<   r   c                     [        U[        R                  5      (       aK  [        U[        R                  5      (       a+  [        R                  " [	        U5      [	        U5      -  5      $ g g rG   )r9   r:   r   rH   rP   )r   r   r  s      r?   r   FloatPow.evalL  sJ     dELL))jell.K.K;;uT{eCj899 /L)rA   rZ   Nr   r   r   r   r  r   rb   r   r   r   r   rZ   rA   r?   r2   r2   G  s#    GJ: :rA   r2   c                   8    \ rS rSr% SrSr\\S'   \S 5       r	Sr
g)r)   i]  Tr   r   c                    UR                   (       a  [        S5      e[        U[        R                  5      (       aK  [        U[        R                  5      (       a+  [        R
                  " [        U5      [        U5      -  5      $ g g Nr   )r   r   r9   r:   r   rH   rP   r  s      r?   r   FloatTrueDiv.evalb  s]    
 ??#$677dELL))j%,,.O.O;;uT{U7^;<< /P)rA   rZ   Nr'  rZ   rA   r?   r)   r)   ]  s#    GJ= =rA   r)   c                   F    \ rS rSr% SrSr\\S'   \S 5       r	S\
4S jrSrg	)
r(   iv  Tr   r   c                    UR                   (       a  [        S5      e[        U[        R                  5      (       a  [        U[        R                  5      (       a  U[
        [
        * [        R                  [        R                  * 4;   d0  U[
        [
        * [        R                  [        R                  * 4;   a+  [        R                  " [        U5      [        U5      -  5      $ [        U[        R                  5      (       aK  [        U[        R                  5      (       a+  [        R                  " [        U5      [        U5      -  5      $ g g r*  )r   r   r9   r:   r   r   r   rH   rP   rc   rb   r  s      r?   r   IntTrueDiv.eval{  s    ??#$677 tU\\**7ELL11&%((UXXI>>vw588)DD
 ;;uT{U7^;<<dEMM**z'5==/Q/Q;;s4y3w<788 0R*rA   r6   c                     UR                  U R                  S   [        S   S-
  5      nUR                  U R                  S   [        S   S-
  5      nSU SU S3$ )Nr   r   r   r   z((int)z/(int)r   )r   rD   r   r   s       r?   r   IntTrueDiv._ccode  s_    ##DIIaL*V2Ds2JK&&tyy|Z5G#5MNvVG9A..rA   rZ   N)r   r   r   r   r  r   rb   r   r   r   r   r   r   rZ   rA   r?   r(   r(   v  s/    GJ9 9$/ /rA   r(   c                   (    \ rS rSrSr\S 5       rSrg)r,   i  Tc           
      T   [        U5      S-  S:w  a  [        S[        U5       35      e[        U5      S-  nUSU nXS  nSSKJn  [	        S U 5       5      (       a=  U" U Vs/ s H  n[        U5      PM     snU Vs/ s H  n[        U5      PM     sn5      $ US:X  a<  US   R                  (       a
  US   S:X  a  gUS   R                  (       a
  US   S:  a  g[	        S U 5       5      (       a  US:X  a  [        S5      e[        [        [        X4S	S
9[        R                  " S5      S9SS	06u  px[	        S US S  5       5      (       aE  US S S-   nU" U Vs/ s H  n[        U5      PM     snU Vs/ s H  n[        U5      PM     sn5      $ g s  snf s  snf s  snf s  snf )Nr8   r   z*expected an even number of arguments, got )!eval_is_non_overlapping_and_densec              3   V   #    U  H  n[        U[        R                  5      v   M!     g 7frG   r9   r:   rc   rI   s     r?   rL   9IsNonOverlappingAndDenseIndicator.eval.<locals>.<genexpr>  s     :Tz!U]]++TrN   r   c              3   V   #    U  H  n[        U[        R                  5      v   M!     g 7frG   r5  rI   s     r?   rL   r6    s     =Wz!U]]++WrN   zdim must not be zeroT)strict)keyr8  c              3   V   #    U  H  n[        U[        R                  5      v   M!     g 7frG   r5  rI   s     r?   rL   r6    s     FA:a//rN   r   )*   )r<   r   %torch.fx.experimental.symbolic_shapesr3  rx   rb   r   zipsortedoperator
itemgetter)	r   rD   dimsizesstridesr3  rK   s_sizes	s_stridess	            r?   r   &IsNonOverlappingAndDenseIndicator.eval  s   t9q=A <SYKH  $i1nQst*	
 :T:::4!&'AQ'')B'Q#a&')B  !8qz##
aQx!!eAhl =W===ax$%;<< "%E48h>Q>QRS>TU""G
 F"FFF!#2,. 9%,-WSVW-	/J	1A	/J  K ()BD ./Js   $F?F
F 9F%
rZ   Nr  rZ   rA   r?   r,   r,     s    J5 5rA   r,   c                   (    \ rS rSrSr\S 5       rSrg)r-   i  Tc                     U[         R                  [         R                  * 4;   a  U$ [        U[         R                  5      (       a3  [         R                  " [
        R                  " [        U5      5      5      $ g rG   )r:   r   r9   r   rH   re   truncrP   r  s     r?   r   TruncToFloat.eval  sR    ehh	**Mfell++ ;;tzz%-899	 ,rA   rZ   Nr   r   r   r   r  r   r   r   rZ   rA   r?   r-   r-     s    G: :rA   r-   c                   (    \ rS rSrSr\S 5       rSrg)r.   i  Tc                 .   U[         R                  [        4;   a  [        $ U[         R                  * [        * 4;   a  [        * $ [        U[         R                  5      (       a3  [         R
                  " [        R                  " [        U5      5      5      $ g rG   )	r:   r   r   r9   r   rc   re   rI  rP   r  s     r?   r   TruncToInt.eval  sh     ehh''Muxxi&))7Nfell++==E&M!:;; ,rA   rZ   Nr  rZ   rA   r?   r.   r.     s    J< <rA   r.   c                   (    \ rS rSrSr\S 5       rSrg)r/   i  Tc                     U[         R                  L a  [        $ U[         R                  * L a  [        * $ [        U[         R                  5      (       a)  [         R
                  " [        [        U5      S5      5      $ g r   )r:   r   r   r9   r   rc   roundrP   r  s     r?   r   RoundToInt.eval  sZ     UXXMehhY7Nfell++==uV}a!899 ,rA   rZ   Nr  rZ   rA   r?   r/   r/     s    J: :rA   r/   c                   (    \ rS rSrSr\S 5       rSrg)r0   i  Tc                     [        U[        R                  5      (       aR  [        U[        R                  5      (       a2  [        R                  " [        [        U5      [        U5      5      5      $ g g rG   )r9   r:   r   rc   rH   rQ  rP   rb   )r   r  ndigitss      r?   r   RoundDecimal.eval  sL     fell++
7EMM0R0R;;uU6]CLABB 1S+rA   rZ   NrK  rZ   rA   r?   r0   r0     s    GC CrA   r0   c                   (    \ rS rSrSr\S 5       rSrg)r1   i   Tc                 6   U[         R                  [         R                  * 4;   a  U$ [        U[         R                  5      (       a  [         R                  " [        U5      5      $ U[        L a  [         R                  $ U[        * L a  [         R                  * $ g rG   )r:   r   r9   rc   rH   rb   r   r  s     r?   r   ToFloat.eval#  sn    ehh	**Mfemm,,;;s6{++V88OfWHH9 rA   rZ   NrK  rZ   rA   r?   r1   r1      s    G	 	rA   r1   c                   f    \ rS rSrSrSrS\4S jrS\4S jrS r	S r
S	 rS\4S
 jrS\4S jrSrg)r4   i0  z,
Prevents expansion and other optimizations

   r6   c                 (    SU R                   S    S3$ )Nz	Identity(r   r   r   r   s    r?   __repr__Identity.__repr__7  s    499Q<.**rA   c                 F    SUR                  U R                  S   5       S3$ )z+Controls how sympy's StrPrinter prints thisr   r   r   )doprintrD   )r   r   s     r?   r   Identity._sympystr;  s%     7??499Q<0133rA   c                 4    U R                   S   R                  $ r   )rD   r  r   s    r?   r  Identity._eval_is_real@  s    yy|###rA   c                 4    U R                   S   R                  $ r   r   r   s    r?   r   Identity._eval_is_integerD  s    yy|&&&rA   c                      U R                   S   $ r   r   )r   hintss     r?   _eval_expand_identityIdentity._eval_expand_identityG  r   rA   c                 2    [        U R                  S   5      $ r   )rb   rD   r   s    r?   __int__Identity.__int__L  s    499Q<  rA   c                 2    [        U R                  S   5      $ r   )rP   rD   r   s    r?   	__float__Identity.__float__P  s    TYYq\""rA   rZ   N)r   r   r   r   r   r   r   r]  r   r  r   rh  rb   rk  rP   rn  r   rZ   rA   r?   r4   r4   0  sM     J+# +4C 4
$'
! !#5 #rA   r4   c                 b   ^   " U 4S jS[         R                  5      nST -   nX!l        X!l        U$ )Nc                   :   > \ rS rSrSr Y r\r\U 4S j5       r	Sr
g)+make_opaque_unary_fn.<locals>.OpaqueUnaryFniV  a  
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 optimizations
that change numerics for size compute.
c                   > [        U[        R                  [        R                  45      (       a4   [        R                  " [	        [
        T5      " [        U5      5      5      $ U[        R                  [        R                  * [        R                  [        R                  * [        [        * 4;   ag  U[        L a  [        R                  nU[        * L a  [        R                  * nTS:X  a  [        R                  " US5      $ [	        [        T5      " U5      $ g ! [         a    [	        [        T5      " U5      s $ f = f)Nlog2r8   )r9   r:   rc   rH   r4  re   rP   OverflowErrorr   r!  r   log)r   rK   names     r?   r   0make_opaque_unary_fn.<locals>.OpaqueUnaryFn.evald  s    !emmU[[9::3 ;;wtT':58'DEE
 uxx%((EII		z6F7SS;A<	A6> 99Q?*ud+A.. % 3"5$/223s   2D  D?>D?rZ   N)r   r   r   r   r   _torch_handler_namemake_opaque_unary_fn_torch_unpicklerr   r   r   )rw  s   r?   OpaqueUnaryFnrr  V  s(    	 #/		 
	rA   r|  OpaqueUnaryFn_)r:   Functionr   r   )rw  r|  nms   `  r?   rz  rz  U  s2    $ $L 
D	 B!#rA   sqrtcoscoshsinsinhtantanhasinacosatanr  rv  asinhrt  c                    ^ ^^ T S:X  a
  [         S   mO.T S:X  a
  [         S   mOT S:X  a
  [         S   mO[        ST  35      e " U UU4S jS	[        R                  5      nS
T -   nX2l        X2l        U$ )Nbitwise_and
BitwiseAndbitwise_xor
BitwiseXor
bitwise_or	BitwiseOrzunrecognized c                   h   > \ rS rSr%  Y r Yr\\S'   \R                  " \
 YS9r\U4S j5       rSrg))make_opaque_bitwise_fn.<locals>.BitwiseFni  r   )real_op_namec                 D  > UR                   (       a'  UR                   (       a  [        [        T5      " X5      $ UR                   (       a  [        R                  " U(       a  SOS5      nUR                   (       a  [        R                  " U(       a  SOS5      n[        U[        R                  [        45      (       ab  [        U[        R                  [        45      (       a=  [        R                  " [        [        T5      " [        U5      [        U5      5      5      $ g )Nr   r   )r   r4  r?  r:   rc   r9   rb   )r   rK   br  s      r?   r   .make_opaque_bitwise_fn.<locals>.BitwiseFn.eval  s    ||x6q<<||MMq!a0||MMq!a0!emmS122zEMM3'8 8 }}WX|%DSVSQRV%TUUrA   rZ   N)r   r   r   r   ry  r   rb   r   rT   partialmake_opaque_bitwise_fnr{  r   r   r   )rw  precr  s   r?   	BitwiseFnr    s=    "
C$,,"
 
	 
	rA   r  
BitwiseFn_)r   r   r:   r~  r   r   )rw  r  r  r  r  s   ``  @r?   r  r    s~    },'		,'		+&}TF344 ENN * 
	BrA   r  and_r  or_r  xor)drT   re   r?  r  collections.abcr   typingr   r   r   typing_extensionsr   r   r:   r	   
sympy.corer   sympy.core.exprr   sympy.core.functionr   sympy.core.logicr   r   r   sympy.core.numbersr   sympy.core.operationsr   r   sympy.core.sortingr   sympy.core.traversalr   sympy.printing.precedencer   sympy.utilities.iterablesr   torch.torch_versionr   numbersr   r   r   r   __all__r   r@   rH   rV   r[   rv   r{   r~  r   r    r!   r"   r#   r$   r%   r&   r'   r*   r+   r  rF  rE  r  r  r3   r2   r)   r(   r,   r-   r.   r/   r0   r1   r4   rz  OpaqueUnaryFn_sqrtOpaqueUnaryFn_cosOpaqueUnaryFn_coshOpaqueUnaryFn_sinOpaqueUnaryFn_sinhOpaqueUnaryFn_tanOpaqueUnaryFn_tanhOpaqueUnaryFn_asinOpaqueUnaryFn_acosOpaqueUnaryFn_atanOpaqueUnaryFn_expOpaqueUnaryFn_logOpaqueUnaryFn_asinhOpaqueUnaryFn_log2r  BitwiseFn_bitwise_andBitwiseFn_bitwise_orBitwiseFn_bitwise_xorrZ   rA   r?   <module>r     s      
 $ 8 8 2      + 7 7 + 9 & % 0 * ,  ( T'5B4	uzz 	d 	r!"vc{mR%++--. t t t )5;; )5;; )5;; )|}u~~ }@@<enn @<FENN >BD BDL3%.. 3lx ! !&< <;enn ; U^^ (U^^ (w?y w?t;*k ;&:*k :&'4!5>> !6:u~~ :,=5>> =2/ /L9 9z:5>> :< <: :<C5>> Cenn  "#u~~ "#J+^ *&1 (/ )&1 (/ )&1 (/ )&1 )&1 )&1 )&1 (/ (/ *73 )&1 #L /}fE -lEB .}eD rA   