
    ȅi                         S r SSKrSSKrSSKJr  S r " S S\\5      r " S S	\	\
5      rS
 rS rSS jrSS jrS rS rS rSS.S jrS rS rS rSS jrS rg)zKAssorted utilities, which do not need anything other then torch and stdlib.    N   )_dtypes_implc                 h    [        U [        5      (       a  g [        U 5        g! [         a     gf = f)NFT)
isinstancestrlen	Exception)seqs    L/home/james-whalen/.local/lib/python3.13/site-packages/torch/_numpy/_util.pyis_sequencer      s7    #sC   s   $ 
11c                       \ rS rSrSrg)	AxisError    N__name__
__module____qualname____firstlineno____static_attributes__r       r   r   r          r   r   c                       \ rS rSrSrg)UFuncTypeError   r   Nr   r   r   r   r   r      r   r   r   c                 N    Ub!  U R                   U:w  a  U R                  U5      n U $ N)dtypeto)tensorr   s     r   cast_if_neededr!      s&    V\\U25!Mr   c                     [         R                  " U R                  5      S:  a.  U R                  [         R                  " 5       R
                  5      n U $ )N   )r   	_categoryr   r   default_dtypesfloat_dtype)xs    r   cast_int_to_floatr(   &   s;    agg&*DD,,.::;Hr   c                 \    U* U s=::  a  U:  d  O  [        SU  SU 35      eU S:  a  X-  n U $ )Nzaxis z) is out of bounds for array of dimension r   )r   )axndimargnames      r   normalize_axis_indexr-   .   s=    ER$%t#LTFSTT	Av

