L i^o ddZgdZddlZddlZddlmZddlmZmZm Z m Z m Z m Z m Z mZmZmZddlmZej&j(j*Zd)dZd dgd dgd dgd egd egdegdegdegdZd dgd dgddgddgdegdegdegdegdegdegdegdegd ZegeegeegeegedddZ d*dZegeegeegedZ d+dZ d,dZ!d-dZ"d.d Z#d!Z$egeegeegeeged"Z% d/d#Z&d,d$Z'd%Z(d0d&Z)d1d'Z*d1d(Z+y)2an fitpack (dierckx in netlib) --- A Python-C wrapper to FITPACK (by P. Dierckx). FITPACK is a collection of FORTRAN programs for curve and surface fitting with splines and tensor product splines. See https://web.archive.org/web/20010524124604/http://www.cs.kuleuven.ac.be:80/cwis/research/nalag/research/topics/fitpack.html or http://www.netlib.org/dierckx/ Copyright 2002 Pearu Peterson all rights reserved, Pearu Peterson Permission to use, modify, and distribute this software is given under the terms of the SciPy (BSD style) license. See LICENSE.txt that came with this distribution for specifics. NO WARRANTY IS EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK. TODO: Make interfaces to the following fitpack functions: For univariate splines: cocosp, concon, fourco, insert For bivariate splines: profil, regrid, parsur, surev ) splrepsplprepsplevsplintsprootspaldebisplrepbisplevinsertsplder splantiderN)_fitpack) atleast_1darrayoneszerossqrtravel transposeemptyiinfoasarray) _dfitpackc|ttjkDr| |dt}||tj|S)z^Cast the value to an dfitpack_int and raise an OverflowError if the value cannot fit. z cannot fit into an )r dfitpack_intmaxtype)x exceptionmsgs e/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/scipy/interpolate/_fitpack_impl.py _int_overflowr#)sI 5  " "" ;E-l-=>Cn   Q zHThe spline has a residual sum of squares fp such that abs(fp-s)/s<=0.001z,The spline is an interpolating spline (fp=0)ztThe spline is weighted least-squares polynomial of degree k. fp gives the upper bound fp0 for the smoothing factor szThe required storage space exceeds the available storage space. Probable causes: data (x,y) size is too small or smoothing parameter s is too small (fp>s).zA theoretically impossible result when finding a smoothing spline with fp = s. Probable cause: s too small. (abs(fp-s)/s>0.001)zThe maximal number of iterations (20) allowed for finding smoothing spline with fp=s has been reached. Probable cause: s too small. (abs(fp-s)/s>0.001)zError on input dataAn error occurred)r r unknownz|The spline is weighted least-squares polynomial of degree kx and ky. fp gives the upper bound fp0 for the smoothing factor szWarning. The coefficients of the spline have been computed as the minimal norm least-squares solution of a rank deficient system.zThe required storage space exceeds the available storage space. Probable causes: nxest or nyest too small or s is too small. (fp>s)zA theoretically impossible result when finding a smoothing spline with fp = s. Probable causes: s too small or badly chosen eps. (abs(fp-s)/s>0.001)zNo more knots can be added because the number of B-spline coefficients already exceeds the number of data points m. Probable causes: either s or m too small. (fp>s)zNo more knots can be added because the additional knot would coincide with an old one. Probable cause: s too small or too large a weight to an inaccurate data point. (fp>s)zrwrk2 too small, i.e., there is not enough workspace for computing the minimal least-squares solution of a rank deficient system of linear equations.) r r&r'rr(r)r* r+twrkiwrkuubuec |dkrAtgttgttgttgtddd} t|}|j\}}| r\t |D]N}||d||dk7s| s*t jtd|d|d|dd ||d||d<Pd|cxkrd kstd td |t|t}n t|}|du}|r&| d <| |d| d<n|| d<| |d| d<n|| d<nt|t d <d|cxkrdksntd|dd|cxkrdkstdtdt||k(r|dk(rt||k(s td||td |zz }||dk(r td|t|| d<t| d}|dk(r|d |zd zkr td||kr td| |d |zz} |dk\r|dk(s| dkr| r |d |zz} n||zdz} t| d |zdz} | d }| d}| d}| d}| d}| d}tj t#t%||||||||||| ||| \}}}|d | d <|d| d<|d| d<|| d<|d| d<|d| d<|d}|d}t|}|d }|||z dz f|_|t'||g|f}|dkrG| sEt jtt(|dd|dt|d |d!|d"| zd |dkDrM| sK|d#vr+t jtt(|dd n t)|dt(|d| r |||t(|dfS|S#t*$r!}t)d$dt(d$d|d}~wwxYw#t*$r|||t(d$dfcYSwxYw)%Nr rr0r&z Setting x[z][z]=x[z][0]r( stacklevelr/z0 < idim < 11 must holdr4r5r6r.z1 <= k= z <=5 must holdtask must be -1, 0 or 1zMismatch of input dimensionsKnots must be given for task=-1r1z.There must be at least 2*k+2 knots for task=-1m > k must holdr)r2r3ierfp k= n= m= fp= s=rr(r)r+)rfloatrrshaperangewarningswarnRuntimeWarning TypeErrorrrlenrrr_parcurrrlist_iermessKeyError)rwr4r5r6ktasksr1 full_outputnestperquiet _parcur_cacheidimmiiparnr2r3cor=r>tckues r"rrks qy#B.uR7G!&rr=tck_messs r"rrs^ qy zAq6 "DAq AAy EN 9A qM 9D1I A q6Q;'#a&) >q6"HqsNQ$6#8%@ c'( c1Q3r!t$=%&  q1Q3w!a(Dq1uqy!A#'*D 4'5 ! c qy #(!QU)dA!Gn*D)F#NM% #(!QU)dA!Gn*D)F#NM% %tg| < f7 # E"V$  aAq#t(*B16 1b#!aAq#tQJ 1b# Ra5!BQ% C ax#q!d1#SQA3d2$c!$MM nU+: Qw{ )  MM.#q)9:q I LsmA&x}Q'788 8C#q!11 1 5 7/056 77$ Ly)!,Xi-@-CD!K L  8C)!4Q!77 7 8sBL3M!M=3 M < MM  M:M55M:=NNcF|\}}} |ddd}|rtt||||ffd |Sd|cxkr|ksntd|d|ddvrtd d t |}|j }t |j}|dk(rtj||||\} } ntj|||||\} } | d k(r td | d k(r td| r td| j|S#t$rd}Y wxYw)Nr TFc&t||||g|SN)r)r_rr1rRderexts r"zsplev..:sa!QC5r$z0<=der=z<=k= must hold)r rr(r)zext = z not in (0, 1, 2, 3) r*Invalid input datarzFound x value not in the domainr%) ExceptionrNri ValueErrorrrFrrrjrr rKreshape) rrrrwrxr1r_rR parametricrFrmr=s ` r"rr1sKGAq! !Q CAaS678:; ;S A wse4s*=> > l "vcU*?@A A AJ qM   ! !8^^Aq!Q4FAs__Q1ac:FAs "912 2 !8>? ? /0 0yy5  s D D D c|\}}} |ddd}|rtt||||fd|Stj|||||\}} |r|| fS|S#t$rd}YNwxYw)Nr TFc"t|||||gSrv)r)r_abr1rRs r"ryzsplint..]sq!aAY/r$)r|rNrirjr) rrrrrUr1r_rRraintr2s r"rrTsGAq! !Q CAa101245 5OOAq!Q2 c 9 K  s A A! A!c|\}}}|dk7r td |ddd}|rtt|||fd|St |dkrt dt |d t j|||\}}}|d k(r t d |dk(r|d|S|d k(r%tjtd d|d|St d#t$rd}YwxYw)Nr)z)sproot works only for cubic (k=3) splinesr TFc t|||g|Srv)r)r_r1rRmests r"ryzsproot..rs1ay$/r$rgzThe number of knots z>=8r*z? ?OOAq$/ 1c "9GH H !8Ra5L !8 MM.)KL%& (Ra5L((%  s C CCcf|\}}} |ddd}|rtt|||fd|St|}t |dkDrtt|fd|St j |||dz|d\}}|dk(r|S|dk(r tdtd #t$rd}YwxYw) Nr TFc t||||gSrvr)r_rr1rRs r"ryzspalde..sq1a),r$rct||Srvr)rrrs r"ryzspalde..s va~r$r*z0Invalid input data. t(k)<=x<=t(n-k+1) must hold.r)r|rNrirrLrjrrK)rrrr1r_rRrdr=s r"rrsGAq! !Q CAa-./12 2 qM q6A:#=qAB BAqsAaD13 !8H "9NO O((  s B"" B0/B0)txtyr2r3c tt|||g\}}}t|}|t|cxk(rt|k(stdtd|t |t }n t |}t||k(stdt|d|||j}||j}||j}||j}d| cxkrdkstdtd| |td|zz } | | dk(r td | t | td <ttd }|| dk(r td |t |td <ttd }| dk(r|d|zdzkr td | dk(r|d| zdzkr tdd|cxkrdkrnn d| cxkrdksntd|d| d||dz| dzzkr td|t|t|dz z}|t| t|dz z}t|d|zdzt|d| zdz}}| dk\r9| dk(r4t|td|zz}t| td|zz}| dk(r$t | td <t |td <td td }} td}||z dz }|| z dz }t|| dz}t||}||z| zdz| |z|zdz}}|||z| z }}||kDr |||z|z }}d}t||zd|z|zzd||z|||zzz|z|z | z zz|zdzt|} t||z|dzz|zt|}!tj|||||||||| | | | | ||||| |!\} }}"}#| t d <|t d <|#dt d<|#d|#d}%}$| ||"|| g}&tdtd|$}'|'dkrX|sVt"|'dd|d| dt| dt|d|d |%d!| z}(t%j&t)|(d"|'dkDry|sw|$d#vrWd$|d| dt| dt|d|d |%d!| }(t%j&t)t"|'d|(zd"n t#|'dt"|'d|r |&|%|$t"|'dfS|&S#t*$r!})t#d%dt"d%d|)d})~)wwxYw#t*$r|&|%|$t"d%dfcYSwxYw)&aT Find a bivariate B-spline representation of a surface. Given a set of data points (x[i], y[i], z[i]) representing a surface z=f(x,y), compute a B-spline representation of the surface. Based on the routine SURFIT from FITPACK. Parameters ---------- x, y, z : ndarray Rank-1 arrays of data points. w : ndarray, optional Rank-1 array of weights. By default ``w=np.ones(len(x))``. xb, xe : float, optional End points of approximation interval in `x`. By default ``xb = x.min(), xe=x.max()``. yb, ye : float, optional End points of approximation interval in `y`. By default ``yb=y.min(), ye = y.max()``. kx, ky : int, optional The degrees of the spline (1 <= kx, ky <= 5). Third order (kx=ky=3) is recommended. task : int, optional If task=0, find knots in x and y and coefficients for a given smoothing factor, s. If task=1, find knots and coefficients for another value of the smoothing factor, s. bisplrep must have been previously called with task=0 or task=1. If task=-1, find coefficients for a given set of knots tx, ty. s : float, optional A non-negative smoothing factor. If weights correspond to the inverse of the standard-deviation of the errors in z, then a good s-value should be found in the range ``(m-sqrt(2*m),m+sqrt(2*m))`` where m=len(x). eps : float, optional A threshold for determining the effective rank of an over-determined linear system of equations (0 < eps < 1). `eps` is not likely to need changing. tx, ty : ndarray, optional Rank-1 arrays of the knots of the spline for task=-1 full_output : int, optional Non-zero to return optional outputs. nxest, nyest : int, optional Over-estimates of the total number of knots. If None then ``nxest = max(kx+sqrt(m/2),2*kx+3)``, ``nyest = max(ky+sqrt(m/2),2*ky+3)``. quiet : int, optional Non-zero to suppress printing of messages. Returns ------- tck : array_like A list [tx, ty, c, kx, ky] containing the knots (tx, ty) and coefficients (c) of the bivariate B-spline representation of the surface along with the degree of the spline. fp : ndarray The weighted sum of squared residuals of the spline approximation. ier : int An integer flag about splrep success. Success is indicated if ier<=0. If ier in [1,2,3] an error occurred but was not raised. Otherwise an error is raised. msg : str A message corresponding to the integer flag, ier. See Also -------- splprep, splrep, splint, sproot, splev UnivariateSpline, BivariateSpline Notes ----- See `bisplev` to evaluate the value of the B-spline given its tck representation. If the input data is such that input dimensions have incommensurate units and differ by many orders of magnitude, the interpolant may have numerical artifacts. Consider rescaling the data before interpolation. References ---------- .. [1] Dierckx P.:An algorithm for surface fitting with spline functions Ima J. Numer. Anal. 1 (1981) 267-283. .. [2] Dierckx P.:An algorithm for surface fitting with spline functions report tw50, Dept. Computer Science,K.U.Leuven, 1980. .. [3] Dierckx P.:Curve and surface fitting with splines, Monographs on Numerical Analysis, Oxford University Press, 1993. Examples -------- Examples are given :ref:`in the tutorial `. z!len(x)==len(y)==len(z) must hold.Nrdrer&rr:r(z!Knots_x must be given for task=-1rz!Knots_y must be given for task=-1rz1There must be at least 2*kx+2 knots_x for task=-1z1There must be at least 2*ky+2 knots_x for task=-1r.z"Given degree of the spline (kx,ky=,rfzm >= (kx+1)(ky+1) must holdr)r r2z#Too many data points to interpolater!r=r>r/r,z kx,ky=z nx,ny=rArBrCr8)rr(r)r-r.z kx,ky=r+)rirrLrKrrErminrr _surfit_cacheintr# OverflowErrorr_surfitrp _iermess2rHrIrJrP)*rrmrrQrnroybyekxkyrSrTepsrrrUnxestnyestrXr[nxnyr2r4vkmnebxbyb1b2r!lwrk1lwrk2r_r`r=r>rriermrsrbs* r"rrs!~%!Q#GAq! AA Q !3q6 !;<< ";<<y EN qM q6Q;'#a&)`. r z 0 <= dx = z < kx = rzz 0 <= dy = z < ky = rz*First two entries should be rank-1 arrays.z$Too many data points to interpolate.rr*r{r%) r}rirrLrFr#size MemoryErrorrjparderbispevrK) rrmrrdxdyrrr_rrr!rr=s r"r r jshBAr2 LbL:bT"Z@AA LbL:bT"Z@AA zAq6 "DAq AGG s177|q0EFF 0C!&&166/;C8 Qw"'rww|a'"''B,*:;!s ,RBBAqA3RBAq93 by-.. +,,!fc!fnAG 1vz 1Q4y1}t Q47Nr$c L|\}}}}} tj||||| |||| S)aEvaluate the integral of a spline over area [xa,xb] x [ya,yb]. Parameters ---------- xa, xb : float The end-points of the x integration interval. ya, yb : float The end-points of the y integration interval. tck : list [tx, ty, c, kx, ky] A sequence of length 5 returned by bisplrep containing the knot locations tx, ty, the coefficients c, and the degrees kx, ky of the spline. Returns ------- integ : float The value of the resulting integral. )rjdblint) xarnyarrrrrr_rrs r"rrs3&BAr2 ??2r1b"b"b" ==r$c0|\}}} |ddd}|r3g}|D]'} t||| |g|\} } } |j| ) | fStj||||||\} }} | dk(r t d| r t d| ||fS#t$rd}YwxYw)Nr TFr*r{r%)r|r appendr_insertr}rK)rrrr[rWr1r_rRrccc_valsttcc_valkkr=s r"r r sGAq! !Q   F#A61~q9NB IIf  B|&&sAq!Q: B "912 2 /0 0B{  s B BBc `|dkr t|| S|\}}}||kDrtd|d|ddtdfdt|jddzz}t j dd 5 t|D]s}||dzd |d| dz z }||}|dd |z |dd |z z |z|z }t j|t j|f|jddzf}|dd }|dz}u ddd|||fS#t$r}td |d |d}~wwxYw#1swY/xYw)Nr zOrder of derivative (n = z") must be <= order of spline (k = r()rvrraise)invaliddivider&r'zAThe spline has internal repeated knots and is not differentiable z times) r r}slicerLrFnperrstaterGr_rFloatingPointError) rrr^r1r_rRshjdtrbs r"r r s1u#r""GAq!1u4QE:114Q !=> > +73qwwqr{#33 4B WW 5M M1X  qs2Y1aRT*VqAY5BqD)Q.3EE!RXXqdQWWQR[&899:aGQ M* a7N " M;;<#VEFKL M M#MMs+2D$4BD D! DD!!D$$D-c|dkr t|| S|\}}}tdfdt|jddzz}t |D]}||dzd|d| dz z }||}t j |d| dz |zd|dzz }t jt jd|jddz||dg|dzzf}t j|d||df}|dz }|||fS)Nr rvr)axisrr&r() r rrLrFrGrcumsumrr)rrr^r1r_rRrrrs r"r r s$1ucA2GAq! +'#aggabk"22 2B 1X qstWq1"Q$x  V IIa!Ahm! ,A 6 EE"((4!''!"+-.R5'QqS/" # EE!A$1R5. ! Q  a7Nr$rv) NNNNr)r NNr Nr r) NNNr)r NNr r r)r r )r )r*)NNNNNr)r)r NgؗҜrs@. D*** $~~$$**   "$ 7> CDH J #$.0 H )+  +#Y/! ( "$ 7> EFJ L K   N )+ )+ ;B&uR/?r<0uR7G# IM34hVB&uR/?r<02 CG'(Tn  F&):)4R'uR/?b%(%L2IK =A@D:;EPQh>..#Lr$