
    `iX                         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 Zdd	Zd
 Zd Zd Zd Zd Zd Z	 	 ddZd Zd ZdS )    N2)normalize_axis_index)sparse)spsolve)_get_module_funcINTERVAL_MODULED_BOOR_MODULEBSplinec                 p    t          j        | t           j                  rt           j        S t           j        S )z>Return np.complex128 for complex dtypes, np.float64 otherwise.)cupy
issubdtypecomplexfloatingcomplex_float_dtypes    u/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/cupyx/scipy/interpolate/_bspline2.py
_get_dtyper      s)    ud233 }{    Fc                    t          j        |           } t          j        |           } t          | j                  }|                     |d          } |r5t          j        |                                           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dtyps      r   _as_float_arrayr!      s    
 	QAq!!AagD	E""A ADM!,,0022 A?@@@Hr   c                     d}| D ]}||z  }|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.
        )iterableproductr   s      r   prodr'   (   s(     G  1Nr   c                     t          j        |           } |dz  dk    rt          d|z            |dz
  dz  }| |dz   | dz
           }t           j        | d         f|dz   z  || d         f|dz   z  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ts       r   _not_a_knotr/   8   s     	QA1uzz;a?@@@	
Q1A	!A#qbd(A11q1R5(AaC.01AHr   c                 T    t           j        | d         f|z  | | d         f|z  f         S )zBConstruct a knot vector appropriate for the order-k interpolation.r   r*   )r   r+   )r   r,   s     r   _augkntr1   E   s*    7AaD719a!B%!+,,r   c                    t          j        |           }t          |          }|dz  dk    r1t          j        |          }|ddxx         |dd         dz  z  cc<   t          j        |          }t          j        |d|z  z             }|||| <   t          d|          D ]N}|||z
           |||dz
  z   dz
           z
  |||z
  dz
  <   || |z   dz
           |||dz
  z           z   || |z   <   O|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.   is          r   _periodic_knotsr;   J   s   	1BBA1uzzYr]]
1b5			RWq[ 				2B
1q1u9AAa!eH1a[[ 4 4Qx"qAE{^a%7"88!a%!)qb1fqjMBqAE{O31"q&		Hr   c                     t          | t                    rN| dk    rdt          j        |          fg} n0| dk    rdt          j        |          fg} nt	          d| z            | S )Nclampedr#   naturalr)   zUnknown boundary condition : %s)
isinstancestrr   r5   r   )derivtarget_shapes     r   _convert_string_aliasesrC   \   sv    % HIL1123EEiL1123EE>FGGGLr   c                     | 2	 t          |  \  }}n(# t          $ r}d}t          |          |d }~ww xY wg g }}t          j        ||          S )Nz^Derivatives, `bc_type`, should be specified as a pair of iterables of pairs of (order, value).)zip	TypeErrorr   r   
atleast_1d)rA   ordsvalsemsgs        r   _process_deriv_specrL   g   sm    	)eJD$$ 	) 	) 	);CS//q(	)
 d?4&&&s    
2-2   Tc                    ||dk    s|dk    rd\  }}nFt          |t                    r||}}n,	 |\  }}n%# t          $ r}	t          d|z            |	d}	~	ww xY wt	          j        |          }t          ||j                  }t          | |          } t          ||          }t	          j	        ||d          }|dk    r2t	          j
        |d         |d         d	          st          d
          | j        |j        d         k    r-t          d                    | j        |j                            | dd         | dd         k                                    rt          d          | j        dk    s(| dd         | dd         k                                     rt          d          |dk    rt          d |||fD                       rt          d          t          j        | | d         f         }t	          j        |          }
t	          j        |
t#          |
j                            }
t'          j        ||
||          S |dk    r|||t          d          t          j        | d         | | d         f         }t	          j        |          }
t	          j        |
t#          |
j                            }
t'          j        ||
||          S t+          j        |          }|dk    r|t/          d          |||~|dk    rt1          | |          }nw|dk    rP| dd         | dd         z   dz  }t          j        | d         f|dz   z  |dd         | d         f|dz   z  f         }n!t3          | |          }nt5          | |          }t          ||          }|dk     rt          d          |j        dk    s(|dd         |dd         k                                     rt          d          |j        | j        |z   dz   k     r$t          d|j        | j        |z   dz   fz            | d         ||         k     s| d         ||          k    rt          d| z            |dk    rt7          | ||||          S t9          ||j        dd                   }t;          |          \  }}|j        d         }t9          ||j        dd                   }t;          |          \  }}|j        d         }| j        }|j        |z
  dz
  }||z
  ||z   k    rt          d||z
  d|d|          |j        dk    rDt	          j        |f|j        dd         z   t>                    }
t'          j        ||
||          S t'          j         | ||          }|dk    s|dk    rt	          j        d|z  dz   ft>                    }d}t	          j!        ||ft>                    }t	          j!        d|j                  }tE          tF          d|          }t	          j!        dt          j$                  }tE          tJ          d           } |d!d"|t          j        | d         | d         f         |||d#df           |dk    rt	          j&        | d         g| j                  }t	          j        ||ft>                    }tO          |          D ]L\  }} |d!d!|||tQ          |          |||||ddf           |d         }|d|dz            ||||z
  |dz   f<   MtS          j*        tS          j+        |          |g          }|dk    r|d         |d<   t	          j&        | d         g| j                  }t	          j        ||ft>                    }tO          |          D ]L\  }} |d!d!|||tQ          |          |||||ddf           |d         }|d|dz            ||||z
  |dz   f<   MtS          j*        |tS          j+        |          g          }tY          |j        dd                   } t	          j!        || f|j                  }!|dk    r|-                    d|           |!d|<   |-                    d|           |!|||z
  <   |dk    r|-                    d|           |!||z
  d<   t	          j.        |!j        t          j/                  r/ta          ||!j1                  ta          ||!j2                  d$z  z   }"nta          ||!          }"t	          j        |"-                    |f|j        dd         z                       }"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              3      K   | ]}|d uV  	d S )Nr$   ).0_s     r   	<genexpr>z%make_interp_spline.<locals>.<genexpr>   s&      <<q}<<<<<<r   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.zAThe number of derivatives at boundaries does not match: expected z, got z + )r#   r#   d_boorfind_intervalr#   r)   Fy              ?)3r?   r@   rF   r   r   r   r   ndimr!   moveaxisallclosesizeshapeformatanyr+   r   r   r   r
   construct_fastoperatorindexNotImplementedErrorr;   r/   r1   _make_periodic_splinerC   rL   r5   floatdesign_matrixemptyr   r	   int64r   array	enumerateintr   vstack
csr_matrixr'   reshaper   r   r   realimag)#r   yr,   r.   bc_typerX   r   deriv_lderiv_rrJ   cderiv_l_ordsderiv_l_valsnleftderiv_r_ordsderiv_r_valsnrightr8   ntmatrtempnum_cdummy_coutd_boor_kernelintervals_bcinterval_kernelx0rowsjr-   leftextradimrhscoefs#                                      r   make_interp_spliner   t   s0
   f '\11W
5J5J%	GS	!	! P"G	P&GWW 	P 	P 	P=GHHaO	P 	QAaf--D<((A<((Aaq!!A *T]1Q42U%K%K%K 2 3 3 	3vC &!'224 4 	4	!""3B3 <:;;;v{{quq"v~**,,{LMMM 	Avv<<q'7&;<<<<< 	) ( ) ) )GAquHLOO"1Jqw,?,?@@@%aAD9999 	Avv!)GOBD D DGAaD!QrUN#LOO"1Jqw,?,?@@@%aAD9999qA*! #- . . 	.
 	y?w*$$#Aq))a qrrUQssV^r)GQqTGQqSMadGrUHacN+ ,  1%%1A<((A1uu1222v{{quq"v~**,,{BCCCv
Q:&!&1*q.12 3 3 	3	!qt2A23a7888*$Q1a666 &gqwqrr{;;G!4W!=!=L,q!E%gqwqrr{;;G!4W!=!=L,"F 	
A	
!aB	AvjQ$$$vv/ 0 0 	0
 	v{{Juqwqrr{*%888%aAD9999  Aq))D qyyFQJJ z1q519-u555*b%[666jw}555('JJ z!4:666*?OLLdDGAaD!B%K0,2#	$ 	$ 	$
 qyyZ1ag...z5"+U333l++ 
	0 
	0DAqM$gq#a&&"l     ?D%)$1Q3$ZDDF46M!""}f/55"$ % % zz&r*QZ2qw///z62,e444l++ 
	0 
	0DAqM$gq#a&&"l     ?D%)$1Q3$ZDDF46M!""}d$/557 8 8 AGABBK  H
*b(^17
3
3
3Cqyy"**2x88FUFYYr844Cb6kzz(00X>>BKLL sy$"677 "ch''ch''#-. tS!!!$,,uqwqrr{/B"C"CDDD1dAs   6 
A AAc                 2   ||dk    rd\  }}nFt          |t                    r||}}n,	 |\  }}n%# t          $ r}t          d|z            |d}~ww xY wt	          ||j        dd                   }t          |          \  }}	|j        d         }
t	          ||j        dd                   }t          |          \  }}|j        d         }| j        }|j        |z
  dz
  }t	          ||j        dd                   }t          |          \  }}	|j        d         }
t	          ||j        dd                   }t          |          \  }}|j        d         }| j        }|j        |z
  dz
  }||z
  |
|z   k    sJ t          j	        | t          j
                  }t          t          d          } || j        d         d	z   dz
  d	z  fd
|| |||d| j        d         f           t          j        |dft                    }t          j        t          |           t!          |j        dd                   f|j                  }t!          |j        dd                   }t          j        | j        d         d|z  dz   z            }t          t$          d|          } || j        d         d	z   dz
  d	z  fd
|||d| ||||d| j        d         f           t          j        ||ft                    }|
}t)          t          |                     D ]<}||         }||d|z  dz   z  |d|z  dz   z  |z   dz            |||z   ||z
  |dz   f<   =t          j        dt          j
                  }|
dk    r|d         |d<   t          j        | d         g| j                  }t-          |          D ]L\  }} |dd|||t/          |          |||||ddf           |d         }|d|dz            ||||z
  |dz   f<   M|dk    r|d         |d<   t          j        | d         g| j                  }t-          |          D ]a\  }} |dd|||t/          |          |||||ddf           |d         }|
t          |           z   |z   }|d|dz            ||||z
  |dz   f<   bt!          |j        dd                   }t          j        ||f|j                  } |
dk    r|	                    d|          | d|
<   |                    d|          | |
||z
  <   |dk    r|                    d|          | ||z
  d<   ddlm}!  |!||           }"t          j        |"                    |f|j        dd         z                       }"t9          ||"|          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.
    NrO   rQ   rR   r#   r   r   rZ      )r   Fr)   rY   r[   r*   )solve)r?   r@   rF   r   rC   ra   rL   r`   r   
