
    J/PhA                     B   d dl Z d dlmZmZ d dlZd dlZd dlmZ d dlZ	d dl
Z
d dlmZmZmZmZmZ d dlmZ d dlmZ d Z ej        d           G d	 d
ej                              Ze
j        e j        e
j        e j        e
j        e j        iZe
j        e j         e
j!        e j"        e
j#        e j$        e
j%        e j&        e
j'        e j(        e
j)        e j*        e
j+        e j,        e
j-        e j.        e
j/        e j0        e
j1        e j2        e
j3        e j4        e
j5        e j6        iZ7e
j8        e j9        e
j:        e j;        e
j<        e j=        e
j>        e j?        e
j@        e jA        e
jB        e jC        iZDd ZEejF        d             ZG G d de jH                  ZIde jJ        fdZKd ZLdS )    N)defaultdictOrderedDict)SimpleNamespace)typestargetconfigirrewritescompiler)npydecl)DUFuncc                 D    t          | t          j        t          f          S N)
isinstancenpufuncr   )funcs    Z/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/numba/np/ufunc/array_exprs.py	_is_ufuncr      s    dRXv.///    zafter-inferencec                   X     e Zd ZdZ fdZd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Z xZS )RewriteArrayExprszThe RewriteArrayExprs class is responsible for finding array
    expressions in Numba intermediate representation code, and
    rewriting those expressions to a single operation that will expand
    into something similar to a ufunc call.
    c                      t          t          |           j        |g|R i | |j        j        }d|vrt
          |d<   d S d S )N	arrayexpr)superr   __init__	targetctxspecial_ops_lower_array_expr)selfstateargskwsr   	__class__s        r   r   zRewriteArrayExprs.__init__   s^    /&&/DDDDDDDo1k))'8K$$$ *)r   c                 "   t          |          dk    rdS || _        || _        t                      | _        i | _        |                    t          j                  }|D ]}|j	        j
        }|j        }t          |t          j                  rFt          |                    |d          t          j                  r|                     |||           ut          |t          j                  r
|| j        |<   t          | j                  dk    S )z
        Using typing and a basic block, search the basic block for array
        expressions.
        Return True when one or more matches were found, False otherwise.
        r   FN)len
crnt_blocktypemapr   array_assignsconst_assigns
find_instsr   Assigntargetnamevaluer   Exprgetr   Array_match_array_exprConst)	r   func_irblockr'   	calltypesassignmentsinstrtarget_nameexprs	            r   matchzRewriteArrayExprs.match!   s     y>>Q5(]]&&ry11  
	7 
	7E,+K;D4)) 77;;{D995;GG7&&udK@@@@D"(++ 7 37";/4%&&**r   c                     |j         } j        }|dv rI|j        t          j        v r6t           fd|                                D                       r|||<   dS dS |dk    rw|j        j         j	        v rf j	        |j        j                 }t          |t          j                  r7|j        }t          |          r#                     ||          s|||<   dS dS dS dS dS dS )z
        Find whether the given assignment (*instr*) of an expression (*expr*)
        to variable *target_name* is an array expression.
        unarybinopc              3   F   K   | ]}j         |j                 j        V  d S r   )r'   r-   is_internal).0varr   s     r   	<genexpr>z6RewriteArrayExprs._match_array_expr.<locals>.<genexpr>Q   sC       0 0 <)5 0 0 0 0 0 0r   callN)opr(   fnr   supported_array_operatorsall	list_varsr   r-   r'   r   r   Function
typing_keyr   _has_explicit_output)r   r8   r:   r9   expr_opr(   	func_typefunc_keys   `       r   r2   z#RewriteArrayExprs._match_array_exprB   s;    '****7<<<  0 0 0 0"nn..0 0 0 0 0 3-2k***3 3 &  ty~'E'ETY^4I)U^44 ;$/X&& ;44T8DD ;5:k222 ! 'E'E; ;; ;; ;r   c                 |    t          |j                  t          |j                  z   }|j        dS ||j        k    S )zr
        Return whether the *expr* call to *func* (a ufunc) features an
        explicit output argument.
        NT)r%   r!   r"   varargnin)r   r:   r   nargss       r   rM   z&RewriteArrayExprs._has_explicit_output`   s:    
 DITX.;" 4txr   c                     |j         }|dv r|j        S |dk    r| j        |j        j                 j        S t          d                    |                    )Nr=   rE   z:Don't know how to find the operator for '{0}' expressions.)rF   rG   r'   r   r-   rL   NotImplementedErrorformatr   ir_exprir_ops      r   _get_array_operatorz%RewriteArrayExprs._get_array_operatorl   sc    
&&&:f__< 12==!HOO   	r   c                     |j         }|dk    r|j        |j        fS |dk    r|                                S |dk    r|j        S t          d                    |                    )z{Given a Numba IR expression, return the operands to the expression
        in order they appear in the expression.
        r?   r>   rE   z:Don't know how to find the operands for '{0}' expressions.)rF   lhsrhsrJ   r!   rV   rW   rX   s      r   _get_operandszRewriteArrayExprs._get_operandsv   s{     
G;++g$$&&&f__<!HOO   	r   c                      |j         }|dk    r|j        S  fd                     |          D             }                     |          |fS )zZTranslate the given expression from Numba IR to an array expression
        tree.
        r   c                 P    g | ]"}j                             |j        |          #S  )r)   r0   r-   )rB   op_varr   s     r   
