o
    Ö­j4:  ã                   @   sª   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
  mZ d dlmZ ddlmZ dgZdd	„ ZG d
d„ dƒZdd„ Zejfdd„Zdejdœdd„ZdS )é    N)Úprodé   )Ú_bspl)Ú	csr_array)Ú_not_a_knotÚ	NdBSplinec                 C   s   t  | t j¡r
t jS t jS )z>Return np.complex128 for complex dtypes, np.float64 otherwise.)ÚnpÚ
issubdtypeÚcomplexfloatingÚ
complex128Úfloat64©Údtype© r   úY/var/www/html/CropPilot/venv/lib/python3.10/site-packages/scipy/interpolate/_ndbspline.pyÚ
_get_dtype   s   r   c                   @   sT   e Zd ZdZddœdd„Zedd„ ƒZedd	„ ƒZddd
œdd„Ze	ddd„ƒZ
dS )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                C   s   t ||ƒ\| _| _\| _| _|d u rd}t|ƒ| _t |¡| _	| jj
d }| j	j|k r3td|› d�ƒ‚t|ƒD ]7}| j| }| j| }|j
d | d }	| j	j
| |	krntd|› d| j	j
| › dt|ƒ› d	|	› d
|› d�ƒ‚q7t| j	jƒ}
tj| j	|
d�| _	d S )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Údtr   r   r   Ú__init__M   s6   


ÿ
þýý
üÿzNdBSpline.__init__c                 C   s
   t | jƒS ©N)Útupler   ©r$   r   r   r   r!   i   s   
zNdBSpline.kc                    s"   t ‡ fdd„tˆ jjd ƒD ƒƒS )Nc                 3   s(   � | ]}ˆ j |d ˆ j| …f V  qd S r+   )r   r   ©Ú.0r%   r-   r   r   Ú	<genexpr>p   s   €& zNdBSpline.t.<locals>.<genexpr>r   )r,   r   r   r   r-   r   r-   r   r    m   s   "zNdBSpline.t)Únur   c                   sâ  | j jd }|du r| j}t|ƒ}|du rtj|ftjd�}n/tj|tjd�}|jdks3|jd |krAt	d|›dt
| jƒ› d�ƒ‚t|dk ƒrNt	d|›�ƒ‚tj|td�}|j}| d	|d	 ¡}t |¡}|d	 |krut	d
|› d|› �ƒ‚| jjjdk}| j}|rŒ| jj|krŒ| jd }| t¡}| |jd|… d ¡‰ ˆ  ¡ }tj‡ fdd„ˆ jD ƒtjd�}	ˆ jd	 }
tj|jdd	… |
f ˆ jd�}t || j | j| j||||
|	| j|¡ | | jj¡}| |dd	… | jj|d…  ¡S )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)r2   c                    s   g | ]}|ˆ j j ‘qS r   )r   Úitemsize)r/   Ús©Úc1r   r   Ú
<listcomp>¯   s    ÿz&NdBSpline.__call__.<locals>.<listcomp>)r   r   r   r   r   ÚzerosÚintcr   r   r   r"   r    ÚanyÚfloatÚreshaper#   r   r   ÚkindÚviewÚravelÚstridesÚintpÚemptyr   Úevaluate_ndbspliner   r   r   )r$   Úxir1   r   r   Úxi_shapeÚwas_complexÚccÚc1rÚ_strides_c1Únum_c_trÚoutr   r5   r   Ú__call__r   sb   ÿÿ


ÿÿ
 ö"zNdBSpline.__call__Tc                    sÎ   t j|td�}|jd }t|ƒ|krtdt|ƒ› d|›d�ƒ‚tˆ |ƒ\‰ }\}‰t‡ ‡fdd„t|ƒD ƒƒ}|dd	… d
 }	t j	|	d	d	d… t j
d�d	d	d…  ¡ }
t ||ˆˆ ||
¡\}}}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   r2   z*Data and knots are inconsistent: len(t) = z for  ndim = r   c                 3   s$   � | ]}ˆ| ˆ |  d  V  qdS ©r   Nr   r.   ©r!   Úlen_tr   r   r0   é   s   €" z*NdBSpline.design_matrix.<locals>.<genexpr>r   N)r   )r   r   r;   r   r"   r   r   r,   r   ÚcumprodrA   Úcopyr   Ú
_colloc_ndr   )ÚclsÚxvalsr    r!   r   r   r   r   Úc_shapeÚcsÚcstridesÚdataÚindicesÚindptrr   rN   r   Údesign_matrixÃ   s(   
ÿÿ(
ûzNdBSpline.design_matrix)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r*   Úpropertyr!   r    rL   Úclassmethodr[   r   r   r   r   r      s    2

Qc              	   C   s^  t |tƒstd|› d�ƒ‚t|ƒ}zt| ƒ W n ty%   | f| } Y nw tjdd„ | D ƒtjd�} t| ƒ|krHtdt|ƒ› dt| ƒ›d�ƒ‚t|ƒ}t|ƒD ]~}t || ¡}| | }|j	d	 | d
 }|d	k rrtd|› d�ƒ‚|j
