o
    iX                     @   s   d dl Z d dlZejdk rd dlmZ nd dlmZ d dlZd dlmZ d dl	m
Z
 d dlmZmZmZmZ dd Zd d
dZdd Zdd Zdd Zdd Zdd Zdd Z	 	d!ddZdd Zdd ZdS )"    N2)normalize_axis_index)sparse)spsolve)_get_module_funcINTERVAL_MODULED_BOOR_MODULEBSplinec                 C   s   t | t jr
t jS t jS )z>Return np.complex128 for complex dtypes, np.float64 otherwise.)cupy
issubdtypecomplexfloatingcomplex_float_dtype r   ^/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/cupyx/scipy/interpolate/_bspline2.py
_get_dtype   s   r   Fc                 C   sJ   t | } t | } t| j}| j|dd} |r#t |  s#td| S )zConvert the input into a C contiguous float array.

    NB: Upcasts half- and single-precision floats to double precision.
    F)copyz$Array must not contain infs or nans.)	r
   asarrayascontiguousarrayr   r   astypeisfiniteall
ValueError)xcheck_finitedtypr   r   r   _as_float_array   s   


r   c                 C   s   d}| D ]}||9 }q|S )z
    Product of a sequence of numbers.
    Faster than np.prod for short lists like array shapes, and does
    not overflow if using Python integers.
       r   )iterableproductr   r   r   r   prod(   s   
r"   c                 C   st   t | } |d dkrtd| |d d }| |d | d  }t j| d f|d  || d f|d  f }|S )zSGiven data x, construct the knot vector w/ not-a-knot BC.
    cf de Boor, XIII(12).   r   z Odd degree for now only. Got %s.r   )r
   r   r   r_)r   kmtr   r   r   _not_a_knot8   s   
,r)   c                 C   s$   t j| d f| | | d f| f S )zBConstruct a knot vector appropriate for the order-k interpolation.r   r$   )r
   r%   )r   r&   r   r   r   _augkntE   s   $r*   c                 C   s   t | }t|}|d dkr$t |}|dd  |dd d 8  < t |}t |d|  }|||| < td|D ]/}|||  |||d   d   ||| d < || | d  |||d    || | < q>|S )z$Returns vector of nodes on a circle.r#   r   r   r$   N)r
   r   lendiffzerosrange)r   r&   xcndxr(   ir   r   r   _periodic_knotsJ   s   

 
..r3   c                 C   sR   t | tr'| dkrdt|fg} | S | dkr!dt|fg} | S td|  | S )Nclampedr   naturalr#   zUnknown boundary condition : %s)
isinstancestrr
   r-   r   )derivtarget_shaper   r   r   _convert_string_aliases\   s   
r:   c              
   C   sV   | d ur zt |  \}}W n ty } zd}t||d }~ww g g }}t||S )Nz^Derivatives, `bc_type`, should be specified as a pair of iterables of pairs of (order, value).)zip	TypeErrorr   r
   
atleast_1d)r8   ordsvalsemsgr   r   r   _process_deriv_specg   s   