Ir   c                 f  ^^ [        U 5      [        [        4;  a   [        R                  " U 5      /n [        UU4S jU  5       5      n U(       dP  [        [        [        [        U 5      5      5      [        U 5      :w  a!  T(       a  [        ST S35      e[        S5      eU $ ! [
         a     N{f = f)ar  
Normalizes an axis argument into a tuple of non-negative integer axes.

This handles shorthands such as ``1`` and converts them to ``(1,)``,
as well as performing the handling of negative indices covered by
`normalize_axis_index`.

By default, this forbids axes from being specified multiple times.
Used internally by multi-axis-checking logic.

Parameters
----------
axis : int, iterable of int
    The un-normalized index or indices of the axis.
ndim : int
    The number of dimensions of the array that `axis` should be normalized
    against.
argname : str, optional
    A prefix to put before the error message, typically the name of the
    argument.
allow_duplicate : bool, optional
    If False, the default, disallow an axis from being specified twice.

Returns
-------
normalized_axes : tuple of int
    The normalized axis index, such that `0 <= normalized_axis < ndim`
c              3   >   >#    U  H  n[        UTT5      v   M     g 7fr   )r-   ).0r*   r,   r+   s     r   	<genexpr>'normalize_axis_tuple.<locals>.<genexpr>[   s     H4R%b$884   zrepeated axis in `z
` argumentzrepeated axis)typetuplelistoperatorindex	TypeErrorr   setmapint
ValueError)axisr+   r,   allow_duplicates    `` r   normalize_axis_tupler@   7   s    < Dz%&	NN4()D H4HHDs3s3~#673t9D1'*EFF_--K  		s   B# #
B0/B0c                 J    U c  U $ [        U 5      S:w  a  [        S5      eU S   $ )Nr   zdoes not handle tuple axisr   )r   NotImplementedErrorr>   s    r   allow_only_single_axisrD   d   s,    |
4yA~!">??7Nr   c                     [        U5      [        [        4;  a  U4n[        U5      [        U 5      -   n[	        X5      n[        U 5      n[        U5       Vs/ s H  oDU;   a  SO
[        U5      PM     nnU$ s  snf )Nr   )r4   r6   r5   r   r@   iterrangenext)	arr_shaper>   out_ndimshape_itr*   shapes         r   expand_shaperM   l   sn    Dz$&w4y3y>)H/DIH;@?K?R*Q$x.0?EKL Ls   A7c                     Uc&  SU-  nU R                  U5      R                  5       n U $ [        U R                  U5      nU R	                  U5      n U $ )Nr   )expand
contiguousrM   rL   reshape)r    r>   r+   rL   s       r   apply_keepdimsrS   w   sS    |tu%002 M V\\40&Mr   rC   c                 :    U c  [        S U 5       5      nUS4$ X4$ )z#Flatten the arrays if axis is None.c              3   @   #    U  H  oR                  5       v   M     g 7fr   )flatten)r0   ars     r   r1   $axis_none_flatten.<locals>.<genexpr>   s     7w

ws   r   r5   )r>   tensorss     r   axis_none_flattenr[      s)    |7w77z}r   c           	          [         R                  nU" U R                  XS9(       d  [        SU R                   SU SU S35      e[	        X5      $ )a  Dtype-cast tensor to target_dtype.

Parameters
----------
t : torch.Tensor
    The tensor to cast
target_dtype : torch dtype object
    The array dtype to cast all tensors to
casting : str
    The casting mode, see `np.can_cast`

 Returns
 -------
`torch.Tensor` of the `target_dtype` dtype

 Raises
 ------
 ValueError
    if the argument cannot be cast according to the `casting` rule

)castingzCannot cast array data from z to z according to the rule '')r   can_cast_implr   r9   r!   )ttarget_dtyper]   can_casts       r   typecast_tensorrc      s[    , ))HAGG\;*177) 4~5gYaA
 	
 !**r   c                 2   ^^ [        UU4S jU  5       5      $ )Nc              3   >   >#    U  H  n[        UTT5      v   M     g 7fr   )rc   )r0   r`   r]   ra   s     r   r1   #typecast_tensors.<locals>.<genexpr>   s     LGqL'::Gr3   rY   )rZ   ra   r]   s    ``r   typecast_tensorsrg      s    LGLLLr   c                      [         R                  " U 5      nU$ ! [         a"  nSU  S[        U5       S3n[	        U5      eS nAff = f)Nzfailed to convert z! to ndarray. 
Internal error is: .)torch	as_tensorr	   r   rB   )objr    emesgs       r   _try_convert_to_tensorro      sS    (% M  (#C5(J3q6(RST!$''(s    
AAAc                 H   [        U [        R                  5      (       a  U nOo[        R                  " 5       n[        R                  " [
        R                  " [        R                  5      5         [        U 5      n[        R                  " U5        [        XA5      nX4R                  -
  nUS:  a!  UR                  SU-  UR                  -   5      n [        U5      nU(       a  UR                  5       nU$ ! [        R                  " U5        f = f! [         a    Sn NBf = f)ad  The core logic of the array(...) function.

Parameters
----------
obj : tensor_like
    The thing to coerce
dtype : torch.dtype object or None
    Coerce to this torch dtype
copy : bool
    Copy or not
ndmin : int
    The results as least this many dimensions
is_weak : bool
    Whether obj is a weakly typed python scalar.

Returns
-------
tensor : torch.Tensor
    a tensor object with requested dtype, ndim and copy semantics.

Notes
-----
This is almost a "tensor_like" coercive function. Does not handle wrapper
ndarrays (those should be handled in the ndarray-aware layer prior to
invoking this function).
r   rO   F)r   rj   Tensorget_default_dtypeset_default_dtyper   get_default_dtype_forfloat32ro   r!   r+   viewrL   boolr=   clone)rl   r   copyndminr    default_dtype
ndim_extras          r   _coerce_to_tensorr}      s    6 #u||$$ //1 B B5== QR	3+C0F##M2 F*F $JA~TJ.=>Dz
 M) ##M2  s   1C7 D 7DD! D!c                     SSK Jn  [        U 5      S:X  a
  [        5       $ [        U 5      S:X  aj  U S   n[	        X!5      (       a  UR
                  $ [	        U[        5      (       a2  / nU H  n[        U5      nUR                  U5        M!     [        U5      $ U$ [	        U [        5      (       d!  [        S[        U 5      R                   35      e[        U 5      $ )zHConvert all ndarrays from `inputs` to tensors. (other things are intact)r   )ndarrayr   z#Expected inputs to be a tuple, got )_ndarrayr   r   r=   r   r    r5   ndarrays_to_tensorsappendAssertionErrorr4   r   )inputsr   input_result	sub_input
sub_results         r   r   r      s    !
6{a|	V	f&&== &&F#	0;
j) $ = M&%(( 5d6l6K6K5LM  #6**r   r   )NF)NFr   )__doc__r7   rj    r   r   r=   
IndexErrorr   r9   RuntimeErrorr   r!   r(   r-   r@   rD   rM   rS   r[   rc   rg   ro   r}   r   r   r   r   <module>r      s    R   	
J 		Y 	*Z &* +@M;|+r   