empty_likerl   r   r   rk   ri   r3   r'   r   r	   r5   r6   rm   rn   ro   rr   cupy.linalgr   r   r
   )#r   ru   r,   r.   rv   rw   rx   rJ   rz   r{   r|   r}   r~   r   r8   r   	intervalsr   r   r   r   r   r   Aoffsetr   r   r   r   r-   rowr   r   r   r   s#                                      r   _make_interp_spline_full_matrixr   }  s]    '\11%	GS	!	! P"G	P&GWW 	P 	P 	P=GHHaO	P &gqwqrr{;;G!4W!=!=L,q!E%gqwqrr{;;G!4W!=!=L,"F 	
A	
!aB%gqwqrr{;;G!4W!=!=L,q!E%gqwqrr{;;G!4W!=!=L,"F 	
A	
!aB6UV^#### 444I&HHOOagaj3&*s24f9aUAGAJ?A A A j"a...G
*	QgmABB'(()@ @ @C qrr"##E:agajAEAI.//D$]HgFFMMAGAJ$q(S02Fgq!Q	3eQ71:      	
B85)))AF3q66]] E E|'+Aqs1uIa1Qik!m,C'D!f*d1fT!Vm
#$$ :atz222Lqyy#A,QZ1ag...l++ 	- 	-DAqM$gq#a&&"lC !Q() ) )  ?D"&t!t*AaaQ zz#B-QZ2qw///l++ 	/ 	/DAqM$gq#a&&"lC !Q() ) )  ?D#a&&.1$C$(!A#JAc46$q&= !! AGABBK  H
*b(^17
3
3
3Cqyy"**2x88FUFYYr844Cb6kzz(00X>>BKLL "!!!!!5C==D!$,,uqwqrr{/B"C"CDDD1dAs   0 
AAAc                 R   | j         }t          j        | ||          }t          j        dd|z  dz   z  t
                    }d}t          j        |j         |z
  dz
  |ft
                    }	t          j        d|	j                  }
