
    Ed'                         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dS )    )Ssympify)Dummysymbols)	Piecewisepiecewise_fold)And)Interval)	lru_cachec                     t          | t                    r?t          | 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       @lib/python3.11/site-packages/sympy/functions/special/bsplines.py_ivlr   
   sn     $ TY1!4 y15A: 	aqAuae|
.5
6
66    c                    t           j        || fv rt          ||z            }nUt           j        ||fv rt          | |z            }n1g }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     r ||k    r|	                    |           |	|=  n]|	                    ||f           |
                    |	           |	                    d           t          |ddi}|                                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    	v"a 32AF##	
B7	 12AF##AF##AF## bgcrcl## 73B3< 	* 	*C8D8DqMM!$E %V,,  4		#'q>> D= EMDq	Eu_ E)9  OOD)))q	E OOT4L)))) 	 		"""151199;;r      )maxsizec           	         |}t                      }t          d |D                       }t          |           } t          |          }t          |          }|dz
  }|| z   dz   |k    rt	          d          | dk    rMt          t          j        t          ||         ||dz                      	                    |          fd          }n| dk    r||| z   dz            ||dz            z
  }|t          j
        k    r-||| z   dz            |z
  |z  }	t          | dz
  ||dz   |          }
nt          j
        x}
}	||| z            ||         z
  }|t          j
        k    r$|||         z
  |z  }t          | dz
  |||          }nt          j
        x}}t          |||	|
|          }nt	          d|z            |                    ||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   4   K   | ]}t          |          V  d S Nr   ).0ks     r   	<genexpr>z bspline_basis.<locals>.<genexpr>   s(      ,,'!**,,,,,,r      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   rG   rG   T   s   f DA,,e,,,,,EAAAA%jjGA+K1uqy; ECDDDAv @UHU1XuQU|44==a@@A9
 
 
Q @a!eai 5Q</AF? 	q1uqy!A%.Aq1ueQUA66BBVOBa!euQx'AF? 	U1X&Aq1ueQ22BBVOBaQA..:Q>??? ??At9%%%r   c                 l     t                     z
  dz
  } fdt          |          D             S )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

    rA   c                 N    g | ]!}t          t                    |          "S  )rG   rB   )r>   r1   r(   rI   r   s     r   
<listcomp>z%bspline_basis_set.<locals>.<listcomp>   s-    KKKQM!U5\\1a00KKKr   )r   range)r(   rI   r   	n_spliness   ``` r   bspline_basis_setrX      sD    ` E

Q"IKKKKKK%	:J:JKKKKr   c           
      ,   ddl m} ddlm} t	          |           } | j        r| j        st          d| z            t          |          t          |          k    rt          d          t          |          | dz   k     rt          d          t          d t          ||dd	                   D                       st          d
          d |D             }| j        r| dz   dz  }|||          }n7| dz  }d t          ||| dz
           ||dz   |                    D             }|d         g| dz   z  t          |          z   |d         g| dz   z  z   }t          | |          fd|D             }	 | ||	           ||          ft          d                    t          |                    t                               }
t          |
          d         }
d D             }fd|D             }t          ||          }t#          |d           }d |D             }d D             }g }|D ]Mt%          fdt          |
|          D             t&          j                  }|                    |f           Nt-          | S )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 integer values
        list of X coordinates through which the spline passes

    Y : list of strictly increasing integer values
        list of 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.rA   z6Degree must be less than the number of control points.c              3   (   K   | ]\  }}||k     V  d S r<   rT   r>   r   r   s      r   r@   z'interpolating_spline.<locals>.<genexpr>9  s*      //Aq1u//////r   Nz.The x-coordinates must be strictly increasing.c                 ,    g | ]}t          |          S rT   r=   )r>   r1   s     r   rU   z(interpolating_spline.<locals>.<listcomp>;  s    r   r   c                 $    g | ]\  }}||z   d z  S )r   rT   r]   s      r   rU   z(interpolating_spline.<locals>.<listcomp>C  s1     
 
 
!QQUAI
 
 
r   r   c                 0    g | ]fd D             S )c                 <    g | ]}|                               S rT   )subs)r>   r   vr   s     r   rU   z3interpolating_spline.<locals>.<listcomp>.<listcomp>K  s%    	&	&	&1!&&A,,	&	&	&r   rT   )r>   rc   basisr   s    @r   rU   z(interpolating_spline.<locals>.<listcomp>K  s2    2221	&	&	&	&	&	&	&	&222r   zc0:{})clsc                 8    h | ]}|j         D ]\  }}|d k    |S )Tr   )r>   r   er&   s       r   	<setcomp>z'interpolating_spline.<locals>.<setcomp>O  s4    DDDqDDfq!!t)DDDDDr   c                 0    g | ]}t          |          S rT   )r   )r>   r&   r   s     r   rU   z(interpolating_spline.<locals>.<listcomp>S  s!    ***1DAJJ***r   c                     | d         S )Nr   rT   )r   s    r   <lambda>z&interpolating_spline.<locals>.<lambda>U  s
    AaD r   )keyc                     g | ]\  }}|S rT   rT   )r>   r   ys      r   rU   z(interpolating_spline.<locals>.<listcomp>V  s    ###tq!###r   c                 0    g | ]}d  |j         D             S )c                     i | ]\  }}||	S rT   rT   )r>   rh   r&   s      r   
<dictcomp>z3interpolating_spline.<locals>.<listcomp>.<dictcomp>X  s    ...VaAq...r   rg   )r>   r   s     r   rU   z(interpolating_spline.<locals>.<listcomp>X  s)    >>>1..qv...>>>r   c                 \    g | ](\  }}||                     t          j                  z  )S rT   )getr   r   )r>   r&   r(   r1   s      r   rU   z(interpolating_spline.<locals>.<listcomp>\  s2    HHHfq!Qq!&!!!HHHr   )sympy.solvers.solvesetrZ   sympy.matrices.denser[   r   
is_Integeris_positiverD   r   allzipis_oddr    rX   r   formatr   sortedsumr   r   r#   r   )r(   r   XYrZ   r[   jinterior_knotsrI   rQ   coeff	intervalsivalcombasis_dictssplinepiecerd   r1   s    `               @@r   interpolating_spliner      s   \ 0/////++++++ 	

AL RQ] RLqPQQQ
1vvQ LJKKK
1vvA~ SQRRR//Q!""///// KIJJJQA 	x 
UqL1aR4F
 
"%aQBF
mQq1urz]"C"C
 
 
 qTFa!etN333qugQ6GGEa**E22222222AHffQii+WW^^CFF5K5KQV-W-W-WXXEKKNEDDEDDDI +***	***D
dI

C
..
)
)
)C##s###I>>>>>KF " "HHHHE;0G0GHHH!&
 
 	uaj!!!!fr   N)
sympy.corer   r   sympy.core.symbolr   r   sympy.functionsr   r   sympy.logic.boolalgr	   sympy.sets.setsr
   	functoolsr   r   r7   rG   rX   r   rT   r   r   <module>r      s    ! ! ! ! ! ! ! ! . . . . . . . . 5 5 5 5 5 5 5 5 # # # # # # $ $ $ $ $ $      7 7 78 8 8v 3t& t& t&n1L 1L 1Lh_ _ _ _ _r   