<listcomp>z5RewriteArrayExprs._translate_expr.<locals>.<listcomp>   sD     G G G & !.226;GG G G Gr   )rF   r:   r_   r[   )r   rY   rZ   operands_or_argss   `   r   _translate_exprz!RewriteArrayExprs._translate_expr   sw     
K<G G G G*.*<*<W*E*EG G G''002BBBr   c                    i }t                      }t          t                    }| j                                        D ]}|j        }g }|                     |          |f}t          j        d|j	        || j
        |j        j                           }t          j        ||j        |j	                  }	|	||<   |	| j        |j        j        <   |                     |          D ]	}
|
j        }|
j        r|| j        v r| j        |         }|j        }|                                }|D ]}
||
j        xx         dz  cc<   |                    |                     |                     |j        j        r$|                    |j        j                   d||<   || j        v r!|                    | j        |                    ||
j        xx         dz  cc<   |                    |
           |||fS )ztIterate over the matches, trying to find which instructions should
        be rewritten, deleted, or moved.
        r   )rF   locr:   ty   N)setr   intr(   valuesr.   r[   r   r/   rh   r'   r,   r-   r+   r_   is_temprJ   appendrf   addr)   )r   replace_map	dead_vars	used_varsr8   r:   arr_inpsarr_exprnew_expr	new_instroperandoperand_namechild_assign
child_exprchild_operandss                  r   _handle_matchesz!RewriteArrayExprs._handle_matches   s    EE	$$	'..00 	- 	-E;DH//55x?Hw+#'8$,"&,u|/@"AC C CH 	(EL%)DDI!*K4=Du|01--d33 - -&|? -|t7I'I'I#'#5l#CL!-!3J%/%9%9%;%;N#1 5 5!',///14////OOD$8$8$D$DEEE#*2 9!l&9&>???48L1!T%777OOD$6|$DEEEEgl+++q0+++OOG,,,,!-" Iy00r   c                 6    ||         }||v r||         }||v |S )zFind the final replacement instruction for a given initial
        instruction by chasing instructions in a map from instructions
        to replacement instructions.
        rb   )r   replacement_mapr8   replacements       r   _get_final_replacementz(RewriteArrayExprs._get_final_replacement   s6    
 &e,_,,)+6K _,,r   c                    |                                  \  }}}| j                                        }|                                 i }| j        j        D ]D}t          |t          j                  r||v r|                     ||          }|r|	                    |           |j
                                        D ]Q}|j        }	|	|v r(|	                    |                    |	                     ||	         dk    r||	xx         dz  cc<   R|	                    |           t          |t          j                  rE|j
        }
||
         dk    r||
xx         dz  cc<   |||
<   |
|vr|	                    |           /|	                    |           F|r,|                                D ]}|                    |           |S )z}When we've found array expressions in a basic block, rewrite that
        block, returning a new, transformed block.
        r   rj   )r}   r&   copyclearbodyr   r   r+   r   ro   r.   rJ   r-   popDelrm   insert_before_terminator)r   rq   rr   rs   result
delete_mapr8   r   rC   var_nameinstr_values              r   applyzRewriteArrayExprs.apply   s    -1,@,@,B,B)Y	 %%''
_) 	% 	%E%++ %K''"&"="=#U#, #,K" 9k222#.#4#>#>#@#@ 9 9C'*xH':55 &jnnX.F.F G G G(2Q66 )( 3 3 3q 8 3 3 3 MM%((((E26** %#k[)A--k***a/***.3J{++ 	11MM%(((e$$$$ 	7#**,, 7 7//6666r   )__name__
__module____qualname____doc__r   r;   r2   rM   r[   r_   rf   r}   r   r   __classcell__r#   s   @r   r   r      s         
9 9 9 9 9+ + +B; ; ;<
  
  
     	C 	C 	C#1 #1 #1J  ( ( ( ( ( ( (r   r   c                    t          | t                    r| \  }}g }i }|D ]>}t          |          \  }}|                    |           |                    |           ?|t
          j        v rt          |          dk    r|t          v r6t          j
        |d         t          |                     |d                   |fS |t          v r8t          j        |d         t          |                     g|d         g          |fS ns|t          v sJ t          j        t          |                     |d                   |fS t          |          rd                    t#          t%          |                                        dd                    }t          j        |t          j                              }	|||<   t          j        |	|g           }
|
|fS nt          | t.          j                  rRt          j        | j        t          j                    | j        j        | j        j        r| j        j        nd          i fS t          | t.          j                  rt          j        | j                  i fS tA          d| d	          )
z[Build a Python expression AST from an array expression built by
    RewriteArrayExprs.
       r   rj   z__ufunc_or_dufunc_{0}-_)lineno
col_offsetz.Don't know how to translate array expression '')!r   tuple_arr_expr_to_astro   updater   rH   r%   _binopsastBinOp_cmpopsCompare	_unaryopsUnaryOpr   rW   hexhashreplaceNameLoadCallr   Varr-   rh   linecolr3   Constantr.   rV   )r:   rF   arr_expr_argsast_argsenvargast_arg	child_envfn_namefn_ast_nameast_calls              r   r   r   
  s    $ , M  	" 	"C!1#!6!6GYOOG$$$JJy!!!!2228}}!!==9 WR[]]HQKA ABEF F==; gbkmm_x{mE EFIJ J ! Y{9R=??HQK@@#EEr]] 	!-44DHH%%c3//1 1G(7CHJJ77KCLxXr::HS= 	! 
D"&	!	! ,x	38::#x}378<#F48<<QH H HIKL 	L 
D"(	#	# ,|DJ''++

?CttEG G Gr   c              #     K   t                      }| D ]}}|j        }|j                            ||j                  j        }|                    dd                              dd          }||v rt          |d          ||f||<   ||_        ~t          |          }	 |V  |                                D ]\  }}||_        dS # |                                D ]\  }}||_        w xY w)za
    Legalize names in the variable list for use as a Python function's
    parameter names.
    )rh   $r   .z not uniqueN)	r   r-   scoperedefinerh   r   AssertionErrorlistrm   )var_listvar_maprC   old_namenew_nameparam_namess         r   _legalize_parameter_namesr   2  s       mmG  89%%hCG%<<A##C--55c3??w H!;!;!;<<<Mw--K  %^^-- 	  	 MCCHH	  	 W^^-- 	  	 MCCHH	 s   !C #C+c                   <     e Zd Zdej        dej        f fdZ xZS )_EraseInvalidLineRangesnodereturnc                     t                                          |          }t          |d          r%t          |dd           |j        |j        k    r|`|`|S )Nr   
end_lineno)r   generic_visithasattrgetattrr   r   )r   r   r#   s     r   r   z%_EraseInvalidLineRanges.generic_visitM  s\    ww$$T**4"" 	(t\400<;00r   )r   r   r   r   ASTr   r   r   s   @r   r   r   L  sO        #' cg          r   r   astreec                     t          j        |            t                                          |            t          j        |            dS )z)Inplace fixes invalid lineno ranges.
    N)r   fix_missing_locationsr   visit)r   s    r   _fix_invalid_lineno_rangesr   W  sF     f%%%##F+++f%%%%%r   c                 $    dt          t          |                                        dd          z  }|j        j        }|                                }t          t          |          d           }d |D             }t          |          5 }d |D             }t          j
        d                    |          |d	          }	t          |	d
          rt          |	j                  dk    sJ |	j        d         }
||
j        _        t!          |j                  \  |
j        d         _        }t'          |	           ddd           n# 1 swxY w Y   t)          |	|d	          }t+          ||           ||         } j        } j         |j         fd|D              }g }|j        D ]m}t3          |t4          j                  r|j        }t3          |t4          j                  r|                    |j                   X|                    |           n |j         j        | tC          j"                    #                                }|tI          j%                    n|&                                }d|_'        |(                    ||d          ddl)m*}  G fdd|j+                  }tY          t          |          d|          }|j-        |j.        z   |_/         fd|D             }|0                    |||||          S )z:Lower an array expression built by RewriteArrayExprs.
    z__numba_array_expr_%sr   r   c                     | j         S r   r-   )rC   s    r   <lambda>z#_lower_array_expr.<locals>.<lambda>j  s     r   )keyc                     g | ]	}|j         
S rb   r   )rB   rC   s     r   rd   z%_lower_array_expr.<locals>.<listcomp>m  s    555c555r   c                 8    g | ]}t          j        |d           S r   )r   r   )rB   
param_names     r   rd   z%_lower_array_expr.<locals>.<listcomp>q  s4     3 3 3" GJ-- 3 3 3r   zdef {0}(): returnexecr   rj   r   Nc              3   B   K   | ]}                     |          V  d S r   )typeofrB   r-   lowerers     r   rD   z$_lower_array_expr.<locals>.<genexpr>  s/      EE4'....EEEEEEr   numpyF)flagscaching)npyimplc                       e Zd Z fdZdS )%_lower_array_expr.<locals>.ExprKernelc                      t          | j        j        j                  } fd|D             } j                            j        |          }                     |j         j        j                  S )Nc                 F    g | ]\  }}}                     |||          S rb   )cast)rB   valintyouttyr   s       r   rd   zB_lower_array_expr.<locals>.ExprKernel.generate.<locals>.<listcomp>  s?     : : :-S$ 3e44 : : :r   )zip	outer_sigr!   contextcall_internalfndescr   return_type)r   r!   arg_zip	cast_argsr   buildercres	inner_sigs   `    r   generatez._lower_array_expr.<locals>.ExprKernel.generate  s    $ 3Y^DDG: : : :18: : :I\//i< <F99VY%:!^79 9 9r   N)r   r   r   r   )r   r   r   s   r   
ExprKernelr     s8        	9 	9 	9 	9 	9 	9 	9 	9 	9r   r   )rS   noutr   c                 :    g | ]}                     |          S rb   )loadvarr   s     r   rd   z%_lower_array_expr.<locals>.<listcomp>  s%    888dGOOD!!888r   )1r   r   r   rh   filenamerJ   sortedrk   r   r   parserW   r   r%   r   r!   r   r:   r.   r   compiler   r   r   ri   r   r   Optionaltyper1   ro   dtyper   r   ConfigStacktop_or_noner
   Flagsr   error_modelcompile_subroutinenumba.npr   _Kernelr   rS   r   rT   numpy_ufunc_kernel)r   r:   	expr_nameexpr_filenameexpr_var_listexpr_var_unique	expr_argsexpr_paramsr   
