
    doiE                     
   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  SSK	J
r
  S SKJs  Jr  S SKJr  S SKJrJr  SSKJrJr  S	/rS
 r\" SSS/S9 " S S	5      5       rS r\R6                  4S jrS\R6                  S.S jjrg)    N)prod)GenericAlias   )_dierckx)	csr_array)array_namespacexp_capabilities)_not_a_knotBSpline	NdBSplinec                     [         R                  " U [         R                  5      (       a  [         R                  $ [         R                  $ )z>Return np.complex128 for complex dtypes, np.float64 otherwise.)np
issubdtypecomplexfloating
complex128float64dtypes    X/var/www/html/land-ocr/venv/lib/python3.13/site-packages/scipy/interpolate/_ndbspline.py
_get_dtyper      s-    	}}UB..//}}zz    TF)z
dask.arrayz7https://github.com/data-apis/array-api-extra/issues/488)cpu_onlyjax_jitskip_backendsc                       \ rS rSrSr\" \5      rSS.S jr\	S 5       r
\	S 5       r\	S 5       rSSS	.S
 jr\SS j5       rSS jrS rSrg)r      a  Tensor product spline object.

The value at point ``xp = (x1, x2, ..., xN)`` is evaluated as a linear
combination of products of one-dimensional b-splines in each of the ``N``
dimensions::

   c[i1, i2, ..., iN] * B(x1; i1, t1) * B(x2; i2, t2) * ... * B(xN; iN, tN)


Here ``B(x; i, t)`` is the ``i``-th b-spline defined by the knot vector
``t`` evaluated at ``x``.

Parameters
----------
t : tuple of 1D ndarrays
    knot vectors in directions 1, 2, ... N,
    ``len(t[i]) == n[i] + k + 1``
c : ndarray, shape (n1, n2, ..., nN, ...)
    b-spline coefficients
k : int or length-d tuple of integers
    spline degrees.
    A single integer is interpreted as having this degree for
    all dimensions.
extrapolate : bool, optional
    Whether to extrapolate out-of-bounds inputs, or return `nan`.
    Default is to extrapolate.

Attributes
----------
t : tuple of ndarrays
    Knots vectors.
c : ndarray
    Coefficients of the tensor-product spline.
k : tuple of integers
    Degrees for each dimension.
extrapolate : bool, optional
    Whether to extrapolate or return nans for out-of-bounds inputs.
    Defaults to true.

Methods
-------
__call__
derivative
design_matrix

See Also
--------
BSpline : a one-dimensional B-spline object
NdPPoly : an N-dimensional piecewise tensor product polynomial

