L i9ddlZddlZddlZddlZddlmZddlmZddl m cm Z ddl mZddlmZdgZdZGd dZd Ze j*fd Zde j*d d Zy)N)prod)_dierckx) csr_array) _not_a_knot NdBSplinectj|tjrtjStjS)z>Return np.complex128 for complex dtypes, np.float64 otherwise.)np issubdtypecomplexfloating complex128float64dtypes b/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/scipy/interpolate/_ndbspline.py _get_dtypers* }}UB../}}zzc\eZdZdZdddZedZedZddddZe d d Z y) raTensor 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) extrapolatect||\|_|_\|_|_|d}t ||_tj||_ |jjd}|jj|krtd|dt|D]}|j|}|j|}|jd|z dz } |jj|| k7sStd|d|jj|dt!|d | d |d t#|jj$} tj&|j| |_ y) NTrzCoefficients must be at least z -dimensional.rz,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_tboolrr asarraycshapendim ValueErrorrangetklenrrascontiguousarray) selfr$rr%rr!dtdkdndts r__init__zNdBSpline.__init__Ms\=OPQST=U:"$:TWdk  K ,Aww}}Q 66;; =dV=QR Rt -ABB b 1$Avv||A!# $%%&C())-a(9:%%(WI-CA3G''(c ",-- - %%%dffB7rc,t|jSN)tuplerr(s rr%z NdBSpline.kisTWW~rcltfdtjjdDS)Nc3^K|]$}j|dj|f&ywr0)rr).0r)r(s r zNdBSpline.t..ps+RQTWWQQ/0Rs*-r)r1r#rr r2s`rr$z NdBSpline.tms(R% a@P:QRRRr)nurc |jjd}| |j}t|}|'t j |ftj }nt j|tj }|jdk7s|jd|k7r%td|dt|jdt|dkrtd|t j|t}|j}|jd |d }t j|}|d |k7rtd |d ||j j"j$d k(}|j }|r(|j j|k(r|j d }|j't}|j|jd|dz}|j)} t j|j*D cgc]} | |j"j,zc} tj } |jd } t/j0||j|j2|j4||| | | |j6 } | j'|j j"} | j|dd |j j|dzScc} 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`` rNrrz)invalid number of derivative orders nu = z for ndim = rz'derivatives must be positive, got nu = zShapes: xi.shape=z and ndim=r).N)r9)rr rrr zerosint64rr!r"r&r$anyfloatreshaper'rrkindviewravelstridesitemsizerevaluate_ndbsplinerrr)r(xir7rr!xi_shape was_complexccc1c1rs _strides_c1num_c_trouts r__call__zNdBSpline.__call__rs}*ww}}Q  **K;' :4'2BBbhh/Bww!|rxx{d2 @2'B!$&&k]!-..26{ #KbW!MNNZZ% (88 ZZHRL )  ! !" % B<4 0 *TFKL Lffll''3. VV 466;;$. "B WWU^ZZ$%/ 0hhjjj+-::"7&'#$rxx'8'8"8"7>@hhH 88B<))"!%!%!%!#!,!$!)!,!%!2!2  hhtvv||${{8CR=466<<+>>??!"7s K(ctj|t}|jd}t ||k7rt dt |d|dt |\}\}tfdt|D}|ddd z} tj| dddtjdddj} tj|||| \} } } t| | | fS) 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. rr9z*Data and knots are inconsistent: len(t) = z for ndim = rc3:K|]}||z dz ywrN)r5r)r%len_ts rr6z*NdBSpline.design_matrix..s"Aa1Q4!+AsrN)r)r rr=r r&r"rr1r#cumprodr;copyr _coloc_ndr)clsxvalsr$r%rr!rrc_shapecscstridesdataindicesindptrrTs ` @r design_matrixzNdBSpline.design_matrixs0 5.{{2 q6T>.-s%R"q&%sN) isinstancer1r"r& TypeErrorr roperatorindexr;r#r r!diffr<uniqueisfiniteall unravel_indexarangerTrVemptymaxr=fillnan)r%t_tplr!kir)r*r+r,r r^rtirTrs rrrs7 eU # %wi1  u:D A 32HNN2&3288DA 1v~9SZLSVKqABB u:D 4[0 ZZa ! qT HHQK" q  6:1#>*+, , 77a<8<123 3 rAv:~adQhZ8!!#N1#Q89 9 GGBK!O "21#6678 8 ryyBq1u& '! + ++,#Q01 1{{2""$21#6./0 0)02 %1% %Eryye5u=G::gRXX688==?L u:D$ %SW %E % 4U$E 2BGGBFFO 4[) %a1ns58}n ) JJuBHH -E lRK ''k  DI4T &s L- MM-M?Mc .tj|jtjr8t ||j |fi|}t ||j |fi|}|d|zzS|jdk(r{|jddk7ritj|}t|jdD]7}|||dd|ffi|\|dd|f<}|dk7s$td|d|d|d|S|||fi|\}}|dk7rtd|d |d|S) Ny?rhrrz solver = z returns info =z for column rz returns info = ) r r rr _iter_solverealimagr!r empty_liker#r") absolver solver_argsr~rresjinfos rr}r}As-  }}QWWb0011afff< <1afff< <bg~vv{qwwqzA~mmAqwwqz" RA$Q!Q$?;?OC1Itqy IF;.>x|A3a!PQQ R 1a/;/ T 19 {*;D9A>? ? rrc Zt}tdD} ttD]L\}}tt j |} | |ks-t d| d|d|d|dzd tfdt|D} t jtjD cgc]} | c} t } tj| | } |j}t|d |t||d f}|j!|}|t"j$k7r$t'j(t*| }d |vrd |d <|| |fi|}|j!|||d z}t| |S#t$r f|zYxwxYwcc} w)aConstruct 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. c32K|]}t|ywr0)r&)r5xs rr6zmake_ndbspl..ys,SV,sz There are z points in dimension z , but order z requires at least rz points per dimension.c3tK|]/}ttj|t|1yw)rN)rr rr=)r5r)r%pointss rr6zmake_ndbspl..s3$"**VAYe)r&r1rk enumerater atleast_1dr"r#r itertoolsproductr=rr`r rr>sslspsolve functoolspartialr})rvaluesr%rrr!rFr)pointnumptsr$xvrYmatrv_shape vals_shapevalscoefs` ` r make_ndbsplrYs> v;D,V,,H A f%A5R]]5)* QqT>z&1FqcJ++,Q4&1!!"1a(>@A AA $T{$ $A JJY%6%6%?@r@ NE  " "5!Q /D llGwu~&WTU^(<=J >>* %D "";v>  $"&K  $ , ,D <<745>1 2D Qa  C  DIAs F F(F%$F%))rrrlnumpyr mathrrscipy.sparse.linalgsparselinalgr scipy.sparser _bsplinesr__all__rrrgcrotmkr}rrSrrrsc!!"" -X2X2vI(X![[0E!s{{E!r