
    -i                     (   S r SSKrSSKrSSKrSSKJr  SSK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  SSKJr  S	S
KJr  S/rSS1rS>S jrS>S jrS rS rS rS\4S.S jrS r S r!S r"S r#S r$S r%S r&S r'S r(S\4S.S jr)S\4SS .S! jr*S\4SS .S" jr+S?S# jr,S@S$ jr-S% r.S& r/S' r0SAS( jr1S?S) jr2SBS* jr3SBS+ jr4SBS, jr5 SCSSS-.S. jjr6S?S/ jr7S0 r8S1 r9S2 r:S3 r;S4SSS5.S6 jr<SDS7 jr=SDS8 jr>SES9 jr?SS:.S; jr@SCS< jrAS?S= jrBg)FzTools to support array_api.    Nwraps   )
get_config)array_api_compat)array_api_extra)numpy   )parse_versionxpxr	   z(sklearn.externals.array_api_compat.numpyTc              #   L   #    S H  nU (       d  U[         ;   a  M  Uv   M     g7f)a  Yield supported namespace.

This is meant to be used for testing purposes only.

Parameters
----------
include_numpy_namespaces : bool, default=True
    If True, also yield numpy namespaces.

Returns
-------
array_namespace : str
    The name of the Array API namespace.
)r	   array_api_strictcupytorchN)_NUMPY_NAMESPACE_NAMES)include_numpy_namespacesarray_namespaces     K/var/www/html/venv/lib/python3.13/site-packages/sklearn/utils/_array_api.pyyield_namespacesr      s'     
 (O?U,U
s   "$c              #   P  #    [        U S9 Hv  nUS:X  a/  [        R                  " SS5       H  u  p#XU4v   M     USS4v   M8  US:X  a1   SS	KnXR	                  S
5      S4v   XR	                  S5      S4v   Mo  US	S	4v   Mx     g	! [
         a    US
S4v   USS4v    M  f = f7f)a  Yield supported namespace, device, dtype tuples for testing.

Use this to test that an estimator works with all combinations.
Use in conjunction with `ids=_get_namespace_device_dtype_ids` to give
clearer pytest parametrization ID names.

Parameters
----------
include_numpy_namespaces : bool, default=True
    If True, also yield numpy namespaces.

Returns
-------
array_namespace : str
    The name of the Array API namespace.

device : str
    The name of the device on which to allocate the arrays. Can be None to
    indicate that the default value should be used.

dtype_name : str
    The name of the data type to use for arrays. Can be None to indicate
    that the default value should be used.
)r   r   )cpucuda)float64float32mpsr   r   r   N
CPU_DEVICEr   device1)r   	itertoolsproductr   DeviceImportError)r   r   devicedtyper   s        r   )yield_namespace_device_dtype_combinationsr$   ;   s     2 ,!9 g%!*!2!2!7" &u44" "5)33 22
<'%'>'>|'LiWW%'>'>y'I9TT "4--/   < &|Y>>%y);;<s*   AB&.B9B&B#B&"B##B&c                      SSK nXR                  S5      :X  a  gXR                  S5      :X  a  gXR                  S5      :X  a  gg! [         a     gf = f)zNGet pytest parametrization IDs for `yield_namespace_device_dtype_combinations`r   Nr   r   device2)r   r    r!   )paramr   s     r   _get_namespace_device_dtype_idsr(   n   sa    
 ++L99++I66++I66 7  s   A 