rB      Tc           #      C   s~  |du s|dks|dkrd\}}n%t |tr||}}nz|\}}W n ty5 }	 ztd| |	d}	~	ww t|}t||j}t| |} t||}t	||d}|dkrftj
|d |d dd	sftd
| j|jd krxtd| j|j| dd | dd k rtd| jdks| dd | dd k  rtd|dkrtdd |||fD rtdtj| | d f }t|}
tj|
t|
jd}
tj||
||dS |dkr|du r|du r|du stdtj| d | | d f }t|}
tj|
t|
jd}
tj||
||dS t|}|dkr#|dur#td|du rv|du rq|du rq|dkr=t| |}n9|dkrk| dd | dd  d }tj| d f|d  |dd | d f|d  f }nt| |}nt| |}t||}|dk rtd|jdks|dd |dd k  rtd|j| j| d k rtd|j| j| d f | d || k s| d ||  krtd|  |dkrt| ||||S t||jdd }t|\}}|jd }t||jdd }t|\}}|jd }| j}|j| d }|| || kr$td|| ||f |jdkrBtj|f|jdd  td}
tj||
||dS t | ||}|dksS|dkrtjd| d ftd}d}tj!||ftd}tj!d|jd}t"t#d|}tj!dtj$d}t"t%d}|d d!|tj| d | d f |||d"df |dkrtj&| d g| jd}tj||ftd}t'|D ].\}}|d d |||t(||||||ddf |d }|d|d  |||| |d f< qt)*t)+||g}|dkrM|d |d< tj&| d g| jd}tj||ftd}t'|D ].\}}|d d |||t(||||||ddf |d }|d|d  |||| |d f< qt)*|t)+|g}t,|jdd } tj!|| f|jd}!|dkro|-d| |!d|< |-d| |!||| < |dkr|-d| |!|| d< t.|!jtj/rt0||!j1t0||!j2d#  }"nt0||!}"t|"-|f|jdd  }"t||"|S )$ai  Compute the (coefficients of) interpolating B-spline.

    Parameters
    ----------
    x : array_like, shape (n,)
        Abscissas.
    y : array_like, shape (n, ...)
        Ordinates.
    k : int, optional
        B-spline degree. Default is cubic, ``k = 3``.
    t : array_like, shape (nt + k + 1,), optional.
        Knots.
        The number of knots needs to agree with the number of data points and
        the number of derivatives at the edges. Specifically, ``nt - n`` must
        equal ``len(deriv_l) + len(deriv_r)``.
    bc_type : 2-tuple or None
        Boundary conditions.
        Default is None, which means choosing the boundary conditions
        automatically. Otherwise, it must be a length-two tuple where the first
        element (``deriv_l``) sets the boundary conditions at ``x[0]`` and
        the second element (``deriv_r``) sets the boundary conditions at
        ``x[-1]``. Each of these must be an iterable of pairs
        ``(order, value)`` which gives the values of derivatives of specified
        orders at the given edge of the interpolation interval.
        Alternatively, the following string aliases are recognized:

        * ``"clamped"``: The first derivatives at the ends are zero. This is
           equivalent to ``bc_type=([(1, 0.0)], [(1, 0.0)])``.
        * ``"natural"``: The second derivatives at ends are zero. This is
          equivalent to ``bc_type=([(2, 0.0)], [(2, 0.0)])``.
        * ``"not-a-knot"`` (default): The first and second segments are the
          same polynomial. This is equivalent to having ``bc_type=None``.
        * ``"periodic"``: The values and the first ``k-1`` derivatives at the
          ends are equivalent.

    axis : int, optional
        Interpolation axis. Default is 0.
    check_finite : bool, optional
        Whether to check that the input arrays contain only finite numbers.
        Disabling may give a performance gain, but may result in problems
        (crashes, non-termination) if the inputs do contain infinities or NaNs.
        Default is True.

    Returns
    -------
    b : a BSpline object of the degree ``k`` and with knots ``t``.

    N
not-a-knotperiodicNNUnknown boundary condition: %sr   r$   gV瞯<)atolzAFirst and last points does not match while periodic case expectedz(Shapes of x {} and y {} are incompatibler   zExpect x to not have duplicatesz1Expect x to be a 1D strictly increasing sequence.c                 s   s    | ]}|d uV  qd S )Nr   ).0_r   r   r   	<genexpr>   s    z%make_interp_spline.<locals>.<genexpr>z6Too much info for k=0: t and bc_type can only be None.r   )axisz0Too much info for k=1: bc_type can only be None.zOFor periodic case t is constructed automatically and can not be passed manuallyr#   g       @zExpect non-negative k.z'Expect t to be a 1-D sorted array_like.zGot %d knots, need at least %d.zOut of bounds w/ x = %s.zPThe number of derivatives at boundaries does not match: expected %s, got %s + %s)r   r   d_boorfind_intervalr   r#   Fy              ?)3r6   r7   r<   r   r
   r   r   ndimr   moveaxisallclosesizeshapeformatanyr%   r   r   r   r	   construct_fastoperatorindexNotImplementedErrorr3   r)   r*   _make_periodic_spliner:   rB   r-   floatdesign_matrixemptyr   r   int64r   array	enumerateintr   vstack
