
    Ug1                         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dS )    N)prod   )_bspl)	csr_array)_not_a_knot	NdBSplinec                 p    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    [/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/scipy/interpolate/_ndbspline.py
_get_dtyper      s)    	}UB.// }z    c                   D    e Zd ZdZdddZddddZe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-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          |           n# t          $ r	 |f|z  }Y nw xY wt          |          |k    r0t          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k    rt          d| d          |	|dz   k     rt          dd|z  dz    d| d| d          t          j        |          dk                                     rt          d| d          t          t          j        |||	dz                                dk     rt          d| d          t          j        |                                          st          d| d          | j        j        |k     rt          d| d          | j        j        |         |	k    r<t          d| d| j        j        |          dt          |           d|	 d| d          t'          | j        j                  }
t          j        | j        |
          | _        d S )Nz	len(t) = z != len(k) = .c              3   >   K   | ]}t          j        |          V  d S N)operatorindex).0kis     r   	<genexpr>z%NdBSpline.__init__.<locals>.<genexpr>Y   s,      66bx~b))666666r   c              3   L   K   | ]}t          j        |t                     V   dS r   N)r
   ascontiguousarrayfloatr   tis     r   r   z%NdBSpline.__init__.<locals>.<genexpr>Z   s2      IIr+Be<<<IIIIIIr   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   r1   dtdkdndts              r   __init__zNdBSpline.__init__M   s   1vv	FFFF 	 	 	T	AAA	 q66T>>;A;;s1vv;;;<<<66A66666IIqIIIIIAK,,At 	- 	-ABBb 1$AAvv  "/q "/ "/ "/ 0 0 0w!||  "6Q "6 "6 "6 7 7 726zz  "<!B$( "< "<%'"< "<78"< "< "< = = =a$$&& <  ";q "; "; "; < < <29R1q5\**++a//  "4/0"4 "4 "4 5 5 5;r??&&(( 4  "3q "3 "3 "3 4 4 4v{T!!  "6%&"6 "6 "6 7 7 7v|A!##  ",%&", ",)-a", ", &)WW", ", EF", ", ()	", ", ", - - - $ %%%dfB777s   ! 44)nur   c                ^   t          | j                  }|| j        }t          |          }|"t	          j        |ft          j                  }nt	          j        |t          j                  }|j        dk    s|j	        d         |k    r(t          d|dt          | j                   d          t          |dk               rt          d|          t	          j        |t                    }|j	        }|                    d	|d	                   }t	          j        |          }|d	         |k    rt          d
| d|           t	          j        | j        t	          j        d                    }d | j        D             }t	          j        |t%          |          ft                    }|                    t          j                   t+          |          D ].}	| j        |	         ||	dt          | j        |	                   f<   /t	          j        |t	          j        d                    }t-          d | j        D                       }
t	          j        t	          j        t3          |
                    |
          }t	          j        |t          j                  j        }| j                            | j        j	        d|         dz                                             }t	          j        fdj        D             t          j                  }j	        d	         }t	          j        |j	        dd	         |fz   j                  }t?          j         |||||||||||           |                    |dd	         | j        j	        |d         z             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``
        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                 ,    g | ]}t          |          S  r&   r#   s     r   