AAc                     U (       d  g[        [        R                  5      nSnU[        U5      :  a  [        SU S35      e[        R
                  R                  S5      S:w  a  [        S5      eg)zCheck that array_api_compat is installed and NumPy version is compatible.

array_api_compat follows NEP29, which has a higher minimum NumPy version than
scikit-learn.
Nz1.14.0zSciPy must be zY or newer (found {scipy.__version__}) to dispatch array using the array API specificationSCIPY_ARRAY_API1zScikit-learn array API support was enabled but scipy's own support is not enabled. Please set the SCIPY_ARRAY_API=1 environment variable before importing sklearn or scipy. More details at: https://docs.scipy.org/doc/scipy/dev/api-dev/array_api.html)r   scipy__version__r!   osenvirongetRuntimeError)array_api_dispatchscipy_versionmin_scipy_versions      r   _check_array_api_dispatchr5      s~     !%"3"34M }%677./ 0+ +
 	
 
zz~~'(C/J
 	
 0    c                     [        U [        R                  [        R                  45      (       d#  [	        U S5      (       a  [        5       S   (       d  gU R                  $ )z0Hardware device where the array data resides on.r"   r2   N)
isinstancer	   ndarraygenerichasattrr   r"   )arrays    r   _single_array_devicer=      sE     	55==%--899uh'' |01||r6   remove_noneremove_typesc                     [        X US.6nU(       d  g[        US   5      nUSS  H$  n[        U5      nX5:w  d  M  [        SU SU 35      e   U$ )a  Hardware device where the array data resides on.

If the hardware device is not the same for all arrays, an error is raised.

Parameters
----------
*array_list : arrays
    List of array instances from NumPy or an array API compatible library.

remove_none : bool, default=True
    Whether to ignore None objects passed in array_list.

remove_types : tuple or list, default=(str,)
    Types to ignore in array_list.

Returns
-------
out : device
    `device` object (see the "Device Support" section of the array API spec).
r>   Nr   r
   z$Input arrays use different devices: z, )_remove_non_arraysr=   
ValueError)r?   r@   
array_listdevice_r<   device_others         r   r"   r"      so    * $	<J ":a=1G AB+E2"6wir,P    Nr6   c                 B    [         R                  " U R                  5      $ )zReturn the total number of elements of x.

Parameters
----------
x : array
    Array instance from NumPy or an array API compatible library.

Returns
-------
out : int
    Total number of elements.
)mathprodshape)xs    r   sizerL      s     99QWWr6   c                 (    U R                   [        ;   $ )z%Return True if xp is backed by NumPy.)__name__r   xps    r   _is_numpy_namespacerQ      s    ;;000r6   c                 V   [        U5      (       a4  SSKJn  U" XUS9u  pEUR                  [        R
                  " XE5      5      $ U R                  UR                  s=:X  a  S:X  d   e   eUR                  UR                  UR                  U 5      UR                  U5      /5      5      $ )Nr
   )cached_uniquerO   )	rQ   _uniquerS   asarrayr	   union1dndimunique_valuesconcat)abrP   rS   a_uniqueb_uniques         r   _union1dr^      s    2**1B7zz%--;<<66QVV q     BIIr'7'7':B<L<LQ<O&PQRRr6   c                r   ^ ^ [        U[        5      (       a  [        U U4S jU 5       5      $ [        T UTS9$ )zReturns a boolean indicating whether a provided dtype is of type "kind".

Included in the v2022.12 of the Array API spec.
https://data-apis.org/array-api/latest/API_specification/generated/array_api.isdtype.html
c              3   :   >#    U  H  n[        TUTS 9v   M     g7frO   N_isdtype_single.0kr#   rP   s     r   	<genexpr>isdtype.<locals>.<genexpr>   s     BT?5!3T   rO   )r8   tupleanyrc   )r#   kindrP   s   ` `r   isdtyperm      s2     $BTBBBudr22r6   c                  ^ ^ [        U[        5      (       GaK  US:X  a  T TR                  :H  $ US:X  a1  T TR                  TR                  TR
                  TR                  1;   $ US:X  a1  T TR                  TR                  TR                  TR                  1;   $ US:X  a  [        U U4S jS 5       5      $ US:X  a  T [        T5      ;   $ US:X  ag  [        5       n[        TS	5      (       a  UR                  TR                   5        [        TS
5      (       a  UR                  TR"                  5        T U;   $ US:X  a  [        U U4S jS 5       5      $ [%        SU< 35      eT U:H  $ )Nboolsigned integerunsigned integerintegralc              3   :   >#    U  H  n[        TUTS 9v   M     g7fra   rb   rd   s     r   rg   "_isdtype_single.<locals>.<genexpr>  s!      ?A  qR0?ri   )rp   rq   real floatingcomplex floating	complex64
complex128numericc              3   :   >#    U  H  n[        TUTS 9v   M     g7fra   rb   rd   s     r   rg   rt     s!      JA  qR0Jri   )rr   ru   rv   zUnrecognized data type kind: )r8   strro   int8int16int32int64uint8uint16uint32uint64rk   supported_float_dtypessetr;   addrw   rx   rC   )r#   rl   rP   complex_dtypess   ` ` r   rc   rc     sW   $6>BGG##%%RWWbhh"((CCC''RXXryy"))RYYGGGZ ?   _$22666'' UNr;''""2<<0r<((""2==1N**Y J  
 <THEFF}r6   c                     [        U S5      (       a#  U R                  U R                  U R                  4$ U R                  U R                  4$ )a-  Supported floating point types for the namespace.

Note: float16 is not officially part of the Array API spec at the
time of writing but scikit-learn estimators and functions can choose
to accept it when xp.float16 is defined.

https://data-apis.org/array-api/latest/API_specification/data_types.html
float16)r;   r   r   r   rO   s    r   r   r   %  s=     r9

BJJ

33

BJJ''r6   c                     [        U 5      u  p#U(       a*  [        U 5      nU Vs/ s H  oRR                  XTS9PM     sn$ U$ s  snf )ab  Ensure that all arrays use the same namespace and device as reference.

If necessary the arrays are moved to the same namespace and device as
the reference array.

Parameters
----------
reference : array
    Reference array.

*arrays : array
    Arrays to check.

Returns
-------
arrays : list
    Arrays with the same namespace and device as reference.
r"   )get_namespacer"   rU   )	referencearraysrP   is_array_apirE   rZ   s         r   ensure_common_namespace_devicer   4  sF    & %Y/B#7=>v!

1
-v>> ?s   Ac                 .    U S;  a  [        SU < 35      eg )N>   Nr   zUnsupported device for NumPy: )rC   r   s    r   _check_device_cpur   Q  s"    ]"9&DEE #r6   c                 0   ^  [        T 5      U 4S j5       nU$ )Nc                  J   > [        UR                  SS 5      5        T" U 0 UD6$ )Nr"   )r   pop)argskwargsfuncs     r   wrapped_func(_accept_device_cpu.<locals>.wrapped_funcW  s&    &**Xt45T$V$$r6   r   )r   r   s   ` r   _accept_device_cpur   V  s!    
4[% % r6   c                     / n[        U5      nU HO  nU (       a  Uc  M  [        XA5      (       a  M!  [        R                  " U5      (       a  M>  UR	                  U5        MQ     U$ )a  Filter arrays to exclude None and/or specific types.

Sparse arrays are always filtered out.

Parameters
----------
*arrays : array objects
    Array objects.

remove_none : bool, default=True
    Whether to ignore None objects passed in arrays.

remove_types : tuple or list, default=(str,)
    Types to ignore in the arrays.

Returns
-------
filtered_arrays : list
    List of arrays filtered as requested. An empty list is returned if no input
    passes the filters.
)rj   r8   spissparseappend)r?   r@   r   filtered_arraysr<   s        r   rB   rB   _  s]    , O&L5=e**;;uu%  r6   )r?   r@   rP   c                 0   [        5       S   nU(       d  Ub  US4$ [        S4$ Ub  US4$ [        UU US.6nU(       d  [        S4$ [        U5        [        R
                  " U6 SpeUR                  S:X  a   [        US5      (       a  UR                  SS9  XV4$ )	a+  Get namespace of arrays.

Introspect `arrays` arguments and return their common Array API compatible
namespace object, if any.

Note that sparse arrays are filtered by default.

See: https://numpy.org/neps/nep-0047-array-api-standard.html

If `arrays` are regular numpy arrays, `array_api_compat.numpy` is returned instead.

Namespace support is not enabled by default. To enabled it call:

  sklearn.set_config(array_api_dispatch=True)

or:

  with sklearn.config_context(array_api_dispatch=True):
      # your code here

Otherwise `array_api_compat.numpy` is
always returned irrespective of the fact that arrays implement the
`__array_namespace__` protocol or not.

Note that if no arrays pass the set filters, ``_NUMPY_API_WRAPPER_INSTANCE, False``
is returned.

Parameters
----------
*arrays : array objects
    Array objects.

remove_none : bool, default=True
    Whether to ignore None objects passed in arrays.

remove_types : tuple or list, default=(str,)
    Types to ignore in the arrays.

xp : module, default=None
    Precomputed array namespace module. When passed, typically from a caller
    that has already performed inspection of its own inputs, skips array
    namespace inspection.

Returns
-------
namespace : module
    Namespace shared by array objects. If any of the `arrays` are not arrays,
    the namespace defaults to the NumPy namespace.

is_array_api_compliant : bool
    True if the arrays are containers that implement the array API spec (see
    https://data-apis.org/array-api/latest/index.html).
    Always False when array_api_dispatch=False.
r2   FTr>   r   set_array_api_strict_flagsz2024.12)api_version)	r   	np_compatrB   r5   r   r   rN   r;   r   )r?   r@   rP   r   r2   	namespaceis_array_api_compliants          r   r   r     s    n $&:;>u9e##	~4x	!F %01(8(F(F(OQU%//G/5 5 	,,,C,,r6   c                     [        S/ S9n[        UU US.6n[        U0 UD6nUc  [        U0 UD6u  p&OUSpbU(       a  X&U4$ USU4$ )a  Combination into one single function of `get_namespace` and `device`.

Parameters
----------
*array_list : array objects
    Array objects.
remove_none : bool, default=True
    Whether to ignore None objects passed in arrays.
remove_types : tuple or list, default=(str,)
    Types to ignore in the arrays.
xp : module, default=None
    Precomputed array namespace module. When passed, typically from a caller
    that has already performed inspection of its own inputs, skips array
    namespace inspection.

Returns
-------
namespace : module
    Namespace shared by array objects. If any of the `arrays` are not arrays,
    the namespace defaults to NumPy.
is_array_api_compliant : bool
    True if the arrays are containers that implement the Array API spec.
    Always False when array_api_dispatch=False.
device : device
    `device` object (see the "Device Support" section of the array API spec).
Fr>   T)dictrB   r"   r   )r?   r@   rP   rD   skip_remove_kwargsarrays_devicer   s          r   get_namespace_and_devicer     sr    : %bA#	!J
 J=*<=M	z(*K8JKLtL..5-''r6   c                     [        XS9u  p[        U5      (       a9  UR                  [        R                  " [
        R                  " U 5      5      5      $ SSUR                  U * 5      -   -  $ )NrO   g      ?)r   rQ   rU   specialexpitr	   exp)XrP   _s      r   _expitr   
  sT    !#EB2zz'--a(89::#r
"##r6   c                 ~   U R                   S:w  a!  [        S[        U R                  5       35      eUR	                  XR
                  [        U 5      S9nSnU R                  S   S-   nU(       d  U R                  S   U R                  S   -  nUR                  U S5      nU(       a  USXV2==   U-  ss'   gXSXV2'   g)a  Implementation to facilitate adding or assigning specified values to the
diagonal of a 2-d array.

If ``add_value`` is `True` then the values will be added to the diagonal
elements otherwise the values will be assigned to the diagonal elements.
By default, ``add_value`` is set to `True. This is currently only
supported for 2-d arrays.

The implementation is taken from the `numpy.fill_diagonal` function:
https://github.com/numpy/numpy/blob/v2.0.0/numpy/lib/_index_tricks_impl.py#L799-L929
r   z*array should be 2-d. Got array with shape r#   r"   Nr
   )rW   rC   rj   rJ   rU   r#   r"   reshape)r<   valuerP   	add_valuewrapendstep
array_flats           r   _fill_or_add_to_diagonalr     s     zzQ8u{{9K8LM
 	
 JJuKKuJFE
C ;;q>ADkk!nu{{1~-E5)J9C9& %9C9r6   c                 2    U R                   USU 3SU 34;   $ )Nzarray_api_compat.z#sklearn.externals.array_api_compat.)rN   )rP   names     r   _is_xp_namespacer   2  s/    ;;
D6"
-dV4  r6   c                     [        U S5      (       a+  [        U5      R                  S5      (       a  U R                  $ U R                  $ )zJReturn the float dtype with the highest precision supported by the device.r   r   )r   r{   
startswithr   r   rP   r"   s     r   _max_precision_float_dtyper   :  s@     G$$V)?)?* * zz::r6   c                 \   U Vs/ s H'  n[        US5      (       d  M  U R                  U5      PM)     nnU Vs/ s H1  o R                  UR                  S5      (       d  M%  UR                  PM3     nnU(       a  U R                  " U6 $ U R                  S5      R                  $ s  snf s  snf )a  Find a suitable floating point dtype when computing with arrays.

If any of the arrays are floating point, return the dtype with the highest
precision by following official type promotion rules:

https://data-apis.org/array-api/latest/API_specification/type_promotion.html

If there are no floating point input arrays (all integral inputs for
instance), return the default floating point dtype for the namespace.
r#   ru           )r;   rU   rm   r#   result_type)rP   r   rZ   dtyped_arraysfloating_dtypess        r   _find_matching_floating_dtyper   E  s     -3JFqga6I]RZZ]FMJ&&A**QWWo*N   ~~//
 ::c?    Ks   B$B$$B)!B)c           	      2   [        X5      u  pEn[        U5      (       aV  U(       a$  UR                  [        R                  " XUS95      $ Uc(  Ub%  UR                  [        R
                  " X5      5      $ UR                  XS9n Ub  UR                  X&S9nUb  U R                  UR                  :w  a  Uc8  [        S[        U R                  5       S[        UR                  5       S35      e[        UR                  5      U R                  U   4:w  a<  [        S[        UR                  5       S[        U R                  5       SU< S	35      eS
/U R                  -  nU R                  U   Xq'   UR                  U[        U5      5      nUR                  U R                  S5      (       a  [        S5      eUb,  UR                  UR                  S5      (       a  [        S5      e[        XUS9nUR!                  X5      n Uc#  U(       a  UR"                  OUR$                  " XS9$ UR!                  X(5      nUR%                  UR'                  X5      US9n	U(       d  U	$ UR%                  X!S9n
UR)                  U
S:H  5      (       a  [+        S5      eX-  $ )zPartial port of np.average to support the Array API.

It does a best effort at mimicking the return dtype rule described at
https://numpy.org/doc/stable/reference/generated/numpy.average.html but
only for the common cases needed in scikit-learn.
)axisweightsr   z+Axis must be specified when the shape of a z and weights z differ.zShape of weights weights.shape=z! must be consistent with a.shape=z
 and axis=.r
   rv   z;Complex floating point values are not supported by average.rO   r   r   z(Weights sum to zero, can't be normalized)r   rQ   rU   r	   averagedotrJ   	TypeErrorrj   rC   rW   r   rm   r#   NotImplementedErrorr   astypemeansummultiplyrk   ZeroDivisionError)rZ   r   r   	normalizerP   r   rE   rJ   output_dtypesum_scales              r   _averager   ^  s]    .a9NB72::emmA'JKK\g1::eii344


1
%A**W*5qww'--7<=eAGGn=M N /0: 
 AGGDM#331%2F1G H++0>*:+waI  affggdm**WeEl3	zz!''-..!I
 	
 rzz'--9KLL!I
 	
 1CL
		!"A$"&&0!??ii.G66"++a)65DFF7F&E	vvesl JKK<r6   c           	          [        XUS9u  p#n[        R                  " SSS9   XR                  U5      -  nS S S 5        UR	                  U S:H  UR                  SWR                  US9U5      $ ! , (       d  f       N== f)NrO   ignore)divideinvalidr   r   )r   r	   errstatelogwhererU   r#   )rK   yrP   r   rE   temps         r   _xlogyr     sh    -ar:NB7	x	:66!9} 
;88AHbjjDJJwjOQUVV 
;	:s   A..
A<c                    [        XS9u  p#n[        U5      (       a#  UR                  [        R                  " XS95      $ UR                  U 5      nUR                  UR                  XRR                  UR                  7U R                  US9U 5      US9n UR                  XQS9nUR                  U5      (       a5  UR                  XRR                  UR                  U R                  US9U 5      n U $ NrO   r   r   )r   rQ   rU   r	   nanminisnanminr   infr#   allrk   nanr   r   rP   r   rE   masks         r   _nanminr          .a7NB72zz%,,q455 xx{FFHHT::rvvgQWWW:MqQ  

 vvdv&66$<<zz"&&zPRSTAr6   c                    [        XS9u  p#n[        U5      (       a#  UR                  [        R                  " XS95      $ UR                  U 5      nUR                  UR                  XRR                  UR                  * U R                  US9U 5      US9n UR                  XQS9nUR                  U5      (       a5  UR                  XRR                  UR                  U R                  US9U 5      n U $ r   )r   rQ   rU   r	   nanmaxr   maxr   r   r#   r   rk   r   r   s         r   _nanmaxr     r   r6   c                    [        XS9u  p#n[        U5      (       a#  UR                  [        R                  " XS95      $ UR                  U 5      nUR                  UR                  XRR                  SU R                  US9U 5      US9nUR                  UR                  UR                  U5      U R                  5      US9nXg-  $ )NrO   r   r   r   )r   rQ   rU   r	   nanmeanr   r   r   r#   r   logical_not)r   r   rP   r   rE   r   totalcounts           r   _nanmeanr     s     .a7NB72zz%--566xx{HHT::c:I1MTX  
 ryy!5qww?dK}r6   r   c                    [        XS9u  pF[        U5      (       aA  USL a  [        R                  " XUS9n O[        R                  " XUS9n UR	                  U 5      $ UR	                  XX5S9$ )a  Helper to support the order kwarg only for NumPy-backed arrays

Memory layout parameter `order` is not exposed in the Array API standard,
however some input validation code in scikit-learn needs to work both
for classes and functions that will leverage Array API only operations
and for code that inherently relies on NumPy backed data containers with
specific memory layout constraints (e.g. our own Cython code). The
purpose of this helper is to make it possible to share code for data
container validation without memory copies for both downstream use cases:
the `order` parameter is only enforced if the input array implementation
is NumPy based, otherwise `order` is just silently ignored.
rO   T)orderr#   )r#   copyr"   )r   rQ   r	   r<   rU   )r<   r#   r   r  rP   r"   r   s          r   _asarray_with_orderr    sf     %'EB24<KK%@EMM%EBE zz%  zz%4zGGr6   c                     [        XS9u  p[        U5      (       a:  [        R                  " U 5      n UR                  [        R                  " U SS95      $ UR                  U SS9$ )zArray API compliant version of np.ravel.

For non numpy namespaces, it just returns a flattened array, that might
be or not be a copy.
rO   C)r   r   )rJ   )r   rQ   r	   rU   ravelr   )r<   rP   r   s      r   _ravelr    sV     %'EB2e$zz%++e3788::e5:))r6   c                 T   [        US5      (       a  U R                  5       R                  5       $ [        US5      (       a  U R                  5       $ [        US5      (       a2  [        R                  " UR	                  XR                  S5      S95      $ [        R                  " U 5      $ )z*Convert X into a NumPy ndarray on the CPU.r   r   r   r   r   )r   r   r	   r0   rU   r    )r<   rP   s     r   _convert_to_numpyr    s}    G$$yy{  ""	"f	%	%yy{	"0	1	1}}RZZii6MZNOO==r6   c                     SSK Jn  U" U 5      n[        U 5      R                  5        HI  u  pE[	        US5      (       d  [        U[        R                  5      (       a  U" U5      n[        X4U5        MK     U$ )a  Create new estimator which converting all attributes that are arrays.

The converter is called on all NumPy arrays and arrays that support the
`DLPack interface <https://dmlc.github.io/dlpack/latest/>`__.

Parameters
----------
estimator : Estimator
    Estimator to convert

converter : callable
    Callable that takes an array attribute and returns the converted array.

Returns
-------
new_estimator : Estimator
    Convert estimator
r   )clone
__dlpack__)	sklearn.baser
  varsitemsr;   r8   r	   r9   setattr)	estimator	converterr
  new_estimatorkey	attributes         r    _estimator_with_converted_arraysr    sc    & #)$My///19l++z)U]]/S/S!),II. 2 r6   c                 n    U c  [         R                  n [         R                  " U 5      R                  S-  $ )z6Return the absolute tolerance for a given numpy dtype.d   )r	   r   finfoeps)dtype_or_dtype_names    r   _atol_for_typer  5  s/    " $mm;;*+//#55r6   c                 8    U R                  S5      R                  $ )a-  Return a platform-specific integer dtype suitable for indexing.

On 32-bit platforms, this will typically return int32 and int64 otherwise.

Note: using dtype is recommended for indexing transient array
datastructures. For long-lived arrays, such as the fitted attributes of
estimators, it is instead recommended to use platform-independent int32 if
we do not expect to index more 2B elements. Using fixed dtypes simplifies
the handling of serialized models, e.g. to deploy a model fit on a 64-bit
platform to a target 32-bit platform such as WASM/pyodide.
r   )rU   r#   rO   s    r   indexing_dtyper  ?  s    * ::a=r6   left)sidesorterrP   c                    [        XUS9u  pE[        US5      (       a  UR                  XX#S9$ [        XS9n[        XS9n[        R                  " XgX#S9nUR                  U[        U 5      S9$ )NrO   searchsorted)r  r   r   )r   r;   r"  r  r	   rU   r"   )	rZ   vr  r   rP   r   a_npv_npindicess	            r   _searchsortedr'  W  so    
 !2&EBr>""q$>>Q&DQ&D  $FG::gfQi:00r6   c           
         [        U5      (       a&  UR                  [        R                  " U UUUS95      $ U R                  nUR                  U S5      n UR                  US5      nUR                  [        U UUUUS9U5      $ )zCalculates ``element in test_elements``, broadcasting over `element`
only.

Returns a boolean array of the same shape as `element` that is True
where an element of `element` is in `test_elements` and False otherwise.
)elementtest_elementsassume_uniqueinvertr   )ar1ar2rP   r+  r,  )rQ   rU   r	   isinrJ   r   _in1d)r)  r*  rP   r+  r,  original_element_shapes         r   _isinr2  f  s     2zzJJ++	
 	
 %]]jj%(GJJ}e4M::'	
 		 	r6   c                    [        XUS9u  p%UR                  S   SU R                  S   S-  -  :  a  U(       aC  UR                  U R                  S   UR                  [	        U 5      S9nU H
  nX`U:g  -  nM     U$ UR                  U R                  S   UR                  [	        U 5      S9nU H
  nX`U:H  -  nM     U$ U(       d$  UR                  U 5      u  pUR                  U5      nUR                  X45      n	[	        U	5      n
UR                  U	SS9nUR                  USS9nUR                  XSS9n[        U5      S	:  a  U(       a  US	S
 US
S :g  O
US	S
 US
S :H  nO+U(       a  UR                  S/5      OUR                  S/5      nUR                  XR                  U/U
S945      nUR                  XSS9nU(       a  US
U R                  S    $ UR                  UWSS9$ )ah  Checks whether each element of an array is also present in a
second array.

Returns a boolean array the same length as `ar1` that is True
where an element of `ar1` is in `ar2` and False otherwise.

This function has been adapted using the original implementation
present in numpy:
https://github.com/numpy/numpy/blob/v1.26.0/numpy/lib/arraysetops.py#L524-L758
rO   r   
   g(\?r   T)stabler   r
   Nr   Fr   )r   rJ   onesro   r"   zerosunique_inverserX   rY   argsorttakerL   rU   )r-  r.  rP   r+  r,  r   r   rZ   rev_idxarrE   r   reverse_ordersarbool_arflagrets                    r   r0  r0    s    #r*EB yy|b399Q<500077399Q<rwwvc{7KDq    88CIIaLs8LDq  ((-s#	C:	BRjGJJr$J'EJJuTJ2M
''"!'
$CCyA~)/#ab'S"X%SWCR5H *0"**eW%RZZ5G99gzz6(7zCDED
''$A'
.C>SYYq\""wwsG!w,,r6   c                    SSK Jn  [        XUS9u  p6[        U5      (       a"  [        R
                  " U 5      (       a  U" XUS9$ U R                  S:X  d   eUR                  XS9nUbM  UR                  X$S9nUR                  X"R                  S   S45      nUR                  XrR                  5      U-  nUR                  SXGR                  S9nUR                  UR                  U S:g  Xx5      US	9$ )
zA variant of `sklearn.utils.sparsefuncs.count_nonzero` for the Array API.

If the array `X` is sparse, and we are using the numpy namespace then we
simply call the original function. This function only supports 2D arrays.
r
   )count_nonzerorO   )r   sample_weightr   r   r   )r"   r#   r   )sparsefuncsrC  r   rQ   r   r   rW   	ones_likerU   r   rJ   r   r#   r   r   )	r   r   rD  rP   r"   rC  r   r   zero_scalars	            r   _count_nonzerorH    s     +!r2EB22;;q>>QGG66Q;;ll1l,G 

=
@

=3F3Fq3I12MN))G%8%89MI**Qv]]*CK66"((1678t6DDr6   )outc                N    [        U 5      (       a  U" USU0UD6  U$ U" U0 UD6nU$ )NrI  )rQ   )rP   r   rI  r   r   s        r   _modify_in_place_if_numpyrK    s<    2d&&v& J D#F#Jr6   c                     [        XS9u  p4[        US5      (       a  UR                  XUS9$ [        XS9nUb
  [        XS9nOS n[        R                  " XVUS9nUR                  U[        U 5      S9$ )NrO   bincount)r   	minlengthr   )r   r;   rM  r  r	   rU   r"   )r<   r   rN  rP   r   array_np
weights_npbin_outs           r   	_bincountrR    sv     %'EBr:{{5Y{GG .H&w6

nnXYOG::gfUm:44r6   c                     [        XS9u  p[        U5      (       a  U R                  5       $ [        XS9nU Vs/ s H  oDR	                  5       PM     sn$ s  snf )NrO   )r   rQ   tolistr  item)r<   rP   r   rO  r)  s        r   _tolistrV    sK    %'EB2||~ .H*23(wLLN(333s   A)T)N)TF)NNTN)NN)NNN)FF)NNNN)C__doc__r   rH   r.   	functoolsr   r	   r,   scipy.sparsesparser   scipy.specialr   _configr   	externalsr   r   r   externals.array_api_compatr   fixesr   __all__r   r   r$   r(   r5   r=   r{   r"   rL   rQ   r^   rm   rc   r   r   r   r   rB   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r'  r2  r0  rH  rK  rR  rV   r6   r   <module>rb     si   !
   	        ( . ;   '!#MN >0.f"
6" %)v )X 1
S	3D(:F
 -1v !H (,3&T S-n "4.(b$&@!2?DW((  )-H48H<*	 :60 !'t 1D.-bE0 48 5 4r6   