
    e!h4:                         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
c mZ d dlmZ ddlmZ dgZd Z G d	 d      Zd
 Zej*                  fdZdej*                  ddZy)    N)prod   )_bspl)	csr_array)_not_a_knot	NdBSplinec                     t        j                  | t         j                        rt         j                  S t         j                  S )z>Return np.complex128 for complex dtypes, np.float64 otherwise.)np
issubdtypecomplexfloating
complex128float64dtypes    d/var/www/html/diagnosisapp-backend/venv/lib/python3.12/site-packages/scipy/interpolate/_ndbspline.py
_get_dtyper      s*    	}}UB../}}zz    c                   \    e Zd ZdZdddZed        Zed        ZddddZe	d
d	       Z
y)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__
    design_matrix

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

    N)extrapolatec                   t        ||      \  | _        | _        \  | _        | _        |d}t        |      | _        t        j                  |      | _	        | j                  j                  d   }| j                  j                  |k  rt        d| d      t        |      D ]  }| j                  |   }| j                  |   }|j                  d   |z
  dz
  }	| j                  j                  |   |	k7  sSt        d| d| j                  j                  |    dt!        |       d	|	 d
| d       t#        | j                  j$                        }
t        j&                  | j                  |
      | _	        y )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_tboolr   r
   asarraycshapendim
ValueErrorrangetklenr   r   ascontiguousarray)selfr$   r   r%   r   r!   dtdkdndts              r   __init__zNdBSpline.__init__M   s\   =OPQST=U:"$:TWdkK,Aww}}Q66;;=dV=QRRt 
	-ABBb 1$Avv||A!#  $%%&C ())-a(9 :%%(WI-CA3 G''(c	", - -
	- %%%dffB7r   c                 ,    t        | j                        S N)tupler   r(   s    r   r%   zNdBSpline.ki   s    TWW~r   c                 l     t         fdt         j                  j                  d         D              S )Nc              3   ^   K   | ]$  }j                   |d j                  |   f    & y wr0   )r   r   ).0r)   r(   s     r   	<genexpr>zNdBSpline.t.<locals>.<genexpr>p   s+     RQTWWQQ/0Rs   *-r   )r1   r#   r   r    r2   s   `r   r$   zNdBSpline.tm   s(     R%a@P:QRRRr   )nur   c                @   | j                   j                  d   }|| j                  }t        |      }|'t	        j
                  |ft        j                        }nt	        j                  |t        j                        }|j                  dk7  s|j                  d   |k7  r%t        d|dt        | j                         d      t        |dk        rt        d|      t	        j                  |t              }|j                  }|j                  d	|d	         }t	        j                  |      }|d	   |k7  rt        d
| d|       | j                   j"                  j$                  dk(  }| j                   }|r(| j                   j                  |k(  r| j                   d   }|j'                  t              }|j                  |j                  d| dz         }|j)                         }	t	        j                  |j*                  D 
cg c]  }
|
|j"                  j,                  z   c}
t        j.                        }|j                  d	   }t	        j0                  |j                  dd	 |fz   |j"                        }t3        j4                  || j                   | j6                  | j8                  |||	||| j:                  |       |j'                  | j                   j"                        }|j                  |dd	 | j                   j                  |d z         S c c}
w )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 : array_like, optional, shape (ndim,)
            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   z)invalid number of derivative orders nu = z for ndim = r   z'derivatives must be positive, got nu = zShapes: xi.shape=z
 and ndim=r   ).N)r9   )r   r    r   r   r
   zerosintcr   r!   r"   r&   r$   anyfloatreshaper'   r   r   kindviewravelstridesitemsizeintpemptyr   evaluate_ndbspliner   r   r   )r(   xir7   r   r!   xi_shapewas_complexccc1c1rs_strides_c1num_c_trouts                 r   __call__zNdBSpline.__call__r   s   * ww}}Q**K;':4'1BBbgg.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 ffll''3.VV466;;$. 	"BWWU^ ZZ$%/0hhj jj+-::"7&' #$rxx'8'8"8 "7>@ggG 88B<hhrxx}{2"((C  !%!%!%!#!,!$!)!,!%!2!2!$
	' hhtvv||${{8CR=466<<+>>??%"7s   	 Lc                    t        j                  |t              }|j                  d   }t	        |      |k7  rt        dt	        |       d|d      t        |      \  }\  }t        fdt        |      D              }|dd d	z   }	t        j                  |	ddd   t         j                        ddd   j                         }
t        j                  ||||
      \  }}}t        |||f      S )
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   r9   z*Data and knots are inconsistent: len(t) = z for  ndim = r   c              3   :   K   | ]  }|   |   z
  d z
    ywr   N )r5   r)   r%   len_ts     r   r6   z*NdBSpline.design_matrix.<locals>.<genexpr>   s"     Aa1Q4!+As   r   N)r   )r
   r   r=   r    r&   r"   r   r1   r#   cumprodrD   copyr   