csr_matrixr"   reshaper   r   r   realimag)#r   yr&   r(   bc_typerL   r   deriv_lderiv_rr@   cderiv_l_ordsderiv_l_valsnleftderiv_r_ordsderiv_r_valsnrightr0   ntmatrtempnum_cdummy_coutd_boor_kernelintervals_bcinterval_kernelx0rowsjr'   leftextradimrhscoefr   r   r   make_interp_splinet   s*  3




 &









*&


	

(
(



 r   c           #      C   s  |du s|dkrd\}}n%t |tr||}}nz|\}}W n ty1 } ztd| |d}~ww t||jdd }t|\}}	|jd }
t||jdd }t|\}}|jd }| j}|j| d }t||jdd }t|\}}	|jd }
t||jdd }t|\}}|jd }| j}|j| d }|| |
| ksJ tj	| tj
d}ttd}|| jd d	 d d	 fd
|| |||d| jd f tj|dftd}tjt| t|jdd f|jd}t|jdd }t| jd d| d  }ttd|}|| jd d	 d d	 fd
|||d| ||||d| jd f tj||ftd}|
}tt| D ])}|| }||d| d  |d| d  | d  ||| || |d f< q3tjdtj
d}|
dkr|d |d< tj| d g| jd}t|D ].\}}|dd|||t||||||ddf |d }|d|d  |||| |d f< q|dkr|d |d< tj| d g| jd}t|D ]6\}}|dd|||t||||||ddf |d }|
t|  | }|d|d  |||| |d f< qt|jdd }tj||f|jd} |
dkr!|	d|| d|
< |d|| |
|| < |dkr>|d|| || d< ddlm}! |!|| }"t|"|f|jdd  }"t||"|S )z Construct the interpolating spline spl(x) = y with *full* linalg.

        Only useful for testing, do not call directly!
        This version is O(N**2) in memory and O(N**3) in flop count.
    NrD   rF   rG   r   r   r   rN      )r   Fr#   rM   rO   r$   )solve)r6   r7   r<   r   r:   rU   rB   rT   r
   
empty_liker`   r   r   r_   r]   r+   r"   r   r   r-   r.   ra   rb   rc   rf   cupy.linalgr   r   r	   )#r   ri   r&   r(   rj   rk   rl   r@   rn   ro   rp   rq   rr   rs   r0   rt   	intervalsr|   rx   ry   rw   rv   rz   Aoffsetr   r   r{   r}   r'   rowr   r   r   r   r   r   r   _make_interp_spline_full_matrix}  s   






	H
(
(


 r   c                 C   s*  | j }t| ||}tjdd| d  td}d}tj|j | d |ftd}	tjd|	jd}
tt	d|	}tj
| d | d f }tj||| d gtjd}tj|d || d ftd}t|d D ]F}|dd	||	||d |||
||ddf |d |d  ||d |d f< ||| d f  |d| d d| d | d  d d 8  < qctt||g}t|jdd  }tj|| d |ftd}d|d |d d d f< |j dkr||dn|d|f||d d d d f< t||}t||| d f|jdd   }tj|||d
|dS )Nr#   r   r   )r#   r   rM   r   r$   rO   rP   rE   )extrapolaterL   )rT   r	   r^   r
   r-   r]   r_   r   r   r   r%   ra   r`   r.   r   rd   re   r"   rU   rf   r   r   rX   )r   ri   r(   r&   rL   r0   ru   rv   rw   rx   ry   rz   r}   r{   r~   r'   matr_csrr   r   r   r   r   r   r\     sH   
 D
(
r\   )F)rC   NNr   T)rY   numpy__version__numpy.core.multiarrayr   numpy.lib.array_utilsr
   cupyx.scipyr   cupyx.scipy.sparse.linalgr    cupyx.scipy.interpolate._bspliner   r   r   r	   r   r   r"   r)   r*   r3   r:   rB   r   r   r\   r   r   r   r   <module>   s2    


   