
    tKg1                         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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    `/home/alanp/www/video.onchill/myenv/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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-produce 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        |      }	 t        |       t        |      |k7  r$t        dt        |      dt        |      d      t        d |D              | _        t        d |D              | _        t        j                  |      | _        |d}t        |      | _
        t        j                  |      | _        t        |      D ]  }| j
                  |   }| 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%                         st        d| d      | j                  j                  |k  rt        d| d      | j                  j                  |   |	k7  sot        d| d| j                  j                  |    dt        |       d|	 d| d       t'        | j                  j(                        }
t        j*                  | j                  |
      | _        y # t        $ r
 |f|z  }Y w xY w)Nz	len(t) = z != len(k) = .c              3   F   K   | ]  }t        j                  |        y wN)operatorindex).0kis     r   	<genexpr>z%NdBSpline.__init__.<locals>.<genexpr>Y   s     6Abx~~b)As   !c              3   R   K   | ]  }t        j                  |t                ! ywr   N)r
   ascontiguousarrayfloat)r   tis     r   r   z%NdBSpline.__init__.<locals>.<genexpr>Z   s!     Iqr++Be<<qs   %'Tr   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.zCoefficients must be at least z-dimensional.z,Knots, coefficients and degree in dimension z are inconsistent: got z coefficients for z knots, need at least z for k=r   )len	TypeError
ValueErrortuplektr
   asarraycboolr   rangeshapendimdiffanyuniqueisfiniteallr   r   r!   )selfr*   r,   r)   r   r0   dtdkdndts              r   __init__zNdBSpline.__init__M   s   1v	F
 q6T>	A{.s1vk;<<6A66IqIIAK,AtABBb 1$AAv #>qc B. "/ 0 0ww!| #<QC @5 "6 7 726z >!B$( <%%'Dqc"< = =a$$& #6qc :: "; < <299R1q5\*+a/  $//0c"4 5 5;;r?&&( #6qc :2 "3 4 4vv{{T!  $%%&C}"6 7 7vv||A!#  $%%&C ())-a(9 :%%(WI-CA3 G''(c	", - -5 @ %%%dffB7e  	T	A	s   J< <KK)nur   c                   t        | j                        }|| 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|       t	        j                  | j                  t	        j                   d            }| j                  D cg c]  }t        |       }}t	        j"                  |t%        |      ft              }	|	j'                  t        j(                         t+        |      D ].  }
| j                  |
   |	|
dt        | j                  |
         f<   0 t	        j                  |t	        j                   d            }t-        d | j                  D              }t	        j.                  t	        j0                  t3        |            |      }t	        j                  |t        j4                        j6                  }| j8                  j                  | j8                  j                  d| dz         }|j;                         }t	        j                  |j<                  D cg c]  }||j                   j>                  z   c}t        j4                        }|j                  d	   }t	        j"                  |j                  dd	 |fz   |j                         }tA        jB                  ||	|||||||||       |j                  |dd	 | j8                  j                  |d z         S c c}w 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``
        Nr   r   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=longc              3   &   K   | ]	  }|d z     yw)r   N )r   r9   s     r   r   z%NdBSpline.__call__.<locals>.<genexpr>   s     .vb1fvs   )r?   )"r%   r*   r   r-   r
   zerosintcr+   r0   r/   r'   r2   r"   reshaper!   r)   r   emptymaxfillnanr.   r(   unravel_indexaranger   intpTr,   ravelstridesitemsizer   evaluate_ndbspline)r6   xir=   r   r0   xi_shape_kr#   len_t_tr7   r/   indices_indices_k1dc1c1rs_strides_c1num_c_trouts                       r   __call__zNdBSpline.__call__   s:   * 466{**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 ZZbhhv&67 $(66*6RR6*XXtSZ(6
tA%)VVAYBq/3tvvay>/!" 

5(89 .tvv..""299T%[#95Azz'9;; VV^^DFFLL$/%78hhj jj+-::"7+5a #$rxx'8'8"8+5"7>@ggG 88B<hhrxx}{2"((C  !#!&!#!#!,!$!)!,!-!$
	' {{8CR=466<<+>>??G +""7s   O.) O3c                    t        j                  |t              }|j                  d   }t	        |      |k7  rt        dt	        |       d|d      	 t	        |       t        j                  |t         j                        }t        j                  |||      \  }}}	t        |||	f      S # t        $ r	 |f|z  }Y _w xY w)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   r?   z*Data and knots are inconsistent: len(t) = z for  ndim = r   )r
   r+   r"   r/   r%   r'   r&   int32r   
_colloc_ndr   )
clsxvalsr*   r)   r   r0   kkdatarW   indptrs
             r   design_matrixzNdBSpline.design_matrix   s    0 

5.{{2q6T><SVH E9A 	F
 ZZ* % 0 02 >gv$011  	T	A	s   B. .C ?C )T)__name__
__module____qualname____doc__r<   r_   classmethodrh   rB   r   r   r   r      s6    1d 04 78r "&4 V@p &2 &2r   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              ?r$   r   r   z	solver = z returns info =z for column r   z returns info = )r
   r   r   r   _iter_solverealimagr0   r/   
empty_liker.   r'   )	absolversolver_argsrp   rq   resjinfos	            r   ro   ro     s&   
 
}}QWWb0011afff<<1afff<<bg~vv{qwwqzA~mmAqwwqz"A$Q!Q$?;?OC1Itqy IF;.>x|A3a!PQQ # 
1a/;/	T19	{*;D9A>??
r   ru   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r   )r%   )r   xs     r   r   zmake_ndbspl.<locals>.<genexpr>@  s     ,VSVVs   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    )r   r
   r+   r"   )r   r7   r)   pointss     r   r   zmake_ndbspl.<locals>.<genexpr>O  s3      $"! "**VAYe<adC"s   58r   Nrz   atolgư>)r%   r(   r&   	enumerater
   
atleast_1dr'   r.   r+   	itertoolsproductr"   r   rh   r/   r   rE   sslspsolve	functoolspartialro   )r   valuesr)   ru   rv   r0   rS   r7   pointnumptsr*   xvrd   matrv_shape
vals_shapevalscoefs   ` `               r   make_ndbsplr      s   > v;D,V,,HA
 f%5R]]5)*QqT>z&1Fqc J++,Q4& 1!!"1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   r   numpyr
   mathr    r   scipy.sparse.linalgsparselinalgr   scipy.sparser   	_bsplinesr   __all__r   r   gcrotmkro   r   rB   r   r   <module>r      s\          ! ! " "-k2 k2\ ![[ 0E!s{{ E!r   