ast_moduleast_fn	namespacecode_objimplr   r   inner_sig_argsargtyr   r   r   r   r!   r   r   r   s   `                      @@@r   r   r   b  s    (3tDzz??+B+B3+L+LMIH%MNN$$M S//5I5IJJJO 65_555I 
#?	3	3 /{3 3&13 3 3 Y299)DD,f6 6
z6**Hs:?/C/Cq/H/H/H/H##*:49*E*E'Ai":.../ / / / / / / / / / / / / / / z=&99H9YD oGoGEEEE9EEEFIN ) )eU^,, 	JEeU[)) 	)!!%+....!!%((((+	%+^<I$&&2244E %HN5::<<E  E%%gtYe.3 & 5 5D !     9 9 9 9 9 9 9 9 9W_ 9 9 9 IQKKKE)ej(EK8888i888D%%)T5*> > >s   B/EEE)Mr   collectionsr   r   
contextlibsysr   r   r   r   operator
numba.corer   r   r	   r
   numba.core.typingr   numba.np.ufunc.dufuncr   r   register_rewriteRewriter   posUAddnegUSubinvertInvertr   rp   AddsubSubmulMulttruedivDivmodModor_BitOrrshiftRShiftxorBitXorlshiftLShiftand_BitAndpowPowfloordivFloorDivr   eqEqneNotEqltLtleLtEgtGtgeGtEr   r   contextmanagerr   NodeTransformerr   r   r   r   rb   r   r   <module>rM     s   



 0 0 0 0 0 0 0 0     



 ! ! ! ! ! !      B B B B B B B B B B B B B B % % % % % % ( ( ( ( ( (0 0 0 ,--S S S S S( S S .-Sn L#(L#(OSZ	 L#'L#'L#(cgL#'L#)OSZL#*OSZM3:L#'s|" KKKKKK%G %G %GP      2    c1   &sw & & & &J> J> J> J> J>r   