K iL'ddlmZmZddlmZmZddlmZmZddl m Z ddl m Z ddl mZdZdZed d Zd Zd Zy))Ssympify)Dummysymbols) Piecewisepiecewise_fold)And)Interval) lru_cachect|trRt|jdk(r:|j\}}|j|k(r||}}|j|j fSt d|z)zreturn the interval corresponding to the condition Conditions in spline's Piecewise give the range over which an expression is valid like (lo <= x) & (x <= hi). This function returns (lo, hi). zunexpected cond type: %s) isinstancer lenargsltsgts TypeError)condxabs f/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/functions/special/bsplines.py_ivlr sa$TYY1!4yy1 55A:aqAuuaee| .5 66c tj||fvrt||z}|jStj||fvrt||z}|jSg}t||z}t||z}t|jdd} |jddD]} | j } | j } t| |d} t| D]\\}}|j }|j }t||\}}|| k(r | |z } | |=n#|| ksB|| ksH|j|| |=n|j| | f|j| |jdt|ddi}|jS)zConstruct c*b1 + d*b2.NrrTevaluateF) rZerorlistrexprrr enumerateappendextendrexpand)cb1db2rrvnew_argsp1p2p2argsargr!rloweriarg2expr2cond2lower_2upper_2s r _add_splinesr7s vv"a AF #h 99;g B7  AF #d 99;a AF # AF #bggcrl#773B< *C88D88DqM!$E%V, 4  #'q> D=EMDq u_E)9OOD)q % * OOT4L )9 *>   "  15 1 99;r)maxsizec |}t}td|D}t|}t|}t|}|dz }||zdz|kDr t d|dk(r?t t jt||||dzj|fd}n|dkDr|||zdz||dzz }|t jk7r&|||zdz|z |z } t|dz ||dz|} nt jx} } |||z||z }|t jk7r|||z |z } t|dz |||} nt jx} } t| | | | |}nt d|z|j||iS)a0 The $n$-th B-spline at $x$ of degree $d$ with knots. Explanation =========== B-Splines are piecewise polynomials of degree $d$. They are defined on a set of knots, which is a sequence of integers or floats. Examples ======== The 0th degree splines have a value of 1 on a single interval: >>> from sympy import bspline_basis >>> from sympy.abc import x >>> d = 0 >>> knots = tuple(range(5)) >>> bspline_basis(d, knots, 0, x) Piecewise((1, (x >= 0) & (x <= 1)), (0, True)) For a given ``(d, knots)`` there are ``len(knots)-d-1`` B-splines defined, that are indexed by ``n`` (starting at 0). Here is an example of a cubic B-spline: >>> bspline_basis(3, tuple(range(5)), 0, x) Piecewise((x**3/6, (x >= 0) & (x <= 1)), (-x**3/2 + 2*x**2 - 2*x + 2/3, (x >= 1) & (x <= 2)), (x**3/2 - 4*x**2 + 10*x - 22/3, (x >= 2) & (x <= 3)), (-x**3/6 + 2*x**2 - 8*x + 32/3, (x >= 3) & (x <= 4)), (0, True)) By repeating knot points, you can introduce discontinuities in the B-splines and their derivatives: >>> d = 1 >>> knots = (0, 0, 2, 3, 4) >>> bspline_basis(d, knots, 0, x) Piecewise((1 - x/2, (x >= 0) & (x <= 2)), (0, True)) It is quite time consuming to construct and evaluate B-splines. If you need to evaluate a B-spline many times, it is best to lambdify them first: >>> from sympy import lambdify >>> d = 3 >>> knots = tuple(range(10)) >>> b0 = bspline_basis(d, knots, 0, x) >>> f = lambdify(x, b0) >>> y = f(0.5) Parameters ========== d : integer degree of bspline knots : list of integer values list of knots points of bspline n : integer $n$-th B-spline x : symbol See Also ======== bspline_basis_set References ========== .. [1] https://en.wikipedia.org/wiki/B-spline c32K|]}t|ywN)r).0ks r z bspline_basis..s,'!*,sz(n + d + 1 must not exceed len(knots) - 1rrzdegree must be non-negative: %r)rtupleintr ValueErrorrrOner containsr bspline_basisr7xreplace) r(knotsnrxvarn_knots n_intervalsresultdenomBr)Ar's rrFrFTsf D A ,e, ,E AA AA%jGA+K1uqy;CDDAv UUHU1XuQU|4==a@ A9  Qa!eai 5Q</ AFF?q1uqy!A%.Aq1ueQUA6BVVOBa!e uQx' AFF?U1X&Aq1ueQ2BVVOBaQA.:Q>?? ??At9 %%rc t||z dz }t|Dcgc]}t|t|||c}Scc}w)a| Return the ``len(knots)-d-1`` B-splines at *x* of degree *d* with *knots*. Explanation =========== This function returns a list of piecewise polynomials that are the ``len(knots)-d-1`` B-splines of degree *d* for the given knots. This function calls ``bspline_basis(d, knots, n, x)`` for different values of *n*. Examples ======== >>> from sympy import bspline_basis_set >>> from sympy.abc import x >>> d = 2 >>> knots = range(5) >>> splines = bspline_basis_set(d, knots, x) >>> splines [Piecewise((x**2/2, (x >= 0) & (x <= 1)), (-x**2 + 3*x - 3/2, (x >= 1) & (x <= 2)), (x**2/2 - 3*x + 9/2, (x >= 2) & (x <= 3)), (0, True)), Piecewise((x**2/2 - x + 1/2, (x >= 1) & (x <= 2)), (-x**2 + 5*x - 11/2, (x >= 2) & (x <= 3)), (x**2/2 - 4*x + 8, (x >= 3) & (x <= 4)), (0, True))] Parameters ========== d : integer degree of bspline knots : list of integers list of knots points of bspline x : symbol See Also ======== bspline_basis r@)rrangerFrA)r(rHr n_splinesr1s rbspline_basis_setrTs?`E Q"I:? :J KQM!U5\1a 0 KK KsAc ddlm}ddlm}t |}|j r |j std|zt|t|k7r tdt||dzkr tdtdt||dd Ds td |Dcgc] }t |}}|jr|dzd z}||| }n7|d z}t||| dz ||dz| D cgc] \} } | | zd z }} } |dg|dzzt|z|d g|dzzz} t|| } |D cgc]"} | D cgc]} | j| c} $}} } |||||ftd j!t|t"}t|d}| D chc]} | j$D] \}}|dk7s | }}} }t'|fd}| D cgc]"} | j$Dcic]\}}|| c}}$}}} }g}|D]m}t)t||Dcgc](\}}||j+|t,j.z*c}}t,j.}|j1||fot3|Scc}wcc} } wcc} wcc} } wcc}}} wcc}}wcc}}} wcc}}w)a Return spline of degree *d*, passing through the given *X* and *Y* values. Explanation =========== This function returns a piecewise function such that each part is a polynomial of degree not greater than *d*. The value of *d* must be 1 or greater and the values of *X* must be strictly increasing. Examples ======== >>> from sympy import interpolating_spline >>> from sympy.abc import x >>> interpolating_spline(1, x, [1, 2, 4, 7], [3, 6, 5, 7]) Piecewise((3*x, (x >= 1) & (x <= 2)), (7 - x/2, (x >= 2) & (x <= 4)), (2*x/3 + 7/3, (x >= 4) & (x <= 7))) >>> interpolating_spline(3, x, [-2, 0, 1, 3, 4], [4, 2, 1, 1, 3]) Piecewise((7*x**3/117 + 7*x**2/117 - 131*x/117 + 2, (x >= -2) & (x <= 1)), (10*x**3/117 - 2*x**2/117 - 122*x/117 + 77/39, (x >= 1) & (x <= 4))) Parameters ========== d : integer Degree of Bspline strictly greater than equal to one x : symbol X : list of strictly increasing real values list of X coordinates through which the spline passes Y : list of real values list of corresponding Y coordinates through which the spline passes See Also ======== bspline_basis_set, interpolating_poly r)linsolve)Matrixz1Spline degree must be a positive integer, not %s.z/Number of X and Y coordinates must be the same.r@z6Degree must be less than the number of control points.c3,K|] \}}||kywr<)r=rrs rr?z'interpolating_spline..9s/Aq1u/sNz.The x-coordinates must be strictly increasing.r rzc0:{})clsTct|Sr<)r)r&rs rz&interpolating_spline..Ss Q r)key)sympy.solvers.solvesetrVsympy.matrices.denserWr is_Integer is_positiverCrallzipis_oddr rTsubsrformatrrsortedsumgetrrr#r)r(rXYrVrWr1jinterior_knotsrrrHbasisvrPcoeffer& intervals basis_dictssplinepieces ` rinterpolating_splinervs\0+  A LLQ]]LqPQQ 1vQJKK 1vA~QRR /Q!"/ /IJJA xx UqL1aR F"%aQBFmQq1ur]"C !QQUAI  qTFa!e tN3 3qugQ6G GE a *E0121 &1!&&A, &2A2 fQi+WW^^CF5KQV-W XE KNE!DDqDfq!!t)DDIDy&:;I8=>>1qvv.VaAqD.>K> F "03E;0G Hfq!Qq!&&! ! H!&&   uaj! " f C   '2E /> IsN+J(<J- J8J3 *J8J>!J>K  K"K -K 3J8K N) sympy.corerrsympy.core.symbolrrsympy.functionsrrsympy.logic.boolalgr sympy.sets.setsr functoolsr rr7rFrTrvrYrrr}sK!.5#$ 78v 3t&t&n1Lh\r