_colloc_ndr   )clsxvalsr$   r%   r   r!   r   r   c_shapecscstridesdataindicesindptrrV   s      `          @r   design_matrixzNdBSpline.design_matrix   s   0 

5.{{2q6T><SVH E9A  (:!Q'?$<"e
 AU4[AA QR[4::b2hbgg6tt<AAC !& 0 00205010<08!:gv $011r   )T)__name__
__module____qualname____doc__r.   propertyr%   r$   rQ   classmethodrb   rU   r   r   r   r      s_    1d 04 88   S S "&4 O@b 32 32r   c           
         t        |t              st        d| d      t        |      }	 t        |        t        j                  | D cg c]  }t        j                  |       c}t
        j                        } t        |       |k7  r$t        dt        |       dt        |       d      t        |      }t        |      D ]'  }t        j                  ||         }| |   }|j                  d   |z
  dz
  }|dk  rt        d	| d
      |j                  dk7  rt        d| d      ||dz   k  rt        dd|z  dz    d| d| d      t        j                  |      dk  j                         rt        d| d      t        t        j                  |||dz                dk  rt        d| d      t        j                   |      j#                         rt        d| d       t        d | D              }t        j$                  t        j&                  t)        |            |      }	t        j                  |	t
        j*                        j,                  j/                         }
t        |      }|D cg c]  }t        |       }}t        j0                  |t3        |      ft4              }|j7                  t
        j8                         t        |      D ]  }||   ||dt        ||         f<    t        j                  |t
        j                        }| |
||ffS # t        $ r
 | f|z  } Y w xY wc c}w c c}w )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   &   K   | ]	  }|d z     ywrT   rU   )r5   r+   s     r   r6   z%_preprocess_inputs.<locals>.<genexpr>2  s     %R"q&%s   N)
isinstancer1   r"   r&   	TypeErrorr
   r   operatorindexint32r#   r    r!   diffr<   uniqueisfiniteallunravel_indexaranger   rD   TrX   rE   maxr=   fillnan)r%   t_tplr!   kir)   r*   r+   r,   r    r`   r   tirV   r   s                 r   r   r      s7    eU#   %wi1 
 	

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

32HNN2&3288DA
1v~9SZLSVKqABB u:D4[ 0ZZa!qTHHQK"q 6:1# >* + , ,77a<8 <1 2 3 3rAv:~adQhZ 8!!#N1#Q8 9 9GGBK!O  "21# 66 7 8 8ryyBq1u&'!+  ++,#Q0 1 1{{2""$21# 6. / 0 0)02 %1%%Eryye5u=G::gRWW577<<>L u:D$%SW%E%	4U$E	2BGGBFFO4[ ) %a1ns58}n)JJuBHH-ElRK''k  DI 4T &s   L- 
M M-M ?M c           
      .   t        j                  |j                  t         j                        r8t	        | |j
                  |fi |}t	        | |j                  |fi |}|d|z  z   S |j                  dk(  r{|j                  d   dk7  rit        j                  |      }t        |j                  d         D ]7  } || |d d |f   fi |\  |d d |f<   }|dk7  s$t        d|d|d| d       |S  || |fi |\  }}|dk7  rt        d|d	|d      |S )
Ny              ?rj   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"   )	absolversolver_argsr   r   resjinfos	            r   r   r   F  s-   
 
}}QWWb0011afff<<1afff<<bg~vv{qwwqzA~mmAqwwqz" 	RA$Q!Q$?;?OC1Itqy IF;.>x|A3a!PQQ	R 
1a/;/	T19	{*;D9A>??
r   r   c                Z    t               }t        d  D              }	 t               t               D ]L  \  }}t        t	        j
                  |            }	|	|   k  s-t        d|	 d| d|    d|   dz    d	       t         fdt        |      D              }
t	        j                  t        j                    D cg c]  }| c}t        	      }t        j                  ||
      }|j                  }t        |d
|       t        ||d
       f}|j!                  |      }|t"        j$                  k7  r$t'        j(                  t*        |      }d|vrd|d<    |||fi |}|j!                  |||d
 z         }t        |
|      S # t        $ r
 f|z  Y xw xY wc c}w )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   2   K   | ]  }t        |        y wr0   )r&   )r5   xs     r   r6   zmake_ndbspl.<locals>.<genexpr>~  s     ,SV,s   z
There are z points in dimension z, but order z requires at least  r   z points per dimension.c              3   t   K   | ]/  }t        t        j                  |   t               |          1 yw)r   N)r   r
   r   r=   )r5   r)   r%   pointss     r   r6   zmake_ndbspl.<locals>.<genexpr>  s3      $ "**VAYe<adC $s   58r   Nr   atolgư>)r&   r1   rm   	enumerater
   
atleast_1dr"   r#   r   	itertoolsproductr=   r   rb   r    r   r>   sslspsolve	functoolspartialr   )r   valuesr%   r   r   r!   rH   r)   pointnumptsr$   xvr[   matrv_shape
vals_shapevalscoefs   ` `               r   make_ndbsplr   ^  s   > v;D,V,,HA
 f% A5R]]5)*QqT>z&1Fqc J++,Q4& 1!!"1a(>@ A AA 	 $T{$ 	$AJJY%6%6%?@r@NE ""5!Q/D
 llGwu~&WTU^(<=J>>*%D"";v>$"&K$,,D<<745>12DQa  C  DI As   F 	F(F%$F%)   )r   r   rn   numpyr
   mathr    r   scipy.sparse.linalgsparselinalgr   scipy.sparser   	_bsplinesr   __all__r   r   r   gcrotmkr   r   rU   r   r   <module>r      sc          ! ! " "-]2 ]2@I(X ![[ 0E!s{{ E!r   