
    AVhS                     \   d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z	 ddlm
Z
 ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ d Zd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#d Z$	 dNdZ% ejL                  dd      a'd Z(d Z) G d  d!      Z* G d" d#      Z+ G d$ d%      Z,d& Z- ejL                  d'd(      d)v a.d* Z/d+ Z0d, Z1 ejL                  d-d(      d)v a2d. Z3d/ Z4dNd0Z5dOd1Z6 ej2                  d2g 3       e5d4      d5               Z7d6 Z8 ej2                  d7g 3       e6d8      d9               Z9d: Z:d; Z; ej2                  d<g 3       e5d=      d>               Z<d? Z=d@ Z>dA Z?dB ZdC Z@dD ZAdE ZBdF ZCdG ZDdH ZEdI ZFdJ ZGdPdKZHdPdLZIdM ZJy)Qz#Utility functions for internal use.    N)dtypes)flexible_dtypes)indexed_slices)ops)tensor_util)	array_opscond)math_ops)	np_arrays)	np_dtypes)core)nest)	tf_exportc                 "    t        | g|      d   S Nr   )_canonicalize_axesaxisranks     X/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/ops/numpy_ops/np_utils.py_canonicalize_axisr   (   s    	TFD	)!	,,    c                     t              t        t        j                        rfd}nfd}| D cg c]
  } ||       c}S c c}w )Nc                 4     t         dk   fd fd      S )Nr   c                       z   S N r   s   r   <lambda>z6_canonicalize_axes.<locals>.<lambda>.<locals>.<lambda>1   s    $+ r   c                       S r   r   )r   s   r   r   z6_canonicalize_axes.<locals>.<lambda>.<locals>.<lambda>1   s    t r   r	   r   s   `r   r   z$_canonicalize_axes.<locals>.<lambda>0   s    q%|" r   c                     | dk  r| z   S | S r   r   r   s    r   r   z$_canonicalize_axes.<locals>.<lambda>4   s    q d r   )_maybe_static
isinstancer   Tensor)axesr   canonicalizerr   s    `  r   r   r   ,   sC    	t	$dkk"M CM*.	/$-
	//	/s   Ac                  "    t        t        d      S )N	signature)hasattrinspectr   r   r   _supports_signaturer+   9   s    	+	&&r   c                 ,    t        j                  |       S )zConverts a native python or numpy type to TF DType.

  Args:
    dtype: Could be a python type, a numpy type or a TF DType.

  Returns:
    A tensorflow `DType`.
  )r   as_dtypedtypes    r   _to_tf_typer0   =   s     
	r   c                 x    t        | t        j                        r| j                  S t	        j
                  |       S )zConverts a native python or TF DType to numpy type.

  Args:
    dtype: Could be a python type, a numpy type or a TF DType.

  Returns:
    A NumPy `dtype`.
  )r#   r   DTypeas_numpy_dtypenpr/   r.   s    r   _to_numpy_typer5   I   s,     v||$	%r   c                 8   t        | t        j                        r| j                  } t        | t        j
                        rF| j                  j                  }||dk(  S t        j                  t        j                  |       d      S t        j                  |       S )z9Returns whether `val` is a scalar value or scalar Tensor.r   )r#   r   ndarraydatar   r$   shapendimsr   equalr   r   r4   isscalar)valr:   s     r   r<   r<   W   so    Y&&'
((CT[[!IIOOEaZ^^INN3/33;;sr   c                 v    | xr6 t        | d      xr( t        | j                  t              xr | j                  S )N__doc__)r)   r#   r?   str)fs    r   _has_docstringrB   e   s/    NGAy!NjC&@NQYYr   c                 8    | j                  d      r| dz   S | dz   S )N
z

)endswith)ss    r   _add_blank_linerG   k   s!    ZZt8Ov:r   c           
         t        t        d      sy| yt        | t        j                        s	 t        j
                  |       S d } |d| j                        } |d| j                        }g d}g }||D cg c]0  }t        j                  |t        j                  j                        2 c}z  }| j                  dkD  rA||D cg c]2  }t        j                  |t        j                  j                  d      4 c}z  }|t        j                  dt        j                  j                  | j                  dk(  rdnd	| j                  z        gz  }||D cg c]5  \  }}t        j                  |t        j                  j                  |      7 c}}z  }t        j                  |      S # t        $ r Y yw xY wc c}w c c}w c c}}w )
