
    sg'                         d dl mZmZ d dlmZmZ d dlmZmZ d dl	m
Z
 d dlmZ d dlmZ d Zd Z ed	
      d        Zd Zd Zy)    )Ssympify)Dummysymbols)	Piecewisepiecewise_fold)And)Interval)	lru_cachec                     t        | t              rRt        | j                        dk(  r:| j                  \  }}|j                  |k(  r||}}|j                  |j
                  fS t        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).
       zunexpected cond type: %s)
isinstancer	   lenargsltsgts	TypeError)condxabs       S/var/www/html/venv/lib/python3.12/site-packages/sympy/functions/special/bsplines.py_ivlr   
   sa     $TYY1!4yy155A:aqAuuaee|
.5
66    c                     t         j                  || fv rt        ||z        }|j                         S t         j                  ||fv rt        | |z        }|j                         S g }t        | |z        }t        ||z        }t        |j                  dd       }	|j                  dd D ]  }
|
j
                  }|
j                  }t        ||      d   }t        |	      D ]\  \  }}|j
                  }|j                  }t        ||      \  }}||k(  r
||z  }|	|=  n#||k  sB||k  sH|j                  |       |	|=  n |j                  ||f        |j                  |	       |j                  d       t        |ddi}|j                         S )zConstruct c*b1 + d*b2.Nr   r   TevaluateF)r   Zeror   listr   exprr   r   	enumerateappendextendr   expand)cb1db2r   rvnew_argsp1p2p2argsargr!   r   loweriarg2expr2cond2lower_2upper_2s                       r   _add_splinesr7      s    	vv"aAF#h 99;g 
B7	AF#d 99;a AF#AF# bggcrl# 773B< 	*C88D88DqM!$E %V, 4				#'q> D=EMDq	u_E)9 OOD)q	%* OOT4L)9	*> 	 		"15199;r      )maxsizec           	         |}t               }t        d |D              }t        |       } t        |      }t        |      }|dz
  }|| z   dz   |kD  rt	        d      | dk(  r?t        t        j                  t        ||   ||dz            j                  |      fd      }n| dkD  r||| z   dz      ||dz      z
  }|t        j                  k7  r&||| z   dz      |z
  |z  }	t        | dz
  ||dz   |      }
nt        j                  x}
}	||| z      ||   z
  }|t        j                  k7  r|||   z
  |z  }t        | dz
  |||      }nt        j                  x}}t        |||	|
|      }nt	        d|z        |j                  ||i      S )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

    c              3   2   K   | ]  }t        |        y wN)r   ).0ks     r   	<genexpr>z bspline_basis.<locals>.<genexpr>   s     ,'!*,s      z(n + d + 1 must not exceed len(knots) - 1r   r   zdegree must be non-negative: %r)r   tupleintr   
ValueErrorr   r   Oner
   containsr   bspline_basisr7   xreplace)r(   knotsnr   xvarn_knotsn_intervalsresultdenomBr)   Ar'   s                r   rF   rF   T   s   f DA,e,,EAAAA%jGA+K1uqy;CDDAvUUHU1XuQU|4==a@A9
 
Qa!eai 5Q</AFF?q1uqy!A%.Aq1ueQUA6BVVOBa!euQx'AFF?U1X&Aq1ueQ2BVVOBaQA.:Q>?? ??At9%%r   c           	          t        |      | z
  dz
  }t        |      D cg c]  }t        | t        |      ||       c}S c c}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@   )r   rangerF   rA   )r(   rH   r   	n_splinesr1   s        r   bspline_basis_setrT      s?    ` E
Q"I:?	:JKQM!U5\1a0KKKs   A c                 2   ddl m} ddlm} t	        |       } | j
                  r| j                  st        d| z        t        |      t        |      k7  rt        d      t        |      | dz   k  rt        d      t        d t        ||dd	       D              st        d
      |D cg c]  }t	        |       }}| j                  r| dz   dz  }|||  }n7| dz  }t        ||| dz
   ||dz   |        D 	
cg c]  \  }	}
|	|
z   dz   }}	}
|d   g| dz   z  t        |      z   |d   g| dz   z  z   }t        | ||      }|D 
cg c]"  }|D 
cg c]  }
|
j                  ||       c}
$ }}}
 | ||       ||      ft        dj!                  t        |            t"                    }t        |      d   }|D 
ch c]  }
|
j$                  D ]  \  }}|dk7  s|   }}}
}|D cg c]  }t'        ||       }}t        ||      }t)        |d       }|D cg c]  \  }}|	 }}}|D 
cg c]"  }
|
j$                  D ci c]  \  }}||
 c}}$ }}}
}g }|D ]m  }t+        t        ||      D  cg c](  \  }} || j-                  |t.        j0                        z  * c} }t.        j0                        }|j3                  ||f       o t5        | S c c}w c c}
}	w c c}
w c c}
}w c c}}}
w c c}w c c}}w c c}}w c c}}}
w c c} }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.c              3   ,   K   | ]  \  }}||k    y wr<    )r=   r   r   s      r   r?   z'interpolating_spline.<locals>.<genexpr>9  s     /Aq1u/s   Nz.The x-coordinates must be strictly increasing.r   r   zc0:{})clsTc                     | d   S )Nr   rY   )r   s    r   <lambda>z&interpolating_spline.<locals>.<lambda>U  s
    AaD r   )key)sympy.solvers.solvesetrV   sympy.matrices.denserW   r   
is_Integeris_positiverC   r   allzipis_oddr    rT   subsr   formatr   r   r   sortedsumgetr   r   r#   r   )r(   r   XYrV   rW   r1   jinterior_knotsr   r   rH   basisvrP   coeffer&   	intervalsivalcomybasis_dictssplinepieces                            r   interpolating_splinery      s   \ 0+ 	
ALLQ]]LqPQQ
1vQJKK
1vA~QRR/Q!"//IJJA 	xxUqL1aRF"%aQBFmQq1ur]"C
!QQUAI
 
 qTFa!etN33qugQ6GGEa*E0121	&1!&&A,	&2A2fQi+WW^^CF5KQV-WXEKNE!DDqDfq!!t)DDID !**1DAJ*D*
dI
C
.
)C"#tq!#I#8=>>1qvv.VaAqD.>K>F "03E;0GHfq!Qq!&&!!H!&&
 	uaj!	"
 fI 	 
 
'2 E + $.> IsZ   *K;K#	K.K))K.K4 K4.K;"L 7LLL=-L)K.LN)
sympy.corer   r   sympy.core.symbolr   r   sympy.functionsr   r   sympy.logic.boolalgr	   sympy.sets.setsr
   	functoolsr   r   r7   rF   rT   ry   rY   r   r   <module>r      sK    ! . 5 # $ 78v 3t& t&n1Lh_r   