<listcomp>z&NdBSpline.__call__.<locals>.<listcomp>   s    ***RR***r   c              3       K   | ]	}|d z   V  
dS )r   NrC   )r   r:   s     r   r   z%NdBSpline.__call__.<locals>.<genexpr>   s&      ..b1f......r   )r@   c                 .    g | ]}|j         j        z  S rC   )r   itemsize)r   sc1s     r   rE   z&NdBSpline.__call__.<locals>.<listcomp>   s3     "7 "7 "7&' #$rx'8"8 "7 "7 "7r   )!r&   r+   r   r.   r
   zerosintcr,   r1   r0   r(   r3   r"   reshaper!   r*   r   emptymaxfillnanr/   r)   unravel_indexaranger   intpTr-   ravelstridesr   evaluate_ndbspline)r7   xir>   r   r1   xi_shape_klen_t_tr8   r0   indices_indices_k1dc1r_strides_c1num_c_troutrJ   s                    @r   __call__zNdBSpline.__call__   s   * 46{{*K;'':4'111BBBbg...Bw!||rx{d22 -2 - -!$&kk- - -. . . 26{{ O !Mb!M!MNNN Z%(((8ZZHRL))!"%%B<4KKKTKKLLL Zbhv&6&6777 +*46***XtSZZ(666
t 	/ 	/A%)VAYBq/3tvay>>/!""
5(8(8999 ..tv....."29T%[[#9#95AAz'999; V^^DFL$/%788hhjj j "7 "7 "7 "7+-:"7 "7 "7>@gG G G 8B<hrx}{2"(CCC !#!&!#!#!,!$!)!,!-!$
	' 
	' 
	' {{8CRC=46<+>>???r   Tc                    t          j        |t                    }|j        d         }t	          |          |k    r#t          dt	          |           d|d          	 t	          |           n# t          $ r	 |f|z  }Y nw xY wt          j        |t           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   r@   z*Data and knots are inconsistent: len(t) = z for  ndim = r   )r
   r,   r"   r0   r&   r(   r'   int32r   
_colloc_ndr   )
clsxvalsr+   r*   r   r1   kkdatar^   indptrs
             r   design_matrixzNdBSpline.design_matrix   s    0 
5...{2q66T>>SVV      	FFFF 	 	 	T	AAA	 Z*** % 02 > >gv$0111s    A0 0BB)T)__name__
__module____qualname____doc__r=   rd   classmethodrm   rC   r   r   r   r      s        1 1d 04 78 78 78 78 78r "&4 V@ V@ V@ V@ V@p &2 &2 &2 [&2 &2 &2r   c           
         t          j        |j        t           j                  r0t	          | |j        |fi |}t	          | |j        |fi |}|d|z  z   S |j        dk    r|j        d         dk    rpt          j	        |          }t          |j        d                   D ]?} || |d d |f         fi |\  |d d |f<   }|dk    rt          d|d|d| d          @|S  || |fi |\  }}|dk    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imagr1   r0   
empty_liker/   r(   )	absolversolver_argsru   rv   resjinfos	            r   rt   rt     sg   
 
}QWb011 1aff<<<<1aff<<<<bg~v{{qwqzA~~mAqwqz"" 	R 	RA$fQ!!!Q$??;??OC1Itqyy !PF!P!P!P!PA!P!P!PQQQ 
F1a//;//	T199>>>D>>>???
r      rz   c                    t                     }t          d  D                       }	 t                     n# t          $ r	 f|z  Y nw xY wt                     D ]]\  }}t          t	          j        |                    }	|	|         k    r+t          d|	 d| d|          d|         dz    d	          ^t           fdt          |          D                       }
t	          j        d	 t          j
          D             t          
          }t                              ||
          }|j        }t          |d|                   t          ||d                   f}|                    |          }|t"          j        k    r$t'          j        t*          |          }d|vrd|d<    |||fi |}|                    |||d         z             }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              3   4   K   | ]}t          |          V  d S r   rD   )r   xs     r   r   zmake_ndbspl.<locals>.<genexpr>@  s(      ,,SVV,,,,,,r   z
There are z points in dimension z, but order z requires at least  r   z points per dimension.c              3      K   | ]9}t          t          j        |         t                     |                   V  :dS r    )r   r
   r,   r"   )r   r8   r*   pointss     r   r   zmake_ndbspl.<locals>.<genexpr>O  sX       $ $ "*VAYe<<<adCC $ $ $ $ $ $r   c                     g | ]}|S rC   rC   )r   xvs     r   rE   zmake_ndbspl.<locals>.<listcomp>Q  s    @@@r@@@r   r   Nr   atolgư>)r&   r)   r'   	enumerater
   
atleast_1dr(   r/   r,   	itertoolsproductr"   r   rm   r0   r   rM   sslspsolve	functoolspartialrt   )r   valuesr*   rz   r{   r1   rZ   r8   pointnumptsr+   ri   matrv_shape
vals_shapevalscoefs   ` `              r   make_ndbsplr      sb   > v;;D,,V,,,,,HA   DI f%% A A5R]5))**QqT>> @& @ @q @ @+,Q4@ @!"1a@ @ @ A A A 
 	 $ $ $ $ $T{{$ $ $ 	$ 	$AJ@@Y%6%?@@@NNNE ""5!Q//D
 lGwuu~&&WTUU^(<(<=J>>*%%D";v>>>$$"&K6$,,,,D<<7455>122DQa   s   < AA)r   )r   r   r   numpyr
   mathr    r   scipy.sparse.linalgsparselinalgr   scipy.sparser   	_bsplinesr   __all__r   r   gcrotmkrt   r   rC   r   r   <module>r      s+                            ! ! ! ! ! ! ! ! ! " " " " " " " " " " " "-  k2 k2 k2 k2 k2 k2 k2 k2\ ![    0E!s{ E! E! E! E! E! E! E!r   