def xspline_integrate(): import numpy as np import scipy.integrate as integrate from xspline.__init__ import xspline ok = True # setup test problem # ------------------------------------------------------------------------- knots = np.linspace(0.0, 1.0, 5) degree = 1 bs = xspline(knots, degree, l_linear=True, r_linear=True) # test integrate function # ------------------------------------------------------------------------- N = 3 x = np.linspace(-1.0, 2.0, N) a = np.repeat(-1.0, N) my_If = bs.splineIF(a, x, 0, 2) def func(x): return bs.splineIF(-1.0, x, 0, 1) tr_If = np.array([integrate.quad(func, a[i], x[i])[0] for i in range(N)]) tol = 1e-8 ok = ok and np.linalg.norm(my_If - tr_If) < tol if not ok: print('tr_If', tr_If) print('my_If', my_If) return ok
def xspline_intgLinear(): import numpy as np from xspline.__init__ import xspline ok = True # setup test problem # ------------------------------------------------------------------------- knots = np.linspace(0.0, 1.0, 3) degree = 1 bs = xspline(knots, degree) # test intgLinear function # ------------------------------------------------------------------------- a = np.random.rand(20) x = a + 1.0 slope = 2.0 intercept_at_0 = 1.0 my_if = bs.intgLinear(a, x, 1, 0.0, intercept_at_0, slope) tr_if = 0.5*slope*(x**2 - a**2) + intercept_at_0*(x - a) tol = 1e-10 ok = ok and np.linalg.norm(my_if - tr_if) < tol if not ok: print('tr_if', tr_if) print('my_if', my_if) return ok
def xspline_lastDMat(): import numpy as np from xspline.__init__ import xspline ok = True # setup test problem # ------------------------------------------------------------------------- knots = np.linspace(0.0, 1.0, 5) degree = 3 bs = xspline(knots, degree, l_linear=True, r_linear=True) # test lastDMat function # ------------------------------------------------------------------------- my_dmat = bs.lastDMat() x = np.array( [0.5 * (knots[i] + knots[i + 1]) for i in range(bs.num_knots - 1)]) tr_dmat = bs.designDMat(x[1:-1], degree) tr_dmat = np.vstack((bs.designDMat(x[0:1], 1), tr_dmat)) tr_dmat = np.vstack((tr_dmat, bs.designDMat(x[-1:], 1))) tol = 1e-10 ok = ok and np.linalg.norm(my_dmat - tr_dmat) < tol return ok
def xspline_designIMat(): import numpy as np from xspline.__init__ import xspline ok = True # setup test problem # ------------------------------------------------------------------------- knots = np.linspace(0.0, 1.0, 3) degree = 1 tol = 1e-10 # test designIMat function left linear # ------------------------------------------------------------------------- bs = xspline(knots, degree, l_linear=True) a = np.zeros(201) x = np.linspace(0.0, 1.0, 201) my_imat = bs.designIMat(a, x, 1) tr_imat = np.zeros((x.size, 2)) tr_imat[:, 0] = 1.0 - (x - 1.0)**2 tr_imat[:, 1] = x**2 - x ok = ok and np.linalg.norm(my_imat - tr_imat) < tol if not ok: print('xspline_designIMat: l_linear error.') # test designIMat function right linear # ------------------------------------------------------------------------- bs = xspline(knots, degree, r_linear=True) a = np.zeros(201) x = np.linspace(0.0, 1.0, 201) my_imat = bs.designIMat(a, x, 1) tr_imat = np.zeros((x.size, 2)) tr_imat[:, 0] = x - x**2 tr_imat[:, 1] = x**2 ok = ok and np.linalg.norm(my_imat - tr_imat) < tol if not ok: print('xspline_designIMat: r_linear error.') return ok
def xspline_splineDF(): import numpy as np from xspline.__init__ import xspline ok = True # setup test problem # ------------------------------------------------------------------------- knots = np.linspace(0.0, 1.0, 3) degree = 1 tol = 1e-10 # test splineDF function left linear without extrapolation # ------------------------------------------------------------------------- bs = xspline(knots, degree, l_linear=True) x = np.linspace(-1.0, 1.0, 201) my_Df = bs.splineDF(x, 0, 1, l_extra=False) valid_index = (x >= bs.knots[0]) & (x <= bs.knots[-1]) tr_Df = np.zeros(x.size) tr_Df[valid_index] = -2.0 ok = ok and np.linalg.norm(my_Df - tr_Df) < tol if not ok: print('xspline_splineDF: l_linear error.') # test splineDF function left linear extrapolation # ------------------------------------------------------------------------- x = np.linspace(-1.0, 0.0, 101) my_Df = bs.splineDF(x, 0, 1, l_extra=True) tr_Df = -2.0 * np.ones(x.size) ok = ok and np.linalg.norm(my_Df - tr_Df) < tol if not ok: print('xspline_splineDF: l_linear and l_extra error.') # test splineDF function right linear without extrapolation # ------------------------------------------------------------------------- bs = xspline(knots, degree, r_linear=True) x = np.linspace(0.0, 2.0, 201) my_Df = bs.splineDF(x, 1, 1, r_extra=False) valid_index = (x >= bs.knots[0]) & (x <= bs.knots[-1]) tr_Df = np.zeros(x.size) tr_Df[valid_index] = 2.0 ok = ok and np.linalg.norm(my_Df - tr_Df) < tol if not ok: print('xspline_splineDF: r_linear error.') # test splineDF function right linear extrapolation # ------------------------------------------------------------------------- x = np.linspace(1.0, 2.0, 101) my_Df = bs.splineDF(x, 1, 1, r_extra=True) tr_Df = 2.0 * np.ones(x.size) ok = ok and np.linalg.norm(my_Df - tr_Df) < tol if not ok: print('xspline_splineDF: r_linear and r_extra error.') return ok