z:An enhanced inspect.signature that can handle numpy.ufunc.r(   Nc                 z    |dk  rg S |dk(  r| gS t        |      D cg c]  }| t        |dz         z    c}S c c}w )Nr      )ranger@   )prefixnis      r   names_from_numz%_np_signature.<locals>.names_from_num   sA    Avi	
aXo+084afs1q5z!444s   8xout))whereT)casting	same_kind)orderK)r/   N)subokT)r(   N)extobjNrJ   )defaultr   )r)   r*   r#   r4   ufuncr(   
ValueErrorninnout	ParameterPOSITIONAL_ONLYPOSITIONAL_OR_KEYWORDKEYWORD_ONLY	Signature)rA   rO   input_namesoutput_nameskeyword_only_paramsparamsnamerY   s           r   _np_signaturerh   r   s    
+	&Y	Arxx	 q!!5 sAEE*+qvv., &
 g//??@ & VVaZ
  	$ 1 1 A A4P F 	




1
1&&A+$7QVV+; & 	.
$ g//<<gN & 
		6	""W  .
s#   F0 95F?7G:G	0	F<;F<c                 *    d } ||        ||      k(  S )Nc                     | t         j                  j                  t         j                  j                  fv rt         j                  j                  S | S r   )r*   r^   r_   ra   r`   )ks    r   relaxz(_is_compatible_param_kind.<locals>.relax   s>    W..0A0A0N0NOO444Hr   r   )abrl   s      r   _is_compatible_param_kindro      s    
 
qU1X	r   c                     | t        | t              sJ |t        |t              rJ || J 	 t        t        t        |             }| |J |j
                  } | |fS # t        $ r d}Y #w xY w)a  Mutually propagates information between `np_fun_name` and `np_fun`.

  If one is None and the other is not, we'll try to make the former not None in
  a best effort.

  Args:
    np_fun_name: name for the np_fun symbol. At least one of np_fun or
      np_fun_name shoud be set.
    np_fun: the numpy function whose docstring will be used.

  Returns:
    Processed `np_fun_name` and `np_fun`.
  N)r#   r@   getattrr4   AttributeError__name__)np_fun_namenp_funs     r   _prepare_np_fun_name_and_funrv      s     k3'''&#&&&^"""r3{+,f //K	f	  fs   A A-,A-c                     |s|sJ |s|j                   }d|z  }|r!|ddj                  d |D              z   dz   z  }t        |       r|| j                  z  }t	        |      }t        ||||      }|S )zHelper to get docs.z%TensorFlow variant of NumPy's `%s`.