Nextrapolatec                *   [        X15      u  U l        U l        u  U l        U l        [        U/UQ76 R                  U l        Uc  Sn[        U5      U l	        [        R                  " U5      U l        U R                  R                  S   nU R                  R                  U:  a  [        SU S35      e[        U5       H  nU R                   U   nU R"                  U   nUR                  S   U-
  S-
  n	U R                  R                  U   U	:w  d  MU  [        SU SU R                  R                  U    S[%        U5       S	U	 S
U S35      e   ['        U R                  R(                  5      n
[        R*                  " U R                  U
S9U l        g )NTr   zCoefficients must be at least z-dimensional.r   z,Knots, coefficients and degree in dimension z are inconsistent: got z coefficients for z knots, need at least z for k=.r   )_preprocess_inputs_k_indices_k1d_t_len_tr   asarray_asarrayboolr   r   _cshapendim
ValueErrorrangetklenr   r   ascontiguousarray)selfr.   cr/   r   r+   dtdkdndts              r   __init__NdBSpline.__init__[   sm   =OPQ=U:"$:TWdk'.A.66K,**Q-ww}}Q77<<$=dV=QRRtABBb 1$Aww}}Q1$  $%%&C ())-q)9(: ;%%(WI-CA3 G''(c	", - -  &&&twwb9r   c                 ,    [        U R                  5      $ N)tupler"   r2   s    r   r/   NdBSpline.ky   s    TWW~r   c                 n   ^  [        U 4S j[        T R                  R                  S   5       5       5      $ )Nc              3      >#    U  H4  nTR                  TR                  US TR                  U   24   5      v   M6     g 7fr<   )r'   r$   r%   ).0r4   r2   s     r   	<genexpr>NdBSpline.t.<locals>.<genexpr>   s;      
@W1DMM$''!_dkk!n_"4566@Ws   <?r   )r=   r-   r$   r*   r>   s   `r   r.   NdBSpline.t}   s2      
@EdggmmTUFV@W
 
 	
r   c                 8    U R                  U R                  5      $ r<   )r'   r)   r>   s    r   r3   NdBSpline.c   s    }}TWW%%r   )nur   c                   U R                   R                  S   nUc  U R                  n[        U5      nUc%  [        R
                  " U4[        R                  S9nO[        R                  " U[        R                  S9nUR                  S:w  d  UR                  S   U:w  a&  [        SU< S[        U R                  5       S35      e[        US:  5      (       a  [        SU< 35      e[        R                  " U[        S9nUR                  nUR                  S	US	   5      n[        R                  " U5      nUS	   U:w  a  [        S
U SU 35      eU R                   R"                  R$                  S:H  nU R                   nU(       a)  U R                   R                  U:X  a  U R                   S   nUR'                  [        5      nUR                  UR                  SU S-   5      nUR)                  5       n	[        R                  " UR*                   V
s/ s H  n
XR"                  R,                  -  PM     sn
[        R                  S9nUR                  S	   n[.        R0                  " UU R                   U R2                  U R4                  UUU	UUU R6                  5
      nUR'                  U R                   R"                  5      nUR                  USS	 U R                   R                  US -   5      nU R9                  U5      $ s  sn
f )a  Evaluate the tensor product b-spline at ``xi``.

Parameters
----------
xi : array_like, shape(..., ndim)
    The coordinates to evaluate the interpolator at.
    This can be a list or tuple of ndim-dimensional points
    or an array with the shape (num_points, ndim).
nu : sequence of length ``ndim``, optional
    Orders of derivatives to evaluate. Each must be non-negative.
    Defaults to the zeroth derivivative.
extrapolate : bool, optional
    Whether to exrapolate based on first and last intervals in each
    dimension, or return `nan`. Default is to ``self.extrapolate``.

Returns
-------
values : ndarray, shape ``xi.shape[:-1] + self.c.shape[ndim:]``
    Interpolated values at ``xi``
r   Nr   r   )invalid number of derivative orders nu =  for ndim = r    z'derivatives must be positive, got nu = zShapes: xi.shape=z
 and ndim=r3   ).N)rL   )r$   r*   r   r(   r   zerosint64r&   r+   r,   r0   r.   anyfloatreshaper1   r)   r   kindviewravelstridesitemsizer   evaluate_ndbspliner%   r"   r#   r'   )r2   xirH   r   r+   xi_shapewas_complexccc1c1rs_strides_c1num_c_trouts                 r   __call__NdBSpline.__call__   s   * ww}}Q**K;':4'2BBbhh/Bww!|rxx{d2 @2' B!$&&k]!-. . 26{{ #KbW!MNN ZZ%(88ZZHRL)!!"%B<40
*TFKLL ggmm((C/WW477<<4/ #BWWU^ ZZ$%/0hhj jj+-::"7+5a #$xx'8'8"8+5"7>@hhH 88B<))"!%!%!%!#!,!$!)!,!%!2!2

 hhtww}}%kk(3B-$''--*>>?}}S!!#"7s   "Lc                   ^^ [         R                  " U[        S9nUR                  S   n[	        U5      U:w  a  [        S[	        U5       SU< S35      e[        TU5      u  mnu  nm[        UU4S j[        U5       5       5      nUSS S	-   n	[         R                  " U	SSS2   [         R                  S9SSS2   R                  5       n
[        R                  " UUTTXj5      u  pn[        XU45      $ )
a|  Construct the design matrix as a CSR format sparse array.

Parameters
----------
xvals :  ndarray, shape(npts, ndim)
    Data points. ``xvals[j, :]`` gives the ``j``-th data point as an
    ``ndim``-dimensional array.
t : tuple of 1D ndarrays, length-ndim
    Knot vectors in directions 1, 2, ... ndim,
k : int
    B-spline degree.
extrapolate : bool, optional
    Whether to extrapolate out-of-bounds values of raise a `ValueError`

Returns
-------
design_matrix : a CSR array
    Each row of the design matrix corresponds to a value in `xvals` and
    contains values of b-spline basis elements which are non-zero
    at this value.

r   rL   z*Data and knots are inconsistent: len(t) = z for  ndim = r    c              3   @   >#    U  H  nTU   TU   -
  S -
  v   M     g7fr   N )rB   r4   r/   len_ts     r   rC   *NdBSpline.design_matrix.<locals>.<genexpr>   s"     A[a1Q4!+[s   r   Nr   )r   r&   rP   r*   r0   r,   r!   r=   r-   cumprodrN   copyr   	_coloc_ndr   )clsxvalsr.   r/   r   r+   r#   r$   c_shapecscstridesdataindicesindptrrh   s      `          @r   design_matrixNdBSpline.design_matrix   s    0 

5.{{2q6T><SVH E9A  (:!Q'?$<"e
 AU4[AA QR[4::b2hbhh7"=BBD !) 2 25E1l!6v $011r   c           	         [         R                  " XS5      nUR                  S   nUR                  SS  nUR                  US5      n/ n	S n
[	        UR                  S   5       H  nX5:  ag  [
        R                  " X(S S 2U4   U5      nUR                  U5      nUR                  S [        UR                  5      UR                  -
  S-
   Ul        O8[
        R                  " U[         R                  " [        U5      S-
  5      S5      nU
c  UR                  n
U	R                  UR                  5        M     [         R                  " U	SS9R                  [        U	S   5      4U-   5      n[         R                  " USU5      nX4$ )Nr   r   rL   )axis)r   moveaxisr*   rQ   r-   r   construct_fast
derivativer3   r0   r.   r/   rM   appendstack)r2   r3   r.   r/   ry   rH   r7   trailing_shapec_flat
new_c_listnew_tibdbnew_cs                  r   _bspline_derivative_along_axis(NdBSpline._bspline_derivative_along_axis  sE   KK#GGAJ1b!
v||A'Aw**1QTlA>\\"%tt1SY-12++ArxxA
/CQG}bdd# ( !,44A!N24E1d+|r   c           	      h  ^  [         R                  " U[         R                  S9n[        T R                  5      nUR
                  S:w  d  UR                  S   U:w  a&  [        SU< S[        T R                  5       S35      e[        US:  5      (       a  [        SU< 35      e[        T R                  R                  S   5       Vs/ s H#  nT R                  UST R                  U   24   PM%     nn[        T R                  5      nT R                  R                  5       n[!        U5       H;  u  pU	S:X  a  M  T R#                  XuU   Xh   XS	9u  ouU'   [%        Xh   U	-
  S5      Xh'   M=     ['        [)        U 4S
 jU 5       5      T R+                  U5      [)        U5      T R,                  S9$ s  snf )a+  
Construct a new NdBSpline representing the partial derivative.

Parameters
----------
nu : array_like of shape (ndim,)
    Orders of the partial derivatives to compute along each dimension.

Returns
-------
NdBSpline
    A new NdBSpline representing the partial derivative of the original spline.

r   r   r   rJ   rK   r    z-derivative orders must be positive, got nu = N)rH   c              3   F   >#    U  H  nTR                  U5      v   M     g 7fr<   )r'   )rB   r.   r2   s     r   rC   'NdBSpline.derivative.<locals>.<genexpr>Q  s     ?At}}Q//s   !r   )r   r&   rN   r0   r.   r+   r*   r,   rO   r-   r$   r%   listr/   r)   rl   	enumerater   maxr   r=   r'   r   )
r2   rH   nu_arrr+   r4   t_newk_newc_newry   r7   s
   `         r   r|   NdBSpline.derivative)  s    Bbhh/466{;;!v||A$6<rg >dff+a)* * vz??MwOPP 7<DGGMM!<L6MN6MOT[[^O+,6MNTVV (GDAv!%!D!DT{EK "E "E; ekAoq1EK ) ???u-u%)%5%5
 	
 Os   	*F/)r'   r)   r#   r"   r%   r$   r   )Trj   )__name__
__module____qualname____firstlineno____doc__classmethodr   __class_getitem__r9   propertyr/   r.   r3   rb   rv   r   r|   __static_attributes__rg   r   r   r   r      s    2j $L1/3 :<   
 
 & & "&4 N"` 02 02d<,
r   c           
         [        U[        5      (       d  [        SU S35      e[        U5      n [        U 5        [
        R                  " U  Vs/ s H  n[        R                  " U5      PM     sn[
        R                  S9n [        U 5      U:w  a%  [        S[        U5       S[        U 5      < S35      e[        U5      n[        U5       GH6  n[
        R                  " X   5      nX   nUR                  S   U-
  S-
  nUS:  a  [        S	U S
35      eUR                  S:w  a  [        SU S35      eXvS-   :  a  [        SSU-  S-    SU SU S35      e[
        R                  " U5      S:  R                  5       (       a  [        SU S35      e[        [
        R                  " XVUS-    5      5      S:  a  [        SU S35      e[
        R                   " U5      R#                  5       (       a  GM*  [        SU S35      e   [        S U  5       5      n[
        R$                  " [
        R&                  " [)        U5      5      U5      n	[
        R                  " U	[
        R                  S9R*                  R-                  5       n
U Vs/ s H  n[
        R                  " U5      PM     nn[        U5      nU Vs/ s H  n[        U5      PM     nn[
        R.                  " U[1        U5      4[2        S9nUR5                  [
        R6                  5        [        U5       H  nX   XS[        X   5      24'   M     [
        R                  " U[
        R                  S9nX
X44$ ! [         a
    U 4U-  n  GN?f = fs  snf s  snf s  snf )zHelpers: validate and preprocess NdBSpline inputs.

Parameters
----------
k : int or tuple
   Spline orders
t_tpl : tuple or array-likes
   Knots.
z-Expect `t` to be a tuple of array-likes. Got z	 instead.r   z	len(t) = z != len(k) = r    r   r   zSpline degree in dimension z cannot be negative.zKnot vector in dimension z must be one-dimensional.zNeed at least    z knots for degree z in dimension zKnots in dimension z# must be in a non-decreasing order.z.Need at least two internal knots in dimension z should not have nans or infs.c              3   *   #    U  H	  oS -   v   M     g7frf   rg   )rB   r6   s     r   rC   %_preprocess_inputs.<locals>.<genexpr>  s     %1Rq&1s   N)
isinstancer=   r,   r0   	TypeErrorr   r&   operatorindexrN   r-   r*   r+   diffrO   uniqueisfiniteallunravel_indexaranger   Trl   emptyr   rP   fillnan)r/   t_tplr+   kir4   r5   r6   r7   r*   rt   r#   r.   tirh   r$   s                  r   r!   r!   W  sH    eU##   %wi1 
 	

 u:DA
 	

32HNN2&3288DA
1v~9SZLSVKqABB u:D4[ZZ!THHQK"q 6:1# >* + , ,77a<8 <1 2 3 3Av:~adQhZ 8!!#N1#Q8 9 9GGBK!O  ""21# 66 7 8 8ryyq1u&'!+  ++,#Q0 1 1{{2""$$21# 6. / 0 0) 2 %1%%Eryye5u=G::gRXX688==?L %**EqRZZ]EE*u:D$%uSWuE%	4U$E	2BGGBFFO4[ %ns58}n JJuBHH-ERK''m  DI 4R +%s#   M(  M? N?N	(M<;M<c           
      F   [         R                  " UR                  [         R                  5      (       a6  [	        XR
                  U40 UD6n[	        XR                  U40 UD6nUSU-  -   $ UR                  S:X  a  UR                  S   S:w  an  [         R                  " U5      n[        UR                  S   5       H:  nU" XS S 2U4   40 UD6u  US S 2U4'   nUS:w  d  M%  [        SU< SU< SU S35      e   U$ U" X40 UD6u  phUS:w  a  [        SU< S	U< S35      eU$ )
Ny              ?r   r   r   z	solver = z returns info =z for column r    z returns info = )r   r   r   r   _iter_solverealimagr+   r*   
empty_liker-   r,   )	ar   solversolver_argsr   r   resjinfos	            r   r   r     s   
 
}}QWWb00111fff<<1fff<<bg~vv{qwwqzA~mmAqwwqz"A$Q!Q$?;?OC1Itqy IF;.>x|A3a!PQQ # 
1/;/	19	{*;D9A>??
r   r   c                  ^ ^ [        T 5      n[        S T  5       5      n [        T5        [        T 5       HN  u  px[        [        R
                  " U5      5      n	U	TU   ::  d  M/  [        SU	 SU STU    STU   S-    S3	5      e   [        UU 4S j[        U5       5       5      n
[        R                  " [        R                  " T 6  Vs/ s H  oPM     sn[        S	9n[        R                  XT5      nTS
   S:  a  UR                  5         UR                  n[!        USU 5      [!        XS 5      4nUR#                  U5      nU[$        R&                  :w  a$  [(        R*                  " [,        US9nSU;  a  SUS'   U" UU40 UD6nUR#                  XnUS -   5      n[        U
UT5      $ ! [         a
    T4U-  m GNf = fs  snf )a+  Construct an interpolating NdBspline.

Parameters
----------
points : tuple of ndarrays of float, with shapes (m1,), ... (mN,)
    The points defining the regular grid in N dimensions. The points in
    each dimension (i.e. every element of the `points` tuple) must be
    strictly ascending or descending.
values : ndarray of float, shape (m1, ..., mN, ...)
    The data on the regular grid in n dimensions.
k : int, optional
    The spline degree. Must be odd. Default is cubic, k=3
solver : a `scipy.sparse.linalg` solver (iterative or direct), optional.
    An iterative solver from `scipy.sparse.linalg` or a direct one,
    `sparse.sparse.linalg.spsolve`.
    Used to solve the sparse linear system
    ``design_matrix @ coefficients = rhs`` for the coefficients.
    Default is `scipy.sparse.linalg.gcrotmk`
solver_args : dict, optional
    Additional arguments for the solver. The call signature is
    ``solver(csr_array, rhs_vector, **solver_args)``

Returns
-------
spl : NdBSpline object

Notes
-----
Boundary conditions are not-a-knot in all dimensions.
c              3   8   #    U  H  n[        U5      v   M     g 7fr<   )r0   )rB   xs     r   rC   make_ndbspl.<locals>.<genexpr>  s     ,VSVVVs   z
There are z points in dimension z, but order z requires at least  r   z points per dimension.c              3   v   >#    U  H.  n[        [        R                  " TU   [        S 9TU   5      v   M0     g7f)r   N)r
   r   r&   rP   )rB   r4   r/   pointss     r   rC   r     s5      $"! "**VAYe<adCC"s   69r   r      Nr   atolgư>)r0   r=   r   r   r   
atleast_1dr,   r-   r&   	itertoolsproductrP   r   rv   eliminate_zerosr*   r   rQ   sslspsolve	functoolspartialr   )r   valuesr/   r   r   r+   rY   r4   pointnumptsr.   xvro   matrv_shape
vals_shapevalscoefs   ` `               r   make_ndbsplr     s   > v;D,V,,HA
 f%R]]5)*QqT>z&1Fqc J++,Q4& 1!!"1a(>@ A A & 	 $T{$ 	$AJJY%6%6%?@%?r%?@NE ""5Q/D 	tqy
 llGwu~&WU^(<=J>>*%D"";v>$"&K$,,D<<45>12DQa  M  DI As   F. G.GG)r   )r   r   r   numpyr   mathr   typesr    r   scipy.sparse.linalgsparselinalgr   scipy.sparser   scipy._lib._array_apir   r	   	_bsplinesr
   r   __all__r   r   r!   gcrotmkr   r   rg   r   r   <module>r      s           ! ! " B +- 5	Dr
 r
r
h	J(Z ![[ 0J!s{{ J!r   