t          t          d|	          }t          j
        | d         | d         f         }t          j        |||z   dz
  gt          j                  }t          j        |dz
  ||z   dz
  ft
                    }t          |dz
            D ]k} |dd	||	||dz   |||
||ddf           |d |dz            ||d |dz   f<   ||| d fxx         |d|z  dz   d|z  dz   |z   dz            d d         z  cc<   lt          j        t          j        |          |g          }t#          |j        dd                    }t          j        ||z   dz
  |ft
                    }d|d |dz
  d d f<   |j         dk    r|                    |d          n|                    d|f          ||dz
  d d d f<   t)          ||          }t          j        |                    ||z   dz
  f|j        dd          z                       }t          j        |||d
|          S )Nr)   r#   r   )r)   r#   rY   r   r*   r[   r\   rP   )extrapolaterX   )r`   r
   rj   r   r5   ri   rk   r   r   r	   r+   rm   rl   r6   r   rp   rq   r'   ra   rr   r   r   rd   )r   ru   r.   r,   rX   r8   r   r   r   r   r   r   r   r   r   r-   matr_csrr   r   r   s                       r   rh   rh     s   	A  Aq))D :a1Qiu---DEj!&1*q.%0>>>G
*V7=
1
1
1C$]HgFFM 
1qu	B:q!a%!)nDJ???L :qsAEAI&e444D1Q3ZZ 
; 
;dD'1ac2|	 	 	 TacT
Q1WQVQqS1WacAg]1_45crc::}f/55"$ % %H AGABBK  H
*a!eai*%
8
8
8CC!a%!!!+,6Q;;		!Q		2x.11 Q!!! 8S!!D!$,,A	|agabbk/I"J"JKKD!!T1.8tE E E Er   )F)rM   NNr   T)re   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/   r1   r;   rC   rL   r   r   rh   r$   r   r   <module>r      s    s:::::::::::::        - - - - - -? ? ? ? ? ? ? ? ? ? ? ?     	 	 	 
 
 
- - -
  $  
' 
' 
' >?$(F F F FRB B BJ6E 6E 6E 6E 6Er   