zUnsupported arguments: z, c              3   ,   K   | ]  }d |z   d z     yw)`Nr   ).0rg   s     r   	<genexpr>z!_np_doc_helper.<locals>.<genexpr>   s     DC$J$Ds   z.

)link)rs   joinrB   r?   rG   _add_np_doc)rA   np_frt   unsupported_paramsr|   docs         r   _np_doc_helperr      s     
		--K1K?#!
))D1CD
D	E
	C
 A199C
#
C 	Cd6#	*r   TF_NP_DOC_FORMstablec                      t         S )zvGets the form of the original numpy docstrings.

  Returns:
    See `set_np_doc_form` for the list of valid values.
  _np_doc_formr   r   r   get_np_doc_formr      s
     
r   c                     | a y)a  Selects the form of the original numpy docstrings.

  This function sets a global variable that controls how a tf-numpy symbol's
  docstring should refer to the original numpy docstring. If `value` is
  `'inlined'`, the numpy docstring will be verbatim copied into the tf-numpy
  docstring. Otherwise, a link to the original numpy docstring will be
  added. Which numpy version the link points to depends on `value`:
  * `'stable'`: the current stable version;
  * `'dev'`: the current development version;
  * pattern `\d+(\.\d+(\.\d+)?)?`: `value` will be treated as a version number,
    e.g. '1.16'.

  Args:
    value: the value to set the global variable to.
  Nr   values    r   set_np_doc_formr      s	    " ,r   c                       e Zd Zd Zy)Linkc                     || _         y r   r   selfvs     r   __init__zLink.__init__  	    DJr   Nrs   
__module____qualname__r   r   r   r   r   r         r   r   c                       e Zd Zd Zy)AliasOfc                     || _         y r   r   r   s     r   r   zAliasOf.__init__  r   r   Nr   r   r   r   r   r     r   r   r   c                       e Zd Zy)NoLinkN)rs   r   r   r   r   r   r   r     s    r   r   c                 v    | dk(  rd}||z  S | dk(  rd}||z  S t        j                  d|       rd|  d}||z  S y)	zGenerates link from numpy function name.

  Args:
    flag: the flag to control link form. See `set_np_doc_form`.
    np_fun_name: the numpy function name.

  Returns:
    A string.
  devz;https://numpy.org/devdocs/reference/generated/numpy.%s.htmlr   z>https://numpy.org/doc/stable/reference/generated/numpy.%s.htmlz\d+(\.\d+(\.\d+)?)?$zhttps://numpy.org/doc/z"/reference/generated/numpy.%s.htmlN)rematch)flagrt   templates      r   generate_linkr     sk     
U]LH 
K	 xOH 
K	 
xx'.'v-OPH 
K	 r   TF_NP_CHECK_LINKFalse)Truetrue1c                      t         S r   _is_check_linkr   r   r   is_check_linkr   6  s    	r   c                     | a y r   r   r   s    r   set_check_linkr   :  s    .r   c           	         t               }|dk(  r4t        |      r'| d|z  z  } | |j                  j                  dd      z  } | S t	        |t
              r|t        ||      }nFt	        |t              rt        ||j                        }nt	        |t              r|j                  }nd}|Yt               rCddl}|j                  |      }|j                  dk7  rt        d| d	|j                   d
| d      | d|d|dz  } | S )a  Appends the numpy docstring to `doc`, according to `set_np_doc_form`.

  See `set_np_doc_form` for how it controls the form of the numpy docstring.

  Args:
    doc: the docstring to be appended to.
    np_fun_name: the name of the numpy function.
    np_f: (optional) the numpy function.
    link: (optional) which link to use. See `np_doc` for details.

  Returns:
    `doc` with numpy docstring appended.
  inlinedzDocumentation for `numpy.%s`:

z>>>>Nr      zCheck link failed at [z] with status code . Argument `np_fun_name` is .z(See the NumPy documentation for [`numpy.z`](z).)r   rB   r?   replacer#   r@   r   r   r   r   r   requestsheadstatus_coder[   )r   rt   r   r|   r   urlr   rs           r   r~   r~   ?  s    
	$	Yd	0;>>c 
T\\!!%--c2 
*1 $|$,c	D'	"$

+c	D$	JJcc
	MM#==C&se+>q}}o N++6-q:  


 c 
*r   TF_NP_SIG_MISMATCH_IS_ERRORc                      t         S r   _is_sig_mismatch_an_errorr   r   r   is_sig_mismatch_an_errorr   u  s    	""r   c                     | a y r   r   r   s    r   set_is_sig_mismatch_an_errorr   y  s    #r   c                 p      ct               \   t              g  fd}|S )a  Attachs numpy docstring to a function.

  Args:
    np_fun_name: name for the np_fun symbol. At least one of np_fun or
      np_fun_name shoud be set.
    np_fun: (optional) the numpy function whose docstring will be used.
    unsupported_params: (optional) the list of parameters not supported by
      tf.numpy.
    link: (optional) which link to use. If `None`, a default link generated from
      `np_fun_name` will be used. If an instance of `AliasOf`, `link.value` will
      be used in place of `np_fun_name` for the link generation. If an instance
      of `Link`, `link.value` will be used as the whole link. If an instance of
      `NoLink`, no link will be added.

  Returns:
    A function decorator that attaches the docstring from `np_fun` to the
    decorated function.
  c                    t        t        d      r	 t        j                  |       }||j                  j                         D ]@  \  }}j                  j                  |      }|Dt               r9t        d| dt        j                  j                                d
 d d	      ht               rOt        |j                  |j                        s/t        d| d	|j                   d
|j                   d
 d d      |j                  t        j                  j                  k7  }|j                  t        j                  j                  k7  }t               s||k7  s"t        dj!                  ||rdnd
             j                  D ]"  }||j                  vsj#                  |       $ t%        | 	      | _        | S # t        $ r d}Y w xY w)zThe decorator.r(   NzCannot find parameter z@ in the numpy function's signature (which has these parameters: z). Argument `np_fun_name` is z. Argument `np_fun` is r   z
Parameter z is of kind z while in numpy it is of kind r   zbParameter {} should{} have a default value. Argument `np_fun_name` is {}. Argument `np_fun` is {}. z not)rt   r   r|   )r)   r*   r(   r[   
parametersitemsgetr   	TypeErrorlistkeysro   kindrY   r^   emptyformatappendr   r?   )rA   sigrg   paramnp_paramhas_defaultnp_has_defaultr|   ru   rt   np_fun_name_orignp_fun_orignp_sigr   s          r   	decoratorznp_doc.<locals>.decorator  s   w$);" 
>>//1 	KD%&&**40(')*4& 1<&++00234 5())@QP  %'0Ijj(--1 TF,uzzl ;&mm_,H#$$;K=K 
 ):):)@)@@+#++w/@/@/F/FF.%'K>,I@@F(Bf$	A /	@ %% 	,D'%%d+	, 	-AI H[  s   G G$#G$)rv   rh   )rt   ru   r   r|   r   r   r   r   s   ```` @@@r   np_docr   ~  sL    & #.vK4[&I+v &2 2h 
r   c                 4     t               \    fd}|S )a  Attachs numpy docstring to a function.

  This differs from np_doc in that it doesn't check for a match in signature.

  Args:
    np_fun_name: name for the np_fun symbol. At least one of np_fun or
      np_fun_name shoud be set.
    np_fun: (optional) the numpy function whose docstring will be used.

  Returns:
    A function decorator that attaches the docstring from `np_fun` to the
    decorated function.
  c                 .    t        |       | _        | S )N)rt   )r   r?   )rA   ru   rt   s    r   r   znp_doc_only.<locals>.decorator  s    q&kBAIHr   )rv   )rt   ru   r   s   `` r   np_doc_onlyr     s#     5[&I+v 
r   zexperimental.numpy.finfo)v1finfoc                 >    t        j                  t        |             S )zNote that currently it just forwards to the numpy namesake, while

  tensorflow and numpy dtypes may have different properties.
  )r4   r   r5   r.   s    r   r   r     s     
.'	((r   c                 n   t        | t        j                        r| S t        | t        j                        st        j                  |       rt        | j                        S t        | t        j                        r| j                  S t        | t        t        f      rt        dt        |        d      | S )zFReturns a numpy type if available from x. Skips if x is numpy.ndarray.zJCannot find dtype for type inference from argument `x` of a sequence type zH. For sequences, please call this function on each element individually.)r#   numbersRealr   IndexedSlicesr   
is_tf_typer5   r/   r   r2   r3   r   tupler[   typerP   s    r   _maybe_get_dtyper     s     7<< H>//0K4J4J14M!''""6<< D%=!
	Qy  	  
 
(r   zexperimental.numpy.result_typeresult_typec                     t        j                         rt        j                  |  \  }}|S t	        j
                  |       D cg c]  }t        |       } }| st        j                  g       g} t        j                  |  S c c}w r   )r    is_auto_dtype_conversion_enabledr   r   r   flattenr   r4   asarrayr   _result_type)arrays_and_dtypesr/   _rP   s       r   r   r     s     	))+**,=>HE1L#'<<0A#Bq  
B(			!2	33s   Bc                     |rt        |      S t        | t              rt        j                  S t        | t
              rt        j                  S t        |       S )z5Find the result type from a single input and a dtype.)r   r#   r@   r4   str_bytesbytes_)rm   r/   s     r   result_type_unaryr     sE    
u 377N!U99 
Qr   c                     	 t        j                  t        |       t        |            S # t        $ r t	        | |      cY S w xY w)zHA specialization of result_type for 2 arguments for performance reasons.)r   r   r   r[   r   )t1t2s     r   _result_type_binaryr   )  sH    !!  
 r2s   '* AAz experimental.numpy.promote_typespromote_typesc                     t        |       } t        |      }t        j                  t        j                  | |            S r   )r5   r   canonicalize_dtyper4   r   )type1type2s     r   r   r   4  s6     
%

%		%	%b&6&6ue&D	EEr   c                  
   t        |       dk  r| S t        j                  | d         }| dd D ]+  }t        j                  |t        j                  |            }- | D cg c]  }t        j                  ||       c}S c c}w )zBroadcast tensors.

  Args:
    *args: a list of tensors whose shapes are broadcastable against each other.

  Returns:
    Tensors broadcasted to the common shape.
  rJ   r   N)lenr   r9   broadcast_dynamic_shapebroadcast_to)argsshargs      r   tf_broadcastr  <  sy     	Y!^KtAw"!"X Ec		*	*2ys/C	DBE59	:c)
 
 b
)	::	:s    B c                     t        | t        j                        r-| j                  j                  s| j                  j
                  ryt        j                  |       S )a  A version of tf.get_static_value that returns None on float dtypes.

  It returns None on float dtypes in order to avoid breaking gradients.

  Args:
    x: a tensor.

  Returns:
    Same as `tf.get_static_value`, except that it returns None when `x` has a
    float dtype.
  N)r#   r   r$   r/   is_floating
is_complexr   constant_valuer   s    r   get_static_valuer  Q  s>     4;;QWW%8%8AGG<N<N		#	#A	&&r   c                 $    t        |       }|| S |S r   )r  )rP   r   s     r   r"   r"   b  s    
1
%
]HLr   c                 j    t        |       }|t        j                  | ||      S |r |       S  |       S )z:A version of tf.cond that tries to evaluate the condition.)r  tf_condr
   )predtrue_fnfalse_fnr   s       r   r
   r
   n  s7    t!Y<<gx009:r   c                 0    t        |       t        |      z   S )z7A version of tf.add that eagerly evaluates if possible.r"   rm   rn   s     r   addr  y      	q	M!,	,,r   c                 0    t        |       t        |      z
  S )z<A version of tf.subtract that eagerly evaluates if possible.r  r  s     r   subtractr  ~  r  r   c                 0    t        |       t        |      kD  S )z;A version of tf.greater that eagerly evaluates if possible.r  r  s     r   greaterr    r  r   c                 0    t        |       t        |      k\  S )zAA version of tf.greater_equal that eagerly evaluates if possible.r  r  s     r   greater_equalr        	q	]1-	--r   c                 0    t        |       t        |      k  S )z>A version of tf.less_equal that eagerly evaluates if possible.r  r  s     r   
less_equalr    r  r   c                     t        |       }|2t        j                  |      r|rt        |      S |S |t        |      z  S | t        |      z  S )z?A version of tf.logical_and that eagerly evaluates if possible.r  r4   r<   r"   rm   rn   a_values      r   logical_andr$    sQ    Q'	{{7	Q}Q'''}Qr   c                     t        |       }|2t        j                  |      r|r|S t        |      S |t        |      z  S | t        |      z  S )z>A version of tf.logical_or that eagerly evaluates if possible.r!  r"  s      r   
logical_orr&    sQ    Q'	{{7	Q}Q'''}Qr   c                     t        |       |   S )z<A version of __getitem__ that eagerly evaluates if possible.r  )rm   
slice_specs     r   getitemr)    s    	q	*	%%r   c                 r    t        |       }|t        j                  | ||      S |j                  ||      S )z>A version of tf.reduce_all that eagerly evaluates if possible.r   keepdims)r  r   
reduce_allallinput_tensorr   r,  r   s       r   r-  r-    9    |$!Y|$JJ55dX5..r   c                 r    t        |       }|t        j                  | ||      S |j                  ||      S )z>A version of tf.reduce_any that eagerly evaluates if possible.r+  )r  r   
reduce_anyanyr/  s       r   r3  r3    r1  r   c                 `    | j                   j                  }||S t        j                  |       S r   )r9   r   r   )tr   s     r   tf_rankr7    s(    ggll!]H		r   )NNNr   )NF)Kr?   r*   r   osr   numpyr4   tensorflow.python.frameworkr   r   r   r   r   tensorflow.python.opsr   r
   r  r   tensorflow.python.ops.numpy_opsr   r   tensorflow.python.typesr   tensorflow.python.utilr   r   r   r   r+   r0   r5   r<   rB   rG   rh   ro   rv   r   getenvr   r   r   r   r   r   r   r   r   r   r~   r   r   r   r   r   r   r   r   r   r   r   r  r  r"   r  r  r  r  r  r$  r&  r)  r-  r3  r7  r   r   r   <module>r@     s   *   	 	  . 7 6 + 3 + 1 * 5 5 ( ' ,-
0'	 6#x> >B. ryy)84(    . -w7;PP
.b &BII!7 
#$
M`0 /B7)  8)& 5"=]4  >4$ 7B?F  @F;*'"-
-
-
.
.
  &
//r   