<s> from __future__ import print_function , division <EOL> from sympy . core import S , sympify <EOL> from sympy . core . compatibility import range <EOL> from sympy . functions import Piecewise , piecewise_fold <EOL> from sympy . sets . sets import Interval <EOL> def _add_splines ( c , b1 , d , b2 ) : <EOL> """<STR_LIT>""" <EOL> if b1 == S . Zero or c == S . Zero : <EOL> rv = piecewise_fold ( d * b2 ) <EOL> elif b2 == S . Zero or d == S . Zero : <EOL> rv = piecewise_fold ( c * b1 ) <EOL> else : <EOL> new_args = [ ] <EOL> n_intervals = len ( b1 . args ) <EOL> if n_intervals != len ( b2 . args ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> new_args . append ( ( c * b1 . args [ <NUM_LIT:0> ] . expr , b1 . args [ <NUM_LIT:0> ] . cond ) ) <EOL> for i in range ( <NUM_LIT:1> , n_intervals - <NUM_LIT:1> ) : <EOL> new_args . append ( ( <EOL> c * b1 . args [ i ] . expr + d * b2 . args [ i - <NUM_LIT:1> ] . expr , <EOL> b1 . args [ i ] . cond <EOL> ) ) <EOL> new_args . append ( ( d * b2 . args [ - <NUM_LIT:2> ] . expr , b2 . args [ - <NUM_LIT:2> ] . cond ) ) <EOL> new_args . append ( b2 . args [ - <NUM_LIT:1> ] ) <EOL> rv = Piecewise ( * new_args ) <EOL> return rv . expand ( ) <EOL> def bspline_basis ( d , knots , n , x , close = True ) : <EOL> """<STR_LIT>""" <EOL> knots = [ sympify ( k ) for k in knots ] <EOL> d = int ( d ) <EOL> n = int ( n ) <EOL> n_knots = len ( knots ) <EOL> n_intervals = n_knots - <NUM_LIT:1> <EOL> if n + d + <NUM_LIT:1> > n_intervals : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if d == <NUM_LIT:0> : <EOL> result = Piecewise ( <EOL> ( S . One , Interval ( knots [ n ] , knots [ n + <NUM_LIT:1> ] , False , <EOL> not close ) . contains ( x ) ) , <EOL> ( <NUM_LIT:0> , True ) <EOL> ) <EOL> elif d > <NUM_LIT:0> : <EOL> denom = knots [ n + d + <NUM_LIT:1> ] - knots [ n + <NUM_LIT:1> ] <EOL> if denom != S . Zero : <EOL> B = ( knots [ n + d + <NUM_LIT:1> ] - x ) / denom <EOL> b2 = bspline_basis ( d - <NUM_LIT:1> , knots , n + <NUM_LIT:1> , x , close ) <EOL> else : <EOL> b2 = B = S . Zero <EOL> denom = knots [ n + d ] - knots [ n ] <EOL> if denom != S . Zero : <EOL> A = ( x - knots [ n ] ) / denom <EOL> b1 = bspline_basis ( <EOL> d - <NUM_LIT:1> , knots , n , x , close and ( B == S . Zero or b2 == S . Zero ) ) <EOL> else : <EOL> b1 = A = S . Zero <EOL> result = _add_splines ( A , b1 , B , b2 ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % n ) <EOL> return result <EOL> def bspline_basis_set ( d , knots , x ) : <EOL> """<STR_LIT>""" <EOL> n_splines = len ( knots ) - d - <NUM_LIT:1> <EOL> return [ bspline_basis ( d , knots , i , x ) for i in range ( n_splines ) ] </s>