d
krtd|› d�ƒ‚||d
 k r—tdd| d › d|› d|› d�ƒ‚t |¡d	k  ¡ r¨td|› d�ƒ‚tt |||d
 … ¡ƒdk r¿td|› d�ƒ‚t |¡ ¡ sÎtd|› d�ƒ‚qPtdd„ | D ƒƒ}t t t|ƒ¡|¡}tj|tjd�j ¡ }	t|ƒ}dd„ |D ƒ}
tj|t|
ƒftd�}| tj¡ t|ƒD ]}|| ||dt|| ƒ…f< �qtj|
tjd�}
| |	||
ffS )zÁHelpers: 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.c                 S   s   g | ]}t  |¡‘qS r   )ÚoperatorÚindex)r/   Úkir   r   r   r7     s    z&_preprocess_inputs.<locals>.<listcomp>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                 s   s   � | ]}|d  V  qdS rM   r   )r/   r'   r   r   r   r0   2  ó   € z%_preprocess_inputs.<locals>.<genexpr>c                 S   s   g | ]}t |ƒ‘qS r   ©r"   )r/   Útir   r   r   r7   <  s    N)Ú
isinstancer,   r   r"   Ú	TypeErrorr   r   Úint32r   r   r   Údiffr:   ÚuniqueÚisfiniteÚallÚunravel_indexÚaranger   rA   ÚTrQ   rB   Úmaxr;   ÚfillÚnan)r!   Út_tplr   r%   r&   r'   r(   r   rY   r   rO   r   r   r   r   r   ú   s`   

ÿþ
ÿ
ÿ
ÿÿ r   c           	   	   K   s  t  |jt j¡r$t| |j|fi |¤Ž}t| |j|fi |¤Ž}|d|  S |jdkrj|jd dkrjt  	|¡}t
|jd ƒD ]+}|| |d d …|f fi |¤Ž\|d d …|f< }|dkrgtd|›d|›d|› d�ƒ‚q<|S || |fi |¤Ž\}}|dkr„td|›d	|›d�ƒ‚|S )
Ny              ð?re   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_argsrx   ry   ÚresÚjÚinfor   r   r   rw   F  s    
.ÿrw   é   ©r}   c                   sr  t ˆƒ}tdd„ ˆD ƒƒ}zt ˆ ƒ W n ty!   ˆ f| ‰ Y nw tˆƒD ](\}}t t |¡ƒ}	|	ˆ | krNtd|	› d|› dˆ | › dˆ | d › d�	ƒ‚q&t‡ ‡fd	d„t|ƒD ƒƒ}
tjd
d„ t	j
ˆŽ D ƒtd�}t ||
ˆ ¡}|j}t|d|… ƒt||d… ƒf}| |¡}|tjkrŸtjt|d�}d|vrŸd|d< |||fi |¤Ž}| |||d…  ¡}t|
|ˆ ƒS )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                 s   s   � | ]}t |ƒV  qd S r+   rg   )r/   Úxr   r   r   r0   ~  rf   zmake_ndbspl.<locals>.<genexpr>z
There are z points in dimension z, but order z requires at least  r   z points per dimension.c                 3   s,   � | ]}t tjˆ| td �ˆ | ƒV  qdS )r   N)r   r   r   r;   r.   ©r!   Úpointsr   r   r0   �  s   € $ÿc                 S   s   g | ]}|‘qS r   r   )r/   Úxvr   r   r   r7   �  s    zmake_ndbspl.<locals>.<listcomp>r   Nrƒ   Úatolg�íµ ÷Æ°>)r"   r,   rj   Ú	enumerater   Ú
atleast_1dr   r   r   Ú	itertoolsÚproductr;   r   r[   r   r   r<   ÚsslÚspsolveÚ	functoolsÚpartialrw   )r†   Úvaluesr!   r}   r~   r   rE   r%   ÚpointÚnumptsr    rT   ÚmatrÚv_shapeÚ
vals_shapeÚvalsÚcoefr   r…   r   Úmake_ndbspl^  s>   þÿ

þÿÿ 

r™   )r‚   )r‹   r�   rb   Únumpyr   Úmathr   Ú r   Úscipy.sparse.linalgÚsparseÚlinalgr�   Úscipy.sparser   Ú	_bsplinesr   Ú__all__r   r   r   Úgcrotmkrw   r™   r   r   r   r   Ú<module>   s      aL